From 4965493a0eeec5f717a3fb035c095c853fe1a19e Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Mon, 10 Mar 2025 19:48:09 +0100 Subject: [PATCH] fix c.addi16sp --- src/emu.rs | 41 +++++++++++++++++++++++++++++++++++++---- src/inst.rs | 9 ++++++--- test/Makefile | 3 +++ test/init1.rs | 9 --------- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/emu.rs b/src/emu.rs index 83dcd2e..0b10ee7 100644 --- a/src/emu.rs +++ b/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" ), } } diff --git a/src/inst.rs b/src/inst.rs index 782dbb8..0d73d34 100644 --- a/src/inst.rs +++ b/src/inst.rs @@ -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), diff --git a/test/Makefile b/test/Makefile index 086da9e..1910c02 100644 --- a/test/Makefile +++ b/test/Makefile @@ -10,3 +10,6 @@ init: init.c init1: init1.rs $(RUSTC) init1.rs + +x: x.S + $(CC_STATIC) x.S -o x diff --git a/test/init1.rs b/test/init1.rs index c22f5c7..aa6047b 100644 --- a/test/init1.rs +++ b/test/init1.rs @@ -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 {