Compare commits

..
Author SHA1 Message Date
dingusdev fb277945c2 Floating point test fixes 2021-10-19 07:16:15 -07:00
Maxim Poliakovski 331b93d4d6 Fix compilation with Clang 11. 2021-10-17 23:41:53 +02:00
dingusdev 3c7fc58ac0 Small test case fixes for floating points 2021-10-16 22:33:56 -07:00
dingusdev 00dd99d851 Temporarily removing comparisons
These seem to cause the test program to crash. Will reinstate once the cause of the crash is resolved.
2021-10-15 20:02:00 -07:00
dingusdev 7d7a4b453f Partial test file reading fix 2021-10-15 06:28:11 -07:00
dingusdev 7ce94a6ab5 Further fixing the test code 2021-10-14 21:05:58 -07:00
dingusdev 061b061813 Floating point overhaul, part 4
Reworked the INF/NAN checks again - This time, only checking them when the result is calculated
2021-10-14 20:31:10 -07:00
dingusdev 9251745d6f Fixed floating-point test generation 2021-10-14 07:19:04 -07:00
dingusdev 5672a154cb Floating-point refactor, part 3
Condensed code to shorten enum names and remove casting.

Condensed mffs and partially fixed NAN checks for FADD(S).
2021-10-10 07:48:49 -07:00
dingusdev 2d65ed47fc Floating point overhaul, part 2
Further formatting fixes. Removed obsoleted separation definitions. Fixed rounding to nearest.
2021-10-09 19:42:25 -07:00
dingusdev e344b089b3 Floating point overhaul, part 1
Cleaned up formatting and reworked the INF/NAN checks
2021-10-09 15:08:53 -07:00
dingusdev d5960ca70b Fixed warnings for frsqrte and fsqrts 2021-10-06 18:59:31 -07:00
dingusdev 2106c4ca47 Fixed floating-point opcode table issues
fmuls, fmsubs, fmadds, fnmsubs, and fnmadds are all properly mapped now.
2021-10-06 18:36:23 -07:00
dingusdev ad6a2a9f17 Merge pull request #20 from dingusdev/floating-point
Merging floating point branch
2021-10-06 18:32:23 -07:00
dingusdev 1922a20cdd Partial revert for exception handling
This will be for a future update regarding 601 instructions
2021-10-05 17:42:55 -07:00
dingusdev f605c484dd ecowx and eciwx added 2021-10-05 17:40:09 -07:00
Maxim Poliakovski 69c357b70f ppcopcodes: fix compilation error with Clang 11. 2021-10-05 00:40:12 +02:00
Maxim Poliakovski c313a9c8bb Use std::bind() based callbacks. 2021-10-05 00:29:27 +02:00
Maxim Poliakovski 03e58dac35 Overhaul AWACs and implement PDM sound HW. 2021-10-05 00:29:27 +02:00
dingusdev 983e278498 Refactoring 601 opcode emulation - part 1
All opcodes should be emulated now. There was also a significant amount of clean-up, particularly with lscbx and the bit rotation/shifting instructions.
2021-10-01 22:37:28 -07:00
Maxim Poliakovski 3ca7a78a37 AWAC-PDM control and status registers. 2021-10-01 01:02:43 +02:00
Maxim Poliakovski 0f55877137 Add basic PDM I/O emulation. 2021-09-30 23:01:56 +02:00
Maxim Poliakovski b4d399ffa2 Improve three logging messages. 2021-09-30 23:01:56 +02:00
Maxim Poliakovski 8c9f23daf4 Fix memory controller interface for PDM. 2021-09-30 22:55:10 +02:00
Maxim Poliakovski 22827642e4 ppcmmu: implement 601-style BAT. 2021-09-27 12:37:35 +02:00
Maxim Poliakovski e9fcc51b93 Debugger fixes for PDM. 2021-09-26 14:21:31 +02:00
Maxim Poliakovski f104a634ea ppcmmu: some more cleanup. 2021-09-26 14:20:46 +02:00
Maxim Poliakovski 81ea96a058 ppcmmu.cpp: fix includes. 2021-09-25 23:38:27 +02:00
Maxim Poliakovski e052eb4a87 Merge branch 'atirage-hacks'. 2021-09-25 23:16:38 +02:00
Maxim Poliakovski d2cd43fcb1 Merge branch 'machine-pdm'. 2021-09-25 22:30:31 +02:00
Maxim Poliakovski 4b16cb826e Merge branch 'soft-tlb'. 2021-09-25 19:58:09 +02:00
Maxim Poliakovski f0b03aa1a2 zdocs: describe Heathrow ATA cells. 2021-09-18 02:26:32 +02:00
Maxim Poliakovski de8d6487f9 Add forgotten include path for CLI11. 2021-09-17 13:27:49 +02:00
Maxim Poliakovski ed56d018b0 zdocs: fix table in the Heathrow doc. 2021-09-16 16:35:37 +02:00
Maxim Poliakovski 8c13c30162 zdocs: rewamp Heathrow doc, part 1. 2021-09-16 16:29:36 +02:00
Maxim Poliakovski 84e111290f Fix includes for loguru and SDL. 2021-09-16 00:46:38 +02:00
dingusdev 1c77057860 Fixed building through CMake 2021-09-12 08:08:22 -07:00
dingusdev 738e2d3bd1 Fixed compiling for Visual Studio 2019 2021-09-11 22:55:24 -07:00
Maxim Poliakovski ea5b0d9f52 atirage: framebuffer rendering and various improvements. 2021-09-11 21:02:46 +02:00
Maxim Poliakovski 16d9e6c681 Basic MESH emulation skeleton with events logging. 2021-08-23 00:20:28 +02:00
Maxim Poliakovski 9ce15be106 ppcmmu.c: restructure and clean up. 2021-08-22 21:33:59 +02:00
Maxim Poliakovski e3a4539b02 WIP: Improve MMU emulation documentation. 2021-08-22 21:33:59 +02:00
Maxim Poliakovski 6d80ead5b1 Fix spelling in openfirmware.md. 2021-08-07 22:26:27 +02:00
Maxim Poliakovski 29dcd11648 Another small fix in openfirmware.md. 2021-08-07 22:24:46 +02:00
Maxim Poliakovski 62070659f8 Update openfirmware.md 2021-08-07 22:19:25 +02:00
Maxim Poliakovski 87ccfaed53 zdocs: rewrite OpenFirmware description. 2021-08-07 16:00:56 +02:00
Maxim Poliakovski 22a15f4780 ppcfpopcodes: fix mtfsb0 & mtfsb1 emulation. 2021-07-09 01:07:42 +02:00
dingusdev 48a65279e1 Fixed crf_d for mtfsb0 and mtfsb1 2021-07-07 18:58:02 -07:00
dingusdev 6abe86589b Update ppcfpopcodes.cpp 2021-07-07 08:00:37 -07:00
dingusdev 07a4166eef Preliminary work on floating point tests 2021-06-25 21:20:53 -07:00
Maxim Poliakovski 0cc1ac5731 debugger: PPC disassembler uses MMU instruction path. 2020-12-20 14:29:04 +01:00
Maxim Poliakovski 68b8da2182 Add PDM machine factory and the minimal HW. 2020-12-19 10:06:05 +01:00
Maxim Poliakovski 18a19cd52f Add an implementation for the Highspeed Memory Controller. 2020-12-19 09:56:39 +01:00
Maxim Poliakovski 282940e580 memctrlbase: fix range bug in add_mem_mirror. 2020-12-19 08:24:04 +01:00
56 changed files with 7229 additions and 1076 deletions

No files matched your search

+5
View File
@@ -11,6 +11,8 @@ set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
if (NOT WIN32)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
else()
include_directories("${PROJECT_SOURCE_DIR}/thirdparty/SDL2/include/")
if (UNIX AND NOT APPLE)
find_package (Threads)
endif()
@@ -82,6 +84,8 @@ include_directories("${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/devices"
"${PROJECT_SOURCE_DIR}/debugger"
"${PROJECT_SOURCE_DIR}/utils"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/"
"${PROJECT_SOURCE_DIR}/thirdparty/CLI11/"
# ${SDL2_INCLUDE_DIRS})
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/")
# "${PROJECT_SOURCE_DIR}/thirdparty/cubeb/include")
# ${LIBSOUNDIO_HEADERS})
@@ -165,6 +169,7 @@ if (DPPC_BUILD_PPC_TESTS)
TARGET testppc POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcinttests.csv"
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcfloattests.csv"
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcdisasmtest.csv"
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endif()
+2 -1
View File
@@ -1,4 +1,5 @@
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
+245 -208
View File
@@ -30,7 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <limits>
#include <stdexcept>
#include <stdio.h>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
// Affects the XER register's SO and OV Bits
@@ -42,12 +42,19 @@ inline void power_setsoov(uint32_t a, uint32_t b, uint32_t d) {
}
}
/** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */
static inline uint32_t power_rot_mask(unsigned rot_mb, unsigned rot_me) {
uint32_t m1 = 0xFFFFFFFFUL >> rot_mb;
uint32_t m2 = 0xFFFFFFFFUL << (31 - rot_me);
return ((rot_mb <= rot_me) ? m2 & m1 : m1 | m2);
}
void dppc_interpreter::power_abs() {
ppc_grab_regsda();
if (ppc_result_a == 0x80000000) {
ppc_result_d = ppc_result_a;
if (oe_flag)
ppc_state.spr[SPR::XER] |= 0x40000000;
ppc_state.spr[SPR::XER] |= 0xC0000000;
} else {
ppc_result_d = ppc_result_a & 0x7FFFFFFF;
@@ -62,21 +69,16 @@ void dppc_interpreter::power_abs() {
void dppc_interpreter::power_clcs() {
ppc_grab_regsda();
switch (reg_a) {
case 12:
case 13:
case 14:
case 15:
ppc_result_d = 65535;
case 12: //instruction cache line size
case 13: //data cache line size
case 14: //minimum line size
case 15: //maximum line size
ppc_result_d = 64;
break;
default:
ppc_result_d = 0;
}
if (rc_flag) {
ppc_changecrf0(ppc_result_d);
printf("Does RC do anything here? (TODO) \n");
}
ppc_store_result_regd();
}
@@ -111,88 +113,94 @@ void dppc_interpreter::power_doz() {
if (((int32_t)ppc_result_a) > ((int32_t)ppc_result_b)) {
ppc_result_d = 0;
} else {
ppc_result_d = ~ppc_result_a + ppc_result_b + 1;
ppc_result_d = ppc_result_b - ppc_result_a;
}
if (rc_flag)
ppc_changecrf0(ppc_result_d);
if (oe_flag)
power_setsoov(ppc_result_a, ppc_result_b, ppc_result_d);
ppc_store_result_rega();
}
void dppc_interpreter::power_dozi() {
ppc_grab_regsdab();
ppc_grab_regsdasimm();
if (((int32_t)ppc_result_a) > simm) {
ppc_result_d = 0;
} else {
ppc_result_d = ~ppc_result_a + simm + 1;
ppc_result_d = simm - ppc_result_a;
}
ppc_store_result_rega();
}
void dppc_interpreter::power_lscbx() {
ppc_grab_regsdab();
uint32_t bytes_copied = 0;
bool match_found = false;
uint32_t shift_amount = 0;
uint8_t return_value;
uint8_t byte_compared = (uint8_t)((ppc_state.spr[SPR::XER] & 0xFF00) >> 8);
if ((ppc_state.spr[SPR::XER] & 0x7f) == 0) {
return;
}
uint32_t bytes_to_load = (ppc_state.spr[SPR::XER] & 0x7f) + 1;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : ppc_result_a + ppc_result_b;
do {
ppc_effective_address++;
bytes_to_load--;
if (match_found == false) {
switch (shift_amount) {
case 0:
return_value = mmu_read_vmem<uint8_t>(ppc_effective_address);
//return_value = mem_grab_byte(ppc_effective_address);
ppc_result_d = (ppc_result_d & 0x00FFFFFF) | (return_value << 24);
ppc_store_result_regd();
break;
case 1:
return_value = mmu_read_vmem<uint8_t>(ppc_effective_address);
//return_value = mem_grab_byte(ppc_effective_address);
ppc_result_d = (ppc_result_d & 0xFF00FFFF) | (return_value << 16);
ppc_store_result_regd();
break;
case 2:
return_value = mmu_read_vmem<uint8_t>(ppc_effective_address);
//return_value = mem_grab_byte(ppc_effective_address);
ppc_result_d = (ppc_result_d & 0xFFFF00FF) | (return_value << 8);
ppc_store_result_regd();
break;
case 3:
return_value = mmu_read_vmem<uint8_t>(ppc_effective_address);
//return_value = mem_grab_byte(ppc_effective_address);
ppc_result_d = (ppc_result_d & 0xFFFFFF00) | return_value;
ppc_store_result_regd();
break;
}
bytes_copied++;
uint8_t return_value = 0;
uint32_t bytes_to_load = (ppc_state.spr[SPR::XER] & 0x7f);
uint32_t bytes_copied = 0;
uint8_t matching_byte = (uint8_t)((ppc_state.spr[SPR::XER] & 0xFF00) >> 8);
uint32_t byte_offset = 0;
//for storing each byte
uint32_t bitmask = 0;
uint32_t shift_amount = 0;
while (bytes_to_load > 0) {
if (byte_offset == 24) {
reg_d = (reg_d + 1) % 32;
ppc_result_d = 0xFFFFFFFF;
ppc_store_result_regd();
}
if (return_value == byte_compared) {
switch (byte_offset) {
case 0:
bitmask = 0x00FFFFFF;
shift_amount = 24;
break;
case 8:
bitmask = 0xFF00FFFF;
shift_amount = 16;
break;
case 16:
bitmask = 0xFFFF00FF;
shift_amount = 8;
break;
case 24:
bitmask = 0xFFFFFF00;
shift_amount = 0;
break;
}
return_value = mmu_read_vmem<uint8_t>(ppc_effective_address);
// return_value = mem_grab_byte(ppc_effective_address);
ppc_result_d = (ppc_result_d & bitmask) | (return_value << shift_amount);
ppc_store_result_regd();
bytes_copied++;
if (shift_amount == 3) {
shift_amount = 0;
reg_d = (reg_d + 1) & 0x1F;
} else {
shift_amount++;
if (return_value == matching_byte) {
//Match has been found - Time to break out
break;
}
} while (bytes_to_load > 0);
byte_offset += 8;
if (byte_offset == 32) {
byte_offset = 0;
}
ppc_effective_address++;
bytes_to_load--;
}
ppc_state.spr[SPR::XER] = (ppc_state.spr[SPR::XER] & 0xFFFFFF80) | bytes_copied;
if (rc_flag)
ppc_changecrf0(ppc_result_d);
ppc_store_result_regd();
}
void dppc_interpreter::power_maskg() {
@@ -202,16 +210,13 @@ void dppc_interpreter::power_maskg() {
uint32_t insert_mask = 0;
if (mask_start < (mask_end + 1)) {
for (uint32_t i = mask_start; i < mask_end; i++) {
insert_mask |= (0x80000000 >> i);
}
} else if (mask_start == (mask_end + 1)) {
insert_mask = power_rot_mask(mask_start, mask_end);
}
else if (mask_start == (mask_end + 1)) {
insert_mask = 0xFFFFFFFF;
} else {
insert_mask = 0xFFFFFFFF;
for (uint32_t i = (mask_end + 1); i < (mask_start - 1); i++) {
insert_mask &= (~(0x80000000 >> i));
}
}
else {
insert_mask = ~(power_rot_mask(mask_end + 1, mask_start - 1));
}
ppc_result_a = insert_mask;
@@ -224,17 +229,7 @@ void dppc_interpreter::power_maskg() {
void dppc_interpreter::power_maskir() {
ppc_grab_regssab();
uint32_t mask_insert = ppc_result_a;
uint32_t insert_rot = 0x80000000;
do {
if (ppc_result_b & insert_rot) {
mask_insert &= ~insert_rot;
mask_insert |= (ppc_result_d & insert_rot);
}
insert_rot = insert_rot >> 1;
} while (insert_rot > 0);
ppc_result_a = (ppc_result_d & ppc_result_b);
ppc_result_a = (ppc_result_d & ppc_result_b) | (~(ppc_result_b) & ppc_result_a);
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -270,24 +265,16 @@ void dppc_interpreter::power_rlmi() {
ppc_grab_regssab();
unsigned rot_mb = (ppc_cur_instruction >> 6) & 31;
unsigned rot_me = (ppc_cur_instruction >> 1) & 31;
uint32_t rot_amt = ppc_result_b & 31;
uint32_t insert_mask = 0;
unsigned rot_sh = ppc_result_b & 31;
if (rot_mb < (rot_me + 1)) {
for (uint32_t i = rot_mb; i < rot_me; i++) {
insert_mask |= (0x80000000 >> i);
}
} else if (rot_mb == (rot_me + 1)) {
insert_mask = 0xFFFFFFFF;
} else {
insert_mask = 0xFFFFFFFF;
for (uint32_t i = (rot_me + 1); i < (rot_mb - 1); i++) {
insert_mask &= (~(0x80000000 >> i));
}
}
uint32_t r = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
uint32_t mask = power_rot_mask(rot_mb, rot_me);
ppc_result_a = ((r & mask) | (ppc_result_a & ~mask));
if (rc_flag)
ppc_changecrf0(ppc_result_a);
uint32_t step2 = (ppc_result_d << rot_amt) | (ppc_result_d >> rot_amt);
ppc_result_a = step2 & insert_mask;
ppc_store_result_rega();
}
@@ -295,9 +282,9 @@ void dppc_interpreter::power_rrib() {
ppc_grab_regssab();
if (ppc_result_d & 0x80000000) {
ppc_result_a |= (0x80000000 >> ppc_result_b);
ppc_result_a |= ((ppc_result_d & 0x80000000) >> ppc_result_b);
} else {
ppc_result_a &= ~(0x80000000 >> ppc_result_b);
ppc_result_a &= ~((ppc_result_d & 0x80000000) >> ppc_result_b);
}
if (rc_flag)
@@ -307,15 +294,13 @@ void dppc_interpreter::power_rrib() {
}
void dppc_interpreter::power_sle() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
uint32_t rot_amt = ppc_result_b & 31;
for (uint32_t i = 31; i > rot_amt; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_final = ((ppc_result_d << rot_amt) | (ppc_result_d >> (32 - rot_amt)));
ppc_state.spr[SPR::MQ] = insert_final & insert_mask;
ppc_result_a = insert_final & insert_mask;
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
ppc_result_a = ppc_result_d << rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
ppc_store_result_rega();
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -324,24 +309,13 @@ void dppc_interpreter::power_sle() {
}
void dppc_interpreter::power_sleq() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
uint32_t rot_amt = ppc_result_b & 31;
for (uint32_t i = 31; i > rot_amt; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_start = ((ppc_result_d << rot_amt) | (ppc_result_d >> (rot_amt - 31)));
uint32_t insert_end = ppc_state.spr[SPR::MQ];
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
uint32_t r = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
uint32_t mask = power_rot_mask(0, 31 - rot_sh);
for (int i = 0; i < 32; i++) {
if (insert_mask & (1 << i)) {
insert_end &= ~(1 << i);
insert_end |= (insert_start & (1 << i));
}
}
ppc_result_a = insert_end;
ppc_state.spr[SPR::MQ] = insert_start;
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
ppc_state.spr[SPR::MQ] = r;
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -351,23 +325,10 @@ void dppc_interpreter::power_sleq() {
void dppc_interpreter::power_sliq() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
unsigned rot_sh = (ppc_cur_instruction >> 11) & 31;
for (uint32_t i = 31; i > rot_sh; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_start = ((ppc_result_d << rot_sh) | (ppc_result_d >> (rot_sh - 31)));
uint32_t insert_end = ppc_state.spr[SPR::MQ];
for (int i = 0; i < 32; i++) {
if (insert_mask & (1 << i)) {
insert_end &= ~(1 << i);
insert_end |= (insert_start & (1 << i));
}
}
ppc_result_a = insert_end & insert_mask;
ppc_state.spr[SPR::MQ] = insert_start;
ppc_result_a = ppc_result_d << rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -377,23 +338,12 @@ void dppc_interpreter::power_sliq() {
void dppc_interpreter::power_slliq() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
unsigned rot_sh = (ppc_cur_instruction >> 11) & 31;
for (uint32_t i = 31; i > rot_sh; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_start = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
uint32_t insert_end = ppc_state.spr[SPR::MQ];
uint32_t r = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
uint32_t mask = power_rot_mask(0, 31 - rot_sh);
for (int i = 0; i < 32; i++) {
if (insert_mask & (1 << i)) {
insert_end &= ~(1 << i);
insert_end |= (insert_start & (1 << i));
}
}
ppc_result_a = insert_end;
ppc_state.spr[SPR::MQ] = insert_start;
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
ppc_state.spr[SPR::MQ] = r;
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -402,59 +352,119 @@ void dppc_interpreter::power_slliq() {
}
void dppc_interpreter::power_sllq() {
LOG_F(WARNING, "OOPS! Placeholder for sllq!!! \n");
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
uint32_t r = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
uint32_t mask = power_rot_mask(0, 31 - rot_sh);
if (ppc_result_b >= 0x20) {
ppc_result_a = (ppc_state.spr[SPR::MQ] & mask);
}
else {
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
}
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_slq() {
LOG_F(WARNING, "OOPS! Placeholder for slq!!! \n");
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
if (ppc_result_b >= 0x20) {
ppc_result_a = ppc_result_d << rot_sh;
} else {
ppc_result_a = 0;
}
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
}
void dppc_interpreter::power_sraiq() {
LOG_F(WARNING, "OOPS! Placeholder for sraiq!!! \n");
ppc_grab_regssa();
unsigned rot_sh = (ppc_cur_instruction >> 11) & 0x1F;
uint32_t mask = (1 << rot_sh) - 1;
ppc_result_a = (int32_t)ppc_result_d >> rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
if ((ppc_result_d & 0x80000000UL) && (ppc_result_d & mask)) {
ppc_state.spr[SPR::XER] |= 0x20000000UL;
} else {
ppc_state.spr[SPR::XER] &= 0xDFFFFFFFUL;
}
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_sraq() {
LOG_F(WARNING, "OOPS! Placeholder for sraq!!! \n");
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 0x1F;
uint32_t mask = (1 << rot_sh) - 1;
ppc_result_a = (int32_t)ppc_result_d >> rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
if ((ppc_result_d & 0x80000000UL) && (ppc_result_d & mask)) {
ppc_state.spr[SPR::XER] |= 0x20000000UL;
} else {
ppc_state.spr[SPR::XER] &= 0xDFFFFFFFUL;
}
ppc_state.spr[SPR::MQ] = ((ppc_result_d << ppc_result_b) | (ppc_result_d >> (ppc_result_b)));
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_sre() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
uint32_t rot_amt = ppc_result_b & 31;
for (uint32_t i = 31; i > rot_amt; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_final = ((ppc_result_d >> rot_amt) | (ppc_result_d << (32 - rot_amt)));
ppc_state.spr[SPR::MQ] = insert_final & insert_mask;
ppc_result_a = insert_final;
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
ppc_result_a = ppc_result_d >> rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_srea() {
LOG_F(WARNING, "OOPS! Placeholder for srea!!! \n");
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 0x1F;
ppc_result_a = (int32_t)ppc_result_d >> rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
if ((ppc_result_d & 0x80000000UL) && (ppc_result_d & rot_sh)) {
ppc_state.spr[SPR::XER] |= 0x20000000UL;
} else {
ppc_state.spr[SPR::XER] &= 0xDFFFFFFFUL;
}
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_sreq() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
for (uint32_t i = 31; i > rot_sh; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_start = ((ppc_result_d >> rot_sh) | (ppc_result_d << (32 - rot_sh)));
uint32_t insert_end = ppc_state.spr[SPR::MQ];
unsigned mask = power_rot_mask(rot_sh, 31);
for (int i = 0; i < 32; i++) {
if (insert_mask & (1 << i)) {
insert_end &= ~(1 << i);
insert_end |= (insert_start & (1 << i));
}
}
ppc_result_a = insert_end;
ppc_state.spr[SPR::MQ] = insert_start;
ppc_result_a = ((rot_sh & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
ppc_state.spr[SPR::MQ] = rot_sh;
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -464,23 +474,9 @@ void dppc_interpreter::power_sreq() {
void dppc_interpreter::power_sriq() {
ppc_grab_regssa();
uint32_t insert_mask = 0;
unsigned rot_sh = (ppc_cur_instruction >> 11) & 31;
for (uint32_t i = 31; i > rot_sh; i--) {
insert_mask |= (1 << i);
}
uint32_t insert_start = ((ppc_result_d >> rot_sh) | (ppc_result_d << (32 - rot_sh)));
uint32_t insert_end = ppc_state.spr[SPR::MQ];
for (int i = 0; i < 32; i++) {
if (insert_mask & (1 << i)) {
insert_end &= ~(1 << i);
insert_end |= (insert_start & (1 << i));
}
}
ppc_result_a = insert_end;
ppc_state.spr[SPR::MQ] = insert_start;
unsigned rot_sh = (ppc_cur_instruction >> 11) & 31;
ppc_result_a = ppc_result_d >> rot_sh;
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (rot_sh)));
if (rc_flag)
ppc_changecrf0(ppc_result_a);
@@ -489,13 +485,54 @@ void dppc_interpreter::power_sriq() {
}
void dppc_interpreter::power_srliq() {
LOG_F(WARNING, "OOPS! Placeholder for slriq!!! \n");
ppc_grab_regssa();
unsigned rot_sh = (ppc_cur_instruction >> 11) & 31;
uint32_t r = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
unsigned mask = power_rot_mask(rot_sh, 31);
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
ppc_state.spr[SPR::MQ] = r;
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_srlq() {
LOG_F(WARNING, "OOPS! Placeholder for slrq!!! \n");
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
uint32_t r = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
unsigned mask = power_rot_mask(rot_sh, 31);
if (ppc_result_b >= 0x20) {
ppc_result_a = (ppc_state.spr[SPR::MQ] & mask);
}
else {
ppc_result_a = ((r & mask) | (ppc_state.spr[SPR::MQ] & ~mask));
}
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
void dppc_interpreter::power_srq() {
LOG_F(WARNING, "OOPS! Placeholder for srq!!! \n");
ppc_grab_regssab();
unsigned rot_sh = ppc_result_b & 31;
if (ppc_result_b >= 0x20) {
ppc_result_a = 0;
} else {
ppc_result_a = ppc_result_d >> rot_sh;
}
ppc_state.spr[SPR::MQ] = ((ppc_result_d << rot_sh) | (ppc_result_d >> (32 - rot_sh)));
if (rc_flag)
ppc_changecrf0(ppc_result_a);
ppc_store_result_rega();
}
+69 -8
View File
@@ -200,6 +200,71 @@ enum class BB_end_kind {
BB_RFI /* the rfi instruction is encountered */
};
enum CR_select : int32_t {
CR0_field = (0xF << 28),
CR1_field = (0xF << 24),
};
enum CRx_bit : uint32_t {
CR_SO = 0,
CR_EQ,
CR_GT,
CR_LT
};
enum CR1_bit : uint32_t {
CR1_OX = 24,
CR1_VX,
CR1_FEX,
CR1_FX,
};
enum FPSCR : uint32_t {
RN = 0x3,
NI = 0x4,
XE = 0x8,
ZE = 0x10,
UE = 0x20,
OE = 0x40,
VE = 0x80,
VXCVI = 0x100,
VXSQRT = 0x200,
VXSOFT = 0x400,
FPRF = 0x1F000,
FPCC_FUNAN = 0x10000,
FPCC_NEG = 0x8000,
FPCC_POS = 0x4000,
FPCC_ZERO = 0x2000,
FPCC_FPRCD = 0x1000,
FI = 0x20000,
FR = 0x40000,
VXVC = 0x80000,
VXIMZ = 0x100000,
VXZDZ = 0x200000,
VXIDI = 0x400000,
VXISI = 0x800000,
VXSNAN = 0x1000000,
XX = 0x2000000,
ZX = 0x4000000,
UX = 0x8000000,
OX = 0x10000000,
VX = 0x20000000,
FEX = 0x40000000,
FX = 0x80000000
};
//for inf and nan checks
enum FPOP : int {
DIV = 0x12,
SUB = 0x14,
ADD = 0x15,
MUL = 0x19,
FMSUB = 0x1C,
FMADD = 0x1D,
FNMSUB = 0x1E,
FNMADD = 0x1F,
};
/** PowerPC exception types. */
enum class Except_Type {
EXC_SYSTEM_RESET = 1,
@@ -255,8 +320,8 @@ extern uint64_t exceptions_processed;
#endif
// Function prototypes
extern void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t proc_version);
extern void ppc_mmu_init();
extern void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version);
extern void ppc_mmu_init(uint32_t cpu_version);
[[noreturn]] void ppc_illegalop();
[[noreturn]] void ppc_fpu_off();
@@ -271,7 +336,6 @@ void ppc_opcode63();
void initialize_ppc_opcode_tables();
extern bool ppc_confirm_inf_nan(uint64_t input_a, uint64_t input_b, bool is_single, uint32_t op);
extern double fp_return_double(uint32_t reg);
extern uint64_t fp_return_uint64(uint32_t reg);
@@ -339,6 +403,8 @@ extern void ppc_dcbtst();
extern void ppc_dcbz();
extern void ppc_divw();
extern void ppc_divwu();
extern void ppc_eciwx();
extern void ppc_ecowx();
extern void ppc_eieio();
extern void ppc_eqv();
extern void ppc_extsb();
@@ -414,15 +480,10 @@ extern void ppc_mtspr();
extern void ppc_mtfsb0();
extern void ppc_mtfsb1();
extern void ppc_mcrfs();
extern void ppc_mtfsb0dot();
extern void ppc_mtfsb1dot();
extern void ppc_fmr();
extern void ppc_mffs();
extern void ppc_mffsdot();
extern void ppc_mtfsf();
extern void ppc_mtfsfdot();
extern void ppc_mtfsfi();
extern void ppc_mtfsfidot();
extern void ppc_addi();
extern void ppc_addic();
+31 -16
View File
@@ -26,7 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <unordered_map>
#include "ppcemu.h"
@@ -253,8 +253,7 @@ void ppc_opcode31() {
oe_flag = ppc_cur_instruction & 0x400;
#ifdef EXHAUSTIVE_DEBUG
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(INFO, "Executing Opcode 63 table subopcode entry \n", regrab);
LOG_F(INFO, "Executing Opcode 31 table subopcode entry %n \n", (uint32_t)subop_grab);
#endif // EXHAUSTIVE_DEBUG
SubOpcode31Grabber[subop_grab]();
@@ -264,8 +263,7 @@ void ppc_opcode59() {
uint16_t subop_grab = (ppc_cur_instruction >> 1) & 0x1F;
rc_flag = ppc_cur_instruction & 1;
#ifdef EXHAUSTIVE_DEBUG
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(INFO, "Executing Opcode 59 table subopcode entry \n", regrab);
LOG_F(INFO, "Executing Opcode 59 table subopcode entry %n \n", (uint32_t)subop_grab);
#endif // EXHAUSTIVE_DEBUG
SubOpcode59Grabber[subop_grab]();
}
@@ -274,8 +272,7 @@ void ppc_opcode63() {
uint16_t subop_grab = (ppc_cur_instruction >> 1) & 0x3FF;
rc_flag = ppc_cur_instruction & 1;
#ifdef EXHAUSTIVE_DEBUG
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(INFO, "Executing Opcode 63 table subopcode entry \n", regrab);
LOG_F(INFO, "Executing Opcode 63 table subopcode entry %n \n", (uint32_t)subop_grab);
#endif // EXHAUSTIVE_DEBUG
SubOpcode63Grabber[subop_grab]();
}
@@ -606,6 +603,9 @@ void initialize_ppc_opcode_tables() {
SubOpcode31Grabber[918] = ppc_sthbrx;
SubOpcode31Grabber[983] = ppc_stfiwx;
SubOpcode31Grabber[310] = ppc_eciwx;
SubOpcode31Grabber[438] = ppc_ecowx;
SubOpcode31Grabber[24] = ppc_slw;
SubOpcode31Grabber[28] = ppc_and;
SubOpcode31Grabber[60] = ppc_andc;
@@ -684,11 +684,26 @@ void initialize_ppc_opcode_tables() {
SubOpcode59Grabber[21] = ppc_fadds;
SubOpcode59Grabber[22] = ppc_fsqrts;
SubOpcode59Grabber[24] = ppc_fres;
SubOpcode59Grabber[25] = ppc_fmuls;
SubOpcode59Grabber[28] = ppc_fmsubs;
SubOpcode59Grabber[29] = ppc_fmadds;
SubOpcode59Grabber[30] = ppc_fnmsubs;
SubOpcode59Grabber[31] = ppc_fnmadds;
for (int i = 25; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmuls;
}
for (int i = 28; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmsubs;
}
for (int i = 29; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmadds;
}
for (int i = 30; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fnmsubs;
}
for (int i = 31; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fnmadds;
}
SubOpcode63Grabber[0] = ppc_fcmpu;
SubOpcode63Grabber[12] = ppc_frsp;
@@ -736,7 +751,7 @@ void initialize_ppc_opcode_tables() {
}
}
void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t proc_version) {
void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t cpu_version) {
int i;
mem_ctrl_instance = mem_ctrl;
@@ -787,9 +802,9 @@ void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t proc_version) {
ppc_state.spr[i] = 0;
}
ppc_state.spr[SPR::PVR] = proc_version;
ppc_state.spr[SPR::PVR] = cpu_version;
if ((proc_version & 0xFFFF0000) == 0x00010000) {
if ((cpu_version & 0xFFFF0000) == 0x00010000) {
/* MPC601 sets MSR[ME] bit during hard reset / Power-On */
ppc_state.msr = 0x1040;
} else {
@@ -797,7 +812,7 @@ void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t proc_version) {
ppc_state.spr[SPR::DEC] = 0xFFFFFFFFUL;
}
ppc_mmu_init();
ppc_mmu_init(cpu_version);
/* redirect code execution to reset vector */
ppc_state.pc = 0xFFF00100;
+269 -307
View File
@@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "ppcmmu.h"
#include <array>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <cmath>
#include <iostream>
@@ -33,7 +34,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdexcept>
#include <stdio.h>
#include <unordered_map>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
// Used for FP calcs
uint64_t ppc_result64_b;
@@ -150,20 +151,11 @@ void ppc_fp_changecrf1() {
ppc_state.fpscr |= 0xf0000000;
}
void ppc_divbyzero(uint64_t input_a, uint64_t input_b, bool is_single) {
if (input_b == 0) {
ppc_state.fpscr |= 0x84000000;
if (input_a == 0) {
ppc_state.fpscr |= 0x200000;
}
}
}
int64_t round_to_nearest(double f) {
if (f >= 0.0) {
return static_cast<int32_t>(static_cast<int64_t> (f + 0.5));
return static_cast<int32_t>(static_cast<int64_t> (ceil(f)));
} else {
return static_cast<int32_t>(static_cast<int64_t> (-f + 0.5));
return static_cast<int32_t>(static_cast<int64_t> (floor(f)));
}
}
@@ -179,153 +171,113 @@ int64_t round_to_neg_inf(double f) {
return static_cast<int32_t>(floor(f));
}
void ppc_toggle_fpscr_fex() {
bool fex_result = ((ppc_state.fpscr & 0x20000000) & (ppc_state.fpscr & 0x80));
fex_result |= ((ppc_state.fpscr & 0x10000000) & (ppc_state.fpscr & 0x40));
fex_result |= ((ppc_state.fpscr & 0x8000000) & (ppc_state.fpscr & 0x20));
fex_result |= ((ppc_state.fpscr & 0x4000000) & (ppc_state.fpscr & 0x10));
fex_result |= ((ppc_state.fpscr & 0x2000000) & (ppc_state.fpscr & 0x8));
ppc_state.fpscr |= (fex_result << 30);
void update_fex() {
int fex_result = !!((ppc_state.fpscr & (ppc_state.fpscr << 22)) & 0x3E000000);
ppc_state.fpscr = (ppc_state.fpscr & ~0x40000000) | (fex_result << 30);
}
bool ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, bool is_single, uint32_t op) {
uint64_t input_a = ppc_state.fpr[chosen_reg_1].int64_r;
uint64_t input_b = ppc_state.fpr[chosen_reg_2].int64_r;
template <typename T, const FPOP fpop>
void ppc_confirm_inf_nan(int chosen_reg_1, int chosen_reg_2, int chosen_reg_3, bool rc_flag = false) {
T input_a = T(ppc_state.fpr[chosen_reg_1].int64_r);
T input_b = T(ppc_state.fpr[chosen_reg_2].int64_r);
T input_c = T(ppc_state.fpr[chosen_reg_3].int64_r);
if (is_single) {
uint32_t exp_a = (input_a >> 23) & 0xff;
uint32_t exp_b = (input_b >> 23) & 0xff;
ppc_state.fpscr &= 0x7fbfffff;
ppc_state.fpscr &= 0x7fbfffff;
switch (fpop) {
case FPOP::DIV:
if (isnan(input_a) && isnan(input_b)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXIDI);
} else if ((input_a == FP_ZERO) && (input_b == FP_ZERO)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXZDZ);
}
update_fex();
break;
case FPOP::SUB:
if (isnan(input_a) && isnan(input_b)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXISI);
}
update_fex();
break;
case FPOP::ADD:
if (isnan(input_a) && isnan(input_b)) {
ppc_state.fpscr |= (FPSCR::FX | FPSCR::VXISI);
}
update_fex();
break;
case FPOP::MUL:
if (((input_a == FP_ZERO) && (input_c == FP_INFINITE)) ||
((input_c == FP_ZERO) && (input_a == FP_INFINITE))) {
ppc_state.fpscr |=
(FPSCR::FX | FPSCR::VXSNAN |
FPSCR::VXIMZ);
}
update_fex();
break;
case FPOP::FMSUB:
case FPOP::FNMSUB:
if (isnan(input_a) || isnan(input_b) || isnan(input_c)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
if (((input_a == FP_ZERO) && (input_c == FP_INFINITE)) ||
((input_c == FP_ZERO) && (input_a == FP_INFINITE))) {
ppc_state.fpscr |= FPSCR::VXIMZ;
}
}
switch (op) {
case 36:
if ((exp_a == 0xff) && (exp_b == 0xff)) {
ppc_state.fpscr |= 0x80400000;
ppc_toggle_fpscr_fex();
return true;
} else if ((input_a == 0) && (input_b == 0)) {
ppc_state.fpscr |= 0x80200000;
ppc_toggle_fpscr_fex();
return true;
}
update_fex();
break;
case 40:
if ((exp_a == 0xff) && (exp_b == 0xff)) {
ppc_state.fpscr |= 0x80800000;
ppc_toggle_fpscr_fex();
return true;
case FPOP::FMADD:
case FPOP::FNMADD:
if (isnan(input_a) || isnan(input_b) || isnan(input_c)) {
ppc_state.fpscr |= (FPSCR::VXSNAN | FPSCR::FPCC_FUNAN);
}
update_fex();
break;
case 50:
if (((exp_a == 0xff) & (input_b == 0)) | ((exp_b == 0xff) & (input_a == 0))) {
ppc_state.fpscr |= 0x80100000;
ppc_toggle_fpscr_fex();
return true;
}
break;
case 56:
case 58:
if ((exp_a == 0xff) && (exp_b == 0xff)) {
ppc_state.fpscr |= 0x80800000;
ppc_toggle_fpscr_fex();
return true;
}
break;
default:
return false;
}
} else {
uint32_t exp_a = (input_a >> 52) & 0x7ff;
uint32_t exp_b = (input_b >> 52) & 0x7ff;
ppc_state.fpscr &= 0x7fbfffff;
switch (op) {
case 36:
if ((exp_a == 0x7ff) && (exp_b == 0x7ff)) {
ppc_state.fpscr |= 0x80400000;
ppc_toggle_fpscr_fex();
return true;
} else if ((input_a == 0) && (input_b == 0)) {
ppc_state.fpscr |= 0x80200000;
ppc_toggle_fpscr_fex();
return true;
}
break;
case 40:
if ((exp_a == 0x7ff) && (exp_b == 0x7ff)) {
ppc_state.fpscr |= 0x80800000;
ppc_toggle_fpscr_fex();
return true;
}
break;
case 50:
if (((exp_a == 0x7ff) & (input_b == 0)) | ((exp_b == 0x7ff) & (input_a == 0))) {
ppc_state.fpscr |= 0x80100000;
ppc_toggle_fpscr_fex();
return true;
}
break;
case 56:
case 58:
if ((exp_a == 0xff) && (exp_b == 0xff)) {
ppc_state.fpscr |= 0x80800000;
ppc_toggle_fpscr_fex();
return true;
}
break;
default:
return false;
}
}
return false;
}
void fpresult_update(uint64_t set_result, bool confirm_arc) {
void fpresult_update(double set_result, bool confirm_arc) {
bool confirm_ov = (bool)std::fetestexcept(FE_OVERFLOW);
if (ppc_state.fpscr & 0x3)
ppc_state.cr |= 0x2;
if (set_result > 0.0) {
ppc_state.fpscr |= FPSCR::FPCC_POS;
} else if (set_result < 0.0) {
ppc_state.fpscr |= FPSCR::FPCC_NEG;
} else if (set_result == 0.0) {
ppc_state.fpscr |= FPSCR::FPCC_ZERO;
} else {
ppc_state.fpscr |= FPSCR::FPCC_FPRCD;
}
if (confirm_ov) {
ppc_state.fpscr |= 0x80001000;
ppc_state.fpscr |= (FPSCR::FX | (FPSCR::FPRF & FPSCR::FPCC_FUNAN));
}
if (confirm_arc) {
ppc_state.fpscr |= 0x80010000;
ppc_state.fpscr &= 0xFFFF0FFF;
if (set_result == 0) {
ppc_state.fpscr |= 0x2000;
} else {
if (set_result < 0) {
ppc_state.fpscr |= 0x8000;
} else if (set_result > 0) {
ppc_state.fpscr |= 0x4000;
} else {
ppc_state.fpscr |= 0x1000;
}
}
}
}
void ppc_frsqrte_result() {
if (ppc_result64_d & 0x007FF000000000000UL) {
ppc_state.fpscr |= (FPSCR::FX | (FPSCR::FPRF & FPSCR::FPCC_FUNAN));
//ppc_state.fpscr &= 0xFFFF0FFF;
}
}
void ppc_changecrf1() {
ppc_state.cr &= 0xF0FFFFFF;
ppc_state.cr |= (ppc_state.fpscr & 0xF0000000) >> 4;
ppc_state.cr &= ~((uint32_t)CR_select::CR1_field);
ppc_state.cr |= (ppc_state.fpscr & (uint32_t)CR_select::CR0_field) >> 4;
}
// Floating Point Arithmetic
void dppc_interpreter::ppc_fadd() {
ppc_grab_regsfpdab();
if (!ppc_confirm_inf_nan(reg_a, reg_b, false, 58)) {
ppc_dblresult64_d = val_reg_a + val_reg_b;
ppc_dblresult64_d = val_reg_a + val_reg_b;
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, ADD>(reg_a, reg_b, 0, rc_flag);
}
if (rc_flag)
@@ -335,9 +287,13 @@ void dppc_interpreter::ppc_fadd() {
void dppc_interpreter::ppc_fsub() {
ppc_grab_regsfpdab();
if (!ppc_confirm_inf_nan(reg_a, reg_b, false, 56)) {
ppc_dblresult64_d = val_reg_a - val_reg_b;
ppc_dblresult64_d = val_reg_a - val_reg_b;
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, SUB>(reg_a, reg_b, 0, rc_flag);
}
if (rc_flag)
@@ -347,9 +303,13 @@ void dppc_interpreter::ppc_fsub() {
void dppc_interpreter::ppc_fdiv() {
ppc_grab_regsfpdab();
if (!ppc_confirm_inf_nan(reg_a, reg_b, false, 36)) {
ppc_dblresult64_d = val_reg_a / val_reg_b;
ppc_dblresult64_d = val_reg_a / val_reg_b;
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, DIV>(reg_a, reg_b, 0, rc_flag);
}
if (rc_flag)
@@ -359,9 +319,13 @@ void dppc_interpreter::ppc_fdiv() {
void dppc_interpreter::ppc_fmul() {
ppc_grab_regsfpdac();
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
ppc_dblresult64_d = val_reg_a * val_reg_c;
ppc_dblresult64_d = val_reg_a * val_reg_c;
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, MUL>(reg_a, reg_b, 0, rc_flag);
}
if (rc_flag)
@@ -371,14 +335,14 @@ void dppc_interpreter::ppc_fmul() {
void dppc_interpreter::ppc_fmadd() {
ppc_grab_regsfpdabc();
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
if (!ppc_confirm_inf_nan(reg_a, reg_b, false, 58)) {
ppc_dblresult64_d += val_reg_b;
}
}
ppc_dblresult64_d = std::fma(val_reg_a, val_reg_c, val_reg_b);
ppc_store_dfpresult_flt(reg_d);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, FMADD>(reg_a, reg_b, reg_c);
}
if (rc_flag)
ppc_changecrf1();
@@ -387,14 +351,15 @@ void dppc_interpreter::ppc_fmadd() {
void dppc_interpreter::ppc_fmsub() {
ppc_grab_regsfpdabc();
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
if (!ppc_confirm_inf_nan(reg_d, reg_b, false, 56)) {
ppc_dblresult64_d -= val_reg_b;
}
}
ppc_dblresult64_d = (val_reg_a * val_reg_c);
ppc_dblresult64_d -= val_reg_b;
ppc_store_dfpresult_flt(reg_d);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, FMSUB>(reg_a, reg_b, reg_c);
}
if (rc_flag)
ppc_changecrf1();
@@ -403,15 +368,16 @@ void dppc_interpreter::ppc_fmsub() {
void dppc_interpreter::ppc_fnmadd() {
ppc_grab_regsfpdabc();
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
if (!ppc_confirm_inf_nan(reg_a, reg_b, false, 58)) {
ppc_dblresult64_d += val_reg_b;
}
}
ppc_dblresult64_d = (val_reg_a * val_reg_c);
ppc_dblresult64_d += val_reg_b;
ppc_dblresult64_d = -ppc_dblresult64_d;
ppc_store_dfpresult_flt(reg_d);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
}
else {
ppc_confirm_inf_nan<double, FNMADD>(reg_a, reg_b, reg_c);
}
if (rc_flag)
ppc_changecrf1();
@@ -420,15 +386,16 @@ void dppc_interpreter::ppc_fnmadd() {
void dppc_interpreter::ppc_fnmsub() {
ppc_grab_regsfpdabc();
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
ppc_dblresult64_d = (val_reg_a * val_reg_c);
if (!ppc_confirm_inf_nan(reg_d, reg_b, false, 56)) {
ppc_dblresult64_d -= val_reg_b;
}
}
ppc_dblresult64_d = (val_reg_a * val_reg_c);
ppc_dblresult64_d -= val_reg_b;
ppc_dblresult64_d = -ppc_dblresult64_d;
ppc_store_dfpresult_flt(reg_d);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_dfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<double, FNMSUB>(reg_a, reg_b, reg_c);
}
if (rc_flag)
ppc_changecrf1();
@@ -437,10 +404,14 @@ void dppc_interpreter::ppc_fnmsub() {
void dppc_interpreter::ppc_fadds() {
ppc_grab_regsfpdab();
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 58)) {
float intermediate = (float)val_reg_a + (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
float intermediate = (float)val_reg_a + (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, ADD>(reg_a, reg_b, 0);
}
if (rc_flag)
@@ -450,23 +421,14 @@ void dppc_interpreter::ppc_fadds() {
void dppc_interpreter::ppc_fsubs() {
ppc_grab_regsfpdab();
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 56)) {
float intermediate = (float)val_reg_a - (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_store_sfpresult_flt(reg_d);
}
float intermediate = (float)val_reg_a - (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (rc_flag)
ppc_changecrf1();
}
void dppc_interpreter::ppc_fmuls() {
ppc_grab_regsfpdac();
if (!ppc_confirm_inf_nan(reg_a, reg_c, true, 50)) {
float intermediate = (float)val_reg_a * (float)val_reg_c;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, SUB>(reg_a, reg_b, 0);
}
if (rc_flag)
@@ -476,10 +438,31 @@ void dppc_interpreter::ppc_fmuls() {
void dppc_interpreter::ppc_fdivs() {
ppc_grab_regsfpdab();
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 36)) {
float intermediate = (float)val_reg_a / (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
float intermediate = (float)val_reg_a / (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, DIV>(reg_a, reg_b, 0);
}
if (rc_flag)
ppc_changecrf1();
}
void dppc_interpreter::ppc_fmuls() {
ppc_grab_regsfpdac();
float intermediate = (float)val_reg_a * (float)val_reg_c;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, MUL>(reg_a, 0, reg_c);
}
if (rc_flag)
@@ -489,16 +472,14 @@ void dppc_interpreter::ppc_fdivs() {
void dppc_interpreter::ppc_fmadds() {
ppc_grab_regsfpdabc();
float intermediate;
ppc_dblresult64_d = static_cast<double>(
std::fma((float)val_reg_a, (float)val_reg_c, (float)val_reg_b));
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 58)) {
intermediate = (float)val_reg_a * (float)val_reg_c;
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 58)) {
intermediate += (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_store_sfpresult_flt(reg_d);
}
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, FMADD>(reg_a, reg_b, reg_c);
}
if (rc_flag)
@@ -508,16 +489,15 @@ void dppc_interpreter::ppc_fmadds() {
void dppc_interpreter::ppc_fmsubs() {
ppc_grab_regsfpdabc();
float intermediate;
float intermediate = (float)val_reg_a * (float)val_reg_c;
intermediate -= (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
intermediate = (float)val_reg_a * (float)val_reg_c;
if (!ppc_confirm_inf_nan(reg_d, reg_b, false, 56)) {
intermediate -= (float)val_reg_b;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_store_sfpresult_flt(reg_d);
}
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, FMSUB>(reg_a, reg_b, reg_c);
}
if (rc_flag)
@@ -527,18 +507,16 @@ void dppc_interpreter::ppc_fmsubs() {
void dppc_interpreter::ppc_fnmadds() {
ppc_grab_regsfpdabc();
float intermediate;
float intermediate = (float)val_reg_a * (float)val_reg_c;
intermediate += (float)val_reg_b;
intermediate = -intermediate;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 58)) {
intermediate = (float)val_reg_a * (float)val_reg_c;
if (!ppc_confirm_inf_nan(reg_a, reg_b, true, 58)) {
intermediate += (float)val_reg_b;
intermediate = -intermediate;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_store_sfpresult_flt(reg_d);
}
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, FNMADD>(reg_a, reg_b, reg_c);
}
if (rc_flag)
@@ -548,18 +526,17 @@ void dppc_interpreter::ppc_fnmadds() {
void dppc_interpreter::ppc_fnmsubs() {
ppc_grab_regsfpdabc();
float intermediate;
float intermediate = (float)val_reg_a * (float)val_reg_c;
intermediate -= (float)val_reg_b;
intermediate = -intermediate;
ppc_dblresult64_d = static_cast<double>(intermediate);
if (!ppc_confirm_inf_nan(reg_a, reg_c, false, 50)) {
intermediate = (float)val_reg_a * (float)val_reg_c;
if (!ppc_confirm_inf_nan(reg_d, reg_b, false, 56)) {
intermediate -= (float)val_reg_b;
intermediate = -intermediate;
ppc_dblresult64_d = static_cast<double>(intermediate);
ppc_store_sfpresult_flt(reg_d);
}
if (!isnan(ppc_dblresult64_d)) {
ppc_store_sfpresult_flt(reg_d);
fpresult_update(ppc_dblresult64_d, rc_flag);
} else {
ppc_confirm_inf_nan<float, FNMSUB>(reg_a, reg_b, reg_c);
}
if (rc_flag)
@@ -603,7 +580,7 @@ void dppc_interpreter::ppc_fneg() {
void dppc_interpreter::ppc_fsel() {
ppc_grab_regsfpdabc();
ppc_dblresult64_d = (val_reg_a >= 0.0) ? val_reg_c : val_reg_b;
ppc_dblresult64_d = (val_reg_a >= -0.0) ? val_reg_c : val_reg_b;
ppc_store_dfpresult_flt(reg_d);
@@ -622,7 +599,7 @@ void dppc_interpreter::ppc_fsqrt() {
void dppc_interpreter::ppc_fsqrts() {
ppc_grab_regsfpdb();
uint32_t test = (uint32_t)ppc_result64_b;
uint32_t test = (uint32_t)(GET_FPR(reg_b));
test += 127 << 23;
test >>= 1;
uint64_t* pre_final = (uint64_t*)&test;
@@ -635,7 +612,7 @@ void dppc_interpreter::ppc_fsqrts() {
void dppc_interpreter::ppc_frsqrte() {
ppc_grab_regsfpdb();
double testd2 = (double)ppc_result64_b;
double testd2 = (double)(GET_FPR(reg_b));
for (int i = 0; i < 10; i++) {
testd2 = testd2 * (1.5 - (testd2 * .5) * testd2 * testd2);
}
@@ -658,11 +635,23 @@ void dppc_interpreter::ppc_frsp() {
void dppc_interpreter::ppc_fres() {
ppc_grab_regsfpdb();
float testf2 = (float)GET_FPR(reg_b);
double start_num = GET_FPR(reg_b);
float testf2 = (float)start_num;
testf2 = 1 / testf2;
ppc_dblresult64_d = (double)testf2;
ppc_store_dfpresult_flt(reg_d);
if (start_num == 0.0) {
ppc_state.fpscr |= FPSCR::ZX;
}
else if (std::isnan(start_num)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
}
else if (std::isinf(start_num)){
ppc_state.fpscr |= FPSCR::VXSNAN;
ppc_state.fpscr &= 0xFFF9FFFF;
}
if (rc_flag)
ppc_changecrf1();
}
@@ -673,26 +662,30 @@ void dppc_interpreter::ppc_fctiw() {
if (std::isnan(val_reg_b)) {
ppc_state.fpr[reg_d].int64_r = 0x80000000;
ppc_state.fpscr |= 0x1000100;
ppc_state.fpscr |= FPSCR::VXSNAN | FPSCR::VXCVI;
}
else if (val_reg_b > static_cast<double>(0x7fffffff)) {
ppc_state.fpr[reg_d].int64_r = 0x7fffffff;
ppc_state.fpscr |= 0x100;
ppc_state.fpscr |= FPSCR::VXCVI;
}
else if (val_reg_b < -static_cast<double>(0x80000000)) {
ppc_state.fpr[reg_d].int64_r = 0x80000000;
ppc_state.fpscr |= 0x100;
ppc_state.fpscr |= FPSCR::VXCVI;
}
else {
switch (ppc_state.fpscr & 0x3) {
case 0:
ppc_result64_d = round_to_nearest(val_reg_b);
break;
case 1:
ppc_result64_d = round_to_zero(val_reg_b);
break;
case 2:
ppc_result64_d = round_to_pos_inf(val_reg_b);
break;
case 3:
ppc_result64_d = round_to_neg_inf(val_reg_b);
break;
}
ppc_store_dfpresult_int(reg_d);
@@ -709,15 +702,15 @@ void dppc_interpreter::ppc_fctiwz() {
if (std::isnan(val_reg_b)) {
ppc_state.fpr[reg_d].int64_r = 0x80000000;
ppc_state.fpscr |= 0x1000100;
ppc_state.fpscr |= FPSCR::VXSNAN | FPSCR::VXCVI;
}
else if (val_reg_b > static_cast<double>(0x7fffffff)) {
ppc_state.fpr[reg_d].int64_r = 0x7fffffff;
ppc_state.fpscr |= 0x100;
ppc_state.fpscr |= FPSCR::VXCVI;
}
else if (val_reg_b < -static_cast<double>(0x80000000)) {
ppc_state.fpr[reg_d].int64_r = 0x80000000;
ppc_state.fpscr |= 0x100;
ppc_state.fpscr |= FPSCR::VXCVI;
}
else {
ppc_result64_d = round_to_zero(val_reg_b);
@@ -914,22 +907,14 @@ void dppc_interpreter::ppc_fmr() {
ppc_state.fpr[reg_d].dbl64_r = ppc_state.fpr[reg_b].dbl64_r;
}
void dppc_interpreter::ppc_mffs() {
ppc_grab_regsda();
uint64_t fpstore1 = ppc_state.fpr[reg_d].int64_r & 0xFFFFFFFF00000000;
uint64_t fpstore2 = ppc_state.fpscr & 0x00000000FFFFFFFF;
fpstore1 |= fpstore2;
uint64_t fpstore1 = ppc_state.fpr[reg_d].int64_r & ((uint64_t)0xFFF80000 << 32);
fpstore1 |= (uint64_t)ppc_state.fpscr;
fp_save_uint64(fpstore1);
}
void dppc_interpreter::ppc_mffsdot() {
ppc_grab_regsda();
uint64_t fpstore1 = ppc_state.fpr[reg_d].int64_r & 0xFFFFFFFF00000000;
uint64_t fpstore2 = ppc_state.fpscr & 0x00000000FFFFFFFF;
fpstore1 |= fpstore2;
fp_save_uint64(fpstore1);
ppc_fp_changecrf1();
if (rc_flag)
ppc_fp_changecrf1();
}
void dppc_interpreter::ppc_mtfsf() {
@@ -945,22 +930,9 @@ void dppc_interpreter::ppc_mtfsf() {
crm += (((fm_mask >> 7) & 1) == 1) ? 0x0000000F : 0x00000000;
uint32_t quickfprval = (uint32_t)ppc_state.fpr[reg_b].int64_r;
ppc_state.fpscr = (quickfprval & crm) | (quickfprval & ~(crm));
}
void dppc_interpreter::ppc_mtfsfdot() {
reg_b = (ppc_cur_instruction >> 11) & 31;
uint32_t fm_mask = (ppc_cur_instruction >> 17) & 255;
crm += ((fm_mask & 1) == 1) ? 0xF0000000 : 0x00000000;
crm += (((fm_mask >> 1) & 1) == 1) ? 0x0F000000 : 0x00000000;
crm += (((fm_mask >> 2) & 1) == 1) ? 0x00F00000 : 0x00000000;
crm += (((fm_mask >> 3) & 1) == 1) ? 0x000F0000 : 0x00000000;
crm += (((fm_mask >> 4) & 1) == 1) ? 0x0000F000 : 0x00000000;
crm += (((fm_mask >> 5) & 1) == 1) ? 0x00000F00 : 0x00000000;
crm += (((fm_mask >> 6) & 1) == 1) ? 0x000000F0 : 0x00000000;
crm += (((fm_mask >> 7) & 1) == 1) ? 0x0000000F : 0x00000000;
uint32_t quickfprval = (uint32_t)ppc_state.fpr[reg_b].int64_r;
ppc_state.fpscr = (quickfprval & crm) | (quickfprval & ~(crm));
ppc_fp_changecrf1();
if (rc_flag)
ppc_fp_changecrf1();
}
void dppc_interpreter::ppc_mtfsfi() {
@@ -969,45 +941,29 @@ void dppc_interpreter::ppc_mtfsfi() {
crf_d = crf_d << 2;
ppc_state.fpscr = (ppc_state.cr & ~(0xF0000000UL >> crf_d)) |
((ppc_state.spr[SPR::XER] & 0xF0000000UL) >> crf_d);
}
void dppc_interpreter::ppc_mtfsfidot() {
ppc_result_b = (ppc_cur_instruction >> 11) & 15;
crf_d = (ppc_cur_instruction >> 23) & 7;
crf_d = crf_d << 2;
ppc_state.fpscr = (ppc_state.cr & ~(0xF0000000UL >> crf_d)) |
((ppc_state.spr[SPR::XER] & 0xF0000000UL) >> crf_d);
ppc_fp_changecrf1();
if (rc_flag)
ppc_fp_changecrf1();
}
void dppc_interpreter::ppc_mtfsb0() {
crf_d = (ppc_cur_instruction >> 21) & 0x31;
crf_d = (ppc_cur_instruction >> 21) & 0x1F;
if ((crf_d == 0) || (crf_d > 2)) {
ppc_state.fpscr &= ~(1 << (31 - crf_d));
ppc_state.fpscr &= ~(0x80000000UL >> crf_d);
}
}
void dppc_interpreter::ppc_mtfsb0dot() {
crf_d = (ppc_cur_instruction >> 21) & 0x31;
if ((crf_d == 0) || (crf_d > 2)) {
ppc_state.fpscr &= ~(1 << crf_d);
}
ppc_fp_changecrf1();
if (rc_flag)
ppc_fp_changecrf1();
}
void dppc_interpreter::ppc_mtfsb1() {
crf_d = (ppc_cur_instruction >> 21) & 0x31;
crf_d = (ppc_cur_instruction >> 21) & 0x1F;
if ((crf_d == 0) || (crf_d > 2)) {
ppc_state.fpscr |= (1 << crf_d);
ppc_state.fpscr |= (0x80000000UL >> crf_d);
}
}
void dppc_interpreter::ppc_mtfsb1dot() {
crf_d = ~(ppc_cur_instruction >> 21) & 0x31;
if ((crf_d == 0) || (crf_d > 2)) {
ppc_state.fpscr |= (1 << crf_d);
}
ppc_fp_changecrf1();
if (rc_flag)
ppc_fp_changecrf1();
}
void dppc_interpreter::ppc_mcrfs() {
@@ -1027,25 +983,28 @@ void dppc_interpreter::ppc_fcmpo() {
ppc_state.fpscr &= 0xFFFF0FFF;
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
cmp_c |= 0x01;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_SO);
} else if (db_test_a < db_test_b) {
cmp_c |= 0x08;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_LT);
} else if (db_test_a > db_test_b) {
cmp_c |= 0x04;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_GT);
} else {
cmp_c |= 0x02;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_EQ);
}
ppc_state.fpscr |= (cmp_c << 12);
ppc_state.cr = ((ppc_state.cr & ~(0xf0000000 >> crf_d)) | ((cmp_c + xercon) >> crf_d));
fpresult_update(db_test_a, true);
if ((db_test_a == snan) || (db_test_b == snan)) {
ppc_state.fpscr |= 0x1000000;
ppc_state.fpscr = (ppc_state.fpscr & ~(FPSCR::FPRF)) | (cmp_c << 12);
ppc_state.cr =
((ppc_state.cr & ~((uint32_t)CR_select::CR0_field >> crf_d)) | ((cmp_c + xercon) >> crf_d));
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
if (ppc_state.fpscr & 0x80) {
ppc_state.fpscr |= 0x80000;
ppc_state.fpscr |= FPSCR::VXVC;
}
} else if ((db_test_a == qnan) || (db_test_b == qnan)) {
ppc_state.fpscr |= 0x80000;
ppc_state.fpscr |= FPSCR::VXVC;
}
}
@@ -1055,19 +1014,22 @@ void dppc_interpreter::ppc_fcmpu() {
ppc_state.fpscr &= 0xFFFF0FFF;
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
cmp_c |= 0x01;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_SO);
} else if (db_test_a < db_test_b) {
cmp_c |= 0x08;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_LT);
} else if (db_test_a > db_test_b) {
cmp_c |= 0x04;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_GT);
} else {
cmp_c |= 0x02;
cmp_c |= (1 << (uint32_t)CRx_bit::CR_EQ);
}
ppc_state.fpscr |= (cmp_c << 12);
ppc_state.cr = ((ppc_state.cr & ~(0xf0000000 >> crf_d)) | ((cmp_c + xercon) >> crf_d));
fpresult_update(db_test_a, true);
if ((db_test_a == snan) || (db_test_b == snan)) {
ppc_state.fpscr |= 0x1000000;
ppc_state.fpscr = (ppc_state.fpscr & ~(FPSCR::FPRF)) | (cmp_c << 12);
ppc_state.cr =
((ppc_state.cr & ~((uint32_t)CR_select::CR0_field >> crf_d)) | ((cmp_c + xercon) >> crf_d));
if (std::isnan(db_test_a) || std::isnan(db_test_b)) {
ppc_state.fpscr |= FPSCR::VXSNAN;
}
}
+228 -113
View File
@@ -21,29 +21,29 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file PowerPC Memory Management Unit emulation. */
/* TODO:
- implement 601-style BATs
*/
#include "ppcmmu.h"
#include "devices/memctrlbase.h"
#include "memaccess.h"
#include "ppcemu.h"
#include <array>
#include <cinttypes>
#include <cstdint>
#include <iostream>
#include <functional>
#include <loguru.hpp>
#include <stdexcept>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
/* pointer to exception handler to be called when a MMU exception is occured. */
void (*mmu_exception_handler)(Except_Type exception_type, uint32_t srr1_bits);
/* pointers to BAT update functions. */
std::function<void(uint32_t bat_reg)> ibat_update;
std::function<void(uint32_t bat_reg)> dbat_update;
/** PowerPC-style MMU BAT arrays (NULL initialization isn't prescribed). */
PPC_BAT_entry ibat_array[4] = {{0}};
PPC_BAT_entry dbat_array[4] = {{0}};
bool is_601_MMU = false;
//#define MMU_PROFILING // uncomment this to enable MMU profiling
//#define TLB_PROFILING // uncomment this to enable SoftTLB profiling
@@ -88,6 +88,49 @@ void ppc_set_cur_instruction(const uint8_t* ptr) {
ppc_cur_instruction = READ_DWORD_BE_A(ptr);
}
/** 601-style block address translation. */
static BATResult mpc601_block_address_translation(uint32_t la)
{
uint32_t pa; // translated physical address
uint8_t prot; // protection bits for the translated address
unsigned key;
bool bat_hit = false;
unsigned msr_pr = !!(ppc_state.msr & 0x4000);
// I/O controller interface takes precedence over BAT in 601
// Report BAT miss if T bit is set in the corresponding SR
if (ppc_state.sr[(la >> 28) & 0x0F] & 0x80000000) {
return BATResult{false, 0, 0};
}
for (int bat_index = 0; bat_index < 4; bat_index++) {
PPC_BAT_entry* bat_entry = &ibat_array[bat_index];
if (bat_entry->valid && ((la & bat_entry->hi_mask) == bat_entry->bepi)) {
bat_hit = true;
key = (((bat_entry->access & 1) & msr_pr) |
(((bat_entry->access >> 1) & 1) & (msr_pr ^ 1)));
// remapping BAT access from 601-style to PowerPC-style
static uint8_t access_conv[8] = {2, 2, 2, 1, 0, 1, 2, 1};
prot = access_conv[(key << 2) | bat_entry->prot];
#ifdef MMU_PROFILING
bat_transl_total++;
#endif
// logical to physical translation
pa = bat_entry->phys_hi | (la & ~bat_entry->hi_mask);
break;
}
}
return BATResult{bat_hit, prot, pa};
}
/** PowerPC-style block address translation. */
template <const BATType type>
static BATResult ppc_block_address_translation(uint32_t la)
@@ -196,7 +239,16 @@ static PATResult page_address_translation(uint32_t la, bool is_instr_fetch,
sr_val = ppc_state.sr[(la >> 28) & 0x0F];
if (sr_val & 0x80000000) {
ABORT_F("Direct-store segments not supported, LA=%0xX\n", la);
// check for 601-specific memory-forced I/O segments
if (((sr_val >> 20) & 0x1FF) == 0x7F) {
return PATResult{
(la & 0x0FFFFFFF) | (sr_val << 28),
0, // prot = read/write
1 // no C bit updates
};
} else {
ABORT_F("Direct-store segments not supported, LA=0x%X\n", la);
}
}
/* instruction fetch from a no-execute segment will cause ISI exception */
@@ -256,100 +308,6 @@ static PATResult page_address_translation(uint32_t la, bool is_instr_fetch,
};
}
/** PowerPC-style MMU instruction address translation. */
static uint32_t mmu_instr_translation(uint32_t la)
{
uint32_t pa; /* translated physical address */
bool bat_hit = false;
unsigned msr_pr = !!(ppc_state.msr & 0x4000);
// Format: %XY
// X - supervisor access bit, Y - problem/user access bit
// Those bits are mutually exclusive
unsigned access_bits = ((msr_pr ^ 1) << 1) | msr_pr;
for (int bat_index = 0; bat_index < 4; bat_index++) {
PPC_BAT_entry* bat_entry = &ibat_array[bat_index];
if ((bat_entry->access & access_bits) && ((la & bat_entry->hi_mask) == bat_entry->bepi)) {
bat_hit = true;
#ifdef MMU_PROFILING
bat_transl_total++;
#endif
if (!bat_entry->prot) {
mmu_exception_handler(Except_Type::EXC_ISI, 0x08000000);
}
// logical to physical translation
pa = bat_entry->phys_hi | (la & ~bat_entry->hi_mask);
break;
}
}
/* page address translation */
if (!bat_hit) {
PATResult pat_res = page_address_translation(la, true, msr_pr, 0);
pa = pat_res.phys;
#ifdef MMU_PROFILING
ptab_transl_total++;
#endif
}
return pa;
}
/** PowerPC-style MMU data address translation. */
static uint32_t ppc_mmu_addr_translate(uint32_t la, int is_write)
{
uint32_t pa; /* translated physical address */
bool bat_hit = false;
unsigned msr_pr = !!(ppc_state.msr & 0x4000);
// Format: %XY
// X - supervisor access bit, Y - problem/user access bit
// Those bits are mutually exclusive
unsigned access_bits = ((msr_pr ^ 1) << 1) | msr_pr;
for (int bat_index = 0; bat_index < 4; bat_index++) {
PPC_BAT_entry* bat_entry = &dbat_array[bat_index];
if ((bat_entry->access & access_bits) && ((la & bat_entry->hi_mask) == bat_entry->bepi)) {
bat_hit = true;
#ifdef MMU_PROFILING
bat_transl_total++;
#endif
if (!bat_entry->prot || ((bat_entry->prot & 1) && is_write)) {
ppc_state.spr[SPR::DSISR] = 0x08000000 | (is_write << 25);
ppc_state.spr[SPR::DAR] = la;
mmu_exception_handler(Except_Type::EXC_DSI, 0);
}
// logical to physical translation
pa = bat_entry->phys_hi | (la & ~bat_entry->hi_mask);
break;
}
}
/* page address translation */
if (!bat_hit) {
PATResult pat_res = page_address_translation(la, false, msr_pr, is_write);
pa = pat_res.phys;
#ifdef MMU_PROFILING
ptab_transl_total++;
#endif
}
return pa;
}
uint8_t* mmu_get_dma_mem(uint32_t addr, uint32_t size)
{
if (addr >= last_dma_area.start && (addr + size) <= last_dma_area.end) {
@@ -396,7 +354,7 @@ uint32_t tlb_size_mask = TLB_SIZE - 1;
// fake TLB entry for handling of unmapped memory accesses
uint64_t UnmappedVal = -1ULL;
TLBEntry UnmappedMem = {TLB_INVALID_TAG, 0, 0, 0};
TLBEntry UnmappedMem = {TLB_INVALID_TAG, TLBFlags::PAGE_NOPHYS, 0, 0};
uint8_t CurITLBMode = {0xFF}; // current ITLB mode
uint8_t CurDTLBMode = {0xFF}; // current DTLB mode
@@ -526,6 +484,7 @@ static TLBEntry* tlb2_target_entry(uint32_t gp_va)
static TLBEntry* itlb2_refill(uint32_t guest_va)
{
BATResult bat_res;
uint32_t phys_addr;
TLBEntry *tlb_entry;
uint16_t flags = 0;
@@ -533,7 +492,11 @@ static TLBEntry* itlb2_refill(uint32_t guest_va)
/* instruction address translation if enabled */
if (ppc_state.msr & 0x20) {
// attempt block address translation first
BATResult bat_res = ppc_block_address_translation<BATType::IBAT>(guest_va);
if (is_601_MMU) {
bat_res = mpc601_block_address_translation(guest_va);
} else {
bat_res = ppc_block_address_translation<BATType::IBAT>(guest_va);
}
if (bat_res.hit) {
// check block protection
// only PP = 0 (no access) causes ISI exception
@@ -565,7 +528,7 @@ static TLBEntry* itlb2_refill(uint32_t guest_va)
tlb_entry->tag = tag;
tlb_entry->flags = flags | TLBFlags::PAGE_MEM;
tlb_entry->host_va_offset = (int64_t)reg_desc->mem_ptr - guest_va +
(phys_addr - reg_desc->start);
(phys_addr - reg_desc->start);
} else {
ABORT_F("Instruction fetch from unmapped memory at 0x%08X!\n", phys_addr);
}
@@ -575,6 +538,7 @@ static TLBEntry* itlb2_refill(uint32_t guest_va)
static TLBEntry* dtlb2_refill(uint32_t guest_va, int is_write)
{
BATResult bat_res;
uint32_t phys_addr;
uint16_t flags = 0;
TLBEntry *tlb_entry;
@@ -584,7 +548,11 @@ static TLBEntry* dtlb2_refill(uint32_t guest_va, int is_write)
/* data address translation if enabled */
if (ppc_state.msr & 0x10) {
// attempt block address translation first
BATResult bat_res = ppc_block_address_translation<BATType::DBAT>(guest_va);
if (is_601_MMU) {
bat_res = mpc601_block_address_translation(guest_va);
} else {
bat_res = ppc_block_address_translation<BATType::DBAT>(guest_va);
}
if (bat_res.hit) {
// check block protection
if (!bat_res.prot || ((bat_res.prot & 1) && is_write)) {
@@ -637,9 +605,7 @@ static TLBEntry* dtlb2_refill(uint32_t guest_va, int is_write)
}
return tlb_entry;
} else {
LOG_F(ERROR, "Read from unmapped memory at 0x%08X!\n", phys_addr);
UnmappedMem.tag = tag;
UnmappedMem.host_va_offset = (int64_t)(&UnmappedVal) - guest_va;
LOG_F(WARNING, "Access to unmapped physical memory, phys_addr=0x%08X\n", phys_addr);
return &UnmappedMem;
}
}
@@ -763,6 +729,9 @@ inline T mmu_read_vmem(uint32_t guest_va)
// secondary TLB miss ->
// perform full address translation and refill the secondary TLB
tlb2_entry = dtlb2_refill(guest_va, 0);
if (tlb2_entry->flags & PAGE_NOPHYS) {
return UnmappedVal;
}
}
#ifdef TLB_PROFILING
else {
@@ -858,6 +827,9 @@ inline void mmu_write_vmem(uint32_t guest_va, T value)
// secondary TLB miss ->
// perform full address translation and refill the secondary TLB
tlb2_entry = dtlb2_refill(guest_va, 1);
if (tlb2_entry->flags & PAGE_NOPHYS) {
return;
}
}
#ifdef TLB_PROFILING
else {
@@ -1096,7 +1068,44 @@ void tlb_flush_pat_entries()
gTLBFlushPatEntries = false;
}
void ibat_update(uint32_t bat_reg)
static void mpc601_bat_update(uint32_t bat_reg)
{
PPC_BAT_entry *ibat_entry, *dbat_entry;
uint32_t bsm, hi_mask;
uint8_t key, pp, prot;
int upper_reg_num;
upper_reg_num = bat_reg & 0xFFFFFFFE;
ibat_entry = &ibat_array[(bat_reg - 528) >> 1];
dbat_entry = &dbat_array[(bat_reg - 528) >> 1];
if (ppc_state.spr[bat_reg | 1] & 0x40) {
bsm = ppc_state.spr[upper_reg_num + 1] & 0x3F;
hi_mask = ~((bsm << 17) | 0x1FFFF);
ibat_entry->valid = true;
ibat_entry->access = (ppc_state.spr[upper_reg_num] >> 2) & 3;
ibat_entry->prot = ppc_state.spr[upper_reg_num] & 3;
ibat_entry->hi_mask = hi_mask;
ibat_entry->phys_hi = ppc_state.spr[upper_reg_num + 1] & hi_mask;
ibat_entry->bepi = ppc_state.spr[upper_reg_num] & hi_mask;
// copy IBAT entry to DBAT entry
*dbat_entry = *ibat_entry;
} else {
// disable the corresponding BAT paars
ibat_entry->valid = false;
dbat_entry->valid = false;
}
if (!gTLBFlushBatEntries) {
gTLBFlushBatEntries = true;
add_ctx_sync_action(&tlb_flush_bat_entries);
}
}
static void ppc_ibat_update(uint32_t bat_reg)
{
int upper_reg_num;
uint32_t bl, hi_mask;
@@ -1122,7 +1131,7 @@ void ibat_update(uint32_t bat_reg)
}
}
void dbat_update(uint32_t bat_reg)
static void ppc_dbat_update(uint32_t bat_reg)
{
int upper_reg_num;
uint32_t bl, hi_mask;
@@ -1394,6 +1403,54 @@ static inline void write_phys_mem(AddressMapEntry *mru_rgn, uint32_t addr, T val
}
}
/** PowerPC-style MMU data address translation. */
static uint32_t ppc_mmu_addr_translate(uint32_t la, int is_write)
{
uint32_t pa; /* translated physical address */
bool bat_hit = false;
unsigned msr_pr = !!(ppc_state.msr & 0x4000);
// Format: %XY
// X - supervisor access bit, Y - problem/user access bit
// Those bits are mutually exclusive
unsigned access_bits = ((msr_pr ^ 1) << 1) | msr_pr;
for (int bat_index = 0; bat_index < 4; bat_index++) {
PPC_BAT_entry* bat_entry = &dbat_array[bat_index];
if ((bat_entry->access & access_bits) && ((la & bat_entry->hi_mask) == bat_entry->bepi)) {
bat_hit = true;
#ifdef MMU_PROFILING
bat_transl_total++;
#endif
if (!bat_entry->prot || ((bat_entry->prot & 1) && is_write)) {
ppc_state.spr[SPR::DSISR] = 0x08000000 | (is_write << 25);
ppc_state.spr[SPR::DAR] = la;
mmu_exception_handler(Except_Type::EXC_DSI, 0);
}
// logical to physical translation
pa = bat_entry->phys_hi | (la & ~bat_entry->hi_mask);
break;
}
}
/* page address translation */
if (!bat_hit) {
PATResult pat_res = page_address_translation(la, false, msr_pr, is_write);
pa = pat_res.phys;
#ifdef MMU_PROFILING
ptab_transl_total++;
#endif
}
return pa;
}
static void mem_write_unaligned(uint32_t addr, uint32_t value, uint32_t size) {
#ifdef MMU_DEBUG
LOG_F(WARNING, "Attempt to write unaligned %d bytes to 0x%08X\n", size, addr);
@@ -1660,6 +1717,52 @@ uint64_t mem_grab_qword(uint32_t addr) {
return read_phys_mem<uint64_t, true>(&last_read_area, addr);
}
/** PowerPC-style MMU instruction address translation. */
static uint32_t mmu_instr_translation(uint32_t la)
{
uint32_t pa; /* translated physical address */
bool bat_hit = false;
unsigned msr_pr = !!(ppc_state.msr & 0x4000);
// Format: %XY
// X - supervisor access bit, Y - problem/user access bit
// Those bits are mutually exclusive
unsigned access_bits = ((msr_pr ^ 1) << 1) | msr_pr;
for (int bat_index = 0; bat_index < 4; bat_index++) {
PPC_BAT_entry* bat_entry = &ibat_array[bat_index];
if ((bat_entry->access & access_bits) && ((la & bat_entry->hi_mask) == bat_entry->bepi)) {
bat_hit = true;
#ifdef MMU_PROFILING
bat_transl_total++;
#endif
if (!bat_entry->prot) {
mmu_exception_handler(Except_Type::EXC_ISI, 0x08000000);
}
// logical to physical translation
pa = bat_entry->phys_hi | (la & ~bat_entry->hi_mask);
break;
}
}
/* page address translation */
if (!bat_hit) {
PATResult pat_res = page_address_translation(la, true, msr_pr, 0);
pa = pat_res.phys;
#ifdef MMU_PROFILING
ptab_transl_total++;
#endif
}
return pa;
}
uint8_t* quickinstruction_translate(uint32_t addr) {
uint8_t* real_addr;
@@ -1736,9 +1839,21 @@ uint64_t mem_read_dbg(uint32_t virt_addr, uint32_t size) {
return ret_val;
}
void ppc_mmu_init() {
void ppc_mmu_init(uint32_t cpu_version)
{
mmu_exception_handler = ppc_exception_handler;
if ((cpu_version >> 16) == 1) {
// use 601-style BATs
ibat_update = &mpc601_bat_update;
is_601_MMU = true;
} else {
// use PPC-style BATs
ibat_update = &ppc_ibat_update;
dbat_update = &ppc_dbat_update;
is_601_MMU = false;
}
// invalidate all IDTLB entries
for (auto &tlb_el : itlb1_mode1) {
tlb_el.tag = TLB_INVALID_TAG;
+14 -11
View File
@@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <array>
#include <cinttypes>
#include <functional>
#include <vector>
#include "devices/memctrlbase.h"
@@ -34,11 +35,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** generic PowerPC BAT descriptor (MMU internal state) */
typedef struct PPC_BAT_entry {
uint8_t access; /* copy of Vs | Vp bits */
uint8_t prot; /* copy of PP bits */
uint32_t phys_hi; /* high-order bits for physical address generation */
uint32_t hi_mask; /* mask for high-order logical address bits */
uint32_t bepi; /* copy of Block effective page index */
bool valid; /* BAT entry valid for MPC601 */
uint8_t access; /* copy of Vs | Vp bits */
uint8_t prot; /* copy of PP bits */
uint32_t phys_hi; /* high-order bits for physical address generation */
uint32_t hi_mask; /* mask for high-order logical address bits */
uint32_t bepi; /* copy of Block effective page index */
} PPC_BAT_entry;
/** Block address translation types. */
@@ -85,14 +87,15 @@ typedef struct TLBEntry {
enum TLBFlags : uint16_t {
PAGE_MEM = 1 << 0, // memory page backed by host memory
PAGE_IO = 1 << 1, // memory mapped I/O page
TLBE_FROM_BAT = 1 << 2, // TLB entry has been translated with BAT
TLBE_FROM_PAT = 1 << 3, // TLB entry has been translated with PAT
PAGE_WRITABLE = 1 << 4, // page is writable
PTE_SET_C = 1 << 5, // tells if C bit of the PTE needs to be updated
PAGE_NOPHYS = 1 << 2, // no physical storage for this page (unmapped)
TLBE_FROM_BAT = 1 << 3, // TLB entry has been translated with BAT
TLBE_FROM_PAT = 1 << 4, // TLB entry has been translated with PAT
PAGE_WRITABLE = 1 << 5, // page is writable
PTE_SET_C = 1 << 6, // tells if C bit of the PTE needs to be updated
};
extern void ibat_update(uint32_t bat_reg);
extern void dbat_update(uint32_t bat_reg);
extern std::function<void(uint32_t bat_reg)> ibat_update;
extern std::function<void(uint32_t bat_reg)> dbat_update;
extern uint8_t* mmu_get_dma_mem(uint32_t addr, uint32_t size);
+52 -12
View File
@@ -31,7 +31,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <thread>
#include <vector>
@@ -73,7 +73,7 @@ void ppc_store_result_rega() {
ppc_state.gpr[reg_a] = ppc_result_a;
}
inline void ppc_grab_regsdasimm() {
void ppc_grab_regsdasimm() {
reg_d = (ppc_cur_instruction >> 21) & 31;
reg_a = (ppc_cur_instruction >> 16) & 31;
simm = (int32_t)((int16_t)((ppc_cur_instruction)&65535));
@@ -909,17 +909,17 @@ void dppc_interpreter::ppc_mtspr() {
void dppc_interpreter::ppc_mftb() {
uint32_t ref_spr = (((ppc_cur_instruction >> 11) & 31) << 5) | ((ppc_cur_instruction >> 16) & 31);
reg_d = (ppc_cur_instruction >> 21) & 31;
reg_d = (ppc_cur_instruction >> 21) & 31;
switch (ref_spr) {
case 268:
ppc_state.gpr[reg_d] = timebase_counter & 0xFFFFFFFFUL;
break;
case 269:
ppc_state.gpr[reg_d] = (timebase_counter >> 32) & 0xFFFFFFFFUL;
break;
default:
std::cout << "Invalid TBR access attempted!" << std::endl;
}
case 268:
ppc_state.gpr[reg_d] = timebase_counter & 0xFFFFFFFFUL;
break;
case 269:
ppc_state.gpr[reg_d] = (timebase_counter >> 32) & 0xFFFFFFFFUL;
break;
default:
LOG_F(ERROR, "Invalid TBR access attempted !\n");
}
}
void dppc_interpreter::ppc_mtcrf() {
@@ -2096,6 +2096,46 @@ void dppc_interpreter::ppc_stswx() {
}
}
void dppc_interpreter::ppc_eciwx() {
uint32_t ear_enable = 0x80000000;
// error if EAR[E] != 1
if (!(ppc_state.spr[282] && ear_enable)) {
ppc_exception_handler(Except_Type::EXC_DSI, 0x0);
}
ppc_grab_regsdab();
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
if (ppc_effective_address & 0x3) {
ppc_exception_handler(Except_Type::EXC_ALIGNMENT, 0x0);
}
ppc_result_d = mmu_read_vmem<uint32_t>(ppc_effective_address);
ppc_store_result_regd();
}
void dppc_interpreter::ppc_ecowx() {
uint32_t ear_enable = 0x80000000;
// error if EAR[E] != 1
if (!(ppc_state.spr[282] && ear_enable)) {
ppc_exception_handler(Except_Type::EXC_DSI, 0x0);
}
ppc_grab_regssab();
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
if (ppc_effective_address & 0x3) {
ppc_exception_handler(Except_Type::EXC_ALIGNMENT, 0x0);
}
mmu_write_vmem<uint32_t>(ppc_effective_address, ppc_result_d);
ppc_store_result_regd();
}
// TLB Instructions
void dppc_interpreter::ppc_tlbie() {
+290 -44
View File
@@ -99,6 +99,94 @@ def gen_ppc_opcode(opc_str, imm):
return (0x1F << 26) + (3 << 21) + (3 << 16) + (0x39A << 1)
elif opc_str == "EXTSH.":
return (0x1F << 26) + (3 << 21) + (3 << 16) + (0x39A << 1) + 1
elif opc_str == "FABS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x108 << 1)
elif opc_str == "FABS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x108 << 1) + 1
elif opc_str == "FADD":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1)
elif opc_str == "FADD.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
elif opc_str == "FADDS":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1)
elif opc_str == "FADDS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
elif opc_str == "FCMPO":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0x20 << 1)
elif opc_str == "FCMPU":
return (0x3B << 26) + (3 << 16) + (4 << 11)
elif opc_str == "FCTIW":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0xE << 1)
elif opc_str == "FCTIW.":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0xE << 1) + 1
elif opc_str == "FCTIWZ":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0xF << 1)
elif opc_str == "FCTIWZ.":
return (0x3B << 26) + (3 << 16) + (4 << 11) + (0xF << 1) + 1
elif opc_str == "FDIV":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x12 << 1)
elif opc_str == "FDIV.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x12 << 1) + 1
elif opc_str == "FDIVS":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x12 << 1)
elif opc_str == "FDIVS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x12 << 1) + 1
elif opc_str == "FMADD":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1D << 1)
elif opc_str == "FMADD.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1D << 1) + 1
elif opc_str == "FMADDS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1D << 1)
elif opc_str == "FMADDS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1D << 1) + 1
elif opc_str == "FMUL":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1)
elif opc_str == "FMUL.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1) + 1
elif opc_str == "FMULS":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1)
elif opc_str == "FMULS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x19 << 1) + 1
elif opc_str == "FNABS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x88 << 1)
elif opc_str == "FNABS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x88 << 1) + 1
elif opc_str == "FNEG":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x28 << 1)
elif opc_str == "FMULS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 6) + (0x28 << 1) + 1
elif opc_str == "FMSUB":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1)
elif opc_str == "FMSUB.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1) + 1
elif opc_str == "FMSUBS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1)
elif opc_str == "FMSUBS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1) + 1
elif opc_str == "FNMADD":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1F << 1)
elif opc_str == "FNMADD.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1F << 1) + 1
elif opc_str == "FNMADDS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1F << 1)
elif opc_str == "FNMADDS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1F << 1) + 1
elif opc_str == "FNMSUB":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1)
elif opc_str == "FNMSUB.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1) + 1
elif opc_str == "FNMSUBS":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1)
elif opc_str == "FNMSUBS.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (5 << 6) + (0x1C << 1) + 1
elif opc_str == "FSUB":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1)
elif opc_str == "FSUB.":
return (0x3F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
elif opc_str == "FSUBS":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1)
elif opc_str == "FSUBS.":
return (0x3B << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x15 << 1) + 1
elif opc_str == "MULHW":
return (0x1F << 26) + (3 << 21) + (3 << 16) + (4 << 11) + (0x4B << 1)
elif opc_str == "MULHW.":
@@ -262,51 +350,209 @@ def extract_rot_params(line):
pos += 16
return (sh, mb, me)
if __name__ == "__main__":
with open("ppcinttest.txt", "r") as in_file:
with open("ppcinttests.csv", "w") as out_file:
lineno = 0
for line in in_file:
lineno += 1
with open("ppcinttest.txt", "r") as in_file:
with open("ppcinttests.csv", "w") as out_file:
lineno = 0
for line in in_file:
lineno += 1
line = line.strip()
opcode = (line[0:8]).rstrip().upper()
out_file.write(opcode + ",")
line = line.strip()
opcode = (line[0:8]).rstrip().upper()
out_file.write(opcode + ",")
if opcode.startswith("RLWI"):
sh, mb, me = extract_rot_params(line)
out_file.write("0x{:X}".format(gen_rot_opcode(opcode, sh, mb, me)))
else:
imm = extract_imm(line)
out_file.write("0x{:X}".format(gen_ppc_opcode(opcode, imm)))
pos = 12
while pos < len(line):
reg_id = line[pos:pos+4]
if reg_id.startswith("rD"):
out_file.write(",rD=" + line[pos+3:pos+13])
pos += 16
elif reg_id.startswith("rA"):
out_file.write(",rA=" + line[pos+3:pos+13])
pos += 16
elif reg_id.startswith("rB") or reg_id.startswith("rS"):
out_file.write(",rB=" + line[pos+3:pos+13])
pos += 16
elif reg_id.startswith("XER:"):
out_file.write(",XER=" + line[pos+5:pos+15])
pos += 18
elif reg_id.startswith("CR:"):
out_file.write(",CR=" + line[pos+4:pos+14])
pos += 17
elif reg_id.startswith("imm"):
pos += 17 # ignore immediate operands
elif reg_id.startswith("SH"):
pos += 16
elif reg_id.startswith("MB") or reg_id.startswith("ME"):
pos += 16
if opcode.startswith("RLWI"):
sh, mb, me = extract_rot_params(line)
out_file.write("0x{:X}".format(gen_rot_opcode(opcode, sh, mb, me)))
else:
out_file.write("Unknown reg ID" + reg_id)
break
imm = extract_imm(line)
out_file.write("0x{:X}".format(gen_ppc_opcode(opcode, imm)))
out_file.write("\n")
pos = 12
while pos < len(line):
reg_id = line[pos:pos+4]
if reg_id.startswith("rD"):
out_file.write(",rD=" + line[pos+3:pos+13])
pos += 16
elif reg_id.startswith("rA"):
out_file.write(",rA=" + line[pos+3:pos+13])
pos += 16
elif reg_id.startswith("rB") or reg_id.startswith("rS"):
out_file.write(",rB=" + line[pos+3:pos+13])
pos += 16
elif reg_id.startswith("XER:"):
out_file.write(",XER=" + line[pos+5:pos+15])
pos += 18
elif reg_id.startswith("CR:"):
out_file.write(",CR=" + line[pos+4:pos+14])
pos += 17
elif reg_id.startswith("imm"):
pos += 17 # ignore immediate operands
elif reg_id.startswith("SH"):
pos += 16
elif reg_id.startswith("MB") or reg_id.startswith("ME"):
pos += 16
else:
out_file.write("Unknown reg ID" + reg_id)
break
out_file.write("\n")
with open("ppcfloattest.txt", "r") as in_file:
with open("ppcfloattests.csv", "w") as out_file:
lineno = 0
for line in in_file:
lineno += 1
line = line.strip()
opcode = (line[0:8]).rstrip().upper()
out_file.write(opcode+ ",")
out_file.write("0x{:X}".format(gen_ppc_opcode(opcode, 0)))
pos = 10
checkstring = ''
while pos < len(line):
if (line[pos].isalnum()):
checkstring += line[pos]
if ("RTN" in checkstring):
if (line[pos+1:pos+2] == "I"):
out_file.write(",round=RNI")
checkstring = ''
else:
out_file.write(",round=RTN")
checkstring = ''
pos += 1
elif ("RTZ" in checkstring):
out_file.write(",round=RTZ")
checkstring = ''
pos += 1
elif ("RTPI" in checkstring):
out_file.write(",round=RPI")
checkstring = ''
pos += 1
elif ("VE" in checkstring):
out_file.write(",round=VEN")
checkstring = ''
pos += 1
elif ("frD" in checkstring):
out_file.write(",frD=0x" + line[pos+4:pos+20])
checkstring = ''
elif ("frA" in checkstring): #sloppy temp code
check2 = line[pos+1:pos+10]
if ("-inf" in check2):
out_file.write(",frA=-inf")
elif ("inf" in check2):
out_file.write(",frA=inf")
pos += 1
elif ("nan" in check2):
out_file.write(",frA=nan")
pos += 1
elif ("-0." or "-2." or "-4." or \
"-6." or "-8." or "-7." or\
"-5." or "-3." or "-1." or \
"-9." in check2):
get_pos = line[pos+2:pos+16].strip("|")
out_file.write(",frA=" + get_pos.strip())
elif ("0." or "2." or "4." or \
"6." or "8." or "7." or\
"5." or "3." or "1." or \
"9." in check2):
get_pos = line[pos+2:pos+15]
out_file.write(",frA=" + get_pos.strip())
checkstring = ''
elif ("frB" in checkstring): #sloppy temp code
check2 = line[pos+1:pos+10]
if ("-inf" in check2):
out_file.write(",frB=-inf")
elif ("inf" in check2):
out_file.write(",frB=inf")
pos += 1
elif ("nan" in check2):
out_file.write(",frB=nan")
pos += 1
elif ("-0." or "-2." or "-4." or \
"-6." or "-8." or "-7." or\
"-5." or "-3." or "-1." or \
"-9." in check2):
get_pos = line[pos+2:pos+16].strip("|")
out_file.write(",frB=" + get_pos.strip())
elif ("0." or "2." or "4." or \
"6." or "8." or "7." or\
"5." or "3." or "1." or \
"9." in check2):
get_pos = line[pos+2:pos+15]
out_file.write(",frB=" + get_pos.strip())
checkstring = ''
elif ("frC" in checkstring): #sloppy temp code
check2 = line[pos+1:pos+10]
if ("-inf" in check2):
out_file.write(",frC=-inf")
elif ("inf" in check2):
out_file.write(",frC=inf")
pos += 1
elif ("nan" in check2):
out_file.write(",frC=nan")
pos += 1
elif ("-0." or "-2." or "-4." or \
"-6." or "-8." or "-7." or\
"-5." or "-3." or "-1." or \
"-9." in check2):
get_pos = line[pos+2:pos+16].strip("|")
out_file.write(",frC=" + get_pos.strip())
elif ("0." or "2." or "4." or \
"6." or "8." or "7." or\
"5." or "3." or "1." or \
"9." in check2):
get_pos = line[pos+2:pos+15]
out_file.write(",frC=" + get_pos.strip())
checkstring = ''
elif ("FPSCR" in checkstring):
out_file.write(",FPSCR=" + line[pos+3:pos+13])
checkstring = ''
elif ("CR" in checkstring):
out_file.write(",CR=0x0" + line[pos+6:pos+14])
checkstring = ''
pos += 1
# reg_id = line[pos:pos+4]
# if reg_id.startswith("frD"):
# out_file.write(",frD=" + line[pos+4:pos+22])
# pos += 24
# elif reg_id.startswith("frA"):
# out_file.write(",frA=" + line[pos+4:pos+14])
# pos += 16
# elif reg_id.startswith("frB"):
# out_file.write(",frB=" + line[pos+4:pos+14])
# pos += 16
# elif reg_id.startswith("frC"):
# out_file.write(",frC=" + line[pos+4:pos+14])
# pos += 16
# elif reg_id.startswith("FPSCR:"):
# out_file.write(",FPSCR=" + line[pos+7:pos+17])
# pos += 19
# elif reg_id.startswith("CR:"):
# out_file.write(",CR=" + line[pos+4:pos+14])
# pos += 17
# elif reg_id.startswith("VE)"):
# out_file.write("round=VEN" + line[pos+4:pos+20])
# pos += 17
# elif reg_id.startswith("TN)"):
# out_file.write("round=RTN" + line[pos+4:pos+20])
# pos += 17
# elif reg_id.startswith("TZ)"):
# out_file.write("round=RTZ" + line[pos+4:pos+20])
# pos += 17
# elif reg_id.startswith("NI)"):
# out_file.write("round=RNI" + line[pos+4:pos+20])
# pos += 17
# elif reg_id.startswith("PI)"):
# out_file.write("round=RPI" + line[pos+4:pos+20])
# pos += 17
# else:
# out_file.write("Unknown reg ID" + reg_id)
# break
out_file.write("\n")
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+129
View File
@@ -1,5 +1,6 @@
#include "../ppcdisasm.h"
#include "../ppcemu.h"
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
@@ -117,6 +118,7 @@ static void read_test_data() {
ppc_state.gpr[3] = src1;
ppc_state.gpr[4] = src2;
ppc_state.spr[SPR::XER] = 0;
ppc_state.cr = 0;
@@ -141,6 +143,129 @@ static void read_test_data() {
}
}
static void read_test_float_data() {
string line, token;
int i, lineno;
uint32_t opcode, dest, src1, src2, check_xer, check_cr, check_fpscr;
uint64_t dest_64, src1_64, src2_64;
//float sfp_dest, sfp_src1, sfp_src2, sfp_src3;
double dfp_dest, dfp_src1, dfp_src2, dfp_src3;
string rounding_mode;
ifstream tf2stream("ppcfloattests.csv");
if (!tf2stream.is_open()) {
cout << "Could not open tests CSV file. Exiting..." << endl;
return;
}
lineno = 0;
while (getline(tf2stream, line)) {
lineno++;
if (line.empty() || !line.rfind("#", 0))
continue; /* skip empty/comment lines */
istringstream lnstream(line);
vector<string> tokens;
while (getline(lnstream, token, ',')) {
tokens.push_back(token);
}
if (tokens.size() < 5) {
cout << "Too few values in line " << lineno << ". Skipping..." << endl;
continue;
}
opcode = stoul(tokens[1], NULL, 16);
src1 = 0;
src2 = 0;
check_xer = 0;
check_cr = 0;
check_fpscr = 0;
//sfp_dest = 0.0;
//sfp_src1 = 0.0;
//sfp_src2 = 0.0;
//sfp_src3 = 0.0;
dfp_dest = 0.0;
dfp_src1 = 0.0;
dfp_src2 = 0.0;
dfp_src3 = 0.0;
dest_64 = 0;
for (i = 2; i < tokens.size(); i++) {
if (tokens[i].rfind("frD=", 0) == 0) {
dest_64 = stoull(tokens[i].substr(4), NULL, 16);
} else if (tokens[i].rfind("frA=", 0) == 0) {
dfp_src1 = stod(tokens[i].substr(4), NULL);
} else if (tokens[i].rfind("frB=", 0) == 0) {
dfp_src2 = stod(tokens[i].substr(4), NULL);
} else if (tokens[i].rfind("frC=", 0) == 0) {
dfp_src3 = stod(tokens[i].substr(4), NULL);
} else if (tokens[i].rfind("round=", 0) == 0) {
rounding_mode = tokens[i].substr(6, 3);
ppc_state.fpscr &= 0xFFFFFFFC;
if (rounding_mode.compare("RTN") == 0) {
ppc_state.fpscr = 0x0;
} else if (rounding_mode.compare("RTZ") == 0) {
ppc_state.fpscr = 0x1;
} else if (rounding_mode.compare("RPI") == 0) {
ppc_state.fpscr = 0x2;
} else if (rounding_mode.compare("RNI") == 0) {
ppc_state.fpscr = 0x3;
} else if (rounding_mode.compare("VEN") == 0) {
ppc_state.fpscr = FPSCR::VE;
} else {
cout << "ILLEGAL ROUNDING METHOD: " << tokens[i] << " in line " << lineno
<< ". Exiting..." << endl;
exit(0);
}
} else if (tokens[i].rfind("FPSCR=", 0) == 0) {
check_fpscr = stoul(tokens[i].substr(6), NULL, 16);
} else if (tokens[i].rfind("CR=", 0) == 0) {
check_cr = stoul(tokens[i].substr(3), NULL, 16);
} else {
cout << "Unknown parameter " << tokens[i] << " in line " << lineno << ". Exiting..."
<< endl;
exit(0);
}
}
ppc_state.gpr[3] = src1;
ppc_state.gpr[4] = src2;
ppc_state.fpr[3].dbl64_r = dfp_src1;
ppc_state.fpr[4].dbl64_r = dfp_src2;
ppc_state.fpr[5].dbl64_r = dfp_src3;
ppc_state.cr = 0;
ppc_cur_instruction = opcode;
ppc_main_opcode();
ntested++;
if ((tokens[0].rfind("FCMP") && (ppc_state.fpr[3].int64_r != dest_64)) ||
(ppc_state.fpscr != check_fpscr) ||
(ppc_state.cr != check_cr)) {
cout << "Mismatch: instr=" << tokens[0] << ", src1=" << scientific << dfp_src1 << ", src2=" << scientific << dfp_src2 << ", src3=" << scientific << dfp_src3 << endl;
cout << "expected: dest=0x" << hex << dest_64 << ", FPSCR=0x" << hex << check_fpscr
<< ", CR=0x"
<< hex << check_cr << endl;
cout << "got: dest=0x" << hex << ppc_state.fpr[3].int64_r << ", FPSCR=0x" << hex
<< ppc_state.fpscr << ", CR=0x" << hex << ppc_state.cr << endl;
cout << "Test file line #: " << dec << lineno << endl << endl;
nfailed++;
}
}
}
int main() {
initialize_ppc_opcode_tables(); //kludge
@@ -157,6 +282,10 @@ int main() {
read_test_data();
cout << endl << "Testing floating point instructions:" << endl;
read_test_float_data();
cout << "... completed." << endl;
cout << "--> Tested instructions: " << dec << ntested << endl;
cout << "--> Failed: " << dec << nfailed << endl << endl;
+1
View File
@@ -1,4 +1,5 @@
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/"
"${PROJECT_SOURCE_DIR}/thirdparty/capstone/include")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
+15 -10
View File
@@ -23,14 +23,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <loguru.hpp>
#include <map>
#include <sstream>
#include <stdio.h>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
#include "../cpu/ppc/ppcdisasm.h"
#include "../cpu/ppc/ppcemu.h"
#include "../cpu/ppc/ppcmmu.h"
#include "memaccess.h"
#include "utils/profiler.h"
#ifdef ENABLE_68K_DEBUGGER // optionally defined in CMakeLists.txt
@@ -330,7 +331,7 @@ static void disasm(uint32_t count, uint32_t address) {
ctx.simplified = true;
for (int i = 0; i < count; i++) {
ctx.instr_code = mem_read_dbg(ctx.instr_addr, 4);
ctx.instr_code = READ_DWORD_BE_A(mmu_translate_imem(ctx.instr_addr));
cout << uppercase << hex << ctx.instr_addr;
cout << " " << disassemble_single(&ctx) << endl;
}
@@ -541,16 +542,20 @@ void enter_debugger() {
}
} else {
/* disas without arguments defaults to disas 1,pc */
if (context == 2) {
try {
if (context == 2) {
#ifdef ENABLE_68K_DEBUGGER
addr_str = "R24";
addr = get_reg(addr_str);
disasm_68k(1, addr - 2);
addr_str = "R24";
addr = get_reg(addr_str);
disasm_68k(1, addr - 2);
#endif
} else {
addr_str = "PC";
addr = get_reg(addr_str);
disasm(1, addr);
} else {
addr_str = "PC";
addr = get_reg(addr_str);
disasm(1, addr);
}
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
}
}
} else if (cmd == "dump") {
+3 -1
View File
@@ -1,4 +1,6 @@
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/"
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
+1 -1
View File
@@ -37,7 +37,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <thirdparty/SDL2/include/SDL_stdinc.h>
#endif
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
using namespace std;
+277
View File
@@ -0,0 +1,277 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(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/>.
*/
/** Apple memory-mapped I/O controller emulation.
Author: Max Poliakovski
*/
#include "amic.h"
#include "cpu/ppc/ppcmmu.h"
#include "dmacore.h"
#include "machines/machinebase.h"
#include "memctrlbase.h"
#include "viacuda.h"
#include <algorithm>
#include <cinttypes>
#include <loguru.hpp>
#include <memory>
AMIC::AMIC()
{
this->name = "Apple Memory-mapped I/O Controller";
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
/* add memory mapped I/O region for the AMIC control registers */
if (!mem_ctrl->add_mmio_region(0x50F00000, 0x00040000, this)) {
LOG_F(ERROR, "Couldn't register AMIC registers!");
}
this->viacuda = std::unique_ptr<ViaCuda> (new ViaCuda());
this->snd_out_dma = std::unique_ptr<AmicSndOutDma> (new AmicSndOutDma());
this->awacs = std::unique_ptr<AwacDevicePdm> (new AwacDevicePdm());
this->awacs->set_dma_out(this->snd_out_dma.get());
}
bool AMIC::supports_type(HWCompType type) {
if (type == HWCompType::MMIO_DEV) {
return true;
} else {
return false;
}
}
uint32_t AMIC::read(uint32_t reg_start, uint32_t offset, int size)
{
uint32_t phase_val;
if (offset < 0x2000) {
return this->viacuda->read(offset >> 9);
}
switch(offset) {
case AMICReg::Snd_Stat_0:
case AMICReg::Snd_Stat_1:
case AMICReg::Snd_Stat_2:
return (this->awacs->read_stat() >> (offset & 3 * 8)) & 0xFF;
case AMICReg::Snd_Phase0:
case AMICReg::Snd_Phase1:
case AMICReg::Snd_Phase2:
// the sound phase register is organized as follows:
// 000000oo oooooooo oopppppp where 'o' is the 12-bit offset
// into the DMA buffer and 'p' is an undocumented prescale value
// HWInit doesn't care about. Let's hope it will be sufficient
// to return 0 for prescale.
phase_val = this->snd_out_dma->get_cur_buf_pos() << 6;
return (phase_val >> ((2 - (offset & 3)) * 8)) & 0xFF;
case AMICReg::Snd_Out_Ctrl:
return this->snd_out_ctrl;
case AMICReg::Snd_Out_DMA:
return this->snd_out_dma->read_stat();
default:
LOG_F(WARNING, "Unknown AMIC register read, offset=%x", offset);
}
return 0;
}
void AMIC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size)
{
uint32_t mask;
if (offset < 0x2000) {
this->viacuda->write(offset >> 9, value);
return;
}
switch(offset) {
case AMICReg::Snd_Ctrl_0:
case AMICReg::Snd_Ctrl_1:
case AMICReg::Snd_Ctrl_2:
// remember values of sound control registers
this->imm_snd_regs[offset & 3] = value;
// transfer control information to the sound codec when ready
if ((this->imm_snd_regs[0] & 0xC0) == PDM_SND_CTRL_VALID) {
this->awacs->write_ctrl(
(this->imm_snd_regs[1] >> 4) | (this->imm_snd_regs[0] & 0x3F),
((this->imm_snd_regs[1] & 0xF) << 8) | this->imm_snd_regs[2]
);
}
break;
case AMICReg::Snd_Buf_Size_Hi:
case AMICReg::Snd_Buf_Size_Lo:
mask = 0xFF00U >> (8 * (offset & 1));
this->snd_buf_size = (this->snd_buf_size & ~mask) |
((value & 0xFF) << (8 * ((offset & 1) ^1)));
this->snd_buf_size &= ~3; // sound buffer size is always a multiple of 4
LOG_F(9, "AMIC: Sound buffer size set to 0x%X", this->snd_buf_size);
break;
case AMICReg::Snd_Out_Ctrl:
LOG_F(9, "AMIC Sound Out Ctrl updated, val=%x", value);
if ((value & 1) != (this->snd_out_ctrl & 1)) {
if (value & 1) {
LOG_F(9, "AMIC Sound Out DMA enabled!");
this->snd_out_dma->init(this->dma_base & ~0x3FFFF,
this->snd_buf_size);
this->snd_out_dma->enable();
this->awacs->set_sample_rate((this->snd_out_ctrl >> 1) & 3);
this->awacs->start_output_dma();
} else {
LOG_F(9, "AMIC Sound Out DMA disabled!");
this->snd_out_dma->disable();
}
}
this->snd_out_ctrl = value;
break;
case AMICReg::Snd_In_Ctrl:
LOG_F(INFO, "AMIC Sound In Ctrl updated, val=%x", value);
break;
case AMICReg::Snd_Out_DMA:
this->snd_out_dma->write_dma_out_ctrl(value);
break;
case AMICReg::VIA2_Slot_IER:
LOG_F(INFO, "AMIC VIA2 Slot Interrupt Enable Register updated, val=%x", value);
break;
case AMICReg::VIA2_IER:
LOG_F(INFO, "AMIC VIA2 Interrupt Enable Register updated, val=%x", value);
break;
case AMICReg::Video_Mode_Reg:
LOG_F(INFO, "AMIC Video Mode Register set to %x", value);
break;
case AMICReg::Int_Ctrl:
LOG_F(INFO, "AMIC Interrupt Control Register set to %X", value);
break;
case AMICReg::DMA_Base_Addr_0:
case AMICReg::DMA_Base_Addr_1:
case AMICReg::DMA_Base_Addr_2:
case AMICReg::DMA_Base_Addr_3:
mask = 0xFF000000UL >> (8 * (offset & 3));
this->dma_base = (this->dma_base & ~mask) |
((value & 0xFF) << (8 * (3 - (offset & 3))));
LOG_F(9, "AMIC: DMA base address set to 0x%X", this->dma_base);
break;
case AMICReg::Enet_DMA_Xmt_Ctrl:
LOG_F(INFO, "AMIC Ethernet Transmit DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCSI_DMA_Ctrl:
LOG_F(INFO, "AMIC SCSI DMA Ctrl updated, val=%x", value);
break;
case AMICReg::Enet_DMA_Rcv_Ctrl:
LOG_F(INFO, "AMIC Ethernet Receive DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SWIM3_DMA_Ctrl:
LOG_F(INFO, "AMIC SWIM3 DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Xmt_A_Ctrl:
LOG_F(INFO, "AMIC SCC Transmit Ch A DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Rcv_A_Ctrl:
LOG_F(INFO, "AMIC SCC Receive Ch A DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Xmt_B_Ctrl:
LOG_F(INFO, "AMIC SCC Transmit Ch B DMA Ctrl updated, val=%x", value);
break;
case AMICReg::SCC_DMA_Rcv_B_Ctrl:
LOG_F(INFO, "AMIC SCC Receive Ch B DMA Ctrl updated, val=%x", value);
break;
default:
LOG_F(WARNING, "Unknown AMIC register write, offset=%x, val=%x",
offset, value);
}
}
// =========================== DMA related stuff =============================
AmicSndOutDma::AmicSndOutDma()
{
this->dma_out_ctrl = 0;
this->enabled = false;
}
bool AmicSndOutDma::is_active()
{
return true;
}
void AmicSndOutDma::init(uint32_t buf_base, uint32_t buf_samples)
{
this->out_buf0 = buf_base + AMIC_SND_BUF0_OFFS;
this->out_buf1 = buf_base + AMIC_SND_BUF1_OFFS;
this->out_buf_len = buf_samples * 2 * 2;
this->snd_buf_num = 0;
this->cur_buf_pos = 0;
}
uint8_t AmicSndOutDma::read_stat()
{
return this->dma_out_ctrl;
}
void AmicSndOutDma::write_dma_out_ctrl(uint8_t value)
{
// clear interrupt flags
value &= ~PDM_DMA_INTS_MASK;
this->dma_out_ctrl = value;
LOG_F(9, "AMIC: Sound out DMA control set to 0x%X", value);
}
DmaPullResult AmicSndOutDma::pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data)
{
*avail_len = 0;
int rem_len = this->out_buf_len - this->cur_buf_pos;
if (rem_len <= 0) {
if (!this->snd_buf_num) {
// signal buffer 0 drained
this->dma_out_ctrl |= PDM_DMA_IF0;
// TODO: generate IE0 interrupt if enabled
} else {
// signal buffer 1 drained
this->dma_out_ctrl |= PDM_DMA_IF1;
// TODO: generate IE1 interrupt if enabled
}
// check DMA enable flag after buffer 1 was processed
// if it's false stop delivering sound data
// this will effectively stop audio playback
if (this->snd_buf_num && !this->enabled) {
this->cur_buf_pos = 0;
return DmaPullResult::NoMoreData;
}
this->cur_buf_pos = 0; // reset buffer position
this->snd_buf_num ^= 1; // toggle sound buffers
rem_len = this->out_buf_len; // buffer size = full buffer
}
uint32_t len = std::min((uint32_t)rem_len, req_len);
*p_data = mmu_get_dma_mem(
(this->snd_buf_num ? this->out_buf1 : this->out_buf0) + this->cur_buf_pos,
len);
this->cur_buf_pos += len;
*avail_len = len;
return DmaPullResult::MoreData;
}
+145
View File
@@ -0,0 +1,145 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(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/>.
*/
/** @file Apple memory-mapped I/O controller emulation. */
#ifndef AMIC_H
#define AMIC_H
#include "awacs.h"
#include "dmacore.h"
#include "mmiodevice.h"
#include "viacuda.h"
#include <cinttypes>
#include <memory>
/** AMIC sound buffers are located at fixed offsets from DMA base. */
#define AMIC_SND_BUF0_OFFS 0x10000
#define AMIC_SND_BUF1_OFFS 0x12000
// PDM HWInit source defines two constants: kExpBit = 0x80 and kCmdBit = 0x40
// I don't know what they means but it seems that their combination will
// cause sound control parameters to be transferred to the sound chip.
#define PDM_SND_CTRL_VALID 0xC0
#define PDM_DMA_IF1 0x80 // DMA interrupt flag => buffer 1 drained
#define PDM_DMA_IF0 0x40 // DMA interrupt flag => buffer 0 drained
#define PDM_DMA_INTS_MASK 0xF0 // mask for clearing all interrupt flags
/** AMIC-specific sound output DMA implementation. */
class AmicSndOutDma : public DmaOutChannel {
public:
AmicSndOutDma();
~AmicSndOutDma() = default;
bool is_active();
void init(uint32_t buf_base, uint32_t buf_samples);
void enable() { this->enabled = true; };
void disable() { this->enabled = false; };
uint8_t read_stat();
void write_dma_out_ctrl(uint8_t value);
uint32_t get_cur_buf_pos() { return this->cur_buf_pos; };
DmaPullResult pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data);
private:
bool enabled;
uint8_t dma_out_ctrl;
uint32_t out_buf0;
uint32_t out_buf1;
uint32_t out_buf_len;
uint32_t snd_buf_num;
uint32_t cur_buf_pos;
};
/* AMIC registers offsets from AMIC base (0x50F00000). */
enum AMICReg : uint32_t {
// Sound control registers
Snd_Ctrl_0 = 0x14000, // audio codec control register 0
Snd_Ctrl_1 = 0x14001, // audio codec control register 1
Snd_Ctrl_2 = 0x14002, // audio codec control register 2
Snd_Stat_0 = 0x14004, // audio codec status register 0
Snd_Stat_1 = 0x14005, // audio codec status register 1
Snd_Stat_2 = 0x14006, // audio codec status register 2
Snd_Buf_Size_Hi = 0x14008, // sound buffer size, high-order byte
Snd_Buf_Size_Lo = 0x14009, // sound buffer size, low-order byte
Snd_Phase0 = 0x1400C, // high-order byte of the sound phase register
Snd_Phase1 = 0x1400D, // middle byte of the sound phase register
Snd_Phase2 = 0x1400E, // low-order byte of the sound phase register
Snd_Out_Ctrl = 0x14010, // audio codec output control register
Snd_In_Ctrl = 0x14011, // audio codec input control register
Snd_In_DMA = 0x14014, // sound input DMA status/control register
Snd_Out_DMA = 0x14018, // sound output DMA status/control register
// VIA2 registers
VIA2_Slot_IER = 0x26012,
VIA2_IER = 0x26013,
// Video control registers
Video_Mode_Reg = 0x28000,
Int_Ctrl = 0x2A000,
// DMA control registers
DMA_Base_Addr_0 = 0x31000,
DMA_Base_Addr_1 = 0x31001,
DMA_Base_Addr_2 = 0x31002,
DMA_Base_Addr_3 = 0x31003,
Enet_DMA_Xmt_Ctrl = 0x31C20,
SCSI_DMA_Ctrl = 0x32008,
Enet_DMA_Rcv_Ctrl = 0x32028,
SWIM3_DMA_Ctrl = 0x32068,
SCC_DMA_Xmt_A_Ctrl = 0x32088,
SCC_DMA_Rcv_A_Ctrl = 0x32098,
SCC_DMA_Xmt_B_Ctrl = 0x320A8,
SCC_DMA_Rcv_B_Ctrl = 0x320B8,
};
/** Apple Memory-mapped I/O controller device. */
class AMIC : public MMIODevice {
public:
AMIC();
~AMIC() = default;
bool supports_type(HWCompType type);
/* MMIODevice methods */
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
protected:
void dma_reg_write(uint32_t offset, uint32_t value, int size);
private:
uint8_t imm_snd_regs[4]; // temporary storage for sound control registers
uint32_t dma_base = 0; // DMA physical base address
uint16_t snd_buf_size = 0; // sound buffer size in bytes
uint8_t snd_out_ctrl = 0;
std::unique_ptr<ViaCuda> viacuda;
std::unique_ptr<AwacDevicePdm> awacs;
std::unique_ptr<AmicSndOutDma> snd_out_dma;
};
#endif // AMIC_H
+141 -17
View File
@@ -24,9 +24,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "memaccess.h"
#include "pcidevice.h"
#include <atirage.h>
#include <chrono>
#include <cstdint>
#include <map>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
/* Mach64 post dividers. */
static const int mach64_post_div[8] = {
@@ -75,6 +76,12 @@ static const std::map<uint16_t, std::string> mach64_reg_names = {
{0x01FC, "SCALE_3D_CNTL"},
{0x0310, "FIFO_STAT"},
{0x0338, "GUI_STAT"},
{0x04C0, "MPP_CONFIG"},
{0x04C4, "MPP_STROBE_SEQ"},
{0x04C8, "MPP_ADDR"},
{0x04CC, "MPP_DATA"},
{0x0500, "TVO_CNTL"},
{0x0704, "SETUP_CNTL"},
};
@@ -89,6 +96,9 @@ ATIRage::ATIRage(uint16_t dev_id, uint32_t mem_amount) : PCIDevice("ati-rage") {
/* ATI Rage driver needs to know ASIC ID (manufacturer's internal chip code)
to operate properly */
switch (dev_id) {
case ATI_RAGE_GT_DEV_ID:
asic_id = 0x9A; // GT-B2U3 fabricated by UMC
break;
case ATI_RAGE_PRO_DEV_ID:
asic_id = 0x5C; // R3B/D/P-A4 fabricated by UMC
break;
@@ -108,10 +118,14 @@ ATIRage::ATIRage(uint16_t dev_id, uint32_t mem_amount) : PCIDevice("ati-rage") {
/* initialize display identification */
this->disp_id = new DisplayID();
//this->surface = new uint8_t[640 * 480];
}
ATIRage::~ATIRage()
{
//delete (this->surface);
if (this->vram_ptr) {
delete this->vram_ptr;
}
@@ -163,11 +177,6 @@ uint32_t ATIRage::read_reg(uint32_t offset, uint32_t size) {
read_mem(&this->block_io_regs[offset], size));
}
if (offset > sizeof(this->block_io_regs)) {
LOG_F(WARNING, "ATI Rage: register offset 0x%04X out of bounds!", offset);
return 0;
}
res = read_mem(&this->block_io_regs[offset], size);
return res;
@@ -177,21 +186,30 @@ void ATIRage::write_reg(uint32_t offset, uint32_t value, uint32_t size) {
uint32_t gpio_val;
uint16_t gpio_dir;
if (offset > sizeof(this->block_io_regs)) {
LOG_F(WARNING, "ATI Rage: register offset 0x%04X out of bounds!", offset);
return;
}
/* size-dependent endian conversion */
write_mem(&this->block_io_regs[offset], value, size);
switch (offset & ~3) {
case ATI_CRTC_OFF_PITCH:
LOG_F(INFO, "ATI Rage: CRTC_OFF_PITCH=0x%08X", READ_DWORD_LE_A(&this->block_io_regs[ATI_CRTC_OFF_PITCH]));
break;
case ATI_CRTC_GEN_CNTL:
if (this->block_io_regs[ATI_CRTC_GEN_CNTL+3] & 2) {
this->crtc_enable();
} else {
this->crtc_on = false;
}
LOG_F(INFO, "ATI Rage: CRTC_GEN_CNTL:CRTC_ENABLE=%d", !!(this->block_io_regs[ATI_CRTC_GEN_CNTL+3] & 2));
LOG_F(INFO, "ATI Rage: CRTC_GEN_CNTL:CRTC_DISPLAY_DIS=%d", !!(this->block_io_regs[ATI_CRTC_GEN_CNTL] & 0x40));
break;
case ATI_CUR_OFFSET:
LOG_F(INFO, "ATI Rage: CUR_OFFSET=0x%08X", READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_OFFSET]));
break;
case ATI_CUR_HORZ_VERT_POSN:
LOG_F(INFO, "ATI Rage: CUR_HORZ_VERT_POSN=0x%08X", READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_HORZ_VERT_POSN]));
break;
case ATI_CUR_HORZ_VERT_OFF:
LOG_F(INFO, "ATI Rage: CUR_HORZ_VERT_OFF=0x%08X", READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_HORZ_VERT_OFF]));
break;
case ATI_GP_IO:
if (offset < (ATI_GP_IO + 2)) {
@@ -239,6 +257,9 @@ void ATIRage::write_reg(uint32_t offset, uint32_t value, uint32_t size) {
}
}
break;
case ATI_GEN_TEST_CNTL:
LOG_F(INFO, "HW cursor: %s", this->block_io_regs[ATI_GEN_TEST_CNTL] & 0x80 ? "on" : "off");
break;
default:
LOG_F(
INFO,
@@ -247,6 +268,11 @@ void ATIRage::write_reg(uint32_t offset, uint32_t value, uint32_t size) {
offset & ~3,
READ_DWORD_LE_A(&this->block_io_regs[offset & ~3]));
}
if ((this->block_io_regs[ATI_CRTC_GEN_CNTL+3] & 2) &&
!(this->block_io_regs[ATI_CRTC_GEN_CNTL] & 0x40)) {
this->update_screen();
}
}
@@ -373,9 +399,17 @@ uint32_t ATIRage::read(uint32_t reg_start, uint32_t offset, int size)
/* read from little-endian VRAM region */
return read_mem(this->vram_ptr + offset, size);
}
else if (offset >= MEMMAP_OFFSET) {
/* read from memory-mapped registers */
return this->read_reg(offset - MEMMAP_OFFSET, size);
else if (offset >= BE_FB_OFFSET) {
/* read from big-endian VRAM region */
return read_mem_rev(this->vram_ptr + (offset - BE_FB_OFFSET), size);
}
else if (offset >= MM_REGS_0_OFF) {
/* read from memory-mapped registers, block 0 */
return this->read_reg(offset - MM_REGS_0_OFF, size);
}
else if (offset >= MM_REGS_1_OFF) {
/* read from memory-mapped registers, block 1 */
return this->read_reg(offset - MM_REGS_1_OFF + 0x400, size);
}
else {
LOG_F(WARNING, "ATI Rage: read attempt from unmapped aperture region at 0x%08X", offset);
@@ -396,9 +430,18 @@ void ATIRage::write(uint32_t reg_start, uint32_t offset, uint32_t value, int siz
if (offset < this->vram_size) {
/* write to little-endian VRAM region */
write_mem(this->vram_ptr + offset, value, size);
} else if (offset >= MEMMAP_OFFSET) {
/* write to memory-mapped registers */
this->write_reg(offset - MEMMAP_OFFSET, value, size);
}
else if (offset >= BE_FB_OFFSET) {
/* write to big-endian VRAM region */
write_mem_rev(this->vram_ptr + (offset - BE_FB_OFFSET), value, size);
}
else if (offset >= MM_REGS_0_OFF) {
/* write to memory-mapped registers, block 0 */
this->write_reg(offset - MM_REGS_0_OFF, value, size);
}
else if (offset >= MM_REGS_1_OFF) {
/* write to memory-mapped registers, block 1 */
this->write_reg(offset - MM_REGS_1_OFF + 0x400, value, size);
}
else {
LOG_F(WARNING, "ATI Rage: write attempt to unmapped aperture region at 0x%08X", offset);
@@ -497,3 +540,84 @@ void ATIRage::crtc_enable() {
this->crtc_on = true;
}
void ATIRage::draw_hw_cursor(uint8_t *dst_buf, int dst_pitch) {
uint8_t *src_buf, *src_row, *dst_row, px4;
int horz_offset = READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_HORZ_VERT_OFF]) & 0x3F;
int vert_offset = (READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_HORZ_VERT_OFF]) >> 16) & 0x3F;
src_buf = this->vram_ptr + (READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_OFFSET]) * 8);
int cur_height = 64 - vert_offset;
uint32_t color0 = READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_CLR0]) | 0x000000FFUL;
uint32_t color1 = READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_CLR1]) | 0x000000FFUL;
for (int h = 0; h < cur_height; h++) {
dst_row = &dst_buf[h * dst_pitch];
src_row = &src_buf[h * 16];
for (int x = 0; x < 16; x++) {
px4 = src_row[x];
for (int p = 0; p < 4; p++, px4 >>= 2, dst_row += 4) {
switch(px4 & 3) {
case 0: // cursor color 0
WRITE_DWORD_BE_A(dst_row, color0);
break;
case 1: // cursor color 1
WRITE_DWORD_BE_A(dst_row, color1);
break;
case 2: // transparent
break;
case 3: // 1's complement of display pixel
break;
}
}
}
}
}
void ATIRage::update_screen() {
uint8_t *src_buf, *dst_buf, *src_row, *dst_row, pix;
int src_pitch, dst_pitch;
//auto start_time = std::chrono::steady_clock::now();
this->disp_id->get_disp_texture((void **)&dst_buf, &dst_pitch);
uint32_t src_offset = (READ_DWORD_LE_A(&this->block_io_regs[ATI_CRTC_OFF_PITCH]) & 0xFFFF) * 8;
src_pitch = ((READ_DWORD_LE_A(&this->block_io_regs[ATI_CRTC_OFF_PITCH])) >> 19) & 0x1FF8;
src_buf = this->vram_ptr + src_offset;
for (int h = 0; h < this->active_height; h++) {
src_row = &src_buf[h * src_pitch];
dst_row = &dst_buf[h * dst_pitch];
for (int x = 0; x < this->active_width; x++) {
pix = src_row[x];
dst_row[0] = this->palette[pix][2]; // B
dst_row[1] = this->palette[pix][1]; // G
dst_row[2] = this->palette[pix][0]; // R
dst_row[3] = 255; // A
dst_row += 4;
}
}
// HW cursor data is stored at the beginning of the video memory
// HACK: use src_offset to recognize cursor data being ready
// Normally, we should check GEN_CUR_ENABLE bit in the GEN_TEST_CNTL register
if (src_offset > 0x400 && READ_DWORD_LE_A(&this->block_io_regs[ATI_CUR_OFFSET])) {
this->draw_hw_cursor(dst_buf + dst_pitch * 20 + 120, dst_pitch);
}
this->disp_id->update_screen();
//auto end_time = std::chrono::steady_clock::now();
//auto time_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time);
//LOG_F(INFO, "Display uodate took: %lld ns", time_elapsed.count());
SDL_Delay(15);
}
+8 -2
View File
@@ -169,6 +169,7 @@ enum {
ATI_MPP_ADDR = 0x04C8,
ATI_MPP_DATA = 0x04CC,
ATI_TVO_CNTL = 0x0500,
ATI_SETUP_CNTL = 0x0704,
};
/* Mach64 PLL register indices. */
@@ -184,7 +185,9 @@ enum {
};
constexpr auto APERTURE_SIZE = 0x01000000UL; /* Mach64 aperture size */
constexpr auto MEMMAP_OFFSET = 0x007FFC00UL; /* offset to memory mapped registers */
constexpr auto MM_REGS_0_OFF = 0x007FFC00UL; /* offset to memory mapped registers, block 0 */
constexpr auto MM_REGS_1_OFF = 0x007FF800UL; /* offset to memory mapped registers, block 1 */
constexpr auto BE_FB_OFFSET = 0x00800000UL; /* Offset to the big-endian frame buffer */
constexpr auto ATI_XTAL = 14318180.0f; // external crystal oscillator frequency
@@ -221,9 +224,11 @@ protected:
float calc_pll_freq(int scale, int fb_div);
void verbose_pixel_format(int crtc_index);
void crtc_enable();
void draw_hw_cursor(uint8_t *dst_buf, int dst_pitch);
void update_screen();
private:
uint8_t block_io_regs[512] = {0};
uint8_t block_io_regs[2048] = {0};
uint8_t pci_cfg[256] = {0}; /* PCI configuration space */
@@ -246,5 +251,6 @@ private:
uint8_t palette[256][4]; /* internal DAC palette in RGBA format */
int comp_index; /* color component index for DAC palette access */
uint8_t *surface;
};
#endif /* ATI_RAGE_H */
+109 -184
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -21,7 +21,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** AWAC sound device emulation.
Author: Max Poliakovski 2019-20
Currently supported audio codecs:
- PDM AWACs in Nubus Power Macintosh models
- Screamer AWACs in Beige G3
Author: Max Poliakovski 2019-21
*/
#include "awacs.h"
@@ -30,38 +34,107 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "machines/machinebase.h"
#include "soundserver.h"
#include <algorithm>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <memory>
static int awac_freqs[8] = {44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350};
AWACDevice::AWACDevice()
AwacsBase::AwacsBase()
{
this->audio_proc = new AudioProcessor();
/* register audio processor chip with the I2C bus */
I2CBus* i2c_bus = dynamic_cast<I2CBus*>(gMachineObj->get_comp_by_type(HWCompType::I2C_HOST));
i2c_bus->register_device(0x45, this->audio_proc);
// connect to SoundServer
this->snd_server = dynamic_cast<SoundServer *>
(gMachineObj->get_comp_by_name("SoundServer"));
this->out_stream_ready = false;
}
AWACDevice::~AWACDevice()
AwacsBase::~AwacsBase()
{
// disconnect from SoundServer
if (this->out_stream_ready) {
snd_server->close_out_stream();
}
delete this->audio_proc;
}
void AWACDevice::set_dma_out(DMAChannel* dma_out_ch) {
this->dma_out_ch = dma_out_ch;
void AwacsBase::set_sample_rate(int sr_id)
{
if (sr_id > this->max_sr_id) {
LOG_F(ERROR, "AWACs: invalid sample rate ID %d!", sr_id);
} else {
this->cur_sample_rate = this->sr_table[sr_id];
}
};
void AwacsBase::start_output_dma()
{
int err;
if ((err = this->snd_server->open_out_stream(this->cur_sample_rate,
(void *)this->dma_out_ch))) {
LOG_F(ERROR, "AWACs: unable to open sound output stream: %d", err);
this->out_stream_ready = false;
return;
}
this->out_stream_ready = true;
if ((err = snd_server->start_out_stream())) {
LOG_F(ERROR, "AWACs: could not start sound output stream");
}
}
uint32_t AWACDevice::snd_ctrl_read(uint32_t offset, int size) {
void AwacsBase::stop_output_dma()
{
if (this->out_stream_ready) {
snd_server->close_out_stream();
this->out_stream_ready = false;
}
}
//=========================== PDM-style AWACs =================================
AwacDevicePdm::AwacDevicePdm() : AwacsBase()
{
static int pdm_awac_freqs[3] = {22050, 29400, 44100};
// PDM-style AWACs only supports three sample rates
this->sr_table = pdm_awac_freqs;
this->max_sr_id = 2;
}
uint32_t AwacDevicePdm::read_stat()
{
LOG_F(INFO, "AWACs-PDM: status requested!");
// TODO: return valid status including manufacturer & device IDs
return 0;
}
void AwacDevicePdm::write_ctrl(uint32_t addr, uint16_t value)
{
LOG_F(9, "AWACs-PDM: control updated, addr=0x%X, val=0x%X", addr, value);
if (addr <= 4) {
this->ctrl_regs[addr] = value;
} else {
LOG_F(ERROR, "AWACs-PDM: invalid control register address %d!", addr);
}
}
//============================= Screamer AWACs ================================
AwacsScreamer::AwacsScreamer() : AwacsBase()
{
this->audio_proc = std::unique_ptr<AudioProcessor> (new AudioProcessor());
/* register audio processor chip with the I2C bus */
I2CBus* i2c_bus = dynamic_cast<I2CBus*>(gMachineObj->get_comp_by_type(HWCompType::I2C_HOST));
i2c_bus->register_device(0x45, this->audio_proc.get());
static int screamer_freqs[8] = {
44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
};
this->sr_table = screamer_freqs;
this->max_sr_id = 7;
}
uint32_t AwacsScreamer::snd_ctrl_read(uint32_t offset, int size)
{
switch (offset) {
case AWAC_SOUND_CTRL_REG:
return this->snd_ctrl_reg;
@@ -69,191 +142,43 @@ uint32_t AWACDevice::snd_ctrl_read(uint32_t offset, int size) {
return this->is_busy;
case AWAC_CODEC_STATUS_REG:
return (AWAC_AVAILABLE << 8) | (AWAC_MAKER_CRYSTAL << 16) | (AWAC_REV_SCREAMER << 20);
break;
default:
LOG_F(ERROR, "AWAC: unsupported register at offset 0x%X", offset);
LOG_F(ERROR, "Screamer: unsupported register at offset 0x%X", offset);
}
return 0;
}
void AWACDevice::snd_ctrl_write(uint32_t offset, uint32_t value, int size) {
void AwacsScreamer::snd_ctrl_write(uint32_t offset, uint32_t value, int size)
{
int subframe, reg_num;
uint16_t data;
switch (offset) {
case AWAC_SOUND_CTRL_REG:
this->snd_ctrl_reg = BYTESWAP_32(value);
LOG_F(INFO, "New sound control value = 0x%X", this->snd_ctrl_reg);
LOG_F(9, "Screamer: new sound control value = 0x%X", this->snd_ctrl_reg);
break;
case AWAC_CODEC_CTRL_REG:
subframe = (value >> 14) & 3;
reg_num = (value >> 20) & 7;
data = ((value >> 8) & 0xF00) | ((value >> 24) & 0xFF);
LOG_F(INFO, "AWAC subframe = %d, reg = %d, data = %08X\n", subframe, reg_num, data);
LOG_F(9, "Screamer subframe = %d, reg = %d, data = %08X\n",
subframe, reg_num, data);
if (!subframe)
this->control_regs[reg_num] = data;
break;
default:
LOG_F(ERROR, "AWAC: unsupported register at offset 0x%X", offset);
LOG_F(ERROR, "Screamer: unsupported register at offset 0x%X", offset);
}
}
#if 0
static void convert_data(const uint8_t *in, SoundIoChannelArea *out_buf, uint32_t frame_count)
{
uint16_t *p_in = (uint16_t *)in;
for (int i = 0; i < frame_count; i += 2, p_in += 4) {
*(uint16_t *)(out_buf[0].ptr) = BYTESWAP_16(p_in[0]);
out_buf[0].ptr += out_buf[0].step;
*(uint16_t *)(out_buf[0].ptr) = BYTESWAP_16(p_in[1]);
out_buf[0].ptr += out_buf[0].step;
*(uint16_t *)(out_buf[1].ptr) = BYTESWAP_16(p_in[2]);
out_buf[1].ptr += out_buf[1].step;
*(uint16_t *)(out_buf[1].ptr) = BYTESWAP_16(p_in[3]);
out_buf[1].ptr += out_buf[1].step;
}
}
static void insert_silence(SoundIoChannelArea *out_buf, uint32_t frame_count)
{
for (int i = 0; i < frame_count; i += 2) {
*(uint16_t *)(out_buf[0].ptr) = 0;
out_buf[0].ptr += out_buf[0].step;
*(uint16_t *)(out_buf[0].ptr) = 0;
out_buf[0].ptr += out_buf[0].step;
*(uint16_t *)(out_buf[1].ptr) = 0;
out_buf[1].ptr += out_buf[1].step;
*(uint16_t *)(out_buf[1].ptr) = 0;
out_buf[1].ptr += out_buf[1].step;
}
}
#endif
#if 0
static void sound_out_callback(struct SoundIoOutStream *outstream,
int frame_count_min, int frame_count_max)
{
int err, frame_count;
uint8_t *p_in;
uint32_t buf_len, rem_len, got_len;
struct SoundIoChannelArea *areas;
DMAChannel *dma_ch = (DMAChannel *)outstream->userdata; /* C API baby! */
int n_channels = outstream->layout.channel_count;
bool stop = false;
//if (!dma_ch->is_active()) {
// soundio_outstream_clear_buffer(outstream);
// soundio_outstream_pause(outstream, true);
// return;
//}
if (frame_count_max > 512) {
frame_count = 512;
}
else {
frame_count = frame_count_max;
}
buf_len = (frame_count * n_channels) << 1;
//frame_count = frame_count_max;
//LOG_F(INFO, "frame_count_min=%d", frame_count_min);
//LOG_F(INFO, "frame_count_max=%d", frame_count_max);
//LOG_F(INFO, "channel count: %d", n_channels);
//LOG_F(INFO, "buf_len: %d", buf_len);
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
LOG_F(ERROR, "unrecoverable stream error: %s\n", soundio_strerror(err));
return;
}
for (rem_len = buf_len; rem_len > 0; rem_len -= got_len) {
if (!dma_ch->get_data(rem_len, &got_len, &p_in)) {
//LOG_F(INFO, "SndCallback: got_len = %d", got_len);
convert_data(p_in, areas, got_len >> 2);
//LOG_F(9, "Converted sound data, len = %d", got_len);
} else { /* no more data */
//memset(buf, 0, rem_len); /* fill the buffer with silence */
//LOG_F(9, "Inserted silence, len = %d", rem_len);
/* fill the buffer with silence */
//LOG_F(ERROR, "rem_len=%d", rem_len);
insert_silence(areas, rem_len >> 2);
stop = true;
break;
}
}
if ((err = soundio_outstream_end_write(outstream))) {
LOG_F(ERROR, "unrecoverable stream error: %s\n", soundio_strerror(err));
return;
}
if (stop) {
LOG_F(INFO, "pausing result: %s",
soundio_strerror(soundio_outstream_pause(outstream, true)));
}
}
#endif
long AWACDevice::sound_out_callback(cubeb_stream *stream, void *user_data,
void const *input_buffer, void *output_buffer,
long req_frames)
{
uint8_t *p_in;
int16_t* in_buf, * out_buf;
uint32_t got_len;
long frames, out_frames;
AWACDevice *this_ptr = static_cast<AWACDevice*>(user_data); /* C API baby! */
if (!this_ptr->dma_out_ch->is_active()) {
return 0;
}
out_buf = (int16_t*)output_buffer;
out_frames = 0;
while (req_frames > 0) {
if (!this_ptr->dma_out_ch->get_data(req_frames << 2, &got_len, &p_in)) {
frames = got_len >> 2;
in_buf = (int16_t*)p_in;
for (int i = frames; i > 0; i--) {
out_buf[0] = BYTESWAP_16(in_buf[0]);
out_buf[1] = BYTESWAP_16(in_buf[1]);
in_buf += 2;
out_buf += 2;
}
req_frames -= frames;
out_frames += frames;
}
else {
break;
}
}
return out_frames;
}
void status_callback(cubeb_stream *stream, void *user_data, cubeb_state state)
{
LOG_F(9, "Cubeb status callback fired, status = %d", state);
}
void AWACDevice::open_stream(int sample_rate)
void AwacsScreamer::open_stream(int sample_rate)
{
int err;
if ((err = this->snd_server->open_out_stream(sample_rate, sound_out_callback,
status_callback, (void *)this))) {
LOG_F(ERROR, "AWAC: unable to open sound output stream: %d", err);
if ((err = this->snd_server->open_out_stream(sample_rate, (void *)this->dma_out_ch))) {
LOG_F(ERROR, "Screamer: unable to open sound output stream: %d", err);
this->out_stream_ready = false;
} else {
this->out_sample_rate = sample_rate;
@@ -261,17 +186,17 @@ void AWACDevice::open_stream(int sample_rate)
}
}
void AWACDevice::dma_start()
void AwacsScreamer::dma_start()
{
int err;
if (!this->out_stream_ready) {
this->open_stream(awac_freqs[(this->snd_ctrl_reg >> 8) & 7]);
} else if (this->out_sample_rate != awac_freqs[(this->snd_ctrl_reg >> 8) & 7]) {
this->open_stream(this->sr_table[(this->snd_ctrl_reg >> 8) & 7]);
} else if (this->out_sample_rate != this->sr_table[(this->snd_ctrl_reg >> 8) & 7]) {
snd_server->close_out_stream();
this->open_stream(awac_freqs[(this->snd_ctrl_reg >> 8) & 7]);
this->open_stream(this->sr_table[(this->snd_ctrl_reg >> 8) & 7]);
} else {
LOG_F(ERROR, "AWAC: unpausing attempted!");
LOG_F(ERROR, "Screamer: unpausing attempted!");
return;
}
@@ -280,10 +205,10 @@ void AWACDevice::dma_start()
}
if ((err = snd_server->start_out_stream())) {
LOG_F(ERROR, "Could not start sound output stream");
LOG_F(ERROR, "Screamer: could not start sound output stream");
}
}
void AWACDevice::dma_end()
void AwacsScreamer::dma_end()
{
}
+61 -30
View File
@@ -21,29 +21,71 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** AWAC sound devices family definitions.
Audio Waveform Aplifier and Converter (AWAC) is a family of custom audio
Audio Waveform Aplifier and Converters (AWACs) is a family of custom audio
chips used in Power Macintosh.
*/
#ifndef AWAC_H
#define AWAC_H
#include "dmacore.h"
#include "dbdma.h"
#include "i2c.h"
#include "soundserver.h"
#include <cinttypes>
#include <thirdparty/SDL2/include/SDL.h>
#include <memory>
/** AWAC registers offsets. */
/** Base class for the AWACs codecs. */
class AwacsBase {
public:
AwacsBase();
~AwacsBase();
void set_dma_out(DmaOutChannel *dma_out_ch) {
this->dma_out_ch = dma_out_ch;
};
void set_sample_rate(int sr_id);
void start_output_dma();
void stop_output_dma();
protected:
SoundServer *snd_server; // SoundServer instance pointer
DmaOutChannel *dma_out_ch; // DMA output channel instance pointer
int *sr_table; // pointer to the table of supported sample rates
int max_sr_id; // maximum value for sample rate ID
bool out_stream_ready;
int cur_sample_rate;
};
/** AWACs PDM-style sound codec. */
class AwacDevicePdm : public AwacsBase {
public:
AwacDevicePdm();
~AwacDevicePdm() = default;
uint32_t read_stat(void);
void write_ctrl(uint32_t addr, uint16_t value);
void dma_out_start(uint8_t sample_rate_id);
void dma_out_stop();
private:
uint16_t ctrl_regs[5]; // 12-bit wide control registers
};
/** AWACs Screamer registers offsets. */
enum {
AWAC_SOUND_CTRL_REG = 0x00,
AWAC_CODEC_CTRL_REG = 0x10,
AWAC_CODEC_STATUS_REG = 0x20,
};
/** AWAC manufacturer and revision. */
#define AWAC_MAKER_CRYSTAL 1
#define AWAC_REV_SCREAMER 3
/** AWACs Screamer manufacturer and revision. */
#define AWAC_MAKER_CRYSTAL 1
#define AWAC_REV_SCREAMER 3
/** Apple source calls this kValidData but doesn't explain
what it actually means. It seems like it's used to check
@@ -68,7 +110,8 @@ public:
this->sub_addr = sub_addr & 0xF;
this->auto_inc = !!(sub_addr & 0x10);
LOG_F(INFO, "TDA7433 subaddress = 0x%X, auto increment = %d", this->sub_addr, this->auto_inc);
LOG_F(9, "TDA7433 subaddress = 0x%X, auto increment = %d",
this->sub_addr, this->auto_inc);
this->pos++;
return true;
};
@@ -77,7 +120,7 @@ public:
if (!this->pos) {
return send_subaddress(data);
} else if (this->sub_addr <= 6) {
LOG_F(INFO, "TDA7433 byte 0x%X received", data);
LOG_F(9, "TDA7433 byte 0x%X received", data);
this->regs[this->sub_addr] = data;
if (this->auto_inc) {
this->sub_addr++;
@@ -90,7 +133,7 @@ public:
bool receive_byte(uint8_t* p_data) {
*p_data = this->regs[this->sub_addr];
LOG_F(INFO, "TDA7433 byte 0x%X sent", *p_data);
LOG_F(9, "TDA7433 byte 0x%X sent", *p_data);
return true;
};
@@ -101,18 +144,15 @@ private:
int auto_inc;
};
class AWACDevice : public DMACallback {
/** AWACs Screamer sound codec. */
class AwacsScreamer : public AwacsBase {
public:
AWACDevice();
~AWACDevice();
AwacsScreamer();
~AwacsScreamer();
void set_dma_out(DMAChannel* dma_out_ch);
uint32_t snd_ctrl_read(uint32_t offset, int size);
void snd_ctrl_write(uint32_t offset, uint32_t value, int size);
uint32_t snd_ctrl_read(uint32_t offset, int size);
void snd_ctrl_write(uint32_t offset, uint32_t value, int size);
/* DMACallback methods */
void dma_start();
void dma_end();
@@ -120,22 +160,13 @@ protected:
void open_stream(int sample_rate);
private:
static long sound_out_callback(cubeb_stream* stream, void* user_data,
void const* input_buffer, void* output_buffer,
long req_frames);
uint32_t snd_ctrl_reg = {0};
uint16_t control_regs[8] = {0}; /* control registers, each 12-bits wide */
uint32_t snd_ctrl_reg = { 0 };
uint16_t control_regs[8] = { 0 }; /* control registers, each 12-bits wide */
uint8_t is_busy = 0;
AudioProcessor* audio_proc;
SoundServer *snd_server;
bool out_stream_ready;
int out_sample_rate;
DMAChannel *dma_out_ch;
std::unique_ptr<AudioProcessor> audio_proc;
};
#endif /* AWAC_H */
+15 -7
View File
@@ -21,12 +21,19 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file Descriptor-based direct memory access emulation. */
#include "dmacore.h"
#include "dbdma.h"
#include "cpu/ppc/ppcmmu.h"
#include "endianswap.h"
#include <cinttypes>
#include <cstring>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
void DMAChannel::set_callbacks(DbdmaCallback start_cb, DbdmaCallback stop_cb)
{
this->start_cb = start_cb;
this->stop_cb = stop_cb;
}
void DMAChannel::get_next_cmd(uint32_t cmd_addr, DMACmd* p_cmd) {
/* load DMACmd from physical memory */
@@ -184,13 +191,14 @@ void DMAChannel::reg_write(uint32_t offset, uint32_t value, int size) {
}
}
int DMAChannel::get_data(uint32_t req_len, uint32_t *avail_len, uint8_t **p_data)
DmaPullResult DMAChannel::pull_data(uint32_t req_len, uint32_t *avail_len, uint8_t **p_data)
{
*avail_len = 0;
if (this->ch_stat & CH_STAT_DEAD || !(this->ch_stat & CH_STAT_ACTIVE)) {
/* dead or idle channel? -> no more data */
LOG_F(WARNING, "Dead/idle channel -> no more data");
return -1; /* dead or idle channel? -> no more data */
return DmaPullResult::NoMoreData;
}
/* interpret DBDMA program until we get data or become idle */
@@ -212,10 +220,10 @@ int DMAChannel::get_data(uint32_t req_len, uint32_t *avail_len, uint8_t **p_data
*avail_len = this->queue_len;
this->queue_len = 0;
}
return 0; /* tell the caller there is more data */
return DmaPullResult::MoreData; /* tell the caller there is more data */
}
return -1; /* tell the caller there is no more data */
return DmaPullResult::NoMoreData; /* tell the caller there is no more data */
}
bool DMAChannel::is_active()
@@ -239,7 +247,7 @@ void DMAChannel::start()
this->queue_len = 0;
this->dma_cb->dma_start();
this->start_cb();
}
void DMAChannel::resume() {
@@ -257,5 +265,5 @@ void DMAChannel::abort() {
void DMAChannel::pause() {
LOG_F(INFO, "Pausing DMA channel");
this->dma_cb->dma_end();
this->stop_cb();
}
+11 -14
View File
@@ -29,7 +29,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef DB_DMA_H
#define DB_DMA_H
#include "dmacore.h"
#include <cinttypes>
#include <functional>
/** DBDMA Channel registers offsets */
enum DMAReg : uint32_t {
@@ -59,26 +61,19 @@ typedef struct DMACmd {
uint16_t xfer_stat;
} DMACmd;
class DMACallback {
public:
virtual void dma_start(void) = 0;
virtual void dma_end(void) = 0;
//virtual void dma_push(uint8_t *buf, int size) = 0;
//virtual void dma_pull(uint8_t *buf, int size) = 0;
};
typedef std::function<void(void)> DbdmaCallback;
class DMAChannel {
class DMAChannel : public DmaOutChannel {
public:
DMAChannel(DMACallback* cb) {
this->dma_cb = cb;
};
DMAChannel() = default;
~DMAChannel() = default;
void set_callbacks(DbdmaCallback start_cb, DbdmaCallback stop_cb);
uint32_t reg_read(uint32_t offset, int size);
void reg_write(uint32_t offset, uint32_t value, int size);
int get_data(uint32_t req_len, uint32_t *avail_len, uint8_t **p_data);
bool is_active();
bool is_active();
DmaPullResult pull_data(uint32_t req_len, uint32_t *avail_len, uint8_t **p_data);
protected:
void get_next_cmd(uint32_t cmd_addr, DMACmd* p_cmd);
@@ -90,7 +85,9 @@ protected:
void pause(void);
private:
DMACallback* dma_cb = 0;
std::function<void(void)> start_cb; // DMA channel start callback
std::function<void(void)> stop_cb; // DMA channel stop callback
uint16_t ch_stat = 0;
uint32_t cmd_ptr = 0;
+70 -1
View File
@@ -20,7 +20,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "displayid.h"
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include "SDL.h"
DisplayID::DisplayID() {
/* Initialize Apple monitor codes */
@@ -37,8 +38,76 @@ DisplayID::DisplayID() {
/* DDC sense mode is on by default */
this->i2c_on = true;
LOG_F(INFO, "Create display window...");
// Create display window
this->display_wnd = SDL_CreateWindow(
"DingusPPC Display",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);
if (this->display_wnd == NULL) {
LOG_F(ERROR, "Display: SDL_CreateWindow failed with %s\n", SDL_GetError());
}
this->renderer = SDL_CreateRenderer(this->display_wnd, -1, SDL_RENDERER_ACCELERATED);
if (this->renderer == NULL) {
LOG_F(ERROR, "Display: SDL_CreateRenderer failed with %s\n", SDL_GetError());
}
SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255);
SDL_RenderClear(this->renderer);
SDL_RenderPresent(this->renderer);
// Stupidly poll for 10 events.
// Otherwise no window will be shown on mac OS!
SDL_Event e;
for (int i = 0; i < 10; i++) {
SDL_PollEvent(&e);
}
this->disp_texture = SDL_CreateTexture(
this->renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
640, 480
);
if (this->disp_texture == NULL) {
LOG_F(ERROR, "Display: SDL_CreateTexture failed with %s\n", SDL_GetError());
}
}
DisplayID::~DisplayID() {
if (this->disp_texture) {
SDL_DestroyTexture(this->disp_texture);
}
if (this->renderer) {
SDL_DestroyRenderer(this->renderer);
}
if (this->display_wnd) {
SDL_DestroyWindow(this->display_wnd);
}
}
void DisplayID::get_disp_texture(void **pix_buf, int *pitch) {
SDL_LockTexture(this->disp_texture, NULL, pix_buf, pitch);
}
void DisplayID::update_screen() {
SDL_UnlockTexture(this->disp_texture);
//SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
SDL_RenderClear(this->renderer);
SDL_RenderCopy(this->renderer, this->disp_texture, NULL, NULL);
SDL_RenderPresent(this->renderer);
}
uint16_t DisplayID::set_result(uint8_t sda, uint8_t scl) {
this->last_sda = sda;
+8 -1
View File
@@ -33,6 +33,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef DISPLAY_ID_H
#define DISPLAY_ID_H
#include "SDL.h"
#include <cinttypes>
/** I2C bus states. */
@@ -50,15 +51,21 @@ enum I2CState : uint8_t {
class DisplayID {
public:
DisplayID();
~DisplayID() = default;
~DisplayID();
uint16_t read_monitor_sense(uint16_t data, uint16_t dirs);
void get_disp_texture(void **pix_buf, int *pitch);
void update_screen(void);
protected:
uint16_t set_result(uint8_t sda, uint8_t scl);
uint16_t update_ddc_i2c(uint8_t sda, uint8_t scl);
private:
SDL_Window *display_wnd;
SDL_Renderer *renderer;
SDL_Texture *disp_texture;
bool i2c_on;
uint8_t std_sense_code;
+41
View File
@@ -0,0 +1,41 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(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/>.
*/
/** @file Core definitions for direct memory access (DMA) emulation. */
#ifndef DMA_CORE_H
#define DMA_CORE_H
#include <cinttypes>
enum DmaPullResult : int {
MoreData, // data source has more data to be pulled
NoMoreData, // data source has no more data to be pulled
};
class DmaOutChannel {
public:
virtual bool is_active() = 0;
virtual DmaPullResult pull_data(uint32_t req_len, uint32_t *avail_len,
uint8_t **p_data) = 0;
};
#endif // DMA_CORE_H
+22 -5
View File
@@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
Copyright (C) 2018-21 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@@ -25,13 +25,14 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "macio.h"
#include "viacuda.h"
#include <cinttypes>
#include <functional>
#include <iostream>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <cpu/ppc/ppcemu.h>
/** Heathrow Mac I/O device emulation.
Author: Max Poliakovski 2019
Author: Max Poliakovski
*/
using namespace std;
@@ -42,9 +43,16 @@ HeathrowIC::HeathrowIC() : PCIDevice("mac-io/heathrow") {
this->viacuda = new ViaCuda();
gMachineObj->add_subdevice("ViaCuda", this->viacuda);
this->screamer = new AWACDevice();
this->snd_out_dma = new DMAChannel(this->screamer);
// initialize sound chip and its DMA output channel, then wire them together
this->screamer = new AwacsScreamer();
this->snd_out_dma = new DMAChannel();
this->screamer->set_dma_out(this->snd_out_dma);
this->snd_out_dma->set_callbacks(
std::bind(&AwacsScreamer::dma_start, this->screamer),
std::bind(&AwacsScreamer::dma_end, this->screamer)
);
this->mesh = new MESHController(HeathrowMESHID);
}
HeathrowIC::~HeathrowIC() {
@@ -53,6 +61,9 @@ HeathrowIC::~HeathrowIC() {
if (this->viacuda)
delete (this->viacuda);
if (this->mesh)
delete (this->mesh);
}
@@ -122,6 +133,9 @@ uint32_t HeathrowIC::read(uint32_t reg_start, uint32_t offset, int size) {
case 8:
res = dma_read(offset - 0x8000, size);
break;
case 0x10:
res = this->mesh->read((offset >> 4) & 0xF);
break;
case 0x14:
res = this->screamer->snd_ctrl_read(offset - 0x14000, size);
break;
@@ -152,6 +166,9 @@ void HeathrowIC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int
case 8:
dma_write(offset - 0x8000, value, size);
break;
case 0x10:
this->mesh->write((offset >> 4) & 0xF, value);
break;
case 0x14:
this->screamer->snd_ctrl_write(offset - 0x14000, value, size);
break;
+70
View File
@@ -0,0 +1,70 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(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/>.
*/
/** Highspeed Memory Controller emulation.
Author: Max Poliakovski
*/
#include "hmc.h"
HMC::HMC() : MemCtrlBase()
{
this->name = "Highspeed Memory Controller";
/* add memory mapped I/O region for the HMC control register */
add_mmio_region(0x50F40000, 0x10000, this);
this->config_reg = 0ULL;
this->bit_pos = 0;
}
bool HMC::supports_type(HWCompType type) {
if (type == HWCompType::MEM_CTRL || type == HWCompType::MMIO_DEV) {
return true;
} else {
return false;
}
}
uint32_t HMC::read(uint32_t reg_start, uint32_t offset, int size)
{
if (!offset)
return !!(this->config_reg & (1ULL << this->bit_pos++));
else
return 0; /* FIXME: what should be returned for invalid offsets? */
}
void HMC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size)
{
uint64_t bit;
switch(offset) {
case 0:
bit = 1ULL << this->bit_pos++;
this->config_reg = (value & 1) ? this->config_reg | bit :
this->config_reg & ~bit;
break;
case 8: /* writing to HMCBase + 8 resets internal bit position */
this->bit_pos = 0;
break;
}
}
+55
View File
@@ -0,0 +1,55 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(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/>.
*/
/** Highspeed Memory Controller emulation.
Author: Max Poliakovski
Highspeed Memory Controller (HMC) is a custom memory
and L2 cache controller designed especially for the
first generation of the PowerMacintosh computer.
*/
#ifndef HMC_H
#define HMC_H
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "mmiodevice.h"
#include <cinttypes>
class HMC : public MemCtrlBase, public MMIODevice {
public:
HMC();
~HMC() = default;
bool supports_type(HWCompType type);
/* MMIODevice methods */
uint32_t read(uint32_t reg_start, uint32_t offset, int size);
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size);
private:
int bit_pos;
uint64_t config_reg;
};
#endif // HMC_H
+1 -1
View File
@@ -30,7 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdexcept>
#include <cstring>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
/** Base class for I2C devices */
class I2CDevice {
+35 -1
View File
@@ -35,6 +35,39 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
Register location and value meaning are board-dependent.
*/
/**
Machine ID register for Nubus Power Macs.
It's located at physical address 0x5FFFFFFC and contains four bytes:
+0 uint16_t signature = 0xA55A
+1 uint8_t machine_type (3 - PowerMac)
+2 uint8_t model (0x10 = PDM, 0x12 = Carl Sagan, 0x13 = Cold Fusion)
*/
class NubusMacID : public MMIODevice {
public:
NubusMacID(const uint16_t id) {
this->name = "Nubus-Machine-id";
this->id[0] = 0xA5;
this->id[1] = 0x5A;
this->id[2] = (id >> 8) & 0xFF;
this->id[3] = id & 0xFF;
};
~NubusMacID() = default;
bool supports_type(HWCompType type) {
return type == HWCompType::MMIO_DEV;
};
uint32_t read(uint32_t reg_start, uint32_t offset, int size) {
return (offset < 4 ? this->id[offset] : 0);
};
/* not writable */
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) {};
private:
uint8_t id[4];
};
/**
The machine ID for the Gossamer board is accesible at 0xFF000004 (phys).
It contains a 16-bit value revealing machine's capabilities like bus speed,
@@ -57,7 +90,8 @@ public:
return ((!offset && size == 2) ? this->id : 0);
};
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size){}; /* not writable */
/* not writable */
void write(uint32_t reg_start, uint32_t offset, uint32_t value, int size) {};
private:
uint16_t id;
+5 -3
View File
@@ -55,6 +55,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "dbdma.h"
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "mesh.h"
#include "mmiodevice.h"
#include "nvram.h"
#include "pcidevice.h"
@@ -146,9 +147,10 @@ private:
uint32_t aux_ctrl = 0; // aux features control register
/* device cells */
ViaCuda* viacuda; /* VIA cell with Cuda MCU attached to it */
NVram* nvram; /* NVRAM cell */
AWACDevice* screamer; /* Screamer audio codec instance */
ViaCuda* viacuda; // VIA cell with Cuda MCU attached to it
NVram* nvram; // NVRAM cell
AwacsScreamer *screamer; // Screamer audio codec instance
MESHController *mesh; // MESH SCSI cell instance
DMAChannel* snd_out_dma;
};
+2 -2
View File
@@ -99,7 +99,7 @@ bool MemCtrlBase::add_mem_mirror(uint32_t start_addr, uint32_t dest_addr) {
entry = new AddressMapEntry;
entry->start = start_addr;
entry->end = start_addr + (ref_entry->end - ref_entry->start) + 1;
entry->end = start_addr + (ref_entry->end - ref_entry->start);
entry->mirror = dest_addr;
entry->type = ref_entry->type | RT_MIRROR;
entry->devobj = 0;
@@ -130,7 +130,7 @@ bool MemCtrlBase::add_mmio_region(uint32_t start_addr, uint32_t size, MMIODevice
AddressMapEntry *entry;
/* error if another region for the given range already exists */
if (find_range(start_addr) || find_range(start_addr + size))
if (find_range(start_addr) || find_range(start_addr + size - 1))
return false;
entry = new AddressMapEntry;
+68
View File
@@ -0,0 +1,68 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(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/>.
*/
/** @file MESH (Macintosh Enhanced SCSI Hardware) controller emulation. */
#include "mesh.h"
#include <cinttypes>
#include <loguru.hpp>
uint8_t MESHController::read(uint8_t reg_offset)
{
switch(reg_offset) {
case MeshReg::BusStatus0:
LOG_F(9, "MESH: read from BusStatus0 register");
return 0;
case MeshReg::MeshID:
LOG_F(INFO, "MESH: read from MeshID register");
return this->chip_id; // tell them who we are
default:
LOG_F(WARNING, "MESH: read from unimplemented register at offset %d", reg_offset);
}
return 0;
}
void MESHController::write(uint8_t reg_offset, uint8_t value)
{
switch(reg_offset) {
case MeshReg::Sequence:
LOG_F(INFO, "MESH: write 0x%02X to Sequence register", value);
break;
case MeshReg::BusStatus1:
LOG_F(INFO, "MESH: write 0x%02X to BusStatus1 register", value);
break;
case MeshReg::IntMask:
LOG_F(INFO, "MESH: write 0x%02X to IntMask register", value);
break;
case MeshReg::Interrupt:
LOG_F(INFO, "MESH: write 0x%02X to Interrupt register", value);
break;
case MeshReg::SourceID:
LOG_F(INFO, "MESH: write 0x%02X to SourceID register", value);
break;
case MeshReg::SyncParms:
LOG_F(INFO, "MESH: write 0x%02X to SyncParms register", value);
break;
default:
LOG_F(WARNING, "MESH: write to unimplemented register at offset %d", reg_offset);
}
}
+65
View File
@@ -0,0 +1,65 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-21 divingkatae and maximum
(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/>.
*/
/** @file MESH (Macintosh Enhanced SCSI Hardware) controller definitions. */
#ifndef MESH_H
#define MESH_H
#include <cinttypes>
// Chip ID returned by the MESH cell inside the Heathrow ASIC
#define HeathrowMESHID 4
// MESH registers offsets
enum MeshReg : uint8_t {
XferCount0 = 0,
XferCount1 = 1,
FIFO = 2,
Sequence = 3,
BusStatus0 = 4,
BusStatus1 = 5,
FIFOCount = 6,
Exception = 7,
Error = 8,
IntMask = 9,
Interrupt = 0xA,
SourceID = 0xB,
DestID = 0xC,
SyncParms = 0xD,
MeshID = 0xE,
SelTimeOut = 0xF
};
class MESHController {
public:
MESHController(uint8_t mesh_id) { this->chip_id = mesh_id; };
~MESHController() = default;
// MESH registers access
uint8_t read(uint8_t reg_offset);
void write(uint8_t reg_offset, uint8_t value);
private:
uint8_t chip_id; // Chip ID for the MESH controller
};
#endif // MESH_H
+1 -1
View File
@@ -27,7 +27,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cinttypes>
#include <cstring>
#include <iostream>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include "hwcomponent.h"
#include "memctrlbase.h"
+1 -1
View File
@@ -24,7 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstring>
#include <fstream>
#include <iostream>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
/** @file Non-volatile RAM implementation.
*/
+51 -5
View File
@@ -19,9 +19,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dmacore.h"
#include "endianswap.h"
#include "soundserver.h"
//#include <thirdparty/libsoundio/soundio/soundio.h>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <cubeb/cubeb.h>
#ifdef _WIN32
#include <objbase.h>
@@ -124,9 +125,54 @@ void SoundServer::shutdown()
LOG_F(INFO, "Sound Server shut down.");
}
long sound_out_callback(cubeb_stream *stream, void *user_data,
void const *input_buffer, void *output_buffer,
long req_frames)
{
uint8_t *p_in;
int16_t* in_buf, * out_buf;
uint32_t got_len;
long frames, out_frames;
DmaOutChannel *dma_ch = static_cast<DmaOutChannel*>(user_data); /* C API baby! */
int SoundServer::open_out_stream(uint32_t sample_rate, cubeb_data_callback data_cb,
cubeb_state_callback status_cb, void *user_data)
if (!dma_ch->is_active()) {
return 0;
}
out_buf = (int16_t*)output_buffer;
out_frames = 0;
while (req_frames > 0) {
if (!dma_ch->pull_data(req_frames << 2, &got_len, &p_in)) {
frames = got_len >> 2;
in_buf = (int16_t*)p_in;
for (int i = frames; i > 0; i--) {
out_buf[0] = BYTESWAP_16(in_buf[0]);
out_buf[1] = BYTESWAP_16(in_buf[1]);
in_buf += 2;
out_buf += 2;
}
req_frames -= frames;
out_frames += frames;
}
else {
break;
}
}
return out_frames;
}
static void status_callback(cubeb_stream *stream, void *user_data, cubeb_state state)
{
LOG_F(9, "Cubeb status callback fired, status = %d", state);
}
int SoundServer::open_out_stream(uint32_t sample_rate, void *user_data)
{
int res;
uint32_t latency_frames;
@@ -148,7 +194,7 @@ int SoundServer::open_out_stream(uint32_t sample_rate, cubeb_data_callback data_
res = cubeb_stream_init(this->cubeb_ctx, &this->out_stream, "SndOut stream",
NULL, NULL, NULL, &params, latency_frames,
data_cb, status_cb, user_data);
sound_out_callback, status_callback, user_data);
if (res != CUBEB_OK) {
LOG_F(ERROR, "Could not open sound output stream, error: %d", res);
return -1;
+3 -17
View File
@@ -35,7 +35,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#define SOUND_SERVER_H
#include "hwcomponent.h"
//#include <thirdparty/libsoundio/soundio/soundio.h>
#include <cubeb/cubeb.h>
enum {
@@ -52,32 +51,19 @@ public:
int start();
void shutdown();
int open_out_stream(uint32_t sample_rate, cubeb_data_callback data_cb,
cubeb_state_callback status_cb, void *user_data);
int open_out_stream(uint32_t sample_rate, void *user_data);
int start_out_stream();
void close_out_stream();
bool supports_type(HWCompType type) { return type == HWCompType::SND_SERVER; };
#if 0
SoundIoDevice *get_out_device() {
if (this->status == SND_SERVER_UP) {
return this->out_device;
} else {
return NULL;
}
bool supports_type(HWCompType type) {
return type == HWCompType::SND_SERVER;
};
#endif
private:
int status; /* server status */
cubeb *cubeb_ctx;
cubeb_stream *out_stream;
//SoundIo *soundio;
//int out_dev_index; /* current output device index */
//SoundIoDevice *out_device; /* current output device instance */
//SoundIoOutStream *outstream;
};
#endif /* SOUND_SERVER_H */
+1 -1
View File
@@ -52,7 +52,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cinttypes>
#include <stdexcept>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
enum RAMType : int { SDRAM = 4 };
+2 -2
View File
@@ -27,7 +27,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "viacuda.h"
#include "adb.h"
#include <cinttypes>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
using namespace std;
@@ -96,7 +96,7 @@ void ViaCuda::write(int reg, uint8_t value) {
break;
case VIA_A:
case VIA_ANH:
LOG_F(WARNING, "Attempted read from VIA Port A! \n");
LOG_F(WARNING, "Attempted write to VIA Port A! \n");
break;
case VIA_DIRB:
LOG_F(9, "VIA_DIRB = %x \n", (uint32_t)value);
+2 -1
View File
@@ -1,4 +1,5 @@
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
+2 -2
View File
@@ -24,7 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <ctime>
#include <iostream>
#include <interpreter_loop.h>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <cpu/ppc/ppcemu.h>
std::chrono::high_resolution_clock::time_point global; // global timer
@@ -108,4 +108,4 @@ void interpreter_main_loop() {
interpreter_update_counters();
}
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
set(CMAKE_CXX_STANDARD 11)
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/loguru/")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
+1 -1
View File
@@ -3,7 +3,7 @@
#include <map>
#include <memory>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
std::unique_ptr<MachineBase> gMachineObj = 0;
+13 -4
View File
@@ -36,8 +36,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iomanip>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
using namespace std;
@@ -76,6 +77,13 @@ static const PropMap GossamerSettings = {
new IntProperty( 2, vector<uint32_t>({2, 4, 6}))},
};
static const PropMap PDMSettings = {
{"rambank1_size",
new IntProperty(0, vector<uint32_t>({0, 8, 16, 32, 64, 128}))},
{"rambank2_size",
new IntProperty(0, vector<uint32_t>({0, 8, 16, 32, 64, 128}))},
};
static const map<string, string> PropHelp = {
{"rambank1_size", "specifies RAM bank 1 size in MB"},
{"rambank2_size", "specifies RAM bank 2 size in MB"},
@@ -83,8 +91,9 @@ static const map<string, string> PropHelp = {
{"gfxmem_size", "specifies video memory size in MB"},
};
static const map<string, tuple<PropMap, function<int(void)>, string>> machines = {
{"pmg3", {GossamerSettings, create_gossamer, "Power Macintosh G3 (Beige)"}},
static const map<string, tuple<PropMap, function<int(string&)>, string>> machines = {
{"pm6100", {PDMSettings, create_pdm, "PowerMacintosh 6100"}},
{"pmg3", {GossamerSettings, create_gossamer, "Power Macintosh G3 (Beige)"}},
};
string machine_name_from_rom(string& rom_filepath) {
@@ -267,7 +276,7 @@ int create_machine_for_id(string& id, string& rom_filepath) {
auto machine = machines.at(id);
/* build machine and load boot ROM */
if (get<1>(machine)() < 0 || load_boot_rom(rom_filepath) < 0) {
if (get<1>(machine)(id) < 0 || load_boot_rom(rom_filepath) < 0) {
return -1;
}
} catch(out_of_range ex) {
+2 -1
View File
@@ -42,6 +42,7 @@ void list_machines(void);
void list_properties(void);
/* Machine-specific factory functions. */
int create_gossamer(void);
int create_gossamer(string& id);
int create_pdm(string& id);
#endif /* MACHINE_FACTORY_H */
+3 -2
View File
@@ -34,7 +34,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "devices/viacuda.h"
#include "machinebase.h"
#include "machineproperties.h"
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <string>
static void setup_ram_slot(std::string name, int i2c_addr, int capacity_megs) {
@@ -51,7 +52,7 @@ static void setup_ram_slot(std::string name, int i2c_addr, int capacity_megs) {
}
int create_gossamer() {
int create_gossamer(std::string& id) {
if (gMachineObj) {
LOG_F(ERROR, "Global machine object not empty!");
return -1;
+91
View File
@@ -0,0 +1,91 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-20 divingkatae and maximum
(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/>.
*/
/** @file Construct a PDM-style PowerMacintosh machine.
Author: Max Poliakovski
*/
#include "cpu/ppc/ppcemu.h"
#include "devices/amic.h"
#include "devices/hmc.h"
#include "devices/machineid.h"
#include "devices/soundserver.h"
#include "machinebase.h"
#include "machineproperties.h"
#include <loguru.hpp>
#include <string>
int create_pdm(std::string& id) {
if (gMachineObj) {
LOG_F(ERROR, "PDM Factory: global machine object not empty!");
return -1;
}
LOG_F(INFO, "Initializing the %s hardware...", id.c_str());
/* initialize the global machine object */
gMachineObj.reset(new MachineBase("PDM"));
/* register HMC memory controller */
gMachineObj->add_component("HMC", new HMC);
/* start the sound server. */
gMachineObj->add_component("SoundServer", new SoundServer());
/* register AMIC I/O controller */
gMachineObj->add_component("AMIC", new AMIC);
/* get raw pointer to HMC object */
HMC* hmc_obj = dynamic_cast<HMC*>(gMachineObj->get_comp_by_name("HMC"));
/* allocate machine ID register and tell we're running PowerMac 6100 */
// TODO: add a possibility to select another machine
// to be used with the same ROM
gMachineObj->add_component("MachineID", new NubusMacID(0x3010));
hmc_obj->add_mmio_region(0x5FFFFFFC, 4,
dynamic_cast<MMIODevice*>(gMachineObj->get_comp_by_name("MachineID")));
/* allocate ROM region */
if (!hmc_obj->add_rom_region(0x40000000, 0x400000)) {
LOG_F(ERROR, "Could not allocate ROM region!\n");
return -1;
}
/* mirror ROM to 0xFFC00000 for a PowerPC CPU to start */
if (!hmc_obj->add_mem_mirror(0xFFC00000, 0x40000000)) {
LOG_F(ERROR, "Could not create ROM mirror!\n");
return -1;
}
/* add 8MB of soldered on-board RAM */
if (!hmc_obj->add_ram_region(0x00000000, 0x800000)) {
LOG_F(ERROR, "Could not allocate built-in RAM region!\n");
return -1;
}
/* Init virtual CPU and request MPC601 */
ppc_cpu_init(hmc_obj, PPC_VER::MPC601);
LOG_F(INFO, "Initialization completed.\n");
return 0;
}
+2 -2
View File
@@ -1,11 +1,11 @@
#include "endianswap.h"
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
#include <algorithm>
#include <cinttypes>
#include <string>
#include <map>
#include <memory>
#include <limits>
#include <limits>
#include <sstream>
#include <utility>
#include <vector>
+12 -10
View File
@@ -35,9 +35,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <thirdparty/CLI11/CLI11.hpp>
#include <thirdparty/SDL2/include/SDL.h>
#include <thirdparty/loguru/loguru.hpp>
#include <CLI11.hpp>
#include <SDL.h>
#include <loguru.hpp>
using namespace std;
@@ -47,6 +47,7 @@ void sigint_handler(int signum) {
LOG_F(INFO, "Shutting down...");
delete gMachineObj.release();
SDL_Quit();
exit(0);
}
@@ -54,6 +55,7 @@ void sigabrt_handler(int signum) {
LOG_F(INFO, "Shutting down...");
delete gMachineObj.release();
SDL_Quit();
}
static string appDescription = string(
@@ -170,6 +172,11 @@ int main(int argc, char** argv) {
cout << "BootROM path: " << bootrom_path << endl;
cout << "Execution mode: " << execution_mode << endl;
if (SDL_Init(SDL_INIT_VIDEO)) {
LOG_F(ERROR, "SDL_Init error: %s", SDL_GetError());
return 0;
}
// initialize global profiler object
gProfilerObj.reset(new Profiler());
@@ -190,13 +197,6 @@ int main(int argc, char** argv) {
// redirect SIGABRT to our own handler
signal(SIGABRT, sigabrt_handler);
#ifdef SDL
if (SDL_Init(SDL_INIT_AUDIO)){
LOG_F(ERROR, "SDL_Init error: %s", SDL_GetError());
return 0;
}
#endif
switch (execution_mode) {
case 0:
interpreter_main_loop();
@@ -214,5 +214,7 @@ bail:
delete gMachineObj.release();
SDL_Quit();
return 0;
}
+1 -1
View File
@@ -7,7 +7,7 @@
#include "endianswap.h"
#include <cinttypes>
#include <thirdparty/loguru/loguru.hpp>
#include <loguru.hpp>
/* read an aligned big-endian WORD (16bit) */
#define READ_WORD_BE_A(addr) (BYTESWAP_16(*((uint16_t*)((addr)))))
+272 -19
View File
@@ -1,22 +1,275 @@
The Heathrow is an I/O subsystem and a DMA controller.
# Heathrow ASIC
It contains a feature control register, an auxiliary control register, and some registers to save states for the DBDMA and the VIA Cuda.
The Heathrow ASIC is an intergrated I/O controller designed for use in Power
Macintosh G3 computers.
It also contains the emulations for the VIA Cuda, SWIM 3 floppy drive, ESCC, and MESH components.
Its predecessors are Grand Central and O'Hare ASICs used in other Power Macintosh
computers. As those names suggest, Apple engineers liked to name their I/O controllers
after airports and train stations.
# Register Map
| Register Name | Offset |
|:-------------------:|:------:|
| Interrupt Events 2 | 0x10 |
| Interrupt Mask 2 | 0x14 |
| Interrupt Clear 2 | 0x18 |
| Interrupt Levels 2 | 0x1C |
| Interrupt Events 1 | 0x20 |
| Interrupt Mask 1 | 0x24 |
| Interrupt Clear 1 | 0x28 |
| Interrupt Levels 1 | 0x2C |
| Chassis Light Color | 0x32 |
| Media Bay Control | 0x34 |
| Feature Control | 0x38 |
| Auxiliary Control | 0x3C |
Heathrow and its siblings are collectively referred to as __mac-io__ devices in the
OpenFirmware device tree.
## Mac I/O family
The purpose of a Mac I/O (MIO) controller is to bring support for Apple legacy
I/O hardware to the PCI-based Power Macintosh. That legacy hardware has
existed long before Power Macintosh was introduced. It includes:
- versatile interface adapter (VIA)
- Sander-Woz integrated machine (SWIM) that is a floppy disk controller
- CUDA MCU that controls ADB, parameter RAM, realtime clock and power management
- serial communication controller (SCC)
- Macintosh Enhanced SCSI Hardware (MESH)
In the 68k Macintosh era, all this hardware was implemented in separate ICs.
In a PCI-compatible Power Macintosh, the above devices are part
of the MIO chip itself. MIO's functional blocks implementing the above devices
are called __cells__, i.e. "VIA cell", "SWIM cell" etc.
MIO itself is PCI compliant while the legacy hardware it emulates isn't.
MIO occupies 512Kb of the PCI memory space divided into registers space and
DMA space. Access to emulated legacy devices is accomplished by reading from/
writing to MIO's PCI address space at predefined offsets.
MIO includes a DMA controller that offers 15 DMA channels implementing
Apple's own DMA protocol called descriptor-based DMA (DBDMA).
Official documentation (that is somewhat incomplete and erroneous) can be
found in the second chapter of the book "Macintosh Technology in the Common
Hardware Reference Platform" by Apple Computer, Inc.
## Heathrow I/O modules
Being a Mac I/O compatible device, the Heathrow ASIC contains the following modules/cells:
* VIA cell for establishing communication with the Cuda MCU (separate IC)
* ATA/IDE cell for controlling ATA-3 compatible drives
* enhanced serial communication controller (ESCC cell)
* Ethernet controller cell funnily called __bmac__
* floppy disk controller (SWIM3 cell)
* SCSI controller (MESH cell)
* integrated DBDMA controller
* interrupt controller
* logic for controlling built-in audio HW
* logic for accessing NVRAM (separate IC)
* logic for controlling a media bay on portables
## PCI configuration space registers
| Register name | Default value |
|:-------------:|:--------------:|
| VendorID | 0x106B (Apple) |
| DeviceID | 0x0010 |
| RevisionID | 0x01 |
| Class code | 0xFF0000 |
It looks like Heathrow supports only one Base Address Register - __BAR0__.
It contains base address of the memory-mapped registers programmed by system software
during system initialization.
## Memory-mapped registers
Software communicates with the Heathrow ASIC via memory-mapped registers that
occupy 512 Kb starting from the base address located in the BAR0 register
in the PCI configuration space.
Macintosh firmware configures the Heathrow ASIC to live at address `0xF3000000`.
### General chip control registers
| Offset | Register name | Description |
|:------:|:-----------------:|:----------------------------------------------:|
| 0x10 | InterruptEvents2 | each "1" bit indicates a pending int |
| 0x14 | InterruptMask2 | enabling (1) / disabling(0) of specific ints |
| 0x18 | InterruptClear2 | bit value "1" clears the corresponding int |
| 0x1C | InterruptLevels2 | interrupt status for devices 32...63 |
| 0x20 | InterruptEvents1 | each "1" bit indicates a pending int |
| 0x24 | InterruptMask1 | enabling (1) / disabling(0) of specific ints |
| 0x28 | InterruptClear1 | bit value "1" clears the corresponding int |
| 0x2C | InterruptLevels1 | interrupt status for devices 0...31 |
| 0x30 | UnknownReg30 | Not much is known about this register |
| 0x34 | HeathrowIDs | bits for identifying media bay features? |
| 0x38 | FeatureControl | bits for controlling Heathrow operation |
| 0x3C | AuxControl | auxilary control bits |
### Device DMA spaces
| Offset | Size in bytes | Space name |
|:------:|:-------------:|:-----------------------------:|
| 0x8000 | 256 | MESH SCSI DMA |
| 0x8100 | 256 | Floppy controller DMA |
| 0x8200 | 256 | Ethernet transmit DMA |
| 0x8300 | 256 | Ethernet receive DMA |
| 0x8400 | 256 | Serial channel A transmit DMA |
| 0x8500 | 256 | Serial channel A receive DMA |
| 0x8600 | 256 | Serial channel B transmit DMA |
| 0x8700 | 256 | Serial channel B receive DMA |
| 0x8800 | 256 | Audio output DMA |
| 0x8900 | 256 | Audio input DMA   |
| 0x8A00 | 256 | unassigned DMA ? |
| 0x8B00 | 256 | internal ATA (IDE0) DMA |
| 0x8C00 | 256 | media bay (?) ATA (IDE1) DMA |
| 0x8D00 | 256 | unassigned DMA ? |
| 0x8E00 | 256 | unassigned DMA ? |
| 0x8F00 | 256 | unassigned DMA ? |
### Device register spaces
| Offset | Size | Space name |
|:-------:|:-------------:|:----------------------------------------:|
| 0x10000 | 4 KB | MESH SCSI controller registers |
| 0x11000 | 4 KB | Ethernet (bmac) controller registers |
| 0x12000 | 4 KB | Legacy serial (SCC) controller registers |
| 0x13000 | 4 KB | Serial (ESCC) controller registers |
| 0x14000 | 4 KB | Audio codec registers |
| 0x15000 | 4 KB | Floppy (SWIM3) controller registers |
| 0x16000 | 8 KB | VIA registers |
| 0x20000 | 4 KB | IDE0 registers |
| 0x21000 | 4 KB | IDE1 registers |
| 0x60000 | 128 KB | NVRAM access |
## Registers description
### General chip control
#### Feature control register
Bit names in the table below were pulled from OpenFirmware v2.4.
The field "Description" represents my personal attempt to describe the function
of those bits based on publicly available Apple and Linux sources.
| Bit # | Active | Name | Description |
|:-----:|:------:|:-------------:|:---------------------------------------:|
| 0 | high | in_use_led | "1" enables monitor sense on G3 desktop |
| 1 | low | -mb_pwr | media bay power on/off |
| 2 | high | pci_mb_en | enable media bay PCI (?) |
| 3 | high | ide_mb_en | enable media bay ATA (IDE1) |
| 4 | high | floppy_en | enable floppy disk controller cell |
| 5 | high | ide_int_en | enable internal ATA (IDE0) |
| 6 | low | -ide0_reset | reset internal ATA |
| 7 | low | -mb_reset | reset media bay |
| 8 | high | iobus_enable | *not available* |
| 9 | high | scc_enable | enable SCC controller cell |
| 10 | high | scsi_cell_en | enable MESH SCSI controller cell |
| 11 | high | swim_cell_en | enable SWIM3 floppy controller cell |
| 12 | high | snd_pwr | *not available* |
| 13 | high | snd_clk_en | enable sound chip clock (?) |
| 14 | high | scc_a_enable | enable SCC channel A |
| 15 | high | scc_b_enable | enable SCC channel B |
| 16 | low | -port_via | switch VIA cell to portable mode (?) |
| 16 | high | desktop_via | switch VIA cell to desktop mode (?) |
| 17 | low | -pwm | *not available* |
| 17 | high | mon_id | *not available* |
| 18 | low | -hookpb | *not available* |
| 18 | high | mb_cnt | *not available* |
| 19 | low | -swimiii | floppy controller in SWIM3 mode (?) |
| 19 | high | clonefloppy | floppy controller in PC-compat mode (?) |
| 20 | high | aud22run | *not available* |
| 21 | high | scsi_linkmode | *not available* |
| 22 | high | arb_bypass | disable internal PCI arbiter (?) |
| 23 | low | -ide1_reset | reset media bay ATA |
| 24 | high | slow_scc_pclk | *not available* |
| 25 | high | reset_scc | reset serial (SCC) controller cell |
| 26 | high | mfdc_cell_en | enable PC-compat floppy controller (?) |
| 27 | high | use_mfdc | use PC floppy ctrl instead of SWIM3 ? |
| 29 | high | enet_ctrl_en | enable Ethernet controller cell (?) |
| 30 | high | enet_xcvr_en | enable Ethernet Transceiver (?) |
| 31 | high | enet_reset | reset Ethernet cell |
### Cell-specific registers
#### ATA cell registers
##### Internal ATA (IDE0)
| Offset from I/O base | Width in bits | Function on read | Function on write |
|:--------------------:|:-------------:|:-----------------:|:-----------------:|
| 0x20000 | 16 | Data | Data |
| 0x20010 | 8 | Error | Features |
| 0x20020 | 8 | Sector Count | Sector Count |
| 0x20030 | 8 | Sector Number | Sector Number |
| 0x20040 | 8 | Cylinder Low | Cylinder Low |
| 0x20050 | 8 | Cylinder High | Cylinder High |
| 0x20060 | 8 | Drive/Head | Drive/Head |
| 0x20070 | 8 | Status | Command |
| 0x20160 | 8 | Alternate Status | Device Control |
| 0x20200 | 32 | Timing Config | Timing Config |
##### Media Bay ATA (IDE1)
| Offset from I/O base | Width in bits | Function on read | Function on write |
|:--------------------:|:-------------:|:-----------------:|:-----------------:|
| 0x21000 | 16 | Data | Data |
| 0x21010 | 8 | Error | Features |
| 0x21020 | 8 | Sector Count | Sector Count |
| 0x21030 | 8 | Sector Number | Sector Number |
| 0x21040 | 8 | Cylinder Low | Cylinder Low |
| 0x21050 | 8 | Cylinder High | Cylinder High |
| 0x21060 | 8 | Drive/Head | Drive/Head |
| 0x21070 | 8 | Status | Command |
| 0x21160 | 8 | Alternate Status | Device Control |
| 0x21200 | 32 | Timing Config | Timing Config |
##### Timing Config Register
The timing config register is specific to Apple's ATA implementation. According
with
[this source](https://android.googlesource.com/kernel/msm/+/android-wear-5.0.2_r0.1/drivers/ata/pata_macio.c),
this register is used to control timings for Multiword DMA (MDMA) and Programmed I/O (PIO) modes.
This register has the following format:
| 31 ... 22 | 21 | 20 ... 16 | 15 ... 11 | 10 | 9 ... 5 | 4 ... 0 |
|------------|----|---------------|-------------|----|--------------|------------|
| Unused (?) | HT | MDMA Recovery | MDMA Access | E | PIO Recovery | PIO Access |
I don't know what Recovery and Access values mean. It seems that all values are
in 30 ns units. It corresponds to one PCI cycle when running at a 33 MHz clock speed.
Both PIO and MDMA recovery values are specified in `x / 30` units after subtracting
a constant offset. This offset is 120 ns for PIO transfers and 30 ns for MDMA transfers.
- The "HT" stands for _HalfTick_. When set, 15 ns will be added to both MDMA access
and recovery times. Otherwise, 30 ns are added to the MDMA recovery time.
- The "E" bit, when set, adds 30 ns to the final PIO timing.
The [official Apple source](https://opensource.apple.com/source/HeathrowATA/HeathrowATA-108.3.1/HeathrowATA.cpp.auto.html)
specifies the following default values for each PIO mode:
| Mode # | Cycle time | Transfer rate | Recovery | Access |
|:------:|:----------:|:-------------:|:--------:|:------:|
| 0 | 600 ns | 3,33 MB/s | 420 ns | 180 ns |
| 1 | 390 ns | 5,13 MB/s | 240 ns | 150 ns |
| 2 | 360 ns | 5,56 MB/s | 180 ns | 180 ns |
| 3 | 330 ns | 6,1 MB/s | 180 ns | 150 ns |
| 4 | 300 ns | 6,67 MB/s | 150 ns | 150 ns |
Timing values for the PIO modes 2...4 deviate from the corresponding values given
in the ATA-3 specification.
PIO cycle time can be calculated using the following equation:
```
PIO_Cycle_ns = PIO_Recovery * 30 + 120 + PIO_Access * 30 + E * 30
```
Multiword DMA modes are specified as follows:
| Mode # | Cycle time | Transfer rate | Recovery | Access |
|:------:|:----------:|:-------------:|:--------:|:------:|
| 0 | 480 ns | 4,17 MB/s | 240 ns | 240 ns |
| 1 | 360 ns | 5,56 MB/s | 180 ns | 180 ns |
| 2 | 270 ns | 7,4 MB/s | 135 ns | 135 ns |
| 3 | 240 ns | 8,33 MB/s | 120 ns | 120 ns |
| 4 | 210 ns | 9,52 MB/s | 105 ns | 105 ns |
| 5 | 180 ns | 11,11 MB/s | 90 ns | 90 ns |
| 6 | 150 ns | 13,33 MB/s | 75 ns | 75 ns |
| 7 | 120 ns | 16,67 MB/s | 45 ns | 75 ns |
Modes 0, 6 and 7 from the table above correspond to the MDMA modes 0...2 specified
in the ATA-2 standard.
MDMA cycle time can be calculated using the following equation:
```
MDMA_Cycle_ns = MDMA_Recovery * 30 + MDMA_Access * 30 + 30
```
+144 -5
View File
@@ -1,9 +1,148 @@
OpenFirmware is a standard to defined a firmware's interfaces. It primarily uses the Forth programming language. It's available on most PCI Power Macs and all New World Power Macs by holding Command, Option, O, and F together during bootup.
# OpenFirmware in Power Macintosh
The bootpath for Old World Macs is usually set to "/AAPL,ROM". New World Macs specify "\\:tbxi" as the boot path instead.
*compiled from various sources by Max Poliakovski.*
When using an Old World PCI Power Mac, it gets relocated from 0xFF800000 to 0x00400000 during boot-up, then instruction and data translations are enabled.
[Open Firmware](https://en.wikipedia.org/wiki/Open_Firmware) is a platform-independent
boot firmware architecture covered by an IEEE standard.
There is also a section of NVRAM reserved for boot-up code called "NVRAMRC", ran each time the system is booted up.
All PowerMacintosh computers run OpenFirmware except the very first generation
that uses [Nubus](https://en.wikipedia.org/wiki/NuBus) instead of
[PCI](https://en.wikipedia.org/wiki/Peripheral_Component_Interconnect).
It gets compiled to native PowerPC code during boot-up.
OpenFirmware is used to perform hardware identification and initialization during
the booting process after power-on. It also provides a platform-independent
description of the attached devices available for operating systems.
In this respect, OpenFirmware can be compared with [BIOS](https://en.wikipedia.org/wiki/BIOS),
widely used in the PC world.
Being based upon the [Forth programming language](https://en.wikipedia.org/wiki/Forth_(programming_language)),
OpenFirmware offers an operating system, an interactive environment as well as a
programming language in one package. Its shell can be used as well by users for
controlling the boot enviroment as by developers for developing and debugging device
drivers.
This document focuses on various aspects of Apple's OpenFirmware implementation
as found in various PowerMacintosh models.
## OpenFirmware Versions
### Old World Macs
| ROM dump/Machine | BootROM codename | OF version | OF image offset |
|:------------------------------------:|:----------------:|:-------------:|:---------------:|
| Power Macintosh 7300/7600/8600/9600 | TNT | 1.0.5 | 0x330000 |
| Bandai Pippin | Pip | 1.0.5 | 0x330000 |
| Power Macintosh,Performa 6400 | Alchemy | 2.0 | 0x330000 |
| Power Macintosh G3 desktop | Gossamer | 2.0f1 | 0x320000 |
| PowerBook G3 Wallstreet v2 | GRX | 2.0.1 | 0x330000 |
| Power Macintosh 4400/7220 | Zanzibar | 2.0.2 | 0x330000 |
| Power Macintosh 6500 | Gazelle | 2.0.3 | 0x330000 |
| Power Macintosh G3 v3 | Gossamer | 2.4 | 0x320000 |
### NewWorld Macs
*TBD*
## OpenFirmware image
### Old World Macs
OpenFirmware in OldWorld Macs is stored in the monolithic 4MB ROM. Its hibernated
image is located at offset `0x320000` or `0x330000` from beginning of the ROM.
That corresponds to the physical address `0xFFF20000` or `0xFFF30000`, respectively.
The size of the OpenFirmware image varies from 98KB (v1.0.5) to 172KB (v2.4).
Apple's OpenFirmware image has the following structure:
| Section type | Architecture | Relative Size (v1.0.5) | Relative Size (v2.4) |
|:------------------:|:------------:|:----------------------:|:--------------------:|
| OF kernel | PowerPC | 26% | 18% |
| OF main code | FCode | 62% | 51% |
| OF device packages | FCode | 12% | 31% |
OF image is wrapped in the COFF container. At the start of the image, a COFF file
header and a section header are located. The following example parses the COFF
headers of the OF image from the Gossamer ROM v3:
```
org 0xFFF20000
; OF COFF file header
dc.w 0x1DF ; COFF magic
dc.w 1 ; number of sections
dc.l 'Gary' ; Gary Davidian's signature replaces time and date
dc.l 0 ; ptr to the symbol table
dc.l 0 ; number of entries in the symbol table
dc.w 0 ; size of the optional header
dc.w 0 ; flags
; OF COFF section header
dc.b '.text', 0, 0, 0 ; section name
dc.l 0 ; physical address
dc.l 0 ; virtual address
dc.l 0x2ADB0 ; section size in bytes (= 171KB)
dc.l 0x3C ; file offset to section data
dc.l 0 ; file offset to section relocations
dc.l 0 ; file offset to line number entries
dc.w 0 ; number of relocation entries
dc.w 0 ; number of line number entries
dc.l 0x20 ; section flags (this section contains executable code)
```
Right after the COFF section header, a vital OF kernel data structure called `StartVec`
is located. It contains among others several important offsets into the OF image
for finding all required OF parts. That's also the location HWInit passes control
to when invoking OF:
```
org 0xFFF2003C
; OF execution starts here
mflr r11 ; save return address to HWInit in R11
bl OF_kernel_start ; pass control to OF kernel
; LR will contain physical address of StartVec
; Begin of StartVec structure
; All offsets are from the beginning of the .text section (0xFFF2003C)
FFF20044 dc.l 0x7918 ; offset to the FCode stream of the OF main package
...
FFF2007C dc.l 0x28C48 ; offset to the last driver in the device packages
...
FFF20084 dc.l 0x7720 ; offset to the last Forth word descriptor of the kernel
```
### OpenFirmware internals
Apple's OpenFirmware contains a small kernel implemented in the native PowerPC code. This kernel performs the following actions:
* set up memory translation for OF execution
* relocate itself from ROM to RAM
* initialize Forth runtime environment
* recompile OF main image from FCode to native PPC code
* pass control to recompiled OF that starts building the device tree
* process low-level exceptions
## OpenFirmware and the Macintosh boot process
### Old World Macs
1. In response to power coming on, HWInit code in the Power Macintosh ROM performs initialization of the memory controller and the basic I/O facilities as well as some self-testing. After that, the startup chime is played.
2. HWInit passes control to OpenFirmware kernel that prepares OF execution from RAM. OF builds the **device tree** - a platform-independent description of the attached HW.
3. OF returns control to HWInit that initializes several low-level data structures required by the Nanokernel.
4. HWInit passes control to the Nanokernel that initializes the low-level virtual machine and the 68k emulator.
5. 68k emulator executes the start-up code in the Macintosh ROM that initializes various managers.
6. The device tree generated by the OpenFirmware in step 2 is imported by the Expansion Bus Manager initialization and stored in the **NameRegistry**.
7. An operating system is located and loaded.
### New World Macs
*TBD*
## OpenFirmware bugs
Apple OF is known to contain numerous bugs. The following table lists some recently discrovered bugs, not mentioned elsewhere.
| OF version affected | Bug description |
|:-------------------:|-----------------|
| 2.0f1, 2.4 | A numerical overflow in `um/mod` used by `get-usecs-60x` causes the OF console to become unresponsive after approx. 71 minutes. You have to restart your computer once the bug is triggered. |