use super::exceptions_040::dyadic;
use super::operations::RawSource;
use super::state_040::{classify, tag_unsupported};
use crate::core::cpu::CpuCore;
use crate::core::exceptions::vector;
use crate::core::memory::AddressBus;
use crate::fpu::FloatX80;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub(crate) struct Fpu060Excp {
pub(crate) status: u8,
pub(crate) operand: FloatX80,
}
const STATUS_UNSUPP: u8 = 7;
impl Fpu060Excp {
pub(crate) fn new(vector: u32, operand: FloatX80) -> Self {
Self {
status: (vector - vector::FP_BSUN) as u8,
operand,
}
}
pub(crate) fn unsupported(operand: FloatX80) -> Self {
Self {
status: STATUS_UNSUPP,
operand,
}
}
pub(crate) fn vector(self) -> u32 {
if self.status == STATUS_UNSUPP {
vector::FP_UNSUPP_DATA_TYPE
} else {
vector::FP_BSUN + u32::from(self.status)
}
}
pub(crate) fn frame(self) -> [u32; 3] {
let (exp_word, mantissa) = self.operand.to_extended();
[
(u32::from(exp_word) << 16) | 0xE000 | u32::from(self.status),
(mantissa >> 32) as u32,
mantissa as u32,
]
}
pub(crate) fn from_frame(longs: [u32; 3]) -> Self {
Self {
status: (longs[0] & 7) as u8,
operand: FloatX80::from_extended(
(longs[0] >> 16) as u16,
(u64::from(longs[1]) << 32) | u64::from(longs[2]),
),
}
}
}
pub(super) fn register_unsupported(value: FloatX80) -> bool {
tag_unsupported(classify(value))
}
pub(super) fn source_unsupported(raw: RawSource) -> bool {
match raw {
RawSource::Single(bits) => bits & 0x7F80_0000 == 0 && bits & 0x007F_FFFF != 0,
RawSource::Double(bits) => {
bits & 0x7FF0_0000_0000_0000 == 0 && bits & 0x000F_FFFF_FFFF_FFFF != 0
}
RawSource::Extended(exp_word, mantissa) => {
register_unsupported(FloatX80::from_extended(exp_word, mantissa))
}
_ => false,
}
}
impl CpuCore {
pub(super) fn fpu_060_unsupp_operands(&self, w2: u16, source: bool) -> bool {
self.trap_unimpl_060()
&& (source
|| (dyadic(w2 & 0x7F) && register_unsupported(self.fpr[((w2 >> 7) & 7) as usize])))
}
pub(super) fn fpu_060_take_unsupp<B: AddressBus>(
&mut self,
bus: &mut B,
operand: FloatX80,
) -> i32 {
self.fpiar = self.ppc;
self.fpu_060_excp = Some(Fpu060Excp::unsupported(operand));
self.take_fp_exception_past_instruction(bus, vector::FP_UNSUPP_DATA_TYPE)
}
pub(super) fn fpu_060_move_out_unsupp<B: AddressBus>(
&mut self,
bus: &mut B,
opcode: u16,
w2: u16,
) -> Option<i32> {
let value = self.fpr[((w2 >> 7) & 7) as usize];
if !self.trap_unimpl_060() || !register_unsupported(value) {
return None;
}
let _w2 = self.read_imm_16(bus);
let ea = self.fpu_060_move_out_ea(bus, opcode, (w2 >> 10) & 7);
self.fpu_060_excp = Some(Fpu060Excp::unsupported(value));
Some(self.take_fp_unsupp_post_040(bus, ea))
}
pub(super) fn fpu_060_source_operand(&self, raw: RawSource, value: FloatX80) -> FloatX80 {
let (sign, exp_field, fraction, rebias, max) = match raw {
RawSource::Single(bits) => (
bits >> 31 != 0,
((bits >> 23) & 0xFF) as u16,
u64::from(bits & 0x007F_FFFF) << 40,
0x3F80,
0xFF,
),
RawSource::Double(bits) => (
bits >> 63 != 0,
((bits >> 52) & 0x7FF) as u16,
(bits & 0x000F_FFFF_FFFF_FFFF) << 11,
0x3C00,
0x7FF,
),
_ => return value,
};
let sign = u16::from(sign) << 15;
if self.trap_unimpl_060() {
FloatX80::from_extended(sign | (exp_field + rebias), (1 << 63) | fraction)
} else if exp_field == max {
FloatX80::from_extended(sign | 0x7FFF, fraction)
} else {
value
}
}
fn fpu_060_stores_result(&self, vector: u32) -> bool {
!self.trap_unimpl_060()
&& !matches!(vector, vector::FP_SNAN | vector::FP_OPERR | vector::FP_DZ)
}
fn fpu_060_exception_operand(
&self,
w2: u16,
operand: FloatX80,
src: FloatX80,
dst_before: FloatX80,
) -> FloatX80 {
let raised = self.fpsr >> 8;
if self.trap_unimpl_060() || raised & 0x18 == 0 {
return operand;
}
let opmode = if w2 & 0xFC00 == 0x5C00 {
0x17
} else {
w2 & 0x7F
};
let Some(unrounded) = self.fpu_unrounded_result(opmode, src, dst_before) else {
return operand;
};
let (exp, mantissa) = unrounded.rounded_extended();
let exp = if raised & 0x10 != 0 {
exp - 0x6000
} else {
exp + 0x6000
};
let exp = if (0..=0x7FFF).contains(&exp) { exp } else { 0 };
FloatX80::from_extended((u16::from(unrounded.sign) << 15) | exp as u16, mantissa)
}
#[inline]
pub(super) fn fpu_060_hold_exception(
&mut self,
cycles: i32,
w2: u16,
operand: FloatX80,
src: FloatX80,
registers: &super::operations::FprUndo,
) -> i32 {
if cycles != 0
&& self.fpu_may_hold_exception()
&& let Some(vector) = self.fpu_arith_vector(self.trap_unimpl_060())
{
let dst_before = registers.dst_before();
let operand = self.fpu_060_exception_operand(w2, operand, src, dst_before);
self.fpu_060_excp = Some(Fpu060Excp::new(vector, operand));
self.fpu_exception_pending = vector as u8;
if !self.fpu_060_stores_result(vector) {
registers.restore(&mut self.fpr);
}
}
cycles
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::types::CpuType;
fn cpu(emulate: bool) -> CpuCore {
let mut cpu = CpuCore::new();
cpu.set_cpu_type(CpuType::M68060);
cpu.emulate_unimplemented_060 = emulate;
cpu
}
#[test]
fn excp_frame_round_trips() {
let operand = FloatX80::from_extended(0xC002, 0xA000_0000_0000_0001);
let state = Fpu060Excp::new(vector::FP_OVFL, operand);
assert_eq!(state.frame(), [0xC002_E005, 0xA000_0000, 0x0000_0001]);
assert_eq!(Fpu060Excp::from_frame(state.frame()), state);
assert_eq!(state.vector(), vector::FP_OVFL);
assert_eq!(
Fpu060Excp::from_frame([0x0000_E007, 0, 0]).vector(),
vector::FP_UNSUPP_DATA_TYPE
);
}
#[test]
fn single_and_double_sources_are_widened_without_their_special_cases() {
let hw = cpu(false);
let sp = cpu(true);
let x = |e, m| FloatX80::from_extended(e, m);
let cases = [
(
RawSource::Single(0x3FC0_0000),
x(0x3FFF, 0xC000_0000_0000_0000),
x(0x3FFF, 0xC000_0000_0000_0000),
),
(
RawSource::Single(0),
x(0x3F80, 1 << 63),
FloatX80::zero(false),
),
(
RawSource::Double(1 << 63),
x(0xBC00, 1 << 63),
FloatX80::zero(true),
),
(
RawSource::Single(0x007F_FFFF),
x(0x3F80, 0xFFFF_FF00_0000_0000),
x(0x3F80, 0xFFFF_FE00_0000_0000),
),
(
RawSource::Single(0x7F80_0000),
x(0x407F, 1 << 63),
x(0x7FFF, 0),
),
(
RawSource::Double(0x7FF0_0000_0000_0001),
x(0x43FF, 0x8000_0000_0000_0800),
x(0x7FFF, 0x0000_0000_0000_0800),
),
];
for (raw, hardware, corrected) in cases {
let value = raw.to_x80(&mut Default::default());
assert_eq!(hw.fpu_060_source_operand(raw, value), hardware);
assert_eq!(sp.fpu_060_source_operand(raw, value), corrected);
}
let raw = RawSource::Long(0xFFFF_FFFE);
let value = raw.to_x80(&mut Default::default());
assert_eq!(hw.fpu_060_source_operand(raw, value), value);
}
}