mirror of
https://github.com/N64Recomp/N64ModernRuntime.git
synced 2026-02-03 20:26:10 +00:00
226 lines
8.5 KiB
C++
226 lines
8.5 KiB
C++
#include <memory>
|
|
#include <fstream>
|
|
#include <array>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <mutex>
|
|
#include "recomp.h"
|
|
#include "librecomp/addresses.hpp"
|
|
#include "librecomp/game.hpp"
|
|
#include <ultramodern/files.hpp>
|
|
#include <ultramodern/ultra64.h>
|
|
#include <ultramodern/ultramodern.hpp>
|
|
|
|
static std::vector<uint8_t> rom;
|
|
static std::vector<uint8_t> save_buffer;
|
|
|
|
bool recomp::is_rom_loaded() {
|
|
return !rom.empty();
|
|
}
|
|
|
|
void recomp::set_rom_contents(std::vector<uint8_t>&& new_rom) {
|
|
rom = std::move(new_rom);
|
|
}
|
|
|
|
std::span<const uint8_t> recomp::get_rom() {
|
|
return rom;
|
|
}
|
|
|
|
constexpr uint32_t k1_to_phys(uint32_t addr) {
|
|
return addr & 0x1FFFFFFF;
|
|
}
|
|
|
|
constexpr uint32_t phys_to_k1(uint32_t addr) {
|
|
return addr | 0xA0000000;
|
|
}
|
|
|
|
extern "C" void __osPiGetAccess_recomp(uint8_t* rdram, recomp_context* ctx) {
|
|
}
|
|
|
|
extern "C" void __osPiRelAccess_recomp(uint8_t* rdram, recomp_context* ctx) {
|
|
}
|
|
|
|
extern "C" void osCartRomInit_recomp(uint8_t* rdram, recomp_context* ctx) {
|
|
OSPiHandle* handle = TO_PTR(OSPiHandle, recomp::cart_handle);
|
|
handle->type = 0; // cart
|
|
handle->baseAddress = phys_to_k1(recomp::rom_base);
|
|
handle->domain = 0;
|
|
|
|
ctx->r2 = (gpr)recomp::cart_handle;
|
|
}
|
|
|
|
extern "C" void osDriveRomInit_recomp(uint8_t * rdram, recomp_context * ctx) {
|
|
OSPiHandle* handle = TO_PTR(OSPiHandle, recomp::drive_handle);
|
|
handle->type = 1; // bulk
|
|
handle->baseAddress = phys_to_k1(recomp::drive_base);
|
|
handle->domain = 0;
|
|
|
|
ctx->r2 = (gpr)recomp::drive_handle;
|
|
}
|
|
|
|
extern "C" void osCreatePiManager_recomp(uint8_t* rdram, recomp_context* ctx) {
|
|
;
|
|
}
|
|
|
|
void recomp::do_rom_read(uint8_t* rdram, gpr ram_address, uint32_t physical_addr, size_t num_bytes) {
|
|
// TODO use word copies when possible
|
|
|
|
// TODO handle misaligned DMA
|
|
assert((physical_addr & 0x1) == 0 && "Only PI DMA from aligned ROM addresses is currently supported");
|
|
assert((ram_address & 0x7) == 0 && "Only PI DMA to aligned RDRAM addresses is currently supported");
|
|
uint8_t* rom_addr = rom.data() + physical_addr - recomp::rom_base;
|
|
for (size_t i = 0; i < num_bytes; i++) {
|
|
MEM_B(i, ram_address) = *rom_addr;
|
|
rom_addr++;
|
|
}
|
|
}
|
|
|
|
void recomp::do_rom_pio(uint8_t* rdram, gpr ram_address, uint32_t physical_addr) {
|
|
assert((physical_addr & 0x3) == 0 && "PIO not 4-byte aligned in device, currently unsupported");
|
|
assert((ram_address & 0x3) == 0 && "PIO not 4-byte aligned in RDRAM, currently unsupported");
|
|
uint8_t* rom_addr = rom.data() + physical_addr - recomp::rom_base;
|
|
MEM_B(0, ram_address) = *rom_addr++;
|
|
MEM_B(1, ram_address) = *rom_addr++;
|
|
MEM_B(2, ram_address) = *rom_addr++;
|
|
MEM_B(3, ram_address) = *rom_addr++;
|
|
}
|
|
|
|
void do_dma(RDRAM_ARG PTR(OSMesgQueue) mq, gpr rdram_address, uint32_t physical_addr, uint32_t size, uint32_t direction) {
|
|
// TODO asynchronous transfer
|
|
// TODO implement unaligned DMA correctly
|
|
if (direction == 0) {
|
|
if (physical_addr >= recomp::rom_base) {
|
|
// read cart rom
|
|
recomp::do_rom_read(rdram, rdram_address, physical_addr, size);
|
|
|
|
// Send a message to the mq to indicate that the transfer completed
|
|
ultramodern::enqueue_external_message_src(mq, 0, false, ultramodern::EventMessageSource::Pi);
|
|
} else if (physical_addr >= recomp::sram_base) {
|
|
if (!ultramodern::sram_allowed()) {
|
|
ultramodern::error_handling::message_box("Attempted to use SRAM saving with other save type");
|
|
ULTRAMODERN_QUICK_EXIT();
|
|
}
|
|
// read sram
|
|
save_buffer.resize(size);
|
|
ultramodern::save_read_ptr(save_buffer.data(), physical_addr - recomp::sram_base, size);
|
|
for (uint32_t i = 0; i < size; i++) {
|
|
MEM_B(i, rdram_address) = save_buffer[i];
|
|
}
|
|
|
|
// Send a message to the mq to indicate that the transfer completed
|
|
ultramodern::enqueue_external_message_src(mq, 0, false, ultramodern::EventMessageSource::Pi);
|
|
} else {
|
|
fprintf(stderr, "[WARN] PI DMA read from unknown region, phys address 0x%08X\n", physical_addr);
|
|
}
|
|
} else {
|
|
if (physical_addr >= recomp::rom_base) {
|
|
// write cart rom
|
|
throw std::runtime_error("ROM DMA write unimplemented");
|
|
} else if (physical_addr >= recomp::sram_base) {
|
|
if (!ultramodern::sram_allowed()) {
|
|
ultramodern::error_handling::message_box("Attempted to use SRAM saving with other save type");
|
|
ULTRAMODERN_QUICK_EXIT();
|
|
}
|
|
// write sram
|
|
save_buffer.resize(size);
|
|
for (uint32_t i = 0; i < size; i++) {
|
|
save_buffer[i] = MEM_B(i, rdram_address);
|
|
}
|
|
ultramodern::save_write_ptr(save_buffer.data(), physical_addr - recomp::sram_base, size);
|
|
|
|
// Send a message to the mq to indicate that the transfer completed
|
|
ultramodern::enqueue_external_message_src(mq, 0, false, ultramodern::EventMessageSource::Pi);
|
|
} else {
|
|
fprintf(stderr, "[WARN] PI DMA write to unknown region, phys address 0x%08X\n", physical_addr);
|
|
}
|
|
}
|
|
}
|
|
|
|
extern "C" void osPiStartDma_recomp(RDRAM_ARG recomp_context* ctx) {
|
|
uint32_t mb = ctx->r4;
|
|
uint32_t pri = ctx->r5;
|
|
uint32_t direction = ctx->r6;
|
|
uint32_t devAddr = ctx->r7 | recomp::rom_base;
|
|
gpr dramAddr = MEM_W(0x10, ctx->r29);
|
|
uint32_t size = MEM_W(0x14, ctx->r29);
|
|
PTR(OSMesgQueue) mq = MEM_W(0x18, ctx->r29);
|
|
uint32_t physical_addr = k1_to_phys(devAddr);
|
|
|
|
debug_printf("[pi] DMA from 0x%08X into 0x%08X of size 0x%08X\n", devAddr, dramAddr, size);
|
|
|
|
do_dma(PASS_RDRAM mq, dramAddr, physical_addr, size, direction);
|
|
|
|
ctx->r2 = 0;
|
|
}
|
|
|
|
extern "C" void osEPiStartDma_recomp(RDRAM_ARG recomp_context* ctx) {
|
|
OSPiHandle* handle = TO_PTR(OSPiHandle, ctx->r4);
|
|
OSIoMesg* mb = TO_PTR(OSIoMesg, ctx->r5);
|
|
uint32_t direction = ctx->r6;
|
|
uint32_t devAddr = handle->baseAddress | mb->devAddr;
|
|
gpr dramAddr = mb->dramAddr;
|
|
uint32_t size = mb->size;
|
|
PTR(OSMesgQueue) mq = mb->hdr.retQueue;
|
|
uint32_t physical_addr = k1_to_phys(devAddr);
|
|
|
|
debug_printf("[pi] DMA from 0x%08X into 0x%08X of size 0x%08X\n", devAddr, dramAddr, size);
|
|
|
|
do_dma(PASS_RDRAM mq, dramAddr, physical_addr, size, direction);
|
|
|
|
ctx->r2 = 0;
|
|
}
|
|
|
|
extern "C" void osEPiReadIo_recomp(RDRAM_ARG recomp_context * ctx) {
|
|
OSPiHandle* handle = TO_PTR(OSPiHandle, ctx->r4);
|
|
uint32_t devAddr = handle->baseAddress | ctx->r5;
|
|
gpr dramAddr = ctx->r6;
|
|
uint32_t physical_addr = k1_to_phys(devAddr);
|
|
|
|
if (physical_addr > recomp::rom_base) {
|
|
// cart rom
|
|
recomp::do_rom_pio(PASS_RDRAM dramAddr, physical_addr);
|
|
} else {
|
|
// sram
|
|
assert(false && "SRAM ReadIo unimplemented");
|
|
}
|
|
|
|
ctx->r2 = 0;
|
|
}
|
|
|
|
extern "C" void osPiGetStatus_recomp(RDRAM_ARG recomp_context * ctx) {
|
|
ctx->r2 = 0;
|
|
}
|
|
|
|
extern "C" void osPiRawStartDma_recomp(RDRAM_ARG recomp_context * ctx) {
|
|
ultramodern::error_handling::message_box(
|
|
"Stub `osPiRawStartDma_recomp` function called!\n"
|
|
"Most games do not call this function directly, which means the libultra function\n"
|
|
"that uses this function was not properly named.\n"
|
|
"\n"
|
|
"If you triggered this message, please make sure you have properly identified\n"
|
|
"every libultra function on your recompiled game. If you are sure every libultra\n"
|
|
"function has been identified and you still get this problem then open an issue on\n"
|
|
"the N64ModernRuntime Github repository mentioning the game you are trying to\n"
|
|
"recompile and steps to reproduce the issue.\n"
|
|
"\n"
|
|
"The application will close now, bye and good luck!"
|
|
);
|
|
ULTRAMODERN_QUICK_EXIT();
|
|
}
|
|
|
|
extern "C" void osEPiRawStartDma_recomp(RDRAM_ARG recomp_context * ctx) {
|
|
ultramodern::error_handling::message_box(
|
|
"Stub `osEPiRawStartDma_recomp` function called!\n"
|
|
"Most games do not call this function directly, which means the libultra function\n"
|
|
"that uses this function was not properly named.\n"
|
|
"\n"
|
|
"If you triggered this message, please make sure you have properly identified\n"
|
|
"every libultra function on your recompiled game. If you are sure every libultra\n"
|
|
"function has been identified and you still get this problem then open an issue on\n"
|
|
"the N64ModernRuntime Github repository mentioning the game you are trying to\n"
|
|
"recompile and steps to reproduce the issue.\n"
|
|
"\n"
|
|
"The application will close now, bye and good luck!"
|
|
);
|
|
ULTRAMODERN_QUICK_EXIT();
|
|
}
|