Skip to content

Commit

Permalink
feat: Add the minimal basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Feb 3, 2024
1 parent 65f4c3c commit 57ec8ea
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
14 changes: 5 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb


# Added by cargo

/target
7 changes: 7 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "emurv"
version = "0.1.0"
edition = "2021"

[dependencies]
29 changes: 29 additions & 0 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::memory;

pub struct CPU {
// integer registers
regs: [u64; 32],
pc: u64,
bus: BUS,
}

pub struct BUS {}

const DEFAULT_RSTVEC: u64 = 0x00001000;

impl CPU {
fn new() -> Self {
let mut cpu: CPU = CPU {
regs: [0; 32],
pc: memory::MEMORY_BASE,
bus: BUS {},
};
(cpu).regs[2] = memory::MEMORY_BASE + memory::MEMORY_SIZE; // Set stack pointer
(cpu).pc = memory::MEMORY_BASE;
return cpu;
}

fn fetch() {}

fn execute() {}
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod cpu;
pub mod memory;

fn main() {
println!("Hello, world!");
}
83 changes: 83 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
pub const MEMORY_BASE: u64 = 0x80000000; // defined in QEMU
pub const MEMORY_SIZE: u64 = 1024 * 1024 * 1;

pub struct MEMORY {
mem: [u8; MEMORY_SIZE as usize],
}

impl MEMORY {
fn load(self, addr: u64, size: u64) -> u64 {
match size {
8 => return self.load8(addr),
16 => return self.load16(addr),
32 => return self.load32(addr),
64 => return self.load64(addr),
_ => panic!("wrong load size"),
}
}
fn store(&mut self, addr: u64, size: u64, value: u64) {
match size {
8 => self.store8(addr, value),
16 => self.store16(addr, value),
32 => self.store32(addr, value),
64 => self.store64(addr, value),
_ => panic!("wrong store size"),
}
}

// load funcs
fn load8(self, addr: u64) -> u64 {
let index = (addr - MEMORY_BASE) as usize;
return self.mem[index] as u64;
}
fn load16(self, addr: u64) -> u64 {
let index = (addr - MEMORY_BASE) as usize;
return self.mem[index] as u64 | ((self.mem[index + 1] as u64) << 8);
}
fn load32(self, addr: u64) -> u64 {
let index = (addr - MEMORY_BASE) as usize;
return self.mem[index] as u64
| ((self.mem[index + 1] as u64) << 8)
| ((self.mem[index + 2] as u64) << 16)
| ((self.mem[index + 3] as u64) << 24);
}
fn load64(self, addr: u64) -> u64 {
let index = (addr - MEMORY_BASE) as usize;
return self.mem[index] as u64
| ((self.mem[index + 1] as u64) << 8)
| ((self.mem[index + 2] as u64) << 16)
| ((self.mem[index + 3] as u64) << 24)
| ((self.mem[index + 4] as u64) << 32)
| ((self.mem[index + 5] as u64) << 40)
| ((self.mem[index + 6] as u64) << 48)
| ((self.mem[index + 7] as u64) << 56);
}

// store funcs
fn store8(&mut self, addr: u64, value: u64) {
let index = (addr - MEMORY_BASE) as usize;
self.mem[index] = (value & (std::u8::MAX as u64)) as u8;
}
fn store16(&mut self, addr: u64, value: u64) {
let index = (addr - MEMORY_BASE) as usize;
self.mem[index] = (value & (std::u8::MAX as u64)) as u8;
self.mem[index + 1] = ((value >> 8) & (std::u8::MAX as u64)) as u8;
}
fn store32(&mut self, addr: u64, value: u64) {
let index = (addr - MEMORY_BASE) as usize;
self.mem[index] = (value & (std::u8::MAX as u64)) as u8;
self.mem[index + 1] = ((value >> 8) & (std::u8::MAX as u64)) as u8;
self.mem[index + 2] = ((value >> 16) & (std::u8::MAX as u64)) as u8;
}
fn store64(&mut self, addr: u64, value: u64) {
let index = (addr - MEMORY_BASE) as usize;
self.mem[index] = (value & (std::u8::MAX as u64)) as u8;
self.mem[index + 1] = ((value >> 8) & (std::u8::MAX as u64)) as u8;
self.mem[index + 2] = ((value >> 16) & (std::u8::MAX as u64)) as u8;
self.mem[index + 3] = ((value >> 24) & (std::u8::MAX as u64)) as u8;
self.mem[index + 4] = ((value >> 32) & (std::u8::MAX as u64)) as u8;
self.mem[index + 5] = ((value >> 40) & (std::u8::MAX as u64)) as u8;
self.mem[index + 6] = ((value >> 48) & (std::u8::MAX as u64)) as u8;
self.mem[index + 7] = ((value >> 56) & (std::u8::MAX as u64)) as u8;
}
}
Empty file added src/opcode.rs
Empty file.

0 comments on commit 57ec8ea

Please sign in to comment.