mirror of
https://github.com/Noratrieb/rustv32i.git
synced 2026-01-14 13:25:01 +01:00
fix c.addi16sp
This commit is contained in:
parent
377c8412a7
commit
4965493a0e
4 changed files with 46 additions and 16 deletions
41
src/emu.rs
41
src/emu.rs
|
|
@ -150,10 +150,33 @@ impl Display for Reg {
|
|||
}
|
||||
}
|
||||
|
||||
fn hash_color(value: u32) -> impl Display {
|
||||
use owo_colors::*;
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut w = std::collections::hash_map::DefaultHasher::new();
|
||||
value.hash(&mut w);
|
||||
let hash = w.finish();
|
||||
let arr = [
|
||||
AnsiColors::Black,
|
||||
AnsiColors::Red,
|
||||
AnsiColors::Green,
|
||||
AnsiColors::Yellow,
|
||||
AnsiColors::Blue,
|
||||
AnsiColors::Magenta,
|
||||
AnsiColors::Cyan,
|
||||
AnsiColors::White,
|
||||
AnsiColors::Default,
|
||||
];
|
||||
format!(
|
||||
"{}",
|
||||
format!("{value:x}").color(arr[(hash % arr.len() as u64) as usize])
|
||||
)
|
||||
}
|
||||
|
||||
impl Emulator {
|
||||
pub fn start_linux(&mut self) -> Status {
|
||||
// set top of stack. just some yolo address. with no values there. who needs abi?
|
||||
self[Reg::SP] = 4096;
|
||||
self[Reg::SP] = 4096 * 8;
|
||||
|
||||
self.execute()
|
||||
}
|
||||
|
|
@ -180,7 +203,15 @@ impl Emulator {
|
|||
}
|
||||
|
||||
if self.debug {
|
||||
println!("executing 0x{:x} {inst:?}", self.pc);
|
||||
println!(
|
||||
"0x{:x} {} (sp: {}) {inst:?}",
|
||||
self.pc,
|
||||
match was_compressed {
|
||||
IsCompressed::Yes => "C",
|
||||
IsCompressed::No => " ",
|
||||
},
|
||||
hash_color(self.xreg[Reg::SP.0 as usize])
|
||||
);
|
||||
}
|
||||
|
||||
let next_pc = self.pc.wrapping_add(match was_compressed {
|
||||
|
|
@ -435,7 +466,7 @@ impl Emulator {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn debug_interactive(&mut self) {
|
||||
pub fn debug_interactive(&mut self) {
|
||||
use owo_colors::OwoColorize;
|
||||
loop {
|
||||
print!("> ");
|
||||
|
|
@ -451,6 +482,7 @@ impl Emulator {
|
|||
"s" => {
|
||||
return;
|
||||
}
|
||||
"q" => std::process::exit(0),
|
||||
"p" => {
|
||||
let format_value = |v: u32| {
|
||||
if v == 0 {
|
||||
|
|
@ -554,7 +586,8 @@ impl Emulator {
|
|||
- ?: help
|
||||
- p: print registers
|
||||
- c: continue until next breakpoint
|
||||
- s: step one instruction"
|
||||
- s: step one instruction
|
||||
- q: quit"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -224,7 +224,11 @@ impl Display for Inst {
|
|||
}
|
||||
}
|
||||
Inst::Jalr { offset, base, dest } => {
|
||||
write!(f, "jalr {dest}, {}({base})", offset as i32)
|
||||
if dest == Reg::ZERO && offset == 0 && base == Reg::RA {
|
||||
write!(f, "ret")
|
||||
} else {
|
||||
write!(f, "jalr {dest}, {}({base})", offset as i32)
|
||||
}
|
||||
}
|
||||
Inst::Beq { offset, src1, src2 } => write!(f, "beq {src1}, {src2}, {}", offset as i32),
|
||||
Inst::Bne { offset, src1, src2 } => write!(f, "bne {src1}, {src2}, {}", offset as i32),
|
||||
|
|
@ -554,7 +558,6 @@ impl Inst {
|
|||
if code.0 == 0 {
|
||||
return Err(Status::IllegalInstruction(code.into(), "null instruction"));
|
||||
}
|
||||
|
||||
let inst = match code.quadrant() {
|
||||
// C0
|
||||
0b00 => match code.funct3() {
|
||||
|
|
@ -695,7 +698,7 @@ impl Inst {
|
|||
// C.ADDI16SP -> addi sp, sp, \imm
|
||||
2 => Inst::Addi {
|
||||
imm: code.immediate_s(&[
|
||||
(2..=2, 2),
|
||||
(2..=2, 5),
|
||||
(3..=4, 7),
|
||||
(5..=5, 6),
|
||||
(6..=6, 4),
|
||||
|
|
|
|||
|
|
@ -10,3 +10,6 @@ init: init.c
|
|||
|
||||
init1: init1.rs
|
||||
$(RUSTC) init1.rs
|
||||
|
||||
x: x.S
|
||||
$(CC_STATIC) x.S -o x
|
||||
|
|
|
|||
|
|
@ -93,19 +93,10 @@ fn input() -> u64 {
|
|||
}
|
||||
}
|
||||
|
||||
fn x(a: i32) {}
|
||||
|
||||
|
||||
#[no_mangle]
|
||||
fn _start() -> ! {
|
||||
x(0);
|
||||
x(0);
|
||||
exit(0);
|
||||
let random_number = 45;
|
||||
|
||||
let num = core::sync::atomic::AtomicI32::new(0);
|
||||
num.swap(10, core::sync::atomic::Ordering::SeqCst);
|
||||
|
||||
loop {
|
||||
let n = input();
|
||||
if n == random_number {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue