# HG changeset patch # User John Tsiombikas # Date 1402675811 -10800 # Node ID b7130fe3f07364db013114517f5560c03e6a20de # Parent 0d2602a1b8514042b3ec25c8b26cdf109f3a858e foo diff -r 0d2602a1b851 -r b7130fe3f073 .clang_complete --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.clang_complete Fri Jun 13 19:10:11 2014 +0300 @@ -0,0 +1,2 @@ +-Isrc +-I../gbasys/src diff -r 0d2602a1b851 -r b7130fe3f073 Makefile --- a/Makefile Thu Jun 12 05:37:18 2014 +0300 +++ b/Makefile Fri Jun 13 19:10:11 2014 +0300 @@ -13,11 +13,14 @@ CC = $(ARCH)-gcc AS = $(ARCH)-as OBJCOPY = $(ARCH)-objcopy +EMU = vbam -opt = -O3 -fomit-frame-pointer -marm -mcpu=arm7tdmi -mtune=arm7tdmi +#opt = -O3 -fomit-frame-pointer -mcpu=arm7tdmi -mtune=arm7tdmi +dbg = -g -CFLAGS = $(opt) -pedantic -Wall -I../gbasys/src -I. +CFLAGS = $(opt) $(dbg) -pedantic -Wall -I../gbasys/src -I. LDFLAGS = ../gbasys/libgbasys.a -lm +EMUFLAGS = -T 100 -f 1 .PHONY: all all: $(bin) $(bin_mb) @@ -56,4 +59,4 @@ .PHONY: simrun simrun: $(bin) - VisualBoyAdvance -T 100 -2 $(bin) + $(EMU) $(EMUFLAGS) $(bin) diff -r 0d2602a1b851 -r b7130fe3f073 src/logger.c --- a/src/logger.c Thu Jun 12 05:37:18 2014 +0300 +++ b/src/logger.c Fri Jun 13 19:10:11 2014 +0300 @@ -1,17 +1,20 @@ #include +#include #include #include #include +#include "gbasys.h" #include "logger.h" static void putchr(char c); static void putstr(const char *str); +static void agbprint(const char *str); static int curx, cury; static int font_width = 8, font_height = 8; static int ncols = 20, nrows = 16; -void logmsg(const char *fmt, ...) +void logmsg(unsigned short where, const char *fmt, ...) { va_list ap; int sz; @@ -23,24 +26,42 @@ buf = alloca(sz + 1); va_start(ap, fmt); - vsnprintf(buf, sz, fmt, ap); + vsnprintf(buf, sz + 1, fmt, ap); va_end(ap); - putstr(buf); + if(where & LOG_SCREEN) { + putstr(buf); + } + if(where & LOG_DBG) { + agbprint(buf); + } } static void putchr(char c) { switch(c) { case '\n': + curx = 0; + if(cury >= nrows - 1) { + int row_size = font_height * 160 * 2; + memmove(front_buffer->pixels, (char*)front_buffer->pixels + row_size, + (nrows - 1) * row_size); + memset((char*)front_buffer->pixels + (nrows - 1) * row_size, 0, row_size); + } else { + ++cury; + } + break; + case '\r': curx = 0; - cury += font_height; break; default: - if(isprint(c)) { - draw_glyph(c, curx, cury, front_buffer); + if(isprint((int)c)) { + draw_glyph(c, curx * font_width, cury * font_height, front_buffer); + if(++curx >= ncols) { + putchr('\n'); + } } } } @@ -49,3 +70,13 @@ { while(*str) putchr(*str++); } + +static void agbprint(const char *str) +{ + asm volatile ( + "\n\tmov r0, %0" + "\n\tswi 0xff0000" + : + : "r" (str) + : "r0"); +} diff -r 0d2602a1b851 -r b7130fe3f073 src/logger.h --- a/src/logger.h Thu Jun 12 05:37:18 2014 +0300 +++ b/src/logger.h Fri Jun 13 19:10:11 2014 +0300 @@ -1,6 +1,12 @@ #ifndef LOGGER_H_ #define LOGGER_H_ -void logmsg(const char *fmt, ...); +enum { + LOG_SCREEN = 1, + LOG_DBG = 2 +}; +#define LOG_ALL 0xffff + +void logmsg(unsigned short where, const char *fmt, ...); #endif /* LOGGER_H_ */ diff -r 0d2602a1b851 -r b7130fe3f073 src/main.c --- a/src/main.c Thu Jun 12 05:37:18 2014 +0300 +++ b/src/main.c Fri Jun 13 19:10:11 2014 +0300 @@ -1,21 +1,34 @@ #include "gbasys.h" +#include "logger.h" static void draw(void); -static void keyb_handler(void); +static void keyb(int key, int pressed); +static void keyb_intr(void); -static unsigned int keystate; +static unsigned short keystate; + +#define EVQ_SIZE 8 +volatile static unsigned short evhead, evtail; +static unsigned short events[EVQ_SIZE]; int main(void) { gba_init(); - //interrupt(INTR_KEY, keyb_handler); + interrupt(INTR_KEY, keyb_intr); set_video_mode(VMODE_LFB_160x128_16, 1); clear_buffer(front_buffer, 0); - draw_string("please wait...", 0, 0, front_buffer); + set_text_writebg(1); + logmsg(LOG_ALL, "please wait...\n"); for(;;) { + /* process events */ + while(evhead != evtail) { + unsigned short ev = events[evhead]; + evhead = (evhead + 1) % EVQ_SIZE; + keyb(ev, 1); + } draw(); } return 0; @@ -25,6 +38,37 @@ { } -static void keyb_handler(void) +static void keyb(int key, int pressed) { + static const char *keyname[] = { + "A", "B", "select", "start", "right", "left", "up", "down", "RT", "LT" + }; + int i; + for(i=0; i<32; i++) { + if(key & 1) { + break; + } + key >>= 1; + } + if(i < sizeof keyname / sizeof *keyname) { + logmsg(LOG_ALL, "key %s %s\n", keyname[i], pressed ? "pressed" : "released"); + } } + +static void keyb_intr(void) +{ + int i; + unsigned short prev_keystate = keystate; + unsigned short diff; + keystate = get_key_state(KEY_ALL); + diff = keystate ^ prev_keystate; + + for(i=0; i<32; i++) { + int key = 1 << i; + + if(diff & key) { + events[evtail] = key; + evtail = (evtail + 1) % EVQ_SIZE; + } + } +}