Skip to content

Commit

Permalink
feat: Add files mod to read binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Feb 23, 2024
1 parent 14c30b6 commit 5e1e75b
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 16 deletions.
216 changes: 216 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
4 changes: 1 addition & 3 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// pub mod cpu;

use crate::memory;
use crate::opcode::*;
use crate::registers;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct CPU {
// integer registers
pub xregs: registers::XREGS,
Expand Down
17 changes: 17 additions & 0 deletions src/files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub fn read_file(filename: &str) -> Vec<u8> {
return std::fs::read(filename).unwrap();
}

#[cfg(test)]
mod tests {
use super::read_file;
#[test]
fn test_read_file() {
let file_byte = read_file("./tests/addi.bin");
for byte in file_byte {
if byte != 0 {
println!("{:x}", byte);
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cpu;
pub mod files;
pub mod memory;
pub mod opcode;
pub mod registers;
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use clap::Parser;

use riscland::cpu;
use riscland::files;

#[derive(Parser, Debug)]
#[command(version)]
struct Args {
// input binary file
#[arg(short, long)]
file: String,
}

fn main() {
let args = Args::parse();
let mut cpu = cpu::CPU::new();
let file_bin = files::read_file(&args.file);
cpu.bus.init_memory(file_bin);
}
22 changes: 13 additions & 9 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// pub mod memory;

pub const MEM_BASE: u32 = 0x80000000; // defined in QEMU
pub const MEM_SIZE: u32 = 1024;
pub const MEM_SIZE: u32 = 1024 * 10;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct BUS {
mem: MEMORY,
}
Expand All @@ -12,23 +10,29 @@ impl BUS {
pub fn new() -> Self {
BUS { mem: MEMORY::new() }
}
pub fn load(self, addr: u32, size: u32) -> u32 {
return self.mem.load(addr, size) as u32;
pub fn load(&self, addr: u32, size: u32) -> u32 {
return self.mem.clone().load(addr, size) as u32;
}
pub fn store(&mut self, addr: u32, size: u32, value: u32) {
self.mem.store(addr, size, value);
}
pub fn init_memory(&mut self, buf: Vec<u8>) {
if buf.len() > MEM_SIZE as usize {
panic!("binary file is bigger than MEM_SIZE");
}
self.mem.mem = buf.as_slice().try_into().expect("failed to read bin file");
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct MEMORY {
mem: [u8; MEM_SIZE as usize],
mem: Vec<u8>,
}

impl MEMORY {
fn new() -> Self {
MEMORY {
mem: [0; MEM_SIZE as usize],
mem: Vec::<u8>::new(),
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/opcode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// pub mod opcode;

pub const LUI: u32 = 0x37;
pub const AUIPC: u32 = 0x17;

Expand Down
2 changes: 0 additions & 2 deletions src/registers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// pub mod registers;

use core::fmt;

#[derive(Clone, Copy)]
Expand Down
Binary file added tests/addi.bin
Binary file not shown.

0 comments on commit 5e1e75b

Please sign in to comment.