/* ds81 - Nintendo DS ZX81 emulator. Copyright (C) 2006 Ian Cowburn This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. $Id$ */ #include #include #include #include #include #include "framebuffer.h" #include "gui.h" #include "keyboard.h" #include "z80.h" #include "zx81.h" #include "tapes.h" #include "config.h" #include "textmode.h" #include "monitor.h" #include "splashimg_bin.h" #include "rom_font_bin.h" /* ---------------------------------------- STATIC DATA */ static const char *main_menu[]= { "Reset ZX81", "Select Tape", "Configure", "Map Joypad to Keys", "Machine Code Monitor", "Cancel", NULL }; typedef enum { MenuReset, MenuSelectTape, MenuConfigure, MenuMapJoypad, MenuMonitor, } MenuOpt; /* ---------------------------------------- IRQ FUNCS */ /* ---------------------------------------- DISPLAY FUNCS */ static void VBlankFunc(void) { scanKeys(); } static void Splash(void) { static char scroller[]= { " " "Welcome to DS81, a ZX81 emulator for the Ninetendo DS. " "You can safely ignore this message. I was just bored for half an " "hour. " "Thanks to Slay Radio for coding fuel." }; static const char *text[]= { "DS81 \177 2006 Ian C", " ", "ZX81 ROM \177 1981", "Nine Tiles Networks LTD", " ", "PRESS A TO CONTINUE", " ", "http://www.noddybox.co.uk/", " ", " ", " ", " ", "Checking for FAT device...", NULL }; sImage img; int f; int y; int res=FALSE; int scr_x=0; ZX81SuspendDisplay(); ZX81DisplayString("10 print '%the zx81 is ace%'\n20 goto 10"); SUB_BG2_XDX = 0x080; SUB_BG2_YDY = 0x080; TM_printf(0,11,"%-18.18s",scroller); FB_Clear(); loadPCX(splashimg_bin,&img); FB_Blit(&img,0,0,1); y = 10; for(f=0;text[f];f++) { FB_Centre(text[f],y,COL_WHITE,COL_TRANSPARENT); y += 8; } y += 8; #ifndef DS81_DISABLE_FAT res = fatInitDefault(); #endif if (res) { ZX81EnableFileSystem(TRUE); FB_Centre("Found a FAT device.",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("If you place .P tape files in",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("the top directory or ZX81SNAP",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("then you should be able to load",y, COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("GAME.P with the command",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("LOAD \"GAME\"",y,COL_WHITE,COL_TRANSPARENT); y += 8; } else { ZX81EnableFileSystem(FALSE); FB_Centre("Sorry, but you don't have a",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("supported FAT device.",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("Only the internal tape",y,COL_WHITE,COL_TRANSPARENT); y += 8; FB_Centre("files can be used.",y,COL_WHITE,COL_TRANSPARENT); y += 8; } while(!(keysDown() & KEY_A)) { swiWaitForVBlank(); if (++scr_x == 8) { size_t l = sizeof scroller; char c; scr_x = 0; c = scroller[0]; memmove(scroller,scroller+1,l-2); scroller[l-2] = c; TM_printf(0,11,"%-18.18s",scroller); } SUB_BG2_CX = scr_x << 8; } SUB_BG2_XDX = 0x100; SUB_BG2_YDY = 0x100; SUB_BG2_CX = 0; ZX81ResumeDisplay(); } /* ---------------------------------------- JOYPAD MAPPING */ static void MapJoypad(void) { SoftKeyEvent ev; SoftKey pad = NUM_SOFT_KEYS; int done = FALSE; char text[256]; SK_DisplayKeyboard(BG_GFX_SUB); ZX81SuspendDisplay(); ZX81DisplayString("press the joypad button you want\n" "to define and then the ZX81 key\n" "you want to use.\n\n" "press on the config banner to\n" "finish."); while(!done) { while(SK_GetBareEvent(&ev)) { if (ev.pressed) { if (ev.key==SK_ABOUT || ev.key==SK_CONFIG) { done = true; } } else { if (ev.key>=SK_PAD_UP && ev.key<=SK_PAD_SELECT) { pad = ev.key; /* Now, just how dumb was making % the inverse on/off... */ sprintf(text,"defining\n %%%s%%",SK_KeyName(pad)); ZX81DisplayString(text); } if (ev.key<=SK_SPACE && pad!=NUM_SOFT_KEYS) { sprintf(text,"mapped\n %%%s%%\nto\n %%%s%%", SK_KeyName(pad),SK_KeyName(ev.key)); ZX81DisplayString(text); SK_DefinePad(pad,ev.key); pad = NUM_SOFT_KEYS; } } } swiWaitForVBlank(); } ZX81ResumeDisplay(); } /* ---------------------------------------- MAIN */ int main(int argc, char *argv[]) { Z80 *z80; powerON(POWER_ALL_2D); /* Set up main screen for ZX81 and load the ROM character data */ videoSetMode(MODE_3_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG3_ACTIVE); vramSetBankA(VRAM_A_MAIN_BG_0x06000000); vramSetBankB(VRAM_B_MAIN_BG_0x06020000); BG0_CR = BG_COLOR_256 | BG_32x32 | BG_MAP_BASE(0) | BG_TILE_BASE(1) | BG_PRIORITY(0); BG0_X0 = 0; BG0_Y0 = 0; BG_PALETTE[0] = RGB15(31,31,31); BG_PALETTE[1] = RGB15(0,0,0); BG3_CR = BG_BMP16_256x256 | BG_BMP_BASE(2) | BG_PRIORITY(1); BG3_XDX = 0x100; BG3_XDY = 0; BG3_YDX = 0; BG3_YDY = 0x100; BG3_CX = 0; BG3_CY = 0; dmaCopy(rom_font_bin,(void *)BG_TILE_RAM(1),rom_font_bin_size); /* Set up the sub-screen for rotation (basically for use as a framebuffer). Now overlaid with a text screen for the monitor (I thought a bitmapped printing routine would needlessly slow down the monitor when watching the ZX81 run). Having said the overlay is currently a rotation map for some pointless frippery! Still be quicker though. */ videoSetModeSub(MODE_4_2D | DISPLAY_BG2_ACTIVE | DISPLAY_BG3_ACTIVE); vramSetBankC(VRAM_C_SUB_BG_0x06200000); SUB_BG2_CR = BG_COLOR_256 | BG_RS_64x64 | BG_MAP_BASE(4) | BG_TILE_BASE(0) | BG_PRIORITY(0); SUB_BG2_XDX = 0x100; SUB_BG2_XDY = 0; SUB_BG2_YDX = 0; SUB_BG2_YDY = 0x100; SUB_BG2_CX = 0; SUB_BG2_CY = 0; SUB_BG3_CR = BG_BMP8_256x256 | BG_BMP_BASE(1) | BG_PRIORITY(1); SUB_BG3_XDX = 0x100; SUB_BG3_XDY = 0; SUB_BG3_YDX = 0; SUB_BG3_YDY = 0x100; SUB_BG3_CX = 0; SUB_BG3_CY = 0; /* Tell 'framebuffer' routines to use this */ FB_Init((uint16*)BG_BMP_RAM_SUB(1), BG_PALETTE_SUB); /* Set up lower screen text overlay */ FB_LoadASCIITiles((uint16*)BG_TILE_RAM_SUB(0)); TM_Init((uint16*)BG_MAP_RAM_SUB(4),64,64,TRUE); /* Set up interrupts and timers */ irqInit(); irqSet(IRQ_VBLANK,VBlankFunc); irqEnable(IRQ_VBLANK); /* All required stuff initialised */ keysSetRepeat(30,15); z80 = Z80Init(ZX81ReadMem, ZX81WriteMem, ZX81ReadPort, ZX81WritePort, ZX81ReadDisassem); if (!z80) { GUI_Alert(TRUE,"Failed to initialise\nthe Z80 CPU emulation!"); } ZX81Init((uint16*)BG_MAP_RAM(0), (uint16*)BG_BMP_RAM(2), z80); Splash(); LoadConfig(); ZX81Reconfigure(); SK_DisplayKeyboard(BG_GFX_SUB); SK_SetSticky(SK_SHIFT,DS81_Config[DS81_STICKY_SHIFT]); while(1) { SoftKeyEvent ev; TM_printf(0,0,"HL = %4.4x",z80->HL.w); TM_printf(0,23,"PC = %4.4x",z80->PC); Z80Exec(z80); while(SK_GetEvent(&ev)) { switch(ev.key) { case SK_ABOUT: case SK_CONFIG: if (ev.pressed) { switch(GUI_Menu(main_menu)) { case MenuReset: ZX81Reset(z80); break; case MenuSelectTape: SelectTape(); break; case MenuConfigure: GUI_Config(); SK_SetSticky(SK_SHIFT, DS81_Config[DS81_STICKY_SHIFT]); ZX81Reconfigure(); break; case MenuMapJoypad: MapJoypad(); break; case MenuMonitor: MachineCodeMonitor(); break; } SK_DisplayKeyboard(BG_GFX_SUB); } break; default: ZX81HandleKey(ev.key,ev.pressed); break; } } } return 0; }