05f0cd99fe9638072182e4b0ddbcf1178fde1232
[mdlife] / src / debug.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <stdint.h>
4 #include "debug.h"
5 #include "vdp.h"
6
7 #define FONT_ADDR       0xb400
8
9 extern uint16_t font8x8_data[], font8x8_data_end[];
10
11 static int cur_x, cur_y, left_margin;
12
13
14 void dbg_init(void)
15 {
16         uint16_t *src = font8x8_data;
17
18         vdp_setup_addr(VDP_VRAM, FONT_ADDR);
19         while(src < font8x8_data_end) {
20                 VDP_DATA = *src++;
21         }
22 }
23
24 #define TILESZ  32
25
26 void dbg_printchar(int x, int y, char c)
27 {
28         unsigned int addr = 0xc000 + (y << 7) + (x << 1);       /* 64x32 scroll size */
29         int tile = (c - 32) + (FONT_ADDR / TILESZ);
30         vdp_setup_addr(VDP_VRAM, addr);
31         VDP_DATA = VDP_TILENAME(tile, 0, 0);
32 }
33
34 void dbg_printstr(int x, int y, const char *str)
35 {
36         if(!*str) return;
37
38         /* put the first char to set up the address once */
39         dbg_printchar(x++, y, *str++);
40
41         while(*str && x < 40) {         /* 40 visible cells across in H40 (320) mode */
42                 int tile = (*str++ - 32) + (FONT_ADDR / TILESZ);
43                 VDP_DATA = VDP_TILENAME(tile, 0, 0);
44         }
45 }
46
47 void dbg_setcursor(int x, int y)
48 {
49         cur_x = x;
50         cur_y = y;
51         left_margin = x;
52 }
53
54 void dbg_putchar(int c)
55 {
56         switch(c) {
57         case '\n':
58                 cur_x = left_margin;
59                 cur_y++;
60                 break;
61
62         case '\b':
63                 if(cur_x > left_margin) {
64                         dbg_printchar(--cur_x, cur_y, ' ');
65                 }
66                 break;
67
68         case '\t':
69                 cur_x = (cur_x + 8) & 0xf8;
70                 break;
71
72         default:
73                 dbg_printchar(cur_x++, cur_y, c);
74         }
75 }
76
77 static const char *exc_str[] = {
78         "unknown", "",
79         "bus error",
80         "address error",
81         "illegal instruction",
82         "divide by zero",
83         "chk",
84         "trap",
85         "priviledge violation",
86         "trace",
87         "op 1010",
88         "op 1111",
89         "", "",
90         "format error",
91         "uninitialized vector",
92         "", "", "", "", "", "", "", "",
93         "spurious interrupt"
94 };
95
96 struct frame {
97         uint16_t padding;
98         uint16_t status;
99         uint32_t pc;
100         /* group 2 */
101         uint16_t fmtoffs;
102         uint16_t other[1];
103 } __attribute__((packed));
104
105 void exc_dump(unsigned int num, struct frame *frm)
106 {
107         dbg_setcursor(0, 0);
108         printf("num addr: %x\n", &num);
109
110         if(num < sizeof exc_str / sizeof *exc_str) {
111                 printf("Exception %xh: %s\n", num, exc_str[num]);
112         } else {
113                 printf("Exception %xh\n", num);
114         }
115
116         printf("status: %x\n", frm->status);
117         printf("pc: %x\n", frm->pc);
118         printf("fmt: %x / vector: %x\n", frm->fmtoffs >> 12, frm->fmtoffs & 0xfff);
119 }