Skip to content

Commit

Permalink
Merge pull request CTR-tools#14 from mateusfavarin/main
Browse files Browse the repository at this point in the history
update to redhot main
  • Loading branch information
TheUbMunster authored Sep 4, 2024
2 parents 4f14dd3 + fc16ef0 commit 19759aa
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
[submodule "externals/xdelta"]
path = externals/xdelta
url = https://github.com/jmacd/xdelta
[submodule "externals/fmtlog"]
path = externals/fmtlog
url = https://github.com/MengRao/fmtlog
8 changes: 6 additions & 2 deletions Saphi/Server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ OBJS = $(foreach src,$(SRCS),$(BUILD_DIR)/$(call hash,$(src)).o)
#========================================================

RELEASE_CXXFLAGS := -O3
DEBUG_CXXFLAGS := -g -O0
DEBUG_CXXFLAGS := -g -O0 -D_DEBUG

#========================================================
#IN-COMMON FLAGS
Expand All @@ -45,9 +45,13 @@ DEBUG_CXXFLAGS := -g -O0
COMMON_CXXFLAGS = -std=c++20 \
-pipe \
-DCLIENT_SERVER \
-DFMT_HEADER_ONLY \
-DFMTLOG_HEADER_ONLY \
-Isrc \
-I../../decompile/General/AltMods \
-I../../externals/enet/include
-I../../externals/enet/include \
-I../../externals/fmtlog/fmt/include/ \
-I../../externals/fmtlog/

#NOTE!!!

Expand Down
21 changes: 15 additions & 6 deletions Saphi/Server/Server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<VcpkgUseStatic>true</VcpkgUseStatic>
</PropertyGroup>
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<VcpkgUseStatic>true</VcpkgUseStatic>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
Expand Down Expand Up @@ -102,12 +108,14 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;CLIENT_SERVER;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;CLIENT_SERVER;FMT_HEADER_ONLY;FMTLOG_HEADER_ONLY;NOMINMAX;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\externals\enet\include\;$(ProjectDir)..\..\decompile\General\AltMods\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\externals\enet\include\;$(ProjectDir)..\..\decompile\General\AltMods\;$(ProjectDir)..\..\externals\fmtlog\;$(ProjectDir)..\..\externals\fmtlog\fmt\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<DisableSpecificWarnings>4100;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<UseUnicodeForAssemblerListing>false</UseUnicodeForAssemblerListing>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -121,12 +129,14 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;CLIENT_SERVER;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;CLIENT_SERVER;FMT_HEADER_ONLY;FMTLOG_HEADER_ONLY;NOMINMAX;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\externals\enet\include\;$(ProjectDir)..\..\decompile\General\AltMods\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\externals\enet\include\;$(ProjectDir)..\..\decompile\General\AltMods\;$(ProjectDir)..\..\externals\fmtlog\;$(ProjectDir)..\..\externals\fmtlog\fmt\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<DisableSpecificWarnings>4100;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<UseUnicodeForAssemblerListing>false</UseUnicodeForAssemblerListing>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -186,6 +196,5 @@
<ClInclude Include="src\server.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<ImportGroup Label="ExtensionTargets" />
</Project>
6 changes: 6 additions & 0 deletions Saphi/Server/src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include "server.h"
#include <fmtlog.h>

bool Server::Init()
{
#ifdef _DEBUG
fmtlog::setLogLevel(fmtlog::LogLevel::DBG);
#else
fmtlog::setLogLevel(fmtlog::LogLevel::WRN);
#endif
return m_net.Init();
}

Expand Down
1 change: 1 addition & 0 deletions externals/fmtlog
Submodule fmtlog added at acd521
130 changes: 130 additions & 0 deletions tools/mips_to_mips/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import rabbitizer
import os

PSX_RAM_BASE = 0x80010000

def get_decompiled_funcs(path: str):
symbols = {}
with open(path, "r") as file:
for line in file:
line = [l.strip() for l in line.split()]
if "DECOMP_" not in line[1]:
continue
addr = int("0x" + line[0], 16)
if addr >= PSX_RAM_BASE:
addr -= PSX_RAM_BASE
symbol = line[1][7:]
symbols[symbol] = addr
return symbols

def read_symbols(path: str, begin: int, end: int):
symbols = {}
symbols_all = {}
sorted_addr = []
if begin >= PSX_RAM_BASE:
begin -= PSX_RAM_BASE
if end >= PSX_RAM_BASE:
end -= PSX_RAM_BASE
with open(path, "r") as file:
for line in file:
line = [l.strip() for l in line.split("=") if l.strip() != '']
if len(line) == 0:
continue
if len(line) == 2:
symbol = line[0]
addr = int(line[1][:-1], 0)
if addr >= PSX_RAM_BASE:
addr -= PSX_RAM_BASE
symbols_all[addr] = symbol
if addr <= begin or addr >= end:
continue
symbols[addr] = symbol
sorted_addr.append(addr)
sorted_addr.append(end)
sorted_addr.sort()
return symbols, sorted_addr, symbols_all

def to_le(be: int):
return ((be & 0xFF) << 24) + (((be >> 8) & 0xFF) << 16) + (((be >> 16) & 0xFF) << 8) + ((be >> 24) & 0xFF)

buffer = bytearray()
with open("SCUS_944.26", "rb") as file:
buffer = file.read()[0x800:]

rabbitizer.config.toolchainTweaks_treatJAsUnconditionalBranch = True
rabbitizer.config.misc_expandJalr = True
rabbitizer.config.misc_opcodeLJust = 1

symbols, sorted_addr, symbols_all = read_symbols("gcc-syms926.txt", 0x800100cc, 0x8007170c)
decomp_funcs = get_decompiled_funcs("redux.map")

for i in range(len(sorted_addr) - 1):
func_start = sorted_addr[i]
func_end = sorted_addr[i + 1]
func = buffer[func_start : func_end]
func_name = symbols[func_start]
if func_name in decomp_funcs:
continue

labels = {}
sorted_labels = []
lbl_count = 0
for i in range(0, len(func), 4):
bytecode = (func[i]) + (func[i + 1] << 8) + (func[i + 2] << 16) + (func[i + 3] << 24 )
addr = func_start + PSX_RAM_BASE + i
inst = rabbitizer.Instruction(bytecode, addr, rabbitizer.InstrCategory.R3000GTE)
if not inst.isFunctionCall() and inst.isJumpWithAddress():
jump_addr = inst.getBranchVramGeneric()
lbl_name = "lbl_" + func_name + "_" + str(lbl_count)
if jump_addr in symbols_all:
lbl_name = decomp_funcs[jump_addr]
if lbl_name in decomp_funcs:
lbl_name = "DECOMP_" + lbl_name
sorted_labels.append(lbl_name)
continue
labels[jump_addr] = lbl_name
sorted_labels.append(lbl_name)
lbl_count += 1

jal_errors = {}
jmp_id = 0
warning_func_ptr = False
function = ".set noreorder\n.align 2\n.global " + func_name + "\n.type " + func_name + ", @function\n\n" + func_name + ":\n"
for i in range(0, len(func), 4):
bytecode = (func[i]) + (func[i + 1] << 8) + (func[i + 2] << 16) + (func[i + 3] << 24 )
addr = func_start + PSX_RAM_BASE + i
inst = rabbitizer.Instruction(bytecode, addr, rabbitizer.InstrCategory.R3000GTE)
if addr in labels:
function += labels[addr] + ":\n"
labels.pop(addr)
disassembled = False
if inst.isFunctionCall():
if inst.isJumpWithAddress():
func_addr = inst.getBranchVramGeneric() - PSX_RAM_BASE
jal_name = str()
if func_addr in symbols_all:
jal_name = symbols_all[func_addr]
if jal_name in decomp_funcs:
jal_name = "DECOMP_" + jal_name
function += "\t" + inst.disassemble(jal_name) + "\n"
else:
function += "\t" + inst.disassemble("ERROR") + "\n"
jal_addr = inst.getBranchVramGeneric()
if jal_addr not in jal_errors:
jal_errors[jal_addr] = True
print("ERROR: Function does not exist: " + str(hex(jal_addr)))
disassembled = True
else:
warning_func_ptr = True
elif inst.isJumpWithAddress():
function += "\t" + inst.disassemble(sorted_labels[jmp_id]) + "\n"
jmp_id += 1
disassembled = True
if not disassembled:
function += "\t.word " + hex(inst.getRaw()) + "\n"
if len(labels) > 0:
print("ERROR: missing labels in " + func_name)
if warning_func_ptr:
print("WARNING: " + func_name + " contains function pointer calls")
with open("src/" + func_name + ".s", "w") as file:
file.write(function)

0 comments on commit 19759aa

Please sign in to comment.