Initial start on LOAD, ADD, HUL and DIV opcodes
This commit is contained in:
parent
08b16dae6b
commit
d1340fe9fc
2 changed files with 26 additions and 2 deletions
|
@ -1,10 +1,13 @@
|
|||
#[derive(Debug, PartialEq)]
|
||||
pub enum Opcode {
|
||||
HLT,
|
||||
IGL
|
||||
IGL,
|
||||
LOAD,
|
||||
ADD,
|
||||
MUL,
|
||||
DIV
|
||||
}
|
||||
|
||||
|
||||
impl From<u8> for Opcode {
|
||||
fn from(vm: u8) -> Self {
|
||||
match vm {
|
||||
|
|
21
src/vm.rs
21
src/vm.rs
|
@ -21,12 +21,30 @@ impl VM {
|
|||
opcode
|
||||
}
|
||||
|
||||
fn next_8_bits(&mut self) -> u8 {
|
||||
let result = self.program[self.pc];
|
||||
self.pc += 1;
|
||||
result
|
||||
}
|
||||
|
||||
fn next_16_bits(&mut self) -> u16 {
|
||||
|
||||
let result = ((self.program[self.pc] as u16) << 8) | self.program[self.pc + 1] as u16;
|
||||
self.pc += 2;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
loop {
|
||||
if self.pc >= self.program.len() {
|
||||
break;
|
||||
}
|
||||
match self.decode_opcode() {
|
||||
Opcode::LOAD => {
|
||||
let reg = self.next_8_bits() as usize;
|
||||
let num = self.next_16_bits() as u32;
|
||||
self.registers[reg] = num as i32;
|
||||
}
|
||||
Opcode::HLT => {
|
||||
println!("HLT encountered");
|
||||
return;
|
||||
|
@ -35,6 +53,9 @@ impl VM {
|
|||
println!("Unrecognized opcode found! Terminating!");
|
||||
return;
|
||||
}
|
||||
Opcode::ADD => {}
|
||||
Opcode::MUL => {}
|
||||
Opcode::DIV => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue