diff options
| author | krolxon <krolyxon@tutanota.com> | 2026-01-08 18:47:07 +0530 |
|---|---|---|
| committer | krolxon <krolyxon@tutanota.com> | 2026-01-08 18:47:07 +0530 |
| commit | 3372e774e9505acdc3778a8155476cca4bfbd3e8 (patch) | |
| tree | 60131fe5c0367a9ab9908b33bcfe81f6aaece543 /src | |
| parent | 68d85406ef834312b39474c11be09e5ed1a228b4 (diff) | |
add MUL & DIV
Diffstat (limited to 'src')
| -rw-r--r-- | src/assembler.rs | 21 | ||||
| -rw-r--r-- | src/cpu.rs | 42 | ||||
| -rw-r--r-- | src/instructions.rs | 4 |
3 files changed, 64 insertions, 3 deletions
diff --git a/src/assembler.rs b/src/assembler.rs index e7aa305..1b9c85b 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -25,7 +25,7 @@ fn is_reg(s: &str) -> bool { fn instr_size(tokens: &[String]) -> u16 { match tokens[0].as_str() { - "mov" | "add" | "sub" | "jmp" | "jz" | "jnz" | "cmp" => 3, + "mov" | "add" | "sub" | "jmp" | "jz" | "jnz" | "cmp" | "mul" | "div" => 3, "hlt" => 1, _ => panic!("Unknown instruction {}", tokens[0]), } @@ -153,6 +153,25 @@ pub fn assembler(source: &str) -> Vec<u8> { } } + "mul" => { + let r1 = parse_reg(&tokens[1]); + let r2 = parse_reg(&tokens[2]); + + bytes.push(Instruction::MUL as u8); + bytes.push(r1); + bytes.push(r2); + + } + + "div" => { + let r1 = parse_reg(&tokens[1]); + let r2 = parse_reg(&tokens[2]); + + bytes.push(Instruction::DIV as u8); + bytes.push(r1); + bytes.push(r2); + } + "hlt" => { bytes.push(Instruction::HLT as u8); } @@ -51,6 +51,8 @@ impl CPU { x if x == Instruction::JNZ as u8 => self.jnz(mem), x if x == Instruction::CMP_RI as u8 => self.cmp_ri(mem), x if x == Instruction::CMP_RR as u8 => self.cmp_rr(mem), + x if x == Instruction::MUL as u8 => self.mul(mem), + x if x == Instruction::DIV as u8 => self.div(mem), x if x == Instruction::HLT as u8 => self.halt(), _ => panic!("Unknown opcode {:02X}", opcode), } @@ -298,6 +300,42 @@ impl CPU { self.carry = borrow; } + pub fn mul(&mut self, mem: &Memory) { + let dest = mem.read(self.pc); self.inc_pc(); + let src = mem.read(self.pc); self.inc_pc(); + + let lhs = self.get_reg(dest); + let rhs = self.get_reg(src); + + let result16 = (lhs as u16) * (rhs as u16); + let result8 = (result16 & 0xFF) as u8; + + self.set_reg(dest, result8); + + self.zero = result8 == 0; + self.carry = result16 > 0xFF; + } + + pub fn div(&mut self, mem: &Memory) { + let dest = mem.read(self.pc); self.inc_pc(); + let src = mem.read(self.pc); self.inc_pc(); + + let lhs = self.get_reg(dest); + let rhs = self.get_reg(src); + + if rhs == 0 { + panic!("Division by zero"); + // OR: set halted = true + // OR: define a syscall/exception later + } + + let result = lhs / rhs; + + self.set_reg(dest, result); + + self.zero = result == 0; + // carry unchanged + } fn get_reg(&self, r: u8) -> u8 { match r { @@ -305,7 +343,7 @@ impl CPU { 1 => self.b, 2 => self.c, 3 => self.d, - _ => 0, + _ => panic!("Invalid Register"), } } @@ -315,7 +353,7 @@ impl CPU { 1 => self.b = val, 2 => self.c = val, 3 => self.d = val, - _ => {}, + _ => panic!("Invalid register"), } } diff --git a/src/instructions.rs b/src/instructions.rs index bfc4fc6..9f81b9a 100644 --- a/src/instructions.rs +++ b/src/instructions.rs @@ -12,6 +12,8 @@ pub enum Instruction { JNZ = 0x06, CMP_RI = 0x07, CMP_RR = 0x09, + MUL = 0x0C, + DIV = 0x0D, HLT = 0xFF, } @@ -25,6 +27,8 @@ impl Instruction { 0x05 => "JZ", 0x06 => "JNZ", 0x07 | 0x09 => "CMP", + 0x0C => "MUL", + 0x0D => "DIV", 0xFF => "HLT", _ => "???", } |
