From be5dd66e992ee866abf48b46030da64be1e97b0e Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Mon, 4 Mar 2024 18:49:27 +0530 Subject: [PATCH] [cpu] decode upto draw --- src/device/instruction.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/device/instruction.rs b/src/device/instruction.rs index b0e999c..975354d 100644 --- a/src/device/instruction.rs +++ b/src/device/instruction.rs @@ -26,6 +26,10 @@ pub enum Instruction { ConditionalInEqRegisterSkipNext(usize, usize), /// ANNN - Set index value SetIndex(u16), + /// B(X+N)NN - Jump to address with offset. Either v0 or specified register + JumpWithOffset(usize,u16), + /// CXNN - AND a random number with NN and place in register + RandomOr(usize,u16), /// DXYN - Draw pixels at xy pointed by register for n bytes long Draw(usize, usize, u8), @@ -107,6 +111,16 @@ impl Instruction { 0xA => { Instruction::SetIndex(instruction & 0xfff) } + 0xB =>{ + let register_x = (instruction & 0xf00) >> 8; + let jump_address_base = instruction & 0xfff; + Instruction::JumpWithOffset(register_x as usize, jump_address_base) + } + 0xC =>{ + let register_x = (instruction & 0xf00) >> 8; + let mask = instruction & 0xff; + Instruction::RandomOr(register_x as usize,mask) + } 0xD => { let x = (instruction & 0xf00) >> 8; let y = (instruction & 0xf0) >> 4; @@ -148,7 +162,7 @@ impl Instruction { } 7=>{ Instruction::RSub(reg_x,reg_y) - } + }, 0xe=>{ Instruction::LShift(reg_x,reg_y) } @@ -264,6 +278,22 @@ mod tests { let ins = Instruction::decode_instruction(&instruction_bytes); assert_eq!(ins, SetIndex(0xfaf)); } + #[test] + fn test_jump_with_offset() { + let instruction_bytes = 0xbfae_u16.to_be_bytes(); + let ins = Instruction::decode_instruction(&instruction_bytes); + assert_eq!(ins, JumpWithOffset(0xf,0xfae)); + } + + #[test] + fn test_random_and() { + let instruction_bytes = 0xcabd_u16.to_be_bytes(); + let ins = Instruction::decode_instruction(&instruction_bytes); + assert_eq!(ins, RandomOr(0xa,0xbd)); + } + + + #[test] fn test_draw() {