rust makes no damn sense

This commit is contained in:
Ben Kyd
2023-05-25 00:40:57 +01:00
parent 1f03f4ce84
commit 0b2b39d308
5 changed files with 124 additions and 25 deletions

83
Cargo.lock generated
View File

@@ -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]]

View File

@@ -5,3 +5,4 @@ edition = "2021"
[dependencies]
modular-bitfield = "0.11.2"
num = "0.4.0"

View File

@@ -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<RType>,
pub i: std::mem::ManuallyDrop<IType>,
pub s: std::mem::ManuallyDrop<SType>,
pub b: std::mem::ManuallyDrop<BType>,
pub u: std::mem::ManuallyDrop<UType>,
pub j: std::mem::ManuallyDrop<JType>,
}

View File

@@ -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<inst::RType>,
i: std::mem::ManuallyDrop<inst::IType>,
s: std::mem::ManuallyDrop<inst::SType>,
b: std::mem::ManuallyDrop<inst::BType>,
u: std::mem::ManuallyDrop<inst::UType>,
j: std::mem::ManuallyDrop<inst::JType>,
}
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::<rv32::Word>(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::<rv32::Byte>(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();
}

View File

@@ -16,13 +16,13 @@ impl RAM {
pub fn read<T>(&mut self, address: rv32::XLen) -> T
where
T: std::ops::Shl<Output = T>
+ std::ops::BitOr<Output = T>
+ From<rv32::Byte>
+ From<rv32::HalfWord>
+ From<rv32::Word>
//+ From<rv32::DoubleWord>
T: num::Num
+ num::ToPrimitive
+ Default
+ std::fmt::LowerHex
+ std::ops::Shl<T, Output = T>
+ std::ops::BitOr<T, Output = T>
+ 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::<T>())
.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<T>(&mut self, address: rv32::XLen, data: T) {
}
pub fn write<T>(&mut self, address: rv32::XLen, data: T) {}
}