Skip to content

Commit

Permalink
Add ADD opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
belen-albeza committed Jul 18, 2024
1 parent 5290b9e commit 188fc69
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions coco-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl Cpu {
opcodes::DEI => self.op_dei(machine),
opcodes::DEO => self.op_deo(machine),
opcodes::DEO2 => self.op_deo2(machine),
opcodes::ADD => self.op_add(),
opcodes::SUB => self.op_sub(),
opcodes::MUL => self.op_mul(),
opcodes::DIV => self.op_div(),
Expand Down Expand Up @@ -177,6 +178,14 @@ impl Cpu {
machine.deo(self, target);
}

#[inline]
fn op_add(&mut self) {
let b = self.stack.pop_byte();
let a = self.stack.pop_byte();
let value = a.wrapping_add(b);
self.stack.push_byte(value);
}

#[inline]
fn op_sub(&mut self) {
let b = self.stack.pop_byte();
Expand Down Expand Up @@ -363,6 +372,18 @@ mod tests {
// TODO: check AnyMachine.deo has been called with 0xab as target arg
}

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

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

assert_eq!(pc, 0x106);
assert_eq!(cpu.stack.len(), 1);
assert_eq!(cpu.stack.byte_at(0), 0xad);
}

#[test]
fn sub_opcode() {
let rom = rom_from(&[PUSH, 0xab, PUSH, 0x02, SUB, BRK]);
Expand Down
1 change: 1 addition & 0 deletions coco-core/src/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub const DUP: u8 = 0x06;
pub const DUP2: u8 = 0x26;
pub const DEI: u8 = 0x16;
pub const DEO: u8 = 0x17;
pub const ADD: u8 = 0x18;
pub const SUB: u8 = 0x19;
pub const MUL: u8 = 0x1a;
pub const DIV: u8 = 0x1b;
Expand Down

0 comments on commit 188fc69

Please sign in to comment.