Skip to content

Commit

Permalink
Add EQU and EQU2 opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
belen-albeza committed Jul 19, 2024
1 parent 3e2f7af commit e2902e5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions coco-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Cpu {
opcodes::INC => self.op_inc(),
opcodes::DUP => self.op_dup(),
opcodes::DUP2 => self.op_dup2(),
opcodes::EQU => self.op_equ::<0x00>(),
opcodes::EQU2 => self.op_equ::<FLAG_SHORT>(),
opcodes::JMP => self.op_jmp(),
opcodes::JMP2 => self.op_jmp2(),
opcodes::JNZ => self.op_jnz::<0x00>(),
Expand Down Expand Up @@ -181,6 +183,21 @@ impl Cpu {
self.stack.push_short(value);
}

#[inline]
fn op_equ<const FLAGS: u8>(&mut self) {
let res = if short_mode(FLAGS) {
let b = self.stack.pop_short();
let a = self.stack.pop_short();
a == b
} else {
let b = self.stack.pop_byte();
let a = self.stack.pop_byte();
a == b
};

self.stack.push_byte(if res { 0x01 } else { 0x00 });
}

#[inline]
fn op_jmp(&mut self) {
let offset = self.stack.pop_byte();
Expand Down Expand Up @@ -653,4 +670,28 @@ mod tests {
assert_eq!(cpu.stack.len(), 0);
assert_eq!(cpu.ram_peek_short(0x01), 0xabcd);
}

#[test]
fn equ_opcode() {
let rom = rom_from(&[PUSH, 0xab, PUSH, 0xab, EQU, PUSH, 0x00, EQU, BRK]);
let mut cpu = Cpu::new(&rom);

let pc = cpu.run(0x100, &mut AnyMachine {});

assert_eq!(pc, 0x109);
assert_eq!(cpu.stack.len(), 1);
assert_eq!(cpu.stack.byte_at(0), 0x00);
}

#[test]
fn equ2_opcode() {
let rom = rom_from(&[PUSH2, 0xab, 0xcd, PUSH2, 0xab, 0xcd, EQU2, BRK]);
let mut cpu = Cpu::new(&rom);

let pc = cpu.run(0x100, &mut AnyMachine {});

assert_eq!(pc, 0x108);
assert_eq!(cpu.stack.len(), 1);
assert_eq!(cpu.stack.byte_at(0), 0x01);
}
}
2 changes: 2 additions & 0 deletions coco-core/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub const BRK: u8 = 0x00;
pub const INC: u8 = 0x01;
pub const DUP: u8 = 0x06;
pub const DUP2: u8 = 0x26;
pub const EQU: u8 = 0x08;
pub const EQU2: u8 = 0x28;
pub const JMP: u8 = 0x0c;
pub const JMP2: u8 = 0x2c;
pub const JNZ: u8 = 0x0d;
Expand Down

0 comments on commit e2902e5

Please sign in to comment.