-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiffy.c
2820 lines (2784 loc) · 76.9 KB
/
spiffy.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
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
spiffy - ZX spectrum emulator
Copyright Edward Cree, 2010-13
License: GNU GPL v3+
Acknowledgements (Heavily used references)
Z80 core based on Cristian Dinu's "Decoding Z80 Opcodes" http://www.z80.info/decoding.htm
and Sean Young's "Z80 Undocumented Features" http://www.gaby.de/z80/z80undoc3.txt
Timings etc. chiefly from WoS' "Z80 Instruction Information" http://www.worldofspectrum.org/Z80instructions.html
and CSSFAQ "16K / 48K ZX Spectrum Reference" http://www.worldofspectrum.org/faq/reference/48kreference.htm#ZXSpectrum
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL.h>
#include <SDL/SDL_audio.h>
#include <SDL/SDL_ttf.h>
#include <sys/time.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include <libspectrum.h>
#include "bits.h"
#include "ops.h"
#include "z80.h"
#include "vchips.h"
#include "machine.h"
#include "pbm.h"
#include "sysvars.h"
#include "basic.h"
#include "debug.h"
#include "ui.h"
#include "audio.h"
#include "filters.h"
#include "coretest.h"
#define GPL_MSG "spiffy Copyright (C) 2010-13 Edward Cree.\n\
This program comes with ABSOLUTELY NO WARRANTY; for details see the GPL v3.\n\
This is free software, and you are welcome to redistribute it\n\
under certain conditions: GPL v3+\n"
bool zxp_enabled=false; // Emulate a connected ZX Printer?
machine zx_machine=MACHINE_48;
unsigned int filt_mask=0; // Which graphics filters to enable (see filters.h)
// helper fns
void scrn_update(SDL_Surface *screen, int Tstates, int frames, int frameskip, int Fstate, ram_t *ram, bus_t *bus, ula_t *ula);
uint8_t scale38(uint8_t v);
void getedge(libspectrum_tape *deck, bool *play, bool stopper, bool *ear, uint32_t *T_to_tape_edge, int *edgeflags, int *oldtapeblock, unsigned int *tapeblocklen);
void putedge(uint32_t *T_since_tape_edge, unsigned long *trecpuls, FILE *trec);
void loadfile(const char *fn, libspectrum_tape **deck, libspectrum_snap **snap);
void loadsnap(libspectrum_snap *snap, z80 *cpu, bus_t *bus, ram_t *ram, int *Tstates);
void savesnap(libspectrum_snap **snap, z80 *cpu, bus_t *bus, ram_t *ram, int Tstates);
int main(int argc, char * argv[])
{
TTF_Font *font=NULL;
if(!TTF_Init())
{
font=TTF_OpenFont(PREFIX"/share/fonts/Vera.ttf", 12);
if(!font) font=TTF_OpenFont("Vera.ttf", 12);
if(!font)
{
fprintf(stderr, "Failed to open font (Vera.ttf)\n");
return(1);
}
}
if(libspectrum_init())
{
fprintf(stderr, "Failed to initialise libspectrum\n");
return(1);
}
bool debug=false; // Generate debugging info?
bool debugcycle=false; // Single-Tstate stepping?
bool trace=false; // execution tracing in debugger?
bool coretest=false; // run the core tests?
bool pause=false;
bool stopper=false; // stop tape at end of this block?
bool edgeload=true; // edge loader enabled
#ifdef AUDIO
bool delay=true; // attempt to maintain approximately a true Speccy speed, 50fps at 69888 T-states per frame, which is 3.4944MHz
uint16_t filterfactor=52; // this value minimises noise with various beeper engines (dunno why). Other good values are 38, 76
update_sinc(filterfactor);
#endif /* AUDIO */
const char *fn=NULL;
ay_enabled=false;
bool ulaplus_enabled=false;
bool timex_enabled=false;
const char *zxp_fn="zxp.pbm";
bool zxp_fix=false; // change the ZXP address from ¬A2 to A6.¬A2? For compatibility with ZXI devices
js_type keystick=JS_C; // keystick mode: Cursor, Sinclair, Kempston, disabled
bool showkb=false; // show a keyboard helper?
unsigned int nbreaks=0;
unsigned int *breakpoints=NULL;
int arg;
/* First, look for a machine type, to get defaults */
for(arg=1;arg<argc;arg++)
{
if(strncmp(argv[arg], "-m", 2)==0)
{
machine m=machine_from_name(argv[arg]+2);
if(m<_MACHINES) zx_machine=m;
}
}
/* Now we apply those defaults */
ay_enabled=cap_ay(zx_machine);
/* Then process normal args */
for(arg=1;arg<argc;arg++)
{
if((strcmp(argv[arg], "--debug") == 0) || (strcmp(argv[arg], "-d") == 0))
{ // activate debugging mode
debug=true;
}
else if(strncmp(argv[arg], "-b=", 3) == 0)
{ // activate debugging mode at a breakpoint
unsigned int breakpoint;
if(sscanf(argv[arg]+3, "%04x", &breakpoint)==1)
{
unsigned int n=nbreaks++;
unsigned int *nbp=realloc(breakpoints, nbreaks*sizeof(unsigned int));
if(!nbp)
{
perror("malloc");
return(1);
}
(breakpoints=nbp)[n]=breakpoint;
}
else
{
fprintf(stderr, "Ignoring bad argument '%s'\n", argv[arg]);
}
}
else if((strcmp(argv[arg], "--pause") == 0) || (strcmp(argv[arg], "-p") == 0))
{ // start with the emulation paused
pause=true;
}
else if(strcmp(argv[arg], "--zxprinter") == 0)
{ // enable ZX Printer
zxp_enabled=true;
}
else if(strncmp(argv[arg], "--zxprinter=", 12) == 0)
{ // enable ZX Printer and set zxp_fn
zxp_enabled=true;
zxp_fn=argv[arg]+12;
}
else if(strcmp(argv[arg], "--zxpfix") == 0)
{ // ZX Printer address fix
zxp_fix=true;
}
else if(strcmp(argv[arg], "--ay") == 0)
{ // enable AY chip
ay_enabled=true;
}
else if(strcmp(argv[arg], "--no-ay") == 0)
{ // disable AY chip
ay_enabled=false;
}
else if(strcmp(argv[arg], "--ula+") == 0)
{ // enable ULAplus
ulaplus_enabled=true;
}
else if(strcmp(argv[arg], "--no-ula+") == 0)
{ // disable ULAplus
ulaplus_enabled=false;
}
else if(strcmp(argv[arg], "--timex") == 0)
{ // enable 8x1 attribute mode
timex_enabled=true;
}
else if(strcmp(argv[arg], "--kb") == 0)
{ // enable keyboard assistant
showkb=true;
}
else if((strcmp(argv[arg], "--Tstate") == 0) || (strcmp(argv[arg], "-T") == 0))
{ // activate single-Tstate stepping under debugger
debugcycle=true;
}
else if((strcmp(argv[arg], "--no-Tstate") == 0) || (strcmp(argv[arg], "+T") == 0))
{ // deactivate single-Tstate stepping under debugger
debugcycle=false;
}
else if((strcmp(argv[arg], "--coretest") == 0) || (strcmp(argv[arg], "-c") == 0))
{ // run the core tests
coretest=true;
}
else if(strncmp(argv[arg], "-m", 2)==0)
{ // ignore it; we handled -mMachine in the first pass
}
else
{ // unrecognised option, assume it's a filename
fn=argv[arg];
}
}
fprintf(stderr, GPL_MSG);
if(coretest)
{
FILE *f;
const char *progname = argv[0];
const char *testsfile = "tests.in";
/* Initialise the tables used by the Z80 core */
z80_init();
f = fopen( testsfile, "r" );
if( !f ) {
fprintf( stderr, "%s: couldn't open tests file `%s': %s\n", progname,
testsfile, strerror( errno ) );
return 1;
}
while(run_test(f));
if( fclose(f) ) {
fprintf( stderr, "%s: couldn't close `%s': %s\n", progname, testsfile,
strerror( errno ) );
return 1;
}
return 0;
}
// State
z80 _cpu, *cpu=&_cpu; // we want to work with a pointer
bus_t _bus, *bus=&_bus;
ula_t _ula, *ula=&_ula;
ram_t _ram, *ram=&_ram;
ui_offsets(showkb, zxp_enabled);
SDL_Surface * screen=gf_init(320, y_end);
if(!screen)
{
fprintf(stderr, "Failed to set up video\n");
return(2);
}
button *buttons;
ui_init(screen, &buttons, edgeload, pause, showkb, zxp_enabled);
int errupt=0;
bus->portfe=0; // used by mixaudio (for the beeper), tape writing (MIC) and the screen update (for the BORDCR)
bus->port7ffd=0; // controls paging
bus->kempbyte=0; // used in Kempston keystick mode
bool ear=false; // tape reading EAR
bool kstate[8][5]; // keyboard state
uint8_t kenc[8]; // encoded keyboard state
for(int i=0;i<8;i++)
{
for(int j=0;j<5;j++)
kstate[i][j]=false;
kenc[i]=0;
}
if(init_keyboard())
{
fprintf(stderr, "spiffy: failed to load keymap\n");
return(1);
}
ula->t1=false;
ula->memwait=false;
ula->iowait=false;
if((ula->ulaplus_enabled=ulaplus_enabled))
{
ula->ulaplus_regsel=0;
for(unsigned int reg=0;reg<64;reg++)
ula->ulaplus_regs[reg]=0;
ula->ulaplus_mode=0;
}
ula->timex_enabled=timex_enabled;
#ifdef AUDIO
if(SDL_InitSubSystem(SDL_INIT_AUDIO))
{
fprintf(stderr, "spiffy: failed to initialise audio subsystem:\tSDL_InitSubSystem:%s\n", SDL_GetError());
return(3);
}
uint8_t *sinc_rate=get_sinc_rate();
SDL_AudioSpec fmt;
fmt.freq = SAMPLE_RATE;
fmt.format = AUDIO_S16;
fmt.channels = 1;
fmt.samples = AUDIOBUFLEN*2;
fmt.callback = mixaudio;
audiobuf abuf = {.rp=0, .wp=0, .record=NULL, .busy={true, true}};
fmt.userdata = &abuf;
/* Open the audio device */
if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
return(3);
}
#endif /* AUDIO */
// ZX Printer state
unsigned int zxp_stylus_posn=0; // ranges from 0 to 384, with the paper starting at 128
bool zxp_slow_motor=false; // d1
bool zxp_stop_motor=true; // d2
bool zxp_stylus_power=false; // d7
bool zxp_d0_latch=false; // encoder disc
bool zxp_d7_latch=false; // stylus hits left of paper
bool zxp_feed_button=false; // is the feed button being held?
int zxp_height_offset; // offset of height field in zxp_output pbm file
unsigned int zxp_rows=0;
FILE *zxp_output=NULL;
if(zxp_enabled)
{
zxp_output=fopen(zxp_fn, "wb");
if(!zxp_output)
{
fprintf(stderr, "Failed to open `%s'; ZX printer output will not be saved!\n", zxp_fn);
perror("\tfopen");
}
else
zxp_height_offset=pbm_putheader(zxp_output, 256, 0);
}
// Timing
struct timeval frametime[100];
gettimeofday(frametime, NULL);
for(int i=1;i<100;i++) frametime[i]=frametime[0];
// Mouse handling
pos mouse;
SDL_GetMouseState(&mouse.x, &mouse.y);
//SDL_ShowCursor(SDL_DISABLE);
char button;
unsigned int hover=nbuttons;
// Spectrum State
const char *rom_file=default_rom(zx_machine);
FILE *fp = configopen(rom_file, "rb");
if(!fp)
{
fprintf(stderr, "Failed to open Spectrum ROM `%s'!\n", rom_file);
return(1);
}
if(ram_init(ram, fp, zx_machine))
{
fprintf(stderr, "Failed to set up RAM\n");
return(1);
}
fclose(fp);
z80_init(); // initialise decoding tables
int Fstate=0; // FLASH state
z80_reset(cpu, bus);
bus_reset(bus);
ay_init(&ay);
#ifdef AUDIO
if(ay_enabled)
{
*sinc_rate=2;
filterfactor=128;
update_sinc(filterfactor);
}
#endif /* AUDIO */
libspectrum_tape *deck=NULL;
bool play=false;
int oldtapeblock=-1;
unsigned int tapeblocklen=0;
libspectrum_snap *snap=NULL;
FILE *trec=NULL;
unsigned long trecpuls=0;
uint32_t T_since_tape_edge=0;
bool oldmic=false;
unsigned int keyb_mode=0;
SDL_Flip(screen);
#ifdef AUDIO
// Start sound
SDL_PauseAudio(0);
#endif /* AUDIO */
int frames=0;
int Tstates=0;
int T_per_frame=frame_length(zx_machine);
bool debug_screen=false; // should we update the screen when single-stepping?
uint32_t T_to_tape_edge=0;
int edgeflags=0;
if(fn)
{
loadfile(fn, &deck, &snap);
if(snap)
{
loadsnap(snap, cpu, bus, ram, &Tstates);
fprintf(stderr, "Loaded snap '%s'\n", fn);
libspectrum_snap_free(snap);
snap=NULL;
}
}
// Main program loop
while(likely(!errupt))
{
if(unlikely(nbreaks))
{
for(unsigned int bp=0;bp<nbreaks;bp++)
{
if((!debug)&&(*PC==breakpoints[bp])&&(cpu->M==0)&&(cpu->dT==0)&&(cpu->shiftstate==0))
{
debug=true;
}
}
}
Tstates++;
#ifdef AUDIO
if(!(Tstates%(T_per_frame*50/(SAMPLE_RATE**sinc_rate))))
{
abuf.play=play||trec;
unsigned int newwp=(abuf.wp+1)%AUDIOBITLEN;
if(delay&&!(play||trec))
{
unsigned int waits=0;
while(newwp==abuf.rp)
{
if(waits++>AUDIO_MAXWAITS)
{
fprintf(stderr, "Audio overrun! waits %u\n", waits);
break;
}
usleep(AUDIO_WAIT);
}
}
abuf.bits[abuf.wp]=(bus->portfe&PORTFE_SPEAKER)?0x80:0;
if(ear) abuf.bits[abuf.wp]^=0x40;
if((bus->portfe&PORTFE_MIC)&&(bus->portfe&PORTFE_SPEAKER)) abuf.bits[abuf.wp]^=0x08;
if(ay_enabled)
{
abuf.bits[abuf.wp]+=(ay.out[0]+ay.out[1]+ay.out[2])/8;
}
abuf.wp=newwp;
}
#endif /* AUDIO */
if(play&&!pause)
{
if(unlikely(!deck))
play=false;
else if(T_to_tape_edge)
T_to_tape_edge--;
else
{
getedge(deck, &play, stopper, &ear, &T_to_tape_edge, &edgeflags, &oldtapeblock, &tapeblocklen);
}
}
if(bus->mreq)
do_ram(ram, bus);
if(unlikely(trec&&!pause))
T_since_tape_edge++;
if(unlikely(bus->iorq&&(bus->tris==TRIS_OUT)))
{
if(!(bus->addr&0x01)) // ULA
{
bus->portfe=bus->data;
if(trec&&((bus->portfe&PORTFE_MIC)?!oldmic:oldmic))
{
putedge(&T_since_tape_edge, &trecpuls, trec);
oldmic=bus->portfe&PORTFE_MIC;
}
}
else if(cap_128_paging(zx_machine)&&!(bus->addr&0x8002)) // 128 Paging
{
if(!(bus->port7ffd&0x20))
{
bus->port7ffd=bus->data;
ram->paged[0]=(bus->port7ffd&0x10)?1:0;
ram->paged[3]=(bus->port7ffd&0x7)+2;
}
}
else if(zxp_enabled&&!(bus->addr&0x04)&&((!zxp_fix)||(bus->addr&0x40))) // ZX Printer
{
zxp_d0_latch=false;
zxp_d7_latch=false;
zxp_slow_motor=bus->data&0x02;
zxp_stop_motor=bus->data&0x04;
zxp_stylus_power=bus->data&0x80;
}
else if(ay_enabled&&((bus->addr&0x8002)==0x8000))
{
if(bus->addr&0x4000)
ay.regsel=bus->data;
else if(ay.regsel<16)
{
ay.reg[ay.regsel]=bus->data;
if(ay.regsel==13)
{
ay.envcount=0;
ay.envstop=false;
ay.envrev=false;
if(bus->data&0x04) ay.env=0;
else ay.env=15;
}
}
}
else if(ula->ulaplus_enabled&&(bus->addr==0xbf3b))
{
ula->ulaplus_regsel=bus->data;
}
else if(ula->ulaplus_enabled&&(bus->addr==0xff3b))
{
if(!(ula->ulaplus_regsel&0xC0))
{
ula->ulaplus_regs[ula->ulaplus_regsel]=bus->data;
}
else if(ula->ulaplus_regsel==0x40)
{
ula->ulaplus_mode=bus->data;
}
}
}
if(unlikely(bus->iorq&&(bus->tris==TRIS_IN)))
{
if(!(bus->addr&0x01)) // ULA
{
uint8_t hi=bus->addr>>8;
bus->data=(ear?0x40:0)|0x1f;
for(int i=0;i<8;i++)
if(!(hi&(1<<i)))
bus->data&=~kenc[i];
}
else if(zxp_enabled&&!(bus->addr&0x04)&&((!zxp_fix)||(bus->addr&0x40))) // ZX Printer
{
bus->data=0x3e;
if(zxp_d0_latch) bus->data|=0x01;
if(zxp_d7_latch) bus->data|=0x80;
}
else if((keystick==JS_K)&&((bus->addr&0xFF)==0x1F)) // Kempston joystick
{
bus->data=bus->kempbyte;
}
else if(ay_enabled&&((bus->addr&0x8002)==0x8000))
{
if(bus->addr&0x4000)
{
bus->data=ay.reg[ay.regsel];
}
}
else if(ula->ulaplus_enabled&&(bus->addr==0xff3b))
{
if(!(ula->ulaplus_regsel&0xC0))
{
bus->data=ula->ulaplus_regs[ula->ulaplus_regsel];
}
else if(ula->ulaplus_regsel==0x40)
{
bus->data=ula->ulaplus_mode;
}
}
else
bus->data=0xff; // technically this is wrong, TODO floating bus
}
if(unlikely(debug&&(((cpu->M==0)&&(cpu->dT==0)&&(cpu->shiftstate==0))||debugcycle)))
{
SDL_PauseAudio(1);
debugctx ctx={.Tstates=Tstates, .cpu=cpu, .bus=bus, .ram=ram, .ula=ula, .ay=&ay};
if(trace)
show_state(ctx);
if(debug_screen)
SDL_Flip(screen);
int derrupt=0;
static unsigned int blanks=0;
while(!derrupt)
{
if(feof(stdin))
{
fprintf(stderr, "EOF on stdin, closing debugger\n");
nodebug:
debug=false;
bugbutton.col=0x4f4f4f;
bugbutton.tooltip="Debugger is unavailable";
drawbutton(screen, bugbutton);
break;
}
fprintf(stderr, ">");
fflush(stderr);
char *line=finpl(stdin);
if(line)
{
static char *oldline=NULL;
if(!line) line=oldline;
else
{
free(oldline);
oldline=strdup(line);
}
if(!line)
{
if(!blanks++) fprintf(stderr, "This is the spiffy debugger.\nType `h' for a list of commands, or `help h' for a list of help sections\n");
if(blanks>4) goto nodebug;
}
else
{
int drgc;
char *drgv[256];
debugger_tokenise(line, &drgc, drgv);
const char *cmd=drgv[0];
if(!cmd)
{
if(!blanks++) fprintf(stderr, "This is the spiffy debugger.\nType `h' for a list of commands, or `help h' for a list of help sections\n");
if(blanks>4) goto nodebug;
}
else if((strcmp(cmd, "c")==0)||(strcmp(cmd, "cont")==0))
{
debug=false;
derrupt++;
}
else if((strcmp(cmd, "h")==0)||(strcmp(cmd, "help")==0))
{
const char *what=drgv[1];
if(!what) what="";
if(strcmp(what, "h")==0)
fprintf(stderr, h_h);
else if(strcmp(what, "m")==0)
fprintf(stderr, h_m);
else if(strcmp(what, "p")==0)
fprintf(stderr, h_p);
else if(strcmp(what, "v")==0)
fprintf(stderr, h_v);
else if(strcmp(what, "k")==0)
fprintf(stderr, h_k);
else if(strcmp(what, "=")==0)
fprintf(stderr, h_eq);
else if(strcmp(what, "u")==0)
fprintf(stderr, h_u);
else
fprintf(stderr, h_cmds);
}
else if(strcmp(cmd, "w")==0) // short for 'screen show'
SDL_Flip(screen);
else if(strcmp(cmd, "@")==0) // short for 'screen raster'
goto dbg_screen_raster;
else if(strcmp(cmd, "screen")==0)
{
const char *what=drgv[1];
if(!what)
fprintf(stderr, "screen is: %s\n", debug_screen?"on":"off");
else if(strcmp(what, "show")==0)
SDL_Flip(screen);
else if(strcmp(what, "on")==0)
{
debug_screen=true;
SDL_Flip(screen);
}
else if(strcmp(what, "off")==0)
debug_screen=false;
else if(strcmp(what, "raster")==0)
{
dbg_screen_raster:;
bool t128=cap_128_ula_timings(zx_machine);
int line,col;
if(t128)
{
line=((Tstates)/228)-15;
col=(((Tstates)%228)<<1);
}
else
{
line=((Tstates+12)/224)-16;
col=(((Tstates+12)%224)<<1);
}
fprintf(stderr, "raster: %d,%d\n", line, col);
if(likely((line>=0) && (line<296)))
{
if((col>=0) && (col<screen->w))
{
for(unsigned int t=6;;t--)
{
unsigned char a=(t&1)?255:0, b=(t&1)?0:255;
pset(screen, col, line, a, a, a);
pset(screen, col+1, line, b, b, b);
SDL_Flip(screen);
if(t) usleep(4e5);
else break;
}
}
}
SDL_Flip(screen);
}
else
fprintf(stderr, "usage: screen [on|off|show]\n");
}
else if((strcmp(cmd, "s")==0)||(strcmp(cmd, "state")==0))
show_state(ctx);
else if((strcmp(cmd, "t")==0)||(strcmp(cmd, "trace")==0))
trace=true;
else if((strcmp(cmd, "!t")==0)||(strcmp(cmd, "!trace")==0))
trace=false;
else if(strcmp(cmd, "1")==0)
debugcycle=true;
else if(strcmp(cmd, "!1")==0)
debugcycle=false;
else if((strcmp(cmd, "n")==0)||(strcmp(cmd, "next")==0))
derrupt++;
else if((strcmp(cmd, "p")==0)||(strcmp(cmd, "print")==0))
{
if(drgc<2)
fprintf(stderr, "print: But what do you want to print?\n");
else
{
debugval val=debugger_expr(stderr, drgc-1, (const char *const *)drgv+1, ctx);
debugval_display(stderr, val);
}
}
else if((strcmp(cmd, "b")==0)||(strcmp(cmd, "break")==0))
{
const char *rest=drgv[1];
unsigned int bp=0;
if(rest&&(sscanf(rest, "%x", &bp)==1))
{
unsigned int n=nbreaks++;
unsigned int *nbp=realloc(breakpoints, nbreaks*sizeof(unsigned int));
if(!nbp)
{
perror("malloc");
}
else
{
(breakpoints=nbp)[n]=bp;
fprintf(stderr, "breakpoint at %04x\n", bp);
}
}
else
{
fprintf(stderr, "break: missing argument\n");
}
}
else if((strcmp(cmd, "!b")==0)||(strcmp(cmd, "!break")==0))
{
const char *rest=drgv[1];
unsigned int bp=0;
if(rest&&(sscanf(rest, "%x", &bp)==1))
{
for(unsigned int i=0;i<nbreaks;i++)
{
while(breakpoints[i]==bp)
{
fprintf(stderr, "deleted breakpoint at %04x\n", breakpoints[i]);
if(i<--nbreaks)
breakpoints[i]=breakpoints[i+1];
else break;
}
}
}
else
{
for(unsigned int i=0;i<nbreaks;i++)
fprintf(stderr, "deleted breakpoint at %04x\n", breakpoints[i]);
free(breakpoints);
breakpoints=NULL;
nbreaks=0;
}
}
else if((strcmp(cmd, "l")==0)||(strcmp(cmd, "list")==0))
{
for(unsigned int i=0;i<nbreaks;i++)
{
fprintf(stderr, "breakpoint at %04x\n", breakpoints[i]);
}
}
else if(strcmp(cmd, "ei")==0)
cpu->IFF[0]=cpu->IFF[1]=1;
else if(strcmp(cmd, "di")==0)
cpu->IFF[0]=cpu->IFF[1]=0;
else if((strcmp(cmd, "r")==0)||(strcmp(cmd, "reset")==0))
bus_reset(bus);
else if((strcmp(cmd, "i")==0)||(strcmp(cmd, "int")==0))
bus->irq=true;
else if((strcmp(cmd, "!i")==0)||(strcmp(cmd, "!int")==0))
bus->irq=false;
else if(strcmp(cmd, "nmi")==0)
bus->nmi=true;
else if(strcmp(cmd, "!nmi")==0)
bus->nmi=false;
else if((strcmp(cmd, "v")==0)||(strcmp(cmd, "vars")==0))
{
uint16_t sv_vars=ram_read_word(ram, sysvarbyname("VARS")->addr), i=sv_vars, l;
char *what=drgv[1];
size_t wlen=what?strlen(what):0;
double num;
bool match=!what;
while(i)
{
uint8_t first=ram_read(ram, i);
if(first==0x80) break;
uint8_t name=(first&0x1f)|0x60;
uint16_t addr=i;
uint8_t byte;
uint8_t bytes[5];
switch(first>>5)
{
case 2: // String
// 010aaaaa Length[2] Text[]
if(what) match=((wlen==2)&&(what[0]==name)&&(what[1]=='$'));
i++;
l=ram_read_word(ram, i);
i+=2;
if(match)
{
if(drgc>2)
{
unsigned int j;
if(sscanf(drgv[2], "%u", &j)!=1)
fprintf(stderr, "3 Subscript wrong, 0:1\n");
else if((j<1)||(j>l))
fprintf(stderr, "3 Subscript wrong, 0:1\n");
else
{
uint8_t c=ram_read(ram, i+j-1);
fprintf(stderr, "%04x $ %c$(%u) = ", i+j-1, name, j);
if((c>=32)&&(c<127))
fprintf(stderr, "'%c' = ", c);
fprintf(stderr, "%u = 0x%02x = '\\%03o'\n", c, c, c);
}
}
else
{
fprintf(stderr, "%04x $ %c$ = \"", addr, name);
unsigned int k=0;
bool overlong=false;
for(unsigned int j=0;j<l;j++)
{
if(k>=64)
{
overlong=true;
break;
}
uint8_t c=ram_read(ram, i+j);
if((c>=32)&&(c<127))
{
fputc(c, stderr);
k++;
}
else
{
int len;
fprintf(stderr, "\\%03o%n", c, &len);
k+=len;
}
}
fprintf(stderr, overlong?"\"...\n":"\"\n");
}
}
i+=l;
break;
case 3: // Number whose name is one letter
// 011aaaaa Value[5]
if(what) match=((wlen==1)&&(*what==name));
if(drgc>2) match=false;
if(match)
{
fprintf(stderr, "%04x # ", addr+1);
fputc(name, stderr);
}
ram_read_bytes(ram, ++i, 5, bytes);
num=float_decode(bytes);
if(match) fprintf(stderr, " = %g\n", num);
i+=5;
break;
case 4: // Array of numbers
// 100aaaaa TotalLength[2] DimensionsAndValues[]
if(what) match=((wlen==1)&&(*what==name)&&(drgc>2));
if(!what) fprintf(stderr, "%04x # %c (array)\n", addr, name);
i++;
l=ram_read_word(ram, i);
i+=2;
if(what&&match)
{
uint8_t ndim=ram_read(ram, i), sub=0;
uint16_t dims[ndim];
for(uint8_t dim=0;dim<ndim;dim++)
dims[dim]=ram_read_word(ram, i+1+dim*2);
unsigned int subs[ndim];
if((drgc>2)&&(strcmp(drgv[2], "()")!=0))
{
int drg=2;
while((drg<drgc)&&(sub<ndim))
{
if((sscanf(drgv[drg], "%u", subs+sub)!=1)||(subs[sub]<1)||(subs[sub]>dims[sub]))
{
fprintf(stderr, "3 Subscript wrong, 0:%u\n", drg);
break;
}
sub++;
drg++;
}
}
addr=i+1+ndim*2;
if(sub<ndim)
{
for(uint8_t dim=0;dim<sub;dim++)
{
uint16_t offset=(subs[dim]-1)*5;
for(uint8_t d2=dim+1;d2<ndim;d2++)
offset*=dims[d2];
addr+=offset;
}
fprintf(stderr, "%04x # %c", addr, name);
for(uint8_t dim=0;dim<sub;dim++)
{
fprintf(stderr, "(%u)", subs[dim]);
}
fprintf(stderr, " (array");
for(uint8_t dim=sub;dim<ndim;dim++)
fprintf(stderr, "[%u]", dims[dim]);
fprintf(stderr, ")\n");
}
else
{
for(uint8_t dim=0;dim<ndim;dim++)
{
uint16_t offset=(subs[dim]-1)*5;
for(uint8_t d2=dim+1;d2<ndim;d2++)
offset*=dims[d2];
addr+=offset;
}
fprintf(stderr, "%04x # %c", addr, name);
for(uint8_t dim=0;dim<sub;dim++)
{
fprintf(stderr, "(%u)", subs[dim]);
}
ram_read_bytes(ram, addr, 5, bytes);
fprintf(stderr, " = %g\n", float_decode(bytes));
}
}
i+=l;
break;
case 5:; // Number whose name is longer than one letter
// 101aaaaa 0bbbbbbb ... 1zzzzzzz Value[5]
string fullname=init_string();
append_char(&fullname, name);
i++;
do
{
byte=ram_read(ram, i++);
append_char(&fullname, byte&0x7F);
}
while(!(byte&0x80));
if(what) match=!strcmp(fullname.buf, what);
ram_read_bytes(ram, i, 5, bytes);
num=float_decode(bytes);
if(match) fprintf(stderr, "%04x # %s = %g\n", i, fullname.buf, num);
free_string(&fullname);
i+=5;
break;
case 6: // Array of characters
// 110aaaaa TotalLength[2] DimensionsAndValues[]
if(what) match=((wlen==2)&&(what[0]==name)&&(what[1]=='$'));
if(!what) fprintf(stderr, "%04x $ %c$ (array)\n", addr, name);
i++;
l=ram_read_word(ram, i);
i+=2;
if(what&&match)
{
uint8_t ndim=ram_read(ram, i), sub=0;
uint16_t dims[ndim];
for(uint8_t dim=0;dim<ndim;dim++)
dims[dim]=ram_read_word(ram, i+1+dim*2);
unsigned int subs[ndim];
if((drgc>2)&&(strcmp(drgv[2], "()")!=0))
{
int drg=2;
while((drg<drgc)&&(sub<ndim))
{
if((sscanf(drgv[drg], "%u", subs+sub)!=1)||(subs[sub]<1)||(subs[sub]>dims[sub]))
{
fprintf(stderr, "3 Subscript wrong, 0:%u\n", drg);
break;
}
sub++;
drg++;
}
}
addr=i+1+ndim*2;
if(sub<ndim)
{
for(uint8_t dim=0;dim<sub;dim++)
{
uint16_t offset=subs[dim]-1;
for(uint8_t d2=dim+1;d2<ndim;d2++)
offset*=dims[d2];
addr+=offset;
}
fprintf(stderr, "%04x $ %c$", addr, name);
for(uint8_t dim=0;dim<sub;dim++)
{
fprintf(stderr, "(%u)", subs[dim]);
}
fprintf(stderr, " (array");
for(uint8_t dim=sub;dim<ndim;dim++)
fprintf(stderr, "[%u]", dims[dim]);
fputc(')', stderr);
if(sub+1==ndim)
{
fprintf(stderr, " = \"");
unsigned int k=0;
bool overlong=false;
for(unsigned int j=0;j<dims[ndim-1];j++)
{
if(k>=64)