Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PrimeAPI/include/PrimeAPI.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PRIMEAPI_H
#define PRIMEAPI_H

#include <gccore.h>
#include "gccore.h"

// Add this macro at the beginning of _prolog
#define MODULE_INIT \
Expand Down
53 changes: 40 additions & 13 deletions PrimeAPI/include/rstl/rc_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,51 @@ RSTL_BEGIN

/** Reference counting pointer. Needs to be fixed so it actually counts references */
template<typename T>
class rc_ptr
class rc_ptr_prime1
{
T** mpRawPtr;
struct rc_data
{
T* ptr;
int refCount;
};
rc_data* data;

public:
inline T* RawPointer() const {
if (mpRawPtr == nullptr) {
return nullptr;
}
return *mpRawPtr;
}
inline T& operator*() { return **mpRawPtr; }
inline const T& operator*() const { return **mpRawPtr; }
inline T& operator->() { return **mpRawPtr; }
inline const T& operator->() const { return **mpRawPtr; }
inline operator bool() const { return mpRawPtr != NULL && *mpRawPtr != NULL; }
inline T* RawPointer() const {
if (data) {
return data->ptr;
}
return nullptr;
}
inline T& operator*() { return *data->ptr; }
inline const T& operator*() const { return *data->ptr; }
inline T* operator->() { return data->ptr; }
inline const T* operator->() const { return data->ptr; }
inline operator bool() const { return data != nullptr && data->ptr != nullptr; }
};

template<typename T>
class rc_ptr_prime2
{
T* rawPtr;
int* refCount;

public:
inline T* RawPointer() const { return rawPtr; }
inline T& operator*() { return *rawPtr; }
inline const T& operator*() const { return *rawPtr; }
inline T* operator->() { return rawPtr; }
inline const T* operator->() const { return rawPtr; }
inline operator bool() const { return rawPtr != nullptr; }
};


#if PRIME == 1
#define rc_ptr rc_ptr_prime1
#elif PRIME == 2
#define rc_ptr rc_ptr_prime2
#endif

RSTL_END

#endif
132 changes: 69 additions & 63 deletions PrimeAPI2/PrimeAPI.cmake
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@

set(PRIMEAPI2_PATH "${CMAKE_CURRENT_LIST_DIR}")

if (NOT DEFINED LLVM_DIR)
message(FATAL_ERROR "Must specify LLVM_DIR")
endif()
set(CMAKE_TOOLCHAIN_FILE "${PRIMEAPI2_PATH}/PrimeToolchain.cmake")

set(DEVKITPRO "/opt/devkitpro")
set(DEVKITPPC "/opt/devkitpro/devkitPPC")

#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdlib")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib")
set(DEVKIT_PATHS
"${DEVKITPRO}/libogc/include"
"${DEVKITPPC}/lib/gcc/powerpc-eabi/10.2.0/include"
"${DEVKITPPC}/powerpc-eabi/include"
)

foreach(P IN LISTS DEVKIT_PATHS)
if(NOT EXISTS "${P}")
message(FATAL_ERROR "${P} does not exist")
endif()
endforeach()

set(CMAKE_PRIME_C_FLAGS_LIST
-target powerpc-unknown-eabi
-mllvm --relocation-model=static
-nostdlib
-nostdinc
-ffreestanding
-isystem "${DEVKITPPC}/lib/gcc/powerpc-eabi/10.2.0/include"
-isystem "${DEVKITPPC}/powerpc-eabi/include"
-fno-function-sections
-fno-data-sections
-fno-exceptions
-fno-asynchronous-unwind-tables
# -fPIC
-fvisibility=hidden
-flto=thin
# -mno-sdata
)

set(CMAKE_PRIME_CXX_FLAGS_LIST
Expand All @@ -34,25 +43,13 @@ set(CMAKE_PRIME_CXX_FLAGS_LIST

set(CMAKE_PRIME_LINK_FLAGS_LIST
-nostdlib
# -nodefaultlibs
# -flto
# -Os
# -lgcc
# -lsysbase
# -d
# -x
# "-z nocopyreloc"
# "-z combreloc"
# -call_shared
# --strip-discarded
--gc-sections
"-e __rel_prolog"
"--unresolved-symbols=report-all"
--error-unresolved-symbols
--no-allow-shlib-undefined
--no-undefined
-r
# -shared
"-T ${PRIMEAPI2_PATH}/eppc.ld"
)

Expand All @@ -63,86 +60,95 @@ list(JOIN CMAKE_PRIME_LINK_FLAGS_LIST " " CMAKE_PRIME_LINK_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_PRIME_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_PRIME_CXX_FLAGS}")

include_directories("${DEVKITPRO}/libogc/include/")

# Macro to get the required link arguments in place
macro(add_prime_library name symbol_list base_dol)
macro(add_prime_library_common name symbol_list base_dol)
add_executable(${name} ${ARGN}
"${CMAKE_CURRENT_BINARY_DIR}/ApplyCodePatches.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/dol_symbols.o"
"${CMAKE_CURRENT_BINARY_DIR}/ApplyCodePatches-${name}.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/dol_symbols-${name}.o"
)
set_target_properties(${name} PROPERTIES LINK_FLAGS
"${CMAKE_PRIME_LINK_FLAGS} -Map ${CMAKE_CURRENT_BINARY_DIR}/${name}.map"
"${CMAKE_PRIME_LINK_FLAGS} -Map ${CMAKE_CURRENT_BINARY_DIR}/${name}.map"
)
target_include_directories(${name} SYSTEM PUBLIC ${DEVKIT_PATHS})

# Create the ApplyCodePatches.cpp
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/ApplyCodePatches.cpp"
COMMAND python3 "parse_and_generate_patch.py"
-i "${CMAKE_CURRENT_SOURCE_DIR}/${base_dol}"
-o "${CMAKE_CURRENT_BINARY_DIR}/ApplyCodePatches.cpp"
${PRIME_PATCH_FUNCTIONS}
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/python/"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/ApplyCodePatches-${name}.cpp"
COMMAND python3 "parse_and_generate_patch.py"
-i "${CMAKE_CURRENT_SOURCE_DIR}/${base_dol}"
-o "${CMAKE_CURRENT_BINARY_DIR}/ApplyCodePatches-${name}.cpp"
${PRIME_PATCH_FUNCTIONS}
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/python/"
)

# Create the dol_symbols.o
get_filename_component(absolute_symbol_list "${symbol_list}" REALPATH)

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/dol_symbols.o"
COMMAND python3 "symbols_to_yaml2obj_file.py"
--llvm-dir "${LLVM_DIR}"
"${absolute_symbol_list}"
"${CMAKE_CURRENT_BINARY_DIR}/dol_symbols.o"
MAIN_DEPENDENCY "${absolute_symbol_list}"
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/python/"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/dol_symbols-${name}.o"
COMMAND python3 "symbols_to_yaml2obj_file.py"
--llvm-dir "${LLVM_DIR}"
"${absolute_symbol_list}"
"${CMAKE_CURRENT_BINARY_DIR}/dol_symbols-${name}.o"
MAIN_DEPENDENCY "${absolute_symbol_list}"
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/python/"
)

# Create the patched dol
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/default_mod.dol"
COMMAND python3 "patch_dol_file.py"
-i "${CMAKE_CURRENT_SOURCE_DIR}/${base_dol}"
-o "${CMAKE_CURRENT_BINARY_DIR}/default_mod.dol"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${base_dol}"
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/python/"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${name}-default-mod.dol"
COMMAND python3 "patch_dol_file.py"
-i "${CMAKE_CURRENT_SOURCE_DIR}/${base_dol}"
-o "${CMAKE_CURRENT_BINARY_DIR}/${name}-default-mod.dol"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${base_dol}"
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/python/"
)
add_custom_target(
patch_dol
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/default_mod.dol"
SOURCES "${base_dol}"
${name}-patch-dol
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${name}-default-mod.dol"
SOURCES "${base_dol}"
)
add_dependencies(${name} patch_dol)

add_dependencies(${name} ${name}-patch-dol)

# Create the Mod.rel
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/Mod.rel"
COMMAND cargo run -p dol_linker --
rel
-o "${CMAKE_CURRENT_BINARY_DIR}/Mod.rel"
-s "${PRIMEAPI2_PATH}/empty.lst"
"${CMAKE_CURRENT_BINARY_DIR}/${name}"
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/randomprime/"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${name}"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${name}-Mod.rel"
COMMAND cargo run -p dol_linker --
rel
-o "${CMAKE_CURRENT_BINARY_DIR}/${name}-Mod.rel"
-s "${PRIMEAPI2_PATH}/empty.lst"
"${CMAKE_CURRENT_BINARY_DIR}/${name}"
WORKING_DIRECTORY "${PRIMEAPI2_PATH}/randomprime/"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${name}"
)
add_custom_target(
build_mod ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/Mod.rel"
${name}-build-mod ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${name}-Mod.rel"
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/Mod.rel"
DESTINATION "files/")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/default_mod.dol"
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${name}-Mod.rel"
DESTINATION "files/"
RENAME "Mod.rel")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${name}-default-mod.dol"
DESTINATION "files/"
RENAME "default.dol")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/default_mod.dol"
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${name}-default-mod.dol"
DESTINATION "sys/"
RENAME "main.dol")
endmacro()

macro(add_prime_library name symbol_list base_dol)
add_prime_library_common("${name}" "${symbol_list}" "${base_dol}" ${ARGN})
target_compile_definitions(${name} PUBLIC -DPRIME=1)
endmacro()

macro(add_echoes_library name symbol_list base_dol)
add_prime_library_common("${name}" "${symbol_list}" "${base_dol}" ${ARGN})
target_compile_definitions(${name} PUBLIC -DPRIME=2)
endmacro()

macro(patch_function orig dest)
list(APPEND PRIME_PATCH_FUNCTIONS
-p "'${orig}'" "'${dest}'"
)
)
endmacro()
19 changes: 2 additions & 17 deletions PrimeAPI2/PrimeToolchain.cmake
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
# Hanafuda toolchain
set(CMAKE_SYSTEM_NAME powerpc-elf-prime)

if (NOT DEFINED LLVM_DIR)
message(FATAL_ERROR "Must specify LLVM_DIR")
endif()

# Set toolchain programs
set(CMAKE_C_COMPILER "${LLVM_DIR}/bin/clang")
set(CMAKE_CXX_COMPILER "${LLVM_DIR}/bin/clang++")
set(CMAKE_C_COMPILER "${LLVM_DIR}/bin/clang" CACHE FILEPATH "" FORCE)
set(CMAKE_CXX_COMPILER "${LLVM_DIR}/bin/clang++" CACHE FILEPATH "" FORCE)

# Set triple for CMake's identification
set(triple powerpc-none-unknown-elf)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER_TARGET ${triple})

# Sysroont
#set(CMAKE_SYSROOT "${CMAKE_SOURCE_DIR}")

# Skip test compile (hanafuda has a somewhat unorthodox compiler workflow)
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)

# Compile a C file into an object file
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> -c <INCLUDES> <FLAGS> -o <OBJECT> <SOURCE>")
set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> -c <INCLUDES> <FLAGS> -o <OBJECT> <SOURCE>")

# Link object files to an executable
set(CMAKE_C_LINK_EXECUTABLE "${LLVM_DIR}/bin/ld.lld <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES> -o <TARGET> <OBJECTS>")
set(CMAKE_CXX_LINK_EXECUTABLE "${LLVM_DIR}/bin/ld.lld <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES> -o <TARGET> <OBJECTS>")

# Thing that doesn't work
set(CMAKE_C_OUTPUT_EXTENSION ".o")
set(CMAKE_CXX_OUTPUT_EXTENSION ".o")
28 changes: 28 additions & 0 deletions PrimeAPI2/python/symbols/v1.028.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
0x8036fcf8 OSReport
0x8036017c DVDOpen
0x8036048c DVDReadAsyncPrio
0x80360244 DVDClose
0x8037333c OSLink
0x8035712c PPCSetFpIEEEMode
0x802ce224 __nwa__FUlPCcPCc
0x802ce278 __nw__FUlPCcPCc
0x80372f8c __OSLinkHandleREL14
0x8001de68 AdvanceGameState__9CMainFlowFR18CArchitectureQueue
0x800401d8 InitializeState__13CStateManagerFUi7TAreaIdUi
0x8005926c StartStreamIn__9CGameAreaFR13CStateManager
0x800031e8 memcpy
0x80142590 _ZN10CGameState17SetCurrentWorldIdEj
0x801425e0 _ZN10CGameState17CurrentWorldStateEv
0x80144f70 _ZN11CWorldState21SetDesiredAreaAssetIdEj
0x8001db54 _ZN9CMainFlow12SetGameStateE17EClientFlowStatesR18CArchitectureQueue
0x8001de68 _ZN9CMainFlow16AdvanceGameStateER18CArchitectureQueue
0x800401d8 _ZN13CStateManager15InitializeStateEj7TAreaIdj
0x80186534 _ZN7CPlayer8TeleportERK12CTransform4fR13CStateManagerb
0x800859d8 _ZN12CPlayerState19ReInitializePowerUpENS_9EItemTypeEi
0x80085760 _ZN12CPlayerState10IncrPickUpENS_9EItemTypeEi
0x8005926c _ZN9CGameArea13StartStreamInER13CStateManager
0x8004ebe4 _ZN6CWorld12AuxGetAreaIdEP7TAreaIdPS_j
0x80144f68 _ZN11CWorldState13GetLayerStateEv
0x80418eb8 g_GameState
0x803a7288 gkPowerUpMaxValues
0x803a743c gkPowerUpShouldPersist
16 changes: 16 additions & 0 deletions src/include/VersionSelector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef PRIMEAPI_GETFIELD_HPP
#define PRIMEAPI_GETFIELD_HPP

#include "types.h"

constexpr inline u32 ByVersion(u32 prime1, u32 prime2) {
static_assert(1 <= PRIME && PRIME <= 2, "Unknown game");

if constexpr (PRIME == 1) {
return prime1;
} else {
return prime2;
}
}

#endif
2 changes: 2 additions & 0 deletions src/include/prime/CGameState.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CGAMESTATE_HPP
#define CGAMESTATE_HPP

#include "types.h"

class CWorldState;

class CGameState {
Expand Down
Loading