Skip to content

Commit

Permalink
Stole a metric ton of JKernel from TWW
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrkol committed Dec 27, 2024
1 parent abf5a73 commit 265bb1e
Show file tree
Hide file tree
Showing 16 changed files with 2,120 additions and 12 deletions.
15 changes: 10 additions & 5 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,21 @@ def MatchingFor(*versions):

# JKernel
Object(Matching, "JSystem/JKernel/JKRAramHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRAram.cpp"),
Object(Matching, "JSystem/JKernel/JKRDisposer.cpp"),
Object(Matching, "JSystem/JKernel/JKRHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRDvdFile.cpp"),
Object(Matching, "JSystem/JKernel/JKRExpHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRStdHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRSolidHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRFileFinder.cpp"),
Object(Matching, "JSystem/JKernel/JKRFileLoader.cpp"),
Object(Matching, "JSystem/JKernel/JKRHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRSolidHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRStdHeap.cpp"),
Object(Matching, "JSystem/JKernel/JKRThread.cpp"),
Object(Matching, "JSystem/JKernel/JKRFileFinder.cpp"),
Object(Matching, "JSystem/JKernel/JKRDvdFile.cpp"),
Object(Matching, "JSystem/JKernel/JKRDvdRipper.cpp"),
Object(Matching, "JSystem/JKernel/JKRDecomp.cpp"),
Object(Matching, "JSystem/JKernel/JKRAramBlock.cpp"),
Object(Matching, "JSystem/JKernel/JKRAramPiece.cpp"),
Object(Matching, "JSystem/JKernel/JKRAramStream.cpp"),

# JSupport
Object(Matching, "JSystem/JSupport/JSUInputStream.cpp"),
Expand Down
113 changes: 113 additions & 0 deletions include/JSystem/JKernel/JKRAram.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,119 @@
#ifndef JKR_ARAM_HPP
#define JKR_ARAM_HPP

#include <JSystem/JKernel/JKRAramHeap.hpp>
#include <JSystem/JKernel/JKRThread.hpp>
#include <JSystem/JKernel/JKREnum.hpp>

class JKRHeap;
class JKRAMCommand;
class JKRAramBlock;
class JKRAram : public JKRThread {
private:
JKRAram(u32, u32, s32);
virtual ~JKRAram();

/* vt[03] */ void* run(void); /* override */

public:
u32 getAudioMemory() const { return mAudioMemoryPtr; }
u32 getAudioMemSize() const { return mAudioMemorySize; }
u32 getGraphMemory() const { return mGraphMemoryPtr; }
u32 getGraphMemSize() const { return mGraphMemorySize; }
// private:
/* 0x00 */ // vtable
/* 0x04 */ // JKRThread
/* 0x68 */ u32 mAudioMemoryPtr;
/* 0x6C */ u32 mAudioMemorySize;
/* 0x70 */ u32 mGraphMemoryPtr;
/* 0x74 */ u32 mGraphMemorySize;
/* 0x78 */ u32 mAramMemoryPtr;
/* 0x7C */ u32 mAramMemorySize;
/* 0x80 */ JKRAramHeap* mAramHeap;
/* 0x84 */ u32 mStackArray[3];

public:
static JKRAram* create(u32, u32, s32, s32, s32);
static bool checkOkAddress(u8*, u32, JKRAramBlock*, u32);
static void changeGroupIdIfNeed(u8*, int);
static JKRAramBlock* mainRamToAram(u8*, u32, u32, JKRExpandSwitch, u32,
JKRHeap*, int);
static JKRAramBlock* mainRamToAram(u8*, JKRAramBlock*, u32, JKRExpandSwitch,
u32, JKRHeap*, int);
static u8* aramToMainRam(u32, u8*, u32, JKRExpandSwitch, u32, JKRHeap*, int,
u32*);
static u8* aramToMainRam(JKRAramBlock*, u8*, u32, u32, JKRExpandSwitch, u32,
JKRHeap*, int, u32*);
static void dump(void);

static JKRAram* getManager() { return sAramObject; }
static JKRAramHeap* getAramHeap() { return getManager()->mAramHeap; }
static JSUList<JKRAMCommand>& getCommandList() { return sAramCommandList; }

static u8 decideAramGroupId(int groupId)
{
JKRAramHeap* heap;
u8 finalGroupId;

if (groupId < 0) {
return getAramHeap()->getCurrentGroupID();
}

return (u8)groupId;
}

static u32 getSzpBufferSize() { return szpBufferSize; }
static void setSzpBufferSize(u32 size) { szpBufferSize = size; }

static OSMessageQueue sMessageQueue;

private:
static JKRAram* sAramObject;
static u32 szpBufferSize;
static OSMessage sMessageBuffer[4];
static JSUList<JKRAMCommand> sAramCommandList;
};

inline JKRAramBlock* JKRAllocFromAram(u32 size,
JKRAramHeap::EAllocMode allocMode)
{
return JKRAram::getAramHeap()->alloc(size, allocMode);
}

inline void JKRFreeToAram(JKRAramBlock* block)
{
JKRAram::getAramHeap()->free(block);
}

inline u8* JKRAramToMainRam(u32 p1, u8* p2, u32 p3, JKRExpandSwitch p4, u32 p5,
JKRHeap* p6, int p7, u32* p8)
{
return JKRAram::aramToMainRam(p1, p2, p3, p4, p5, p6, p7, p8);
}

inline u8*
JKRAramToMainRam(JKRAramBlock* block, u8* dst, u32 size, u32 p4 = 0,
JKRExpandSwitch expandSwitch = EXPAND_SWITCH_DEFAULT,
u32 p5 = 0, JKRHeap* heap = nullptr, int p6 = -1, u32* p7 = 0)
{
return JKRAram::aramToMainRam(block, dst, size, p4, expandSwitch, p5, heap,
p6, p7);
}

inline JKRAramBlock* JKRMainRamToAram(u8* buf, u32 bufSize, u32 alignedSize,
JKRExpandSwitch expandSwitch,
u32 fileSize, JKRHeap* heap, int id)
{
return JKRAram::mainRamToAram(buf, bufSize, alignedSize, expandSwitch,
fileSize, heap, id);
}

inline JKRAramBlock* JKRMainRamToAram(u8* buf, JKRAramBlock* block, u32 size,
JKRExpandSwitch expandSwitch,
u32 fileSize, JKRHeap* heap, int id)
{
return JKRAram::mainRamToAram(buf, block, size, expandSwitch, fileSize,
heap, id);
}

#endif
76 changes: 76 additions & 0 deletions include/JSystem/JKernel/JKRAramPiece.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef JKR_ARAM_PIECE_HPP
#define JKR_ARAM_PIECE_HPP

#include <JSystem/JSupport/JSUList.hpp>
#include <dolphin/ar.h>
#include <dolphin/os/OSMessage.h>
#include <dolphin/os/OSMutex.h>

class JKRAramBlock;
class JKRDecompCommand;
class JKRAMCommand {
public:
typedef void (*AsyncCallback)(u32);

JKRAMCommand();
~JKRAMCommand();

public:
/* 0x00 */ ARQRequest mRequest;
/* 0x20 */ JSULink<JKRAMCommand> mPieceLink;
/* 0x30 */ JSULink<JKRAMCommand> field_0x30;

/* 0x40 */ s32 mTransferDirection;
/* 0x44 */ u32 mDataLength;
/* 0x48 */ u32 mSrc;
/* 0x4C */ u32 mDst;
/* 0x50 */ JKRAramBlock* mAramBlock;
/* 0x54 */ u32 field_0x54;
/* 0x58 */ AsyncCallback mCallback;
/* 0x5C */ OSMessageQueue* field_0x5C;
/* 0x60 */ s32 field_0x60;
/* 0x64 */ JKRDecompCommand* mDecompCommand;
/* 0x68 */ OSMessageQueue mMessageQueue;
/* 0x88 */ OSMessage mMessage;
/* 0x8C */ void* field_0x8C;
/* 0x90 */ void* field_0x90;
/* 0x94 */ void* field_0x94;
};

class JKRAramPiece {
public:
static OSMutex mMutex;
// TODO: fix type
static JSUList<JKRAMCommand> sAramPieceCommandList;

public:
struct Message {
s32 field_0x00;
JKRAMCommand* command;
};

public:
static JKRAMCommand* prepareCommand(int, u32, u32, u32, JKRAramBlock*,
JKRAMCommand::AsyncCallback);
static void sendCommand(JKRAMCommand*);

static JKRAMCommand* orderAsync(int, u32, u32, u32, JKRAramBlock*,
JKRAMCommand::AsyncCallback);
static BOOL sync(JKRAMCommand*, int);
static BOOL orderSync(int, u32, u32, u32, JKRAramBlock*);
static void startDMA(JKRAMCommand*);
static void doneDMA(u32);

private:
static void lock() { OSLockMutex(&mMutex); }
static void unlock() { OSUnlockMutex(&mMutex); }
};

inline BOOL JKRAramPcs(int direction, u32 source, u32 destination, u32 length,
JKRAramBlock* block)
{
return JKRAramPiece::orderSync(direction, source, destination, length,
block);
}

#endif
77 changes: 77 additions & 0 deletions include/JSystem/JKernel/JKRAramStream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef JKR_ARAM_STREAM_HPP
#define JKR_ARAM_STREAM_HPP

#include <JSystem/JKernel/JKRThread.hpp>

class JSUFileInputStream;

class JKRAramStreamCommand {
public:
enum Type {
UNKNOWN = 0,
READ = 1,
WRITE = 2,
};

JKRAramStreamCommand();

public:
/* 0x00 */ Type mType;
/* 0x04 */ u32 mAddress;
/* 0x08 */ u32 mSize;
/* 0x0C */ u32 field_0x0c;
/* 0x10 */ JSUFileInputStream* mStream;
/* 0x14 */ u32 mOffset;
/* 0x18 */ u8* mTransferBuffer;
/* 0x1C */ u32 mTransferBufferSize;
/* 0x20 */ JKRHeap* mHeap;
/* 0x24 */ bool mAllocatedTransferBuffer;
/* 0x25 */ u8 padding_0x29[3];
/* 0x28 */ u32 field_0x2c;
/* 0x2C */ OSMessageQueue mMessageQueue;
/* 0x4C */ OSMessage mMessage;
/* 0x50 */ u32 field_0x54;
/* 0x54 */ u32 field_0x58;
};

class JKRAramStream : public JKRThread {
private:
JKRAramStream(s32);
virtual ~JKRAramStream();

/* vt[03] */ void* run(void); /* override */

public:
static JKRAramStream* create(s32);

static s32 readFromAram(void);
static s32 writeToAram(JKRAramStreamCommand*);
static JKRAramStreamCommand* write_StreamToAram_Async(JSUFileInputStream*,
u32, u32, u32);
static JKRAramStreamCommand* sync(JKRAramStreamCommand*, BOOL);
static void setTransBuffer(u8*, u32, JKRHeap*);

private:
static JKRAramStream* sAramStreamObject;
static OSMessage sMessageBuffer[4];
static OSMessageQueue sMessageQueue;

static u8* transBuffer;
static u32 transSize;
static JKRHeap* transHeap;
};

inline JKRAramStream* JKRCreateAramStreamManager(s32 priority)
{
return JKRAramStream::create(priority);
}

inline JKRAramStreamCommand* JKRStreamToAram_Async(JSUFileInputStream* stream,
u32 addr, u32 size,
u32 offset,
void (*callback)(u32))
{
return JKRAramStream::write_StreamToAram_Async(stream, addr, size, offset);
}

#endif
6 changes: 3 additions & 3 deletions include/JSystem/JKernel/JKRArchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ class JKRArchive : public JKRFileLoader {
{
int compression;
if (FLAG_ON(attr, JKRARCHIVE_ATTR_COMPRESSION))
compression = JKRCOMPRESSION_NONE;
compression = JKR_COMPRESSION_NONE;
else if (!FLAG_ON(attr, JKRARCHIVE_ATTR_YAY0))
compression = JKRCOMPRESSION_YAZ0;
compression = JKR_COMPRESSION_YAZ0;
else
compression = JKRCOMPRESSION_YAY0;
compression = JKR_COMPRESSION_YAY0;

return compression;
}
Expand Down
80 changes: 80 additions & 0 deletions include/JSystem/JKernel/JKRDecomp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#ifndef JKR_DECOMP_HPP
#define JKR_DECOMP_HPP

#include <JSystem/JKernel/JKRThread.hpp>
#include <Jsystem/JKernel/JKREnum.hpp>

class JKRAMCommand;
class JKRDecompCommand {
public:
typedef void (*AsyncCallback)(u32);

JKRDecompCommand();
~JKRDecompCommand();

public:
/* 0x00 */ u32 field_0x0;
/* 0x04 */ u8* mSrcBuffer;
/* 0x08 */ u8* mDstBuffer;
/* 0x0C */ u32 mSrcLength;
/* 0x10 */ u32 mDstLength;
/* 0x14 */ AsyncCallback mCallback;
/* 0x18 */ JKRDecompCommand* mThis;
/* 0x1C */ OSMessageQueue* field_0x1c;
/* 0x20 */ s32 field_0x20;
/* 0x24 */ JKRAMCommand* mAMCommand;
/* 0x28 */ OSMessageQueue mMessageQueue;
/* 0x48 */ OSMessage mMessage;
};

#define JKRDECOMP_SYNC_BLOCKING 0
#define JKRDECOMP_SYNC_NON_BLOCKING 1

class JKRDecomp : public JKRThread {
private:
JKRDecomp(s32);
virtual ~JKRDecomp();

/* vt[03] */ virtual void* run(); /* override */

public:
static JKRDecomp* create(s32);
static JKRDecompCommand* prepareCommand(u8*, u8*, u32, u32,
JKRDecompCommand::AsyncCallback);
static void sendCommand(JKRDecompCommand*);
static bool sync(JKRDecompCommand*, int);
static JKRDecompCommand* orderAsync(u8*, u8*, u32, u32,
JKRDecompCommand::AsyncCallback);
static bool orderSync(u8*, u8*, u32, u32);
static void decode(u8*, u8*, u32, u32);
static void decodeSZP(u8*, u8*, u32, u32);
static void decodeSZS(u8*, u8*, u32, u32);
static JKRCompression checkCompressed(u8*);

static JKRDecomp* sDecompObject;
static OSMessage sMessageBuffer[4];
static OSMessageQueue sMessageQueue;
};

inline void JKRDecompress(u8* srcBuffer, u8* dstBuffer, u32 srcLength,
u32 dstLength)
{
JKRDecomp::orderSync(srcBuffer, dstBuffer, srcLength, dstLength);
}

inline JKRDecomp* JKRCreateDecompManager(s32 priority)
{
return JKRDecomp::create(priority);
}

inline JKRCompression JKRCheckCompressed(u8* pBuf)
{
return JKRDecomp::checkCompressed(pBuf);
}

inline u32 JKRDecompExpandSize(u8* pBuf)
{
return (pBuf[4] << 24) | (pBuf[5] << 16) | (pBuf[6] << 8) | pBuf[7];
}

#endif
Loading

0 comments on commit 265bb1e

Please sign in to comment.