2020-02-28 16:04:28 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-11-23 19:58:00 +00:00
|
|
|
Copyright (C) 2018-24 divingkatae and maximum
|
2020-02-28 16:04:28 +00:00
|
|
|
(theweirdo) spatium
|
|
|
|
|
|
|
|
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 17:38:32 +00:00
|
|
|
|
2022-01-10 15:36:14 +00:00
|
|
|
#include <core/timermanager.h>
|
|
|
|
#include <loguru.hpp>
|
|
|
|
#include "ppcemu.h"
|
|
|
|
#include "ppcmmu.h"
|
|
|
|
|
2020-02-23 15:41:44 +00:00
|
|
|
#include <algorithm>
|
2024-03-27 02:25:05 +00:00
|
|
|
#include <cstring>
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2020-01-11 20:48:56 +00:00
|
|
|
#include <setjmp.h>
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
2019-12-28 00:58:51 +00:00
|
|
|
|
2020-02-23 14:24:49 +00:00
|
|
|
using namespace std;
|
2020-10-18 04:46:38 +00:00
|
|
|
using namespace dppc_interpreter;
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
MemCtrlBase* mem_ctrl_instance = 0;
|
2020-01-31 03:08:22 +00:00
|
|
|
|
2023-09-18 18:43:53 +00:00
|
|
|
bool is_601 = false;
|
|
|
|
|
2023-11-22 00:57:28 +00:00
|
|
|
bool power_on = false;
|
|
|
|
Po_Cause power_off_reason = po_enter_debugger;
|
2019-12-28 01:49:58 +00:00
|
|
|
|
2020-01-28 01:20:18 +00:00
|
|
|
SetPRS ppc_state;
|
|
|
|
|
|
|
|
bool grab_return;
|
|
|
|
bool grab_breakpoint;
|
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint32_t ppc_cur_instruction; // Current instruction for the PPC
|
2020-01-28 01:20:18 +00:00
|
|
|
uint32_t ppc_effective_address;
|
2020-05-12 18:55:45 +00:00
|
|
|
uint32_t ppc_next_instruction_address; // Used for branching, setting up the NIA
|
2020-01-28 01:20:18 +00:00
|
|
|
|
2024-02-22 07:10:14 +00:00
|
|
|
unsigned exec_flags; // execution control flags
|
|
|
|
// FIXME: exec_timer is read by main thread ppc_main_opcode;
|
2024-03-27 12:15:30 +00:00
|
|
|
// written by audio dbdma DMAChannel::update_irq .. add_immediate_timer
|
2024-02-22 07:10:14 +00:00
|
|
|
volatile bool exec_timer;
|
2022-08-24 10:51:43 +00:00
|
|
|
bool int_pin = false; // interrupt request pin state: true - asserted
|
2023-08-07 18:11:02 +00:00
|
|
|
bool dec_exception_pending = false;
|
2020-01-03 20:01:02 +00:00
|
|
|
|
2020-06-03 12:54:24 +00:00
|
|
|
/* copy of local variable bb_start_la. Need for correct
|
|
|
|
calculation of CPU cycles after setjmp that clobbers
|
|
|
|
non-volatile local variables. */
|
|
|
|
uint32_t glob_bb_start_la;
|
2020-01-03 20:01:02 +00:00
|
|
|
|
2020-05-31 22:46:00 +00:00
|
|
|
/* variables related to virtual time */
|
2023-10-18 02:56:52 +00:00
|
|
|
bool g_realtime;
|
|
|
|
uint64_t g_nanoseconds_base;
|
|
|
|
uint64_t g_icycles_base;
|
2021-12-19 23:10:02 +00:00
|
|
|
uint64_t g_icycles;
|
|
|
|
int icnt_factor;
|
|
|
|
|
|
|
|
/* global variables related to the timebase facility */
|
2022-09-16 03:21:54 +00:00
|
|
|
uint64_t tbr_wr_timestamp; // stores vCPU virtual time of the last TBR write
|
|
|
|
uint64_t rtc_timestamp; // stores vCPU virtual time of the last RTC write
|
2021-12-19 23:10:02 +00:00
|
|
|
uint64_t tbr_wr_value; // last value written to the TBR
|
2024-03-27 12:15:30 +00:00
|
|
|
uint32_t tbr_freq_ghz; // TBR/RTC driving frequency in GHz expressed as a
|
|
|
|
// 32 bit fraction less than 1.0 (999.999999 MHz maximum).
|
|
|
|
uint64_t tbr_period_ns; // TBR/RTC period in ns expressed as a 64 bit value
|
|
|
|
// with 32 fractional bits (<1 Hz minimum).
|
2022-03-02 16:02:32 +00:00
|
|
|
uint64_t timebase_counter; // internal timebase counter
|
2023-02-09 00:38:55 +00:00
|
|
|
uint64_t dec_wr_timestamp; // stores vCPU virtual time of the last DEC write
|
|
|
|
uint32_t dec_wr_value; // last value written to the DEC register
|
2022-03-22 11:23:54 +00:00
|
|
|
uint32_t rtc_lo; // MPC601 RTC lower, counts nanoseconds
|
|
|
|
uint32_t rtc_hi; // MPC601 RTC upper, counts seconds
|
2019-12-28 00:58:51 +00:00
|
|
|
|
2021-04-29 00:26:17 +00:00
|
|
|
#ifdef CPU_PROFILING
|
|
|
|
|
|
|
|
/* global variables for lightweight CPU profiling */
|
|
|
|
uint64_t num_executed_instrs;
|
|
|
|
uint64_t num_supervisor_instrs;
|
|
|
|
uint64_t num_int_loads;
|
|
|
|
uint64_t num_int_stores;
|
|
|
|
uint64_t exceptions_processed;
|
|
|
|
|
|
|
|
#include "utils/profiler.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class CPUProfile : public BaseProfile {
|
|
|
|
public:
|
|
|
|
CPUProfile() : BaseProfile("PPC_CPU") {};
|
|
|
|
|
|
|
|
void populate_variables(std::vector<ProfileVar>& vars) {
|
|
|
|
vars.clear();
|
|
|
|
|
|
|
|
vars.push_back({.name = "Executed Instructions Total",
|
|
|
|
.format = ProfileVarFmt::DEC,
|
|
|
|
.value = num_executed_instrs});
|
|
|
|
|
|
|
|
vars.push_back({.name = "Executed Supervisor Instructions",
|
|
|
|
.format = ProfileVarFmt::DEC,
|
|
|
|
.value = num_supervisor_instrs});
|
|
|
|
|
|
|
|
vars.push_back({.name = "Integer Load Instructions",
|
|
|
|
.format = ProfileVarFmt::DEC,
|
|
|
|
.value = num_int_loads});
|
|
|
|
|
|
|
|
vars.push_back({.name = "Integer Store Instructions",
|
|
|
|
.format = ProfileVarFmt::DEC,
|
|
|
|
.value = num_int_stores});
|
|
|
|
|
|
|
|
vars.push_back({.name = "Exceptions processed",
|
|
|
|
.format = ProfileVarFmt::DEC,
|
|
|
|
.value = exceptions_processed});
|
|
|
|
};
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
num_executed_instrs = 0;
|
|
|
|
num_supervisor_instrs = 0;
|
|
|
|
num_int_loads = 0;
|
|
|
|
num_int_stores = 0;
|
|
|
|
exceptions_processed = 0;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-12-28 00:58:51 +00:00
|
|
|
/** Opcode lookup tables. */
|
|
|
|
|
|
|
|
/** Primary opcode (bits 0...5) lookup table. */
|
2024-03-28 05:39:57 +00:00
|
|
|
static PPCOpcode OpcodeGrabber[64];
|
2019-12-28 00:58:51 +00:00
|
|
|
|
|
|
|
/** Lookup tables for branch instructions. */
|
2024-03-24 21:06:07 +00:00
|
|
|
const static PPCOpcode SubOpcode16Grabber[] = {
|
|
|
|
dppc_interpreter::ppc_bc<LK0, AA0>, // bc
|
|
|
|
dppc_interpreter::ppc_bc<LK1, AA0>, // bcl
|
|
|
|
dppc_interpreter::ppc_bc<LK0, AA1>, // bca
|
|
|
|
dppc_interpreter::ppc_bc<LK1, AA1>}; // bcla
|
|
|
|
|
|
|
|
const static PPCOpcode SubOpcode18Grabber[] = {
|
|
|
|
dppc_interpreter::ppc_b<LK0, AA0>, // b
|
|
|
|
dppc_interpreter::ppc_b<LK1, AA0>, // bl
|
|
|
|
dppc_interpreter::ppc_b<LK0, AA1>, // ba
|
|
|
|
dppc_interpreter::ppc_b<LK1, AA1>}; // bla
|
2019-12-28 00:58:51 +00:00
|
|
|
|
2020-11-14 01:59:49 +00:00
|
|
|
/** Instructions decoding tables for integer,
|
2020-10-17 21:30:37 +00:00
|
|
|
single floating-point, and double-floating point ops respectively */
|
|
|
|
|
2024-03-28 05:39:57 +00:00
|
|
|
static PPCOpcode SubOpcode31Grabber[2048];
|
|
|
|
static PPCOpcode SubOpcode59Grabber[64];
|
|
|
|
static PPCOpcode SubOpcode63Grabber[2048];
|
2019-12-28 00:58:51 +00:00
|
|
|
|
2020-11-29 22:53:03 +00:00
|
|
|
/** Exception helpers. */
|
2020-10-17 21:30:37 +00:00
|
|
|
|
2022-01-20 00:32:45 +00:00
|
|
|
void ppc_illegalop() {
|
2020-11-30 19:59:36 +00:00
|
|
|
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
|
2020-10-17 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 00:32:45 +00:00
|
|
|
void ppc_fpu_off() {
|
2020-11-30 19:59:36 +00:00
|
|
|
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::FPU_OFF);
|
2020-10-17 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 10:51:43 +00:00
|
|
|
void ppc_assert_int() {
|
|
|
|
int_pin = true;
|
2023-11-21 15:06:50 +00:00
|
|
|
if (ppc_state.msr & MSR::EE) {
|
2022-08-24 10:51:43 +00:00
|
|
|
LOG_F(5, "CPU ExtIntHandler called");
|
2022-01-10 16:27:27 +00:00
|
|
|
ppc_exception_handler(Except_Type::EXC_EXT_INT, 0);
|
2022-08-24 10:51:43 +00:00
|
|
|
} else {
|
|
|
|
LOG_F(5, "CPU IRQ ignored!");
|
2022-01-10 16:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 10:51:43 +00:00
|
|
|
void ppc_release_int() {
|
|
|
|
int_pin = false;
|
|
|
|
}
|
|
|
|
|
2020-11-29 22:53:03 +00:00
|
|
|
/** Opcode decoding functions. */
|
2019-12-28 00:58:51 +00:00
|
|
|
|
2020-01-26 02:30:55 +00:00
|
|
|
void ppc_opcode16() {
|
2019-12-28 00:58:51 +00:00
|
|
|
SubOpcode16Grabber[ppc_cur_instruction & 3]();
|
|
|
|
}
|
|
|
|
|
2020-01-26 02:30:55 +00:00
|
|
|
void ppc_opcode18() {
|
2019-12-28 00:58:51 +00:00
|
|
|
SubOpcode18Grabber[ppc_cur_instruction & 3]();
|
|
|
|
}
|
|
|
|
|
2024-03-26 08:14:23 +00:00
|
|
|
template<field_601 for601>
|
2020-01-26 02:30:55 +00:00
|
|
|
void ppc_opcode19() {
|
2020-10-17 21:30:37 +00:00
|
|
|
uint16_t subop_grab = ppc_cur_instruction & 0x7FF;
|
|
|
|
|
2024-03-05 08:02:53 +00:00
|
|
|
switch (subop_grab) {
|
|
|
|
case 0:
|
|
|
|
ppc_mcrf();
|
|
|
|
break;
|
|
|
|
case 32:
|
2024-03-26 08:14:23 +00:00
|
|
|
ppc_bclr<LK0>();
|
2024-03-05 08:02:53 +00:00
|
|
|
break;
|
|
|
|
case 33:
|
2024-03-26 08:14:23 +00:00
|
|
|
ppc_bclr<LK1>();
|
2024-03-05 08:02:53 +00:00
|
|
|
break;
|
|
|
|
case 66:
|
|
|
|
ppc_crnor();
|
|
|
|
break;
|
|
|
|
case 100:
|
|
|
|
ppc_rfi();
|
|
|
|
break;
|
|
|
|
case 258:
|
|
|
|
ppc_crandc();
|
|
|
|
break;
|
|
|
|
case 300:
|
|
|
|
ppc_isync();
|
|
|
|
break;
|
|
|
|
case 386:
|
|
|
|
ppc_crxor();
|
|
|
|
break;
|
|
|
|
case 450:
|
|
|
|
ppc_crnand();
|
|
|
|
break;
|
|
|
|
case 514:
|
|
|
|
ppc_crand();
|
|
|
|
break;
|
|
|
|
case 578:
|
|
|
|
ppc_creqv();
|
|
|
|
break;
|
|
|
|
case 834:
|
|
|
|
ppc_crorc();
|
|
|
|
break;
|
|
|
|
case 898:
|
|
|
|
ppc_cror();
|
|
|
|
break;
|
|
|
|
case 1056:
|
2024-03-26 08:14:23 +00:00
|
|
|
ppc_bcctr<LK0, for601>();
|
2024-03-05 08:02:53 +00:00
|
|
|
break;
|
|
|
|
case 1057:
|
2024-03-26 08:14:23 +00:00
|
|
|
ppc_bcctr<LK1, for601>();
|
2024-03-05 08:02:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ppc_illegalop();
|
2019-12-28 00:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 00:24:36 +00:00
|
|
|
template void ppc_opcode19<NOT601>();
|
|
|
|
template void ppc_opcode19<IS601>();
|
2024-03-07 10:36:43 +00:00
|
|
|
|
2020-01-26 02:30:55 +00:00
|
|
|
void ppc_opcode31() {
|
2024-03-24 19:21:19 +00:00
|
|
|
uint16_t subop_grab = ppc_cur_instruction & 0x7FFUL;
|
2020-10-17 21:30:37 +00:00
|
|
|
SubOpcode31Grabber[subop_grab]();
|
2019-12-28 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 02:30:55 +00:00
|
|
|
void ppc_opcode59() {
|
2024-03-24 19:21:19 +00:00
|
|
|
uint16_t subop_grab = ppc_cur_instruction & 0x3FUL;
|
2020-10-17 21:30:37 +00:00
|
|
|
SubOpcode59Grabber[subop_grab]();
|
2019-12-28 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 02:30:55 +00:00
|
|
|
void ppc_opcode63() {
|
2024-03-24 19:21:19 +00:00
|
|
|
uint16_t subop_grab = ppc_cur_instruction & 0x7FFUL;
|
2020-10-17 21:30:37 +00:00
|
|
|
SubOpcode63Grabber[subop_grab]();
|
2019-12-28 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 13:33:32 +00:00
|
|
|
/* Dispatch using main opcode */
|
|
|
|
void ppc_main_opcode()
|
|
|
|
{
|
2021-04-29 00:26:17 +00:00
|
|
|
#ifdef CPU_PROFILING
|
|
|
|
num_executed_instrs++;
|
|
|
|
#endif
|
2020-06-03 13:33:32 +00:00
|
|
|
OpcodeGrabber[(ppc_cur_instruction >> 26) & 0x3F]();
|
2019-12-28 00:58:51 +00:00
|
|
|
}
|
2019-12-28 01:49:58 +00:00
|
|
|
|
2024-04-07 18:38:46 +00:00
|
|
|
static long long now_ns() {
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
2023-10-18 02:56:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 23:10:02 +00:00
|
|
|
uint64_t get_virt_time_ns()
|
|
|
|
{
|
2023-10-18 02:56:52 +00:00
|
|
|
if (g_realtime) {
|
|
|
|
return now_ns() - g_nanoseconds_base;
|
|
|
|
} else {
|
|
|
|
return g_icycles << icnt_factor;
|
|
|
|
}
|
2021-12-19 23:10:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:36:14 +00:00
|
|
|
uint64_t process_events()
|
|
|
|
{
|
2024-02-22 07:10:14 +00:00
|
|
|
exec_timer = false;
|
2023-10-18 02:48:51 +00:00
|
|
|
uint64_t slice_ns = TimerManager::get_instance()->process_timers();
|
2022-01-10 15:36:14 +00:00
|
|
|
if (slice_ns == 0) {
|
|
|
|
// execute 10.000 cycles
|
|
|
|
// if there are no pending timers
|
|
|
|
return g_icycles + 10000;
|
|
|
|
}
|
2023-11-20 00:56:30 +00:00
|
|
|
return g_icycles + ((slice_ns + (1ULL << icnt_factor)) >> icnt_factor);
|
2022-01-10 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void force_cycle_counter_reload()
|
2021-12-19 23:10:02 +00:00
|
|
|
{
|
2022-01-10 15:36:14 +00:00
|
|
|
// tell the interpreter loop to reload cycle counter
|
2024-02-22 07:10:14 +00:00
|
|
|
exec_timer = true;
|
2021-12-19 23:10:02 +00:00
|
|
|
}
|
2019-12-28 01:49:58 +00:00
|
|
|
|
|
|
|
/** Execute PPC code as long as power is on. */
|
2021-12-19 23:10:02 +00:00
|
|
|
// inner interpreter loop
|
|
|
|
static void ppc_exec_inner()
|
|
|
|
{
|
|
|
|
uint64_t max_cycles;
|
|
|
|
uint32_t page_start, eb_start, eb_end;
|
|
|
|
uint8_t* pc_real;
|
|
|
|
|
|
|
|
max_cycles = 0;
|
|
|
|
|
|
|
|
while (power_on) {
|
|
|
|
// define boundaries of the next execution block
|
|
|
|
// max execution block length = one memory page
|
2022-03-02 15:55:20 +00:00
|
|
|
eb_start = ppc_state.pc;
|
2024-03-26 02:45:06 +00:00
|
|
|
page_start = eb_start & PPC_PAGE_MASK;
|
|
|
|
eb_end = page_start + PPC_PAGE_SIZE - 1;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
|
|
|
|
pc_real = mmu_translate_imem(eb_start);
|
|
|
|
|
|
|
|
// interpret execution block
|
2023-11-22 00:57:28 +00:00
|
|
|
while (power_on && ppc_state.pc < eb_end) {
|
2021-12-19 23:10:02 +00:00
|
|
|
ppc_main_opcode();
|
2024-02-22 07:10:14 +00:00
|
|
|
if (g_icycles++ >= max_cycles || exec_timer) {
|
2021-12-19 23:10:02 +00:00
|
|
|
max_cycles = process_events();
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:55:20 +00:00
|
|
|
if (exec_flags) {
|
2021-12-19 23:10:02 +00:00
|
|
|
// define next execution block
|
|
|
|
eb_start = ppc_next_instruction_address;
|
2024-03-26 02:45:06 +00:00
|
|
|
if (!(exec_flags & EXEF_RFI) && (eb_start & PPC_PAGE_MASK) == page_start) {
|
2021-12-19 23:10:02 +00:00
|
|
|
pc_real += (int)eb_start - (int)ppc_state.pc;
|
|
|
|
ppc_set_cur_instruction(pc_real);
|
|
|
|
} else {
|
2024-03-26 02:45:06 +00:00
|
|
|
page_start = eb_start & PPC_PAGE_MASK;
|
|
|
|
eb_end = page_start + PPC_PAGE_SIZE - 1;
|
2021-12-19 23:10:02 +00:00
|
|
|
pc_real = mmu_translate_imem(eb_start);
|
|
|
|
}
|
|
|
|
ppc_state.pc = eb_start;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
} else {
|
|
|
|
ppc_state.pc += 4;
|
|
|
|
pc_real += 4;
|
|
|
|
ppc_set_cur_instruction(pc_real);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outer interpreter loop
|
|
|
|
void ppc_exec()
|
|
|
|
{
|
|
|
|
if (setjmp(exc_env)) {
|
|
|
|
// process low-level exceptions
|
|
|
|
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
|
|
|
|
ppc_state.pc = ppc_next_instruction_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (power_on) {
|
|
|
|
ppc_exec_inner();
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 01:49:58 +00:00
|
|
|
|
|
|
|
/** Execute one PPC instruction. */
|
2021-12-19 23:10:02 +00:00
|
|
|
void ppc_exec_single()
|
|
|
|
{
|
|
|
|
if (setjmp(exc_env)) {
|
|
|
|
// process low-level exceptions
|
|
|
|
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
|
|
|
|
ppc_state.pc = ppc_next_instruction_address;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mmu_translate_imem(ppc_state.pc);
|
|
|
|
ppc_main_opcode();
|
2022-08-24 10:51:43 +00:00
|
|
|
g_icycles++;
|
|
|
|
process_events();
|
|
|
|
|
2022-03-02 15:55:20 +00:00
|
|
|
if (exec_flags) {
|
2024-02-22 07:10:14 +00:00
|
|
|
ppc_state.pc = ppc_next_instruction_address;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
} else {
|
|
|
|
ppc_state.pc += 4;
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 01:49:58 +00:00
|
|
|
|
|
|
|
/** Execute PPC code until goal_addr is reached. */
|
2020-01-03 20:01:02 +00:00
|
|
|
|
2021-12-19 23:10:02 +00:00
|
|
|
// inner interpreter loop
|
|
|
|
static void ppc_exec_until_inner(const uint32_t goal_addr)
|
|
|
|
{
|
|
|
|
uint64_t max_cycles;
|
|
|
|
uint32_t page_start, eb_start, eb_end;
|
|
|
|
uint8_t* pc_real;
|
|
|
|
|
|
|
|
max_cycles = 0;
|
|
|
|
|
2023-08-11 08:19:32 +00:00
|
|
|
do {
|
2021-12-19 23:10:02 +00:00
|
|
|
// define boundaries of the next execution block
|
|
|
|
// max execution block length = one memory page
|
2022-03-02 15:55:20 +00:00
|
|
|
eb_start = ppc_state.pc;
|
2024-03-26 02:45:06 +00:00
|
|
|
page_start = eb_start & PPC_PAGE_MASK;
|
|
|
|
eb_end = page_start + PPC_PAGE_SIZE - 1;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
|
|
|
|
pc_real = mmu_translate_imem(eb_start);
|
|
|
|
|
|
|
|
// interpret execution block
|
2023-11-22 00:57:28 +00:00
|
|
|
while (power_on && ppc_state.pc < eb_end) {
|
2021-12-19 23:10:02 +00:00
|
|
|
ppc_main_opcode();
|
2024-02-22 07:10:14 +00:00
|
|
|
if (g_icycles++ >= max_cycles || exec_timer) {
|
2021-12-19 23:10:02 +00:00
|
|
|
max_cycles = process_events();
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:55:20 +00:00
|
|
|
if (exec_flags) {
|
2021-12-19 23:10:02 +00:00
|
|
|
// define next execution block
|
|
|
|
eb_start = ppc_next_instruction_address;
|
2024-03-26 02:45:06 +00:00
|
|
|
if (!(exec_flags & EXEF_RFI) && (eb_start & PPC_PAGE_MASK) == page_start) {
|
2021-12-19 23:10:02 +00:00
|
|
|
pc_real += (int)eb_start - (int)ppc_state.pc;
|
|
|
|
ppc_set_cur_instruction(pc_real);
|
|
|
|
} else {
|
2024-03-26 02:45:06 +00:00
|
|
|
page_start = eb_start & PPC_PAGE_MASK;
|
|
|
|
eb_end = page_start + PPC_PAGE_SIZE - 1;
|
2021-12-19 23:10:02 +00:00
|
|
|
pc_real = mmu_translate_imem(eb_start);
|
|
|
|
}
|
|
|
|
ppc_state.pc = eb_start;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
} else {
|
|
|
|
ppc_state.pc += 4;
|
|
|
|
pc_real += 4;
|
|
|
|
ppc_set_cur_instruction(pc_real);
|
|
|
|
}
|
2023-08-11 08:19:32 +00:00
|
|
|
|
|
|
|
if (ppc_state.pc == goal_addr)
|
|
|
|
break;
|
2021-12-19 23:10:02 +00:00
|
|
|
}
|
2023-11-22 00:57:28 +00:00
|
|
|
} while (power_on && ppc_state.pc != goal_addr);
|
2021-12-19 23:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// outer interpreter loop
|
|
|
|
void ppc_exec_until(volatile uint32_t goal_addr)
|
|
|
|
{
|
|
|
|
if (setjmp(exc_env)) {
|
|
|
|
// process low-level exceptions
|
|
|
|
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
|
|
|
|
ppc_state.pc = ppc_next_instruction_address;
|
|
|
|
}
|
|
|
|
|
2023-08-11 08:19:32 +00:00
|
|
|
do {
|
2021-12-19 23:10:02 +00:00
|
|
|
ppc_exec_until_inner(goal_addr);
|
2023-11-22 00:57:28 +00:00
|
|
|
} while (power_on && ppc_state.pc != goal_addr);
|
2021-12-19 23:10:02 +00:00
|
|
|
}
|
2020-05-31 22:46:00 +00:00
|
|
|
|
2020-07-16 12:26:34 +00:00
|
|
|
/** Execute PPC code until control is reached the specified region. */
|
|
|
|
|
2021-12-19 23:10:02 +00:00
|
|
|
// inner interpreter loop
|
|
|
|
static void ppc_exec_dbg_inner(const uint32_t start_addr, const uint32_t size)
|
|
|
|
{
|
|
|
|
uint64_t max_cycles;
|
|
|
|
uint32_t page_start, eb_start, eb_end;
|
|
|
|
uint8_t* pc_real;
|
|
|
|
|
|
|
|
max_cycles = 0;
|
|
|
|
|
2023-11-22 00:57:28 +00:00
|
|
|
while (power_on && (ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size)) {
|
2021-12-19 23:10:02 +00:00
|
|
|
// define boundaries of the next execution block
|
|
|
|
// max execution block length = one memory page
|
2022-03-02 15:55:20 +00:00
|
|
|
eb_start = ppc_state.pc;
|
2024-03-26 02:45:06 +00:00
|
|
|
page_start = eb_start & PPC_PAGE_MASK;
|
|
|
|
eb_end = page_start + PPC_PAGE_SIZE - 1;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
|
|
|
|
pc_real = mmu_translate_imem(eb_start);
|
|
|
|
|
|
|
|
// interpret execution block
|
2023-11-22 00:57:28 +00:00
|
|
|
while (power_on && (ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size)
|
2021-12-19 23:10:02 +00:00
|
|
|
&& (ppc_state.pc < eb_end)) {
|
|
|
|
ppc_main_opcode();
|
2024-02-22 07:10:14 +00:00
|
|
|
if (g_icycles++ >= max_cycles || exec_timer) {
|
2021-12-19 23:10:02 +00:00
|
|
|
max_cycles = process_events();
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:55:20 +00:00
|
|
|
if (exec_flags) {
|
2021-12-19 23:10:02 +00:00
|
|
|
// define next execution block
|
|
|
|
eb_start = ppc_next_instruction_address;
|
2024-03-26 02:45:06 +00:00
|
|
|
if (!(exec_flags & EXEF_RFI) && (eb_start & PPC_PAGE_MASK) == page_start) {
|
2021-12-19 23:10:02 +00:00
|
|
|
pc_real += (int)eb_start - (int)ppc_state.pc;
|
|
|
|
ppc_set_cur_instruction(pc_real);
|
|
|
|
} else {
|
2024-03-26 02:45:06 +00:00
|
|
|
page_start = eb_start & PPC_PAGE_MASK;
|
|
|
|
eb_end = page_start + PPC_PAGE_SIZE - 1;
|
2021-12-19 23:10:02 +00:00
|
|
|
pc_real = mmu_translate_imem(eb_start);
|
|
|
|
}
|
|
|
|
ppc_state.pc = eb_start;
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
} else {
|
|
|
|
ppc_state.pc += 4;
|
|
|
|
pc_real += 4;
|
|
|
|
ppc_set_cur_instruction(pc_real);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outer interpreter loop
|
|
|
|
void ppc_exec_dbg(volatile uint32_t start_addr, volatile uint32_t size)
|
|
|
|
{
|
|
|
|
if (setjmp(exc_env)) {
|
|
|
|
// process low-level exceptions
|
|
|
|
//LOG_F(9, "PPC-EXEC: low_level exception raised!");
|
|
|
|
ppc_state.pc = ppc_next_instruction_address;
|
|
|
|
}
|
|
|
|
|
2023-11-22 00:57:28 +00:00
|
|
|
while (power_on && (ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size)) {
|
2021-12-19 23:10:02 +00:00
|
|
|
ppc_exec_dbg_inner(start_addr, size);
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 01:49:58 +00:00
|
|
|
|
2024-03-27 01:53:34 +00:00
|
|
|
/*
|
|
|
|
Opcode table macros:
|
|
|
|
- d is for dot (RC).
|
|
|
|
- o is for overflow (OV).
|
|
|
|
- c is for carry CARRY0/CARRY1. It also works for other options:
|
|
|
|
SHFT0/SHFT1, RIGHT0/LEFT1, uint8_t/uint16_t/uint32_t, and int8_t/int16_t.
|
|
|
|
*/
|
|
|
|
|
2024-03-28 05:39:57 +00:00
|
|
|
#define OP(opcode, fn) \
|
|
|
|
do { \
|
|
|
|
OpcodeGrabber[opcode] = fn; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define OPX(opcode, subopcode, fn) \
|
|
|
|
do { \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)] = fn; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define OPXd(opcode, subopcode, fn) \
|
|
|
|
do { \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)] = fn<RC0>; \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)+1] = fn<RC1>; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define OPXod(opcode, subopcode, fn) \
|
|
|
|
do { \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)] = fn<RC0, OV0>; \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)+1] = fn<RC1, OV0>; \
|
|
|
|
opcode ## Grabber[1024+((subopcode)<<1)] = fn<RC0, OV1>; \
|
|
|
|
opcode ## Grabber[1024+((subopcode)<<1)+1] = fn<RC1, OV1>; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define OPXdc(opcode, subopcode, fn, carry) \
|
|
|
|
do { \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)] = fn<carry, RC0>; \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)+1] = fn<carry, RC1>; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define OPXcod(opcode, subopcode, fn, carry) \
|
|
|
|
do { \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)] = fn<carry, RC0, OV0>; \
|
|
|
|
opcode ## Grabber[((subopcode)<<1)+1] = fn<carry, RC1, OV0>; \
|
|
|
|
opcode ## Grabber[1024+((subopcode)<<1)] = fn<carry, RC0, OV1>; \
|
|
|
|
opcode ## Grabber[1024+((subopcode)<<1)+1] = fn<carry, RC1, OV1>; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define OP31(subopcode, fn) OPX(SubOpcode31, subopcode, fn)
|
|
|
|
#define OP31d(subopcode, fn) OPXd(SubOpcode31, subopcode, fn)
|
|
|
|
#define OP31od(subopcode, fn) OPXod(SubOpcode31, subopcode, fn)
|
|
|
|
#define OP31dc(subopcode, fn, carry) OPXdc(SubOpcode31, subopcode, fn, carry)
|
|
|
|
#define OP31cod(subopcode, fn, carry) OPXcod(SubOpcode31, subopcode, fn, carry)
|
|
|
|
|
|
|
|
#define OP59d(subopcode, fn) OPXd(SubOpcode59, subopcode, fn)
|
|
|
|
|
|
|
|
#define OP63(subopcode, fn) OPX(SubOpcode63, subopcode, fn)
|
|
|
|
#define OP63d(subopcode, fn) OPXd(SubOpcode63, subopcode, fn)
|
|
|
|
#define OP63dc(subopcode, fn, carry) OPXdc(SubOpcode63, subopcode, fn, carry)
|
2024-03-26 11:57:52 +00:00
|
|
|
|
2024-04-09 04:18:45 +00:00
|
|
|
void initialize_ppc_opcode_tables(bool include_601) {
|
2024-03-28 05:39:57 +00:00
|
|
|
std::fill_n(OpcodeGrabber, 64, ppc_illegalop);
|
|
|
|
OP(3, ppc_twi);
|
|
|
|
//OP(4, ppc_opcode4); - Altivec instructions not emulated yet. Uncomment once they're implemented.
|
|
|
|
OP(7, ppc_mulli);
|
|
|
|
OP(8, ppc_subfic);
|
2024-04-09 04:18:45 +00:00
|
|
|
if (is_601 || include_601) OP(9, power_dozi);
|
2024-03-28 05:39:57 +00:00
|
|
|
OP(10, ppc_cmpli);
|
|
|
|
OP(11, ppc_cmpi);
|
|
|
|
OP(12, ppc_addic<RC0>);
|
|
|
|
OP(13, ppc_addic<RC1>);
|
|
|
|
OP(14, ppc_addi<SHFT0>);
|
|
|
|
OP(15, ppc_addi<SHFT1>);
|
|
|
|
OP(16, ppc_opcode16);
|
|
|
|
OP(17, ppc_sc);
|
|
|
|
OP(18, ppc_opcode18);
|
|
|
|
if (is_601) OP(19, ppc_opcode19<IS601>); else OP(19, ppc_opcode19<NOT601>);
|
|
|
|
OP(20, ppc_rlwimi);
|
|
|
|
OP(21, ppc_rlwinm);
|
2024-04-09 04:18:45 +00:00
|
|
|
if (is_601 || include_601) OP(22, power_rlmi);
|
2024-03-28 05:39:57 +00:00
|
|
|
OP(23, ppc_rlwnm);
|
|
|
|
OP(24, ppc_ori<SHFT0>);
|
|
|
|
OP(25, ppc_ori<SHFT1>);
|
|
|
|
OP(26, ppc_xori<SHFT0>);
|
|
|
|
OP(27, ppc_xori<SHFT1>);
|
|
|
|
OP(28, ppc_andirc<SHFT0>);
|
|
|
|
OP(29, ppc_andirc<SHFT1>);
|
|
|
|
OP(31, ppc_opcode31);
|
|
|
|
OP(32, ppc_lz<uint32_t>);
|
|
|
|
OP(33, ppc_lzu<uint32_t>);
|
|
|
|
OP(34, ppc_lz<uint8_t>);
|
|
|
|
OP(35, ppc_lzu<uint8_t>);
|
|
|
|
OP(36, ppc_st<uint32_t>);
|
|
|
|
OP(37, ppc_stu<uint32_t>);
|
|
|
|
OP(38, ppc_st<uint8_t>);
|
|
|
|
OP(39, ppc_stu<uint8_t>);
|
|
|
|
OP(40, ppc_lz<uint16_t>);
|
|
|
|
OP(41, ppc_lzu<uint16_t>);
|
|
|
|
OP(42, ppc_lha);
|
|
|
|
OP(43, ppc_lhau);
|
|
|
|
OP(44, ppc_st<uint16_t>);
|
|
|
|
OP(45, ppc_stu<uint16_t>);
|
|
|
|
OP(46, ppc_lmw);
|
|
|
|
OP(47, ppc_stmw);
|
|
|
|
OP(48, ppc_lfs);
|
|
|
|
OP(49, ppc_lfsu);
|
|
|
|
OP(50, ppc_lfd);
|
|
|
|
OP(51, ppc_lfdu);
|
|
|
|
OP(52, ppc_stfs);
|
|
|
|
OP(53, ppc_stfsu);
|
|
|
|
OP(54, ppc_stfd);
|
|
|
|
OP(55, ppc_stfdu);
|
|
|
|
OP(59, ppc_opcode59);
|
|
|
|
OP(63, ppc_opcode63);
|
|
|
|
|
2024-03-24 19:21:19 +00:00
|
|
|
std::fill_n(SubOpcode31Grabber, 2048, ppc_illegalop);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31(0, ppc_cmp);
|
|
|
|
OP31(4, ppc_tw);
|
|
|
|
OP31(32, ppc_cmpl);
|
|
|
|
|
|
|
|
OP31cod(8, ppc_subf, CARRY1);
|
|
|
|
OP31cod(40, ppc_subf, CARRY0);
|
|
|
|
OP31od(104, ppc_neg);
|
|
|
|
OP31od(136, ppc_subfe);
|
|
|
|
OP31od(200, ppc_subfze);
|
|
|
|
OP31od(232, ppc_subfme);
|
|
|
|
|
|
|
|
OP31cod(10, ppc_add, CARRY1);
|
|
|
|
OP31od(138, ppc_adde);
|
|
|
|
OP31od(202, ppc_addze);
|
|
|
|
OP31od(234, ppc_addme);
|
|
|
|
OP31cod(266, ppc_add, CARRY0);
|
|
|
|
|
|
|
|
OP31d(11, ppc_mulhwu);
|
|
|
|
OP31d(75, ppc_mulhw);
|
|
|
|
OP31od(235, ppc_mullw);
|
|
|
|
OP31od(459, ppc_divwu);
|
|
|
|
OP31od(491, ppc_divw);
|
|
|
|
|
|
|
|
OP31(20, ppc_lwarx);
|
|
|
|
OP31(23, ppc_lzx<uint32_t>);
|
|
|
|
OP31(55, ppc_lzux<uint32_t>);
|
|
|
|
OP31(87, ppc_lzx<uint8_t>);
|
|
|
|
OP31(119, ppc_lzux<uint8_t>);
|
|
|
|
OP31(279, ppc_lzx<uint16_t>);
|
|
|
|
OP31(311, ppc_lzux<uint16_t>);
|
|
|
|
OP31(343, ppc_lhax);
|
|
|
|
OP31(375, ppc_lhaux);
|
|
|
|
OP31(533, ppc_lswx);
|
|
|
|
OP31(534, ppc_lwbrx);
|
|
|
|
OP31(535, ppc_lfsx);
|
|
|
|
OP31(567, ppc_lfsux);
|
|
|
|
OP31(597, ppc_lswi);
|
|
|
|
OP31(599, ppc_lfdx);
|
|
|
|
OP31(631, ppc_lfdux);
|
|
|
|
OP31(790, ppc_lhbrx);
|
|
|
|
|
|
|
|
SubOpcode31Grabber[(150<<1)+1] = ppc_stwcx; // No Rc=0 variant.
|
|
|
|
OP31(151, ppc_stx<uint32_t>);
|
|
|
|
OP31(183, ppc_stux<uint32_t>);
|
|
|
|
OP31(215, ppc_stx<uint8_t>);
|
|
|
|
OP31(247, ppc_stux<uint8_t>);
|
|
|
|
OP31(407, ppc_stx<uint16_t>);
|
|
|
|
OP31(439, ppc_stux<uint16_t>);
|
|
|
|
OP31(661, ppc_stswx);
|
|
|
|
OP31(662, ppc_stwbrx);
|
|
|
|
OP31(663, ppc_stfsx);
|
|
|
|
OP31(695, ppc_stfsux);
|
|
|
|
OP31(725, ppc_stswi);
|
|
|
|
OP31(727, ppc_stfdx);
|
|
|
|
OP31(759, ppc_stfdux);
|
|
|
|
OP31(918, ppc_sthbrx);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (!is_601) OP31(983, ppc_stfiwx);
|
2024-03-26 11:57:52 +00:00
|
|
|
|
|
|
|
OP31(310, ppc_eciwx);
|
|
|
|
OP31(438, ppc_ecowx);
|
|
|
|
|
2024-03-27 01:53:11 +00:00
|
|
|
OP31dc(24, ppc_shift, LEFT1); // slw
|
2024-03-27 01:51:42 +00:00
|
|
|
OP31dc(28, ppc_logical, ppc_and);
|
|
|
|
OP31dc(60, ppc_logical, ppc_andc);
|
|
|
|
OP31dc(124, ppc_logical, ppc_nor);
|
|
|
|
OP31dc(284, ppc_logical, ppc_eqv);
|
|
|
|
OP31dc(316, ppc_logical, ppc_xor);
|
|
|
|
OP31dc(412, ppc_logical, ppc_orc);
|
|
|
|
OP31dc(444, ppc_logical, ppc_or);
|
|
|
|
OP31dc(476, ppc_logical, ppc_nand);
|
2024-03-27 01:53:11 +00:00
|
|
|
OP31dc(536, ppc_shift, RIGHT0); // srw
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31d(792, ppc_sraw);
|
|
|
|
OP31d(824, ppc_srawi);
|
|
|
|
OP31dc(922, ppc_exts, int16_t);
|
|
|
|
OP31dc(954, ppc_exts, int8_t);
|
|
|
|
|
|
|
|
OP31d(26, ppc_cntlzw);
|
|
|
|
|
|
|
|
OP31(19, ppc_mfcr);
|
|
|
|
OP31(83, ppc_mfmsr);
|
|
|
|
OP31(144, ppc_mtcrf);
|
|
|
|
OP31(146, ppc_mtmsr);
|
|
|
|
OP31(210, ppc_mtsr);
|
|
|
|
OP31(242, ppc_mtsrin);
|
|
|
|
OP31(339, ppc_mfspr);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (!is_601) OP31(371, ppc_mftb);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31(467, ppc_mtspr);
|
|
|
|
OP31(512, ppc_mcrxr);
|
|
|
|
OP31(595, ppc_mfsr);
|
|
|
|
OP31(659, ppc_mfsrin);
|
|
|
|
|
|
|
|
OP31(54, ppc_dcbst);
|
|
|
|
OP31(86, ppc_dcbf);
|
|
|
|
OP31(246, ppc_dcbtst);
|
|
|
|
OP31(278, ppc_dcbt);
|
|
|
|
OP31(598, ppc_sync);
|
|
|
|
OP31(470, ppc_dcbi);
|
|
|
|
OP31(1014, ppc_dcbz);
|
|
|
|
|
2024-04-09 04:18:45 +00:00
|
|
|
if (is_601 || include_601) {
|
2024-03-27 12:15:30 +00:00
|
|
|
OP31d(29, power_maskg);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31od(107, power_mul);
|
2024-03-27 12:15:30 +00:00
|
|
|
OP31d(152, power_slq);
|
|
|
|
OP31d(153, power_sle);
|
|
|
|
OP31d(184, power_sliq);
|
|
|
|
OP31d(216, power_sllq);
|
|
|
|
OP31d(217, power_sleq);
|
|
|
|
OP31d(248, power_slliq);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31od(264, power_doz);
|
2024-03-27 12:15:30 +00:00
|
|
|
OP31d(277, power_lscbx);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31od(331, power_div);
|
|
|
|
OP31od(360, power_abs);
|
|
|
|
OP31od(363, power_divs);
|
|
|
|
OP31od(488, power_nabs);
|
2024-03-27 12:15:30 +00:00
|
|
|
OP31(531, power_clcs);
|
|
|
|
OP31d(537, power_rrib);
|
|
|
|
OP31d(541, power_maskir);
|
|
|
|
OP31d(664, power_srq);
|
|
|
|
OP31d(665, power_sre);
|
|
|
|
OP31d(696, power_sriq);
|
|
|
|
OP31d(728, power_srlq);
|
|
|
|
OP31d(729, power_sreq);
|
|
|
|
OP31d(760, power_srliq);
|
|
|
|
OP31d(920, power_sraq);
|
|
|
|
OP31d(921, power_srea);
|
|
|
|
OP31d(952, power_sraiq);
|
2024-03-26 11:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OP31(306, ppc_tlbie);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (!is_601) OP31(370, ppc_tlbia);
|
|
|
|
if (!is_601) OP31(566, ppc_tlbsync);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP31(854, ppc_eieio);
|
|
|
|
OP31(982, ppc_icbi);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (!is_601) OP31(978, ppc_tlbld);
|
|
|
|
if (!is_601) OP31(1010, ppc_tlbli);
|
2020-10-17 21:30:37 +00:00
|
|
|
|
2024-03-24 19:21:19 +00:00
|
|
|
std::fill_n(SubOpcode59Grabber, 64, ppc_illegalop);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP59d(18, ppc_fdivs);
|
|
|
|
OP59d(20, ppc_fsubs);
|
|
|
|
OP59d(21, ppc_fadds);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (ppc_state.spr[SPR::PVR] == PPC_VER::MPC970MP) OP59d(22, ppc_fsqrts);
|
|
|
|
if (!is_601) OP59d(24, ppc_fres);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP59d(25, ppc_fmuls);
|
|
|
|
OP59d(28, ppc_fmsubs);
|
|
|
|
OP59d(29, ppc_fmadds);
|
|
|
|
OP59d(30, ppc_fnmsubs);
|
|
|
|
OP59d(31, ppc_fnmadds);
|
2024-03-24 19:21:19 +00:00
|
|
|
|
|
|
|
std::fill_n(SubOpcode63Grabber, 2048, ppc_illegalop);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP63(0, ppc_fcmpu);
|
|
|
|
OP63d(12, ppc_frsp);
|
|
|
|
OP63d(14, ppc_fctiw);
|
|
|
|
OP63d(15, ppc_fctiwz);
|
|
|
|
OP63d(18, ppc_fdiv);
|
|
|
|
OP63d(20, ppc_fsub);
|
|
|
|
OP63d(21, ppc_fadd);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (ppc_state.spr[SPR::PVR] == PPC_VER::MPC970MP) OP63d(22, ppc_fsqrt);
|
|
|
|
if (!is_601) OP63d(26, ppc_frsqrte);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP63(32, ppc_fcmpo);
|
|
|
|
OP63d(38, ppc_mtfsb1);
|
|
|
|
OP63d(40, ppc_fneg);
|
|
|
|
OP63(64, ppc_mcrfs);
|
|
|
|
OP63d(70, ppc_mtfsb0);
|
|
|
|
OP63d(72, ppc_fmr);
|
|
|
|
OP63d(134, ppc_mtfsfi);
|
|
|
|
OP63d(136, ppc_fnabs);
|
|
|
|
OP63d(264, ppc_fabs);
|
2024-03-28 05:39:57 +00:00
|
|
|
if (is_601) OP63dc(583, ppc_mffs, IS601); else OP63dc(583, ppc_mffs, NOT601);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP63d(711, ppc_mtfsf);
|
|
|
|
|
|
|
|
for (int i = 0; i < 1024; i += 32) {
|
2024-03-28 05:39:57 +00:00
|
|
|
if (!is_601) OP63d(i + 23, ppc_fsel);
|
2024-03-26 11:57:52 +00:00
|
|
|
OP63d(i + 25, ppc_fmul);
|
|
|
|
OP63d(i + 28, ppc_fmsub);
|
|
|
|
OP63d(i + 29, ppc_fmadd);
|
|
|
|
OP63d(i + 30, ppc_fnmsub);
|
|
|
|
OP63d(i + 31, ppc_fnmadd);
|
2020-10-17 21:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 04:18:45 +00:00
|
|
|
void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version, bool include_601, uint64_t tb_freq)
|
2022-03-22 11:23:54 +00:00
|
|
|
{
|
2020-01-28 00:24:12 +00:00
|
|
|
int i;
|
|
|
|
|
2020-03-13 21:49:58 +00:00
|
|
|
mem_ctrl_instance = mem_ctrl;
|
|
|
|
|
2024-03-27 01:52:56 +00:00
|
|
|
std::memset(&ppc_state, 0, sizeof(ppc_state));
|
|
|
|
set_host_rounding_mode(0);
|
|
|
|
|
2024-03-27 01:41:16 +00:00
|
|
|
ppc_state.spr[SPR::PVR] = cpu_version;
|
2024-03-28 05:39:57 +00:00
|
|
|
is_601 = (cpu_version >> 16) == 1;
|
2024-03-27 01:41:16 +00:00
|
|
|
|
2024-04-09 04:18:45 +00:00
|
|
|
initialize_ppc_opcode_tables(include_601);
|
2020-11-14 01:59:49 +00:00
|
|
|
|
2022-01-10 15:36:14 +00:00
|
|
|
// initialize emulator timers
|
|
|
|
TimerManager::get_instance()->set_time_now_cb(&get_virt_time_ns);
|
|
|
|
TimerManager::get_instance()->set_notify_changes_cb(&force_cycle_counter_reload);
|
|
|
|
|
2021-12-19 23:10:02 +00:00
|
|
|
// initialize time base facility
|
2023-10-18 02:56:52 +00:00
|
|
|
g_realtime = false;
|
|
|
|
g_nanoseconds_base = now_ns();
|
|
|
|
g_icycles_base = 0;
|
2023-10-31 00:07:29 +00:00
|
|
|
g_icycles = 0;
|
2024-03-31 21:13:45 +00:00
|
|
|
//icnt_factor = 6;
|
|
|
|
icnt_factor = 4;
|
2021-12-19 23:10:02 +00:00
|
|
|
tbr_wr_timestamp = 0;
|
2022-09-16 03:21:54 +00:00
|
|
|
rtc_timestamp = 0;
|
2021-12-19 23:10:02 +00:00
|
|
|
tbr_wr_value = 0;
|
2022-09-16 04:22:37 +00:00
|
|
|
tbr_freq_ghz = (tb_freq << 32) / NS_PER_SEC;
|
2023-08-07 18:11:02 +00:00
|
|
|
tbr_period_ns = ((uint64_t)NS_PER_SEC << 32) / tb_freq;
|
2021-12-19 23:10:02 +00:00
|
|
|
|
2022-03-02 15:55:20 +00:00
|
|
|
exec_flags = 0;
|
2024-02-22 07:10:14 +00:00
|
|
|
exec_timer = false;
|
2022-03-02 15:55:20 +00:00
|
|
|
|
2020-01-03 20:01:02 +00:00
|
|
|
timebase_counter = 0;
|
2023-02-09 00:38:55 +00:00
|
|
|
dec_wr_value = 0;
|
2020-01-28 00:24:12 +00:00
|
|
|
|
|
|
|
|
2023-09-18 18:43:53 +00:00
|
|
|
if (is_601) {
|
2020-01-28 00:24:12 +00:00
|
|
|
/* MPC601 sets MSR[ME] bit during hard reset / Power-On */
|
2023-11-21 15:06:50 +00:00
|
|
|
ppc_state.msr = (MSR::ME + MSR::IP);
|
2020-01-28 00:24:12 +00:00
|
|
|
} else {
|
2023-11-21 15:06:50 +00:00
|
|
|
ppc_state.msr = MSR::IP;
|
2020-03-05 04:29:04 +00:00
|
|
|
ppc_state.spr[SPR::DEC] = 0xFFFFFFFFUL;
|
2020-01-28 00:24:12 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 18:43:53 +00:00
|
|
|
ppc_mmu_init();
|
2020-02-23 15:41:44 +00:00
|
|
|
|
2020-01-28 00:24:12 +00:00
|
|
|
/* redirect code execution to reset vector */
|
2020-03-05 14:48:10 +00:00
|
|
|
ppc_state.pc = 0xFFF00100;
|
2021-04-29 00:26:17 +00:00
|
|
|
|
|
|
|
#ifdef CPU_PROFILING
|
|
|
|
gProfilerObj->register_profile("PPC_CPU",
|
|
|
|
std::unique_ptr<BaseProfile>(new CPUProfile()));
|
|
|
|
#endif
|
2020-01-28 00:24:12 +00:00
|
|
|
}
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
void print_fprs() {
|
2020-02-23 14:24:49 +00:00
|
|
|
for (int i = 0; i < 32; i++)
|
2020-03-05 04:29:04 +00:00
|
|
|
cout << "FPR " << dec << i << " : " << ppc_state.fpr[i].dbl64_r << endl;
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static map<string, int> SPRName2Num = {
|
2024-03-05 15:34:09 +00:00
|
|
|
{"XER", SPR::XER}, {"LR", SPR::LR}, {"CTR", SPR::CTR},
|
|
|
|
{"DEC", SPR::DEC}, {"PVR", SPR::PVR}, {"SPRG0", SPR::SPRG0},
|
|
|
|
{"SPRG1", SPR::SPRG1}, {"SPRG2", SPR::SPRG2}, {"SPRG3", SPR::SPRG3},
|
|
|
|
{"SRR0", SPR::SRR0}, {"SRR1", SPR::SRR1}, {"IBAT0U", 528},
|
|
|
|
{"IBAT0L", 529}, {"IBAT1U", 530}, {"IBAT1L", 531},
|
|
|
|
{"IBAT2U", 532}, {"IBAT2L", 533}, {"IBAT3U", 534},
|
|
|
|
{"IBAT3L", 535}, {"DBAT0U", 536}, {"DBAT0L", 537},
|
|
|
|
{"DBAT1U", 538}, {"DBAT1L", 539}, {"DBAT2U", 540},
|
|
|
|
{"DBAT2L", 541}, {"DBAT3U", 542}, {"DBAT3L", 543},
|
|
|
|
{"HID0", SPR::HID0}, {"HID1", SPR::HID1}, {"IABR", 1010},
|
|
|
|
{"DABR", 1013}, {"L2CR", 1017}, {"ICTC", 1019},
|
|
|
|
{"THRM1", 1020}, {"THRM2", 1021}, {"THRM3", 1022},
|
|
|
|
{"PIR", 1023}, {"TBL", SPR::TBL_U}, {"TBU", SPR::TBU_U},
|
|
|
|
{"SDR1", SPR::SDR1}, {"MQ", SPR::MQ}, {"RTCU", SPR::RTCU_U},
|
|
|
|
{"RTCL", SPR::RTCL_U}, {"DSISR", SPR::DSISR}, {"DAR", SPR::DAR},
|
|
|
|
{"MMCR0", SPR::MMCR0}, {"PMC1", SPR::PMC1}, {"PMC2", SPR::PMC2},
|
|
|
|
{"SDA", SPR::SDA}, {"SIA", SPR::SIA}, {"MMCR1", SPR::MMCR1}
|
2023-08-07 18:38:18 +00:00
|
|
|
};
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
uint64_t reg_op(string& reg_name, uint64_t val, bool is_write) {
|
2020-02-23 14:24:49 +00:00
|
|
|
string reg_name_u, reg_num_str;
|
|
|
|
unsigned reg_num;
|
|
|
|
map<string, int>::iterator spr;
|
|
|
|
|
|
|
|
if (reg_name.length() < 2)
|
|
|
|
goto bail_out;
|
|
|
|
|
|
|
|
reg_name_u = reg_name;
|
|
|
|
|
|
|
|
/* convert reg_name string to uppercase */
|
2024-03-05 15:34:09 +00:00
|
|
|
std::for_each(reg_name_u.begin(), reg_name_u.end(), [](char& c) {
|
|
|
|
c = ::toupper(c);
|
|
|
|
});
|
2020-02-23 14:24:49 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (reg_name_u == "PC") {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.pc = (uint32_t)val;
|
2020-03-05 14:48:10 +00:00
|
|
|
return ppc_state.pc;
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
if (reg_name_u == "MSR") {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.msr = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.msr;
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
if (reg_name_u == "CR") {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.cr = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.cr;
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
if (reg_name_u == "FPSCR") {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.fpscr = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.fpscr;
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
2023-08-07 18:31:01 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2023-08-07 18:31:01 +00:00
|
|
|
try {
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_name_u.substr(0, 1) == "R") {
|
|
|
|
reg_num_str = reg_name_u.substr(1);
|
2022-12-21 11:20:39 +00:00
|
|
|
reg_num = (unsigned)stoul(reg_num_str, NULL, 0);
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_num < 32) {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.gpr[reg_num] = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.gpr[reg_num];
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 18:31:01 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2023-08-07 18:31:01 +00:00
|
|
|
try {
|
2023-08-11 08:20:09 +00:00
|
|
|
if (reg_name_u.substr(0, 1) == "F") {
|
|
|
|
reg_num_str = reg_name_u.substr(1);
|
2022-12-21 11:20:39 +00:00
|
|
|
reg_num = (unsigned)stoul(reg_num_str, NULL, 0);
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_num < 32) {
|
|
|
|
if (is_write)
|
2020-03-05 04:29:04 +00:00
|
|
|
ppc_state.fpr[reg_num].int64_r = val;
|
|
|
|
return ppc_state.fpr[reg_num].int64_r;
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 18:31:01 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2023-08-07 18:31:01 +00:00
|
|
|
try {
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_name_u.substr(0, 3) == "SPR") {
|
|
|
|
reg_num_str = reg_name_u.substr(3);
|
2022-12-21 11:20:39 +00:00
|
|
|
reg_num = (unsigned)stoul(reg_num_str, NULL, 0);
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_num < 1024) {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.spr[reg_num] = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.spr[reg_num];
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 18:31:01 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2023-08-07 18:31:01 +00:00
|
|
|
try {
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_name_u.substr(0, 2) == "SR") {
|
|
|
|
reg_num_str = reg_name_u.substr(2);
|
2022-12-21 11:20:39 +00:00
|
|
|
reg_num = (unsigned)stoul(reg_num_str, NULL, 0);
|
2020-02-23 14:24:49 +00:00
|
|
|
if (reg_num < 16) {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.sr[reg_num] = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.sr[reg_num];
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 18:31:01 +00:00
|
|
|
} catch (...) {
|
|
|
|
}
|
2020-02-23 14:24:49 +00:00
|
|
|
|
2023-08-07 18:31:01 +00:00
|
|
|
try {
|
2020-02-23 14:24:49 +00:00
|
|
|
spr = SPRName2Num.find(reg_name_u);
|
|
|
|
if (spr != SPRName2Num.end()) {
|
|
|
|
if (is_write)
|
2022-12-21 11:20:39 +00:00
|
|
|
ppc_state.spr[spr->second] = (uint32_t)val;
|
2020-03-05 04:29:04 +00:00
|
|
|
return ppc_state.spr[spr->second];
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
2020-05-12 18:55:45 +00:00
|
|
|
} catch (...) {
|
2020-02-23 14:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bail_out:
|
|
|
|
throw std::invalid_argument(string("Unknown register ") + reg_name);
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:06:20 +00:00
|
|
|
uint64_t get_reg(string reg_name) {
|
2020-02-23 14:24:49 +00:00
|
|
|
return reg_op(reg_name, 0, false);
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:06:20 +00:00
|
|
|
void set_reg(string reg_name, uint64_t val) {
|
2020-02-23 14:24:49 +00:00
|
|
|
reg_op(reg_name, val, true);
|
|
|
|
}
|