implement ANSI escape macros for VT

This commit is contained in:
Prakxo
2023-03-18 17:34:21 +01:00
parent 85e9bf8111
commit 0630e6501e
3 changed files with 194 additions and 153 deletions
+40
View File
@@ -0,0 +1,40 @@
#ifndef TERMINAL_H
#define TERMINAL_H
// VT_: Helper macros for printing colored text to terminals, using ANSI escape codes
// VT stands for Virtual Terminal
// 3-bit color codes
#define VT_COLOR_BLACK 0
#define VT_COLOR_RED 1
#define VT_COLOR_GREEN 2
#define VT_COLOR_YELLOW 3
#define VT_COLOR_BLUE 4
#define VT_COLOR_MAGENTA 5
#define VT_COLOR_CYAN 6
#define VT_COLOR_WHITE 7
#define VT_COLOR_FOREGROUND 3
#define VT_COLOR_BACKGROUND 4
#define VT_COLOR_EXPAND0(type, color) #type #color
#define VT_COLOR_EXPAND1(type, color) VT_COLOR_EXPAND0(type, color)
#define VT_COLOR(type, color) VT_COLOR_EXPAND1(VT_COLOR_##type, VT_COLOR_##color)
#define VT_ESC "\x1b"
#define VT_CSI "["
#define VT_CUP(x, y) VT_ESC VT_CSI y ";" x "H"
#define VT_ED(n) VT_ESC VT_CSI #n "J"
#define VT_SGR(n) VT_ESC VT_CSI n "m"
// Add more macros if necessary
#define VT_COL(back, fore) VT_SGR(VT_COLOR(BACKGROUND, back) ";" VT_COLOR(FOREGROUND, fore))
#define VT_FGCOL(color) VT_SGR(VT_COLOR(FOREGROUND, color))
#define VT_BGCOL(color) VT_SGR(VT_COLOR(BACKGROUND, color))
#define VT_RST VT_SGR("")
#define VT_CLS VT_ED(2)
// ASCII BEL character, plays an alert tone
#define BEL '\a'
#endif
+9 -9
View File
@@ -1,5 +1,5 @@
#include "boot.h"
#include "terminal.h"
static u8 nintendo_hi_0[0x9900]; // This should be nintendo_hi_0.aw
extern u32 *StringTable;
@@ -50,7 +50,7 @@ void sound_initial(void){
Na_InitAudio(audioFatalCallback, 0, 0, nintendo_hi_0, 0x66a0, 0);
OSReport("sizeof(nintendo_hi_0)=%08x\n", 0x9900);
OSReport("実際のnintendo_hi_0.awのサイズ=%08x \n", 0x66a0);
OSReport("ニンテンド\x81\x5B発生タイムラグまで寝てます(%dms)\x1B\x5B\x6D\n", 0x9c4);
OSReport("ニンテンド\x81%x発生タイムラグまで寝てます(%dms)%x/n", VT_CSI, VT_RST, 0x9c4);
msleep(0x9c4);
}
@@ -100,33 +100,33 @@ void adjustOSArena(void) {
void* arenalo = OSGetArenaLo();
void* arenahi = OSGetArenaHi();
OSReport("ARENA %08x-%08x\x1B\x5B\x6B\n", arenalo, arenahi);
OSReport("ARENA %08x-%08x\%x/n", VT_RST, arenalo, arenahi);
if(arenahi > (void*)0x81800000) {
if(!(osAppNMIBuffer[15] & 0x80)){
OSReport("搭載メモリが 24MB を超えていますが、24MB に限定します。\x1B\x5B\x6D\n");
OSReport("搭載メモリが 24MB を超えていますが、24MB に限定します。%x/n", VT_RST);
arenahi = (void*)0x817ffa80;
}
else if(arenahi > (void*)0x82000000){
OSReport("搭載メモリが 32MB を超えています。\x1B\x5B\x6D\n");
OSReport("搭載メモリが 32MB を超えています。%x/n", VST_RST);
arenahi = (void*)0x81e00000;
}
else {
OSReport("搭載メモリが 32MB を超えていますが、32MB に限定します。\x1B\x5B\x6B\n");
OSReport("搭載メモリが 32MB を超えていますが、32MB に限定します。%x/n", VT_RST);
}
}
else {
OSReport("搭載メモリが 24MB 以下なので動かない事がありえます。\x1B\x5B\x6D\n");
OSReport("搭載メモリが 24MB 以下なので動かない事がありえます。%x/n", VT_RST);
}
OSSetArenaHi(arenahi);
OSReport("ARENA %08x-%08x\x1B\x5B\x6B\n", arenalo, arenahi);
OSReport("ARENA %08x-%08x%x/n", VT_RST, arenalo, arenahi);
bzero(arenalo, ((u32)arenahi - (u32)arenalo));
}
int main(int argc, const char **argv){ //https://decomp.me/scratch/frpgE
// To Finish
}
}
+145 -144
View File
@@ -1,144 +1,145 @@
#include "fault.h"
__declspec(section ".sdata") static fault* this;
static fault fault_class;
extern void fault_AddClientEx(fault_client* client, FaultCallback callback, const char* msg, u32 param, u8 priority, u8 flags) {
BOOL enable;
BOOL client_exists;
fault_client* f0;
fault_client* f1;
client_exists = FALSE;
if (client == NULL || callback == NULL) {
return;
}
enable = OSDisableInterrupts();
f0 = NULL;
for (f1 = this->first_client; f1 != NULL; f1 = f1->next) {
if (f1 == client) {
client_exists = TRUE;
goto exit;
}
if (f1->priority >= priority ) {
f0 = f1;
}
}
client->callback = callback;
client->msg = msg;
client->param = param;
client->priority = priority;
client->flags = flags;
if (f0 != NULL) {
client->next = f0->next;
f0->next = client;
}
else {
client->next = this->first_client;
this->first_client = client;
}
this->num_clients++;
exit:
OSRestoreInterrupts(enable);
if (client_exists != FALSE) {
OSReport("\x1b[41;37mfault_AddClient: %08x は既にリスト中にある\n\x1b[m", client);
}
}
extern void fault_AddClient(fault_client* client, FaultCallback callback, const char* msg, u32 param) {
fault_AddClientEx(client, callback, msg, param, FAULT_MIN_PRIORITY, FAULT_FLAG_POSTEXCEPTION);
}
static void fault_Printf(const char* fmt, ...) {
void* console;
console = JC_JUTException_getConsole();
if (console != NULL) {
va_list arg;
va_start(arg, fmt);
JC_JUTConsole_print_f_va(console, fmt, arg);
va_end(arg);
}
}
static void fault_DrawUpdate() {
void* manager = JC_JUTConsoleManager_getManager();
if (manager != NULL) {
JC_JUTConsoleManager_drawDirect(manager, TRUE);
}
}
extern void fault_WaitTime(u32 waitTime) {
fault_DrawUpdate();
JC_JUTException_waitTime(waitTime);
}
extern int fault_ReadPad(u32* outTrigger, u32* outButton) {
void* manager;
fault_DrawUpdate();
manager = JC_JUTException_getManager();
if (manager == NULL) {
return FAULT_PAD_READ_FAILED;
}
else {
JC_JUTException_readPad(manager, outTrigger, outButton);
return FAULT_PAD_READ_SUCCESS;
}
}
static void fault_CallBackFunc(int stage) {
fault_client* client;
int index;
index = 0;
for (client = this->first_client; client != NULL; client = client->next) {
if (client->callback != NULL) {
if ((client->flags & (1 << stage)) != 0) {
if ((client->flags & FAULT_FLAG_SKIP_DRAW_SEPARATOR) == 0) {
fault_Printf("---------------------------------------------\n");
fault_DrawUpdate();
}
if ((client->flags & FAULT_FLAG_SKIP_DRAW_CALLBACK_INFO) == 0) {
fault_Printf("CallBack (%d/%d) %08x %08x %08x\n", index++, this->num_clients, client, client->msg, client->param);
fault_DrawUpdate();
}
(*client->callback)(client->msg, client->param);
fault_DrawUpdate();
}
}
}
fault_DrawUpdate();
}
static void my_PreExceptionCallback() {
if (JC_JUTException_getConsole() != NULL && JC_JUTConsoleManager_getManager() != NULL && JC_JUTException_getManager() != NULL) {
fault_CallBackFunc(FAULT_STAGE_PRE);
}
}
static void my_PostExceptionCallback() {
if (JC_JUTException_getConsole() != NULL && JC_JUTConsoleManager_getManager() != NULL && JC_JUTException_getManager() != NULL) {
fault_CallBackFunc(FAULT_STAGE_POST);
}
}
extern void fault_Init() {
this = &fault_class;
bzero(&fault_class, sizeof(fault_class));
this->_0 = 0;
this->_1 = 0;
this->_2 = 0;
this->first_client = NULL;
this->_3 = 0;
fault_class._2 = 1;
JC_JUTException_setPreUserCallback(&my_PreExceptionCallback);
JC_JUTException_setPostUserCallback(&my_PostExceptionCallback);
}
#include "fault.h"
#include "terminal.h"
__declspec(section ".sdata") static fault* this;
static fault fault_class;
extern void fault_AddClientEx(fault_client* client, FaultCallback callback, const char* msg, u32 param, u8 priority, u8 flags) {
BOOL enable;
BOOL client_exists;
fault_client* f0;
fault_client* f1;
client_exists = FALSE;
if (client == NULL || callback == NULL) {
return;
}
enable = OSDisableInterrupts();
f0 = NULL;
for (f1 = this->first_client; f1 != NULL; f1 = f1->next) {
if (f1 == client) {
client_exists = TRUE;
goto exit;
}
if (f1->priority >= priority ) {
f0 = f1;
}
}
client->callback = callback;
client->msg = msg;
client->param = param;
client->priority = priority;
client->flags = flags;
if (f0 != NULL) {
client->next = f0->next;
f0->next = client;
}
else {
client->next = this->first_client;
this->first_client = client;
}
this->num_clients++;
exit:
OSRestoreInterrupts(enable);
if (client_exists != FALSE) {
OSReport(VTCOL(RED,WHITE)"fault_AddClient: %08x は既にリスト中にある\n%x",VT_RST, client);
}
}
extern void fault_AddClient(fault_client* client, FaultCallback callback, const char* msg, u32 param) {
fault_AddClientEx(client, callback, msg, param, FAULT_MIN_PRIORITY, FAULT_FLAG_POSTEXCEPTION);
}
static void fault_Printf(const char* fmt, ...) {
void* console;
console = JC_JUTException_getConsole();
if (console != NULL) {
va_list arg;
va_start(arg, fmt);
JC_JUTConsole_print_f_va(console, fmt, arg);
va_end(arg);
}
}
static void fault_DrawUpdate() {
void* manager = JC_JUTConsoleManager_getManager();
if (manager != NULL) {
JC_JUTConsoleManager_drawDirect(manager, TRUE);
}
}
extern void fault_WaitTime(u32 waitTime) {
fault_DrawUpdate();
JC_JUTException_waitTime(waitTime);
}
extern int fault_ReadPad(u32* outTrigger, u32* outButton) {
void* manager;
fault_DrawUpdate();
manager = JC_JUTException_getManager();
if (manager == NULL) {
return FAULT_PAD_READ_FAILED;
}
else {
JC_JUTException_readPad(manager, outTrigger, outButton);
return FAULT_PAD_READ_SUCCESS;
}
}
static void fault_CallBackFunc(int stage) {
fault_client* client;
int index;
index = 0;
for (client = this->first_client; client != NULL; client = client->next) {
if (client->callback != NULL) {
if ((client->flags & (1 << stage)) != 0) {
if ((client->flags & FAULT_FLAG_SKIP_DRAW_SEPARATOR) == 0) {
fault_Printf("---------------------------------------------\n");
fault_DrawUpdate();
}
if ((client->flags & FAULT_FLAG_SKIP_DRAW_CALLBACK_INFO) == 0) {
fault_Printf("CallBack (%d/%d) %08x %08x %08x\n", index++, this->num_clients, client, client->msg, client->param);
fault_DrawUpdate();
}
(*client->callback)(client->msg, client->param);
fault_DrawUpdate();
}
}
}
fault_DrawUpdate();
}
static void my_PreExceptionCallback() {
if (JC_JUTException_getConsole() != NULL && JC_JUTConsoleManager_getManager() != NULL && JC_JUTException_getManager() != NULL) {
fault_CallBackFunc(FAULT_STAGE_PRE);
}
}
static void my_PostExceptionCallback() {
if (JC_JUTException_getConsole() != NULL && JC_JUTConsoleManager_getManager() != NULL && JC_JUTException_getManager() != NULL) {
fault_CallBackFunc(FAULT_STAGE_POST);
}
}
extern void fault_Init() {
this = &fault_class;
bzero(&fault_class, sizeof(fault_class));
this->_0 = 0;
this->_1 = 0;
this->_2 = 0;
this->first_client = NULL;
this->_3 = 0;
fault_class._2 = 1;
JC_JUTException_setPreUserCallback(&my_PreExceptionCallback);
JC_JUTException_setPostUserCallback(&my_PostExceptionCallback);
}