diff options
author | Ian C <ianc@noddybox.co.uk> | 2006-10-07 00:21:12 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2006-10-07 00:21:12 +0000 |
commit | fcaec358f688bba6760cc44421b35e6b3d90a853 (patch) | |
tree | 99d92a6231d2319c3f1a7551a93f91a91ffa953f /source/zx81.c | |
parent | b16b3120a84f1b180e874367d3101264479a595f (diff) |
First working version (3D Monster Maze playable!)
Diffstat (limited to 'source/zx81.c')
-rw-r--r-- | source/zx81.c | 336 |
1 files changed, 71 insertions, 265 deletions
diff --git a/source/zx81.c b/source/zx81.c index df30dd7..b0ec963 100644 --- a/source/zx81.c +++ b/source/zx81.c @@ -1,7 +1,7 @@ /* - ezx81 - X11 ZX81 emulator + ds81 - Nintendo DS ZX81 emulator - Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk) + Copyright (C) 2006 Ian Cowburn (ianc@noddybox.demon.co.uk) 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 @@ -22,20 +22,15 @@ Provides the emulation for the ZX81 */ -static const char ident[]="$Id$"; - -#if 0 #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <nds.h> + #include "zx81.h" -#include "gfx.h" -#include "gui.h" -#include "config.h" -#include "util.h" -#include "exit.h" -static const char ident_h[]=EZX81_ZX81H; +#include "zx81_bin.h" +#include "maze_bin.h" #ifndef TRUE #define TRUE 1 @@ -79,9 +74,6 @@ static int started=FALSE; #define TXT_W 32 #define TXT_H 24 -#define OFF_X (GFX_WIDTH-SCR_W)/2 -#define OFF_Y (GFX_HEIGHT-SCR_H)/2 - static Z80Byte mem[0x10000]; static Z80Word RAMBOT=0; @@ -94,81 +86,27 @@ static Z80Word RAMLEN=0; /* GFX vars */ -static Uint32 white; -static Uint32 black; - +static uint16 *screen; /* The keyboard */ static Z80Byte matrix[8]; -typedef struct -{ - SDLKey key; - int m1,b1,m2,b2; -} MatrixMap; - -#define KY1(m,b) m,1<<b,-1,-1 -#define KY2(m1,b1,m2,b2) m1,1<<b1,m2,1<<b2 - -static const MatrixMap keymap[]= +static struct { - {SDLK_1, KY1(3,0)}, - {SDLK_2, KY1(3,1)}, - {SDLK_3, KY1(3,2)}, - {SDLK_4, KY1(3,3)}, - {SDLK_5, KY1(3,4)}, - {SDLK_6, KY1(4,4)}, - {SDLK_7, KY1(4,3)}, - {SDLK_8, KY1(4,2)}, - {SDLK_9, KY1(4,1)}, - {SDLK_0, KY1(4,0)}, - - {SDLK_a, KY1(1,0)}, - {SDLK_b, KY1(7,4)}, - {SDLK_c, KY1(0,3)}, - {SDLK_d, KY1(1,2)}, - {SDLK_e, KY1(2,2)}, - {SDLK_f, KY1(1,3)}, - {SDLK_g, KY1(1,4)}, - {SDLK_h, KY1(6,4)}, - {SDLK_i, KY1(5,2)}, - {SDLK_j, KY1(6,3)}, - {SDLK_k, KY1(6,2)}, - {SDLK_l, KY1(6,1)}, - {SDLK_m, KY1(7,2)}, - {SDLK_n, KY1(7,3)}, - {SDLK_o, KY1(5,1)}, - {SDLK_p, KY1(5,0)}, - {SDLK_q, KY1(2,0)}, - {SDLK_r, KY1(2,3)}, - {SDLK_s, KY1(1,1)}, - {SDLK_t, KY1(2,4)}, - {SDLK_u, KY1(5,3)}, - {SDLK_v, KY1(0,4)}, - {SDLK_w, KY1(2,1)}, - {SDLK_x, KY1(0,2)}, - {SDLK_y, KY1(5,4)}, - {SDLK_z, KY1(0,1)}, - - {SDLK_RETURN, KY1(6,0)}, - {SDLK_SPACE, KY1(7,0)}, - - {SDLK_COMMA, KY1(7,1)}, /* In the right place... */ - {SDLK_PERIOD, KY1(7,1)}, /* ...or the right key... */ - - {SDLK_BACKSPACE, KY2(0,0,4,0)}, - {SDLK_DELETE, KY2(0,0,4,0)}, - {SDLK_UP, KY2(0,0,4,3)}, - {SDLK_DOWN, KY2(0,0,4,4)}, - {SDLK_LEFT, KY2(0,0,3,4)}, - {SDLK_RIGHT, KY2(0,0,4,2)}, - - {SDLK_RSHIFT, KY1(0,0)}, - {SDLK_LSHIFT, KY1(0,0)}, - - {SDLK_UNKNOWN, 0,0,0,0}, -}; + int row; + int bit; +} key_matrix[]= + { + {3,0x01}, {3,0x02}, {3,0x04}, {3,0x08}, {3,0x10}, /* 1 - 5 */ + {4,0x10}, {4,0x08}, {4,0x04}, {4,0x02}, {4,0x01}, /* 6 - 0 */ + {2,0x01}, {2,0x02}, {2,0x04}, {2,0x08}, {2,0x10}, /* Q - T */ + {5,0x10}, {5,0x08}, {5,0x04}, {5,0x02}, {5,0x01}, /* Y - P */ + {1,0x01}, {1,0x02}, {1,0x04}, {1,0x08}, {1,0x10}, /* A - G */ + {6,0x10}, {6,0x08}, {6,0x04}, {6,0x02}, {6,0x01}, /* H - NL */ + {0,0x01}, {0,0x02}, {0,0x04}, {0,0x08}, {0,0x10}, /* CAPS - V */ + {7,0x10}, {7,0x08}, {7,0x04}, {7,0x02}, {7,0x01} /* B - SPACE */ + }; /* ---------------------------------------- PRIVATE FUNCTIONS @@ -312,102 +250,12 @@ static const char *ConvertFilename(Z80Word addr) static void LoadTape(Z80 *z80) { - const char *p=ConvertFilename(z80->DE.w); - char path[FILENAME_MAX]; - FILE *fp; - - if (strlen(p)==0) - { - GUIMessage(eMessageBox,"ERROR","Can't load empty filename"); - return; - } - - strcpy(path,SConfig(CONF_TAPEDIR)); - strcat(path,"/"); - strcat(path,p); - strcat(path,".p"); - - if (!(fp=fopen(path,"rb"))) - { - GUIMessage(eMessageBox,"ERROR","Can't load file:\n%s",path); - return; - } - - fread(mem+0x4009,1,0x4000,fp); - fclose(fp); + memcpy(mem+0x4009,maze_bin,maze_bin_size); } static void SaveTape(Z80 *z80) { - const char *p=ConvertFilename(z80->DE.w); - char path[FILENAME_MAX]; - FILE *fp; - Z80Word start; - Z80Word end; - - if (strlen(p)==0) - { - GUIMessage(eMessageBox,"ERROR","Can't save empty filename"); - return; - } - - strcpy(path,SConfig(CONF_TAPEDIR)); - strcat(path,"/"); - strcat(path,p); - strcat(path,".p"); - - if (!(fp=fopen(path,"wb"))) - { - GUIMessage(eMessageBox,"ERROR","Can't write file:\n%s",path); - return; - } - - start=0x4009; - end=(Z80Word)mem[0x4014]|(Z80Word)mem[0x4015]<<8; - - while(start<=end) - putc(mem[start++],fp); - - fclose(fp); -} - - -static void PrintZX81Char(int x,int y,int code,int base) -{ - int cx,cy; - Uint32 fg,bg; - - x*=8; - y*=8; - - if (code&0x80) - { - fg=white; - bg=black; - code-=0x80; - } - else - { - fg=black; - bg=white; - } - - base=base<<8; - base+=code*8; - - for(cy=0;cy<8;cy++) - { - for(cx=0;cx<8;cx++) - { - if (mem[base]&(1<<(7-cx))) - GFXFastPlot(OFF_X+x+cx,OFF_Y+y+cy,fg); - else - GFXFastPlot(OFF_X+x+cx,OFF_Y+y+cy,bg); - } - - base++; - } } @@ -426,7 +274,17 @@ static void DrawScreen(Z80 *z80) while((*scr!=118)&&(x<TXT_W)) { - PrintZX81Char(x,y,*scr++,z80->I); + Z80Byte ch = *scr++; + + if (ch&0x80) + { + screen[x+y*32]=(ch&0x3f)|0x40; + } + else + { + screen[x+y*32]=(ch&0x3f); + } + x++; } @@ -437,17 +295,14 @@ static void DrawScreen(Z80 *z80) static void DrawSnow(Z80 *z80) { - int x,y; + uint16 *s; + int f; - x=0; - y=0; + s = screen; - for(x=0;x<SCR_W;x++) + for(f=0;f<TXT_W*TXT_H;f++) { - for(y=0;y<SCR_H;y++) - { - GFXFastPlot(OFF_X+x,OFF_Y+y,rand()&1 ? white:black); - } + *s++=8; } } @@ -537,8 +392,6 @@ static int CheckTimers(Z80 *z80, Z80Val val) { Z80ResetCycles(z80,val-FRAME_TSTATES); - GFXLock(); - if (started && ((mem[CDFLAG] & 0x80) || waitkey)) { DrawScreen(z80); @@ -546,7 +399,8 @@ static int CheckTimers(Z80 *z80, Z80Val val) } else { - DrawSnow(z80); + /*DrawSnow(z80);*/ + DrawScreen(z80); FRAME_TSTATES=FAST_TSTATES; } @@ -559,12 +413,14 @@ static int CheckTimers(Z80 *z80, Z80Val val) ZX81HouseKeeping(z80); } - GFXUnlock(); - GFXEndFrame(TRUE); - GFXStartFrame(); - } + swiWaitForVBlank(); - return TRUE; + return FALSE; + } + else + { + return TRUE; + } } @@ -599,13 +455,11 @@ static int EDCallback(Z80 *z80, Z80Val data) while(pause-- && !(mem[CDFLAG]&1)) { - SDL_Event *e; + SoftKeyEvent ev; - e=GFXGetKey(); - - if (e) + while (SK_GetEvent(&ev)) { - ZX81KeyEvent(e); + ZX81HandleKey(ev.key,ev.pressed); } CheckTimers(z80,FRAME_TSTATES); @@ -624,25 +478,15 @@ static int EDCallback(Z80 *z80, Z80Val data) /* ---------------------------------------- EXPORTED INTERFACES */ -void ZX81Init(Z80 *z80) +void ZX81Init(uint16 *vram, Z80 *z80) { - FILE *fp; Z80Word f; - if (!(fp=fopen(SConfig(CONF_ROMFILE),"rb"))) - { - GUIMessage(eMessageBox, - "ERROR","Failed to open ZX81 ROM\n%s",SConfig(CONF_ROMFILE)); - Exit(""); - } + screen = vram; - if (fread(mem,1,ROMLEN,fp)!=ROMLEN) - { - fclose(fp); - GUIMessage(eMessageBox, - "ERROR","ROM file must be %d bytes long\n",ROMLEN); - Exit(""); - } + /* Load the ROM + */ + memcpy(mem,zx81_bin,ROMLEN); /* Patch the ROM */ @@ -654,15 +498,11 @@ void ZX81Init(Z80 *z80) */ memcpy(mem+ROMLEN,mem,ROMLEN); - RAMBOT=0x4000; - /* Memory size (1 or 16K) + /* Memory size (16K) */ - if (IConfig(CONF_MEMSIZE)==16) - RAMLEN=0x4000; - else - RAMLEN=0x400; - + RAMBOT=0x4000; + RAMLEN=0x4000; RAMTOP=RAMBOT+RAMLEN; for(f=RAMBOT;f<=RAMTOP;f++) @@ -670,41 +510,25 @@ void ZX81Init(Z80 *z80) for(f=0;f<8;f++) matrix[f]=0x1f; - - white=GFXRGB(230,230,230); - black=GFXRGB(0,0,0); - - GFXStartFrame(); } -void ZX81KeyEvent(SDL_Event *e) +void ZX81HandleKey(SoftKey key, int is_pressed) { - const MatrixMap *m; - - m=keymap; - - while(m->key!=SDLK_UNKNOWN) + if (key<SK_CONFIG) { - if (e->key.keysym.sym==m->key) + if (is_pressed) { - if (e->key.state==SDL_PRESSED) - { - matrix[m->m1]&=~m->b1; - - if (m->m2!=-1) - matrix[m->m2]&=~m->b2; - } - else - { - matrix[m->m1]|=m->b1; - - if (m->m2!=-1) - matrix[m->m2]|=m->b2; - } + matrix[key_matrix[key].row]&=~key_matrix[key].bit; } - - m++; + else + { + matrix[key_matrix[key].row]|=key_matrix[key].bit; + } + } + else + { + /* TODO: Joysticks? */ } } @@ -793,21 +617,6 @@ void ZX81WritePort(Z80 *z80, Z80Word port, Z80Byte val) } -Z80Byte ZX81ReadForDisassem(Z80 *z80, Z80Word addr) -{ - return mem[addr&0x7fff]; -} - - -const char *ZX81Info(Z80 *z80) -{ - static char buff[80]; - - sprintf(buff,"T-State/frame: %lu",FRAME_TSTATES); - return buff; -} - - void ZX81Reset(Z80 *z80) { int f; @@ -815,14 +624,11 @@ void ZX81Reset(Z80 *z80) for(f=0;f<8;f++) matrix[f]=0x1f; + Z80Reset(z80); Z80ResetCycles(z80,0); started=FALSE; - - GFXStartFrame(); } -#endif - /* END OF FILE */ |