Skip to content

Commit

Permalink
Dmamgr OK (#37)
Browse files Browse the repository at this point in the history
* dmamgr OK

* Name some bss vars

* Some notes

* format

* OS_MESG_PRI_NORMAL

* vram -> ram
  • Loading branch information
hensldm authored Dec 9, 2023
1 parent e2bf753 commit 4e8ac90
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 7 deletions.
15 changes: 14 additions & 1 deletion include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

struct Input;

typedef struct Y65430UnkStruct {
/* 0x00 */ u32 magic;
/* 0x04 */ size_t unk04;
} Y65430UnkStruct; // size >= 0x08;

#define Y65430UnkStruct_MAGIC 0x434D5052 // 'CMPR'

typedef struct MallocRecord {
/* 0x00 */ UNK_TYPE allocP;
/* 0x04 */ UNK_TYPE requestSize;
Expand Down Expand Up @@ -42,8 +49,14 @@ void func_80074C88(UNK_PTR, struct Input*, s32);
// malloc
void* func_80064DD0(u32 size);

Y65430UnkStruct* func_80064D1C(u32);

void func_80064E84(Y65430UnkStruct*);

void func_8006F560(Y65430UnkStruct*, Y65430UnkStruct*);

// dma
s32 func_8007DF0C(void* vramStart, u32 vromStart, u32 size);
void DmaMgr_RequestSync(void* ram, u32 vrom, u32 size);

// 80C50
void func_800800CC(MtxF* mfA, MtxF* mfB);
Expand Down
7 changes: 4 additions & 3 deletions include/ys64thread.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef YS64_THREAD_H
#define YS64_THREAD_H

#define Y_THREAD_ID_FAULT 2

#define Y_PRIORITY_FAULT OS_PRIORITY_APPMAX
#define Y_THREAD_ID_FAULT 2
#define Y_THREAD_ID_DMA 18

#define Y_PRIORITY_DMA 18
#define Y_PRIORITY_FAULT OS_PRIORITY_APPMAX

#endif
3 changes: 3 additions & 0 deletions linker_scripts/us/symbol_addrs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Fault_SetFramebuffer = 0x80071250; // type:func
Fault_Init = 0x8007127C; // type:func
Fault_HungUp = 0x80071380; // type:func

DmaMgr_RequestSync = 0x8007DF0C; // type:func
DmaMgr_Init = 0x8007DF74; // type:func

ViMode_Init = 0x8007EA54;

gfxprint_setup = 0x8007EF00; // type:func
Expand Down
2 changes: 1 addition & 1 deletion src/main/O2/loadfragment2.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ size_t Overlay_Load(uintptr_t vromStart, uintptr_t vromEnd, uintptr_t vramStart,
}

end = (void*)((uintptr_t)allocatedRamAddr + size);
func_8007DF0C(allocatedRamAddr, vromStart, size);
DmaMgr_RequestSync(allocatedRamAddr, vromStart, size);

ovl = (OverlayRelocationSection*)((uintptr_t)end - ((s32*)end)[-1]);

Expand Down
156 changes: 156 additions & 0 deletions src/main/dmamgr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include "global.h"
#include "stack.h"
#include "stackcheck.h"
#include "ys64thread.h"

OSThread sDmaThread;
STACK(sDmaStack, 0x300);
StackEntry sDmaStackInfo;
OSMesgQueue D_8010ED10;
OSMesg D_8010ED28[10];
OSMesgQueue D_8010ED50;
OSMesg D_8010ED68[10];

typedef struct DmaRequest {
/* 0x00 */ u32 unk00;
/* 0x04 */ void* unk04;
/* 0x08 */ size_t unk08;
/* 0x0C */ void* unk0C;
/* 0x10 */ u8 unk10;
/* 0x11 */ u8 unk11;
/* 0x14 */ u32 unk14;
/* 0x18 */ void* unk18;
/* 0x1C */ size_t unk1C;
/* 0x20 */ OSIoMesg ioMsg;
/* 0x38 */ s32 pri;
/* 0x3C */ OSMesgQueue* queue;
/* 0x40 */ OSMesg msg;
} DmaRequest; // size = 0x44

void DmaMgr_Read(OSIoMesg* ioMsg, s32 pri, s32 op, uintptr_t vrom, void* ram, size_t size, OSMesgQueue* endQueue,
size_t buffSize) {
OSMesgQueue startQueue;
OSMesg msg;

osCreateMesgQueue(&startQueue, &msg, 1);
while (buffSize < size) {
osPiStartDma(ioMsg, pri, op, vrom, ram, buffSize, &startQueue);
osRecvMesg(&startQueue, NULL, OS_MESG_BLOCK);
size -= buffSize;
vrom += buffSize;
ram = (void*)((uintptr_t)ram + buffSize);
}
osPiStartDma(ioMsg, pri, op, vrom, ram, size, endQueue);
}

// DmaMgr_Uncompress?
// CMPR = LZW compression ?
s32 func_8007DC5C(DmaRequest* req) {
Y65430UnkStruct* temp_v0;

if (!req->unk10) {
return 0;
}

if (req->unk1C < 0x10) {
return 0;
}

temp_v0 = (Y65430UnkStruct*)req->unk18;
req->unk10 = false;
if (temp_v0->magic == Y65430UnkStruct_MAGIC) {
req->unk11 = 1;
req->unk14 += 0x10;
req->unk1C = temp_v0->unk04;
if (req->unk0C != NULL) {
req->unk18 = req->unk0C;
} else {
req->unk18 = func_80064D1C(req->unk1C);
if (req->unk18 == NULL) {
return -1;
}
}
} else {
req->unk1C = req->unk08;
}

osSendMesg(&D_8010ED50, (OSMesg)req, OS_MESG_BLOCK);
osInvalDCache(req->unk18, req->unk1C);

(void)"osPiStartDma(%08x, %08x, OS_READ, %08x, %08x, %08x, %08x)\n";

DmaMgr_Read(&req->ioMsg, req->pri, OS_READ, req->unk14, req->unk18, req->unk1C, &D_8010ED10, 0x1000);
return 1;
}

void DmaMgr_ThreadEntry(UNUSED void* arg) {
DmaRequest* req;
OSMesg msg;

// "DMA manager thread execution start"
(void)"DMAマネージャスレッド実行開始\n";

while (true) {
osRecvMesg(&D_8010ED10, &msg, OS_MESG_BLOCK);
osRecvMesg(&D_8010ED50, &msg, OS_MESG_BLOCK);

req = (DmaRequest*)msg;
if (func_8007DC5C(req) != 0) {
continue;
}

if (req->unk11 != 0) {
func_8006F560(req->unk18, req->unk04);
if (req->unk0C == NULL) {
func_80064E84(req->unk18);
}
}
if (req->queue != NULL) {
osSendMesg(req->queue, req->msg, OS_MESG_NOBLOCK);
}
}
}

void DmaMgr_SendRequest(DmaRequest* req, void* ram, u32 vrom, size_t size, void* arg4, OSMesgQueue* queue, OSMesg msg) {
req->unk00 = vrom;
req->unk04 = ram;
req->unk08 = size;
req->unk0C = arg4;
req->pri = OS_MESG_PRI_NORMAL;
req->queue = queue;
req->msg = msg;
req->unk11 = 0;
req->unk14 = vrom;
req->unk18 = ram;
if (size < 0x10) {
req->unk10 = false;
req->unk1C = size;
} else {
req->unk10 = true;
req->unk1C = 0x10;
}

osSendMesg(&D_8010ED50, (OSMesg)req, OS_MESG_BLOCK);
osInvalDCache(req->unk04, req->unk1C);

(void)"osPiStartDma(%08x, OS_MESG_PRI_HIGH, OS_READ, %08x, %08x, %08x, %08x)\n";
DmaMgr_Read(&req->ioMsg, OS_MESG_PRI_HIGH, OS_READ, req->unk14, req->unk18, req->unk1C, &D_8010ED10, req->unk1C);
}

void DmaMgr_RequestSync(void* ram, u32 vrom, u32 size) {
DmaRequest req;
OSMesgQueue queue;
OSMesg msg;

osCreateMesgQueue(&queue, &msg, 1);
DmaMgr_SendRequest(&req, ram, vrom, size, NULL, &queue, NULL);
osRecvMesg(&queue, NULL, OS_MESG_BLOCK);
}

void DmaMgr_Init(void) {
osCreateMesgQueue(&D_8010ED10, D_8010ED28, ARRAY_COUNT(D_8010ED28));
osCreateMesgQueue(&D_8010ED50, D_8010ED68, ARRAY_COUNT(D_8010ED68));
StackCheck_Init(&sDmaStackInfo, sDmaStack, STACK_TOP(sDmaStack), 0, 0x100, "dmamgr");
osCreateThread(&sDmaThread, Y_THREAD_ID_DMA, DmaMgr_ThreadEntry, NULL, &sDmaStackInfo, Y_PRIORITY_DMA);
osStartThread(&sDmaThread);
}
5 changes: 3 additions & 2 deletions yamls/us/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
- [0x7D9A0, asm]
- [0x7E140, asm]
- [0x7E1E0, asm]
- [0x7E760, asm] # Has dmamgr string in StackCheck_Init
- [0x7E760, c, dmamgr]
- [0x7EC30, asm]
- [0x7EE00, asm]
- [0x7EFD0, c, 7EFD0]
Expand Down Expand Up @@ -346,7 +346,7 @@
- [0xB3690, rodata, 7CC30]
- [0xB36A0, rodata, 7D140]
- [0xB36E0, rodata, 7D9A0]
- [0xB39F0, rodata, 7E760]
- [0xB39F0, .rodata, dmamgr]
- [0xB3AA0, rodata, 7EC30]
- [0xB3B10, rodata, 7EE00]
- [0xB3B20, .rodata, 7EFD0]
Expand Down Expand Up @@ -377,6 +377,7 @@
- {start: 0xB58B0, type: bss, vram: 0x800B4CB0, name: 800B4CB0}
- {start: 0xB58B0, type: .bss, vram: 0x80108630, name: fault}
- {start: 0xB58B0, type: bss, vram: 0x801090D0, name: 801090D0}
- {start: 0xB58B0, type: .bss, vram: 0x8010E840, name: dmamgr}
- {start: 0xB58B0, type: .bss, vram: 0x8010ED90, name: 7EFD0}

# Libultra
Expand Down

0 comments on commit 4e8ac90

Please sign in to comment.