Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
feat: vm fetch/decode/execute structure
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jun 11, 2024
1 parent 9e2357c commit c7b61a2
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 31 deletions.
221 changes: 221 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ edition = "2021"

[dev-dependencies]
pretty_assertions = "1.4.0"

[dependencies]
env_logger = "0.11.3"
log = "0.4.21"
51 changes: 51 additions & 0 deletions src/interpreter/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use log::trace;

pub struct Memory {
pub data: Vec<u8>,
}

impl Memory {
pub fn new(size: usize) -> Self {
Memory {
data: vec![0; size],
}
}

pub fn from(data: Vec<u8>) -> Self {
Memory { data }
}

pub fn read(&self, address: u16) -> u8 {
self.data[address as usize]
}

pub fn read_word(&self, address: u16) -> u16 {
u16::from_le_bytes([
self.data[address as usize],
self.data[(address + 1) as usize],
])
}

pub fn read_bytes(&self, address: u16, size: usize) -> &[u8] {
&self.data[address as usize..(address as usize + size)]
}

pub fn write(&mut self, address: u16, value: u8) {
self.data[address as usize] = value;
}

pub fn write_word(&mut self, address: u16, value: u16) {
let bytes = value.to_le_bytes();
self.data[address as usize] = bytes[0];
self.data[(address + 1) as usize] = bytes[1];
}

pub fn write_bytes(&mut self, address: u16, data: &[u8]) {
trace!("WRITE[{:04X}]: {:?}", address, data);
self.data[address as usize..(address as usize + data.len())].copy_from_slice(data);
}

pub fn len(&self) -> usize {
self.data.len()
}
}
3 changes: 3 additions & 0 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod memory;
mod register_set;
mod vm;

pub use vm::Interpretable;
10 changes: 6 additions & 4 deletions src/interpreter/register_set.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;

use crate::x86::Register;
use log::trace;

#[derive(Debug)]
pub struct RegisterSet {
registers: HashMap<Register, i32>,
registers: HashMap<Register, i16>,
}

impl RegisterSet {
Expand All @@ -16,12 +17,13 @@ impl RegisterSet {
Self { registers }
}

pub fn get(&self, reg: Register) -> Option<&i32> {
self.registers.get(&reg)
pub fn get(&self, reg: Register) -> i16 {
*self.registers.get(&reg).expect("Unknown register")
}

pub fn set(&mut self, reg: Register, value: i32) {
pub fn set(&mut self, reg: Register, value: i16) {
if let Some(val) = self.registers.get_mut(&reg) {
trace!("Set {:?}: {} (previous: {})", reg, value, *val);
*val = value;
}
}
Expand Down
Loading

0 comments on commit c7b61a2

Please sign in to comment.