summaryrefslogtreecommitdiff
path: root/src/spec.c
blob: 51006df528ad22a512b1d9ce42619bd58a43ade5 (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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*

    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=0x2000;
static const int	ROM_SAVE=0x2fc;
static const int	ROM_LOAD=0x347;

/* No of cycles in each 64us HSYNC (hopefully)
*/
static const int	HSYNC_PERIOD=321;

/* The SPEC screen
*/
static const int	SCR_W=256;
static const int	SCR_H=192;
static const int	TXT_W=32;
static const int	TXT_H=24;

/* These assume a 320x200 screen
*/
static const int	OFF_X=(320-256)/2;
static const int	OFF_Y=(200-192)/2;

static Z80Byte		mem[0x10000];

static Z80Word		RAMBOT=0;
static Z80Word		RAMTOP=0;
static Z80Word		RAMLEN=0;

/* Counter used when triggering the interrupts for the display
*/
static int		nmigen=FALSE;
static int		hsync=FALSE;

/* The ULA
*/
static struct
{
    int		x;
    int		y;
    int		c;
    int		release;
} ULA;

/* GFX vars
*/
static Uint32		white;
static Uint32		black;


/* 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_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},
};


/* ---------------------------------------- PRIVATE FUNCTIONS
*/
static void RomPatch(void)
{
    static const Z80Byte save[]=
    {
    	0xed, 0xf0,		/* ED F0 illegal op	*/
	0xc3, 0x08, 0x02,	/* JP $0207		*/
	0xff			/* End of patch		*/
    };

    static const Z80Byte load[]=
    {
    	0xed, 0xf1,		/* ED F0 illegal op	*/
	0xc3, 0x08, 0x02,	/* JP $0207		*/
	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 char ToASCII(Z80Byte b)
{
    if (b==0)			/* SPACE */
    	return ' ';
    
    if (b==22)			/* Dash (-) */
    	return '-';

    if (b>=28 && b<=37)		/* 0-9 */
    	return '0'+b-28;

    if (b>=38 && b<=63)		/* A-Z */
    	return 'a'+b-38;

    return 0;
}


static const char *ConvertFilename(Z80Word addr)
{
    static char buff[FILENAME_MAX];
    char *p;

    p=buff;
    *p=0;

    if (addr>0x8000)
    	return buff;

    do
    {
    	char c=ToASCII(mem[addr]&0x7f);

	if (c)
	    *p++=c;

    } while(mem[addr++]<0x80);

    *p=0;

    return buff;
}


static void LoadTape(Z80State *state)
{
    const char *p=ConvertFilename(state->DE);
    char path[FILENAME_MAX];
    FILE *fp;
    Z80Word addr;
    int c;

    if (strlen(p)==0)
    {
    	GUIMessage("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("ERROR","Can't load file:\n%s",path);
	return;
    }

    addr=0x4009;

    while((c=getc(fp))!=EOF)
    {
	if (addr>=0x4000)
	    mem[addr]=(Z80Byte)c;

	addr++;
    }

    fclose(fp);
}


static void SaveTape(Z80State *state)
{
    const char *p=ConvertFilename(state->DE);
    char path[FILENAME_MAX];
    FILE *fp;
    Z80Word start;
    Z80Word end;

    if (strlen(p)==0)
    {
    	GUIMessage("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("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 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;
    }

    return TRUE;
}


static void ULA_Video_Shifter(Z80 *z80, Z80Byte val)
{
    Z80State state;
    Z80Word base;
    int x,y;
    int inv;
    int b;

    Z80GetState(z80,&state);

    /* Extra check due to out dodgy ULA emulation
    */
    if (ULA.y>=0 && ULA.y<SCR_H)
    {
	Uint32 fg,bg;

    	/* Position on screen corresponding to ULA
	*/
	x=OFF_X+ULA.x*8;
	y=OFF_Y+ULA.y;

	/* Get ULA invert state and clear to ULA 6-but code
	*/
	inv=val&0x80;
	val&=0x3f;

	base=((Z80Word)state.I<<8)|(val<<3)|ULA.c;

	if (inv)
	{
	    fg=white;
	    bg=black;
	}
	else
	{
	    fg=black;
	    bg=white;
	}

	for(b=0;b<8;b++)
	{
	    if (mem[base]&(1<<(7-b)))
	    	GFXPlot(x+b,y,fg);
	    else
	    	GFXPlot(x+b,y,bg);
	}
    }

    ULA.x=(ULA.x+1)&0x1f;

    if (ULA.x==0)
    	Z80Interrupt(z80,0xff);
}


static int CheckTimers(Z80 *z80, Z80Val val)
{
    if (val>HSYNC_PERIOD)
    {
	Z80ResetCycles(z80,0);

	if (nmigen)
	{
	    Z80NMI(z80,0xff);
	    printf("NMIGEN\n");
	}
	else if (hsync)
	{
	    printf("HSYNC\n");
	    if (ULA.release)
	    {
	    	/* ULA.release=FALSE; */
		ULA.c=(ULA.c+1)&7;
		ULA.y++;
		ULA.x=0;
	    }
	}
    }

    return TRUE;
}


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

    if (!(fp=fopen(SConfig(CONF_ROMFILE),"rb")))
    {
	GUIMessage("ERROR","Failed to open SPEC 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);

    /* Mirror the ROM
    */
    memcpy(mem+ROMLEN,mem,ROMLEN);

    RAMBOT=0x4000;

    /* Memory size (1 or 16K)
    */
    if (IConfig(CONF_MEMSIZE)==16)
	RAMLEN=0x2000;
    else
	RAMLEN=0x400;

    RAMTOP=RAMBOT+RAMLEN;

    for(f=RAMBOT;f<=RAMTOP;f++)
    	mem[f]=0;

    for(f=0;f<8;f++)
    	matrix[f]=0x1f;

    white=GFXRGB(230,230,230);
    black=GFXRGB(0,0,0);

    nmigen=FALSE;
    hsync=FALSE;

    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)
{
    /* Memory reads above 32K invoke the ULA
    */
    if (addr>0x7fff)
    {
	Z80Byte b;

        /* B6 of R is tied to the IRQ line (only when HSYNC active?)
        if ((HSYNC)&&(!(z80->R&0x40)))
            z80->IRQ=TRUE;
        */

        b=mem[addr&0x7fff];

        /* If bit 6 of the opcode is set the opcode is sent as is to the
           Z80.  If it's not, the byte is interretted by the ULA.
        */
        if (b&0x40)
	{
            ULA_Video_Shifter(z80,0);
	}
        else
	{
            ULA_Video_Shifter(z80,b);
            b=0;
	}

	return b;
    }
    else
        return mem[addr&0x7fff];

}


void SPECWriteMem(Z80 *z80, Z80Word addr, Z80Byte val)
{
    addr=addr&0x7fff;

    if (addr>=RAMBOT && addr<=RAMTOP)
	mem[addr]=val;
}


Z80Byte SPECReadPort(Z80 *z80, Z80Word port)
{
    Z80Byte b=0;

    printf("IN  %4.4x\n",port);

    switch(port&0xff)
    {
    	case 0xfe:	/* ULA */
	    /* Key matrix
	    */
	    switch(port&0xff00)
	    {
	    	case 0xfe00:
		    b=matrix[0];
		    break;
	    	case 0xfd00:
		    b=matrix[1];
		    break;
	    	case 0xfb00:
		    b=matrix[2];
		    break;
	    	case 0xf700:
		    b=matrix[3];
		    break;
	    	case 0xef00:
		    b=matrix[4];
		    break;
	    	case 0xdf00:
		    b=matrix[5];
		    break;
	    	case 0xbf00:
		    b=matrix[6];
		    break;
	    	case 0x7f00:
		    b=matrix[7];
		    break;
	    }

	    /* Turn off HSYNC if NMI generator is OFF and redraw screen to
	       match output ULA has since sent out.
	    */
	    if (!nmigen && hsync)
	    {
	    	hsync=FALSE;

		GFXEndFrame(TRUE);
		GFXClear(white);

		ULA.x=0;
		ULA.y=-1;
		ULA.c=7;
		ULA.release=FALSE;

		GFXStartFrame();
	    }
	    else
	    {
	    	/* Reset and hold ULA counter
		*/
		ULA.c=0;
		ULA.release=FALSE;
	    }
	    break;

	default:
	    break;
    }

    return b;
}


void SPECWritePort(Z80 *z80, Z80Word port, Z80Byte val)
{
    printf("OUT %4.4x\n",port);

    /* Any port write releases the ULA line counter
    */
    ULA.release=TRUE;

    switch(port&0xff)
    {
    	case 0xfd:	/* NMI generator OFF */
	    nmigen=FALSE;
	    break;

	case 0xfe:	/* NMI generator ON */
	    nmigen=TRUE;
	    break;

	case 0xff:	/* HSYNC generator ON */
	    hsync=TRUE;
	    Z80ResetCycles(z80,0);
	    break;
    }
}


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


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

    sprintf(buff,"NMIGEN: %s  HSYNC: %s",
		    nmigen ? "ON":"OFF",
		    hsync ? "ON":"OFF");

    return buff;
}


/* END OF FILE */