From 0b2b39d308b33be6a62587147f489248d2a10834 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Thu, 25 May 2023 00:40:57 +0100 Subject: [PATCH] rust makes no damn sense --- Cargo.lock | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/inst.rs | 13 +++++++++ src/main.rs | 28 +++++++++--------- src/ram.rs | 24 +++++++++------- 5 files changed, 124 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 985844e..6abd02e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "modular-bitfield" version = "0.11.2" @@ -23,6 +29,82 @@ dependencies = [ "syn", ] +[[package]] +name = "num" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "proc-macro2" version = "1.0.58" @@ -46,6 +128,7 @@ name = "riscy-rust" version = "0.1.0" dependencies = [ "modular-bitfield", + "num", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d9c7b83..819cea7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] modular-bitfield = "0.11.2" +num = "0.4.0" diff --git a/src/inst.rs b/src/inst.rs index a6f3e07..fd30b48 100644 --- a/src/inst.rs +++ b/src/inst.rs @@ -1,5 +1,7 @@ use modular_bitfield::prelude::*; +use crate::rv32; + #[bitfield] pub struct RType { opcode: B7, @@ -57,3 +59,14 @@ pub struct JType { imm_10_1: B10, imm_20: B1, } + +#[repr(align(8))] +pub union Instruction { + pub inst: rv32::Word, + pub r: std::mem::ManuallyDrop, + pub i: std::mem::ManuallyDrop, + pub s: std::mem::ManuallyDrop, + pub b: std::mem::ManuallyDrop, + pub u: std::mem::ManuallyDrop, + pub j: std::mem::ManuallyDrop, +} diff --git a/src/main.rs b/src/main.rs index df8443c..487dedb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![feature(unchecked_math)] use std::fs::File; use std::io::BufReader; use std::io::Read; @@ -7,20 +8,8 @@ mod bus; mod ram; mod inst; -use crate::ram::*; use crate::bus::*; -#[repr(align(8))] -union Instruction { - inst: rv32::Word, - r: std::mem::ManuallyDrop, - i: std::mem::ManuallyDrop, - s: std::mem::ManuallyDrop, - b: std::mem::ManuallyDrop, - u: std::mem::ManuallyDrop, - j: std::mem::ManuallyDrop, -} - struct VMRV32I { // 32 bi bus bus: bus::Bus, @@ -55,8 +44,13 @@ impl VMRV32I { } println!("VM > Program loaded to 0x{:08x}", self.pc); + } - println!("VM > WORD at 0x80000000: 0x{:04x}", self.bus.memory.read::(0x80000000)) + fn dump_prog(&mut self) { + println!("VM > Dumping program"); + for i in 0..12 { + println!("VM > 0x{:08x}: 0x{:02x}", i, self.bus.memory.0[i]); + } } fn init_cpu(&mut self) { @@ -70,11 +64,14 @@ impl VMRV32I { self.x[2] = self.bus.memory.len() as u32; // x2 the addressable space } - fn fetch(&mut self) -> Instruction { - Instruction { inst: 0xFFFFFFFF } + fn fetch(&mut self) -> inst::Instruction { + inst::Instruction { inst: 0xFFFFFFFF } } fn exec(&mut self) { + let val: u8 = self.bus.memory.read::(0x80000000) + println!("VM > WORD at 0x80000000: 0x{:08x}", val); + while self.pc > self.bus.memory.len() as u32 { let inst = self.fetch(); } @@ -87,5 +84,6 @@ fn main() { let mut cpu = VMRV32I::new(); cpu.init_cpu(); cpu.load_prog("./test/add.bin"); + cpu.dump_prog(); cpu.exec(); } diff --git a/src/ram.rs b/src/ram.rs index cebd0f4..855d7d2 100644 --- a/src/ram.rs +++ b/src/ram.rs @@ -16,13 +16,13 @@ impl RAM { pub fn read(&mut self, address: rv32::XLen) -> T where - T: std::ops::Shl - + std::ops::BitOr - + From - + From - + From - //+ From + T: num::Num + + num::ToPrimitive + Default + + std::fmt::LowerHex + + std::ops::Shl + + std::ops::BitOr + + num::cast::NumCast, { let address: usize = (address - bus::DRAM_BASE) as usize; let memory = &self.0; @@ -30,10 +30,14 @@ impl RAM { (address..) .take(core::mem::size_of::()) .enumerate() - .fold(T::default(), |acc, (i, x)| acc | (memory[x] << (i * 8)).into()) + .fold(T::default(), |mut acc, (i, x)| { + println!("VM > Reading from 0x{:08x} to 0x{:08x}", x, acc); + println!("VM > Memory: 0x{:02x}", memory[x]); + println!("VM > Now Shift: {}", i * 8); + + acc << u32::from(i as u32 * 8) | memory[x].from() + }) } - pub fn write(&mut self, address: rv32::XLen, data: T) { - - } + pub fn write(&mut self, address: rv32::XLen, data: T) {} }