fix c.addi16sp

This commit is contained in:
nora 2025-03-10 19:48:09 +01:00
parent 377c8412a7
commit 4965493a0e
4 changed files with 46 additions and 16 deletions

View file

@ -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 { impl Emulator {
pub fn start_linux(&mut self) -> Status { pub fn start_linux(&mut self) -> Status {
// set top of stack. just some yolo address. with no values there. who needs abi? // 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() self.execute()
} }
@ -180,7 +203,15 @@ impl Emulator {
} }
if self.debug { 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 { let next_pc = self.pc.wrapping_add(match was_compressed {
@ -435,7 +466,7 @@ impl Emulator {
Ok(()) Ok(())
} }
fn debug_interactive(&mut self) { pub fn debug_interactive(&mut self) {
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
loop { loop {
print!("> "); print!("> ");
@ -451,6 +482,7 @@ impl Emulator {
"s" => { "s" => {
return; return;
} }
"q" => std::process::exit(0),
"p" => { "p" => {
let format_value = |v: u32| { let format_value = |v: u32| {
if v == 0 { if v == 0 {
@ -554,7 +586,8 @@ impl Emulator {
- ?: help - ?: help
- p: print registers - p: print registers
- c: continue until next breakpoint - c: continue until next breakpoint
- s: step one instruction" - s: step one instruction
- q: quit"
), ),
} }
} }

View file

@ -224,7 +224,11 @@ impl Display for Inst {
} }
} }
Inst::Jalr { offset, base, dest } => { 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::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), Inst::Bne { offset, src1, src2 } => write!(f, "bne {src1}, {src2}, {}", offset as i32),
@ -554,7 +558,6 @@ impl Inst {
if code.0 == 0 { if code.0 == 0 {
return Err(Status::IllegalInstruction(code.into(), "null instruction")); return Err(Status::IllegalInstruction(code.into(), "null instruction"));
} }
let inst = match code.quadrant() { let inst = match code.quadrant() {
// C0 // C0
0b00 => match code.funct3() { 0b00 => match code.funct3() {
@ -695,7 +698,7 @@ impl Inst {
// C.ADDI16SP -> addi sp, sp, \imm // C.ADDI16SP -> addi sp, sp, \imm
2 => Inst::Addi { 2 => Inst::Addi {
imm: code.immediate_s(&[ imm: code.immediate_s(&[
(2..=2, 2), (2..=2, 5),
(3..=4, 7), (3..=4, 7),
(5..=5, 6), (5..=5, 6),
(6..=6, 4), (6..=6, 4),

View file

@ -10,3 +10,6 @@ init: init.c
init1: init1.rs init1: init1.rs
$(RUSTC) init1.rs $(RUSTC) init1.rs
x: x.S
$(CC_STATIC) x.S -o x

View file

@ -93,19 +93,10 @@ fn input() -> u64 {
} }
} }
fn x(a: i32) {}
#[no_mangle] #[no_mangle]
fn _start() -> ! { fn _start() -> ! {
x(0);
x(0);
exit(0);
let random_number = 45; let random_number = 45;
let num = core::sync::atomic::AtomicI32::new(0);
num.swap(10, core::sync::atomic::Ordering::SeqCst);
loop { loop {
let n = input(); let n = input();
if n == random_number { if n == random_number {