kdebugserver.c (3106B)
1 #include "libultra_internal.h" 2 #include "PR/rdb.h" 3 4 #ifndef VERSION_CN 5 static s32 debugState = 0; 6 static s32 numChars = 0; 7 static s32 numCharsToReceive = 0; 8 #endif 9 10 extern u8 debugBuffer[0x100]; 11 12 OSThread __osThreadSave; 13 14 #ifndef VERSION_CN 15 void u32_to_string(u32 i, u8 *str) { 16 str[0] = (i >> 0x18) & 0xff; 17 str[1] = (i >> 0x10) & 0xff; 18 str[2] = (i >> 0x8) & 0xff; 19 str[3] = i & 0xff; 20 } 21 22 u32 string_to_u32(u8 *str) { 23 u32 i; 24 i = (str[0] & 0xff) << 0x18; 25 i |= (str[1] & 0xff) << 0x10; 26 i |= (str[2] & 0xff) << 0x8; 27 i |= (str[3] & 0xff); 28 return i; 29 } 30 31 void send_packet(u8 *a0, s32 a1) { 32 rdbPacket pkt; 33 s32 i; 34 pkt.type = 2; 35 for (pkt.length = a1, i = 0; i < a1; i++) { 36 pkt.buf[i] = a0[i]; 37 } 38 *(volatile u32 *) RDB_BASE_REG = *(u32 *) &pkt; 39 while (!(__osGetCause() & 0x2000)) { 40 ; 41 } 42 *(volatile u32 *) RDB_READ_INTR_REG = 0; 43 } 44 45 void send(u8 *buff, s32 len) { 46 s32 i; 47 s32 end; 48 s32 rem; 49 if (!__osRdbWriteOK) { 50 while (!(__osGetCause() & 0x2000)) { 51 ; 52 } 53 *(volatile u32 *) RDB_READ_INTR_REG = 0; 54 __osRdbWriteOK = 1; 55 } 56 i = 0; 57 rem = len % 3; 58 end = len - rem; 59 for (; i < end; i += 3) { 60 send_packet(&buff[i], 3); 61 } 62 if (rem > 0) { 63 send_packet(&buff[end], rem); 64 } 65 } 66 67 void process_command_memory(void) { 68 u32 sp1c; 69 u32 sp18; 70 sp1c = string_to_u32(&debugBuffer[1]); 71 sp18 = string_to_u32(&debugBuffer[5]); 72 send((u8 *) (uintptr_t) sp1c, sp18); 73 } 74 75 void process_command_register(void) { 76 send((u8 *) &__osThreadSave.context, sizeof(__OSThreadContext)); 77 } 78 79 void kdebugserver(u32 a0) { 80 u32 sp2c; 81 rdbPacket pkt; 82 *(u32 *) &pkt = a0; 83 for (sp2c = 0; sp2c < pkt.length; sp2c++) { 84 debugBuffer[numChars] = pkt.buf[sp2c]; 85 numChars++; 86 } 87 numCharsToReceive -= pkt.length; 88 switch (debugState) { 89 case 0: 90 switch (pkt.buf[0]) { 91 case 1: 92 debugState = 1; 93 numCharsToReceive = 9 - pkt.length; 94 break; 95 case 2: 96 process_command_register(); 97 debugState = 0; 98 numChars = 0; 99 numCharsToReceive = 0; 100 break; 101 default: 102 debugState = 0; 103 numChars = 0; 104 numCharsToReceive = 0; 105 break; 106 } 107 break; 108 case 1: 109 if (numCharsToReceive <= 0) { 110 if (debugBuffer[0] == 1) { 111 process_command_memory(); 112 debugState = 0; 113 numChars = 0; 114 numCharsToReceive = 0; 115 } else { 116 debugState = 0; 117 numChars = 0; 118 numCharsToReceive = 0; 119 } 120 } 121 break; 122 default: 123 debugState = 0; 124 numChars = 0; 125 numCharsToReceive = 0; 126 break; 127 } 128 } 129 #endif