From ff84905b931d202b4883f06c5db07e8f776d9d56 Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Mon, 8 Apr 2024 14:17:04 -0300 Subject: [PATCH 1/8] pc: support more systems --- .editorconfig | 21 +++++ .gitignore | 2 +- .../General/232/232_35_AH_MaskHint_Update.c | 8 +- decompile/General/GHOST/GhostReplay_ThTick.c | 4 +- decompile/General/MAIN/MainDB_GetClipSize.c | 2 +- decompile/General/MEMPACK/MEMPACK_Init.c | 6 ++ .../General/RaceFlag/RaceFlag_t11_GetOT.c | 8 +- .../RaceFlag/RaceFlag_t13_DrawLoadingString.c | 8 +- .../General/RaceFlag/RaceFlag_t14_DrawSelf.c | 11 ++- decompile/General/zGlobal_SDATA.c | 7 ++ include/common.h | 2 +- include/{math.h => ctr_math.h} | 0 rebuild_PC/CrashTeamRacingPC.c | 39 +++++++- rebuild_PC/Makefile | 20 ++++ rebuild_PC/PsyCross.nix | 92 +++++++++++++++++++ rebuild_PC/README.md | 9 ++ rebuild_PC/build-all.sh | 22 +++++ rebuild_PC/build-mingw-clang.nix | 6 ++ rebuild_PC/build-mingw-gcc.nix | 6 ++ rebuild_PC/build-native-clang.nix | 6 ++ rebuild_PC/build-native-gcc.nix | 6 ++ rebuild_PC/default.nix | 63 +++++++++++++ rebuild_PS1/TEST_DrawInstances.c | 4 +- rebuild_PS1/main.c | 2 +- 24 files changed, 328 insertions(+), 26 deletions(-) create mode 100644 .editorconfig rename include/{math.h => ctr_math.h} (100%) create mode 100644 rebuild_PC/Makefile create mode 100644 rebuild_PC/PsyCross.nix create mode 100644 rebuild_PC/README.md create mode 100755 rebuild_PC/build-all.sh create mode 100755 rebuild_PC/build-mingw-clang.nix create mode 100755 rebuild_PC/build-mingw-gcc.nix create mode 100755 rebuild_PC/build-native-clang.nix create mode 100755 rebuild_PC/build-native-gcc.nix create mode 100644 rebuild_PC/default.nix diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..26c2b5523 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +root = true + +[*] +insert_final_newline = false +trim_trailing_whitespace = true +charset = utf-8 + +[*.{nix,md,MD,sh}] +insert_final_newline = true +end_of_line = lf +indent_style = space +indent_size = 2 + +[*.{c,C,h,H}] +indent_style = tab +indent_size = 4 + +[Makefile] +insert_final_newline = true +end_of_line = lf +indent_style = tab \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3157e34d9..0d666a995 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,7 @@ offset.txt comport.txt gcc_out.txt overlay.ld -Makefile +rebuild_PC/result # Version tracker tools/updater/.version diff --git a/decompile/General/232/232_35_AH_MaskHint_Update.c b/decompile/General/232/232_35_AH_MaskHint_Update.c index 202a728a1..4490c76eb 100644 --- a/decompile/General/232/232_35_AH_MaskHint_Update.c +++ b/decompile/General/232/232_35_AH_MaskHint_Update.c @@ -15,7 +15,7 @@ void DECOMP_AH_MaskHint_Update() sdata->AkuAkuHintState++; break; - case 1: + case 1: { int absSpeedApprox = d->speedApprox; if(absSpeedApprox < 0) absSpeedApprox = -absSpeedApprox; if(absSpeedApprox > 0x31) return; @@ -65,7 +65,7 @@ void DECOMP_AH_MaskHint_Update() D232.maskWarppadDelayFrames = FPS_DOUBLE(60); sdata->AkuAkuHintState++; - break; + } break; case 2: @@ -189,7 +189,7 @@ void DECOMP_AH_MaskHint_Update() // stay here forever stuck break; - case 4: + case 4: { int lngIndex = 0; int boolFound = 0; short* ptrLngID = &D232.hintMenu_lngIndexArr[0]; @@ -270,7 +270,7 @@ void DECOMP_AH_MaskHint_Update() // show map again gGT->hudFlags &= ~(0x10); } - break; + } break; case 5: diff --git a/decompile/General/GHOST/GhostReplay_ThTick.c b/decompile/General/GHOST/GhostReplay_ThTick.c index 1184c7a34..597e63743 100644 --- a/decompile/General/GHOST/GhostReplay_ThTick.c +++ b/decompile/General/GHOST/GhostReplay_ThTick.c @@ -316,7 +316,7 @@ void DECOMP_GhostReplay_ThTick(struct Thread *t) tape->packetID++; break; - case 0x81: // Animation + case 0x81: { // Animation int numAnimFrames = DECOMP_INSTANCE_GetNumAnimFrames(inst, buffer[1]); inst->animIndex = (numAnimFrames < 1) ? 0 : buffer[1]; inst->animFrame = (buffer[2] == 0 || numAnimFrames <= buffer[2]) @@ -333,7 +333,7 @@ void DECOMP_GhostReplay_ThTick(struct Thread *t) #endif buffer += 3; - break; + } break; case 0x82: // Boost if ( diff --git a/decompile/General/MAIN/MainDB_GetClipSize.c b/decompile/General/MAIN/MainDB_GetClipSize.c index 42f95e161..220d4f7bd 100644 --- a/decompile/General/MAIN/MainDB_GetClipSize.c +++ b/decompile/General/MAIN/MainDB_GetClipSize.c @@ -1,6 +1,6 @@ #include -u_int DECOMP_MainDB_GetClipSize(u_int levelID, int numPlyrCurrGame) +int DECOMP_MainDB_GetClipSize(u_int levelID, int numPlyrCurrGame) { if(levelID == PAPU_PYRAMID || levelID == POLAR_PASS) if (2 < numPlyrCurrGame) diff --git a/decompile/General/MEMPACK/MEMPACK_Init.c b/decompile/General/MEMPACK/MEMPACK_Init.c index 7dc05fa01..33369feae 100644 --- a/decompile/General/MEMPACK/MEMPACK_Init.c +++ b/decompile/General/MEMPACK/MEMPACK_Init.c @@ -1,4 +1,5 @@ #include +#include #ifdef REBUILD_PC // with 8mb expansion, we have bonus PrimMem @@ -23,6 +24,11 @@ void DECOMP_MEMPACK_Init(int ramSize) // Visual Studio -> Properties -> Linker -> Advanced -> // Base Address, Randomized Base Address, Fixed Base Address ptrMempack->start = &memory[0]; + printf( + "[CTR] Where does memory starts? (%s) %" PRIu32 "\n", + (ptrMempack->start < 0x01000000 ? "GOOD" : "BAD"), + ptrMempack->start + ); memset(memory, 0, 8*1024*1024); ptrMempack->endOfAllocator = &memory[8*1024*1024 - 4]; diff --git a/decompile/General/RaceFlag/RaceFlag_t11_GetOT.c b/decompile/General/RaceFlag/RaceFlag_t11_GetOT.c index 3243e328c..e87eb05ba 100644 --- a/decompile/General/RaceFlag/RaceFlag_t11_GetOT.c +++ b/decompile/General/RaceFlag/RaceFlag_t11_GetOT.c @@ -1,13 +1,13 @@ #include -int DECOMP_RaceFlag_GetOT(void) +u_int* DECOMP_RaceFlag_GetOT(void) { short sVar1; int iVar2; struct GameTracker* gGT = sdata->gGT; - int* otDrawFirst_FarthestDepth; - int* otDrawLast_ClosestDepth; + u_int* otDrawFirst_FarthestDepth; + u_int* otDrawLast_ClosestDepth; otDrawFirst_FarthestDepth = &gGT->pushBuffer[0].ptrOT[0x3FF]; otDrawLast_ClosestDepth = gGT->otSwapchainDB[gGT->swapchainIndex]; @@ -93,4 +93,4 @@ int DECOMP_RaceFlag_GetOT(void) } return otDrawLast_ClosestDepth; -} +} \ No newline at end of file diff --git a/decompile/General/RaceFlag/RaceFlag_t13_DrawLoadingString.c b/decompile/General/RaceFlag/RaceFlag_t13_DrawLoadingString.c index c18d05fdc..694834c0c 100644 --- a/decompile/General/RaceFlag/RaceFlag_t13_DrawLoadingString.c +++ b/decompile/General/RaceFlag/RaceFlag_t13_DrawLoadingString.c @@ -12,7 +12,7 @@ void DECOMP_RaceFlag_DrawLoadingString(void) char *pbVar8; int iVar9; int iVar10; - int uVar11; + u_int *uVar11; char local_30; char local_2f; @@ -49,8 +49,10 @@ void DECOMP_RaceFlag_DrawLoadingString(void) iVar10 = (sdata->RaceFlag_Transition & 0xffff) - ( - ((iVar3 << 0x10) >> 0x10) - - ((iVar3 << 0x10) >> 0x1f) >> 1 + ( + ((iVar3 << 0x10) >> 0x10) - + ((iVar3 << 0x10) >> 0x1f) + ) >> 1 ); iVar3 = FPS_HALF(sdata->RaceFlag_LoadingTextAnimFrame); diff --git a/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c b/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c index 4751403ea..9b3fb6758 100644 --- a/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c +++ b/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c @@ -1,16 +1,16 @@ #include #ifdef REBUILD_PC -int scratchpadBuf[0x1000]; +u_int scratchpadBuf[0x1000]; #endif force_inline char RaceFlag_CalculateBrightness(u_int sine, u_char darkTile) { if (darkTile) { - return sine * -55 + 0x140000 >> 0xD; + return((sine * -55 + 0x140000) >> 0xD); } - return sine * -125 + 0x1fe000 >> 0xD; + return((sine * -125 + 0x1fe000) >> 0xD); } void DECOMP_RaceFlag_DrawSelf() @@ -20,7 +20,7 @@ void DECOMP_RaceFlag_DrawSelf() u_char toggle; short flagPos; u_long *ot; - long *scratchpad; + u_int *scratchpad; u_int screenlimit; u_int dimensions; int approx[2]; @@ -171,7 +171,7 @@ void DECOMP_RaceFlag_DrawSelf() #ifdef REBUILD_PC top = &scratchpadBuf[(toggle * 0x78 / 4) - 1]; toggle = toggle ^ 1; - bottom = &scratchpadBuf[(toggle * 0x78 / 4) - 0]; + bottom = &scratchpadBuf[(toggle * 0x78 / 4)]; #else top = (u_int *)((0x1f800000 + toggle * 0x78) - 4); toggle = toggle ^ 1; @@ -318,6 +318,7 @@ void DECOMP_RaceFlag_DrawSelf() setRGB1(p, colorLeft, colorLeft, colorLeft); setRGB3(p, colorLeft, colorLeft, colorLeft); + // positions *(int *)&p->x0 = bottom[0]; *(int *)&p->x2 = bottom[1]; diff --git a/decompile/General/zGlobal_SDATA.c b/decompile/General/zGlobal_SDATA.c index b0654255d..6695dcc43 100644 --- a/decompile/General/zGlobal_SDATA.c +++ b/decompile/General/zGlobal_SDATA.c @@ -5,6 +5,10 @@ #include +#if __GNUC__ +char memcardBytes[0x1680]; +#endif + struct sData sdata_static = { .langBufferSize = 0x3F04, @@ -353,6 +357,9 @@ struct sData sdata_static = #if NO_BSS .ptrToMemcardBuffer1 = 0x800992e4, .ptrToMemcardBuffer2 = 0x800992e4, +#elif __GNUC__ + .ptrToMemcardBuffer1 = &memcardBytes[0], + .ptrToMemcardBuffer2 = &memcardBytes[0], #else .ptrToMemcardBuffer1 = &sdata_static.memcardBytes[0], .ptrToMemcardBuffer2 = &sdata_static.memcardBytes[0], diff --git a/include/common.h b/include/common.h index c339efef2..6e6bbf3c1 100644 --- a/include/common.h +++ b/include/common.h @@ -87,7 +87,7 @@ #include #include #include -#include +#include #include #ifndef REBUILD_PC diff --git a/include/math.h b/include/ctr_math.h similarity index 100% rename from include/math.h rename to include/ctr_math.h diff --git a/rebuild_PC/CrashTeamRacingPC.c b/rebuild_PC/CrashTeamRacingPC.c index 004feee5f..bb82533d6 100644 --- a/rebuild_PC/CrashTeamRacingPC.c +++ b/rebuild_PC/CrashTeamRacingPC.c @@ -1,5 +1,9 @@ - // ======= Headers ============= +#include +#include +#define _EnterCriticalSection(x) +#define EnterCriticalSection(x) +#define ExitCriticalSection() #define _CRT_SECURE_NO_WARNINGS #define REBUILD_PC @@ -57,11 +61,15 @@ typedef enum { // work in PsyXKeyboardHandler, workaround: int NikoGetEnterKey() { +#if __GNUC__ + return 0; +#else // dont use Windows.h __declspec(dllimport) short __stdcall - GetAsyncKeyState(_In_ int vKey); + GetAsyncKeyState(int vKey); return GetAsyncKeyState(0xd); +#endif } void PsyXKeyboardHandler(int key, char down) @@ -90,11 +98,36 @@ int NikoCalcFPS() printf("NikoCalcFPS: %d fps\n", (1000 * frameGap) / delta); } -int main() +#ifndef CC + #if __GNUC__ + #if _WIN32 + #ifndef __clang__ + #define CC "MINGW-GCC" + #else + #define CC "MINGW-CLANG" + #endif + #else + #ifndef __clang__ + #define CC "GCC" + #else + #define CC "CLANG" + #endif + #endif + #elif defined(_MSC_VER) + #define CC "MSVC" + #else + #define CC "Unknown" + #endif +#endif + +int main(int argc, char* argv[]) { + printf("[CTR] Built with: " CC "\n"); #ifdef USE_16BY9 + printf("[CTR] USE_16BY9=1\n"); PsyX_Initialise("CTRPC", 1280, 720, 0); #else + printf("[CTR] USE_16BY9=0\n"); PsyX_Initialise("CTRPC", 800, 600, 0); #endif diff --git a/rebuild_PC/Makefile b/rebuild_PC/Makefile new file mode 100644 index 000000000..2b8b514d8 --- /dev/null +++ b/rebuild_PC/Makefile @@ -0,0 +1,20 @@ +.PHONY: all + +CFLAGS+=--std=c99 -DUSE_16BY9=1 -O0 \ + -I./PsyCross/include -I../include \ + `${PKG_CONFIG} --cflags sdl2` + +# Clang is rigorous +CFLAGS+=-Wno-int-conversion -Wno-incompatible-function-pointer-types -Wno-implicit-function-declaration -Wno-return-type + +LDLIBS+=-lpsycross \ + `${PKG_CONFIG} --libs sdl2` \ + `${PKG_CONFIG} --libs openal` \ + -lm + +# Clang always needs "no-pie", some distros might add PIE to GCC too. +LDFLAGS+=-fno-pie -no-pie -Wl,-Ttext,0x00D00000 + +CrashTeamRacingPC: CrashTeamRacingPC.o + +all: CrashTeamRacingPC diff --git a/rebuild_PC/PsyCross.nix b/rebuild_PC/PsyCross.nix new file mode 100644 index 000000000..bc86e7e24 --- /dev/null +++ b/rebuild_PC/PsyCross.nix @@ -0,0 +1,92 @@ +{ lib +, stdenv +, pkg-config +, cmake +, SDL2 +, openal +, fetchFromGitHub +}: + +let + isWindows = stdenv.hostPlatform.uname.system == "Windows"; + + openalWithWindows = + if isWindows then + (openal.override { + alsaSupport = false; + dbusSupport = false; + pipewireSupport = false; + pulseSupport = false; + }).overrideAttrs (prevAttrs: { + meta = prevAttrs.meta // { inherit (SDL2.meta) platforms; }; + }) + else openal; +in +stdenv.mkDerivation (finalAttrs: { + pname = "PsyCross"; + version = "unstable-2024-02-14-603475326"; + + src = fetchFromGitHub { + owner = "OpenDriver2"; + repo = "PsyCross"; + rev = "603475326dfa546cb47a6cc338c32053cca56022"; + hash = "sha256-1FW0U/XE+4WBrcgvOAaLSdU/GlR2S3Av8UiZla5rybI="; + }; + + # Shows the proper compile date in the logs (taken from commit date) + env.SOURCE_DATE_EPOCH = "1707912179"; + + nativeBuildInputs = [ pkg-config cmake ]; + propagatedBuildInputs = [ SDL2 openalWithWindows ]; + + CFLAGS = [ "-Wno-narrowing" ]; + CXXFLAGS = [ "-Wno-narrowing" ]; + hardeningDisable = [ "format" ]; + + postPatch = '' + substituteInPlace CMakeLists.txt \ + --replace-fail 'OPENAL' 'OpenAL' \ + --replace-fail '"*.c"' '"*.c" "*.C"' + + substituteInPlace 'src/psx/LIBGTE.C' \ + --replace-fail 'RotTransPers(SVECTOR* v0, int* sxy' 'RotTransPers(SVECTOR* v0, long* sxy' + + substituteInPlace 'src/psx/LIBGPU.C' \ + --replace-fail 'u_short LoadTPage(u_int*' 'u_short LoadTPage(u_long*' + + substituteInPlace 'include/psx/kernel.h' \ + --replace-fail '#if 0' "#if 1" + + substituteInPlace 'include/PsyX/PsyX_config.h' \ + --replace-fail 'USE_EXTENDED_PRIM_POINTERS 1' 'USE_EXTENDED_PRIM_POINTERS 0' + + sed -i"" \ + 's/#ifdef _WIN32/#ifdef _WIN32\n#include \n/;/^#define _stricmp/d;s/_stricmp/strcmp/' \ + src/PsyX_main.cpp + + sed -i"" '1s/^/cmake_minimum_required(VERSION 3.28)\n/' CMakeLists.txt + ''; + + installPhase = '' + runHook preInstall + + mkdir "$out" + cp -r ../include "$out/include" + cp libpsycross.a "$out/libpsycross.a" + + runHook postInstall + ''; + + passthru = { + inherit SDL2; + openal = openalWithWindows; + }; + + meta = { + description = "Compatibility framework for building and running Psy-Q SDK"; + homepage = "https://github.com/OpenDriver2/PsyCross"; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ pedrohlc ]; + inherit (SDL2.meta) platforms; + }; +}) diff --git a/rebuild_PC/README.md b/rebuild_PC/README.md new file mode 100644 index 000000000..bacecd083 --- /dev/null +++ b/rebuild_PC/README.md @@ -0,0 +1,9 @@ +### Compiling + +The following are required to successfully build CTR-PC: +- Target 32-bit artifacts; +- Use 32-bit libraries; +- Needs PsyCross built with `USE_EXTENDED_PRIM=0`; +- On MSVC use "Fixed Base Address", on GCC move `-Wl,-Ttext,0x0` to the earliest position that does not conflict. + +For Windows open `CrashTeamRacingPC.c` with VisualStudio; for [Mingw](https://www.mingw-w64.org/)/Linux/Darwin use the `Makefile`; [Nix](https://nixos.org/download/#download-nix) users can use `./build-{native,mingw}-{gcc,clang}.nix` directly. diff --git a/rebuild_PC/build-all.sh b/rebuild_PC/build-all.sh new file mode 100755 index 000000000..41bdbdd61 --- /dev/null +++ b/rebuild_PC/build-all.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env sh +set -e + +if ! [ -e ./CrashTeamRacingPC.c ]; then + echo "Wrong \$PWD!" + exit 1 +fi + +if [ -e ./result ]; then + if ! [ -e ./result/native-gcc ]; then + echo "Cleanup first!" + exit 2 + fi +else + mkdir ./result +fi + +for target in {native,mingw}-{gcc,clang}; do + ./build-${target}.nix -o ./result/$target +done + +echo 'Finished successfully' diff --git a/rebuild_PC/build-mingw-clang.nix b/rebuild_PC/build-mingw-clang.nix new file mode 100755 index 000000000..c0486c34c --- /dev/null +++ b/rebuild_PC/build-mingw-clang.nix @@ -0,0 +1,6 @@ +#! /usr/bin/env nix-build +{ pkgs ? import {} +, pkgsCross ? pkgs.pkgsCross.mingw32 +}: + +pkgsCross.callPackage ./default.nix { stdenv = pkgsCross.clangStdenv; } diff --git a/rebuild_PC/build-mingw-gcc.nix b/rebuild_PC/build-mingw-gcc.nix new file mode 100755 index 000000000..03267b3bf --- /dev/null +++ b/rebuild_PC/build-mingw-gcc.nix @@ -0,0 +1,6 @@ +#! /usr/bin/env nix-build +{ pkgs ? import {} +, pkgsCross ? pkgs.pkgsCross.mingw32 +}: + +pkgsCross.callPackage ./default.nix {} diff --git a/rebuild_PC/build-native-clang.nix b/rebuild_PC/build-native-clang.nix new file mode 100755 index 000000000..6fcfc05d2 --- /dev/null +++ b/rebuild_PC/build-native-clang.nix @@ -0,0 +1,6 @@ +#! /usr/bin/env nix-build +{ pkgs ? import {} +, pkgsCross ? pkgs.pkgsi686Linux +}: + +pkgsCross.callPackage ./default.nix { stdenv = pkgsCross.clangStdenv; } diff --git a/rebuild_PC/build-native-gcc.nix b/rebuild_PC/build-native-gcc.nix new file mode 100755 index 000000000..25fb86a85 --- /dev/null +++ b/rebuild_PC/build-native-gcc.nix @@ -0,0 +1,6 @@ +#! /usr/bin/env nix-build +{ pkgs ? import {} +, pkgsCross ? pkgs.pkgsi686Linux +}: + +pkgsCross.callPackage ./default.nix {} diff --git a/rebuild_PC/default.nix b/rebuild_PC/default.nix new file mode 100644 index 000000000..47770eb51 --- /dev/null +++ b/rebuild_PC/default.nix @@ -0,0 +1,63 @@ +{ lib +, stdenv +, callPackage +, pkg-config +, ctrModSDK ? ./.. +, psyCross ? callPackage ./PsyCross.nix {} +}: + +let + isWindows = stdenv.hostPlatform.uname.system == "Windows"; + + mainProgram = if isWindows then "CrashTeamRacingPC.exe" else "CrashTeamRacingPC"; +in +stdenv.mkDerivation (finalAttrs: { + pname = "CTR-PC"; + version = "0.0.1"; + + src = ctrModSDK; + sourceRoot = "CTR-ModSDK/rebuild_PC"; + + nativeBuildInputs = [ pkg-config ]; + buildInputs = psyCross.propagatedBuildInputs; + + LDLIBS = "-L./PsyCross"; + + # Disables incompatible hardening + hardeningDisable = [ "format" ]; + + postPatch = '' + rm -r PsyCross + ln -s ${psyCross} PsyCross + ''; + + installPhase = '' + runHook preInstall + + mkdir -p $out/bin + '' + lib.strings.optionalString isWindows '' + cp ${psyCross.openal}/bin/*.dll $out/bin/ + cp ${psyCross.SDL2}/bin/*.dll $out/bin/ + '' + '' + cp ${mainProgram} $out/bin/ + + runHook postInstall + ''; + + # Debug + CFLAGS = "-g -gdwarf-2"; + dontStrip = true; + passthru = { inherit psyCross; }; + + # Shows the proper compile date in the logs + env.SOURCE_DATE_EPOCH = (builtins.currentTime or ctrModSDK.lastModified); + + meta = { + description = "CTR in C"; + homepage = "https://github.com/CTR-tools/CTR-ModSDK"; + license = lib.licenses.publicDomain; + maintainers = with lib.maintainers; [ pedrohlc ]; + inherit mainProgram; + inherit (psyCross.meta) platforms; + }; +}) diff --git a/rebuild_PS1/TEST_DrawInstances.c b/rebuild_PS1/TEST_DrawInstances.c index e241d49b1..ec963e602 100644 --- a/rebuild_PS1/TEST_DrawInstances.c +++ b/rebuild_PS1/TEST_DrawInstances.c @@ -3,7 +3,9 @@ int bi = 0; #ifdef REBUILD_PC -inline +#ifndef __GNUC__ +inline +#endif #endif int GetBit(unsigned int* vertData) { diff --git a/rebuild_PS1/main.c b/rebuild_PS1/main.c index f11c73f03..443515b99 100644 --- a/rebuild_PS1/main.c +++ b/rebuild_PS1/main.c @@ -78,7 +78,7 @@ #include "../decompile/General/DecalHUD/DecalHUD_DrawPolyGT4.c" #include "../decompile/General/DecalHUD/DecalHUD_Arrow2D.c" -#include "../decompile/General/DISPLAY/DISPLAY_Swap.c" +#include "../decompile/General/Display/DISPLAY_Swap.c" #include "../decompile/General/DotLights/DotLights.c" #include "../decompile/General/DropRain/DropRain.c" From 2af8409cc468f7a0b9e082e62abd0e4dc271791a Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Mon, 8 Apr 2024 14:45:35 -0300 Subject: [PATCH 2/8] move to %08x --- decompile/General/MEMPACK/MEMPACK_Init.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/decompile/General/MEMPACK/MEMPACK_Init.c b/decompile/General/MEMPACK/MEMPACK_Init.c index 33369feae..390c84bfb 100644 --- a/decompile/General/MEMPACK/MEMPACK_Init.c +++ b/decompile/General/MEMPACK/MEMPACK_Init.c @@ -1,5 +1,4 @@ #include -#include #ifdef REBUILD_PC // with 8mb expansion, we have bonus PrimMem @@ -25,7 +24,7 @@ void DECOMP_MEMPACK_Init(int ramSize) // Base Address, Randomized Base Address, Fixed Base Address ptrMempack->start = &memory[0]; printf( - "[CTR] Where does memory starts? (%s) %" PRIu32 "\n", + "[CTR] Where does memory starts? (%s) %08x\n", (ptrMempack->start < 0x01000000 ? "GOOD" : "BAD"), ptrMempack->start ); From c8acda4eca4da17d23ddeaaddbfcd2c797c78da2 Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Mon, 8 Apr 2024 14:47:52 -0300 Subject: [PATCH 3/8] include SDL2 only for us --- rebuild_PC/CrashTeamRacingPC.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rebuild_PC/CrashTeamRacingPC.c b/rebuild_PC/CrashTeamRacingPC.c index bb82533d6..9506baee5 100644 --- a/rebuild_PC/CrashTeamRacingPC.c +++ b/rebuild_PC/CrashTeamRacingPC.c @@ -1,6 +1,8 @@ // ======= Headers ============= -#include +#if __GNUC__ #include +#endif + #define _EnterCriticalSection(x) #define EnterCriticalSection(x) #define ExitCriticalSection() From 81e744138abff708882e820d62128127a339e09f Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Mon, 8 Apr 2024 14:59:25 -0300 Subject: [PATCH 4/8] no critical session only for us --- decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c | 1 - rebuild_PC/CrashTeamRacingPC.c | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c b/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c index 9b3fb6758..c7c2f8b26 100644 --- a/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c +++ b/decompile/General/RaceFlag/RaceFlag_t14_DrawSelf.c @@ -318,7 +318,6 @@ void DECOMP_RaceFlag_DrawSelf() setRGB1(p, colorLeft, colorLeft, colorLeft); setRGB3(p, colorLeft, colorLeft, colorLeft); - // positions *(int *)&p->x0 = bottom[0]; *(int *)&p->x2 = bottom[1]; diff --git a/rebuild_PC/CrashTeamRacingPC.c b/rebuild_PC/CrashTeamRacingPC.c index 9506baee5..af1d97d10 100644 --- a/rebuild_PC/CrashTeamRacingPC.c +++ b/rebuild_PC/CrashTeamRacingPC.c @@ -1,11 +1,10 @@ // ======= Headers ============= #if __GNUC__ #include -#endif - #define _EnterCriticalSection(x) #define EnterCriticalSection(x) #define ExitCriticalSection() +#endif #define _CRT_SECURE_NO_WARNINGS #define REBUILD_PC From 13f32e64b07332c750c4a66bae07c33f173ed184 Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Mon, 8 Apr 2024 22:07:36 -0300 Subject: [PATCH 5/8] make it playable in clang --- decompile/General/LOAD/LOAD_A27_HowlSectorChainStart.c | 1 + 1 file changed, 1 insertion(+) diff --git a/decompile/General/LOAD/LOAD_A27_HowlSectorChainStart.c b/decompile/General/LOAD/LOAD_A27_HowlSectorChainStart.c index 7b4a1e96b..700508395 100644 --- a/decompile/General/LOAD/LOAD_A27_HowlSectorChainStart.c +++ b/decompile/General/LOAD/LOAD_A27_HowlSectorChainStart.c @@ -40,4 +40,5 @@ int DECOMP_LOAD_HowlSectorChainStart(CdlFILE* cdlFileHWL, void* ptrDestination, DECOMP_LOAD_HowlCallback(CdlComplete); #endif + return 1; } \ No newline at end of file From bbde7f16e42a257522d0dc5d8b5265c3c3d67980 Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Tue, 9 Apr 2024 08:35:45 -0300 Subject: [PATCH 6/8] clang fixes, adopt my PsyCross PR, and flakenize --- .gitignore | 1 + flake.lock | 25 +++++++++++++++++++++++++ flake.nix | 15 +++++++++++++++ rebuild_PC/Makefile | 2 +- rebuild_PC/PsyCross.nix | 39 ++++++++++++--------------------------- rebuild_PC/default.nix | 7 +++++-- 6 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index 0d666a995..cad4f256a 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ comport.txt gcc_out.txt overlay.ld rebuild_PC/result +/result # Version tracker tools/updater/.version diff --git a/flake.lock b/flake.lock new file mode 100644 index 000000000..fe48b2e23 --- /dev/null +++ b/flake.lock @@ -0,0 +1,25 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1712439257, + "narHash": "sha256-aSpiNepFOMk9932HOax0XwNxbA38GOUVOiXfUVPOrck=", + "rev": "ff0dbd94265ac470dda06a657d5fe49de93b4599", + "revCount": 608655, + "type": "tarball", + "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.608655%2Brev-ff0dbd94265ac470dda06a657d5fe49de93b4599/018eb77a-ddbc-7dfa-9c9c-d5444d2bb52f/source.tar.gz" + }, + "original": { + "type": "tarball", + "url": "https://flakehub.com/f/NixOS/nixpkgs/0.1.%2A.tar.gz" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 000000000..8833ebca4 --- /dev/null +++ b/flake.nix @@ -0,0 +1,15 @@ +{ + inputs.nixpkgs.url = https://flakehub.com/f/NixOS/nixpkgs/0.1.*.tar.gz; + outputs = { self, nixpkgs }: { + packages.x86_64-linux = with nixpkgs.legacyPackages.x86_64-linux; { + native32 = with pkgsi686Linux; { + gcc = callPackage ./rebuild_PC { ctrModSDK = self; }; + clang = callPackage ./rebuild_PC { ctrModSDK = self; stdenv = clangStdenv; }; + }; + mingw32 = with mingw32; { + gcc = callPackage ./rebuild_PC { ctrModSDK = self; }; + clang = callPackage ./rebuild_PC { ctrModSDK = self; stdenv = clangStdenv; }; + }; + }; + }; +} diff --git a/rebuild_PC/Makefile b/rebuild_PC/Makefile index 2b8b514d8..ec8193d5d 100644 --- a/rebuild_PC/Makefile +++ b/rebuild_PC/Makefile @@ -10,7 +10,7 @@ CFLAGS+=-Wno-int-conversion -Wno-incompatible-function-pointer-types -Wno-implic LDLIBS+=-lpsycross \ `${PKG_CONFIG} --libs sdl2` \ `${PKG_CONFIG} --libs openal` \ - -lm + -lstdc++ -lm # Clang always needs "no-pie", some distros might add PIE to GCC too. LDFLAGS+=-fno-pie -no-pie -Wl,-Ttext,0x00D00000 diff --git a/rebuild_PC/PsyCross.nix b/rebuild_PC/PsyCross.nix index bc86e7e24..f40738b32 100644 --- a/rebuild_PC/PsyCross.nix +++ b/rebuild_PC/PsyCross.nix @@ -5,6 +5,7 @@ , SDL2 , openal , fetchFromGitHub +, psyCrossDebug ? false }: let @@ -17,54 +18,38 @@ let dbusSupport = false; pipewireSupport = false; pulseSupport = false; + inherit stdenv; }).overrideAttrs (prevAttrs: { meta = prevAttrs.meta // { inherit (SDL2.meta) platforms; }; }) - else openal; + else openal.override { inherit stdenv; }; in stdenv.mkDerivation (finalAttrs: { pname = "PsyCross"; - version = "unstable-2024-02-14-603475326"; + version = "unstable-2024-02-14-0ce306d6c"; src = fetchFromGitHub { owner = "OpenDriver2"; repo = "PsyCross"; - rev = "603475326dfa546cb47a6cc338c32053cca56022"; - hash = "sha256-1FW0U/XE+4WBrcgvOAaLSdU/GlR2S3Av8UiZla5rybI="; + rev = "0ce306d6c32412986037e7e5e1dbdc1bf72e066a"; + hash = "sha256-c57xyzM7GkL9SazUUxAM4DVXxb7cTcjJxgJpZ7UMBVQ="; }; # Shows the proper compile date in the logs (taken from commit date) - env.SOURCE_DATE_EPOCH = "1707912179"; + env.SOURCE_DATE_EPOCH = "1712578640"; nativeBuildInputs = [ pkg-config cmake ]; - propagatedBuildInputs = [ SDL2 openalWithWindows ]; + propagatedBuildInputs = [ (SDL2.override { inherit stdenv; }) openalWithWindows ]; - CFLAGS = [ "-Wno-narrowing" ]; - CXXFLAGS = [ "-Wno-narrowing" ]; hardeningDisable = [ "format" ]; - postPatch = '' - substituteInPlace CMakeLists.txt \ - --replace-fail 'OPENAL' 'OpenAL' \ - --replace-fail '"*.c"' '"*.c" "*.C"' - - substituteInPlace 'src/psx/LIBGTE.C' \ - --replace-fail 'RotTransPers(SVECTOR* v0, int* sxy' 'RotTransPers(SVECTOR* v0, long* sxy' - - substituteInPlace 'src/psx/LIBGPU.C' \ - --replace-fail 'u_short LoadTPage(u_int*' 'u_short LoadTPage(u_long*' - - substituteInPlace 'include/psx/kernel.h' \ - --replace-fail '#if 0' "#if 1" + CFLAGS = "-Wno-implicit-function-declaration" + lib.strings.optionalString psyCrossDebug "-D_DEBUG=1 -g -gdwarf-2 -O0"; + CXXFLAGS = lib.strings.optionalString psyCrossDebug "-D_DEBUG=1 -g -gdwarf-2 -O0"; + dontStrip = psyCrossDebug; + postPatch = '' substituteInPlace 'include/PsyX/PsyX_config.h' \ --replace-fail 'USE_EXTENDED_PRIM_POINTERS 1' 'USE_EXTENDED_PRIM_POINTERS 0' - - sed -i"" \ - 's/#ifdef _WIN32/#ifdef _WIN32\n#include \n/;/^#define _stricmp/d;s/_stricmp/strcmp/' \ - src/PsyX_main.cpp - - sed -i"" '1s/^/cmake_minimum_required(VERSION 3.28)\n/' CMakeLists.txt ''; installPhase = '' diff --git a/rebuild_PC/default.nix b/rebuild_PC/default.nix index 47770eb51..8196b7b93 100644 --- a/rebuild_PC/default.nix +++ b/rebuild_PC/default.nix @@ -3,7 +3,8 @@ , callPackage , pkg-config , ctrModSDK ? ./.. -, psyCross ? callPackage ./PsyCross.nix {} +, psyCross ? callPackage ./PsyCross.nix { inherit psyCrossDebug; } +, psyCrossDebug ? false }: let @@ -16,7 +17,9 @@ stdenv.mkDerivation (finalAttrs: { version = "0.0.1"; src = ctrModSDK; - sourceRoot = "CTR-ModSDK/rebuild_PC"; + sourceRoot = + if ctrModSDK == ./.. then "CTR-ModSDK/rebuild_PC" + else "source/rebuild_PC"; nativeBuildInputs = [ pkg-config ]; buildInputs = psyCross.propagatedBuildInputs; From 998673e9569ab5446285645812b6f00aa972e455 Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Tue, 9 Apr 2024 08:48:51 -0300 Subject: [PATCH 7/8] add CI building them all --- .github/workflows/main.yml | 23 +++++++++++++++++++++++ flake.nix | 20 +++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..4df9dc774 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,23 @@ +name: Build main + +on: + push: + branches: + - main + +permissions: + contents: read + pages: write + id-token: write + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: DeterminateSystems/nix-installer-action@main + - uses: DeterminateSystems/magic-nix-cache-action@main + - name: Build + run: nix build -L --no-link --keep-going .#all \ No newline at end of file diff --git a/flake.nix b/flake.nix index 8833ebca4..504360509 100644 --- a/flake.nix +++ b/flake.nix @@ -1,15 +1,29 @@ { inputs.nixpkgs.url = https://flakehub.com/f/NixOS/nixpkgs/0.1.*.tar.gz; - outputs = { self, nixpkgs }: { - packages.x86_64-linux = with nixpkgs.legacyPackages.x86_64-linux; { + outputs = { self, nixpkgs }: rec { + packages.x86_64-linux = with nixpkgs.legacyPackages.x86_64-linux; rec { native32 = with pkgsi686Linux; { gcc = callPackage ./rebuild_PC { ctrModSDK = self; }; clang = callPackage ./rebuild_PC { ctrModSDK = self; stdenv = clangStdenv; }; }; - mingw32 = with mingw32; { + mingw32 = with pkgsCross.mingw32; { gcc = callPackage ./rebuild_PC { ctrModSDK = self; }; clang = callPackage ./rebuild_PC { ctrModSDK = self; stdenv = clangStdenv; }; }; + all = stdenvNoCC.mkDerivation { + name = "ctr-join"; + dontUnpack = true; + dontBuild = true; + installPhase = '' + mkdir $out + ln -s ${native32.gcc} $out/native32-gcc + ln -s ${native32.clang} $out/native32-clang + ln -s ${mingw32.gcc} $out/mingw32-gcc + ln -s ${mingw32.clang} $out/mingw32-clang + ''; + }; }; + # default + package.x86_64-linux = packages.x86_64-linux.native32.gcc; }; } From 7115888a295afb2bae2357287ce7ada71e6bdedb Mon Sep 17 00:00:00 2001 From: PedroHLC Date: Wed, 10 Apr 2024 08:15:16 -0300 Subject: [PATCH 8/8] make -O2 the default --- .editorconfig | 2 +- .github/workflows/main.yml | 2 +- rebuild_PC/Makefile | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.editorconfig b/.editorconfig index 26c2b5523..e8e9c5123 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,7 +5,7 @@ insert_final_newline = false trim_trailing_whitespace = true charset = utf-8 -[*.{nix,md,MD,sh}] +[*.{nix,md,MD,sh,yml}] insert_final_newline = true end_of_line = lf indent_style = space diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4df9dc774..1730aa397 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,4 +20,4 @@ jobs: - uses: DeterminateSystems/nix-installer-action@main - uses: DeterminateSystems/magic-nix-cache-action@main - name: Build - run: nix build -L --no-link --keep-going .#all \ No newline at end of file + run: nix build -L --no-link --keep-going .#all diff --git a/rebuild_PC/Makefile b/rebuild_PC/Makefile index ec8193d5d..efdbf3d38 100644 --- a/rebuild_PC/Makefile +++ b/rebuild_PC/Makefile @@ -1,6 +1,6 @@ .PHONY: all -CFLAGS+=--std=c99 -DUSE_16BY9=1 -O0 \ +CFLAGS+=-std=c99 -DUSE_16BY9=1 -O2 \ -I./PsyCross/include -I../include \ `${PKG_CONFIG} --cflags sdl2`