d6220c016fcfd4c4407c7d5f437af915cfcb5a1a
[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 }