add missing tools/pngdump to the repo
[gbajam22] / src / debug.c
index e983ff6..e36ed1f 100644 (file)
@@ -1,23 +1,28 @@
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 #include <stdarg.h>
 #include "gbaregs.h"
 #include "intr.h"
 #include "debug.h"
+#include "gba.h"
 #include "util.h"
 
 uint16_t vblperf_color[] = {
-       /* grn  blue   cyan  yellow  orng    red     purple  d.green purple ... */
-       /* 60    30     20     15     12      10      8.5     7.5    ... */
-       0x3e0, 0xf863, 0xffc0, 0x3ff, 0x1ff, 0x001f, 0xf81f, 0x1e0, 0xf81f, 0xf81f, 0xf81f
+       /* white white  grn    blue   cyan    yellow   orng    red   purple  d.green white ... */
+       /* 60    30     20     15     12       10      8.5     7.5    6.6      6      5.4  ... */
+       0xffff, 0xffff, 0x3e0, 0xf863, 0xffc0, 0x3ff, 0x1ff, 0x001f, 0xf81f, 0x1e0, 0xffff, 0xffff, 0xffff
 };
 
 void vblperf_setcolor(int palidx)
 {
-       vblperf_palptr = (uint16_t*)CRAM_BG_ADDR + palidx;
+       vblperf_palptr = gba_bgpal + palidx;
 }
 
 
+#ifdef BUILD_GBA
+
 uint32_t panic_regs[16];
 void get_panic_regs(void);
 
@@ -32,6 +37,9 @@ void panic(void *pc, const char *fmt, ...)
        intr_disable();
        REG_DISPCNT = 4 | DISPCNT_BG2;
 
+       glyphcolor = 0xff;
+       glyphfb = (void*)VRAM_LFB_FB0_ADDR;
+
        set_bg_color(0, 31, 0, 0);
        set_bg_color(0xff, 31, 31, 31);
 
@@ -62,24 +70,45 @@ void panic(void *pc, const char *fmt, ...)
        for(;;);
 }
 
+#else  /* non-GBA build */
+
+void panic(void *pc, const char *fmt, ...)
+{
+       va_list ap;
+
+       fputs("~~~ PANIC ~~~\n", stderr);
+       va_start(ap, fmt);
+       vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       fputc('\n', stderr);
+
+       abort();
+}
+
+#endif
+
+int glyphcolor = 0xff;
+int glyphbg = 0;
+void *glyphfb = (void*)VRAM_LFB_FB0_ADDR;
+
 void dbg_drawglyph(int x, int y, int c)
 {
        int i;
        uint16_t pp;
        unsigned char row;
-       uint16_t *ptr = (uint16_t*)VRAM_LFB_FB0_ADDR + (y << 7) - (y << 3) + (x >> 1);
+       uint16_t *ptr = (uint16_t*)glyphfb + (y << 7) - (y << 3) + (x >> 1);
        unsigned char *fnt = font_8x8 + ((c & 0xff) << 3);
 
        for(i=0; i<8; i++) {
                row = *fnt++;
-               pp = row & 0x80 ? 0xff : 0;
-               *ptr++ = pp | (row & 0x40 ? 0xff00 : 0);
-               pp = row & 0x20 ? 0xff : 0;
-               *ptr++ = pp | (row & 0x10 ? 0xff00 : 0);
-               pp = row & 0x08 ? 0xff : 0;
-               *ptr++ = pp | (row & 0x04 ? 0xff00 : 0);
-               pp = row & 0x02 ? 0xff : 0;
-               *ptr++ = pp | (row & 0x01 ? 0xff00 : 0);
+               pp = row & 0x80 ? glyphcolor : glyphbg;
+               *ptr++ = pp | ((row & 0x40 ? glyphcolor : glyphbg) << 8);
+               pp = row & 0x20 ? glyphcolor : glyphbg;
+               *ptr++ = pp | ((row & 0x10 ? glyphcolor : glyphbg) << 8);
+               pp = row & 0x08 ? glyphcolor : glyphbg;
+               *ptr++ = pp | ((row & 0x04 ? glyphcolor : glyphbg) << 8);
+               pp = row & 0x02 ? glyphcolor : glyphbg;
+               *ptr++ = pp | ((row & 0x01 ? glyphcolor : glyphbg) << 8);
                ptr += 120 - 4;
        }
 }
@@ -129,26 +158,60 @@ int dbg_drawstr(int x, int y, const char *fmt, ...)
        return res;
 }
 
+#ifdef BUILD_GBA
+
 #ifdef EMUBUILD
-__attribute__((target("arm")))
+#define REG_DBG_ENABLE REG16(0xfff780)
+#define REG_DBG_FLAGS  REG16(0xfff700)
+#define REG_DBG_STR            REG8(0xfff600)
+
+/*__attribute__((target("arm")))*/
 void emuprint(const char *fmt, ...)
 {
+       static int opened;
        char buf[128];
        va_list ap;
 
+       if(!opened) {
+               REG_DBG_ENABLE = 0xc0de;
+               if(REG_DBG_ENABLE != 0x1dea) {
+                       return;
+               }
+               opened = 1;
+       }
+
        va_start(ap, fmt);
        vsnprintf(buf, sizeof buf, fmt, ap);
        va_end(ap);
 
+       strcpy((char*)0x4fff600, buf);
+       REG_DBG_FLAGS = 0x104;  /* debug message */
+
+       /*
        asm volatile(
                "mov r0, %0\n\t"
                "swi 0xff0000\n\t" :
                : "r" (buf)
                : "r0"
        );
+       */
 }
 #else
 void emuprint(const char *fmt, ...)
 {
 }
 #endif
+
+#else  /* non-GBA build */
+
+void emuprint(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vfprintf(stdout, fmt, ap);
+       va_end(ap);
+       fputc('\n', stdout);
+}
+
+#endif