Made recompiled output run the game's entrypoint, various reorganization, clang warning fixes

This commit is contained in:
Mr-Wiseguy 2022-11-25 15:31:16 -05:00
parent 6e45fac005
commit 0af9d489b3
13 changed files with 3921 additions and 100 deletions

4
disable_warnings.h Normal file
View file

@ -0,0 +1,4 @@
#ifdef __clang__
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wimplicit-function-declaration"
#endif

View file

@ -2,6 +2,7 @@
#include <cstdlib>
#include <unordered_set>
#include <span>
#include <filesystem>
#include "rabbitizer.hpp"
#include "elfio/elfio.hpp"
@ -243,6 +244,7 @@ std::unordered_set<std::string> renamed_funcs{
"memcpy",
"memset",
"strchr",
"bzero",
};
// Functions that weren't declared properly and thus have no size in the elf
@ -256,8 +258,8 @@ std::unordered_map<std::string, size_t> unsized_funcs{
};
int main(int argc, char** argv) {
if (argc != 2) {
fmt::print("Usage: {} [input elf file]\n", argv[0]);
if (argc != 3) {
fmt::print("Usage: {} [input elf file] [entrypoint RAM address]\n", argv[0]);
std::exit(EXIT_SUCCESS);
}
@ -271,10 +273,18 @@ int main(int argc, char** argv) {
std::exit(EXIT_FAILURE);
};
if (!elf_file.load(argv[1])) {
std::string elf_name{ argv[1] };
if (!elf_file.load(elf_name)) {
exit_failure("Failed to load provided elf file\n");
}
char* end;
const uint32_t entrypoint = (uint32_t)strtoul(argv[2], &end, 0);
if (argv[2] == end) {
exit_failure("Invalid entrypoint value: " + std::string(argv[2]) + "\n");
}
if (elf_file.get_class() != ELFIO::ELFCLASS32) {
exit_failure("Incorrect elf class\n");
}
@ -320,6 +330,8 @@ int main(int argc, char** argv) {
fmt::print("Num symbols: {}\n", symbols.get_symbols_num());
bool found_entrypoint_func = false;
for (int sym_index = 0; sym_index < symbols.get_symbols_num(); sym_index++) {
std::string name;
ELFIO::Elf64_Addr value;
@ -336,12 +348,18 @@ int main(int argc, char** argv) {
// Check if this symbol is unsized and if so populate its size from the unsized_funcs map
if (size == 0) {
if (value == entrypoint && type == ELFIO::STT_FUNC) {
found_entrypoint_func = true;
size = 0x50; // dummy size for entrypoints, should cover them all
name = "recomp_entrypoint";
} else {
auto size_find = unsized_funcs.find(name);
if (size_find != unsized_funcs.end()) {
size = size_find->second;
type = ELFIO::STT_FUNC;
}
}
}
if (ignored_funcs.contains(name)) {
name = name + "_recomp";
@ -383,6 +401,10 @@ int main(int argc, char** argv) {
}
}
if (!found_entrypoint_func) {
exit_failure("Could not find entrypoint function\n");
}
fmt::print("Function count: {}\n", context.functions.size());
std::ofstream func_lookup_file{ "test/funcs/lookup.cpp" };
@ -421,9 +443,17 @@ int main(int argc, char** argv) {
}
}
}
fmt::print(func_lookup_file,
"}};\n"
"extern const size_t num_funcs = sizeof(funcs) / sizeof(funcs[0]);\n"
"\n"
"gpr get_entrypoint_address() {{ return (gpr)(int32_t)0x{:08X}u; }}\n"
"\n"
"const char* get_rom_name() {{ return \"{}\"; }}\n"
"\n",
entrypoint,
std::filesystem::path{ elf_name }.replace_extension(".z64").string()
);
fmt::print(func_header_file,

View file

@ -771,6 +771,7 @@ bool RecompPort::recompile_function(const RecompPort::Context& context, const Re
std::ofstream output_file{ output_path.data() };
fmt::print(output_file,
"#include \"recomp.h\"\n"
"#include \"disable_warnings.h\"\n"
"\n"
"void {}(uint8_t* restrict rdram, recomp_context* restrict ctx) {{\n"
// these variables shouldn't need to be preserved across function boundaries, so make them local for more efficient output

View file

@ -28,12 +28,12 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>ClangCL</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>ClangCL</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
@ -69,6 +69,12 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<CopyCppRuntimeToOutputDir>true</CopyCppRuntimeToOutputDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<CopyCppRuntimeToOutputDir>true</CopyCppRuntimeToOutputDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@ -105,11 +111,15 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<AdditionalIncludeDirectories>$(ProjectDir)..;$(ProjectDir)thirdparty;$(ProjectDir)Lib\SDL2-2.24.0\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalOptions>
</AdditionalOptions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>$(ProjectDir)RT64\$(Configuration)\RT64.lib;$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
</Link>
<PostBuildEvent>
<Command>XCOPY "$(ProjectDir)RT64\$(Configuration)\*.dll" "$(TargetDir)" /S /Y
@ -119,11 +129,15 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>$(ProjectDir)..;$(ProjectDir)thirdparty;$(ProjectDir)Lib\SDL2-2.24.0\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalOptions>
</AdditionalOptions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>$(ProjectDir)RT64\$(Configuration)\RT64.lib;$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
</Link>
<PostBuildEvent>
<Command>XCOPY "$(ProjectDir)RT64\$(Configuration)\*.dll" "$(TargetDir)" /S /Y
@ -147,7 +161,7 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S
<ClCompile Include="src\cont.cpp" />
<ClCompile Include="src\dp.cpp" />
<ClCompile Include="src\eep.cpp" />
<ClCompile Include="src\misc_ultra.cpp" />
<ClCompile Include="portultra\misc_ultra.cpp" />
<ClCompile Include="src\pi.cpp" />
<ClCompile Include="src\portultra_translation.cpp" />
<ClCompile Include="src\recomp.cpp" />
@ -155,6 +169,7 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S
<ClCompile Include="src\vi.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\disable_warnings.h" />
<ClInclude Include="..\recomp.h" />
<ClInclude Include="portultra\multilibultra.hpp" />
<ClInclude Include="portultra\platform_specific.h" />

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,8 @@
#include <cinttypes>
#include <variant>
#include <unordered_map>
#include <utility>
#include <mutex>
#include <Windows.h>
#include "SDL.h"
@ -18,7 +20,7 @@ struct SpTaskAction {
};
struct SwapBuffersAction {
int32_t origin;
uint32_t origin;
};
using Action = std::variant<SpTaskAction, SwapBuffersAction>;
@ -262,7 +264,7 @@ void gfx_thread_func(uint8_t* rdram, uint8_t* rom) {
}
extern "C" void osViSwapBuffer(RDRAM_ARG PTR(void) frameBufPtr) {
events_context.action_queue.enqueue(SwapBuffersAction{ frameBufPtr + 640 });
events_context.action_queue.enqueue(SwapBuffersAction{ osVirtualToPhysical(frameBufPtr) + 640 });
}
void Multilibultra::submit_rsp_task(RDRAM_ARG PTR(OSTask) task_) {

View file

@ -0,0 +1,24 @@
#include "ultra64.h"
extern uint64_t start_time;
#define K0BASE 0x80000000
#define K1BASE 0xA0000000
#define K2BASE 0xC0000000
#define IS_KSEG0(x) ((u32)(x) >= K0BASE && (u32)(x) < K1BASE)
#define IS_KSEG1(x) ((u32)(x) >= K1BASE && (u32)(x) < K2BASE)
#define K0_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg0 to physical */
#define K1_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg1 to physical */
u32 osVirtualToPhysical(PTR(void) addr) {
uintptr_t addr_val = (uintptr_t)addr;
if (IS_KSEG0(addr_val)) {
return K0_TO_PHYS(addr_val);
} else if (IS_KSEG1(addr_val)) {
return K1_TO_PHYS(addr_val);
} else {
// TODO handle TLB mappings
return (u32)addr_val;
}
}

View file

@ -164,6 +164,7 @@ void osViSetEvent(RDRAM_ARG PTR(OSMesgQueue), OSMesg, u32);
void osViSwapBuffer(RDRAM_ARG PTR(void) frameBufPtr);
u32 osGetCount();
OSTime osGetTime();
u32 osVirtualToPhysical(PTR(void) addr);
#ifdef __cplusplus
} // extern "C"

View file

@ -1,47 +0,0 @@
#ifdef _WIN32
#include <Windows.h>
#endif
#include <cstdio>
#include "recomp.h"
extern uint64_t start_time;
extern "C" void osVirtualToPhysical_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
uint32_t virtual_addr = ctx->r4;
// TODO handle TLB mappings
ctx->r2 = virtual_addr - 0x80000000;
}
extern "C" void osInvalDCache_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void osInvalICache_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void osWritebackDCache_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void osWritebackDCacheAll_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void osSetIntMask_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void __osDisableInt_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void __osRestoreInt_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
;
}
extern "C" void __osSetFpcCsr_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
ctx->r2 = 0;
}

View file

@ -55,3 +55,39 @@ extern "C" void osGetTime_recomp(uint8_t * restrict rdram, recomp_context * rest
ctx->r2 = (uint32_t)(total_count >> 32);
ctx->r3 = (int32_t)(total_count >> 0);
}
extern "C" void osVirtualToPhysical_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
ctx->r2 = osVirtualToPhysical((int32_t)ctx->r2);
}
extern "C" void osInvalDCache_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void osInvalICache_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void osWritebackDCache_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void osWritebackDCacheAll_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void osSetIntMask_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void __osDisableInt_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void __osRestoreInt_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
;
}
extern "C" void __osSetFpcCsr_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) {
ctx->r2 = 0;
}

View file

@ -34,7 +34,7 @@ extern "C" recomp_func_t* get_function(uint32_t addr) {
return func_find->second;
}
extern "C" void bzero(uint8_t* restrict rdram, recomp_context* restrict ctx) {
extern "C" void _bzero(uint8_t* restrict rdram, recomp_context* restrict ctx) {
gpr start_addr = ctx->r4;
gpr size = ctx->r5;
@ -61,13 +61,6 @@ void run_thread_function(uint8_t* rdram, uint64_t addr, uint64_t sp, uint64_t ar
func(rdram, &ctx);
}
extern "C" void init(uint8_t * restrict rdram, recomp_context * restrict ctx);
// rocket robot
extern "C" void game_init(uint8_t* restrict rdram, recomp_context* restrict ctx);
// test rom
//extern "C" void init(uint8_t * restrict rdram, recomp_context * restrict ctx);
void do_rom_read(uint8_t* rdram, gpr ram_address, uint32_t dev_address, size_t num_bytes);
std::unique_ptr<uint8_t[]> rom;
@ -75,21 +68,26 @@ size_t rom_size;
uint64_t start_time;
// Recomp generation functions
extern "C" void recomp_entrypoint(uint8_t * restrict rdram, recomp_context * restrict ctx);
gpr get_entrypoint_address();
const char* get_rom_name();
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s [baserom]\n", argv[0]);
exit(EXIT_SUCCESS);
}
//if (argc != 2) {
// printf("Usage: %s [baserom]\n", argv[0]);
// exit(EXIT_SUCCESS);
//}
{
std::basic_ifstream<uint8_t> rom_file{ argv[1], std::ios::binary };
std::basic_ifstream<uint8_t> rom_file{ get_rom_name(), std::ios::binary };
size_t iobuf_size = 0x100000;
std::unique_ptr<uint8_t[]> iobuf = std::make_unique<uint8_t[]>(iobuf_size);
rom_file.rdbuf()->pubsetbuf(iobuf.get(), iobuf_size);
if (!rom_file) {
fprintf(stderr, "Failed to open rom: %s\n", argv[1]);
fprintf(stderr, "Failed to open rom: %s\n", get_rom_name());
exit(EXIT_FAILURE);
}
@ -102,16 +100,8 @@ int main(int argc, char **argv) {
rom_file.read(rom.get(), rom_size);
}
// Byteswap the rom
//for (size_t rom_addr = 0; rom_addr < rom_size; rom_addr += 4) {
// uint32_t word = *reinterpret_cast<uint32_t*>(rom.get() + rom_addr);
// word = byteswap(word);
// *reinterpret_cast<uint32_t*>(rom.get() + rom_addr) = word;
//}
// Get entrypoint from ROM
// TODO fix this for other IPL3 versions
gpr entrypoint = (int32_t)byteswap(*reinterpret_cast<uint32_t*>(rom.get() + 0x8));
// Get entrypoint from recomp function
gpr entrypoint = get_entrypoint_address();
// Allocate rdram_buffer
std::unique_ptr<uint8_t[]> rdram_buffer = std::make_unique<uint8_t[]>(8 * 1024 * 1024);
@ -157,21 +147,11 @@ int main(int argc, char **argv) {
MEM_W(osResetType, 0) = 0; // cold reset
MEM_W(osMemSize, 0) = 8 * 1024 * 1024; // 8MB
// Clear bss
// TODO run the entrypoint instead
// rocket robot
memset(rdram_buffer.get() + 0xAF860, 0, 0xC00A0u - 0XAF860);
// test rom
//memset(rdram_buffer.get() + 0x18670, 0, 0x20D120);
debug_printf("[Recomp] Starting\n");
Multilibultra::preinit(rdram_buffer.get(), rom.get());
// rocket robot
game_init(rdram_buffer.get(), &context);
// test rom
//init(rdram_buffer.get(), &context);
recomp_entrypoint(rdram_buffer.get(), &context);
debug_printf("[Recomp] Quitting\n");

View file

@ -1,4 +1,5 @@
#include <cstdio>
#include <fstream>
#include "../portultra/multilibultra.hpp"
#include "recomp.h"
@ -6,6 +7,8 @@ extern "C" void osSpTaskLoad_recomp(uint8_t* restrict rdram, recomp_context* res
;
}
bool dump_frame = false;
extern "C" void osSpTaskStartGo_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
//printf("[sp] osSpTaskStartGo(0x%08X)\n", (uint32_t)ctx->r4);
OSTask* task = TO_PTR(OSTask, ctx->r4);
@ -14,6 +17,21 @@ extern "C" void osSpTaskStartGo_recomp(uint8_t* restrict rdram, recomp_context*
} else if (task->t.type == M_AUDTASK) {
printf("[sp] Audio task: %08X\n", (uint32_t)ctx->r4);
}
// For debugging
if (dump_frame) {
char addr_str[32];
constexpr size_t ram_size = 0x800000;
std::unique_ptr<char[]> ram_unswapped = std::make_unique<char[]>(ram_size);
sprintf(addr_str, "%08X", task->t.data_ptr);
std::ofstream dump_file{ "../../ramdump" + std::string{ addr_str } + ".bin", std::ios::binary};
for (size_t i = 0; i < ram_size; i++) {
ram_unswapped[i] = rdram[i ^ 3];
}
dump_file.write(ram_unswapped.get(), ram_size);
dump_frame = false;
}
Multilibultra::submit_rsp_task(rdram, ctx->r4);
}

View file

@ -22,7 +22,7 @@ extern "C" void osViGetNextFramebuffer_recomp(uint8_t* restrict rdram, recomp_co
}
extern "C" void osViSwapBuffer_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {
osViSwapBuffer(rdram, ctx->r4);
osViSwapBuffer(rdram, (int32_t)ctx->r4);
}
extern "C" void osViSetMode_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {