summaryrefslogtreecommitdiff
path: root/source/spec.c
blob: e5932da3a48d7942edf16636ecc424201b5adf47 (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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
    3dsspec - Nintendo 3DS Spectrum emulator

    Copyright (C) 2021  Ian Cowburn <ianc@noddybox.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 3 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, see <http://www.gnu.org/licenses/>

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

    Provides the emulation for the Spectrum

*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <3ds.h>

#include "spec.h"
#include "gui.h"
#include "framebuffer.h"

#include "stream.h"

#include "config.h"

#include "spec48_bin.h"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

/* ---------------------------------------- STATICS
*/
#define ROMLEN		0x4000
#define ROM_SAVE	0x2fc
#define ROM_LOAD	0x347

#define ED_SAVE		0xf0
#define ED_LOAD		0xf1

#define SLOW_TSTATES	16000
#define FAST_TSTATES	64000

#define E_LINE		16404
#define LASTK1		16421
#define LASTK2		16422
#define MARGIN		16424
#define FRAMES		16436
#define CDFLAG		16443

#define NMI_PERIOD      208

/* The SPEC screen and memory
*/
static int              fast_mode=FALSE;
static int              nmigen=FALSE;
static int              hsync=FALSE;
static int              drawing_screen=FALSE;
static int              ula_line_counter;

static u16              black;
static u16              white;

#define	SCR_W		256
#define	SCR_H		192
#define	TXT_W		32
#define	TXT_H		24

static Z80Byte		mem[0x10000];

static u16              screen[SCR_W*SCR_H];
static int              ula_ptr;

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

#define DFILE		0x400c

#define WORD(a)		(mem[a] | (Z80Word)mem[a+1]<<8)

/* Tape
*/
static int		enable_filesystem;
static int		allow_save;
static const Z80Byte	*tape_image;
static int		tape_len;

static char		last_dir[FILENAME_MAX] = "/";

/* The keyboard
*/
static Z80Byte		matrix[8];

static struct
{
    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
*/
#define PEEKW(addr)		(mem[addr] | (Z80Word)mem[addr+1]<<8)

#define POKEW(addr,val)         do					\
                                {					\
                                    Z80Word wa=addr;			\
                                    Z80Word wv=val;			\
                                    mem[wa]=wv;				\
                                    mem[wa+1]=wv>>8;			\
                                } while(0)

static void RomPatch(void)
{
    static const Z80Byte save[]=
    {
    	0xed, ED_SAVE,		/* (SAVE)		*/
	0xc3, 0x07, 0x02,	/* JP $0207		*/
	0xff			/* End of patch		*/
    };

    static const Z80Byte load[]=
    {
    	0xed, ED_LOAD,		/* (LOAD)		*/
	0xc3, 0x07, 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];
    }
}

/* Open a tape file the passed address
*/
static FILE *OpenTapeFile(Z80Word addr, int *cancelled, const char *mode)
{
    static const char zx_chars[] = "\"#$:?()><=+-*/;,."
				   "0123456789"
				   "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    FILE *fp;
    char full_fn[FILENAME_MAX] = DEFAULT_SNAPDIR;
    char fn[FILENAME_MAX];
    int f;
    int done;

    fp = NULL;
    f = 0;
    done = FALSE;
    *cancelled = FALSE;

    while(f<(FILENAME_MAX-3) && !done)
    {
    	int ch;

	ch = mem[addr++];

	if (ch&0x80)
	{
	    done = TRUE;
	    ch &= 0x7f;
	}

	if (ch>=11 && ch<=63)
	{
	    fn[f++] = zx_chars[ch-11];
	}
    }

    if (fn[0] == '*')
    {
    	if (GUI_FileSelect(last_dir,fn,".P"))
	{
	    fp = fopen(fn, mode);
	}
	else
	{
	    *cancelled = TRUE;
	}

	SK_DisplayKeyboard();
    }
    else
    {
	fn[f++] = '.';
	fn[f++] = 'P';
	fn[f] = 0;

	strcat(full_fn,fn);

	if (!(fp = fopen(full_fn, mode)))
	{
	    fp = fopen(fn, mode);
	}
    }

    return fp;
}


static void LoadInternalTape(Z80 *z80)
{
    memcpy(mem+0x4009,tape_image,tape_len);
}


static void LoadExternalTape(FILE *tape, Z80 *z80)
{
    int c;
    Z80Byte *a;

    a=mem+0x4009;

    while((c=getc(tape))!=EOF)
    {
    	*a++=c;
    }
}


static void SaveExternalTape(FILE *tape, Z80 *z80)
{
    int f;
    int end;

    f = 0x4009;
    end = WORD(E_LINE);

    while(f <= end)
    {
    	fputc(mem[f++], tape);
    }
}


static int CheckTimers(Z80 *z80, Z80Val val)
{
    static Z80Word last_PC;
    static Z80Byte last_R;
    int ret = TRUE;

    /* See if we're started or stopping executing the display file
    */
    if (z80->PC > 0x7fff)
    {
        if (last_PC < 0x8000)
        {
            drawing_screen = TRUE;
            ula_ptr = 0;
        }
    }

    if (z80->PC < 0x8000)
    {
        if (last_PC > 0x7fff && ula_ptr == SCR_W * SCR_H)
        {
            drawing_screen = FALSE;
            ula_ptr = 0;
            Z80ResetCycles(z80, val - SLOW_TSTATES);
            ret = FALSE;
        }
    }

    last_PC = z80->PC;

    /* Check HSYNC generation
    */
    if (hsync && !nmigen)
    {
        if (last_R & 0x40 && !(z80->R & 0x40))
        {
            Z80Interrupt(z80, 0xff);
            ula_line_counter = (ula_line_counter + 1) % 8;
        }
    }

    last_R = z80->R;

    /* Check NMI generator
    */
    if (nmigen && val > NMI_PERIOD)
    {
        Z80NMI(z80);
        Z80ResetCycles(z80, val - NMI_PERIOD);
    }

    /* We should only be able to execute FAST_TSTATES if we're not drawing the
       screen.
    */
    if (val >= FAST_TSTATES)
    {
	Z80ResetCycles(z80,val-FAST_TSTATES);

	if (mem[CDFLAG] & 0x80)
	{
	    fast_mode = FALSE;
	}
	else
	{
            fast_mode = TRUE;
	}

	ret = FALSE;
    }

    return ret;
}


static int EDCallback(Z80 *z80, Z80Val data)
{
    switch((Z80Byte)data)
    {
    	case ED_SAVE:
	    if (allow_save && z80->DE.w<0x8000)
	    {
		FILE *fp;
		int cancel;

		if ((fp=OpenTapeFile(z80->HL.w, &cancel, "wb")))
		{
		    SaveExternalTape(fp,z80);
		    fclose(fp);
		}
	    }
	    break;

    	case ED_LOAD:
	    /* Try and load the external file if a name given.  Otherwise, we
	       try the internal one.  Some of this is slightly dodgy -- it was
	       never intended for the emulator to be doing any GUI related
	       nonsense (like the alerts) but simply emulating.
	    */
	    if (enable_filesystem && z80->DE.w<0x8000)
	    {
		FILE *fp;
		int cancel;

		if ((fp=OpenTapeFile(z80->DE.w, &cancel, "rb")))
		{
		    LoadExternalTape(fp,z80);
		    fclose(fp);
		}
		else
		{
		    if (!cancel)
		    {
			GUI_Alert(FALSE,"Couldn't open tape");
			SK_DisplayKeyboard();
		    }
		}
	    }
	    else
	    {
		if (tape_image)
		{
		    LoadInternalTape(z80);
		}
		else
		{
		    GUI_Alert(FALSE,"No tape image selected");
		    SK_DisplayKeyboard();
		}
	    }

	    mem[CDFLAG]=0xc0;
	    break;

	default:
	    break;
    }

    return TRUE;
}


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

    black = FB_GetColour(COL_BLACK);
    white = FB_GetColour(COL_WHITE);

    /* Load the ROM
    */
    memcpy(mem,spec48_bin,ROMLEN);

    /* Patch the ROM
    */
    RomPatch();
    Z80LodgeCallback(z80,eZ80_EDHook,EDCallback);
    Z80LodgeCallback(z80,eZ80_Instruction,CheckTimers);

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

    /* Memory size (16K)
    */
    RAMBOT=0x4000;
    RAMTOP=RAMBOT+0x4000;

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

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


void SPECRenderDisplay(Framebuffer *fb, Z80 *z80)
{
    if (fast_mode)
    {
        u16 x;
        u16 y;

        for(x = 0; x < fb->width; x++)
        {
            for(y = 0; y < fb->height; y++)
            {
                FB_ADDR(fb, x, y) = ((x+y) % 2) ? black : white;
            }
        }
    }
    else
    {
        int x;
        int y;

        FB_Clear(fb, COL_WHITE);

        for(y = 0; y < SCR_H; y++)
        {
            for(x = 0; x < SCR_W; x++)
            {
                FB_ADDR(fb, 72 + x, fb->height - 24 - y) =
                                            screen[x + y * SCR_W];
            }
        }
    }
}


void SPECHandleKey(SoftKey key, int is_pressed)
{
    if (key<SK_CONFIG)
    {
	if (is_pressed)
	{
	    matrix[key_matrix[key].row]&=~key_matrix[key].bit;
	}
	else
	{
	    matrix[key_matrix[key].row]|=key_matrix[key].bit;
	}
    }
    else
    {
    	/* TODO: Joysticks?  Were there any common ones for the 81? */
    }
}


Z80Byte SPECReadMem(Z80 *z80, Z80Word addr)
{
    if (addr & 0x8000)
    {
        Z80Byte b;

        b = mem[addr & 0x7fff];

        /* If bit 6 of the byte is set it is sent to the CPU, otherwise it is
           used to generate the video signal and zero sent to the CPU
        */
        if (b & 0x40)
        {
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
            screen[ula_ptr++] = white;
        }
        else
        {
            Z80Word base;
            int inv;
            int f;

            inv=b & 0x80;
            b &= 0x3f;

            base = ((Z80Word)z80->I<<8)|(b<<3)|ula_line_counter;

            b = mem[base];

            for(f = 0; f < 8; f++)
            {
                if (b & 0x80)
                {
                    screen[ula_ptr++] = rand(); // inv ? white : black;
                }
                else
                {
                    screen[ula_ptr++] = rand(); // inv ? black : white;
                }

                b = b << 1;
            }

            b = 0;
        }

        return b;
    }
    else
    {
        return mem[addr];
    }
}


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


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

    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;
	    }

	    /* Some code expects some of the top bits set...  Of course, whether
	       or not this may be worse as other code doesn't expect the bits,
	       we shall find out!
	    */
	    b |= 0x60;

            /* Reset ULA line counter
            */
            ula_line_counter = 0;

	    break;

	default:
	    b = 0xff;	/* Idle bus */
	    break;
    }

    return b;
}


void SPECWritePort(Z80 *z80, Z80Word port, Z80Byte val)
{
    switch(port&0xff)
    {
    	case 0xfd:              /* NMI generator OFF */
            nmigen = FALSE;
	    break;

	case 0xfe:              /* NMI generator ON */
            nmigen = TRUE;
            Z80ResetCycles(z80, 0);
	    break;

        case 0xff:              /* HSYNC generator ON */
            hsync = TRUE;
            break;
    }
}


void SPECReset(Z80 *z80)
{
    int f;

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

    Z80Reset(z80);
    Z80ResetCycles(z80,0);

    nmigen = FALSE;
    hsync = FALSE;
}


void SPECEnableFileSystem(int enable)
{
    enable_filesystem=enable;
}


void SPECSetTape(const Z80Byte *image, int len)
{
    tape_image=image;
    tape_len=len;
}


void SPECReconfigure(void)
{
    allow_save = enable_filesystem && DSSPEC_Config[DSSPEC_ALLOW_TAPE_SAVE];
}


void SPECSaveSnapshot(FILE *fp)
{
    int f;

    for(f=0; f<sizeof mem; f++)
    {
	PUT_Byte(fp, mem[f]);
    }

    for(f=0; f<sizeof matrix; f++)
    {
	PUT_Byte(fp, matrix[f]);
    }

    PUT_ULong(fp, RAMBOT);
    PUT_ULong(fp, RAMTOP);
}


void SPECLoadSnapshot(FILE *fp)
{
    int f;

    for(f=0; f<sizeof mem; f++)
    {
	mem[f] = GET_Byte(fp);
    }

    for(f=0; f<sizeof matrix; f++)
    {
	matrix[f] = GET_Byte(fp);
    }

    RAMBOT = GET_ULong(fp);
    RAMTOP = GET_ULong(fp);
}


/* END OF FILE */