-
Notifications
You must be signed in to change notification settings - Fork 5
/
gzx.c
543 lines (468 loc) · 12.3 KB
/
gzx.c
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
/*
* GZX - George's ZX Spectrum Emulator
* Main module
*
* Copyright (c) 1999-2024 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#undef LOG
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "clock.h"
#include "memio.h"
#include "midi.h"
#include "mgfx.h"
#include "gzx.h"
#include "iorec.h"
#include "z80.h"
#include "zx_kbd.h"
#include "zx_scr.h"
#include "rs232.h"
#include "snap.h"
#include "tape/quick.h"
#include "ui/display.h"
#include "ui/fdlg.h"
#include "ui/hwopts.h"
#include "ui/mainmenu.h"
#include "ui/tapemenu.h"
#include "zx_sound.h"
#include "ay.h"
#include "midi.h"
#include "debug.h"
#include "xmap.h"
#include "xtrace.h"
#include "z80g.h"
#include "zx.h"
#include "sys_all.h"
#include "sysmidi.h"
/*
Clock comparison is calculated in unsigned long. It works as long
as u.long has least 32 bits, and as long
as the clocks don't diverge by more than 2^31 T-states (=~600s).
(Much more than what is needed.)
*/
#define CLOCK_LT(a,b) ( (((a)-(b)) >> 31) != 0 )
#define CLOCK_GE(a,b) ( (((a)-(b)) >> 31) == 0 )
static void zx_scr_save(void);
static void zx_proc_instr(void);
int scr_no=0;
FILE *logfi;
static unsigned long disp_t;
int quit=0;
int slow_load=0;
/** User interface lock */
static bool ui_lock = false;
/* Start up working directory */
/* ... used as base for finding the ROM files */
char *start_dir;
/** MIDI device specification */
const char *midi_dev;
/** I/O recording */
iorec_t *iorec;
int key_lalt_held;
int key_lshift_held;
static uint8_t tape_smp;
/** Lock down user interface */
void gzx_ui_lock(void)
{
ui_lock = true;
}
/** Notify on 48K mode change.
*
* 48K mode is on if we're Spectrum 48K (or lower) or if we are Spectrum
* 128K or higher locked in 48K emulation mode.
*/
void gzx_notify_mode_48k(bool mode48k)
{
/* Tape needs to know to handle Stop the tape if in 48K mode */
if (tape_deck != NULL)
tape_deck_set_48k(tape_deck, mode48k);
}
void gzx_toggle_dbl_ln(void)
{
mgfx_toggle_dbl_ln();
zx_scr_disp_fast();
}
static void key_unmod(wkey_t *k)
{
switch(k->key) {
case WKEY_ESC:
main_menu();
break;
case WKEY_F1:
zx_load_snap("test.sna");
break;
case WKEY_F2:
save_snap_dialog();
if (0) zx_scr_save(); // XXX
break;
case WKEY_F4: display_menu(); break;
case WKEY_F3: load_snap_dialog(); break;
case WKEY_F5: tape_menu(); break;
case WKEY_F6: hwopts_menu(); break;
case WKEY_F7: zx_select_memmodel(ZXM_48K); zx_reset(); break;
case WKEY_F8: zx_select_memmodel(ZXM_128K); zx_reset(); break;
case WKEY_F9: select_tapefile_dialog(); break;
case WKEY_F10: printf("F10 pressed\n"); quit=1; break;
case WKEY_F11: mgfx_toggle_fs(); break;
case WKEY_NPLUS: tape_deck_play(tape_deck); break;
case WKEY_NMINUS: tape_deck_stop(tape_deck); break;
case WKEY_NSTAR: tape_deck_rewind(tape_deck); break;
case WKEY_NSLASH: slow_load=!slow_load; break;
#ifdef XMAP
case WKEY_N5: xmap_clear(); break;
#endif
case WKEY_F12: debugger_run(&dbg); break;
default: break;
}
}
static void key_lalt(wkey_t *k)
{
switch(k->key) {
case WKEY_R:
if (iorec == NULL)
(void) iorec_open("out.ior", &iorec);
break;
case WKEY_T:
if (iorec != NULL) {
iorec_close(iorec);
iorec = NULL;
}
break;
case WKEY_W:
key_lalt_held = 0;
rec_audio_dialog();
break;
case WKEY_E:
printf("Stopping audio capture.\n");
zx_sound_stop_capture();
break;
case WKEY_N:
zx_scr_prev_bg();
break;
case WKEY_M:
zx_scr_next_bg();
break;
case WKEY_9:
zx_scr_mode(0);
break;
case WKEY_0:
zx_scr_mode(1);
break;
}
}
static void key_lalt_lshift(wkey_t *k)
{
switch(k->key) {
case WKEY_L:
ui_lock = true;
break;
case WKEY_U:
ui_lock = false;
break;
}
}
/** Currently we always map cursor keys to Kempston joystick */
static void key_joy_state_set(int key, int press)
{
switch (key) {
case WKEY_UP:
kempston_joy_set_reset(&kjoy0, kempston_up, press);
break;
case WKEY_DOWN:
kempston_joy_set_reset(&kjoy0, kempston_down, press);
break;
case WKEY_LEFT:
kempston_joy_set_reset(&kjoy0, kempston_left, press);
break;
case WKEY_RIGHT:
kempston_joy_set_reset(&kjoy0, kempston_right, press);
break;
case WKEY_INS:
kempston_joy_set_reset(&kjoy0, kempston_button_1, press);
break;
case WKEY_DEL:
kempston_joy_set_reset(&kjoy0, kempston_button_2, press);
break;
case WKEY_HOME:
kempston_joy_set_reset(&kjoy0, kempston_button_3, press);
break;
}
}
static void key_handler(wkey_t *k) {
if (k->key == WKEY_LALT) {
key_lalt_held = k->press;
return;
}
if (k->key == WKEY_LSHIFT)
key_lshift_held = k->press;
if (key_lalt_held && key_lshift_held && k->press) {
key_lalt_lshift(k);
return;
}
if (key_lalt_held && k->press && !ui_lock) {
key_lalt(k);
return;
}
if(k->key>=KST_SIZE) {
printf("warning. got a key with a too high scancode (>=KST_SIZE)\n");
printf("ignoring key\n");
return;
}
zx_key_state_set(&keys, k->key, k->press?1:0);
key_joy_state_set(k->key, k->press?1:0);
if (k->press && !ui_lock) {
key_unmod(k);
}
}
static void zx_scr_save(void) {
FILE *f;
char name[32];
snprintf(name, 32, "scr%04d.bin",scr_no++);
f=fopen(name,"wb");
fwrite(zxscr,1,0x1B00,f);
fclose(f);
}
/** Value was written to AY I/O port.
*
* @param arg Callback argument
* @param val Value
*/
static void gzx_ay_ioport_write(void *arg, uint8_t val)
{
rs232_write(&rs232, val);
}
/** Character was sent via RS-232 port */
static void gzx_rs232_sendchar(void *arg, uint8_t val)
{
midi_port_write(&midi, val);
}
/** Midi event was sent via MIDI port */
static void gzx_midi_msg(void *arg, midi_msg_t *msg)
{
#ifdef WITH_MIDI
sysmidi_send_msg(z80_clock, msg);
#endif
}
static unsigned long snd_t,tapp_t;
void zx_reset(void) {
#ifdef XTRACE
xtrace_reset();
#endif
if (gpu_is_on()) {
gpu_reset();
gpu_disable();
}
zx_scr_reset();
z80_reset();
ay_reset(&ay0);
/* select default banks */
bnk_lock48=0;
zx_out8(0x7ffd,0x07);
}
static int zx_init(void) {
// printf("coreleft:%lu\n",coreleft());
z80_init_tables();
/* important! otherwise zx_select_memmodel would crash reallocing */
zxrom=NULL;
zxram=NULL;
zx_select_memmodel(ZXM_48K);
gpu_init();
// if (gpu_enable() < 0)
// return -1;
printf("load font\n");
gloadfont("font.bin");
printf("init screen\n");
if(zx_scr_init(0)<0) return -1;
if(zx_keys_init(&keys)<0) return -1;
printf("sound\n");
if(zx_sound_init()<0) return -1;
#ifdef WITH_MIDI
if (sysmidi_init(midi_dev)<0) {
printf("Note: MIDI not available.\n");
}
#endif
printf("ay\n");
if(ay_init(&ay0, ZX_SOUND_TICKS_SMP)<0) return -1;
ay0.ioport_write = gzx_ay_ioport_write;
ay0.ioport_write_arg = &ay0;
rs232_init(&rs232, Z80_CLOCK / MIDI_BAUD);
rs232.sendchar = gzx_rs232_sendchar;
rs232.sendchar_arg = &rs232;
midi_port_init(&midi);
midi.midi_msg = gzx_midi_msg;
midi.midi_msg_arg = &midi;
kempston_joy_init(&kjoy0);
if(tape_deck_create(&tape_deck, true) != 0) return -1;
tape_deck->delta_t = ZX_TAPE_TICKS_SMP;
zx_reset();
disp_t=0;
snd_t=0;
tapp_t=0;
border=7;
return 0;
}
static void writestat_i(int i) {
int j;
for(j=0;j<64;j++)
fprintf(logfi,"0x%02x: %10d, %10d, %10d, %10d\n",j*4,
z80_getstat(i,4*j), z80_getstat(i,4*j+1),
z80_getstat(i,4*j+2),z80_getstat(i,4*j+3));
}
static void writestat(void) {
fprintf(logfi,"\nop:\n"); writestat_i(0);
fprintf(logfi,"\nDDop:\n"); writestat_i(1);
fprintf(logfi,"\nFDop:\n"); writestat_i(2);
fprintf(logfi,"\nCBop:\n"); writestat_i(3);
fprintf(logfi,"\nDDCBop:\n"); writestat_i(4);
fprintf(logfi,"\nFDCBop:\n"); writestat_i(5);
fprintf(logfi,"\nEDop:\n"); writestat_i(6);
}
/** Process an instruction and anything that we check for every instruction. */
static void zx_proc_instr(void)
{
if (!gpu_is_on()) {
while(CLOCK_LT(zx_scr_get_clock(),z80_clock)) {
zx_scr_disp();
}
} else {
while(CLOCK_LT(zx_scr_get_clock(),z80_clock)) {
zx_scr_disp_fast();
}
}
if(CLOCK_GE(z80_clock-snd_t,ZX_SOUND_TICKS_SMP)) {
zx_sound_smp(ay_get_sample(&ay0)+(tape_smp?+16:-16));
/* build a new sound sample */
snd_t+=ZX_SOUND_TICKS_SMP;
}
if(CLOCK_GE(z80_clock-tapp_t,ZX_TAPE_TICKS_SMP)) {
tape_deck_getsmp(tape_deck, &tape_smp);
ear=tape_smp;
tapp_t+=ZX_TAPE_TICKS_SMP;
}
if(!slow_load) {
if(cpus.PC==TAPE_LDBYTES_TRAP) {
printf("load trapped!\n");
tape_quick_ldbytes(tape_deck);
}
if(cpus.PC==TAPE_SABYTES_TRAP) {
printf("save trapped!\n");
tape_quick_sabytes(tape_deck);
}
}
if (dbg.stop_enabled && cpus.PC == dbg.stop_addr) {
debugger_run(&dbg);
}
#ifdef XMAP
xmap_mark();
#endif
#ifdef XTRACE
xtrace_instr();
#endif
if (gpu_is_on())
z80_g_execinstr();
else
z80_execinstr();
/* Instruction trap? */
if (dbg.itrap_enabled) {
/*
* Normally we don't want to break into debugger during int_lock
* (i.e. after DD/CB prefix. However, if there are more DD/CB
* prefixes in sequence, we will break into debugger.
*/
if (!cpus.int_lock || dbg.prev_int_lock)
debugger_run(&dbg);
dbg.prev_int_lock = cpus.int_lock;
}
}
int main(int argc, char **argv) {
int argi;
timer frmt;
wkey_t k;
argi = 1;
dbl_ln=0;
while (argc > argi && argv[argi][0] == '-') {
if (!strcmp(argv[argi],"-midi")) {
if (argc <= argi + 1) {
printf("Option -midi missing argument.\n");
exit(1);
}
midi_dev = argv[argi + 1];
argi+=2;
} else {
printf("Invalid option '%s'.\n", argv[argi]);
exit(1);
}
}
uoc=0;
smc=0;
logfi=fopen("log.txt","wt");
start_dir = sys_getcwd(NULL,0);
printf("\n\n\n");
if(zx_init()<0) return -1;
/* slow_load=1;*/
/*if(zx_load_snap(SNAP_NAME1)<0) {
printf("error loading snapshot\n");
return -1;
}*/
if(argc > argi && zx_load_snap(argv[argi])<0) {
printf("error loading snapshot\n");
return -1;
}
//printf("inited.\n");
timer_reset(&frmt);
while(!quit) {
if(CLOCK_GE(z80_clock-disp_t,ULA_FIELD_TICKS)) { /* every 50th of a second */
disp_t+=ULA_FIELD_TICKS;
#ifdef WITH_MIDI
sysmidi_poll(z80_clock);
#endif
mgfx_updscr();
mgfx_input_update();
while(w_getkey(&k)) key_handler(&k);
#ifdef LOG
if(cpus.iff1) fprintf(logfi,"interrupt\n");
#endif
}
zx_proc_instr();
}
/* Graphics is closed automatically atexit() */
#ifdef XMAP
xmap_save();
#endif
zx_sound_done();
tape_deck_destroy(tape_deck);
tape_deck = NULL;
writestat();
fclose(logfi);
printf("uoc:%lu\nsmc:%lu\n",uoc,smc);
return 0;
}