use super::operations::RawSource;
use super::softfloat::ExcFlags;
use super::state_040::{
E1, E3, Fpu040Frame, Fpu040State, NMC_UNSUP, SFLAG, TFLAG, UFLAG, classify, off, tag,
tag_unsupported,
};
use crate::core::cpu::CpuCore;
use crate::core::exceptions::vector;
use crate::core::memory::AddressBus;
use crate::fpu::FloatX80;
fn hardware_opmode(opmode: u16) -> bool {
matches!(
opmode,
0x00 | 0x04
| 0x18
| 0x1A
| 0x20
| 0x22
| 0x23
| 0x24
| 0x27
| 0x28
| 0x38
| 0x3A
| 0x40
| 0x41
| 0x44
| 0x45
| 0x58
| 0x5A
| 0x5C
| 0x5E
| 0x60
| 0x62
| 0x63
| 0x64
| 0x66
| 0x67
| 0x68
| 0x6C
)
}
fn nu_opmode(opmode: u16) -> bool {
hardware_opmode(opmode)
&& !matches!(
opmode,
0x00 | 0x18 | 0x1A | 0x38 | 0x3A | 0x40 | 0x44 | 0x58 | 0x5A | 0x5C | 0x5E
)
}
fn operand_exception(vector: u32) -> bool {
matches!(vector, vector::FP_SNAN | vector::FP_OPERR | vector::FP_DZ)
}
pub(super) fn dyadic(opmode: u16) -> bool {
opmode & 0x30 == 0x20 || opmode & 0x7F == 0x38
}
fn latched_command(w2: u16) -> u16 {
if w2 & 0xE07F == 0x0004 || w2 & 0xE07F == 0x4004 {
w2 | 1
} else {
w2
}
}
fn cmdreg3b(w2: u16) -> u16 {
(w2 & 0x03C3) | ((w2 & 0x0038) >> 1) | ((w2 & 0x0004) << 3)
}
#[derive(Clone, Copy)]
struct Latched {
value: FloatX80,
tag: u8,
packed: Option<[u32; 3]>,
}
impl Latched {
fn register(value: FloatX80) -> Self {
Self {
value,
tag: classify(value),
packed: None,
}
}
fn source(raw: RawSource) -> Self {
match raw {
RawSource::Single(bits) if bits & 0x7F80_0000 == 0 && bits & 0x007F_FFFF != 0 => Self {
value: FloatX80::from_extended(
(((bits >> 31) as u16) << 15) | 0x3F80,
u64::from(bits & 0x007F_FFFF) << 40,
),
tag: tag::SD_DENORM,
packed: None,
},
RawSource::Double(bits)
if bits & 0x7FF0_0000_0000_0000 == 0 && bits & 0x000F_FFFF_FFFF_FFFF != 0 =>
{
Self {
value: FloatX80::from_extended(
(((bits >> 63) as u16) << 15) | 0x3C00,
(bits & 0x000F_FFFF_FFFF_FFFF) << 11,
),
tag: tag::SD_DENORM,
packed: None,
}
}
RawSource::Single(bits)
if bits & 0x7F80_0000 == 0x7F80_0000 && bits & 0x007F_FFFF != 0 =>
{
Self {
value: FloatX80::from_extended(
(((bits >> 31) as u16) << 15) | 0x7FFF,
(1 << 63) | (u64::from(bits & 0x007F_FFFF) << 40),
),
tag: tag::NAN,
packed: None,
}
}
RawSource::Double(bits)
if bits & 0x7FF0_0000_0000_0000 == 0x7FF0_0000_0000_0000
&& bits & 0x000F_FFFF_FFFF_FFFF != 0 =>
{
Self {
value: FloatX80::from_extended(
(((bits >> 63) as u16) << 15) | 0x7FFF,
(1 << 63) | ((bits & 0x000F_FFFF_FFFF_FFFF) << 11),
),
tag: tag::NAN,
packed: None,
}
}
RawSource::Packed(longs) => Self {
value: FloatX80::from_extended(
(longs[0] >> 16) as u16,
(u64::from(longs[1]) << 32) | u64::from(longs[2]),
),
tag: tag::NORM,
packed: Some(longs),
},
other => Self::register(other.to_x80(&mut ExcFlags::default())),
}
}
}
impl CpuCore {
fn fpu_040_latch(&mut self, frame: Fpu040Frame, w2: u16, src: Latched, dst: FloatX80) {
let mut st = Fpu040State {
frame,
..Fpu040State::default()
};
st.set_word(off::CMDREG1B, latched_command(w2));
st.set_word(off::CMDREG3B, cmdreg3b(w2));
st.set_extended(off::ETEMP, src.value);
st.set_byte(off::STAG, Fpu040State::tag_byte(src.tag, src.value));
st.set_extended(off::FPTEMP, dst);
st.set_byte(off::DTAG, Fpu040State::tag_byte(classify(dst), dst));
if let Some(longs) = src.packed {
st.set_long(off::FPTEMP + 4, longs[1]);
st.set_long(off::FPTEMP + 8, longs[0]);
}
st.set_long(off::FPIARCU, self.ppc);
self.fpu_040 = st;
}
fn fpu_040_unimp_state(&mut self, w2: u16, src: Latched, dst: FloatX80) {
self.fpu_040_latch(Fpu040Frame::Unimp, w2, src, dst);
self.fpu_040.set_byte(off::T_BYTE, UFLAG);
if src.packed.is_some() {
self.fpu_040.set_byte(off::E_BYTE, E1);
}
}
fn fpu_040_unsupp_state(&mut self, w2: u16, src: Latched, dst: FloatX80) {
self.fpu_040_latch(Fpu040Frame::Busy, w2, src, dst);
self.fpu_040.set_byte(off::E_BYTE, E1);
self.fpu_040.set_byte(off::NMCEXC, NMC_UNSUP);
if w2 & 0xE000 == 0x6000 {
self.fpu_040.set_byte(off::T_BYTE, TFLAG);
}
}
pub(super) fn fpu_040_fmovecr_trap<B: AddressBus>(&mut self, bus: &mut B, w2: u16) -> i32 {
let none = Latched::register(FloatX80::default());
let dst = self.fpr[((w2 >> 7) & 7) as usize];
self.fpu_040_unimp_state(w2, none, dst);
self.take_fp_unimp_060(bus, 0)
}
fn fpu_040_take_unsupp<B: AddressBus>(&mut self, bus: &mut B) -> i32 {
self.fpiar = self.ppc;
self.take_fp_exception_past_instruction(bus, vector::FP_UNSUPP_DATA_TYPE)
}
pub(super) fn fpu_040_ea_source<B: AddressBus>(
&mut self,
bus: &mut B,
opcode: u16,
w2: u16,
) -> Option<i32> {
let src_fmt = (w2 >> 10) & 0x7;
let dst = ((w2 >> 7) & 7) as usize;
let opmode = w2 & 0x7F;
let unimplemented = super::operations::fpu_040_traps_opmode(opmode);
if !unimplemented && !hardware_opmode(opmode) {
return None;
}
let dst_value = self.fpr[dst];
if unimplemented {
let (src, ea) = match self.fpu_fetch_source(bus, opcode, src_fmt) {
Some((raw, ea)) => (Latched::source(raw), ea),
None => (Latched::register(FloatX80::default()), 0),
};
self.fpu_040_unimp_state(w2, src, dst_value);
return Some(self.take_fp_unimp_060(bus, ea));
}
let (raw, _) = self.fpu_fetch_source(bus, opcode, src_fmt)?;
let src = Latched::source(raw);
if src.packed.is_some()
|| tag_unsupported(src.tag)
|| (dyadic(opmode) && tag_unsupported(classify(dst_value)))
{
self.fpu_040_unsupp_state(w2, src, dst_value);
return Some(self.fpu_040_take_unsupp(bus));
}
self.fpiar = self.ppc;
let mut flags = ExcFlags::default();
let value = if src.tag == tag::NAN {
raw.to_x80(&mut flags)
} else {
src.value
};
let registers = super::operations::FprUndo::before(&self.fpr, w2);
let cycles = self.fpu_apply_op(opmode, dst, value, flags);
Some(self.fpu_040_hold_exception(cycles, w2, src.value, ®isters))
}
pub(super) fn fpu_040_register_source<B: AddressBus>(
&mut self,
bus: &mut B,
w2: u16,
) -> Option<i32> {
let src = Latched::register(self.fpr[((w2 >> 10) & 7) as usize]);
let dst = ((w2 >> 7) & 7) as usize;
let opmode = w2 & 0x7F;
let dst_value = self.fpr[dst];
if super::operations::fpu_040_traps_opmode(opmode) {
self.fpu_040_unimp_state(w2, src, dst_value);
return Some(self.take_fp_unimp_060(bus, 0));
}
if !hardware_opmode(opmode) {
return None;
}
if tag_unsupported(src.tag) || (dyadic(opmode) && tag_unsupported(classify(dst_value))) {
self.fpu_040_unsupp_state(w2, src, dst_value);
return Some(self.fpu_040_take_unsupp(bus));
}
self.fpiar = self.ppc;
let registers = super::operations::FprUndo::before(&self.fpr, w2);
let cycles = self.fpu_apply_op(opmode, dst, src.value, ExcFlags::default());
Some(self.fpu_040_hold_exception(cycles, w2, src.value, ®isters))
}
pub(super) fn fpu_040_move_out<B: AddressBus>(
&mut self,
bus: &mut B,
opcode: u16,
w2: u16,
) -> Option<i32> {
let dst_fmt = (w2 >> 10) & 0x7;
let packed = dst_fmt == 3 || dst_fmt == 7;
let value = self.fpr[((w2 >> 7) & 7) as usize];
let src = Latched::register(value);
if !packed && !tag_unsupported(src.tag) {
return None;
}
let _w2 = self.read_imm_16(bus);
let ea = self.fpu_060_trap_ea(bus, opcode, if packed { 3 } else { dst_fmt });
self.fpu_040_unsupp_state(w2, src, value);
Some(self.take_fp_unsupp_post_040(bus, ea))
}
fn fpu_040_arith_vector(&self) -> Option<u32> {
self.fpu_arith_vector(self.trap_unimpl_040())
}
fn fpu_040_stores_result(&self, vector: u32) -> bool {
let left_to_the_fpsp =
self.trap_unimpl_040() && matches!(vector, vector::FP_OVFL | vector::FP_UNFL);
!(operand_exception(vector) || left_to_the_fpsp)
}
#[inline]
pub(super) fn fpu_040_hold_exception(
&mut self,
cycles: i32,
w2: u16,
src: FloatX80,
registers: &super::operations::FprUndo,
) -> i32 {
if cycles != 0
&& self.fpu_may_hold_exception()
&& let Some(vector) = self.fpu_040_arith_vector()
{
self.fpu_040_arith_state(vector, w2, src, registers.dst_before());
if !self.fpu_040_stores_result(vector) {
registers.restore(&mut self.fpr);
}
self.fpu_040.pending = true;
}
cycles
}
fn fpu_040_arith_state(&mut self, vector: u32, w2: u16, src: FloatX80, dst_before: FloatX80) {
let opmode = if w2 & 0xFC00 == 0x5C00 {
0x17
} else {
w2 & 0x7F
};
let result = self.fpr[((w2 >> 7) & 7) as usize];
if operand_exception(vector) {
self.fpu_040_latch(Fpu040Frame::Unimp, w2, Latched::register(src), dst_before);
self.fpu_040.set_byte(off::E_BYTE, E1);
return;
}
let image = self.fpu_unrounded_result(opmode, src, dst_before).map(|u| {
if vector == vector::FP_OVFL {
let (exp, mantissa) = u.rounded();
(u.sign, exp, mantissa, 0)
} else {
let (mantissa, grs) = u.truncated();
(u.sign, u.exp, mantissa, grs)
}
});
if hardware_opmode(opmode) && !nu_opmode(opmode) {
self.fpu_040_latch(Fpu040Frame::Unimp, w2, Latched::register(src), dst_before);
self.fpu_040.set_byte(off::E_BYTE, E1);
if let Some((sign, exp, mantissa, _)) = image {
let e15 = self.fpu_040.set_internal(off::FPTEMP, sign, exp, mantissa);
self.fpu_040
.set_byte(off::DTAG, (tag::NORM << 5) | (u8::from(e15) << 4));
}
return;
}
self.fpu_040_latch(Fpu040Frame::Busy, w2, Latched::register(src), dst_before);
self.fpu_040.set_byte(off::E_BYTE, E3);
match image {
Some((sign, exp, mantissa, grs)) => {
let e15 = self.fpu_040.set_internal(off::WBTEMP, sign, exp, mantissa);
self.fpu_040.set_byte(off::WB_BYTE, u8::from(e15) << 4);
let stag = self.fpu_040.byte(off::STAG);
self.fpu_040.set_byte(off::STAG, stag | (grs >> 1));
self.fpu_040.set_byte(off::SBIT, (grs & 1) << 7);
}
None => {
self.fpu_040.set_extended(off::WBTEMP, result);
self.fpu_040
.set_byte(off::WB_BYTE, u8::from(result.biased_exp() < 0x4000) << 4);
}
}
self.fpu_040.set_long(off::FPSR_SHADOW, self.fpsr);
}
pub(crate) fn fpu_040_pre_instruction<B: AddressBus>(&mut self, bus: &mut B) -> Option<i32> {
if self.fpu_040.is_idle() {
return None;
}
let reported = self.fpu_040_report_pending(bus);
if reported.is_none() {
self.fpu_040.clear();
}
reported
}
pub(crate) fn fpu_040_report_pending<B: AddressBus>(&mut self, bus: &mut B) -> Option<i32> {
if self.fpu_040.pending
&& let Some(vector) = self.fpu_040_pending_vector()
{
self.fpu_040.pending = false;
return Some(self.take_exception(bus, vector));
}
None
}
fn fpu_040_pending_vector(&self) -> Option<u32> {
let st = &self.fpu_040;
let e = st.byte(off::E_BYTE);
if e & (E1 | E3) == 0 {
return None;
}
let busy = st.frame == Fpu040Frame::Busy;
let mut exc = (self.fpsr >> 8) & 0xFF;
let mut nmc = 0;
if busy {
if e & E3 != 0 {
exc |= (st.long(off::FPSR_SHADOW) >> 8) & 0xFF;
}
nmc = st.byte(off::NMCEXC);
exc |= u32::from(((nmc & 0x03) | ((nmc >> 5) & 0x03)) << 3);
}
let enabled = self.fpcr >> 8;
let mut reportable = exc & enabled & 0x7F;
if self.trap_unimpl_040() {
reportable |= exc & 0x18;
} else {
reportable |= (exc & 0x10) >> 3 & enabled;
}
[
(0x40, vector::FP_SNAN),
(0x20, vector::FP_OPERR),
(0x10, vector::FP_OVFL),
(0x08, vector::FP_UNFL),
(0x04, vector::FP_DZ),
(0x03, vector::FP_INEX),
]
.into_iter()
.find(|&(bit, _)| reportable & bit != 0)
.map(|(_, v)| v)
.or_else(|| {
(busy && (nmc & NMC_UNSUP != 0 || st.byte(off::NMNEXC) & 0x02 != 0))
.then_some(vector::FP_UNSUPP_DATA_TYPE)
})
}
fn fpu_040_frame_longs(&self) -> ([u32; 25], usize) {
let mut longs = [0u32; 25];
match self.fpu_040.frame {
Fpu040Frame::Idle => {
longs[0] = 0x4100_0000;
(longs, 1)
}
Fpu040Frame::Unimp => {
longs[0] = 0x4130_0000;
let first = (off::UNIMP_START - 4) / 4;
longs[1..13].copy_from_slice(&self.fpu_040.longs[first..]);
(longs, 13)
}
Fpu040Frame::Busy => {
longs[0] = 0x4160_0000;
longs[1..25].copy_from_slice(&self.fpu_040.longs);
(longs, 25)
}
}
}
pub(super) fn fpu_040_fsave<B: AddressBus>(
&mut self,
bus: &mut B,
ea_mode: u8,
ea_reg: usize,
) -> i32 {
let (longs, count) = self.fpu_040_frame_longs();
let bytes = 4 * count as u32;
let addr = match ea_mode {
3 => {
let addr = self.a(ea_reg);
self.set_a(ea_reg, addr.wrapping_add(bytes));
addr
}
4 => {
let addr = self.a(ea_reg).wrapping_sub(bytes);
self.set_a(ea_reg, addr);
addr
}
_ => match self.fpu_frame_control_ea(bus, ea_mode, ea_reg, false) {
Some(addr) => addr,
None => return 0,
},
};
for (i, long) in longs.iter().take(count).enumerate() {
self.write_32(bus, addr.wrapping_add(4 * i as u32), *long);
}
self.fpu_040.clear();
8
}
pub(super) fn fpu_040_frestore<B: AddressBus>(&mut self, bus: &mut B, addr: u32, header: u32) {
let size = ((header >> 16) & 0xFF) as usize;
let mut st = Fpu040State::default();
match size {
0x60 => st.frame = Fpu040Frame::Busy,
0x30 | 0x28 => st.frame = Fpu040Frame::Unimp,
_ => {
self.fpu_040.clear();
return;
}
}
let first = st.longs.len() - size / 4;
for i in 0..size / 4 {
st.longs[first + i] = self.read_32(bus, addr.wrapping_add(4 + 4 * i as u32));
}
let e = st.byte(off::E_BYTE);
if st.frame == Fpu040Frame::Busy && st.byte(off::CU_SAVEPC) == 0xFE {
self.fpu_040.clear();
self.fpu_040_resume(&st);
} else if e & (E1 | E3) != 0 {
st.pending = true;
self.fpu_040 = st;
} else if st.frame == Fpu040Frame::Unimp {
self.fpu_040 = st;
} else {
self.fpu_040.clear();
}
}
fn fpu_040_resume(&mut self, st: &Fpu040State) {
let w2 = st.word(off::CMDREG1B);
if w2 & 0xA000 != 0 {
return;
}
let opmode = match w2 & 0x7F {
0x05 => 0x04, other => other,
};
if !hardware_opmode(opmode) {
return;
}
let dst = ((w2 >> 7) & 7) as usize;
let src = st.operand(off::ETEMP, st.byte(off::STAG) & 0x10 != 0);
let register = self.fpr[dst];
if dyadic(opmode) {
self.fpr[dst] = st.operand(off::FPTEMP, st.byte(off::DTAG) & 0x10 != 0);
}
let dst_before = self.fpr[dst];
let carried = if st.byte(off::E_BYTE) & SFLAG != 0 {
ExcFlags::from_bits((st.long(off::FPSR_SHADOW) >> 8) as u8)
} else {
ExcFlags::default()
};
self.fpu_apply_op(opmode, dst, src, carried);
if matches!(opmode, 0x38 | 0x3A) {
self.fpr[dst] = register;
}
if let Some(vector) = self.fpu_040_arith_vector() {
let fpiarcu = st.long(off::FPIARCU);
self.fpu_040_arith_state(vector, (w2 & !0x7F) | opmode, src, dst_before);
self.fpu_040.set_long(off::FPIARCU, fpiarcu);
self.fpu_040.pending = true;
if !self.fpu_040_stores_result(vector) {
self.fpr[dst] = register;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn command_words_are_reencoded_for_the_frame() {
assert_eq!(latched_command(0x0404), 0x0405, "FSQRT FP1,FP0");
assert_eq!(latched_command(0x4804), 0x4805, "FSQRT.X <ea>,FP0");
assert_eq!(latched_command(0x0441), 0x0441, "FSSQRT keeps its opmode");
assert_eq!(latched_command(0x6804), 0x6804, "an FMOVE out k-factor");
assert_eq!(cmdreg3b(0x0027) & 0x7F, 0x33);
assert_eq!(cmdreg3b(0x0024) & 0x7F, 0x30);
assert_eq!(cmdreg3b(0x0062) & 0x60, 0x40);
assert_eq!(cmdreg3b(0x0066) & 0x60, 0x60);
assert_eq!(cmdreg3b(0x0380) & 0x0380, 0x0380, "register field kept");
}
#[test]
fn the_intermediate_result_rounds_to_what_the_instruction_stores() {
use crate::core::types::CpuType;
let a = FloatX80::from_extended(0x4001, 0xC90F_DAA2_2168_C235);
let b = FloatX80::from_extended(0xBFFD, 0xAAAA_AAAA_AAAA_AAAB);
let mut cpu = CpuCore::new();
cpu.set_cpu_type(CpuType::M68040);
let mut seen = 0;
for opmode in 0..0x80u16 {
if !hardware_opmode(opmode) || matches!(opmode, 0x38 | 0x3A) {
continue;
}
for fpcr in [0x00, 0x10, 0x20, 0x30, 0x40, 0x80, 0xB0] {
for (src, dst) in [(a, b), (b, a), (a, a)] {
cpu.fpcr = fpcr;
cpu.fpr[0] = dst;
cpu.fpu_apply_op(opmode, 0, src, ExcFlags::default());
let result = cpu.fpr[0];
if result.is_nan() || result.is_zero() {
continue; }
let u = cpu
.fpu_unrounded_result(opmode, src, dst)
.unwrap_or_else(|| panic!("{opmode:#x}: no intermediate"));
let (exp, mantissa) = u.rounded();
assert_eq!(
FloatX80::from_extended((u16::from(u.sign) << 15) | exp as u16, mantissa),
result,
"{opmode:#x} fpcr {fpcr:#x}"
);
seen += 1;
}
}
}
assert!(seen > 300, "{seen}");
assert_eq!(cpu.fpu_unrounded_result(0x0E, a, b), None, "FSIN");
assert_eq!(cpu.fpu_unrounded_result(0x38, a, b), None, "FCMP");
}
#[test]
fn overflow_and_inexact_of_a_move_are_conversion_unit_exceptions() {
for opmode in [0x00, 0x18, 0x1A, 0x40, 0x44, 0x58, 0x5A, 0x5C, 0x5E] {
assert!(hardware_opmode(opmode) && !nu_opmode(opmode), "{opmode:#x}");
}
for opmode in [
0x04, 0x41, 0x45, 0x20, 0x60, 0x64, 0x22, 0x62, 0x66, 0x23, 0x63, 0x67, 0x24, 0x27,
0x28, 0x68, 0x6C,
] {
assert!(nu_opmode(opmode), "{opmode:#x}");
}
assert!(!nu_opmode(0x0E), "FSIN is not a hardware instruction");
}
#[test]
fn dyadic_matches_the_fpsp_classification() {
for opmode in [
0x20, 0x22, 0x23, 0x24, 0x27, 0x28, 0x38, 0x60, 0x67, 0x6C, 0x21,
] {
assert!(dyadic(opmode), "{opmode:#x}");
}
for opmode in [0x00, 0x04, 0x18, 0x1A, 0x3A, 0x40, 0x45, 0x5E, 0x0E, 0x30] {
assert!(!dyadic(opmode), "{opmode:#x}");
}
}
#[test]
fn frame_operands_take_exponent_bit_15() {
let mut st = Fpu040State::default();
st.set_extended(off::ETEMP, FloatX80::from_extended(0xFFFF, 1 << 63));
assert_eq!(
st.operand(off::ETEMP, true),
FloatX80::from_extended(0x8000, 1 << 62)
);
st.set_extended(off::ETEMP, FloatX80::from_extended(0x3FFE, 1 << 63));
assert_eq!(
st.operand(off::ETEMP, true),
FloatX80::from_extended(0x3FFE, 1 << 63)
);
assert_eq!(
st.operand(off::ETEMP, false),
FloatX80::from_extended(0x3FFE, 1 << 63)
);
st.set_extended(off::ETEMP, FloatX80::from_extended(0x7F00, 1 << 63));
assert_eq!(st.operand(off::ETEMP, true), FloatX80::zero(false));
}
#[test]
fn state_bytes_and_words_address_big_endian_longs() {
let mut st = Fpu040State::default();
st.set_byte(off::E_BYTE, 0x04);
st.set_byte(off::T_BYTE, 0x20);
assert_eq!(st.long(0x48), 0x0420_0000);
st.set_word(off::CMDREG1B, 0x4822);
assert_eq!(st.long(0x40), 0x4822_0000);
assert_eq!(st.word(off::CMDREG1B), 0x4822);
assert_eq!(st.byte(off::T_BYTE), 0x20);
st.set_byte(off::NMCEXC, 0x10);
assert_eq!(st.long(0x38), 0x0010_0000);
}
}