diff options
| author | krolxon <krolyxon@tutanota.com> | 2026-01-04 19:10:23 +0530 |
|---|---|---|
| committer | krolxon <krolyxon@tutanota.com> | 2026-01-04 19:10:23 +0530 |
| commit | be61e5ae6bca45a6dafc46cfe0957a8db96f9e4c (patch) | |
| tree | b6e3f7c3cdb9eec153e3c4bbdda8e5b02e6b00ec /src | |
| parent | 244172960fff86e147a8e20cf19773026cbed96f (diff) | |
add jnz, README.md
Diffstat (limited to 'src')
| -rw-r--r-- | src/cpu.rs | 13 | ||||
| -rw-r--r-- | src/instructions.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 5 |
3 files changed, 17 insertions, 2 deletions
@@ -153,4 +153,17 @@ impl CPU { } + pub fn jnz(&mut self, mem: &mut Memory) { + let low = mem.read(self.pc) as u16; self.inc_pc(); + let high = mem.read(self.pc) as u16; self.inc_pc(); + + let addrs = (high << 8) | low; + + if !self.zero { + self.pc = addrs; + } + + } + + } diff --git a/src/instructions.rs b/src/instructions.rs index f5884dc..acfb8bd 100644 --- a/src/instructions.rs +++ b/src/instructions.rs @@ -5,5 +5,6 @@ pub enum Instruction { SUB = 0x03, JMP = 0x04, JZ = 0x05, + JNZ = 0x06, HLT = 0xFF, } diff --git a/src/main.rs b/src/main.rs index efbf5f7..24a43ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ fn main() { // b = 2 mem.write(0x0003, Instruction::MOV as u8); mem.write(0x0004, 1); - mem.write(0x0005, 5); + mem.write(0x0005, 3); // a = a + b mem.write(0x0006, Instruction::SUB as u8); @@ -28,7 +28,7 @@ fn main() { mem.write(0x0008, 1); // JMP to halt - mem.write(0x0009, Instruction::JZ as u8); + mem.write(0x0009, Instruction::JNZ as u8); mem.write(0x000a, 0x0f); // Low mem.write(0x000b, 0x00); // High @@ -50,6 +50,7 @@ fn main() { x if x == Instruction::SUB as u8 => cpu.sub(&mut mem), 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::JNZ as u8 => cpu.jnz(&mut mem), x if x == Instruction::HLT as u8 => cpu.halt(), _ => panic!("Unknown opcode {:02X}", opcode), } |
