public void TestPOP() { 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); POP pop = new POP(generalRegistersMock, pointerRegistersMock, segmentRegistersMock); 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(pop); registryCommand.AddHandler(push); registryCommand.AddHandler(xchg); registryCommand.AddHandler(mov); registryCommand.AddHandler(assignTo); byte[] word; registryCommand.InputCommand("ax 1122"); registryCommand.InputCommand("push ax"); registryCommand.InputCommand("pop cx"); word = generalRegistersMock.GetRegistry("CX"); Assert.AreEqual(34, word[0]); Assert.AreEqual(17, word[1]); Assert.AreEqual("Value 1122h from physical address 0h popped from stack to registry CX.\r\n", loggerMock.outputResult); word = pointerRegistersMock.GetRegistry("SP"); Assert.AreEqual(0, word[0]); registryCommand.InputCommand("ax FFEE"); registryCommand.InputCommand("bx AA99"); registryCommand.InputCommand("SS FF"); registryCommand.InputCommand("push ax"); registryCommand.InputCommand("push bx"); registryCommand.InputCommand("pop dx"); word = generalRegistersMock.GetRegistry("DX"); Assert.AreEqual(153, word[0]); Assert.AreEqual(170, word[1]); Assert.AreEqual("Value AA99h from physical address FF2h popped from stack to registry DX.\r\n", loggerMock.outputResult); word = pointerRegistersMock.GetRegistry("SP"); Assert.AreEqual(2, word[0]); }
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 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); }
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 TestMemory() //MOV AL, BH { MemoryModel.SetAddressBusLength = 20; MemoryModel memory = MemoryModel.GetInstance(); string addressHexMock = "FFFFE"; uint.TryParse(addressHexMock, System.Globalization.NumberStyles.HexNumber, null, out uint result); memory.SetMemoryWord(result, new byte[] { 255, 255 }); byte cell = memory.GetMemoryCell(result); Assert.AreEqual(cell, 255); cell = memory.GetMemoryCell(result + 1); Assert.AreEqual(cell, 255); cell = memory.GetMemoryCell(result - 2); Assert.AreEqual(cell, 0); MemoryModel.SetAddressBusLength = 0; }
private void PushOnStack(string sourcedRegistryName) { const int TO_20BIT_SHIFT = 4; if (sourcedRegistryName.EndsWith('H') || sourcedRegistryName.EndsWith('L')) { outputLogBuilder.AppendLine($"Cannot use command PUSH for half registers."); return; } if (!TryGetRegistry(sourcedRegistryName, out RegistersController sourceContainer)) { outputLogBuilder.AppendLine($"{sourcedRegistryName} 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")); UInt16 stackPointer = BitConverter.ToUInt16(stackPointerContainer.GetRegistry("SP")); uint physicalAddress = (uint)(stackSegment << TO_20BIT_SHIFT) + stackPointer; byte[] wordToPush = sourceContainer.GetRegistry(sourcedRegistryName); memory.SetMemoryWord(physicalAddress, wordToPush); stackPointer += 2; stackPointerContainer.SetBytesToRegistry("SP", BitConverter.GetBytes(stackPointer)); outputLogBuilder.AppendLine($"Value {BitConverter.ToUInt16(wordToPush).ToString("X")}h from registry {sourcedRegistryName} pushed on stack with physical address {physicalAddress.ToString("X")}h."); }
public MainWindow() { InitializeComponent(); MemoryModel.SetAddressBusLength = 20; MemoryModel.GetInstance().AddObserver(this); memoryView = new MemoryView(); Go_Click(null, null); generalPurposeRegisters = new GeneralPurposeRegisters(); indexRegisters = new IndexRegisters(); pointerRegisters = new PointerRegisters(); segmentRegisters = new SegmentRegisters(); commandInterpreter = InitDefaultRegistryCommander(); generalPurposeRegistersView = new GeneralPurposeRegistersView(new HexParser()); indexRegistersView = new IndexRegistersView(new HexParser()); pointerRegistersView = new PointerRegistersView(new HexParser()); segmentRegistersView = new SegmentRegistersView(new HexParser()); if (generalPurposeRegisters is Observable observableGeneralPurposeReg) { observableGeneralPurposeReg.AddObserver(generalPurposeRegistersView); } if (indexRegisters is Observable observableIndexReg) { observableIndexReg.AddObserver(indexRegistersView); } if (pointerRegisters is Observable observablePointersReg) { observablePointersReg.AddObserver(pointerRegistersView); } if (segmentRegisters is Observable observableSegmentsReg) { observableSegmentsReg.AddObserver(segmentRegistersView); } SetBlockDataContext(); }
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; }