public void TestPUSH() { MemoryModel.SetAddressBusLength = 20; MemoryModel memory = MemoryModel.GetInstance(); GeneralPurposeRegisters generalRegistersMock = new GeneralPurposeRegisters(); SegmentRegisters segmentRegistersMock = new SegmentRegisters(); PointerRegisters pointerRegistersMock = new PointerRegisters(); LoggerMock loggerMock = new LoggerMock(); RegistryCommander registryCommand = new RegistryCommander(loggerMock); PUSH push = new PUSH(generalRegistersMock, pointerRegistersMock, segmentRegistersMock); XCHG xchg = new XCHG(generalRegistersMock, segmentRegistersMock); MOV mov = new MOV(generalRegistersMock, segmentRegistersMock); AssignToRegistry assignTo = new AssignToRegistry(generalRegistersMock, pointerRegistersMock, segmentRegistersMock); registryCommand.AddHandler(push); registryCommand.AddHandler(xchg); registryCommand.AddHandler(mov); registryCommand.AddHandler(assignTo); byte[] word; registryCommand.InputCommand("ax 1122"); registryCommand.InputCommand("push ax"); word = memory.GetMemoryWord(0); Assert.AreEqual(word[0], 34); Assert.AreEqual(word[1], 17); Assert.AreEqual("Value 1122h from registry AX pushed on stack with physical address 0h.\r\n", loggerMock.outputResult); word = pointerRegistersMock.GetRegistry("SP"); Assert.AreEqual(word[0], 2); registryCommand.InputCommand("ax FFEE"); registryCommand.InputCommand("bx AA99"); registryCommand.InputCommand("SS FF"); registryCommand.InputCommand("push ax"); registryCommand.InputCommand("push bx"); word = memory.GetMemoryWord(4083); Assert.AreEqual(word[0], 255); Assert.AreEqual(word[1], 153); Assert.AreEqual("Value AA99h from registry BX pushed on stack with physical address FF4h.\r\n", loggerMock.outputResult); word = pointerRegistersMock.GetRegistry("SP"); Assert.AreEqual(word[0], 6); }
private void PopFromStack(string destinatedRegistryName) { const int TO_20BIT_SHIFT = 4; if (destinatedRegistryName.EndsWith('H') || destinatedRegistryName.EndsWith('L')) { outputLogBuilder.AppendLine($"Cannot use command POP for half registers."); return; } if (!TryGetRegistry(destinatedRegistryName, out RegistersController destinatedContainer)) { outputLogBuilder.AppendLine($"{destinatedRegistryName} is unsupported registry name."); return; } if (!TryGetRegistry("SP", out RegistersController stackPointerContainer)) { outputLogBuilder.AppendLine("Command interpreter couldn't find SP registry."); return; } if (!TryGetRegistry("SS", out RegistersController stackSegmentContainer)) { outputLogBuilder.AppendLine("Command interpreter couldn't find SS registry."); return; } MemoryModel memory = MemoryModel.GetInstance(); UInt16 stackSegment = BitConverter.ToUInt16(stackSegmentContainer.GetRegistry("SS")); Int16 stackPointer = BitConverter.ToInt16(stackPointerContainer.GetRegistry("SP")); stackPointer -= 2; if (stackPointer < 0) { outputLogBuilder.AppendLine($"The stack is empty, cannot perform operation."); return; } stackPointerContainer.SetBytesToRegistry("SP", BitConverter.GetBytes(stackPointer)); uint physicalAddress = (uint)(stackSegment << TO_20BIT_SHIFT) + (UInt16)stackPointer; byte[] wordToPop = memory.GetMemoryWord(physicalAddress); destinatedContainer.SetBytesToRegistry(destinatedRegistryName, wordToPop); //memory.SetMemoryWord(physicalAddress, 0); outputLogBuilder.AppendLine($"Value {BitConverter.ToUInt16(wordToPop).ToString("X")}h from physical address {physicalAddress.ToString("X")}h popped from stack to registry {destinatedRegistryName}."); }
public void TestXCHGMemoryRegistry() { MemoryModel.SetAddressBusLength = 20; MemoryModel memory = MemoryModel.GetInstance(); GeneralPurposeRegisters generalRegistersMock = new GeneralPurposeRegisters(); SegmentRegisters segmentRegistersMock = new SegmentRegisters(); LoggerMock loggerMock = new LoggerMock(); RegistryCommander registryCommand = new RegistryCommander(loggerMock); XCHG xchg = new XCHG(generalRegistersMock, segmentRegistersMock); MOV mov = new MOV(generalRegistersMock, segmentRegistersMock); AssignToRegistry assignTo = new AssignToRegistry(generalRegistersMock, segmentRegistersMock); registryCommand.AddHandler(xchg); registryCommand.AddHandler(mov); registryCommand.AddHandler(assignTo); registryCommand.InputCommand("MoV AL, FFh"); registryCommand.InputCommand("MoV [SI + 0], AL"); registryCommand.InputCommand("MoV AL, 1"); registryCommand.InputCommand("XCHG [0], AL"); byte bytetmp = memory.GetMemoryCell(0); Assert.AreEqual(bytetmp, 1); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"0\" as decimal.\r\nConverting arguments into effective address 0h.\r\nValue 1h from registry AL exchanged with FF at physical address 0h.\r\n"); bytetmp = generalRegistersMock.GetRegistry("AL")[0]; Assert.AreEqual(bytetmp, 255); segmentRegistersMock.SetBytesToRegistry("DS", 255); registryCommand.InputCommand("MoV CX, FF11h"); registryCommand.InputCommand("MoV [SI + BP + FFh], CX"); registryCommand.InputCommand("MoV CX, 2233h"); registryCommand.InputCommand("XCHG CX, [100h]"); byte[] word = generalRegistersMock.GetRegistry("CX"); Assert.AreEqual(word[0], 255); Assert.AreEqual(word[1], 0); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"100\" as hexadecimal.\r\nConverting arguments into effective address 100h.\r\nValue 2233h from registry CX exchanged with FF at physical address 10F0h.\r\n"); word = memory.GetMemoryWord(4336); Assert.AreEqual(word[0], 51); Assert.AreEqual(word[1], 34); MemoryModel.SetAddressBusLength = 0; }
public void TestFromToRegistryMemory() { MemoryModel.SetAddressBusLength = 20; MemoryModel memory = MemoryModel.GetInstance(); GeneralPurposeRegisters generalRegistersMock = new GeneralPurposeRegisters(); SegmentRegisters segmentRegistersMock = new SegmentRegisters(); LoggerMock loggerMock = new LoggerMock(); RegistryCommander registryCommand = new RegistryCommander(loggerMock); XCHG xchg = new XCHG(generalRegistersMock, segmentRegistersMock); MOV mov = new MOV(generalRegistersMock, segmentRegistersMock); AssignToRegistry assignTo = new AssignToRegistry(generalRegistersMock, segmentRegistersMock); registryCommand.AddHandler(xchg); registryCommand.AddHandler(mov); registryCommand.AddHandler(assignTo); registryCommand.InputCommand("MoV AL, 255"); registryCommand.InputCommand("MoV [0], AL"); byte bytetmp = memory.GetMemoryCell(0); Assert.AreEqual(bytetmp, 255); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"0\" as decimal.\r\nConverting arguments into effective address 0h.\r\nValue FFh from registry AL assigned to physical address 0h.\r\n"); segmentRegistersMock.SetBytesToRegistry("DS", 255); registryCommand.InputCommand("MoV AH, AAh"); registryCommand.InputCommand("MoV [FFh], AH"); bytetmp = memory.GetMemoryCell(4335); Assert.AreEqual(bytetmp, 170); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"FF\" as hexadecimal.\r\nConverting arguments into effective address FFh.\r\nValue AAh from registry AH assigned to physical address 10EFh.\r\n"); segmentRegistersMock.SetBytesToRegistry("DS", 15); registryCommand.InputCommand("MoV [0h], AX"); byte[] word = memory.GetMemoryWord(240); Assert.AreEqual(word[0], 255); Assert.AreEqual(word[1], 170); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"0\" as hexadecimal.\r\nConverting arguments into effective address 0h.\r\nValue AAFFh from registry AX assigned to physical address F0h.\r\n"); segmentRegistersMock.SetBytesToRegistry("DS", 0); registryCommand.InputCommand("MoV AX, [0h]"); word = generalRegistersMock.GetRegistry("AX"); Assert.AreEqual(word[0], 255); Assert.AreEqual(word[1], 0); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"0\" as hexadecimal.\r\nConverting arguments into effective address 0h.\r\nValue FFh assigned to registry AX from physical address 0h.\r\n"); segmentRegistersMock.SetBytesToRegistry("DS", 255); registryCommand.InputCommand("MoV AX, [FEh]"); word = generalRegistersMock.GetRegistry("AX"); Assert.AreEqual(word[0], 0); Assert.AreEqual(word[1], 170); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"FE\" as hexadecimal.\r\nConverting arguments into effective address FEh.\r\nValue AA00h assigned to registry AX from physical address 10EEh.\r\n"); segmentRegistersMock.SetBytesToRegistry("DS", 15); registryCommand.InputCommand("MoV AX, [0h]"); word = generalRegistersMock.GetRegistry("AX"); Assert.AreEqual(word[0], 255); Assert.AreEqual(word[1], 170); Assert.AreEqual(loggerMock.outputResult, "Parsing value \"0\" as hexadecimal.\r\nConverting arguments into effective address 0h.\r\nValue AAFFh assigned to registry AX from physical address F0h.\r\n"); MemoryModel.SetAddressBusLength = 0; }