public void Test3() { // Arrange var instruction = new Byte2(-985); // Act var decodedInstruction = InstructionDecoder.Decode(instruction); // Assert decodedInstruction.ComputationInstruction.Should().BeTrue(); decodedInstruction.SourceMemory.Should().BeTrue(); decodedInstruction.Operation.zx.Should().BeTrue(); decodedInstruction.Operation.nx.Should().BeTrue(); decodedInstruction.Operation.zy.Should().BeFalse(); decodedInstruction.Operation.ny.Should().BeFalse(); decodedInstruction.Operation.Function.Should().BeFalse(); decodedInstruction.Operation.NegateOutput.Should().BeFalse(); decodedInstruction.Destination.A.Should().BeTrue(); decodedInstruction.Destination.D.Should().BeFalse(); decodedInstruction.Destination.Ram.Should().BeFalse(); decodedInstruction.Condition.GreaterThanZero.Should().BeTrue(); decodedInstruction.Condition.EqualToZero.Should().BeTrue(); decodedInstruction.Condition.LessThanZero.Should().BeTrue(); decodedInstruction.w.ToInt16().Should().Be(0); }
private void HandleINT() { // TODO: Check if INT is supposed to be able to fire during this push and other memory operations var opcode = AcknowledgeInterrupt(); if (Registers.InterruptMode == Z80InterruptMode.External) { var instr = InstructionDecoder.DecodeNextInstruction(this, opcode); instr.Execute(this, instr.InstructionBytes); } else if (Registers.InterruptMode == Z80InterruptMode.FixedAddress) { PushWord(Registers.PC); Registers.PC = 0x38; } else if (Registers.InterruptMode == Z80InterruptMode.Vectorized) { var upper = Registers.I; var lower = opcode; var addr = Utilities.LETo16Bit(lower, upper); PushWord(Registers.PC); var jumpAddr = ReadWord(addr); Registers.PC = jumpAddr; } }
public ControlUnitOutput Do(Byte2 data, bool clock) { var decodedInstruction = InstructionDecoder.Decode(data); var selectedSourceMemory = Select16.Do(decodedInstruction.SourceMemory, _memoryOutput.Ram, _memoryOutput.A); _aluOutput = ArithmeticLogicUnit.Do(decodedInstruction.Operation, _memoryOutput.D, selectedSourceMemory); _selectedBasedOnComputationInstruction = Select16.Do(decodedInstruction.ComputationInstruction, _aluOutput, decodedInstruction.w); _memoryOutput = _memory.Do(decodedInstruction.Destination, _selectedBasedOnComputationInstruction, clock); var isCondition = ArithmeticLogicUnit.Evaluate(decodedInstruction.Condition, _aluOutput); return(new ControlUnitOutput(isCondition, _memoryOutput.A)); }
public InferenceDisassembler(VMConstants constants, KoiStream koiStream) { Constants = constants ?? throw new ArgumentNullException(nameof(constants)); KoiStream = koiStream ?? throw new ArgumentNullException(nameof(koiStream)); _decoder = new InstructionDecoder(constants, KoiStream.Contents.CreateReader()); _processor = new InstructionProcessor(this); _cfgBuilder = new ControlFlowGraphBuilder(); }
public Machine(ILogger logger, IInputStream inputStream) { Memory = new MachineMemory(Stream.Null); StackFrames = new FrameCollection(logger); ObjectTable = new GameObjectTable(this); Decoder = new InstructionDecoder(this); Output = new CompositeOutputStream(new DebugOutputStream(logger)); Logger = logger.ForContext <Machine>(); Input = inputStream; }
public void DefinedCpuInstructions_AreCorrectlyDecoded() { var instructionDecoder = new InstructionDecoder(); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x00E0)), Is.InstanceOf <Instruction_00E0>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x00EE)), Is.InstanceOf <Instruction_00EE>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x1248)), Is.InstanceOf <Instruction_1nnn>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x2468)), Is.InstanceOf <Instruction_2nnn>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x3468)), Is.InstanceOf <Instruction_3xkk>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x4468)), Is.InstanceOf <Instruction_4xkk>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x5460)), Is.InstanceOf <Instruction_5xy0>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x6123)), Is.InstanceOf <Instruction_6xkk>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x7123)), Is.InstanceOf <Instruction_7xkk>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB0)), Is.InstanceOf <Instruction_8xy0>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB1)), Is.InstanceOf <Instruction_8xy1>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB2)), Is.InstanceOf <Instruction_8xy2>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB3)), Is.InstanceOf <Instruction_8xy3>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB4)), Is.InstanceOf <Instruction_8xy4>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB5)), Is.InstanceOf <Instruction_8xy5>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB6)), Is.InstanceOf <Instruction_8xy6>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8AB7)), Is.InstanceOf <Instruction_8xy7>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8ABE)), Is.InstanceOf <Instruction_8xyE>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x9460)), Is.InstanceOf <Instruction_9xy0>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xA123)), Is.InstanceOf <Instruction_Annn>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xB123)), Is.InstanceOf <Instruction_Bnnn>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xC123)), Is.InstanceOf <Instruction_Cxkk>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xD123)), Is.InstanceOf <Instruction_Dxyn>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xE19E)), Is.InstanceOf <Instruction_Ex9E>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xE1A1)), Is.InstanceOf <Instruction_ExA1>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xFA07)), Is.InstanceOf <Instruction_Fx07>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xF10A)), Is.InstanceOf <Instruction_Fx0A>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xFA15)), Is.InstanceOf <Instruction_Fx15>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xFA18)), Is.InstanceOf <Instruction_Fx18>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xFA1E)), Is.InstanceOf <Instruction_Fx1E>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xFA29)), Is.InstanceOf <Instruction_Fx29>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xFA33)), Is.InstanceOf <Instruction_Fx33>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xF155)), Is.InstanceOf <Instruction_Fx55>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xF165)), Is.InstanceOf <Instruction_Fx65>()); }
static void Decode() { int address = TEXT_SECTION_START; while (_memory.Read(address).Any(x => x != 0)) { byte[] bytes = _memory.Read(address); InstructionDecoder.PrintInstruction(bytes); address += 4; } }
public void EightBitOpcodeRegisterBInBits0To3(int register) { var decoder = new InstructionDecoder(); ForEachOpcode(EightBitOpcodes, opcode => { var encodedInstruction = (ushort)(opcode | (register << 0)); var decodedInstruction = decoder.Decode(encodedInstruction); decodedInstruction.RegisterB.Should().Be(register); }); }
private static TInstruction AssertBytesDecodedAs <TInstruction>(byte[] bytes) where TInstruction : IInstruction { var(instruction, bytesRead) = InstructionDecoder.Decode(bytes); Assert.IsType <TInstruction>(instruction); var loadInstruction = (TInstruction)instruction; //Whole message was read Assert.Equal(bytes.Length, bytesRead); return(loadInstruction); }
public Decode() { var instructions = new Instruction[] { new Add(), new Multiply(), new Halt() }; Decoder = new InstructionDecoder(instructions); }
public void FourBitOpcodeRegisterAInBits7To11(int register) { var decoder = new InstructionDecoder(); ForEachOpcode(FourBitOpcodes, opcode => { var encodedInstruction = (ushort)(opcode | (register << 8)); var decodedInstruction = decoder.Decode(encodedInstruction); decodedInstruction.RegisterA.Should().Be(register); }); }
void TestOpcodeParameter(IEnumerable <Opcode> opcodes, ushort rawValue, int expectedDecodedValue) { var decoder = new InstructionDecoder(); ForEachOpcode(opcodes, opcode => { var encodedInstruction = (ushort)(opcode | rawValue); var decodedInstruction = decoder.Decode(encodedInstruction); decodedInstruction.Value.Should().Be(expectedDecodedValue); }); }
public void UndefinedCpuInstructions_AreDecodedAsUndefinedInstructions() { var instructionDecoder = new InstructionDecoder(); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x0000)), Is.InstanceOf <UndefinedInstruction>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x5468)), Is.InstanceOf <UndefinedInstruction>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x8468)), Is.InstanceOf <UndefinedInstruction>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0x9468)), Is.InstanceOf <UndefinedInstruction>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xE101)), Is.InstanceOf <UndefinedInstruction>()); Assert.That(instructionDecoder.GetCpuInstruction(new DecodedInstruction(0xF1FF)), Is.InstanceOf <UndefinedInstruction>()); }
public void Init(ulong programCounter) { // Set the instruction decoder and type decoder instructionDecoder = new InstructionDecoder(EndianType.Little); typeDecoder = new TypeDecoder(); rvcDecoder = new RvcDecoder(architecture); InitDetails(programCounter); environment.ApplyOutputParameter(configuration.Debug, configuration.VerboseMode); isInitialized = true; }
public void For_data_instruction_W_returns_input() { // Arrange var instruction = new Byte2(17); // Act var decodedInstruction = InstructionDecoder.Decode(instruction); // Assert decodedInstruction.w.ToInt16().Should().Be(17); }
private void updateMemoryDisplay() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("address", typeof(string))); dt.Columns.Add(new DataColumn("value", typeof(string))); dt.Columns.Add(new DataColumn("instruction", typeof(String))); int start = _memoryDisplayStartAddress; int end = start + 1000; for (int address = start; address < end;) { if (_memoryDisplaySize == 4) { int machineCode = _vm.cpu.Memory.Get(address, false, 4); object[] firstRowData = new object[3]; firstRowData[0] = address; firstRowData[1] = machineCode; address += 4; Instruction instruction = InstructionDecoder.Decode(machineCode); if (instruction.HasImmediate) { instruction.Immediate = _vm.cpu.Memory.Get(address, false, 4); address += 4; } firstRowData[2] = instruction.ToString(); dt.Rows.Add(firstRowData); if (instruction.HasImmediate) { dt.Rows.Add(address - 4, instruction.Immediate, "[immediate value]"); } } else { int value = BitHelper.ExtractBytes(_vm.cpu.Memory.Get(address, false, 1), 1); dt.Rows.Add(address, value, ""); address++; } } memoryGridView.Invoke((MethodInvoker)(() => { memoryGridView.DataSource = dt; })); }
public void Decode_Works() { var instructionDecoder = new InstructionDecoder(); var decodedInstruction = instructionDecoder.Decode(0x12, 0x34); Assert.That(decodedInstruction.InstructionCode, Is.EqualTo(0x1234)); Assert.That(decodedInstruction.nnn, Is.EqualTo(0x234)); Assert.That(decodedInstruction.n, Is.EqualTo(0x4)); Assert.That(decodedInstruction.x, Is.EqualTo(0x2)); Assert.That(decodedInstruction.y, Is.EqualTo(0x3)); Assert.That(decodedInstruction.kk, Is.EqualTo(0x34)); Assert.That(decodedInstruction.OpCode, Is.EqualTo(0x1)); }
public void Step() { try { int machineCode = Fetch(); _currentInstruction = InstructionDecoder.Decode(machineCode); if (_currentInstruction is Break) { _debugging = true; _debuggingNotification(); } if (_currentInstruction.HasImmediate) { _currentInstruction.Immediate = Fetch(); } _currentInstruction.Execute(this); if (Interrupts.Count > 0) { lock (Interrupts) { throw new InterruptException(Interrupts.Dequeue()); } } } catch (InterruptException ex) { //Index into Interrupt Descriptor Table int interrupt = ex.InterruptNumber; KernelMode = true; //TODO: Need to switch to kernel stack? Should this be handled by IR common stub? //Similar to a call, stored the address where code should resume executing on the stack Memory[Registers[Register.SP]] = Registers[Register.IP]; Registers[Register.SP] += 4; //Set IP to fixed interrupt handler address Registers[Register.IP] = Memory[IDTPointer + interrupt * 4]; } if (_stepNotification != null && _debugging) { _stepNotification(); } }
public void SetRegisterValues(int currentInstruction, int[] registers) { int readRegister1 = InstructionDecoder.source_reg1(currentInstruction); int readRegister2 = InstructionDecoder.source_reg2(currentInstruction); int writeRegister = InstructionDecoder.dest_reg(currentInstruction); ReadReg1Value = registers[readRegister1]; ReadReg2Value = registers[readRegister2]; WriteReg_20_16 = readRegister2; WriteReg_15_11 = writeRegister; SEOffset = InstructionDecoder.offset(currentInstruction); Function = InstructionDecoder.func_code(SEOffset); }
public List <DisassembledInstruction> Disassemble(ushort address) { Registers.PC = address; var instructions = new List <DisassembledInstruction>(); DisassembledInstruction instruction; do { instruction = InstructionDecoder.DecodeNextInstruction(this); instructions.Add(instruction); } while (!instruction.ControlInstruction); return(instructions); }
public void DecodesOpeningZorkInstruction() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var decoder = new InstructionDecoder(machine); var memory = machine.Memory.SpanAt(machine.PC); var instruction = decoder.Decode(memory); instruction.Prepare(memory); instruction.Execute(memory); Assert.NotNull(instruction as VarInstruction); Assert.Equal("Call", instruction.Operation.Name); Assert.Equal(0, instruction.OpCode); Assert.Equal(0, instruction.StoreResult); Assert.Equal(3, instruction.Operands.Count); Assert.Equal(0x5479, machine.PC); memory = machine.Memory.SpanAt(machine.PC); instruction = decoder.Decode(memory); instruction.Prepare(memory); instruction.Execute(memory); Assert.NotNull(instruction as VarInstruction); Assert.Equal("Call", instruction.Operation.Name); Assert.Equal(2, instruction.Operands.Count); Assert.Equal(3, instruction.StoreResult); Assert.Equal(OperandType.Variable, instruction.Operands[1].Type); Assert.Equal(1, instruction.Operands[1].RawValue); memory = machine.Memory.SpanAt(machine.PC); instruction = decoder.Decode(memory); instruction.Prepare(memory); instruction.Execute(memory); Assert.NotNull(instruction as Op2Instruction); Assert.Equal("Add", instruction.Operation.Name); Assert.Equal(4, instruction.Size); Assert.Equal(2, instruction.Operands.Count); Assert.Equal(OperandType.Variable, instruction.Operands[0].Type); Assert.Equal(0x94, instruction.Operands[0].RawValue); Assert.Equal(OperandType.Small, instruction.Operands[1].Type); Assert.Equal(0xB4, instruction.Operands[1].RawValue); Assert.Equal(0x03, instruction.StoreResult); }
public void DecodeStream_StreamWithMultipleOpcode_DecodesInstructions() { var bytesStream = InstructionByteBuilder.Create() .Opcode(Opcode.Load, LoadKind.Integer, 42) .Opcode(Opcode.Load, LoadKind.Integer, 69) .Opcode(Opcode.Add) .Opcode(Opcode.Return) .AsStream(); var instructions = InstructionDecoder.DecodeStream(bytesStream).ToList(); Assert.Equal(4, instructions.Count); Assert.Equal("add", instructions.ElementAt(2).ToString()); Assert.Equal("ret", instructions.ElementAt(3).ToString()); }
public override void Execute(Dictionary <string, object> data) { var decoder = new InstructionDecoder(new Instruction[] { new Add(), new Multiply(), new Halt() }); var computer = new Computer(decoder); computer.State.Memory = (int[])data[_programKey]; computer.Run(); }
public M6502Core(uint frequency) { Pins = new PinsState(); Bus = new Bus(this); Registers = new RegistersState(); _instructionDecoder = new InstructionDecoder(this); _interruptsLogic = new InterruptsLogic(this); _frequency = frequency; if (frequency != 0) { _ticksPerCycle = TimeSpan.TicksPerSecond / (ulong)_frequency; } }
public void SetExecutionPath(int currentinstruction) { if ((InstructionDecoder.is_r_format(currentinstruction)) && ((InstructionDecoder.rfunct(currentinstruction) == InstructionType.Subtract) || InstructionDecoder.rfunct(currentinstruction) == InstructionType.Add)) { _regDestination = 1; _ALUOp = 10; _ALUSrc = 0; _MemRead = 0; _MemWrite = 0; _RegWrite = 1; _MemToReg = 0; } else if (InstructionDecoder.getOPcode(currentinstruction) == InstructionType.LoadByte) { _regDestination = 0; _ALUOp = 0; _ALUSrc = 1; _MemRead = 1; _MemWrite = 0; _RegWrite = 1; _MemToReg = 1; } else if (InstructionDecoder.getOPcode(currentinstruction) == InstructionType.StoreByte) { _regDestination = 0; _ALUOp = 0; _ALUSrc = 1; _MemRead = 0; _MemWrite = 1; _RegWrite = 0; _MemToReg = 0; } else { _regDestination = 0; _ALUOp = 0; _ALUSrc = 0; _MemRead = 0; _MemWrite = 0; _RegWrite = 0; _MemToReg = 0; } }
private Instruction getNextInstruction() { int address = _vm.cpu.Registers[Register.IP]; int machineCode = _vm.cpu.Memory.Get(address, false, 4); address += 4; Instruction instruction = InstructionDecoder.Decode(machineCode); if (instruction.HasImmediate) { instruction.Immediate = _vm.cpu.Memory.Get(address, false, 4); } return(instruction); }
public Benchmark() { const int memorySize = 32 * 1024 * 1024; var mmu = new Mmu(memorySize); var decoder = new InstructionDecoder(); cpu1 = new Cpu <PhysicalMemoryAccessor, NoTracing>(mmu, decoder); cpu2 = new Cpu <PhysicalMemoryAccessor, NoTracing>(mmu, decoder); mmu._pageDirectory = memorySize - 4; var elf = ELFReader.Load <uint>("D:\\fibo"); startAddress = elf.EntryPoint; var loadableSegments = elf.Segments.Where(s => s.Type == SegmentType.Load); foreach (var segment in loadableSegments) { var content = segment.GetMemoryContents(); var address = segment.PhysicalAddress; mmu.CopyToPhysical(address, content); } }
public void InitTestFixture() { _instructionDecoder = new InstructionDecoder(); }
public ModRmOpcodeDecoder( InstructionDecoder defaultDecoder, params (byte, InstructionDecoder) [] decoders)
static void Main(string[] args) { const int memorySize = 32 * 1024 * 1024; var mmu = new Mmu(memorySize); var decoder = new InstructionDecoder(); var cpu = new Cpu <PhysicalMemoryAccessor, Tracing>(mmu, decoder); if (false /*cpu is Cpu<VirtualMemoryAccessor>*/) { var(location, data) = TlbHelper.PrepareIdentityMap(memorySize); mmu.CopyToPhysical(location, data); mmu._pageDirectory = location; } else { mmu._pageDirectory = memorySize - 4; } var elf = ELFReader.Load <uint>("D:\\fibo"); var startAddress = elf.EntryPoint; var loadableSegments = elf.Segments.Where(s => s.Type == SegmentType.Load); foreach (var segment in loadableSegments) { var content = segment.GetMemoryContents(); var address = segment.PhysicalAddress; mmu.CopyToPhysical(address, content); } cpu.Execute((int)startAddress); var stoper = Stopwatch.StartNew(); for (var i = 0; i < 10; ++i) { cpu.Execute((int)startAddress); } stoper.Stop(); Console.WriteLine(stoper.ElapsedMilliseconds); Console.WriteLine(cpu.InstructionsExecuted); Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS"); cpu.Reset(); stoper = Stopwatch.StartNew(); for (var i = 0; i < 100; ++i) { cpu.Execute((int)startAddress); } stoper.Stop(); Console.WriteLine(stoper.ElapsedMilliseconds); Console.WriteLine(cpu.InstructionsExecuted); Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS"); cpu.Reset(); stoper = Stopwatch.StartNew(); for (var i = 0; i < 100; ++i) { cpu.Execute((int)startAddress); } stoper.Stop(); Console.WriteLine(stoper.ElapsedMilliseconds); Console.WriteLine(cpu.InstructionsExecuted); Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS"); }