From e2902e538a77ebdb21420f3905a20cdd28095909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bel=C3=A9n=20Albeza?= Date: Fri, 19 Jul 2024 10:29:09 +0200 Subject: [PATCH] Add EQU and EQU2 opcodes --- coco-core/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++++ coco-core/src/opcodes.rs | 2 ++ 2 files changed, 43 insertions(+) diff --git a/coco-core/src/lib.rs b/coco-core/src/lib.rs index 13f5717..9723b96 100644 --- a/coco-core/src/lib.rs +++ b/coco-core/src/lib.rs @@ -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::(), opcodes::JMP => self.op_jmp(), opcodes::JMP2 => self.op_jmp2(), opcodes::JNZ => self.op_jnz::<0x00>(), @@ -181,6 +183,21 @@ impl Cpu { self.stack.push_short(value); } + #[inline] + fn op_equ(&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(); @@ -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); + } } diff --git a/coco-core/src/opcodes.rs b/coco-core/src/opcodes.rs index 777b23b..5163e63 100644 --- a/coco-core/src/opcodes.rs +++ b/coco-core/src/opcodes.rs @@ -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;