bench512441729
add jmp, jz
+37 −11
2 files changed
- M src/cpu.rs +24 −3
- M src/main.rs +13 −8
| @@ -18,7 +18,7 @@ pub struct CPU{ | |||
| 18 | 18 | } | |
| 19 | 19 | ||
| 20 | 20 | impl CPU { | |
| 21 | − | pub fn inc_cp(&mut self) { | |
| 21 | + | pub fn inc_pc(&mut self) { | |
| 22 | 22 | self.pc += 1; | |
| 23 | 23 | } | |
| 24 | 24 | ||
| @@ -27,8 +27,8 @@ impl CPU { | |||
| 27 | 27 | } | |
| 28 | 28 | ||
| 29 | 29 | pub fn mov(&mut self, mem: &mut Memory) { | |
| 30 | − | let reg = mem.read(self.pc); self.inc_cp(); | |
| 31 | − | let val = mem.read(self.pc); self.inc_cp(); | |
| 30 | + | let reg = mem.read(self.pc); self.inc_pc(); | |
| 31 | + | let val = mem.read(self.pc); self.inc_pc(); | |
| 32 | 32 | ||
| 33 | 33 | match reg { | |
| 34 | 34 | 0 => self.a = val, | |
| @@ -132,4 +132,25 @@ impl CPU { | |||
| 132 | 132 | self.carry = borrow; | |
| 133 | 133 | } | |
| 134 | 134 | ||
| 135 | + | pub fn jmp(&mut self, mem: &mut Memory) { | |
| 136 | + | let low = mem.read(self.pc) as u16; self.inc_pc(); | |
| 137 | + | let high = mem.read(self.pc) as u16; self.inc_pc(); | |
| 138 | + | ||
| 139 | + | let addrs = (high << 8) | low; | |
| 140 | + | ||
| 141 | + | self.pc = addrs; | |
| 142 | + | } | |
| 143 | + | ||
| 144 | + | pub fn jz(&mut self, mem: &mut Memory) { | |
| 145 | + | let low = mem.read(self.pc) as u16; self.inc_pc(); | |
| 146 | + | let high = mem.read(self.pc) as u16; self.inc_pc(); | |
| 147 | + | ||
| 148 | + | let addrs = (high << 8) | low; | |
| 149 | + | ||
| 150 | + | if self.zero { | |
| 151 | + | self.pc = addrs; | |
| 152 | + | } | |
| 153 | + | ||
| 154 | + | } | |
| 155 | + | ||
| 135 | 156 | } | |
| @@ -20,31 +20,36 @@ fn main() { | |||
| 20 | 20 | // b = 2 | |
| 21 | 21 | mem.write(0x0003, Instruction::MOV as u8); | |
| 22 | 22 | mem.write(0x0004, 1); | |
| 23 | − | mem.write(0x0005, 1); | |
| 23 | + | mem.write(0x0005, 5); | |
| 24 | 24 | ||
| 25 | 25 | // a = a + b | |
| 26 | 26 | mem.write(0x0006, Instruction::SUB as u8); | |
| 27 | 27 | mem.write(0x0007, 0); | |
| 28 | 28 | mem.write(0x0008, 1); | |
| 29 | 29 | ||
| 30 | + | // JMP to halt | |
| 31 | + | mem.write(0x0009, Instruction::JZ as u8); | |
| 32 | + | mem.write(0x000a, 0x0f); // Low | |
| 33 | + | mem.write(0x000b, 0x00); // High | |
| 34 | + | ||
| 30 | 35 | // set b = 0 | |
| 31 | − | mem.write(0x0009, Instruction::MOV as u8); | |
| 32 | − | mem.write(0x000a, 1); | |
| 33 | − | mem.write(0x000b, 0); | |
| 36 | + | mem.write(0x000c, Instruction::MOV as u8); | |
| 37 | + | mem.write(0x000d, 1); | |
| 38 | + | mem.write(0x000e, 0); | |
| 34 | 39 | ||
| 35 | 40 | // halt | |
| 36 | − | mem.write(0x000c, Instruction::HLT as u8); | |
| 41 | + | mem.write(0x000f, Instruction::HLT as u8); | |
| 37 | 42 | ||
| 38 | 43 | while !cpu.halted { | |
| 39 | 44 | let opcode = mem.read(cpu.pc); | |
| 40 | − | cpu.inc_cp(); | |
| 45 | + | cpu.inc_pc(); | |
| 41 | 46 | ||
| 42 | 47 | match opcode { | |
| 43 | 48 | x if x == Instruction::MOV as u8 => cpu.mov(&mut mem), | |
| 44 | 49 | x if x == Instruction::ADD as u8 => cpu.add(&mut mem), | |
| 45 | 50 | x if x == Instruction::SUB as u8 => cpu.sub(&mut mem), | |
| 46 | − | x if x == Instruction::JMP as u8 => {} | |
| 47 | − | x if x == Instruction::JZ as u8 => {} | |
| 51 | + | x if x == Instruction::JMP as u8 => cpu.jmp(&mut mem), | |
| 52 | + | x if x == Instruction::JZ as u8 => cpu.jz(&mut mem), | |
| 48 | 53 | x if x == Instruction::HLT as u8 => cpu.halt(), | |
| 49 | 54 | _ => panic!("Unknown opcode {:02X}", opcode), | |
| 50 | 55 | } | |