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