Arithmatic
This commit is contained in:
@@ -1,3 +1,11 @@
|
|||||||
REG rx, 6
|
reg rx, 1
|
||||||
REG ry, 7
|
reg ry, 2
|
||||||
ADD rx, ry, 0
|
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
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ use std::fmt::Display;
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Instruction {
|
pub enum Instruction {
|
||||||
HLT,
|
Hlt,
|
||||||
REG(u8, usize),
|
Reg(u8, usize),
|
||||||
ADD(u8, u8, u8),
|
Add(u8, u8, u8),
|
||||||
|
Sub(u8, u8, u8),
|
||||||
|
Mul(u8, u8, u8),
|
||||||
|
Div(u8, u8, u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Instruction {
|
impl Display for Instruction {
|
||||||
@@ -13,9 +16,12 @@ impl Display for Instruction {
|
|||||||
f,
|
f,
|
||||||
"{}",
|
"{}",
|
||||||
match self {
|
match self {
|
||||||
Instruction::HLT => format!("HLT"),
|
Instruction::Hlt => format!("HLT"),
|
||||||
Instruction::REG(a, b) => format!("REG {a}, {b}"),
|
Instruction::Reg(a, b) => format!("REG {a}, {b}"),
|
||||||
Instruction::ADD(a, b, c) => format!("ADD {a}, {b}, {c}"),
|
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
@@ -20,22 +20,43 @@ impl Emulator {
|
|||||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||||
let mut pc = self.pc.get();
|
let mut pc = self.pc.get();
|
||||||
if let Some(inst) = self.instructions.get(**pc) {
|
if let Some(inst) = self.instructions.get(**pc) {
|
||||||
|
**pc += 1;
|
||||||
**self.inst.get() = inst.clone();
|
**self.inst.get() = inst.clone();
|
||||||
match &inst {
|
match &inst {
|
||||||
// 1 | Halt
|
// 1 | Halt
|
||||||
Instruction::HLT => break,
|
Instruction::Hlt => break,
|
||||||
|
|
||||||
// 2 | Registry set
|
// 2 | Registry set
|
||||||
Instruction::REG(r, v) => self.registers.set(*r, *v),
|
Instruction::Reg(r, v) => self.registers.set(*r, *v),
|
||||||
|
|
||||||
// 3 | Add
|
// 3 | Add
|
||||||
Instruction::ADD(rx, ry, r) => {
|
Instruction::Add(rx, ry, r) => {
|
||||||
let x = self.registers.get(*rx);
|
let x = self.registers.get(*rx);
|
||||||
let y = self.registers.get(*ry);
|
let y = self.registers.get(*ry);
|
||||||
self.registers.set(*r, x + y);
|
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 {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ fn main() {
|
|||||||
|
|
||||||
fn app(_screen: &Arc<Screen>, instructions: Vec<Instruction>) -> Rsx {
|
fn app(_screen: &Arc<Screen>, instructions: Vec<Instruction>) -> Rsx {
|
||||||
let pc = use_state::<usize>(0);
|
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 registers = Registry(use_state([0; 8]));
|
||||||
|
|
||||||
let emulator = emulator::Emulator {
|
let emulator = emulator::Emulator {
|
||||||
|
|||||||
+6
-3
@@ -21,9 +21,12 @@ pub fn from_text(s: &str) -> Vec<Instruction> {
|
|||||||
let args: Vec<&str> = args.split(',').map(str::trim).collect();
|
let args: Vec<&str> = args.split(',').map(str::trim).collect();
|
||||||
|
|
||||||
match name.to_lowercase().as_str() {
|
match name.to_lowercase().as_str() {
|
||||||
"hlt" => Some(Instruction::HLT),
|
"hlt" => Some(Instruction::Hlt),
|
||||||
"reg" => Some(Instruction::REG(reg(args[0]), num(args[1]))),
|
"reg" => Some(Instruction::Reg(reg(args[0]), num(args[1]))),
|
||||||
"add" => Some(Instruction::ADD(reg(args[0]), reg(args[1]), reg(args[2]))),
|
"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,
|
_ => None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user