2023-08-17 18:41:05 -05:00
|
|
|
//! Welcome to the land of The Great Dispatch Loop
|
|
|
|
//!
|
|
|
|
//! Have fun
|
|
|
|
|
|
|
|
use {
|
2023-11-02 10:53:44 -05:00
|
|
|
super::{bmc::BlockCopier, mem::Memory, value::ValueVariant, Vm, VmRunError, VmRunOk},
|
2023-10-22 09:17:51 -05:00
|
|
|
crate::{
|
|
|
|
mem::{addr::AddressOp, Address},
|
|
|
|
value::CheckedDivRem,
|
|
|
|
},
|
2023-10-01 09:02:06 -05:00
|
|
|
core::{cmp::Ordering, ops},
|
2023-08-17 18:41:05 -05:00
|
|
|
hbbytecode::{
|
2023-10-22 09:17:51 -05:00
|
|
|
OpsN, OpsO, OpsP, OpsRB, OpsRD, OpsRH, OpsRR, OpsRRA, OpsRRAH, OpsRRB, OpsRRD, OpsRRH,
|
2024-01-31 09:47:17 -06:00
|
|
|
OpsRRO, OpsRROH, OpsRRP, OpsRRPH, OpsRRR, OpsRRRR, OpsRW, RoundingMode,
|
2023-08-17 18:41:05 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-10-01 09:02:06 -05:00
|
|
|
macro_rules! handler {
|
|
|
|
($self:expr, |$ty:ident ($($ident:pat),* $(,)?)| $expr:expr) => {{
|
2023-11-15 12:03:56 -06:00
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
let $ty($($ident),*) = unsafe { $self.decode::<$ty>() };
|
2023-10-18 05:14:24 -05:00
|
|
|
#[allow(clippy::no_effect)] let e = $expr;
|
2023-11-12 17:12:18 -06:00
|
|
|
$self.bump_pc::<$ty>();
|
2023-10-18 05:14:24 -05:00
|
|
|
e
|
2023-10-01 09:02:06 -05:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:41:05 -05:00
|
|
|
impl<Mem, const TIMER_QUOTIENT: usize> Vm<Mem, TIMER_QUOTIENT>
|
|
|
|
where
|
|
|
|
Mem: Memory,
|
|
|
|
{
|
|
|
|
/// Execute program
|
|
|
|
///
|
|
|
|
/// Program can return [`VmRunError`] if a trap handling failed
|
|
|
|
#[cfg_attr(feature = "nightly", repr(align(4096)))]
|
|
|
|
pub fn run(&mut self) -> Result<VmRunOk, VmRunError> {
|
|
|
|
use hbbytecode::opcode::*;
|
|
|
|
loop {
|
|
|
|
// Big match
|
|
|
|
//
|
|
|
|
// Contribution guide:
|
|
|
|
// - Zero register shall never be overwitten. It's value has to always be 0.
|
|
|
|
// - Prefer `Self::read_reg` and `Self::write_reg` functions
|
2023-11-23 09:25:06 -06:00
|
|
|
// - Try to use `handler!` macro for decoding and then bumping program counter
|
2023-08-17 18:41:05 -05:00
|
|
|
// - Prioritise speed over code size
|
|
|
|
// - Memory is cheap, CPUs not that much
|
|
|
|
// - Do not heap allocate at any cost
|
|
|
|
// - Yes, user-provided trap handler may allocate,
|
|
|
|
// but that is not our »fault«.
|
|
|
|
// - Unsafe is kinda must, but be sure you have validated everything
|
2023-11-23 09:25:06 -06:00
|
|
|
// - Your contributions have to pass sanitizers, fuzzer and Miri
|
2023-08-17 18:41:05 -05:00
|
|
|
// - Strictly follow the spec
|
|
|
|
// - The spec does not specify how you perform actions, in what order,
|
|
|
|
// just that the observable effects have to be performed in order and
|
|
|
|
// correctly.
|
|
|
|
// - Yes, we assume you run 64 bit CPU. Else ?conradluget a better CPU
|
|
|
|
// sorry 8 bit fans, HBVM won't run on your Speccy :(
|
|
|
|
unsafe {
|
2023-10-18 05:14:24 -05:00
|
|
|
match self.memory.prog_read::<u8>(self.pc as _) {
|
2023-08-17 18:41:05 -05:00
|
|
|
UN => {
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsN>();
|
2023-08-17 18:41:05 -05:00
|
|
|
return Err(VmRunError::Unreachable);
|
|
|
|
}
|
|
|
|
TX => {
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsN>();
|
2023-08-17 18:41:05 -05:00
|
|
|
return Ok(VmRunOk::End);
|
|
|
|
}
|
2023-10-01 09:02:06 -05:00
|
|
|
NOP => handler!(self, |OpsN()| ()),
|
2023-10-18 05:14:24 -05:00
|
|
|
ADD8 => self.binary_op(u8::wrapping_add),
|
|
|
|
ADD16 => self.binary_op(u16::wrapping_add),
|
|
|
|
ADD32 => self.binary_op(u32::wrapping_add),
|
|
|
|
ADD64 => self.binary_op(u64::wrapping_add),
|
|
|
|
SUB8 => self.binary_op(u8::wrapping_sub),
|
|
|
|
SUB16 => self.binary_op(u16::wrapping_sub),
|
|
|
|
SUB32 => self.binary_op(u32::wrapping_sub),
|
|
|
|
SUB64 => self.binary_op(u64::wrapping_sub),
|
|
|
|
MUL8 => self.binary_op(u8::wrapping_mul),
|
|
|
|
MUL16 => self.binary_op(u16::wrapping_mul),
|
|
|
|
MUL32 => self.binary_op(u32::wrapping_mul),
|
|
|
|
MUL64 => self.binary_op(u64::wrapping_mul),
|
2023-08-17 18:41:05 -05:00
|
|
|
AND => self.binary_op::<u64>(ops::BitAnd::bitand),
|
|
|
|
OR => self.binary_op::<u64>(ops::BitOr::bitor),
|
|
|
|
XOR => self.binary_op::<u64>(ops::BitXor::bitxor),
|
2023-11-08 05:38:14 -06:00
|
|
|
SLU8 => self.binary_op_shift::<u8>(u8::wrapping_shl),
|
|
|
|
SLU16 => self.binary_op_shift::<u16>(u16::wrapping_shl),
|
|
|
|
SLU32 => self.binary_op_shift::<u32>(u32::wrapping_shl),
|
|
|
|
SLU64 => self.binary_op_shift::<u64>(u64::wrapping_shl),
|
|
|
|
SRU8 => self.binary_op_shift::<u8>(u8::wrapping_shr),
|
|
|
|
SRU16 => self.binary_op_shift::<u16>(u16::wrapping_shr),
|
|
|
|
SRU32 => self.binary_op_shift::<u32>(u32::wrapping_shr),
|
|
|
|
SRU64 => self.binary_op_shift::<u64>(u64::wrapping_shr),
|
|
|
|
SRS8 => self.binary_op_shift::<i8>(i8::wrapping_shr),
|
|
|
|
SRS16 => self.binary_op_shift::<i16>(i16::wrapping_shr),
|
|
|
|
SRS32 => self.binary_op_shift::<i32>(i32::wrapping_shr),
|
|
|
|
SRS64 => self.binary_op_shift::<i64>(i64::wrapping_shr),
|
2023-10-22 11:18:50 -05:00
|
|
|
CMPU => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp(
|
|
|
|
tg,
|
|
|
|
a0,
|
|
|
|
self.read_reg(a1).cast::<u64>()
|
|
|
|
)),
|
|
|
|
CMPS => handler!(self, |OpsRRR(tg, a0, a1)| self.cmp(
|
|
|
|
tg,
|
|
|
|
a0,
|
|
|
|
self.read_reg(a1).cast::<i64>()
|
|
|
|
)),
|
2023-10-18 05:14:24 -05:00
|
|
|
DIRU8 => self.dir::<u8>(),
|
|
|
|
DIRU16 => self.dir::<u16>(),
|
|
|
|
DIRU32 => self.dir::<u32>(),
|
|
|
|
DIRU64 => self.dir::<u64>(),
|
|
|
|
DIRS8 => self.dir::<i8>(),
|
|
|
|
DIRS16 => self.dir::<i16>(),
|
|
|
|
DIRS32 => self.dir::<i32>(),
|
|
|
|
DIRS64 => self.dir::<i64>(),
|
2023-10-01 09:02:06 -05:00
|
|
|
NEG => handler!(self, |OpsRR(tg, a0)| {
|
2023-09-30 18:51:51 -05:00
|
|
|
// Bit negation
|
|
|
|
self.write_reg(tg, !self.read_reg(a0).cast::<u64>())
|
2023-10-01 09:02:06 -05:00
|
|
|
}),
|
|
|
|
NOT => handler!(self, |OpsRR(tg, a0)| {
|
2023-08-17 18:41:05 -05:00
|
|
|
// Logical negation
|
2023-09-30 18:51:51 -05:00
|
|
|
self.write_reg(tg, u64::from(self.read_reg(a0).cast::<u64>() == 0));
|
2023-10-01 09:02:06 -05:00
|
|
|
}),
|
2023-10-18 05:14:24 -05:00
|
|
|
SXT8 => handler!(self, |OpsRR(tg, a0)| {
|
|
|
|
self.write_reg(tg, self.read_reg(a0).cast::<i8>() as i64)
|
2023-10-01 09:02:06 -05:00
|
|
|
}),
|
2023-10-18 05:14:24 -05:00
|
|
|
SXT16 => handler!(self, |OpsRR(tg, a0)| {
|
|
|
|
self.write_reg(tg, self.read_reg(a0).cast::<i16>() as i64)
|
|
|
|
}),
|
|
|
|
SXT32 => handler!(self, |OpsRR(tg, a0)| {
|
|
|
|
self.write_reg(tg, self.read_reg(a0).cast::<i32>() as i64)
|
|
|
|
}),
|
|
|
|
ADDI8 => self.binary_op_imm(u8::wrapping_add),
|
|
|
|
ADDI16 => self.binary_op_imm(u16::wrapping_add),
|
|
|
|
ADDI32 => self.binary_op_imm(u32::wrapping_add),
|
|
|
|
ADDI64 => self.binary_op_imm(u64::wrapping_add),
|
2023-12-19 05:36:54 -06:00
|
|
|
MULI8 => self.binary_op_imm(u8::wrapping_mul),
|
|
|
|
MULI16 => self.binary_op_imm(u16::wrapping_mul),
|
|
|
|
MULI32 => self.binary_op_imm(u32::wrapping_mul),
|
|
|
|
MULI64 => self.binary_op_imm(u64::wrapping_mul),
|
2023-08-17 18:41:05 -05:00
|
|
|
ANDI => self.binary_op_imm::<u64>(ops::BitAnd::bitand),
|
|
|
|
ORI => self.binary_op_imm::<u64>(ops::BitOr::bitor),
|
|
|
|
XORI => self.binary_op_imm::<u64>(ops::BitXor::bitxor),
|
2023-11-08 05:38:14 -06:00
|
|
|
SLUI8 => self.binary_op_ims::<u8>(u8::wrapping_shl),
|
|
|
|
SLUI16 => self.binary_op_ims::<u16>(u16::wrapping_shl),
|
|
|
|
SLUI32 => self.binary_op_ims::<u32>(u32::wrapping_shl),
|
|
|
|
SLUI64 => self.binary_op_ims::<u64>(u64::wrapping_shl),
|
|
|
|
SRUI8 => self.binary_op_ims::<u8>(u8::wrapping_shr),
|
|
|
|
SRUI16 => self.binary_op_ims::<u16>(u16::wrapping_shr),
|
|
|
|
SRUI32 => self.binary_op_ims::<u32>(u32::wrapping_shr),
|
|
|
|
SRUI64 => self.binary_op_ims::<u64>(u64::wrapping_shr),
|
|
|
|
SRSI8 => self.binary_op_ims::<i8>(i8::wrapping_shr),
|
|
|
|
SRSI16 => self.binary_op_ims::<i16>(i16::wrapping_shr),
|
|
|
|
SRSI32 => self.binary_op_ims::<i32>(i32::wrapping_shr),
|
|
|
|
SRSI64 => self.binary_op_ims::<i64>(i64::wrapping_shr),
|
2023-10-22 11:18:50 -05:00
|
|
|
CMPUI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm) }),
|
|
|
|
CMPSI => handler!(self, |OpsRRD(tg, a0, imm)| { self.cmp(tg, a0, imm as i64) }),
|
|
|
|
CP => handler!(self, |OpsRR(tg, a0)| self.write_reg(tg, self.read_reg(a0))),
|
2023-10-01 09:02:06 -05:00
|
|
|
SWA => handler!(self, |OpsRR(r0, r1)| {
|
2023-08-17 18:41:05 -05:00
|
|
|
// Swap registers
|
|
|
|
match (r0, r1) {
|
|
|
|
(0, 0) => (),
|
|
|
|
(dst, 0) | (0, dst) => self.write_reg(dst, 0_u64),
|
|
|
|
(r0, r1) => {
|
|
|
|
core::ptr::swap(
|
|
|
|
self.registers.get_unchecked_mut(usize::from(r0)),
|
|
|
|
self.registers.get_unchecked_mut(usize::from(r1)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 09:02:06 -05:00
|
|
|
}),
|
2023-10-18 05:14:24 -05:00
|
|
|
LI8 => handler!(self, |OpsRB(tg, imm)| self.write_reg(tg, imm)),
|
|
|
|
LI16 => handler!(self, |OpsRH(tg, imm)| self.write_reg(tg, imm)),
|
|
|
|
LI32 => handler!(self, |OpsRW(tg, imm)| self.write_reg(tg, imm)),
|
|
|
|
LI64 => handler!(self, |OpsRD(tg, imm)| self.write_reg(tg, imm)),
|
2023-10-22 11:18:50 -05:00
|
|
|
LRA => handler!(self, |OpsRRO(tg, reg, off)| self.write_reg(
|
|
|
|
tg,
|
2024-07-08 00:22:53 -05:00
|
|
|
self.pcrel(off).wrapping_add(self.read_reg(reg).cast::<i64>()).get(),
|
2023-10-22 11:18:50 -05:00
|
|
|
)),
|
|
|
|
// Load. If loading more than register size, continue on adjecent registers
|
|
|
|
LD => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
|
|
|
.load(dst, base, off, count)?),
|
|
|
|
// Store. Same rules apply as to LD
|
|
|
|
ST => handler!(self, |OpsRRAH(dst, base, off, count)| self
|
|
|
|
.store(dst, base, off, count)?),
|
|
|
|
LDR => handler!(self, |OpsRROH(dst, base, off, count)| self.load(
|
|
|
|
dst,
|
|
|
|
base,
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pcrel(off).get(),
|
2023-10-22 11:18:50 -05:00
|
|
|
count
|
|
|
|
)?),
|
|
|
|
STR => handler!(self, |OpsRROH(dst, base, off, count)| self.store(
|
|
|
|
dst,
|
|
|
|
base,
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pcrel(off).get(),
|
2023-10-22 11:18:50 -05:00
|
|
|
count
|
|
|
|
)?),
|
2023-08-17 18:41:05 -05:00
|
|
|
BMC => {
|
|
|
|
// Block memory copy
|
|
|
|
match if let Some(copier) = &mut self.copier {
|
|
|
|
// There is some copier, poll.
|
|
|
|
copier.poll(&mut self.memory)
|
|
|
|
} else {
|
|
|
|
// There is none, make one!
|
2023-09-26 16:36:27 -05:00
|
|
|
let OpsRRH(src, dst, count) = self.decode();
|
2023-08-17 18:41:05 -05:00
|
|
|
|
|
|
|
self.copier = Some(BlockCopier::new(
|
2023-08-17 19:31:49 -05:00
|
|
|
Address::new(self.read_reg(src).cast()),
|
|
|
|
Address::new(self.read_reg(dst).cast()),
|
2023-08-17 18:41:05 -05:00
|
|
|
count as _,
|
|
|
|
));
|
|
|
|
|
|
|
|
self.copier
|
|
|
|
.as_mut()
|
|
|
|
.unwrap_unchecked() // SAFETY: We just assigned there
|
|
|
|
.poll(&mut self.memory)
|
|
|
|
} {
|
|
|
|
// We are done, shift program counter
|
|
|
|
core::task::Poll::Ready(Ok(())) => {
|
|
|
|
self.copier = None;
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsRRH>();
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
|
|
|
// Error, shift program counter (for consistency)
|
|
|
|
// and yield error
|
|
|
|
core::task::Poll::Ready(Err(e)) => {
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
// Not done yet, proceed to next cycle
|
|
|
|
core::task::Poll::Pending => (),
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 09:02:06 -05:00
|
|
|
BRC => handler!(self, |OpsRRB(src, dst, count)| {
|
2023-08-17 18:41:05 -05:00
|
|
|
// Block register copy
|
|
|
|
if src.checked_add(count).is_none() || dst.checked_add(count).is_none() {
|
|
|
|
return Err(VmRunError::RegOutOfBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
core::ptr::copy(
|
|
|
|
self.registers.get_unchecked(usize::from(src)),
|
|
|
|
self.registers.get_unchecked_mut(usize::from(dst)),
|
|
|
|
usize::from(count),
|
|
|
|
);
|
2023-10-01 09:02:06 -05:00
|
|
|
}),
|
2023-10-22 08:08:45 -05:00
|
|
|
JMP => {
|
|
|
|
let OpsO(off) = self.decode();
|
2024-05-11 09:04:13 -05:00
|
|
|
self.pc = self.pc.wrapping_add(off);
|
2023-10-22 08:08:45 -05:00
|
|
|
}
|
|
|
|
JAL => {
|
2023-08-17 18:41:05 -05:00
|
|
|
// Jump and link. Save PC after this instruction to
|
2023-10-19 17:42:45 -05:00
|
|
|
// specified register and jump to reg + relative offset.
|
2023-10-22 08:08:45 -05:00
|
|
|
let OpsRRO(save, reg, offset) = self.decode();
|
|
|
|
|
2024-05-10 08:29:11 -05:00
|
|
|
self.write_reg(save, self.pc.next::<OpsRRO>());
|
2024-07-08 00:22:53 -05:00
|
|
|
self.pc = self.pcrel(offset).wrapping_add(self.read_reg(reg).cast::<i64>());
|
2023-10-22 08:08:45 -05:00
|
|
|
}
|
|
|
|
JALA => {
|
2023-10-19 17:42:45 -05:00
|
|
|
// Jump and link. Save PC after this instruction to
|
|
|
|
// specified register and jump to reg
|
2023-10-22 08:08:45 -05:00
|
|
|
let OpsRRA(save, reg, offset) = self.decode();
|
|
|
|
|
2024-05-10 08:29:11 -05:00
|
|
|
self.write_reg(save, self.pc.next::<OpsRRA>());
|
2023-10-22 08:08:45 -05:00
|
|
|
self.pc =
|
|
|
|
Address::new(self.read_reg(reg).cast::<u64>().wrapping_add(offset));
|
|
|
|
}
|
2023-08-17 18:41:05 -05:00
|
|
|
// Conditional jumps, jump only to immediates
|
|
|
|
JEQ => self.cond_jmp::<u64>(Ordering::Equal),
|
2023-10-22 11:18:50 -05:00
|
|
|
JNE => {
|
|
|
|
let OpsRRP(a0, a1, ja) = self.decode();
|
2023-08-17 18:41:05 -05:00
|
|
|
if self.read_reg(a0).cast::<u64>() != self.read_reg(a1).cast::<u64>() {
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pc = self.pcrel(ja);
|
2023-10-27 20:29:02 -05:00
|
|
|
} else {
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsRRP>();
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
2023-10-22 11:18:50 -05:00
|
|
|
}
|
2024-09-09 15:52:34 -05:00
|
|
|
JLTS => self.cond_jmp::<i64>(Ordering::Less),
|
|
|
|
JGTS => self.cond_jmp::<i64>(Ordering::Greater),
|
|
|
|
JLTU => self.cond_jmp::<u64>(Ordering::Less),
|
|
|
|
JGTU => self.cond_jmp::<u64>(Ordering::Greater),
|
2023-09-30 18:51:51 -05:00
|
|
|
ECA => {
|
2023-08-17 18:41:05 -05:00
|
|
|
// So we don't get timer interrupt after ECALL
|
|
|
|
if TIMER_QUOTIENT != 0 {
|
|
|
|
self.timer = self.timer.wrapping_add(1);
|
|
|
|
}
|
2023-10-01 09:02:06 -05:00
|
|
|
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsN>();
|
2023-08-17 18:41:05 -05:00
|
|
|
return Ok(VmRunOk::Ecall);
|
|
|
|
}
|
2023-09-30 18:51:51 -05:00
|
|
|
EBP => {
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsN>();
|
2023-09-30 18:51:51 -05:00
|
|
|
return Ok(VmRunOk::Breakpoint);
|
|
|
|
}
|
2023-10-18 05:14:24 -05:00
|
|
|
FADD32 => self.binary_op::<f32>(ops::Add::add),
|
|
|
|
FADD64 => self.binary_op::<f64>(ops::Add::add),
|
|
|
|
FSUB32 => self.binary_op::<f32>(ops::Sub::sub),
|
|
|
|
FSUB64 => self.binary_op::<f64>(ops::Sub::sub),
|
|
|
|
FMUL32 => self.binary_op::<f32>(ops::Mul::mul),
|
|
|
|
FMUL64 => self.binary_op::<f64>(ops::Mul::mul),
|
|
|
|
FDIV32 => self.binary_op::<f32>(ops::Div::div),
|
|
|
|
FDIV64 => self.binary_op::<f64>(ops::Div::div),
|
|
|
|
FMA32 => self.fma::<f32>(),
|
|
|
|
FMA64 => self.fma::<f64>(),
|
2023-10-22 11:18:50 -05:00
|
|
|
FINV32 => handler!(self, |OpsRR(tg, reg)| self
|
|
|
|
.write_reg(tg, 1. / self.read_reg(reg).cast::<f32>())),
|
|
|
|
FINV64 => handler!(self, |OpsRR(tg, reg)| self
|
|
|
|
.write_reg(tg, 1. / self.read_reg(reg).cast::<f64>())),
|
2023-10-18 05:14:24 -05:00
|
|
|
FCMPLT32 => self.fcmp::<f32>(Ordering::Less),
|
|
|
|
FCMPLT64 => self.fcmp::<f64>(Ordering::Less),
|
|
|
|
FCMPGT32 => self.fcmp::<f32>(Ordering::Greater),
|
|
|
|
FCMPGT64 => self.fcmp::<f64>(Ordering::Greater),
|
2023-10-22 11:18:50 -05:00
|
|
|
ITF32 => handler!(self, |OpsRR(tg, reg)| self
|
|
|
|
.write_reg(tg, self.read_reg(reg).cast::<i64>() as f32)),
|
|
|
|
ITF64 => handler!(self, |OpsRR(tg, reg)| self
|
|
|
|
.write_reg(tg, self.read_reg(reg).cast::<i64>() as f64)),
|
|
|
|
FTI32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
|
|
|
tg,
|
|
|
|
crate::float::f32toint(
|
|
|
|
self.read_reg(reg).cast::<f32>(),
|
|
|
|
RoundingMode::try_from(mode)
|
|
|
|
.map_err(|()| VmRunError::InvalidOperand)?,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
FTI64 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
|
|
|
tg,
|
|
|
|
crate::float::f64toint(
|
|
|
|
self.read_reg(reg).cast::<f64>(),
|
|
|
|
RoundingMode::try_from(mode)
|
|
|
|
.map_err(|()| VmRunError::InvalidOperand)?,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
FC32T64 => handler!(self, |OpsRR(tg, reg)| self
|
|
|
|
.write_reg(tg, self.read_reg(reg).cast::<f32>() as f64)),
|
|
|
|
FC64T32 => handler!(self, |OpsRRB(tg, reg, mode)| self.write_reg(
|
|
|
|
tg,
|
|
|
|
crate::float::conv64to32(
|
|
|
|
self.read_reg(reg).cast(),
|
|
|
|
RoundingMode::try_from(mode)
|
|
|
|
.map_err(|()| VmRunError::InvalidOperand)?,
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
LRA16 => handler!(self, |OpsRRP(tg, reg, imm)| self.write_reg(
|
|
|
|
tg,
|
|
|
|
(self.pc + self.read_reg(reg).cast::<u64>() + imm + 3_u16).get(),
|
|
|
|
)),
|
|
|
|
LDR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.load(
|
|
|
|
dst,
|
|
|
|
base,
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pcrel(off).get(),
|
2023-10-22 11:18:50 -05:00
|
|
|
count
|
|
|
|
)?),
|
|
|
|
STR16 => handler!(self, |OpsRRPH(dst, base, off, count)| self.store(
|
|
|
|
dst,
|
|
|
|
base,
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pcrel(off).get(),
|
2023-10-22 11:18:50 -05:00
|
|
|
count
|
|
|
|
)?),
|
2023-10-22 08:08:45 -05:00
|
|
|
JMP16 => {
|
|
|
|
let OpsP(off) = self.decode();
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pc = self.pcrel(off);
|
2023-10-22 08:08:45 -05:00
|
|
|
}
|
2023-08-17 18:41:05 -05:00
|
|
|
op => return Err(VmRunError::InvalidOpcode(op)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if TIMER_QUOTIENT != 0 {
|
|
|
|
self.timer = self.timer.wrapping_add(1);
|
|
|
|
if self.timer % TIMER_QUOTIENT == 0 {
|
|
|
|
return Ok(VmRunOk::Timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:02:06 -05:00
|
|
|
/// Bump instruction pointer
|
|
|
|
#[inline(always)]
|
2023-11-12 17:12:18 -06:00
|
|
|
fn bump_pc<T: Copy>(&mut self) {
|
2024-07-08 00:22:53 -05:00
|
|
|
self.pc = self.pc.wrapping_add(core::mem::size_of::<T>()).wrapping_add(1);
|
2023-10-01 09:02:06 -05:00
|
|
|
}
|
|
|
|
|
2023-08-17 18:41:05 -05:00
|
|
|
/// Decode instruction operands
|
|
|
|
#[inline(always)]
|
2023-10-18 05:14:24 -05:00
|
|
|
unsafe fn decode<T: Copy>(&mut self) -> T {
|
2023-11-15 12:03:56 -06:00
|
|
|
unsafe { self.memory.prog_read::<T>(self.pc + 1_u64) }
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
|
|
|
|
2023-09-29 02:10:36 -05:00
|
|
|
/// Load
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn load(
|
|
|
|
&mut self,
|
|
|
|
dst: u8,
|
|
|
|
base: u8,
|
|
|
|
offset: u64,
|
|
|
|
count: u16,
|
|
|
|
) -> Result<(), VmRunError> {
|
|
|
|
let n: u8 = match dst {
|
|
|
|
0 => 1,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
2023-11-15 12:03:56 -06:00
|
|
|
unsafe {
|
|
|
|
self.memory.load(
|
|
|
|
self.ldst_addr_uber(dst, base, offset, count, n)?,
|
2024-07-08 00:22:53 -05:00
|
|
|
self.registers.as_mut_ptr().add(usize::from(dst) + usize::from(n)).cast(),
|
2023-11-15 12:03:56 -06:00
|
|
|
usize::from(count).saturating_sub(n.into()),
|
|
|
|
)
|
|
|
|
}?;
|
2023-09-29 02:10:36 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn store(
|
|
|
|
&mut self,
|
|
|
|
dst: u8,
|
|
|
|
base: u8,
|
|
|
|
offset: u64,
|
|
|
|
count: u16,
|
|
|
|
) -> Result<(), VmRunError> {
|
2023-11-15 12:03:56 -06:00
|
|
|
unsafe {
|
|
|
|
self.memory.store(
|
|
|
|
self.ldst_addr_uber(dst, base, offset, count, 0)?,
|
|
|
|
self.registers.as_ptr().add(usize::from(dst)).cast(),
|
|
|
|
count.into(),
|
|
|
|
)
|
|
|
|
}?;
|
2023-09-29 02:10:36 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-22 11:18:50 -05:00
|
|
|
/// Three-way comparsion
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn cmp<T: ValueVariant + Ord>(&mut self, to: u8, reg: u8, val: T) {
|
|
|
|
self.write_reg(to, self.read_reg(reg).cast::<T>().cmp(&val) as i64);
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:41:05 -05:00
|
|
|
/// Perform binary operating over two registers
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn binary_op<T: ValueVariant>(&mut self, op: impl Fn(T, T) -> T) {
|
2023-11-15 12:03:56 -06:00
|
|
|
let OpsRRR(tg, a0, a1) = unsafe { self.decode() };
|
2024-07-08 00:22:53 -05:00
|
|
|
self.write_reg(tg, op(self.read_reg(a0).cast::<T>(), self.read_reg(a1).cast::<T>()));
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsRRR>();
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform binary operation over register and immediate
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn binary_op_imm<T: ValueVariant>(&mut self, op: impl Fn(T, T) -> T) {
|
2023-11-12 17:12:18 -06:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct OpsRRImm<I>(OpsRR, I);
|
|
|
|
|
2023-11-15 12:03:56 -06:00
|
|
|
let OpsRRImm::<T>(OpsRR(tg, reg), imm) = unsafe { self.decode() };
|
2023-10-18 05:14:24 -05:00
|
|
|
self.write_reg(tg, op(self.read_reg(reg).cast::<T>(), imm));
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsRRImm<T>>();
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform binary operation over register and shift immediate
|
|
|
|
#[inline(always)]
|
2023-11-08 05:38:14 -06:00
|
|
|
unsafe fn binary_op_shift<T: ValueVariant>(&mut self, op: impl Fn(T, u32) -> T) {
|
2023-11-15 12:03:56 -06:00
|
|
|
let OpsRRR(tg, a0, a1) = unsafe { self.decode() };
|
2024-07-08 00:22:53 -05:00
|
|
|
self.write_reg(tg, op(self.read_reg(a0).cast::<T>(), self.read_reg(a1).cast::<u32>()));
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsRRR>();
|
2023-11-08 05:38:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform binary operation over register and shift immediate
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn binary_op_ims<T: ValueVariant>(&mut self, op: impl Fn(T, u32) -> T) {
|
2023-11-15 12:03:56 -06:00
|
|
|
let OpsRRB(tg, reg, imm) = unsafe { self.decode() };
|
2023-11-08 05:38:14 -06:00
|
|
|
self.write_reg(tg, op(self.read_reg(reg).cast::<T>(), imm.into()));
|
2024-01-15 10:59:46 -06:00
|
|
|
self.bump_pc::<OpsRRB>();
|
2023-09-26 16:36:27 -05:00
|
|
|
}
|
|
|
|
|
2023-10-18 05:14:24 -05:00
|
|
|
/// Fused division-remainder
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn dir<T: ValueVariant + CheckedDivRem>(&mut self) {
|
|
|
|
handler!(self, |OpsRRRR(td, tr, a0, a1)| {
|
|
|
|
let a0 = self.read_reg(a0).cast::<T>();
|
|
|
|
let a1 = self.read_reg(a1).cast::<T>();
|
|
|
|
|
|
|
|
if let Some(div) = a0.checked_div(a1) {
|
|
|
|
self.write_reg(td, div);
|
|
|
|
} else {
|
|
|
|
self.write_reg(td, -1_i64);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(rem) = a0.checked_rem(a1) {
|
|
|
|
self.write_reg(tr, rem);
|
|
|
|
} else {
|
|
|
|
self.write_reg(tr, a0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fused multiply-add
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn fma<T>(&mut self)
|
|
|
|
where
|
|
|
|
T: ValueVariant + core::ops::Mul<Output = T> + core::ops::Add<Output = T>,
|
|
|
|
{
|
|
|
|
handler!(self, |OpsRRRR(tg, a0, a1, a2)| {
|
|
|
|
let a0 = self.read_reg(a0).cast::<T>();
|
|
|
|
let a1 = self.read_reg(a1).cast::<T>();
|
|
|
|
let a2 = self.read_reg(a2).cast::<T>();
|
|
|
|
self.write_reg(tg, a0 * a1 + a2)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Float comparsion
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn fcmp<T: PartialOrd + ValueVariant>(&mut self, nan: Ordering) {
|
|
|
|
handler!(self, |OpsRRR(tg, a0, a1)| {
|
|
|
|
let a0 = self.read_reg(a0).cast::<T>();
|
|
|
|
let a1 = self.read_reg(a1).cast::<T>();
|
|
|
|
self.write_reg(tg, (a0.partial_cmp(&a1).unwrap_or(nan) as i8 + 1) as u8)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-22 07:46:45 -05:00
|
|
|
/// Calculate pc-relative address
|
|
|
|
#[inline(always)]
|
2024-05-11 05:51:32 -05:00
|
|
|
fn pcrel(&self, offset: impl AddressOp) -> Address {
|
|
|
|
self.pc.wrapping_add(offset)
|
2023-10-22 07:46:45 -05:00
|
|
|
}
|
|
|
|
|
2023-09-26 16:36:27 -05:00
|
|
|
/// Jump at `PC + #3` if ordering on `#0 <=> #1` is equal to expected
|
2023-08-17 18:41:05 -05:00
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn cond_jmp<T: ValueVariant + Ord>(&mut self, expected: Ordering) {
|
2023-11-15 12:03:56 -06:00
|
|
|
let OpsRRP(a0, a1, ja) = unsafe { self.decode() };
|
2024-07-08 00:22:53 -05:00
|
|
|
if self.read_reg(a0).cast::<T>().cmp(&self.read_reg(a1).cast::<T>()) == expected {
|
2024-05-11 05:51:32 -05:00
|
|
|
self.pc = self.pcrel(ja);
|
2023-10-27 20:29:02 -05:00
|
|
|
} else {
|
2023-11-12 17:12:18 -06:00
|
|
|
self.bump_pc::<OpsRRP>();
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load / Store Address check-computation überfunction
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn ldst_addr_uber(
|
|
|
|
&self,
|
|
|
|
dst: u8,
|
|
|
|
base: u8,
|
|
|
|
offset: u64,
|
|
|
|
size: u16,
|
|
|
|
adder: u8,
|
2023-08-17 19:31:49 -05:00
|
|
|
) -> Result<Address, VmRunError> {
|
2023-08-17 18:41:05 -05:00
|
|
|
let reg = dst.checked_add(adder).ok_or(VmRunError::RegOutOfBounds)?;
|
|
|
|
|
|
|
|
if usize::from(reg) * 8 + usize::from(size) > 2048 {
|
|
|
|
Err(VmRunError::RegOutOfBounds)
|
|
|
|
} else {
|
|
|
|
self.read_reg(base)
|
|
|
|
.cast::<u64>()
|
|
|
|
.checked_add(offset)
|
|
|
|
.and_then(|x| x.checked_add(adder.into()))
|
|
|
|
.ok_or(VmRunError::AddrOutOfBounds)
|
2023-08-17 19:31:49 -05:00
|
|
|
.map(Address::new)
|
2023-08-17 18:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|