gba-trycatch
view src/logger.c @ 2:5143908d0220
fixed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Jun 2014 01:56:42 +0300 |
parents | 0d2602a1b851 |
children | 850be43b3135 |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <alloca.h>
6 #include "gbasys.h"
7 #include "logger.h"
9 static void putchr(char c);
10 static void putstr(const char *str);
11 static void agbprint(const char *str);
13 static int curx, cury;
14 static int font_width = 8, font_height = 8;
15 static int ncols = 20, nrows = 16;
17 void logmsg(unsigned short where, const char *fmt, ...)
18 {
19 va_list ap;
20 int sz;
21 char *buf;
23 va_start(ap, fmt);
24 sz = vsnprintf(0, 0, fmt, ap);
25 va_end(ap);
27 buf = alloca(sz + 1);
28 va_start(ap, fmt);
29 vsnprintf(buf, sz + 1, fmt, ap);
30 va_end(ap);
32 if(where & LOG_SCREEN) {
33 putstr(buf);
34 }
35 if(where & LOG_DBG) {
36 agbprint(buf);
37 }
38 }
40 static void putchr(char c)
41 {
42 switch(c) {
43 case '\n':
44 curx = 0;
45 if(cury >= nrows - 1) {
46 int row_size = font_height * 160 * 2;
47 memmove(front_buffer->pixels, (char*)front_buffer->pixels + row_size,
48 (nrows - 1) * row_size);
49 memset((char*)front_buffer->pixels + (nrows - 1) * row_size, 0, row_size);
50 } else {
51 ++cury;
52 }
53 break;
55 case '\r':
56 curx = 0;
57 break;
59 default:
60 if(isprint((int)c)) {
61 draw_glyph(c, curx * font_width, cury * font_height, front_buffer);
62 if(++curx >= ncols) {
63 putchr('\n');
64 }
65 }
66 }
67 }
69 static void putstr(const char *str)
70 {
71 while(*str) putchr(*str++);
72 }
74 static void agbprint(const char *str)
75 {
76 asm volatile (
77 "\n\tmov r0, %0"
78 "\n\tswi 0xff0000"
79 :
80 : "r" (str)
81 : "r0");
82 }