-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the minimal basic implementation
- Loading branch information
Showing
7 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "emurv" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.