2016-08-05 18:54:19 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
#include "backend_x64/a32_jitstate.h"
|
2016-08-13 00:10:23 +01:00
|
|
|
#include "backend_x64/block_of_code.h"
|
2016-08-05 18:54:19 +01:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/bit_util.h"
|
|
|
|
#include "common/common_types.h"
|
2018-01-01 15:23:56 +00:00
|
|
|
#include "frontend/A32/location_descriptor.h"
|
2016-08-05 18:54:19 +01:00
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
namespace Dynarmic::BackendX64 {
|
2016-08-05 18:54:19 +01:00
|
|
|
|
2017-12-02 13:55:04 +00:00
|
|
|
/**
|
|
|
|
* CPSR Bits
|
|
|
|
* =========
|
|
|
|
*
|
|
|
|
* ARM CPSR flags
|
|
|
|
* --------------
|
|
|
|
* N bit 31 Negative flag
|
|
|
|
* Z bit 30 Zero flag
|
|
|
|
* C bit 29 Carry flag
|
|
|
|
* V bit 28 oVerflow flag
|
|
|
|
* Q bit 27 Saturation flag
|
|
|
|
* J bit 24 Jazelle instruction set flag
|
|
|
|
* GE bits 16-19 Greater than or Equal flags
|
|
|
|
* E bit 9 Data Endianness flag
|
|
|
|
* A bit 8 Disable imprecise Aborts
|
|
|
|
* I bit 7 Disable IRQ interrupts
|
|
|
|
* F bit 6 Disable FIQ interrupts
|
|
|
|
* T bit 5 Thumb instruction set flag
|
|
|
|
* M bits 0-4 Processor Mode bits
|
|
|
|
*
|
|
|
|
* x64 LAHF+SETO flags
|
|
|
|
* -------------------
|
|
|
|
* SF bit 15 Sign flag
|
|
|
|
* ZF bit 14 Zero flag
|
|
|
|
* AF bit 12 Auxiliary flag
|
|
|
|
* PF bit 10 Parity flag
|
|
|
|
* CF bit 8 Carry flag
|
|
|
|
* OF bit 0 Overflow flag
|
|
|
|
*/
|
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
u32 A32JitState::Cpsr() const {
|
2017-12-09 15:42:47 +00:00
|
|
|
ASSERT((CPSR_nzcv & ~0xF0000000) == 0);
|
|
|
|
ASSERT((CPSR_q & ~1) == 0);
|
|
|
|
ASSERT((CPSR_et & ~3) == 0);
|
|
|
|
ASSERT((CPSR_jaifm & ~0x010001DF) == 0);
|
|
|
|
|
2017-12-02 14:03:54 +00:00
|
|
|
u32 cpsr = 0;
|
|
|
|
|
2017-12-09 15:42:47 +00:00
|
|
|
// NZCV flags
|
|
|
|
cpsr |= CPSR_nzcv;
|
2017-12-02 15:54:48 +00:00
|
|
|
// Q flag
|
|
|
|
cpsr |= CPSR_q ? 1 << 27 : 0;
|
2017-12-02 14:03:54 +00:00
|
|
|
// GE flags
|
|
|
|
cpsr |= Common::Bit<31>(CPSR_ge) ? 1 << 19 : 0;
|
|
|
|
cpsr |= Common::Bit<23>(CPSR_ge) ? 1 << 18 : 0;
|
|
|
|
cpsr |= Common::Bit<15>(CPSR_ge) ? 1 << 17 : 0;
|
|
|
|
cpsr |= Common::Bit<7>(CPSR_ge) ? 1 << 16 : 0;
|
2017-12-02 15:24:10 +00:00
|
|
|
// E flag, T flag
|
|
|
|
cpsr |= Common::Bit<1>(CPSR_et) ? 1 << 9 : 0;
|
|
|
|
cpsr |= Common::Bit<0>(CPSR_et) ? 1 << 5 : 0;
|
2017-12-02 14:03:54 +00:00
|
|
|
// Other flags
|
2017-12-09 15:42:47 +00:00
|
|
|
cpsr |= CPSR_jaifm;
|
2017-12-02 14:03:54 +00:00
|
|
|
|
|
|
|
return cpsr;
|
2017-12-02 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
void A32JitState::SetCpsr(u32 cpsr) {
|
2017-12-09 15:42:47 +00:00
|
|
|
// NZCV flags
|
|
|
|
CPSR_nzcv = cpsr & 0xF0000000;
|
2017-12-02 15:54:48 +00:00
|
|
|
// Q flag
|
|
|
|
CPSR_q = Common::Bit<27>(cpsr) ? 1 : 0;
|
2017-12-02 14:03:54 +00:00
|
|
|
// GE flags
|
|
|
|
CPSR_ge = 0;
|
|
|
|
CPSR_ge |= Common::Bit<19>(cpsr) ? 0xFF000000 : 0;
|
|
|
|
CPSR_ge |= Common::Bit<18>(cpsr) ? 0x00FF0000 : 0;
|
|
|
|
CPSR_ge |= Common::Bit<17>(cpsr) ? 0x0000FF00 : 0;
|
|
|
|
CPSR_ge |= Common::Bit<16>(cpsr) ? 0x000000FF : 0;
|
2017-12-02 15:24:10 +00:00
|
|
|
// E flag, T flag
|
|
|
|
CPSR_et = 0;
|
|
|
|
CPSR_et |= Common::Bit<9>(cpsr) ? 2 : 0;
|
|
|
|
CPSR_et |= Common::Bit<5>(cpsr) ? 1 : 0;
|
2017-12-02 14:03:54 +00:00
|
|
|
// Other flags
|
2017-12-09 15:42:47 +00:00
|
|
|
CPSR_jaifm = cpsr & 0x07F0FDDF;
|
2017-12-02 13:55:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
void A32JitState::ResetRSB() {
|
2016-08-31 21:57:33 +01:00
|
|
|
rsb_location_descriptors.fill(0xFFFFFFFFFFFFFFFFull);
|
|
|
|
rsb_codeptrs.fill(0);
|
2016-08-13 00:10:23 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 18:54:19 +01:00
|
|
|
/**
|
|
|
|
* Comparing MXCSR and FPSCR
|
|
|
|
* =========================
|
|
|
|
*
|
|
|
|
* SSE MXCSR exception flags
|
|
|
|
* -------------------------
|
|
|
|
* PE bit 5 Precision Flag
|
|
|
|
* UE bit 4 Underflow Flag
|
|
|
|
* OE bit 3 Overflow Flag
|
|
|
|
* ZE bit 2 Divide By Zero Flag
|
2016-08-06 17:21:29 +01:00
|
|
|
* DE bit 1 Denormal Flag // Appears to only be set when MXCSR.DAZ = 0
|
2016-08-05 18:54:19 +01:00
|
|
|
* IE bit 0 Invalid Operation Flag
|
|
|
|
*
|
|
|
|
* VFP FPSCR cumulative exception bits
|
|
|
|
* -----------------------------------
|
2016-08-06 17:21:29 +01:00
|
|
|
* IDC bit 7 Input Denormal cumulative exception bit // Only ever set when FPSCR.FTZ = 1
|
2016-08-05 18:54:19 +01:00
|
|
|
* IXC bit 4 Inexact cumulative exception bit
|
|
|
|
* UFC bit 3 Underflow cumulative exception bit
|
|
|
|
* OFC bit 2 Overflow cumulative exception bit
|
|
|
|
* DZC bit 1 Division by Zero cumulative exception bit
|
|
|
|
* IOC bit 0 Invalid Operation cumulative exception bit
|
|
|
|
*
|
|
|
|
* SSE MSCSR exception masks
|
|
|
|
* -------------------------
|
|
|
|
* PM bit 12 Precision Mask
|
|
|
|
* UM bit 11 Underflow Mask
|
|
|
|
* OM bit 10 Overflow Mask
|
|
|
|
* ZM bit 9 Divide By Zero Mask
|
|
|
|
* DM bit 8 Denormal Mask
|
|
|
|
* IM bit 7 Invalid Operation Mask
|
|
|
|
*
|
|
|
|
* VFP FPSCR exception trap enables
|
|
|
|
* --------------------------------
|
|
|
|
* IDE bit 15 Input Denormal exception trap enable
|
|
|
|
* IXE bit 12 Inexact exception trap enable
|
|
|
|
* UFE bit 11 Underflow exception trap enable
|
|
|
|
* OFE bit 10 Overflow exception trap enable
|
|
|
|
* DZE bit 9 Division by Zero exception trap enable
|
|
|
|
* IOE bit 8 Invalid Operation exception trap enable
|
|
|
|
*
|
|
|
|
* SSE MXCSR mode bits
|
|
|
|
* -------------------
|
|
|
|
* FZ bit 15 Flush To Zero
|
|
|
|
* DAZ bit 6 Denormals Are Zero
|
|
|
|
* RN bits 13-14 Round to {0 = Nearest, 1 = Negative, 2 = Positive, 3 = Zero}
|
|
|
|
*
|
|
|
|
* VFP FPSCR mode bits
|
|
|
|
* -------------------
|
|
|
|
* DN bit 25 Default NaN
|
|
|
|
* FZ bit 24 Flush to Zero
|
|
|
|
* RMode bits 22-23 Round to {0 = Nearest, 1 = Positive, 2 = Negative, 3 = Zero}
|
|
|
|
* Stride bits 20-21 Vector stride
|
|
|
|
* Len bits 16-18 Vector length
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NZCV; QC (ASMID only), AHP; DN, FZ, RMode, Stride; SBZP; Len; trap enables; cumulative bits
|
2018-01-01 15:23:56 +00:00
|
|
|
constexpr u32 FPSCR_MODE_MASK = A32::LocationDescriptor::FPSCR_MODE_MASK;
|
2016-08-13 00:10:23 +01:00
|
|
|
constexpr u32 FPSCR_NZCV_MASK = 0xF0000000;
|
2016-08-05 18:54:19 +01:00
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
u32 A32JitState::Fpscr() const {
|
2016-09-05 14:39:17 +01:00
|
|
|
ASSERT((FPSCR_mode & ~FPSCR_MODE_MASK) == 0);
|
|
|
|
ASSERT((FPSCR_nzcv & ~FPSCR_NZCV_MASK) == 0);
|
2016-08-06 17:21:29 +01:00
|
|
|
ASSERT((FPSCR_IDC & ~(1 << 7)) == 0);
|
|
|
|
ASSERT((FPSCR_UFC & ~(1 << 3)) == 0);
|
2016-08-05 18:54:19 +01:00
|
|
|
|
2016-09-05 14:39:17 +01:00
|
|
|
u32 FPSCR = FPSCR_mode | FPSCR_nzcv;
|
2016-08-05 18:54:19 +01:00
|
|
|
FPSCR |= (guest_MXCSR & 0b0000000000001); // IOC = IE
|
|
|
|
FPSCR |= (guest_MXCSR & 0b0000000111100) >> 1; // IXC, UFC, OFC, DZC = PE, UE, OE, ZE
|
2016-08-06 17:21:29 +01:00
|
|
|
FPSCR |= FPSCR_IDC;
|
|
|
|
FPSCR |= FPSCR_UFC;
|
2016-08-05 18:54:19 +01:00
|
|
|
|
|
|
|
return FPSCR;
|
|
|
|
}
|
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
void A32JitState::SetFpscr(u32 FPSCR) {
|
2016-08-05 18:54:19 +01:00
|
|
|
old_FPSCR = FPSCR;
|
2016-09-05 14:39:17 +01:00
|
|
|
FPSCR_mode = FPSCR & FPSCR_MODE_MASK;
|
|
|
|
FPSCR_nzcv = FPSCR & FPSCR_NZCV_MASK;
|
2016-08-05 18:54:19 +01:00
|
|
|
guest_MXCSR = 0;
|
|
|
|
|
2016-08-06 17:21:29 +01:00
|
|
|
// Exception masks / enables
|
2016-08-07 00:40:29 +01:00
|
|
|
guest_MXCSR |= 0x00001f80; // mask all
|
2016-08-06 17:21:29 +01:00
|
|
|
//guest_MXCSR |= (~FPSCR >> 1) & 0b0000010000000; // IM = ~IOE
|
|
|
|
//guest_MXCSR |= (~FPSCR >> 7) & 0b0000100000000; // DM = ~IDE
|
|
|
|
//guest_MXCSR |= (~FPSCR ) & 0b1111000000000; // PM, UM, OM, ZM = ~IXE, ~UFE, ~OFE, ~DZE
|
|
|
|
|
|
|
|
// RMode
|
|
|
|
const std::array<u32, 4> MXCSR_RMode {0x0, 0x4000, 0x2000, 0x6000};
|
|
|
|
guest_MXCSR |= MXCSR_RMode[(FPSCR >> 22) & 0x3];
|
|
|
|
|
|
|
|
// Cumulative flags IOC, IXC, UFC, OFC, DZC
|
2016-08-05 18:54:19 +01:00
|
|
|
guest_MXCSR |= ( FPSCR ) & 0b0000000000001; // IE = IOC
|
|
|
|
guest_MXCSR |= ( FPSCR << 1) & 0b0000000111100; // PE, UE, OE, ZE = IXC, UFC, OFC, DZC
|
2016-08-06 17:21:29 +01:00
|
|
|
|
|
|
|
// Cumulative flag IDC, UFC
|
|
|
|
FPSCR_IDC = FPSCR & (1 << 7);
|
|
|
|
FPSCR_UFC = FPSCR & (1 << 3);
|
2016-08-05 18:54:19 +01:00
|
|
|
|
|
|
|
if (Common::Bit<24>(FPSCR)) {
|
|
|
|
// VFP Flush to Zero
|
2016-08-06 17:21:29 +01:00
|
|
|
//guest_MXCSR |= (1 << 15); // SSE Flush to Zero
|
2017-11-22 17:45:37 +00:00
|
|
|
//guest_MXCSR |= (1 << 6); // SSE Denormals are Zero
|
2016-08-05 18:54:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 22:49:17 +00:00
|
|
|
u64 A32JitState::GetUniqueHash() const {
|
2017-12-12 14:18:21 +00:00
|
|
|
return CPSR_et | FPSCR_mode | (static_cast<u64>(Reg[15]) << 32);
|
|
|
|
}
|
|
|
|
|
2018-01-26 13:51:48 +00:00
|
|
|
} // namespace Dynarmic::BackendX64
|