Skip to content

Commit

Permalink
fix brk, add mul
Browse files Browse the repository at this point in the history
  • Loading branch information
lenawanel committed Apr 20, 2024
1 parent 78fee75 commit 1f187ed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
40 changes: 38 additions & 2 deletions src/emu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Emu {
where
<T as TryFrom<usize>>::Error: Debug,
{
self.rng = unsafe { self.rng.unchecked_add(1) };
self.rng = self.rng.wrapping_add(1);
self.rng.try_into().unwrap()
}

Expand Down Expand Up @@ -697,7 +697,43 @@ impl Emu {
};
}

match_bitness_ts!(sized_imul);
match bitness(instruction) {
Bitness::Eight => todo!(),
Bitness::Sixteen => sized_imul!(i16, 2),
Bitness::ThirtyTwo => sized_imul!(i32, 4),
Bitness::SixtyFour => sized_imul!(i64, 8),
Bitness::HundredTwentyEigth => unsafe { unreachable_unchecked() },
}
}
Mnemonic::Mul => {
// TODO: handle 8 bit case

macro_rules! sized_idiv {
($typ:ty,$size:literal) => {{
let lhs: $typ = self.get_reg(Register::RAX);
let rhs: $typ = self.get_val(instruction, 0)?;

let (rax, rdx) = lhs.widening_mul(rhs);

self.set_reg::<$typ, $size>(rax, Register::RAX);
self.set_reg::<$typ, $size>(rdx, Register::RDX);
// "CF, OF, SF, ZF, AF, and PF flags are undefined" (https://www.felixcloutier.com/x86/idiv)

self.unset_flag(Flag::CF);
self.unset_flag(Flag::OF);
self.unset_flag(Flag::SF);
self.unset_flag(Flag::ZF);
self.unset_flag(Flag::AUXCF);
self.unset_flag(Flag::PF);
}};
}
match bitness(instruction) {
Bitness::Eight => todo!(),
Bitness::Sixteen => sized_idiv!(u16, 2),
Bitness::ThirtyTwo => sized_idiv!(u32, 4),
Bitness::SixtyFour => sized_idiv!(u64, 8),
Bitness::HundredTwentyEigth => unsafe { unreachable_unchecked() },
}
}
Mnemonic::Idiv => {
// TODO: handle 8 bit case
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(portable_simd)]
#![feature(thread_local)]
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]
#![feature(bigint_helper_methods)]
pub mod emu;
pub mod mmu;
pub mod primitive;
Expand Down
8 changes: 4 additions & 4 deletions src/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ impl MMU {
/// Allocate a region of memory as RW in the address space
/// returns (base, cur_alc)
pub fn allocate(&mut self, size: usize) -> Option<(Virtaddr, Virtaddr)> {
// 32-byte align the allocation
// 16-byte align the allocation
// this is required for SSE memcpy
let align_size = (size + 0x1f) & !0x1f;
let align_size = (size + 0xf) & !0xf;

// Get the current allocation base
let base = self.cur_alc;
Expand Down Expand Up @@ -275,9 +275,9 @@ impl MMU {
/// Allocate a region the size of buf in memery as RW, writing buf to it
/// returns (base, cur_alc)
pub fn allocate_write(&mut self, buf: &[u8]) -> Option<(Virtaddr, Virtaddr)> {
// 32-byte align the allocation
// 16-byte align the allocation
// this is required for SSE memcpy
let align_size = (buf.len() + 0x1f) & !0x1f;
let align_size = (buf.len() + 0xf) & !0xf;

// Get the current allocation base
let base = self.cur_alc;
Expand Down

0 comments on commit 1f187ed

Please sign in to comment.