Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the STR RPC to overlord and game code #134

Merged
merged 8 commits into from
Nov 22, 2020
Merged
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
8 changes: 8 additions & 0 deletions common/util/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,12 @@ void MakeISOName(char* dst, const char* src) {
}
*dst_ptr = 0;
}

void assert_file_exists(const char* path, const char* error_message) {
if (!std::filesystem::exists(path)) {
fprintf(stderr, "File %s was not found: %s\n", path, error_message);
assert(false);
}
}

} // namespace file_util
1 change: 1 addition & 0 deletions common/util/FileUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ uint32_t crc32(const uint8_t* data, size_t size);
uint32_t crc32(const std::vector<uint8_t>& data);
void MakeISOName(char* dst, const char* src);
void ISONameFromAnimationName(char* dst, const char* src);
void assert_file_exists(const char* path, const char* error_message);
} // namespace file_util
123 changes: 64 additions & 59 deletions decompiler/config/all-types.gc
Original file line number Diff line number Diff line change
Expand Up @@ -10311,39 +10311,43 @@
; )
; )

; ;; rpc-h
; (deftype rpc-buffer (basic)
; ((elt-size uint32 :offset-assert 4)
; (elt-count uint32 :offset-assert 8)
; (elt-used uint32 :offset-assert 12)
; (busy basic :offset-assert 16)
; (base uint32 :offset-assert 20)
; (data UNKNOWN :dynamic :offset-assert 32)
; )
; :method-count-assert 9
; :size-assert #x20
; :flag-assert #x900000020
; )
;; rpc-h
(deftype rpc-buffer (basic)
((elt-size uint32 :offset-assert 4)
(elt-count uint32 :offset-assert 8)
(elt-used uint32 :offset-assert 12)
(busy basic :offset-assert 16)
(base pointer :offset-assert 20)
(data uint8 :dynamic :offset 32)
)
:method-count-assert 9
:size-assert #x20
:flag-assert #x900000020
(:methods
(new (symbol type uint uint) rpc-buffer 0)
)
)

; ;; rpc-h
; (deftype rpc-buffer-pair (basic)
; ((buffer UNKNOWN 2 :offset-assert 4)
; (current basic :offset-assert 12)
; (last-recv-buffer uint32 :offset-assert 16)
; (rpc-port int32 :offset-assert 20)
; )
; :method-count-assert 15
; :size-assert #x18
; :flag-assert #xf00000018
; (:methods
; (dummy-9 () none 9)
; (dummy-10 () none 10)
; (dummy-11 () none 11)
; (dummy-12 () none 12)
; (dummy-13 () none 13)
; (dummy-14 () none 14)
; )
; )
;; rpc-h
(deftype rpc-buffer-pair (basic)
((buffer rpc-buffer 2 :offset-assert 4)
(current rpc-buffer :offset-assert 12)
(last-recv-buffer pointer :offset-assert 16)
(rpc-port int32 :offset-assert 20)
)
:method-count-assert 15
:size-assert #x18
:flag-assert #xf00000018
(:methods
(new (symbol type uint uint int) rpc-buffer-pair 0)
(call (rpc-buffer-pair uint pointer uint) int 9)
(add-element (rpc-buffer-pair) pointer 10)
(decrement-elt-used (rpc-buffer-pair) int 11)
(sync (rpc-buffer-pair symbol) int 12)
(check-busy (rpc-buffer-pair) symbol 13)
(pop-last-received (rpc-buffer-pair) pointer 14)
)
)

; ;; path-h
; (deftype path-control (basic)
Expand Down Expand Up @@ -10659,38 +10663,39 @@
(b1 uint32 :offset-assert 4)
(b2 uint32 :offset-assert 8)
(bt uint32 :offset-assert 12)
(name uint128 :offset-assert 16)
;(name uint128 :offset-assert 16)
(name uint8 16 :offset-assert 16)
(address uint32 :offset 4)
)
:method-count-assert 9
:size-assert #x20
:flag-assert #x900000020
)

; ;; load-dgo
; (deftype load-chunk-msg (structure)
; ((rsvd uint16 :offset-assert 0)
; (result uint16 :offset-assert 2)
; (address uint32 :offset-assert 4)
; (section uint32 :offset-assert 8)
; (maxlen uint32 :offset-assert 12)
; (id uint32 :offset-assert 4)
; (basename UNKNOWN 48 :offset-assert 16)
; )
; :method-count-assert 9
; :size-assert #x40
; :flag-assert #x900000040
; )
;; load-dgo
(deftype load-chunk-msg (structure)
((rsvd uint16 :offset-assert 0)
(result uint16 :offset-assert 2)
(address uint32 :offset-assert 4)
(section uint32 :offset-assert 8)
(maxlen uint32 :offset-assert 12)
(id uint32 :offset 4)
(basename uint8 48 :offset-assert 16)
)
:method-count-assert 9
:size-assert #x40
:flag-assert #x900000040
)

; ;; load-dgo
; (deftype dgo-header (structure)
; ((length uint32 :offset-assert 0)
; (rootname UNKNOWN 60 :offset-assert 4)
; )
; :method-count-assert 9
; :size-assert #x40
; :flag-assert #x900000040
; )
;; load-dgo
(deftype dgo-header (structure)
((length uint32 :offset-assert 0)
(rootname uint8 60 :offset-assert 4)
)
:method-count-assert 9
:size-assert #x40
:flag-assert #x900000040
)

; ;; ramdisk
; (deftype ramdisk-rpc-fill (structure)
Expand Down Expand Up @@ -31160,7 +31165,7 @@
(define-extern string-strip-whitespace! function)
(define-extern string<? function)
(define-extern string-get-flag!! function)
(define-extern charp<-string function)
(define-extern charp<-string (function (pointer uint8) string int))
(define-extern string>=? function)
(define-extern string-charp= function)
(define-extern string->float function)
Expand Down Expand Up @@ -32848,13 +32853,13 @@
(define-extern dgo-load-begin function)
(define-extern dgo-load-continue function)
(define-extern destroy-mem function)
(define-extern str-load function)
(define-extern str-load (function string int pointer int symbol))
;;(define-extern *load-str-rpc* object) ;; unknown type
;;(define-extern load-chunk-msg object) ;; unknown type
;;(define-extern *dgo-name* object) ;; unknown type
(define-extern str-ambient-play function)
;;(define-extern *load-str-lock* object) ;; unknown type
(define-extern str-load-status function)
(define-extern str-load-status (function (pointer int32) symbol))
(define-extern str-load-cancel function)
(define-extern str-play-queue function)
(define-extern str-ambient-stop function)
Expand Down
16 changes: 3 additions & 13 deletions decompiler/data/StrFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,17 @@
#include <cassert>
#include <cstring>
#include "common/util/FileUtil.h"
#include "game/overlord/isocommon.h"
#include "game/common/overlord_common.h"
#include "game/common/str_rpc_types.h"
#include "StrFileReader.h"

// up to 64 chunks per STR file.
constexpr int SECTOR_TABLE_SIZE = 64;

// there is a 1 sector long header
struct StrFileHeader {
u32 sectors[SECTOR_TABLE_SIZE]; // start of chunk, in sectors. including this sector.
u32 sizes[SECTOR_TABLE_SIZE]; // size of chunk, in bytes. always an integer number of sectors.
u32 pad[512 - 128]; // all zero
};
static_assert(sizeof(StrFileHeader) == SECTOR_SIZE, "Sector header size");

StrFileReader::StrFileReader(const std::string& file_path) {
auto data = file_util::read_binary_file(file_path);
assert(data.size() >= SECTOR_SIZE); // must have at least the header sector
assert(data.size() % SECTOR_SIZE == 0); // should be multiple of the sector size.
int end_sector = int(data.size()) / SECTOR_SIZE;

auto* header = (StrFileHeader*)data.data();
auto* header = (StrFileHeaderSector*)data.data();

bool got_zero = false;
for (int i = 0; i < SECTOR_TABLE_SIZE; i++) {
Expand Down
3 changes: 3 additions & 0 deletions game/common/overlord_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

constexpr int SECTOR_SIZE = 0x800; // media sector size
37 changes: 37 additions & 0 deletions game/common/str_rpc_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "common/common_types.h"
#include "game/common/overlord_common.h"

constexpr int STR_RPC_ID = 0xdeb5;
constexpr int STR_RPC_CHANNEL = 4;

struct RPC_Str_Cmd {
u16 rsvd; // 0, seems unused
u16 result; // 2, return code. see STR_RPC_RESULT_XXX
u32 ee_addr; // 4, GOAL address to load to.
s32 chunk_id; // 8, chunk ID for chunked file. Use -1 to load a non-chunked file, which gets the
// whole file.
u32 length; // 12, length that was actually loaded
char name[64]; // file name
};

constexpr int STR_RPC_RESULT_ERROR = 1;
constexpr int STR_RPC_RESULT_DONE = 0;

// maximum number of chunks in a chunked file.
constexpr int SECTOR_TABLE_SIZE = 64;

// the header of a chunked file
struct StrFileHeader {
u32 sectors[SECTOR_TABLE_SIZE]; // start of chunk, in sectors. including this sector.
u32 sizes[SECTOR_TABLE_SIZE]; // size of chunk, in bytes. always an integer number of sectors
};

// the first sector of a chunked file.
struct StrFileHeaderSector : StrFileHeader {
u32 pad[512 - 128]; // all zero
};

static_assert(sizeof(StrFileHeader) == 0x200, "Sector header size");
static_assert(sizeof(StrFileHeaderSector) == SECTOR_SIZE, "Sector header size");
2 changes: 2 additions & 0 deletions game/fake_iso.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ TEST.CGO resources/TEST.CGO
TWEAKVAL.MUS resources/TWEAKVAL.MUS
VAGDIR.AYB resources/VAGDIR.AYB
SCREEN1.USA resources/SCREEN1.USA
0COMMON.TXT out/iso/0COMMON.TXT
0TEST.TXT out/iso/0TEST.TXT
25 changes: 24 additions & 1 deletion game/kernel/kdgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "game/common/ramdisk_rpc_types.h"
#include "game/common/loader_rpc_types.h"
#include "game/common/play_rpc_types.h"
#include "game/common/str_rpc_types.h"
#include "third-party/spdlog/include/spdlog/spdlog.h"

using namespace ee;
Expand Down Expand Up @@ -49,6 +50,27 @@ s32 RpcCall(s32 rpcChannel,
nullptr);
}

// Terrible hack! Remove soon!

namespace {
struct RpcCallArgCache {
s32 rpcChannel;
u32 fno;
u32 async;
} rpc_arg_cache;
} // namespace

void RpcCall_wrapper_part1(s32 rpcChannel, u32 fno, u32 async) {
rpc_arg_cache.rpcChannel = rpcChannel;
rpc_arg_cache.fno = fno;
rpc_arg_cache.async = async;
}

u64 RpcCall_wrapper_part2(u64 send_buff, s32 send_size, u64 recv_buff, s32 recv_size) {
return RpcCall_wrapper(rpc_arg_cache.rpcChannel, rpc_arg_cache.fno, rpc_arg_cache.async,
send_buff, send_size, recv_buff, recv_size);
}

/*!
* GOAL Wrapper for RpcCall.
*/
Expand All @@ -59,6 +81,7 @@ u64 RpcCall_wrapper(s32 rpcChannel,
s32 send_size,
u64 recv_buff,
s32 recv_size) {
fprintf(stderr, "size in c is %d\n", recv_size);
return sceSifCallRpc(&cd[rpcChannel], fno, async, Ptr<u8>(send_buff).c(), send_size,
Ptr<u8>(recv_buff).c(), recv_size, nullptr, nullptr);
}
Expand Down Expand Up @@ -126,7 +149,7 @@ u32 RpcBind(s32 channel, s32 id) {
u32 InitRPC() {
if (!RpcBind(PLAYER_RPC_CHANNEL, PLAYER_RPC_ID) && !RpcBind(LOADER_RPC_CHANNEL, LOADER_RPC_ID) &&
!RpcBind(RAMDISK_RPC_CHANNEL, RAMDISK_RPC_ID) && !RpcBind(DGO_RPC_CHANNEL, DGO_RPC_ID) &&
!RpcBind(4, 0xdeb5) && !RpcBind(PLAY_RPC_CHANNEL, PLAY_RPC_ID)) {
!RpcBind(STR_RPC_CHANNEL, STR_RPC_ID) && !RpcBind(PLAY_RPC_CHANNEL, PLAY_RPC_ID)) {
return 0;
}
printf("Entering endless loop ... please wait\n");
Expand Down
2 changes: 2 additions & 0 deletions game/kernel/kdgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ u64 RpcCall_wrapper(s32 rpcChannel,
u32 RpcBusy(s32 channel);
void LoadDGOTest();

void RpcCall_wrapper_part1(s32 rpcChannel, u32 fno, u32 async);
u64 RpcCall_wrapper_part2(u64 send_buff, s32 send_size, u64 recv_buff, s32 recv_size);
#endif // JAK_V2_KDGO_H
4 changes: 4 additions & 0 deletions game/kernel/ksound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ void InitSoundScheme() {
make_function_symbol_from_c("rpc-call", (void*)RpcCall_wrapper);
make_function_symbol_from_c("rpc-busy?", (void*)RpcBusy);
make_function_symbol_from_c("test-load-dgo-c", (void*)LoadDGOTest);

// terrible hack!
make_function_symbol_from_c("rpc-call-p1", (void*)RpcCall_wrapper_part1);
make_function_symbol_from_c("rpc-call-p2", (void*)RpcCall_wrapper_part2);
}
25 changes: 4 additions & 21 deletions game/overlord/fake_iso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,10 @@ void fake_iso_init_globals() {
int FS_Init(u8* buffer) {
(void)buffer;

// get path to next/data/fake_iso.txt, the map file.
char fakeiso_path[512];
strcpy(fakeiso_path, file_util::get_file_path({"game", "fake_iso.txt"}).c_str());

// open the map.
FILE* fp = fopen(fakeiso_path, "r");
assert(fp);
fseek(fp, 0, SEEK_END);
size_t len = ftell(fp);
rewind(fp);
char* fakeiso = (char*)malloc(len + 1);
if (fread(fakeiso, len, 1, fp) != 1) {
#ifdef __linux__
assert(false);
#endif
}
fakeiso[len] = '\0';
auto config_str = file_util::read_text_file(file_util::get_file_path({"game", "fake_iso.txt"}));
const char* ptr = config_str.c_str();

// loop over lines
char* ptr = fakeiso;
while (*ptr) {
// newlines
while (*ptr && *ptr == '\n')
Expand Down Expand Up @@ -127,7 +111,7 @@ int FS_Init(u8* buffer) {
}

i = 0;
while (*ptr && (*ptr != '\n') && (*ptr != ' ') && i < 128) {
while (*ptr && (*ptr != '\n') && (*ptr != ' ') && (*ptr != EOF) && i < 128) {
e->file_path[i] = *ptr;
ptr++;
i++;
Expand All @@ -145,8 +129,6 @@ int FS_Init(u8* buffer) {
sFiles[i].location = i;
}

free(fakeiso);

// TODO load tweak music.

return 0;
Expand Down Expand Up @@ -201,6 +183,7 @@ static const char* get_file_path(FileRecord* fr) {
*/
uint32_t FS_GetLength(FileRecord* fr) {
const char* path = get_file_path(fr);
file_util::assert_file_exists(path, "fake_iso FS_GetLength");
FILE* fp = fopen(path, "rb");
assert(fp);
fseek(fp, 0, SEEK_END);
Expand Down
Loading