Arithmatic

This commit is contained in:
2025-10-29 17:54:44 +01:00
parent 4ae0189356
commit 0c93e78ea1
5 changed files with 55 additions and 17 deletions
+11 -3
View File
@@ -1,3 +1,11 @@
REG rx, 6
REG ry, 7
ADD rx, ry, 0
reg rx, 1
reg ry, 2
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
mul rx, ry, rx
+12 -6
View File
@@ -2,9 +2,12 @@ use std::fmt::Display;
#[derive(Debug, Clone)]
pub enum Instruction {
HLT,
REG(u8, usize),
ADD(u8, u8, u8),
Hlt,
Reg(u8, usize),
Add(u8, u8, u8),
Sub(u8, u8, u8),
Mul(u8, u8, u8),
Div(u8, u8, u8),
}
impl Display for Instruction {
@@ -13,9 +16,12 @@ impl Display for Instruction {
f,
"{}",
match self {
Instruction::HLT => format!("HLT"),
Instruction::REG(a, b) => format!("REG {a}, {b}"),
Instruction::ADD(a, b, c) => format!("ADD {a}, {b}, {c}"),
Instruction::Hlt => format!("HLT"),
Instruction::Reg(a, b) => format!("REG {a}, {b}"),
Instruction::Add(a, b, c) => format!("ADD {a}, {b}, {c}"),
Instruction::Sub(a, b, c) => format!("SUB {a}, {b}, {c}"),
Instruction::Mul(a, b, c) => format!("MUL {a}, {b}, {c}"),
Instruction::Div(a, b, c) => format!("DIV {a}, {b}, {c}"),
}
)
}
+25 -4
View File
@@ -20,22 +20,43 @@ impl Emulator {
std::thread::sleep(std::time::Duration::from_millis(100));
let mut pc = self.pc.get();
if let Some(inst) = self.instructions.get(**pc) {
**pc += 1;
**self.inst.get() = inst.clone();
match &inst {
// 1 | Halt
Instruction::HLT => break,
Instruction::Hlt => break,
// 2 | Registry set
Instruction::REG(r, v) => self.registers.set(*r, *v),
Instruction::Reg(r, v) => self.registers.set(*r, *v),
// 3 | Add
Instruction::ADD(rx, ry, r) => {
Instruction::Add(rx, ry, r) => {
let x = self.registers.get(*rx);
let y = self.registers.get(*ry);
self.registers.set(*r, x + y);
}
// 3 | Subtract
Instruction::Sub(rx, ry, r) => {
let x = self.registers.get(*rx);
let y = self.registers.get(*ry);
self.registers.set(*r, x - y);
}
// 4 | Multiply
Instruction::Mul(rx, ry, r) => {
let x = self.registers.get(*rx);
let y = self.registers.get(*ry);
self.registers.set(*r, x * y);
}
// 5 | Divide
Instruction::Div(rx, ry, r) => {
let x = self.registers.get(*rx);
let y = self.registers.get(*ry);
self.registers.set(*r, x / y);
}
}
**pc += 1;
} else {
break;
}
+1 -1
View File
@@ -22,7 +22,7 @@ fn main() {
fn app(_screen: &Arc<Screen>, instructions: Vec<Instruction>) -> Rsx {
let pc = use_state::<usize>(0);
let inst = use_state::<Instruction>(Instruction::HLT);
let inst = use_state::<Instruction>(Instruction::Hlt);
let registers = Registry(use_state([0; 8]));
let emulator = emulator::Emulator {
+6 -3
View File
@@ -21,9 +21,12 @@ pub fn from_text(s: &str) -> Vec<Instruction> {
let args: Vec<&str> = args.split(',').map(str::trim).collect();
match name.to_lowercase().as_str() {
"hlt" => Some(Instruction::HLT),
"reg" => Some(Instruction::REG(reg(args[0]), num(args[1]))),
"add" => Some(Instruction::ADD(reg(args[0]), reg(args[1]), reg(args[2]))),
"hlt" => Some(Instruction::Hlt),
"reg" => Some(Instruction::Reg(reg(args[0]), num(args[1]))),
"add" => Some(Instruction::Add(reg(args[0]), reg(args[1]), reg(args[2]))),
"sub" => Some(Instruction::Sub(reg(args[0]), reg(args[1]), reg(args[2]))),
"mul" => Some(Instruction::Mul(reg(args[0]), reg(args[1]), reg(args[2]))),
"div" => Some(Instruction::Div(reg(args[0]), reg(args[1]), reg(args[2]))),
_ => None,
}
})