mirror of
https://github.com/Noratrieb/rustv32i.git
synced 2026-01-14 13:25:01 +01:00
j immediate decoding fix
This commit is contained in:
parent
13aec667db
commit
9cdc795824
3 changed files with 59 additions and 10 deletions
49
src/emu.rs
49
src/emu.rs
|
|
@ -172,12 +172,57 @@ fn hash_color(value: u32) -> impl Display {
|
||||||
|
|
||||||
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?
|
self.setup_linux_stack().unwrap();
|
||||||
self[Reg::SP] = 4096 * 8;
|
|
||||||
|
|
||||||
self.execute()
|
self.execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn setup_linux_stack(&mut self) -> Result<(), Status> {
|
||||||
|
// set top of stack. just some yolo address. with no values there. who needs abi?
|
||||||
|
let sp = 4096 * 16;
|
||||||
|
self[Reg::SP] = sp;
|
||||||
|
|
||||||
|
// The x86-64 psABI has a nice diagram of the stack layout (it's not arch specific).
|
||||||
|
|
||||||
|
// | Purpose | Start Address | Length |
|
||||||
|
// |----------------------------------|--------- -----|----------------|
|
||||||
|
// | Information block of strings etc | (high) | varies |
|
||||||
|
// | Unspecified | | |
|
||||||
|
// | Null auxiliary vector entry | | word |
|
||||||
|
// | Auxiliary vector entries | | two words each |
|
||||||
|
// | 0 | | word |
|
||||||
|
// | Environment pointers | | word each |
|
||||||
|
// | 0 | 4+4*argc+sp | word |
|
||||||
|
// | Argument pointers | 4+sp | argc words |
|
||||||
|
// | Argument count | sp | word |
|
||||||
|
// | Undefined | (low) | |
|
||||||
|
|
||||||
|
let xlen = 4;
|
||||||
|
|
||||||
|
let mut offset: u32 = 0;
|
||||||
|
let mut next_word = || {
|
||||||
|
let n = offset;
|
||||||
|
offset += xlen;
|
||||||
|
sp + n
|
||||||
|
};
|
||||||
|
|
||||||
|
// argc
|
||||||
|
self.mem.store_u32(next_word(), 0)?;
|
||||||
|
|
||||||
|
// null terminated argument pointers... (we have none.)
|
||||||
|
self.mem.store_u32(next_word(), 0)?;
|
||||||
|
|
||||||
|
// null terminated environment pointers... (we have none.)
|
||||||
|
self.mem.store_u32(next_word(), 0)?;
|
||||||
|
|
||||||
|
// null terminated auxiliary vector entries
|
||||||
|
// TODO: add some auxv for the poor process.
|
||||||
|
self.mem.store_u32(next_word(), 0)?;
|
||||||
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn execute(&mut self) -> Status {
|
fn execute(&mut self) -> Status {
|
||||||
loop {
|
loop {
|
||||||
if let Err(err) = self.step() {
|
if let Err(err) = self.step() {
|
||||||
|
|
|
||||||
11
src/inst.rs
11
src/inst.rs
|
|
@ -394,13 +394,8 @@ impl Display for AmoOp {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sign_extend(value: u32, size: u32) -> u32 {
|
fn sign_extend(value: u32, size: u32) -> u32 {
|
||||||
assert!(size <= u32::BITS);
|
let right = u32::BITS - size;
|
||||||
let sign = value >> (size - 1);
|
(((value << right) as i32) >> right) as u32
|
||||||
if sign == 1 {
|
|
||||||
(u32::MAX << size) | value
|
|
||||||
} else {
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
|
@ -462,7 +457,7 @@ impl InstCode {
|
||||||
self.immediate_u(&[(12..=31, 12)])
|
self.immediate_u(&[(12..=31, 12)])
|
||||||
}
|
}
|
||||||
fn imm_j(self) -> u32 {
|
fn imm_j(self) -> u32 {
|
||||||
self.immediate_u(&[(31..=31, 20), (21..=30, 1), (20..=20, 11), (12..=19, 12)])
|
self.immediate_s(&[(31..=31, 20), (21..=30, 1), (20..=20, 11), (12..=19, 12)])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,15 @@ branch6:
|
||||||
FAIL
|
FAIL
|
||||||
branch7:
|
branch7:
|
||||||
|
|
||||||
|
# Backwards jal
|
||||||
|
li t0, 0
|
||||||
|
backwards:
|
||||||
|
li t1, 0
|
||||||
|
bne t0, t1, end_backwards
|
||||||
|
li t0, 1
|
||||||
|
jal backwards
|
||||||
|
end_backwards:
|
||||||
|
|
||||||
# Test link registers being set correctly:
|
# Test link registers being set correctly:
|
||||||
auipc t1, 0
|
auipc t1, 0
|
||||||
jal t0, link2
|
jal t0, link2
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue