diff --git a/src/emulator.rs b/src/emulator.rs index 6a1a0a1..db7e99e 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -101,11 +101,13 @@ where fn emulation_loop(&mut self) -> Result<(), anyhow::Error> { loop { // fetch instruction - let instruction = self.fetch_instruction(); + let instruction = self.fetch_instruction()?; self.program_counter += 2; - // decode opcode - // let decode = self.decode_instruction(instruction); + debug!("PC={} {:04x}", self.program_counter, instruction) + + // decode & execute + //let decode = self.decode_instruction(instruction); // execute } @@ -113,11 +115,15 @@ where } /// Fetches the current instruction from the memory without incrementing the program counter. - fn fetch_instruction(&self) -> Instruction { - Instruction::new([ + fn fetch_instruction(&self) -> Result { + if self.program_counter as usize >= self.memory.len() { + return Err(anyhow!("program_counter is out of range")); + } + + Ok(Instruction::new([ self.memory[self.program_counter as usize], self.memory[self.program_counter as usize + 1], - ]) + ])) } fn decode_instruction(&mut self, _instruction: Instruction) -> Result<(), anyhow::Error> {