Skip to content

Commit

Permalink
Add USB & emulator config support
Browse files Browse the repository at this point in the history
  • Loading branch information
McCaulay committed Mar 8, 2023
1 parent 5e6f258 commit 0900df8
Show file tree
Hide file tree
Showing 33 changed files with 2,577 additions and 33 deletions.
2 changes: 1 addition & 1 deletion sdk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LINKFLAGS = -Wl,-z,max-page-size=0x1
CPPFLAGS = -mno-gpopt -nostartfiles -nostdlib -nodefaultlibs -ffreestanding -I$(IDIR) $(LINKFLAGS) -D$(SYSTEM)=1 -DFIRMWARE=$(FIRMWARE_NUM) -DEBOOT_VERSION=$(EBOOT_NUM)

# Files
CPPFILES = $(wildcard $(SDIR)/*.cpp $(SDIR)/*/*.cpp $(SDIR)/*/*/*.cpp $(SDIR)/*/*/*/*.cpp)
CPPFILES = $(wildcard $(SDIR)/*.cpp $(SDIR)/*/*.cpp $(SDIR)/*/*/*.cpp $(SDIR)/*/*/*/*.cpp $(SDIR)/*/*/*/*/*.cpp $(SDIR)/*/*/*/*/*/*.cpp)
OBJS = $(patsubst $(SDIR)/%.cpp, $(ODIR)/%.o, $(CPPFILES))

# Target
Expand Down
75 changes: 75 additions & 0 deletions sdk/include/arith64.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// GCC 32/64-bit integer arithmetic support for 32-bit systems that can't link
// to libgcc.

// Function prototypes and descriptions are taken from
// https://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html.

// This file may be #include'd by another file, so we try not to pollute the
// namespace and we don't import any headers.

// All functions must be resolvable by the linker and therefore can't be inline
// or static, even if they're #included into the file where they'll be used.

// For best performance we try to avoid branching. This makes the code a little
// weird in places.

// See https://github.com/glitchub/arith64 for more information.
// This software is released as-is into the public domain, as described at
// https://unlicense.org. Do whatever you like with it.

#pragma once

extern "C"
{
#define arith64_u64 unsigned long long int
#define arith64_s64 signed long long int
#define arith64_u32 unsigned int
#define arith64_s32 int

typedef union
{
arith64_u64 u64;
arith64_s64 s64;
struct
{
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
arith64_u32 hi; arith64_u32 lo;
#else
arith64_u32 lo; arith64_u32 hi;
#endif
} u32;
struct
{
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
arith64_s32 hi; arith64_s32 lo;
#else
arith64_s32 lo; arith64_s32 hi;
#endif
} s32;
} arith64_word;

// extract hi and lo 32-bit words from 64-bit value
#define arith64_hi(n) (arith64_word){.u64=n}.u32.hi
#define arith64_lo(n) (arith64_word){.u64=n}.u32.lo

// Negate a if b is negative, via invert and increment.
#define arith64_neg(a, b) (((a) ^ ((((arith64_s64)(b)) >= 0) - 1)) + (((arith64_s64)(b)) < 0))
#define arith64_abs(a) arith64_neg(a, a)

arith64_s64 __absvdi2(arith64_s64 a);
arith64_s64 __ashldi3(arith64_s64 a, int b);
arith64_s64 __ashrdi3(arith64_s64 a, int b);
int __clzsi2(arith64_u32 a);
int __clzdi2(arith64_u64 a);
int __ctzsi2(arith64_u32 a);
int __ctzdi2(arith64_u64 a);
arith64_u64 __divmoddi4(arith64_u64 a, arith64_u64 b, arith64_u64 *c);
arith64_s64 __divdi3(arith64_s64 a, arith64_s64 b);
int __ffsdi2(arith64_u64 a);
arith64_u64 __lshrdi3(arith64_u64 a, int b);
arith64_s64 __moddi3(arith64_s64 a, arith64_s64 b);
int __popcountsi2(arith64_u32 a);
int __popcountdi2(arith64_u64 a);
arith64_u64 __udivdi3(arith64_u64 a, arith64_u64 b);
arith64_u64 __umoddi3(arith64_u64 a, arith64_u64 b);
}
81 changes: 81 additions & 0 deletions sdk/include/common/list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include <ps2/ps2.hpp>

template <class T>
class List
{
public:
List()
{
this->arraySize = 0;
this->arrayMaxSize = 2;
this->array = (T*)PS2::malloc(this->arrayMaxSize * sizeof(T));
}

List* add(T value)
{
if (this->array == nullptr)
return this;

// Increase array size if we are out of room
if (this->arraySize >= this->arrayMaxSize)
{
uint32_t currentMemorySize = this->arrayMaxSize * sizeof(T);

// Increase max size by double
this->arrayMaxSize *= 2;

// Allocate a new space in memory
T* temp = (T*)PS2::malloc(this->arrayMaxSize * sizeof(T));

// Copy existing data to new location
PS2::memcpy(temp, this->array, currentMemorySize);

// Deallocate existing location
PS2::free(this->array);

this->array = temp;
}

// Add value to list
this->array[this->arraySize] = value;
this->arraySize++;

return this;
}

T& get(uint32_t index)
{
// if (this->array == nullptr || index >= this->arraySize)
// throw 1;
return this->array[index];
}

T& operator[](int index)
{
return this->get(index);
}

uint32_t size()
{
return this->arraySize;
}

uint32_t maxSize()
{
return this->arrayMaxSize;
}

void free()
{
PS2::free(this->array);
this->array = nullptr;
this->arraySize = 0;
this->arrayMaxSize = 0;
}
private:
T* array;
uint32_t arraySize;
uint32_t arrayMaxSize;
};
8 changes: 8 additions & 0 deletions sdk/include/mast1c0re.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

#include <types.hpp>
#include <compiler.hpp>
#include <arith64.hpp>
#include <cstdint>
#include <ps2/ps2.hpp>
#include <offsets/okage.hpp>
#include <ps2/okage.hpp>
#include <ps2/cdvd.hpp>
#include <common/list.hpp>

#if (defined(PS4) && PS4) || (defined(PS5) && PS5)
#include <offsets/ps/eboot/eboot.hpp>
#include <offsets/ps/libkernel.hpp>
#include <offsets/ps/sce/msg-dialog.hpp>
#include <ps/breakout.hpp>
#include <ps/ps.hpp>
#include <ps/memory.hpp>
#include <ps/sce/sce.hpp>
#include <ps/sce/libkernel.hpp>
#include <ps/syscall/syscall.hpp>
#include <ps/filesystem/filesystem.hpp>
#include <ps/syscall/socket.hpp>
Expand All @@ -23,4 +27,8 @@
#include <ps/sce/dialog/progress-bar.hpp>
#include <ps/sce/dialog/user-message.hpp>
#include <ps/sce/dialog/system-message.hpp>
#endif

#if (defined(PS4) && PS4)
#include <ps/sce/usb/usb/usb.hpp>
#endif
13 changes: 13 additions & 0 deletions sdk/include/offsets/ps/eboot/1.01.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
#define EBOOT_MOUNT_DISC_OPTIONS_STACK_OFFSET 0x0412AA0 // 0x7EEFFAAA0 - 0x7EEBE8000
#define EBOOT_MOUNT_DISC_GAME_CODE 0x0845a20

// Configuration File
#define EBOOT_PROCESS_CONFIG_FILE_FUNC 0x043cd70
#define EBOOT_LOAD_LUA_SCRIPTS_FUNC 0x0465a70
#define EBOOT_LUA_CONFIG_DIRECTORY 0x0856ed0
#define EBOOT_LUA_TROPHY_DIRECTORY 0x0856f00
#define EBOOT_LUA_FEATURE_DIRECTORY 0x0856f30
#define EBOOT_LUA_TOOLING_DIRECTORY 0x0856f60
#define EBOOT_LUA_LOCAL_CONFIG_FILE 0x0856fc0

// Emu Command
#define EBOOT_EMU_COMMAND 0x0845a68
#define EBOOT_EMU_COMMAND_COUNTER 0x0845a70

// Function Stubs
#define EBOOT_STRLEN_STUB 0x07635d0
#define EBOOT_EXIT_STUB 0x0763630
Expand Down
31 changes: 31 additions & 0 deletions sdk/include/ps/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ namespace PS
PS::memcpy(address, VAR_TO_NATIVE(value), sizeof(T));
}

template<typename T>
static void write(uint64_t address, uint32_t index, T value)
{
write(address + (sizeof(T) * index), value);
}

template<typename T>
static T read(uint64_t address)
{
Expand All @@ -28,12 +34,22 @@ namespace PS
return value;
}

template<typename T>
static T read(uint64_t address, uint32_t index)
{
return read<T>(address + (sizeof(T) * index));
}

static void write(uint64_t address, void* buffer, size_t n);
static void* read(uint64_t address, void* buffer, size_t n);

static char* readString(uint64_t address);

static void writeStdString(uint64_t address, const char* s);
static char* readStdString(uint64_t address);
public:
Memory(uint64_t address);
uint64_t getAddress();
PS::Memory* dereference();
PS::Memory* move(int64_t offset);

Expand All @@ -43,13 +59,28 @@ namespace PS
PS::Memory::write<T>(this->address, value);
}

template<typename T>
void write(uint32_t index, T value)
{
PS::Memory::write<T>(this->address + (sizeof(T) * index), value);
}

template<typename T>
T read()
{
return PS::Memory::read<T>(this->address);
}

template<typename T>
T read(uint32_t index)
{
return PS::Memory::read<T>(this->address + (sizeof(T) * index));
}

char* readString();

void writeStdString(const char* s);
char* readStdString();
private:
uint64_t address;
};
Expand Down
58 changes: 57 additions & 1 deletion sdk/include/ps/ps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ struct kevent
void* udata; /* opaque user data identifier */
};

#define uxMsg_Snapshot_Save 0x1f4
#define uxMsg_Snapshot_SaveStamped 0x1f5
#define uxMsg_Snapshot_Restore 0x1f6
#define uxMsg_Snapshot_SaveCyclic 0x1f7
#define uxMsg_ResetJIT_EE 0x1fe
#define uxMsg_ResetJIT_IOP 0x1ff
#define uxMsg_ResetJIT_VU 0x200
#define uxMsg_ExitNicely 0x208
#define uxMsg_StopExec 0x212
#define uxMsg_StartExec 0x213
#define uxMsg_ToggleExec 0x214
#define uxMsg_StepExec 0x215
#define uxMsg_EnableToolingMode 0x21c
#define uxMsg_GenCoreDump 0x221
#define uxMsg_GsExternalCommand 0x226
#define uxMsg_RestorePoint_Save 0x22b
#define uxMsg_RestorePoint_Restore 0x22c
#define uxMsg_StartVKLogging 0x230
#define uxMsg_StopVKLogging 0x231
#define uxMsg_SoftReset 0x23a
#define uxMsg_SwitchDisc 0x23b
#define uxMsg_SwitchDiscSwitch 0x23c
#define uxMsg_SwitchDiscClose 0x23d
#define uxMsg_ReloadLuaScripts 0x23e

namespace PS
{
#ifdef LIBKERNEL
Expand Down Expand Up @@ -82,10 +107,41 @@ namespace PS
uint32_t getpid();

int32_t PadSetLightBar(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);

void MountDisc(uint64_t mountDiscOptions);
char* GetMountedGameCode();
void SetMountOptionFilepath(const char* filepath);
void MountDiscWithFilepath(const char* filepath);
char* GetMountedGameCode();

void ProcessConfigFile(const char* filepath);
// void ReloadLuaScriptsWithDirectory(const char* directory);

// Emu Commands
void EmuSendCommand(uint32_t emuMsgId, uint64_t arg = 0);
void Snapshot_Save(uint64_t arg = 0);
void Snapshot_SaveStamped(uint64_t arg = 0);
void Snapshot_Restore(uint64_t arg = 0);
void Snapshot_SaveCyclic(uint64_t arg = 0);
void ResetJIT_EE(uint64_t arg = 0);
void ResetJIT_IOP(uint64_t arg = 0);
void ResetJIT_VU(uint64_t arg = 0);
void ExitNicely(uint64_t arg = 0);
void StopExec(uint64_t arg = 0);
void StartExec(uint64_t arg = 0);
void ToggleExec(uint64_t arg = 0);
void StepExec(uint64_t arg = 0);
void EnableToolingMode(uint64_t arg = 0);
void GenCoreDump(uint64_t arg = 0);
void GsExternalCommand(uint64_t arg = 0);
void RestorePoint_Save(uint64_t arg = 0);
void RestorePoint_Restore(uint64_t arg = 0);
void StartVKLogging(uint64_t arg = 0);
void StopVKLogging(uint64_t arg = 0);
void SoftReset(uint64_t arg = 0);
void SwitchDisc(uint64_t arg = 0);
void SwitchDiscSwitch(uint64_t arg = 0);
void SwitchDiscClose(uint64_t arg = 0);
void ReloadLuaScripts(uint64_t arg = 0);

int32_t readAll(int32_t fd, void* buf, size_t len);
size_t writeAll(int32_t fd, void* buf, size_t len);
Expand Down
Loading

0 comments on commit 0900df8

Please sign in to comment.