diff --git a/src/vm/instruction.rs b/src/vm/instruction.rs index c0510b7..dd5833d 100644 --- a/src/vm/instruction.rs +++ b/src/vm/instruction.rs @@ -84,7 +84,7 @@ pub fn execute_instruction(vm: &mut VM, instr: u16) { OpCode::JMP => no_op(vm, instr), OpCode::RES => no_op(vm, instr), OpCode::LEA => op_lea(vm, instr), - OpCode::TRAP => no_op(vm, instr), + OpCode::TRAP => op_trap(vm, instr), OpCode::NOOP => no_op(vm, instr), } } @@ -105,3 +105,20 @@ fn op_lea(vm: &mut VM, instr: u16) { let offset = sign_extend(instr & 0xff, 9); vm.registers.set_reg(dr, vm.registers.pc + offset); } + +fn op_trap(vm: &mut VM, instr: u16) { + // conform to spec + // (we don't actually need it in this implementation) + vm.registers.r7 = vm.registers.pc; + + let trap_vector = instr & 0xff; + match trap_vector { + 0x20 => todo!("GETC"), + 0x21 => todo!("OUT"), + 0x22 => todo!("PUTS"), + 0x23 => todo!("IN"), + 0x24 => todo!("PUTSP"), + 0x25 => todo!("HALT"), + _ => unimplemented!(), + } +} diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 107ee6c..30e54f2 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -192,6 +192,9 @@ impl VM { while running { let instr = self.mem.get_mem(self.registers.pc); + // NOTE + // remember PC points to the *next* instruction at all times + // disallow reading past memory bounds if self.registers.pc as usize == MEM_SIZE - 1 { running = false