aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorkrolxon <krolyxon@tutanota.com>2026-01-04 19:06:42 +0530
committerkrolxon <krolyxon@tutanota.com>2026-01-04 19:06:42 +0530
commit244172960fff86e147a8e20cf19773026cbed96f (patch)
treed2f03b4d35ef2df87ab4affbb1090d9ee2258263 /src/main.rs
parent798ac8ce75e2784e9e412f9ea7778b80b077c1dd (diff)
add jmp, jz
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 7c638ba..efbf5f7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -20,31 +20,36 @@ fn main() {
// b = 2
mem.write(0x0003, Instruction::MOV as u8);
mem.write(0x0004, 1);
- mem.write(0x0005, 1);
+ mem.write(0x0005, 5);
// a = a + b
mem.write(0x0006, Instruction::SUB as u8);
mem.write(0x0007, 0);
mem.write(0x0008, 1);
+ // JMP to halt
+ mem.write(0x0009, Instruction::JZ as u8);
+ mem.write(0x000a, 0x0f); // Low
+ mem.write(0x000b, 0x00); // High
+
// set b = 0
- mem.write(0x0009, Instruction::MOV as u8);
- mem.write(0x000a, 1);
- mem.write(0x000b, 0);
+ mem.write(0x000c, Instruction::MOV as u8);
+ mem.write(0x000d, 1);
+ mem.write(0x000e, 0);
// halt
- mem.write(0x000c, Instruction::HLT as u8);
+ mem.write(0x000f, Instruction::HLT as u8);
while !cpu.halted {
let opcode = mem.read(cpu.pc);
- cpu.inc_cp();
+ cpu.inc_pc();
match opcode {
x if x == Instruction::MOV as u8 => cpu.mov(&mut mem),
x if x == Instruction::ADD as u8 => cpu.add(&mut mem),
x if x == Instruction::SUB as u8 => cpu.sub(&mut mem),
- x if x == Instruction::JMP as u8 => {}
- x if x == Instruction::JZ as u8 => {}
+ x if x == Instruction::JMP as u8 => cpu.jmp(&mut mem),
+ x if x == Instruction::JZ as u8 => cpu.jz(&mut mem),
x if x == Instruction::HLT as u8 => cpu.halt(),
_ => panic!("Unknown opcode {:02X}", opcode),
}