Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/lib/src/kdebugserver.c
7857 views
1
#include "libultra_internal.h"
2
3
typedef struct {
4
u8 unk00 : 2;
5
u8 pad : 4;
6
u8 unk01 : 2;
7
u8 unk2[3];
8
} unkStruct;
9
10
extern u32 D_80334A44;
11
12
u32 D_80334A30 = 0;
13
u32 D_80334A34 = 0;
14
s32 D_80334A38 = 0;
15
16
extern u8 D_80365E40[0x100];
17
18
OSThread gInterruptedThread;
19
20
void u32_to_string(u32 i, u8 *str) {
21
str[0] = (i >> 0x18) & 0xff;
22
str[1] = (i >> 0x10) & 0xff;
23
str[2] = (i >> 0x8) & 0xff;
24
str[3] = i & 0xff;
25
}
26
27
u32 string_to_u32(u8 *str) {
28
u32 i;
29
i = (str[0] & 0xff) << 0x18;
30
i |= (str[1] & 0xff) << 0x10;
31
i |= (str[2] & 0xff) << 0x8;
32
i |= (str[3] & 0xff);
33
return i;
34
}
35
36
void send_packet(u8 *a0, s32 a1) {
37
unkStruct sp1c;
38
s32 i;
39
sp1c.unk00 = 2;
40
for (sp1c.unk01 = a1, i = 0; i < a1; i++) {
41
sp1c.unk2[i] = a0[i];
42
}
43
*(volatile u32 *) 0xc0000000 = *(u32 *) &sp1c;
44
while (!(__osGetCause() & 0x2000)) {
45
;
46
}
47
*(volatile u32 *) 0xc000000c = 0;
48
}
49
50
void send(u8 *buff, s32 len) {
51
s32 i;
52
s32 end;
53
s32 rem;
54
if (!D_80334A44) {
55
while (!(__osGetCause() & 0x2000)) {
56
;
57
}
58
*(volatile u32 *) 0xc000000c = 0;
59
D_80334A44 = 1;
60
}
61
i = 0;
62
rem = len % 3;
63
end = len - rem;
64
for (; i < end; i += 3) {
65
send_packet(&buff[i], 3);
66
}
67
if (rem > 0) {
68
send_packet(&buff[end], rem);
69
}
70
}
71
72
void process_command_memory(void) {
73
u32 sp1c;
74
u32 sp18;
75
sp1c = string_to_u32(&D_80365E40[1]);
76
sp18 = string_to_u32(&D_80365E40[5]);
77
send((u8 *) (uintptr_t) sp1c, sp18);
78
}
79
80
void process_command_register(void) {
81
send((u8 *) &gInterruptedThread.context, sizeof(__OSThreadContext));
82
}
83
84
void kdebugserver(u32 a0) {
85
u32 sp2c;
86
unkStruct sp28;
87
*(u32 *) &sp28 = a0;
88
for (sp2c = 0; sp2c < sp28.unk01; sp2c++) {
89
D_80365E40[D_80334A34] = sp28.unk2[sp2c];
90
D_80334A34++;
91
}
92
D_80334A38 -= sp28.unk01;
93
switch (D_80334A30) {
94
case 0:
95
switch (sp28.unk2[0]) {
96
case 1:
97
D_80334A30 = 1;
98
D_80334A38 = 9 - sp28.unk01;
99
break;
100
case 2:
101
process_command_register();
102
D_80334A30 = 0;
103
D_80334A34 = 0;
104
D_80334A38 = 0;
105
break;
106
default:
107
D_80334A30 = 0;
108
D_80334A34 = 0;
109
D_80334A38 = 0;
110
break;
111
}
112
break;
113
case 1:
114
if (D_80334A38 <= 0) {
115
if (D_80365E40[0] == 1) {
116
process_command_memory();
117
D_80334A30 = 0;
118
D_80334A34 = 0;
119
D_80334A38 = 0;
120
} else {
121
D_80334A30 = 0;
122
D_80334A34 = 0;
123
D_80334A38 = 0;
124
}
125
}
126
break;
127
default:
128
D_80334A30 = 0;
129
D_80334A34 = 0;
130
D_80334A38 = 0;
131
break;
132
}
133
}
134
135