From 764c97dfb709cf48cc195c4dfac08b359b75f096 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 30 Apr 2014 21:57:03 +0200 Subject: [PATCH] Correct and faster hires mode. Added b/w hires mode. Input fixes. --- Makefile | 16 ++++-- source/emu.c | 4 +- source/input.c | 5 +- source/main.c | 11 ++-- source/menu.c | 30 +++++++---- source/video.c | 139 ++++++++++++++++++++++++++----------------------- source/video.h | 1 + 7 files changed, 119 insertions(+), 87 deletions(-) diff --git a/Makefile b/Makefile index 28c4711..947d0a3 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ endif include $(DEVKITARM)/ds_rules +EMU_BUILD=0 + #--------------------------------------------------------------------------------- # TARGET is the name of the output # BUILD is the directory where object files & intermediate files will be placed @@ -20,7 +22,6 @@ BUILD := build SOURCES := source DATA := INCLUDES := include -NITRODATA := fs #--------------------------------------------------------------------------------- # options for code generation @@ -34,7 +35,7 @@ CFLAGS := -save-temps \ $(ARCH) #-finstrument-functions -mpoke-function-name \ -CFLAGS += $(INCLUDE) -DARM9 -DEMU_BUILD +CFLAGS += $(INCLUDE) -DARM9 CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions ASFLAGS := $(ARCH) @@ -43,14 +44,21 @@ LDFLAGS = -specs=ds_arm9.specs $(ARCH) -Wl,-Map,$(notdir $*.map) #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project (order is important) #--------------------------------------------------------------------------------- -LIBS := -lfilesystem -lfat -lnds9 - +LIBS := -lfat -lnds9 #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing # include and lib #--------------------------------------------------------------------------------- LIBDIRS := $(LIBNDS) + +ifeq ($(strip $(EMU_BUILD)),1) + NITRODATA := fs + CFLAGS += -DEMU_BUILD + LIBS := -lfilesystem $(LIBS) +else + CFLAGS += -DHW_BUILD +endif #--------------------------------------------------------------------------------- # no real need to edit anything past this point unless you need to add additional diff --git a/source/emu.c b/source/emu.c index 248cc44..5ad75b2 100644 --- a/source/emu.c +++ b/source/emu.c @@ -42,7 +42,6 @@ void emu_init () u16 crc; int valid_crc; - keysSetRepeat(30, 10); keyboardShow(); // Set some sane defaults @@ -51,6 +50,7 @@ void emu_init () // Setup the video hardware video_init(); #if 1 + crc = 0xffff; // Load the appropriate bios if (load_bin("BASIC.ROM", 0xD000, 0x3000, &crc)) { valid_crc = valid_rom_crc(crc); @@ -95,6 +95,8 @@ void emu_run () video_draw(); + scanKeys(); + update_input(); frames_done++; diff --git a/source/input.c b/source/input.c index 2353e09..000ac27 100644 --- a/source/input.c +++ b/source/input.c @@ -22,11 +22,10 @@ void update_input () u32 keys; int kbd_key; - scanKeys(); - keys = (keysDownRepeat()|keysDown())&0xfff; + keys = keysDown() | keysHeld(); // Send keyboard scancodes when a key is pressed - if (emu_input == INPUT_KBD && keys) { + if (emu_input == INPUT_KBD && (keys&0xfff)) { int bit_set = __builtin_ffs(keys); if (bit_set) keybd_latch = 0x80 ^ key_map[bit_set-1]; diff --git a/source/main.c b/source/main.c index 347b528..a393900 100644 --- a/source/main.c +++ b/source/main.c @@ -12,13 +12,18 @@ int main(int argc, char **argv) consoleDemoInit(); keyboardDemoInit(); - fatInitDefault(); +#ifdef EMU_BUILD nitroFSInit(NULL); +#else + fatInitDefault(); +#endif soundEnable(); iprintf("-- grape\n"); + keysSetRepeat(30, 10); + emu_init(); if (argc != 2) { @@ -27,7 +32,7 @@ int main(int argc, char **argv) /*load_disk("PACMAN.DSK");*/ /*load_disk("Karateka (1984)(Broderbund).dsk");*/ // Awesome! - load_disk("lode.dsk"); + /*load_disk("lode.dsk");*/ // AppleII+ /*load_disk("Prince of Persia (1989)(Broderbund)(Disk 1 of 3)[cr].dsk");*/ // Gfx heavy @@ -37,7 +42,7 @@ int main(int argc, char **argv) /*load_disk("Round About (1983)(Datamost).dsk");*/ /*load_disk("Bug Attack (1981)(Cavalier Computer).dsk");*/ // Scroller - /*load_disk("TetrisII.DSK");*/ + load_disk("TetrisII.DSK"); // Mixed mode /*load_disk("tetris48k.nib");*/ // Lowres diff --git a/source/menu.c b/source/menu.c index bec106c..bdfd603 100644 --- a/source/menu.c +++ b/source/menu.c @@ -48,13 +48,19 @@ static int opt_exit (const int sel) return 1; } -static int sel_slot_l = 0, sel_slot_s = 0; +static int opt_hires (const int sel) +{ + video_set_hires(sel); +} + +static int sel_slot_l = 0, sel_slot_s = 0, sel_hires = 0; const static struct page_t paused_pg = { - "Paused", 7, (const entry_t []){ + "Paused", 8, (const entry_t []){ { "Vsync", 2, { "No", "Yes" }, &emu_vsync, opt_vsync }, { "Scale", 2, { "No", "Yes" }, &emu_scale, opt_scale }, { "Screen", 2, { "Top", "Bottom" }, &emu_screen, opt_screen }, + { "Hires mode", 2, { "B/W", "Color" }, &sel_hires, opt_hires }, { "Map keys to", 2, { "joystick", "keyboard" }, &emu_input, opt_input }, { "Save state", 9, { "1", "2", "3", "4", "5", "6", "7", "8", "9" }, &sel_slot_s, state_save }, { "Load state", 9, { "1", "2", "3", "4", "5", "6", "7", "8", "9" }, &sel_slot_l, state_load }, @@ -66,7 +72,7 @@ void menu_print_page (const page_t *page) { int i; int cur; - int keys; + u32 keys, keys_; cur = 0; @@ -83,32 +89,34 @@ void menu_print_page (const page_t *page) } scanKeys(); - keys = keysDownRepeat(); + keys = keysDown(); + keys_ = keysDownRepeat(); - if (keys&KEY_UP) { + if (keys_&KEY_UP) { cur--; if (cur < 0) cur = page->entries_no - 1; } - if (keys&KEY_DOWN) { + if (keys_&KEY_DOWN) { cur++; if (cur == page->entries_no) cur = 0; } if (sel_entry->opts_no) { - if (keys&KEY_LEFT && *sel_entry->opt_ptr > 0) + if (keys_&KEY_LEFT && *sel_entry->opt_ptr > 0) (*sel_entry->opt_ptr)--; - if (keys&KEY_RIGHT && *sel_entry->opt_ptr < sel_entry->opts_no - 1) + if (keys_&KEY_RIGHT && *sel_entry->opt_ptr < sel_entry->opts_no - 1) (*sel_entry->opt_ptr)++; } - if (keys&(KEY_TOUCH|KEY_START|KEY_A)) { + if (keys&KEY_A) { if (sel_entry->cb && sel_entry->cb(*sel_entry->opt_ptr)) return; - if (!(keys&KEY_A)) - return; } + if (keys&KEY_START) + return; + swiWaitForVBlank(); } } diff --git a/source/video.c b/source/video.c index e6cd044..6969342 100644 --- a/source/video.c +++ b/source/video.c @@ -73,6 +73,8 @@ static int mixed_mode; static int sel_page; static int hires; +static void (* draw_hires_line)(u16 *, u8 *); + // The mixed mode can be enabled only if graphic mode is set #define render_mixed_mode (mixed_mode&(!text_mode)) @@ -185,94 +187,86 @@ void draw_lores_scr (u16 *__restrict map) } } -void draw_hires_scr (u16 *map) ITCM_CODE; -#if 0 /*odd_color = (b1&0x80) ? 6 : 3;*/ /*even_color = (b1&0x80) ? 9 : 12;*/ -void draw_hires_scr (u16 *__restrict map) + +static void draw_hires_line_mono (u16 *__restrict map, u8 *__restrict ptr) ITCM_CODE; +static void draw_hires_line_mono (u16 *__restrict map, u8 *__restrict ptr) { - int last_line; - int i, j, k; - u8 *__restrict ptr, tmp; - static u8 tmp_line[0x200]; - static const u8 color_lut[] DTCM_DATA = { 0, 12, 0, 15, 0, 9, 0, 15, 0, 3, 0, 15, 0, 6, 0, 15 }; - - last_line = render_mixed_mode ? 168 : 192; - - for (i = 0; i < last_line; i++, map += 0x100) { - if (!page_dirty[8 + (i&7)]) - continue; - - ptr = mainram + (sel_page << 13) + ((i&7) * 0x400) + video_addr[i>>3]; - - for (j = 0, tmp = 0; j < 280; j += 7) { - const u8 b1 = *ptr++; - - tmp = (b1&0x7f) << 1 | tmp; - - // embb - // e = even / odd - // m = msb - // b = data - for (k = 0; k <= 7; k++) - tmp_line[j+k] = color_lut[(((j+k)&1) ? 0x8 : 0x0) + ((b1&0x80)>>5) + ((tmp>>k)&3)]; - - tmp >>= 7; - } - - DC_FlushRange(tmp_line, sizeof(tmp_line)); - dmaCopyAsynch(tmp_line, map, sizeof(tmp_line)); - DC_InvalidateRange(map, sizeof(tmp_line)); + const u16 lut[] = {0x0000, 0x000c, 0x0c00, 0x0c0c}; + int j; + for (j = 0; j < 280/14; j++) { + u16 tmp = (ptr[0]&0x7f) | ((ptr[1]&0x7f) << 7); + ptr += 2; + *map++ = lut[tmp&3]; tmp >>= 2; + *map++ = lut[tmp&3]; tmp >>= 2; + *map++ = lut[tmp&3]; tmp >>= 2; + *map++ = lut[tmp&3]; tmp >>= 2; + *map++ = lut[tmp&3]; tmp >>= 2; + *map++ = lut[tmp&3]; tmp >>= 2; + *map++ = lut[tmp&3]; } - } -#else -void draw_hires_scr (u16 *__restrict map) + +static void draw_hires_line_color (u16 *__restrict map, u8 *__restrict ptr) ITCM_CODE; +static void draw_hires_line_color (u16 *__restrict map, u8 *__restrict ptr) { - int last_line; - int i, j, k, x; - u8 *__restrict xptr, *__restrict ptr, tmp; + static u8 tmp_line[280]; + static const u8 color_lut[] DTCM_DATA = { + 0, 12, 0, 15, 0, 9, 0, 15, 0, 3, 0, 15, 0, 6, 0, 15 + }; + int j, k; + u16 tmp; + + for (j = 0, tmp = 0; j < 280; j += 7) { + const u8 b1 = *ptr++; + + tmp = (b1&0x7f) << 1 | tmp; + const u8 *__restrict lut_b = color_lut + ((b1&0x80)>>5); + + for (k = 0; k < 7; k+=2) + { + tmp_line[j+k+0] = lut_b[((j&1) << 3) + (tmp&3)]; + tmp >>= 1; + if(k == 6) + break; + tmp_line[j+k+1] = lut_b[((~j&1) << 3) + (tmp&3)]; + tmp >>= 1; + } + } + DC_FlushRange(tmp_line, sizeof(tmp_line)); + dmaCopyAsynch(tmp_line, map, sizeof(tmp_line)); + DC_InvalidateRange(map, sizeof(tmp_line)); +} + + +static void draw_hires_scr (u16 *map) ITCM_CODE; +static void draw_hires_scr (u16 *__restrict map) +{ + int i, x, last_line; + u8 *__restrict xptr, *__restrict ptr; u16 *__restrict omap; - static u8 tmp_line[0x200]; - static const u8 color_lut[] DTCM_DATA = { 0, 12, 0, 15, 0, 9, 0, 15, 0, 3, 0, 15, 0, 6, 0, 15 }; last_line = render_mixed_mode ? 168/8 : 192/8; for(x = 0; x < 8; x++, map += 0x100) { - if (!page_dirty[8+x]) + if (!page_dirty[(sel_page<<3)+x]) continue; + xptr = mainram + (sel_page << 13) + (x << 10); for(omap = map, i = 0; i < last_line; i++, omap += 0x800) { ptr = xptr + video_addr[i]; - for (j = 0, tmp = 0; j < 280; j += 7) { - const u8 b1 = *ptr++; + draw_hires_line(omap, ptr); - tmp = (b1&0x7f) << 1 | tmp; - const u8 *__restrict lut_b = color_lut + ((b1&0x80)>>5); - - for (k = 0; k < 7; k+=2) - { - tmp_line[j+k+0] = lut_b[((j&1) << 3) + (tmp&3)]; - tmp >>= 1; - if(k == 6) - break; - tmp_line[j+k+1] = lut_b[((~j&1) << 3) + (tmp&3)]; - tmp >>= 1; - } - } - DC_FlushRange(tmp_line, sizeof(tmp_line)); - dmaCopyAsynch(tmp_line, omap, sizeof(tmp_line)); - DC_InvalidateRange(omap, sizeof(tmp_line)); } - page_dirty[8+x] = 0; + page_dirty[(sel_page<<3)+x] = 0; } } -#endif void draw_text_scr (u16 *map) ITCM_CODE; -void draw_text_scr (u16 *map) +void draw_text_scr (u16 *__restrict map) { int start_line; int i, j; @@ -319,6 +313,19 @@ void video_draw () page_dirty[sel_page] = 0; } +void video_set_hires (int renderer) +{ + switch (renderer) { + default: + case 0: + draw_hires_line = draw_hires_line_mono; + break; + case 1: + draw_hires_line = draw_hires_line_color; + break; + } +} + int video_set_scale (int mode) { int scaleg_x, scalet_x; @@ -366,6 +373,8 @@ void video_init () // BG2 outside WIN_OUT = 0x4; + video_set_hires(-1); + memcpy(BG_PALETTE, palette, sizeof(palette)); struct UnpackStruct unpack_bit; diff --git a/source/video.h b/source/video.h index 1c6d179..6881c2f 100644 --- a/source/video.h +++ b/source/video.h @@ -1,6 +1,7 @@ #ifndef VIDEO_H #define VIDEO_H +void video_set_hires (int renderer); int video_set_scale (int mode); void video_draw (); void video_init ();