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)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Opcode {
|
pub enum Opcode {
|
||||||
HLT,
|
HLT,
|
||||||
IGL
|
IGL,
|
||||||
|
LOAD,
|
||||||
|
ADD,
|
||||||
|
MUL,
|
||||||
|
DIV
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl From<u8> for Opcode {
|
impl From<u8> for Opcode {
|
||||||
fn from(vm: u8) -> Self {
|
fn from(vm: u8) -> Self {
|
||||||
match vm {
|
match vm {
|
||||||
|
|
21
src/vm.rs
21
src/vm.rs
|
@ -21,12 +21,30 @@ impl VM {
|
||||||
opcode
|
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) {
|
pub fn run(&mut self) {
|
||||||
loop {
|
loop {
|
||||||
if self.pc >= self.program.len() {
|
if self.pc >= self.program.len() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
match self.decode_opcode() {
|
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 => {
|
Opcode::HLT => {
|
||||||
println!("HLT encountered");
|
println!("HLT encountered");
|
||||||
return;
|
return;
|
||||||
|
@ -35,6 +53,9 @@ impl VM {
|
||||||
println!("Unrecognized opcode found! Terminating!");
|
println!("Unrecognized opcode found! Terminating!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Opcode::ADD => {}
|
||||||
|
Opcode::MUL => {}
|
||||||
|
Opcode::DIV => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue