summaryrefslogtreecommitdiff
path: root/src/spec.c
blob: a8cb9ebd13d7e3782718326ed4a18b86ab0d4875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*

    espec - Sinclair Spectrum emulator

    Copyright (C) 2003  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
    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

    -------------------------------------------------------------------------

    Provides the emulation for the SPEC

*/
static const char ident[]="$Id$";

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "spec.h"
#include "gfx.h"
#include "gui.h"
#include "config.h"
#include "exit.h"

static const char ident_h[]=ESPEC_SPECH;

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif


/* ---------------------------------------- STATICS
*/
static const int	ROMLEN=0x4000;
static const int	ROM_SAVE=0x4c6;
static const int	ROM_LOAD=0x562;

/* The SPEC screen
*/
#define			GFX_W	320
#define			GFX_H	300
#define			SCR_W	256
#define			SCR_H	192
#define			TXT_W	32
#define			TXT_H	24
#define			SCRDATA	0x4000
#define			ATTR	0x5800

#define			ATTR_AT(x,y)		\
				mem[ATTR+(x)+((y)/8)*32]

static const int	OFF_X=(GFX_W-SCR_W)/2;
static const int	OFF_Y=(GFX_H-SCR_H)/2;

static Z80Byte		mem[0x10000];


/* Number of cycles per scan lines and scan line control
*/
static Z80Val		SCAN_CYCLES=224;
static int		scanline=0;


/* GFX vars
*/
#define			FLASH	16	/* Frames per flash */

static int		flash=0;
static int		flashctr=0;

static int		border=0;

#define			TOPL	64	/* Scanlines before 'first' line */
#define			SCRL	SCR_H	/* Scanlines making up screen data */
#define			BOTL	56	/* Scanlines after 'last' line */

#define			TOTL	(TOPL+SCRL+BOTL)

#define 		NVAL	235	/* Normal RGB intensity */
#define 		BVAL	255	/* Bright RGB intensity */

static struct
{
    Uint32	col;
    int		r,g,b;
} coltable[16]=
{
    {0,     0x00,0x00,0x00},        /* BLACK */
    {0,     0x00,0x00,NVAL},        /* BLUE */
    {0,     NVAL,0x00,0x00},        /* RED */
    {0,     NVAL,0x00,NVAL},        /* MAGENTA */
    {0,     0x00,NVAL,0x00},        /* GREEN */
    {0,     0x00,NVAL,NVAL},        /* CYAN */
    {0,     NVAL,NVAL,0x00},        /* YELLOW */
    {0,     NVAL,NVAL,NVAL},        /* WHITE */

    {0,     0x00,0x00,0x00},        /* BLACK */
    {0,     0x00,0x00,BVAL},        /* BLUE */
    {0,     BVAL,0x00,0x00},        /* RED */
    {0,     BVAL,0x00,BVAL},        /* MAGENTA */
    {0,     0x00,BVAL,0x00},        /* GREEN */
    {0,     0x00,BVAL,BVAL},        /* CYAN */
    {0,     BVAL,BVAL,0x00},        /* YELLOW */
    {0,     BVAL,BVAL,BVAL},        /* WHITE */

};


/* 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[]=
{
    {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_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_LSHIFT,	KY1(0,0)},
    {SDLK_RSHIFT,	KY1(7,1)},
    {SDLK_LCTRL,	KY1(0,0)},
    {SDLK_RCTRL,	KY1(7,1)},
    {SDLK_LALT,		KY1(0,0)},
    {SDLK_RALT,		KY1(7,1)},
    {SDLK_CAPSLOCK,	KY2(0,0,3,1)},

    {SDLK_UNKNOWN,	0,0,0,0},
};


/* ---------------------------------------- PRIVATE FUNCTIONS
*/
void DrawScanline(int y)
{
    int aline;
    int f,r;
    int ink,paper,t;
    Z80Byte *scr;
    Z80Byte b;
    Z80Byte att;

    aline=scanline-TOPL;

    GFXHLine(0,GFX_W-1,y,coltable[border].col);

    if (aline>=0 && aline<SCRL)
    {
	GFXLock();

	scr=mem+SCRDATA+aline*TXT_W;

	for(f=0;f<TXT_W;f++)
	{
	    att=ATTR_AT(f,aline);

	    ink=(att&0x07);
	    paper=(att&0x38)>>3;

	    if (att&0x40)
	    {
		ink+=8;
		paper+=8;
	    }

	    if ((att&0x80)&&(flash))
	    {
		t=ink;
		ink=paper;
		paper=t;
	    }

	    for(r=7,b=*scr++;r>=0;r--)
		if (b&(1<<r))
		    GFXFastPlot(f*8+r+OFF_X,y,coltable[ink].col);
		else
		    GFXFastPlot(f*8+r+OFF_X,y,coltable[paper].col);
	}

	GFXUnlock();
    }
}


static void RomPatch(void)
{
    static const Z80Byte save[]=
    {
    	0xed, 0xf0,		/* ED F0 illegal op	*/
	0xc9,			/* RET 			*/
	0xff			/* End of patch		*/
    };

    static const Z80Byte load[]=
    {
	0x08,			/* EX AF,AF'		*/
    	0xed, 0xf1,		/* ED F1 illegal op	*/
	0xc9,			/* RET 			*/
	0xff			/* End of patch		*/
    };

    int f;

    for(f=0;save[f]!=0xff;f++)
    	mem[ROM_SAVE+f]=save[f];

    for(f=0;load[f]!=0xff;f++)
    	mem[ROM_LOAD+f]=load[f];
}



static void LoadTape(Z80State *state)
{
    state->AF|=Z80_F_Carry;
}


static void SaveTape(Z80State *state)
{
    state->AF|=Z80_F_Carry;
}


static int EDCallback(Z80 *z80, Z80Val data)
{
    Z80State state;

    Z80GetState(z80,&state);

    switch((Z80Byte)data)
    {
    	case 0xf0:
	    SaveTape(&state);
	    break;

    	case 0xf1:
	    LoadTape(&state);
	    break;

	default:
	    break;
    }

    Z80SetState(z80,&state);

    return TRUE;
}


static int CheckTimers(Z80 *z80, Z80Val val)
{
    if (val>SCAN_CYCLES)
    {
	int y;

	Z80ResetCycles(z80,val-SCAN_CYCLES);

	/* Increment scan line and check for frame flyback
	*/
	scanline++;

	if (scanline==TOTL)
	{
	    scanline=0;

	    flashctr++;

	    if (flashctr==FLASH)
	    {
	    	flash^=1;
		flashctr=0;
	    }

	    Z80Interrupt(z80,0xff);

	    GFXEndFrame(TRUE);
	    GFXStartFrame();
	}

	/* Draw scanline
	y=OFF_X-TOPL+scanline;
	*/
	y=scanline-TOPL+OFF_Y;

	if (y>=0 && y<GFX_H)
	    DrawScanline(y);

	/* TODO: Process sound emulation */
    }

    return TRUE;
}


/* ---------------------------------------- EXPORTED INTERFACES
*/
void SPECInit(Z80 *z80)
{
    FILE *fp;
    Z80Word f;

    if (!(fp=fopen(SConfig(CONF_ROMFILE),"rb")))
    {
	GUIMessage("ERROR","Failed to open Spectrum ROM\n%s",
						SConfig(CONF_ROMFILE));
	Exit("");
    }

    if (fread(mem,1,ROMLEN,fp)!=ROMLEN)
    {
    	fclose(fp);
	GUIMessage("ERROR","ROM file must be %d bytes long\n",ROMLEN);
	Exit("");
    }

    /* Patch the ROM
    */
    RomPatch();
    Z80LodgeCallback(z80,Z80_EDHook,EDCallback);
    Z80LodgeCallback(z80,Z80_Fetch,CheckTimers);

    /* Set up the keyboard
    */
    for(f=0;f<8;f++)
    	matrix[f]=0x1f;

    /* Set up the colours
    */
    for(f=0;f<16;f++)
    	coltable[f].col=GFXRGB(coltable[f].r,coltable[f].g,coltable[f].b);

    scanline=0;
    flash=0;
    flashctr=0;

    GFXStartFrame();
}


void SPECKeyEvent(SDL_Event *e)
{
    const MatrixMap *m;

    m=keymap;

    while(m->key!=SDLK_UNKNOWN)
    {
    	if (e->key.keysym.sym==m->key)
	{
	    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;
	    }
	}

	m++;
    }
}


Z80Byte SPECReadMem(Z80 *z80, Z80Word addr)
{
    /* TODO: Emulation of contention */
    return mem[addr];
}


void SPECWriteMem(Z80 *z80, Z80Word addr, Z80Byte val)
{
    if (addr>=ROMLEN)
	mem[addr]=val;
}


Z80Byte SPECReadPort(Z80 *z80, Z80Word port)
{
    Z80Byte lo=port&0xff;
    Z80Byte hi=port>>8;
    Z80Byte b=0xff;
    int f;

    switch(lo)
    {
	case 0x1f:	/* TODO: Kempston joystick */
	    break;

	case 0x7f:	/* TODO: Fuller joystick */
	    break;

	case 0xfb:	/* TODO: ZX Printer */
	    break;

    	case 0x01:	/* ULA */
	    /* Key matrix
	    */
	    b=0;

	    for(f=0;f<8;f++)
	    	if (!(hi&(1<<f)))
		    b&=matrix[f];

	    b|=0xa0;

	    /* TODO: Emulation of contention */

	    break;

	default:
	    b=0xff;
	    break;
    }

    return b;
}


void SPECWritePort(Z80 *z80, Z80Word port, Z80Byte val)
{
    Z80Byte lo=port&0xff;

    switch(lo)
    {
	case 0x01:	/* ULA */
	    border=val&0x07;
	    break;

	default:
	    break;
    }
}


Z80Byte SPECReadForDisassem(Z80 *z80, Z80Word addr)
{
    return mem[addr];
}


const char *SPECInfo(Z80 *z80)
{
    static char buff[80]={0};

    return buff;
}


/* END OF FILE */