src/vm/memory.rs: split off memory-related code
This commit is contained in:
parent
6a25b79d78
commit
f965108172
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -13,4 +13,20 @@ name = "lc3"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
"termios",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.151"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termios"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "411c5bf740737c7918b8b1fe232dca4dc9f8e754b8ad5e20966814001ed0ac6b"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
]
|
]
|
||||||
|
@ -7,3 +7,4 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1.5.0"
|
byteorder = "1.5.0"
|
||||||
|
termios = "0.3.3"
|
||||||
|
32
src/vm/memory.rs
Normal file
32
src/vm/memory.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
//////////////////////////////
|
||||||
|
////// memory and memory-mapped registers
|
||||||
|
//////////////////////////////
|
||||||
|
|
||||||
|
////////////////
|
||||||
|
// memory interface
|
||||||
|
////////////////
|
||||||
|
|
||||||
|
pub const MEM_SIZE: usize = 1 << 16;
|
||||||
|
|
||||||
|
pub struct Memory {
|
||||||
|
data: [u16; MEM_SIZE],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Memory {
|
||||||
|
pub fn new() -> Memory {
|
||||||
|
Memory {
|
||||||
|
data: [0; MEM_SIZE],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_mem(&mut self, addr: u16, val: u16) {
|
||||||
|
self.data[addr as usize] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mem(&self, addr: u16) -> u16 {
|
||||||
|
if addr >= 0xFE00 {
|
||||||
|
unimplemented!("mem-map: {:#X}", addr);
|
||||||
|
}
|
||||||
|
return self.data[addr as usize];
|
||||||
|
}
|
||||||
|
}
|
@ -12,13 +12,14 @@ use byteorder::{BigEndian, ReadBytesExt};
|
|||||||
use std::{fs::File, io::BufReader};
|
use std::{fs::File, io::BufReader};
|
||||||
|
|
||||||
mod instruction;
|
mod instruction;
|
||||||
|
mod memory;
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
// registers
|
// registers
|
||||||
////////////////
|
////////////////
|
||||||
|
|
||||||
// condition flags (COND register)
|
// condition flags (COND register)
|
||||||
pub enum CondFlags {
|
enum CondFlags {
|
||||||
// positive (P)
|
// positive (P)
|
||||||
POS = 1 << 0,
|
POS = 1 << 0,
|
||||||
// zero (Z)
|
// zero (Z)
|
||||||
@ -105,38 +106,12 @@ impl Registers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////
|
|
||||||
// memory
|
|
||||||
////////////////
|
|
||||||
|
|
||||||
const MEM_SIZE: usize = 1 << 16;
|
|
||||||
|
|
||||||
struct Memory {
|
|
||||||
data: [u16; MEM_SIZE],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Memory {
|
|
||||||
fn new() -> Memory {
|
|
||||||
Memory {
|
|
||||||
data: [0; MEM_SIZE],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_mem(&mut self, addr: u16, val: u16) {
|
|
||||||
self.data[addr as usize] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_mem(&self, addr: u16) -> u16 {
|
|
||||||
return self.data[addr as usize];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
// VM interface
|
// VM interface
|
||||||
////////////////
|
////////////////
|
||||||
|
|
||||||
pub struct VM {
|
pub struct VM {
|
||||||
mem: Memory,
|
mem: memory::Memory,
|
||||||
registers: Registers,
|
registers: Registers,
|
||||||
running: bool,
|
running: bool,
|
||||||
}
|
}
|
||||||
@ -144,7 +119,7 @@ pub struct VM {
|
|||||||
impl VM {
|
impl VM {
|
||||||
pub fn new() -> VM {
|
pub fn new() -> VM {
|
||||||
VM {
|
VM {
|
||||||
mem: Memory::new(),
|
mem: memory::Memory::new(),
|
||||||
registers: Registers::new(),
|
registers: Registers::new(),
|
||||||
running: false,
|
running: false,
|
||||||
}
|
}
|
||||||
@ -198,7 +173,7 @@ impl VM {
|
|||||||
// remember PC points to the *next* instruction at all times
|
// remember PC points to the *next* instruction at all times
|
||||||
|
|
||||||
// disallow reading past memory bounds
|
// disallow reading past memory bounds
|
||||||
if self.registers.pc as usize == MEM_SIZE - 1 {
|
if self.registers.pc as usize == memory::MEM_SIZE - 1 {
|
||||||
self.running = false
|
self.running = false
|
||||||
} else {
|
} else {
|
||||||
self.registers.pc += 1;
|
self.registers.pc += 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user