[cpu] decode upto draw

This commit is contained in:
2024-03-04 18:49:27 +05:30
parent 7d1e684fc6
commit be5dd66e99

View File

@@ -26,6 +26,10 @@ pub enum Instruction {
ConditionalInEqRegisterSkipNext(usize, usize), ConditionalInEqRegisterSkipNext(usize, usize),
/// ANNN - Set index value /// ANNN - Set index value
SetIndex(u16), 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 /// DXYN - Draw pixels at xy pointed by register for n bytes long
Draw(usize, usize, u8), Draw(usize, usize, u8),
@@ -107,6 +111,16 @@ impl Instruction {
0xA => { 0xA => {
Instruction::SetIndex(instruction & 0xfff) 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 => { 0xD => {
let x = (instruction & 0xf00) >> 8; let x = (instruction & 0xf00) >> 8;
let y = (instruction & 0xf0) >> 4; let y = (instruction & 0xf0) >> 4;
@@ -148,7 +162,7 @@ impl Instruction {
} }
7=>{ 7=>{
Instruction::RSub(reg_x,reg_y) Instruction::RSub(reg_x,reg_y)
} },
0xe=>{ 0xe=>{
Instruction::LShift(reg_x,reg_y) Instruction::LShift(reg_x,reg_y)
} }
@@ -264,6 +278,22 @@ mod tests {
let ins = Instruction::decode_instruction(&instruction_bytes); let ins = Instruction::decode_instruction(&instruction_bytes);
assert_eq!(ins, SetIndex(0xfaf)); 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] #[test]
fn test_draw() { fn test_draw() {