-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnaps.ino
1729 lines (1573 loc) · 46.1 KB
/
snaps.ino
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
/*=====================================================================
snaps.c -> This file includes all the snapshot handling functions
for the emulator, called from the main loop and when
the user wants to load/save snapshots files.
also routines to manage files, i.e. rom files, search in several sites and so on...
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., 675 Mass Ave, Cambridge, MA 02139, USA.
Copyright (c) 2000 Santiago Romero Iglesias.
Email: [email protected]
======================================================================*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "z80.h"
#include "spectrum.h"
#include "snaps.h"
#include "hardware.h"
extern FILE *tapfile;
// Generic routines based on the filename extension:
uint8_t LoadSnapshot (Z80Regs * regs, char *fname) {
FILE *snafp;
File snapfile;
uint8_t ret;
AS_printf("Cargamos un snapshot (por definir)\n");
snapfile = SPIFFS.open(fname, "rb");
if (!snapfile) {
AS_printf(" Fallo el intento de abrir\n");
return (0);
}
switch (typeoffile (fname)) {
case TYPE_SP:
LoadSP (regs, snafp);
snapfile.close();
return (1);
break;
case TYPE_SNA:
// LoadSNA (regs, snafp);
Load_SNA (regs, fname);
snapfile.close();
return (1);
break;
case TYPE_Z80:
LoadZ80 (regs, snafp);
snapfile.close();
return (1);
break;
case TYPE_SCR:
LoadSCR (regs, snafp);
snapfile.close();
return (1);
break;
default:
AS_printf(" tipo desconocido\n");
snapfile.close();
return (0);
break;
}
}
uint8_t SaveSnapshot (Z80Regs * regs, char *fname)
{
File snapfile;
FILE *snafp;
// char ret;
snapfile = SPIFFS.open(fname, "wb");
if (!snapfile) return (0);
switch (typeoffile (fname)) {
case TYPE_SP:
SaveSP (regs, snafp);
snapfile.close();
return (1);
break;
case TYPE_SNA:
SaveSNA (regs, snafp);
snapfile.close();
return (1);
break;
case TYPE_SCR: // deberiamos quitar la opcion de guardar .scr del menu ??
SaveSCR (regs, snafp);
snapfile.close();
return (1);
break;
case TYPE_Z80:
SaveZ80 (regs, snafp);
snapfile.close();
return (1);
break;
default:
snapfile.close();
return (0);
break;
}
snapfile.close();
return (0);
}
uint8_t SaveScreenshot (Z80Regs * regs, char *fname)
{
File snapfile;
FILE *snafp;
snapfile = SPIFFS.open(fname, "wb");
if (snapfile) {
SaveSCR (regs, snafp);
// fclose (snafp);
snapfile.close();
return (1);
}
return (0);
}
/*-----------------------------------------------------------------
char LoadZ80( Z80Regs *regs, FILE *fp );
This loads a .Z80 file from disk to the Z80 registers/memory.
void UncompressZ80 (int tipo, int pc, Z80Regs *regs, FILE *fp)
This load and uncompres a Z80 block to pc adress memory.
The Z80 Load Routine is (C) 2001 Alvaro Alea Fdz.
e-mail: [email protected] Distributed under GPL2
------------------------------------------------------------------*/
#define Z80BL_V1UNCOMP 0
#define Z80BL_V1COMPRE 1
#define Z80BL_V2UNCOMP 3
#define Z80BL_V2COMPRE 4
void UncompressZ80 (int tipo, int pc, Z80Regs * regs, FILE *fp){
byte c, last, last2, dato;
int cont, num, limit;
extern tipo_mem mem;
switch (tipo) {
case Z80BL_V1UNCOMP:
AS_printf (" Uncompressed V1.\n");
fread (mem.p + pc, 0xC000, 1, fp);
break;
case Z80BL_V2UNCOMP:
AS_printf (" Uncompressed V2.\n");
fread (mem.p + pc, 0x4000, 1,fp);
break;
default:
limit = pc + ((tipo == Z80BL_V1COMPRE) ? 0xc000 : 0x4000); // v1=> bloque de 48K, V2 =>de 16K
/* AS_printf (" tipo: %x PC: %x limit:0x \n",tipo,pc, limit);
if ((tipo == Z80BL_V1UNCOMP) || (tipo == Z80BL_V2UNCOMP)) {
AS_printf (" Block uncompres.\n");
for (cont = pc; cont < limit; cont++)
// regs->RAM[cont] = fgetc (fp);
// writemem (cont, fgetc (fp));
*(mem.p+cont)=fgetc(fp);
AS_printf("pc=%x\n",cont);
} else {
*/
AS_printf (" Compressed.\n");
last = 0;
last2 = 0;
c = 0;
AS_printf ("cargando desde %x hasta %x\n",pc,limit);
do {
last2 = last;
last = c;
c = fgetc (fp);
/* detect compressed blocks */
if ((last == 0xED) && (c == 0xED)) {
num = fgetc (fp);
/* Detect EOF */
if ((tipo == Z80BL_V1COMPRE) && (last2 == 0x00) && (num == 0x00)) break;
dato = fgetc (fp);
for (cont = 0; cont < num; cont++)
*(mem.p+pc++)=dato;
c = 0; /* avoid that ED ED xx yy ED zz uu shows zz uu as compressed */
continue;
} else {
/* we found an 0xED but not the compression flag */
if ((last == 0xED) && (c != 0xED))
// regs->RAM[pc++] = last;
// writemem (pc++, last);
*(mem.p+pc++)=last;
/* If it's ED wait for the next or else to memory */
if (c != 0xED)
// regs->RAM[pc++] = c;
// writemem (pc++, c);
*(mem.p+pc++)=c;
}
// AS_printf("pc=%i\ limit=%i\n",pc,limit);
} while (pc < limit);
}
}
uint8_t LoadZ80 (Z80Regs * regs, FILE *fp)
{
int f, tam, ini, sig, pag, ver = 0, hwmodel=SPECMDL_48K, np=0;
extern tipo_hwopt hwopt;
uint8_t buffer[87];
fread (buffer, 87, 1, fp);
if (buffer[12]==255) buffer[12]=1; /*as told in CSS FAQ / .z80 section */
// Check file version
if ((uint16_t) buffer[6] == 0) {
if ((uint16_t) buffer[30] == 23) {
AS_printf ("Fichero Z80: Version 2.0\n");
ver = 2;
} else if (( (uint16_t)buffer[30] == 54 )||( (uint16_t)buffer[30] == 55 ))
{
AS_printf ("Fichero Z80: Version 3.0\n");
ver = 3;
} else {
AS_printf ("Fichero Z80: Version Desconocida (%04X)\n",(uint16_t) buffer[30]);
}
// version>=2 more hardware allowed
switch (buffer[34]) { /* is it 48K? */
case 0:
if ((buffer[37] & 0x80) == 0x80 ){
AS_printf("Hardware 16K (1)\n");
hwmodel=SPECMDL_16K;
np=1;
} else {
AS_printf ("Hardware 48K (2)\n");
hwmodel=SPECMDL_48K;
np=3;
}
break;
case 1:
AS_printf ("Hardware 48K + IF1 (3)\n");
hwmodel=SPECMDL_48KIF1;
break;
case 2:
AS_printf ("Hardware 48K + SAMRAM (4)\n");
return (-1);
break;
case 3:
if (ver == 2) {
if ((buffer[37] & 0x80) == 0x80 ){
AS_printf("Hardware +2 128K (5)\n");
hwmodel=SPECMDL_PLUS2;
np=8;
} else {
AS_printf ("Hardware 128K (6)\n");
hwmodel=SPECMDL_128K;
np=8;
}
} else {
AS_printf ("48K + M.G.T. (7)\n");
return (-1);
}
break;
case 4:
if (ver == 2) {
AS_printf ("Hardware 128K + IF1 (8)\n");
return (-1);
} else {
if ((buffer[37] & 0x80) == 0x80 ){
AS_printf("Hardware +2 128K (9)\n");
hwmodel=SPECMDL_PLUS2;
np=8;
} else {
AS_printf ("Hardware 128K (A)\n");
hwmodel=SPECMDL_128K;
np=8;
}
}
break;
case 5:
AS_printf ("Hardware 128K + IF1 (B)\n");
return (-1);
break;
case 6:
AS_printf ("Hardware 128K + M.G.T (C)\n");
return (-1);
break;
case 7:
if ((buffer[37] & 0x80) == 0x80 ){
AS_printf("Hardware +2A 128K (D)\n");
hwmodel=SPECMDL_PLUS3;
np=8;
} else {
AS_printf ("Hardware Spectrum +3 (E)\n");
hwmodel=SPECMDL_PLUS3;
np=8;
}
break;
case 8:
if ((buffer[37] & 0x80) == 0x80 ){
AS_printf("Hardware +2A 128K (F)\n");
hwmodel=SPECMDL_PLUS3;
np=8;
} else {
AS_printf ("Hardware Spectrum +3 (xzx deprecated)(10)\n");
hwmodel=SPECMDL_PLUS3;
np=8;
}
break;
case 9:
AS_printf ("Pentagon 128K (11)\n");
np=8;
return (-1);
break;
case 10:
AS_printf ("Hardware Scorpion (12)\n");
np=16;
return (-1);
break;
case 11:
AS_printf ("Hardware Didaktik-Kompakt (13)\n");
return (-1);
break;
case 12:
AS_printf ("Hardware Spectrum +2 128K (14)\n");
hwmodel=SPECMDL_PLUS2;
np=8;
break;
case 13:
AS_printf ("Hardware Spectrum +2A 128K (15)\n");
hwmodel=SPECMDL_PLUS3;
np=8;
break;
case 14:
AS_printf ("Hardware Timex TC2048 (16)\n");
return (-1);
break;
case 64:
AS_printf ("Hardware Inves Spectrum + 48K (Aspectrum Specific)(17)\n");
hwmodel=SPECMDL_INVES;
np=4;
break;
case 65:
AS_printf ("Hardware Spectrum 128K Spanish (Aspectrum Specific)(18)\n");
hwmodel=SPECMDL_128K;
np=8;
break;
case 128:
AS_printf ("Hardware Timex TC2068 (19)\n");
return (-1);
break;
default:
AS_printf ("Unknown hardware, %x\n", buffer[34]);
return (-1);
break;
}
/*
if (hwmodel!=SPECMDL_48K) {
AS_printf("Hardware no soporta carga de .Z80\n");
return (-1);
}
*/
if (hwopt.hw_model != hwmodel ) { // cambiamos de arquitectura si no es correcta.
end_spectrum ();
if (init_spectrum (hwmodel, "")==1) return (1); // si falla la arquitectura no sigo
}
if (np==0) AS_printf("YEPA te has colado np=0\n");
sig = 30 + 2 + buffer[30];
fseek (fp, sig, SEEK_SET);
for (f = 0; f < np; f++) {
tam = fgetc (fp);
tam = tam + (fgetc (fp) << 8);
pag = fgetc (fp);
sig = sig + tam + 3;
AS_printf (" pagina a cargar es: %i, tama�o:%x\n", pag, tam);
switch (hwmodel) {
case SPECMDL_16K:
case SPECMDL_48K:
switch (pag) {
case 4:
ini = 0x8000;
break;
case 5:
ini = 0xc000;
break;
case 8:
ini = 0x4000;
break;
default:
ini = 0x4000;
AS_printf
("Algo raro pasa con ese Snapshot, remitalo al autor para debug\n");
break;
}
break;
case SPECMDL_128K:
case SPECMDL_PLUS2:
case SPECMDL_PLUS3:
switch (pag) {
case 3:
ini = 0x0000;
break;
case 4:
ini = 0x4000;
break;
case 5:
ini = 0x8000;
break;
case 6:
ini = 0xc000;
break;
case 7:
ini = 0x10000;
break;
case 8:
ini = 0x14000;
break;
case 9:
ini = 0x18000;
break;
case 10:
ini = 0x1c000;
break;
default:
ini = 0x04000;
AS_printf("Algo raro pasa con ese Snapshot, remitalo al autor para debug\n");
break;
}
break;
case SPECMDL_INVES:
switch (pag) {
case 4:
ini = 0x8000;
break;
case 5:
ini = 0xc000;
break;
case 8:
ini = 0x4000;
break;
case 0:
ini = 0x10000;
break;
default:
ini = 0x4000;
AS_printf ("Algo raro pasa con ese Snapshot, remitalo al autor para debug\n");
break;
}
break;
}
UncompressZ80 ((tam == 0xffff ? Z80BL_V2UNCOMP : Z80BL_V2COMPRE),ini, regs, fp);
}
regs->PC.B.l = buffer[32];
regs->PC.B.h = buffer[33];
if ((hwmodel==SPECMDL_128K) || (hwmodel==SPECMDL_PLUS2)) {
hwopt.BANKM=0x00; /* need to clear lock latch or next dont work. */
outbankm_128k(buffer[35]);
}
if (hwmodel==SPECMDL_PLUS3) {
hwopt.BANKM=0x00; /* need to clear lock latch or next dont work. */
outbankm_p37(buffer[35]);
outbankm_p31(buffer[86]);
}
// aki abria que a�adir lo del sonido claro.
} else {
AS_printf ("Fichero Z80: Version 1.0\n");
if ( hwopt.hw_model != SPECMDL_48K ) { // V1 es siempre 48K camb. arquit. si no es correcta.
end_spectrum ();
init_spectrum (SPECMDL_48K, "");
}
if (buffer[12] & 0x20) {
AS_printf ("Fichero Z80: Comprimido\n");
fseek (fp, 30, SEEK_SET);
UncompressZ80 (Z80BL_V1COMPRE, 0x4000, regs, fp);
} else {
AS_printf ("Fichero Z80: Sin comprimir\n");
fseek (fp, 30, SEEK_SET);
UncompressZ80 (Z80BL_V1UNCOMP, 0x4000, regs, fp);
}
regs->PC.B.l = buffer[6];
regs->PC.B.h = buffer[7];
}
regs->AF.B.h = buffer[0];
regs->AF.B.l = buffer[1];
regs->BC.B.l = buffer[2];
regs->BC.B.h = buffer[3];
regs->HL.B.l = buffer[4];
regs->HL.B.h = buffer[5];
regs->SP.B.l = buffer[8];
regs->SP.B.h = buffer[9];
regs->I = buffer[10];
regs->R.B.l = buffer[11];
regs->R.B.h = 0;
hwopt.BorderColor = ((buffer[12] & 0x0E) >> 1);
regs->DE.B.l = buffer[13];
regs->DE.B.h = buffer[14];
regs->BCs.B.l = buffer[15];
regs->BCs.B.h = buffer[16];
regs->DEs.B.l = buffer[17];
regs->DEs.B.h = buffer[18];
regs->HLs.B.l = buffer[19];
regs->HLs.B.h = buffer[20];
regs->AFs.B.h = buffer[21];
regs->AFs.B.l = buffer[22];
regs->IY.B.l = buffer[23];
regs->IY.B.h = buffer[24];
regs->IX.B.l = buffer[25];
regs->IX.B.h = buffer[26];
regs->IFF1 = buffer[27] & 0x01;
regs->IFF2 = buffer[28] & 0x01;
regs->IM = buffer[29] & 0x03;
return (0);
}
/*-----------------------------------------------------------------
char LoadSP( Z80Regs *regs, FILE *fp );
This loads a .SP file from disk to the Z80 registers/memory.
------------------------------------------------------------------*/
uint8_t LoadSP (Z80Regs * regs, FILE *fp){
extern tipo_hwopt hwopt;
unsigned short length, start, sword;
int f;
uint8_t buffer[80]; // �por que 80 si leemos 38?
fread (buffer, 38, 1, fp);
/* load the .SP header: */
length = (buffer[3] << 8) + buffer[2];
start = (buffer[5] << 8) + buffer[4];
regs->BC.B.l = buffer[6];
regs->BC.B.h = buffer[7];
regs->DE.B.l = buffer[8];
regs->DE.B.h = buffer[9];
regs->HL.B.l = buffer[10];
regs->HL.B.h = buffer[11];
regs->AF.B.l = buffer[12];
regs->AF.B.h = buffer[13];
regs->IX.B.l = buffer[14];
regs->IX.B.h = buffer[15];
regs->IY.B.l = buffer[16];
regs->IY.B.h = buffer[17];
regs->BCs.B.l = buffer[18];
regs->BCs.B.h = buffer[19];
regs->DEs.B.l = buffer[20];
regs->DEs.B.h = buffer[21];
regs->HLs.B.l = buffer[22];
regs->HLs.B.h = buffer[23];
regs->AFs.B.l = buffer[24];
regs->AFs.B.h = buffer[25];
regs->R.W = 0;
regs->R.B.l = buffer[26];
regs->I = buffer[27];
regs->SP.B.l = buffer[28];
regs->SP.B.h = buffer[29];
regs->PC.B.l = buffer[30];
regs->PC.B.h = buffer[31];
hwopt.BorderColor = buffer[34];
sword = (buffer[37] << 8) | buffer[36];
AS_printf ("\nSP_PC = %04X, SP_START = %d, SP_LENGTH = %d\n", regs->PC,
start, length);
/* interrupt mode */
regs->IFF1 = regs->IFF2 = 0;
if (sword & 0x4)
regs->IFF2 = 1;
if (sword & 0x8)
regs->IM = 0;
else {
if (sword & 0x2)
regs->IM = 2;
else
regs->IM = 1;
}
if (sword & 0x1)
regs->IFF1 = 1;
if (sword & 0x16) {
AS_printf ("\n\nPENDING INTERRUPT!!\n\n");
} else {
AS_printf ("\n\nno pending interrupt.\n\n");
}
//FIXME leer todo a la vez.
for (f = 0; f <= length; f++)
if (start + f < 65536)
// regs->RAM[start + f] = fgetc (fp);
// writemem (start + f, fgetc (fp));
*(mem.p+start + f)=fgetc (fp);
return (0);
}
/*-----------------------------------------------------------------
char LoadSNA( Z80Regs *regs, char *filename );
This loads a .SNA file from disk to the Z80 registers/memory.
------------------------------------------------------------------*/
int Load_SNA (Z80Regs * regs, char *filename){
File f;
uint8_t page;
uint8_t buffer[27];
uint8_t unbyte;
int model;
//FIXIT no extern
extern tipo_hwopt hwopt;
extern tipo_mem mem;
AS_print("Cargamos un SNA (by filename)\n");
f=SPIFFS.open(filename,FILE_READ);
if (!f) {
AS_print("Algo fallo cargando el SNA\n");
return 1;
}
if (f.size() == 49179) model = SPECMDL_48K ; // es un 48Kb
else if (f.size() == 16411) model = SPECMDL_16K ; //16Kb UNSUPPORTED!!
else model = SPECMDL_128K ;
if (hwopt.hw_model != model) { // comprobamos si es el modelo correcto.
end_spectrum ();
init_spectrum (model, "");
}
f.seek (0, SeekSet);
f.read (buffer, 27);
regs->I = buffer[0];
regs->HLs.B.l = buffer[1];
regs->HLs.B.h = buffer[2];
regs->DEs.B.l = buffer[3];
regs->DEs.B.h = buffer[4];
regs->BCs.B.l = buffer[5];
regs->BCs.B.h = buffer[6];
regs->AFs.B.l = buffer[7];
regs->AFs.B.h = buffer[8];
regs->HL.B.l = buffer[9];
regs->HL.B.h = buffer[10];
regs->DE.B.l = buffer[11];
regs->DE.B.h = buffer[12];
regs->BC.B.l = buffer[13];
regs->BC.B.h = buffer[14];
regs->IY.B.l = buffer[15];
regs->IY.B.h = buffer[16];
regs->IX.B.l = buffer[17];
regs->IX.B.h = buffer[18];
regs->IFF1 = regs->IFF2 = (buffer[19] & 0x04) >> 2;
regs->R.W = buffer[20];
regs->AF.B.l = buffer[21];
regs->AF.B.h = buffer[22];
regs->SP.B.l = buffer[23];
regs->SP.B.h = buffer[24];
regs->IM = buffer[25];
hwopt.BorderColor = buffer[26];
if (model=SPECMDL_48K) { // es un 48Kb
f.read (mem.p + 16384, 0x4000 * 3);
regs->PC.B.l = z80_peek(regs->SP.W);
regs->SP.W++;
regs->PC.B.h = z80_peek(regs->SP.W);
regs->SP.W++;
} else if (model=SPECMDL_16K) { // es un 16Kb UNSUPPORTED
f.read (mem.p + 16384, 0x4000 * 1);
regs->PC.B.l = z80_peek(regs->SP.W);
regs->SP.W++;
regs->PC.B.h = z80_peek(regs->SP.W);
regs->SP.W++;
} else { // es un 128Kb FIXME hay SNA de 16Kb
f.seek (49179, SeekSet);
f.read(&unbyte,1); //FIXME seguro que hay formas mejores
regs->PC.B.l = unbyte;
f.read(&unbyte,1);
regs->PC.B.h = unbyte;
f.read(&page,1);
hwopt.BANKM = 0x00; /* unlock 128K pagging */
outbankm_128k (page);
page &= 0x07;
// ahora empezamos a rellenar ram.
f.seek (27, SeekSet);
f.read (mem.p + (5 * 0x4000) , 0x4000);
f.read (mem.p + (2 * 0x4000) , 0x4000);
f.read (mem.p + (page * 0x4000), 0x4000);
//resto de paginas.
f.seek (49183, SeekSet);
if (page != 0) f.read (mem.p + (0 * 0x4000), 0x4000);
if (page != 1) f.read (mem.p + (1 * 0x4000), 0x4000);
if (page != 3) f.read (mem.p + (3 * 0x4000), 0x4000);
if (page != 4) f.read (mem.p + (4 * 0x4000), 0x4000);
if (page != 6) f.read (mem.p + (6 * 0x4000), 0x4000);
if (page != 7) f.read (mem.p + (7 * 0x4000), 0x4000);
}
f.close();
return (0);
}
/*-----------------------------------------------------------------
char SaveSNA( Z80Regs *regs, FILE *fp );
This saves a .SNA file from the Z80 registers/memory to disk.
------------------------------------------------------------------*/
uint8_t SaveSNA (Z80Regs * regs, FILE * fp){
extern tipo_hwopt hwopt;
extern tipo_mem mem;
unsigned char sptmpl, sptmph;
// int c;
// SNA solo esta soportado en 48K, 128K y +2
if ((hwopt.hw_model != SPECMDL_48K) && (hwopt.hw_model != SPECMDL_128K)) {
AS_print ("El modelo de Spectrum utilizado");
AS_print ("No permite grabar el snapshot en formato SNA");
AS_print ("Utilize otro tipo de archivo (extension)");
return 1;
}
/* save the .SNA header */
fputc (regs->I, fp);
fputc (regs->HLs.B.l, fp);
fputc (regs->HLs.B.h, fp);
fputc (regs->DEs.B.l, fp);
fputc (regs->DEs.B.h, fp);
fputc (regs->BCs.B.l, fp);
fputc (regs->BCs.B.h, fp);
fputc (regs->AFs.B.l, fp);
fputc (regs->AFs.B.h, fp);
fputc (regs->HL.B.l, fp);
fputc (regs->HL.B.h, fp);
fputc (regs->DE.B.l, fp);
fputc (regs->DE.B.h, fp);
fputc (regs->BC.B.l, fp);
fputc (regs->BC.B.h, fp);
fputc (regs->IY.B.l, fp);
fputc (regs->IY.B.h, fp);
fputc (regs->IX.B.l, fp);
fputc (regs->IX.B.h, fp);
fputc (regs->IFF1 << 2, fp);
fputc (regs->R.W & 0xFF, fp);
fputc (regs->AF.B.l, fp);
fputc (regs->AF.B.h, fp);
// sptmpl = Z80MemRead (regs->SP.W - 1, regs);
// sptmph = Z80MemRead (regs->SP.W - 2, regs);
sptmpl = z80_peek (regs->SP.W - 1);
sptmph = z80_peek (regs->SP.W - 2);
if (hwopt.hw_model == SPECMDL_48K) { // code for the 48K version.
AS_printf("Guardando SNA 48K\n");
/* save PC on the stack */
// Z80MemWrite (--(regs->SP.W), regs->PC.B.h, regs);
// Z80MemWrite (--(regs->SP.W), regs->PC.B.l, regs);
z80_poke (--(regs->SP.W), regs->PC.B.h);
z80_poke (--(regs->SP.W), regs->PC.B.l);
fputc (regs->SP.B.l, fp);
fputc (regs->SP.B.h, fp);
fputc (regs->IM, fp);
fputc (hwopt.BorderColor, fp);
/* save the RAM contents */
// fwrite (regs->RAM + 16384, 48 * 1024, 1, fp);
fwrite (mem.p + 16384, 0x4000 * 3, 1, fp);
/* restore the stack and the SP value */
regs->SP.W += 2;
// Z80MemWrite (regs->SP.W - 1, sptmpl, regs);
// Z80MemWrite (regs->SP.W - 2, sptmph, regs);
z80_poke (regs->SP.W - 1, sptmpl);
z80_poke (regs->SP.W - 2, sptmph);
} else { // code for the 128K version
AS_printf("Guardando SNA 128K\n");
fputc (regs->SP.B.l, fp);
fputc (regs->SP.B.h, fp);
fputc (regs->IM, fp);
fputc (hwopt.BorderColor, fp);
// volcar la ram
// Volcar 5
// for (c=mem.ro[1];(c<mem.ro[1]+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + mem.ro[1], 0x4000, 1, fp);
// Volcar 2
// for (c=mem.ro[2];(c<mem.ro[2]+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + mem.ro[2], 0x4000, 1, fp);
// volcar N
// for (c=mem.ro[3];(c<mem.ro[3]+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + mem.ro[3], 0x4000, 1, fp);
// resto de cosas
fputc (regs->PC.B.l, fp);
fputc (regs->PC.B.h, fp);
fputc (hwopt.BANKM, fp);
fputc (0, fp); // TR-DOS rom paged (1) or not (0)
// volcar el resto de ram.
// Volcar 0 si no en (4)
if (mem.ro[3] != (0 * mem.sp))
// for (c=0*mem.sp;c<((0*mem.sp)+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + (0 * mem.sp), 0x4000, 1, fp);
// Volcar 1 si no en (4)
if (mem.ro[3] != (1 * mem.sp))
// for (c=1*mem.sp;c<((1*mem.sp)+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + (1 * mem.sp), 0x4000, 1, fp);
// Volcar 3 si no en (4)
if (mem.ro[3] != (3 * mem.sp))
// for (c=3*mem.sp;c<((3*mem.sp)+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + (3 * mem.sp), 0x4000, 1, fp);
// Volcar 4 si no en (4)
if (mem.ro[3] != (4 * mem.sp))
// for (c=4*mem.sp;c<((4*mem.sp)+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + (4 * mem.sp), 0x4000, 1, fp);
// Volcar 6 si no en (4)
if (mem.ro[3] != (6 * mem.sp))
// for (c=6*mem.sp;c<((6*mem.sp)+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + (6 * mem.sp), 0x4000, 1, fp);
// Volcar 7 si no en (4)
if (mem.ro[3] != (7 * mem.sp))
// for (c=7*mem.sp;c<((7*mem.sp)+0x4000);c++) fputc (mem.p[c],fp);
fwrite (mem.p + (7 * mem.sp), 0x4000, 1, fp);
}
return (0);
}
uint8_t SaveSP (Z80Regs * regs, FILE * fp){
extern tipo_mem mem;
extern tipo_hwopt hwopt;
// save the .SP header
fputc ('S', fp);
fputc ('P', fp);
fputc (00, fp);
fputc (0xC0, fp); // 49152
fputc (00, fp);
fputc (0x40, fp); //16384
// save the state
fputc (regs->BC.B.l, fp);
fputc (regs->BC.B.h, fp);
fputc (regs->DE.B.l, fp);
fputc (regs->DE.B.h, fp);
fputc (regs->HL.B.l, fp);
fputc (regs->HL.B.h, fp);
fputc (regs->AF.B.l, fp);
fputc (regs->AF.B.h, fp);
fputc (regs->IX.B.l, fp);
fputc (regs->IX.B.h, fp);
fputc (regs->IY.B.l, fp);
fputc (regs->IY.B.h, fp);
fputc (regs->BCs.B.l, fp);
fputc (regs->BCs.B.h, fp);
fputc (regs->DEs.B.l, fp);
fputc (regs->DEs.B.h, fp);
fputc (regs->HLs.B.l, fp);
fputc (regs->HLs.B.h, fp);
fputc (regs->AFs.B.l, fp);
fputc (regs->AFs.B.h, fp);
fputc (regs->R.B.l, fp);
fputc (regs->I, fp);
fputc (regs->SP.B.l, fp);
fputc (regs->SP.B.h, fp);
fputc (regs->PC.B.l, fp);
fputc (regs->PC.B.h, fp);
fputc (0, fp);
fputc (0, fp);
fputc (hwopt.BorderColor, fp);
fputc (0, fp);
// Estado, pendiente por poner: Si hay una int pendiente (bit 4), valor de flash (bit 5).
fputc (0, fp);
fputc ((((regs->IFF2) << 2) + ((regs->IM) == 2 ? 0x02 : 0x00) + regs->IFF1),
fp);
// Save the ram
// fwrite (regs->RAM + 16384, 48 * 1024, 1, fp);
fwrite (mem.p + 16384, 0x4000 * 3, 1, fp);
return (0);
}
void writeZ80block(int block, int offset, FILE *fp)
{
extern tipo_mem mem;
fputc(0xff,fp); fputc(0xff,fp);
fputc((byte)block,fp);
fwrite (mem.p + offset, 0x4000 , 1, fp);
}
uint8_t SaveZ80 (Z80Regs * regs, FILE * fp){
extern tipo_mem mem;
extern tipo_hwopt hwopt;
int c;
// 48K .z80 are saved as ver 1.45
AS_printf ("Guardando Z80...\n");
fputc (regs->AF.B.h, fp);
fputc (regs->AF.B.l, fp);
fputc (regs->BC.B.l, fp);
fputc (regs->BC.B.h, fp);
fputc (regs->HL.B.l, fp);
fputc (regs->HL.B.h, fp);
if (hwopt.hw_model==SPECMDL_48K) { // si no es 48K entonces V 3.0
AS_printf ("...de 48K\n");
fputc (regs->PC.B.l, fp);
fputc (regs->PC.B.h, fp);
} else {
AS_printf ("...de NO 48K\n");
fputc(0,fp);
fputc(0,fp);
}
fputc (regs->SP.B.l, fp);
fputc (regs->SP.B.h, fp);
fputc (regs->I, fp);
fputc ((regs->R.B.l & 0x7F), fp);
fputc ((((regs->R.B.l & 0x80) >> 7) | ((hwopt.BorderColor & 0x07) << 1)),
fp); //Datos sin comprimir por defecto.
fputc (regs->DE.B.l, fp);
fputc (regs->DE.B.h, fp);
fputc (regs->BCs.B.l, fp);
fputc (regs->BCs.B.h, fp);
fputc (regs->DEs.B.l, fp);
fputc (regs->DEs.B.h, fp);
fputc (regs->HLs.B.l, fp);
fputc (regs->HLs.B.h, fp);
fputc (regs->AFs.B.h, fp);
fputc (regs->AFs.B.l, fp);
fputc (regs->IY.B.l, fp);
fputc (regs->IY.B.h, fp);
fputc (regs->IX.B.l, fp);
fputc (regs->IX.B.h, fp);
fputc (regs->IFF1, fp);
fputc (regs->IFF2, fp);
fputc ((regs->IM & 0x7), fp); // issue 2 and joystick cfg not saved.
if (hwopt.hw_model==SPECMDL_48K) {
// fwrite (regs->RAM + 16384, 48 * 1024, 1, fp);
fwrite (mem.p + 16384, 0x4000 * 3, 1, fp);
} else { // aki viene la parte de los 128K y demas
fputc( (hwopt.hw_model==SPECMDL_PLUS3 ? 55:54 ),fp);
fputc(0,fp);
fputc (regs->PC.B.l, fp);
fputc (regs->PC.B.h, fp);
switch (hwopt.hw_model) {
case SPECMDL_16K:
fputc(0,fp);
fputc(0,fp);
break;
case SPECMDL_INVES:
fputc(64,fp);
fputc(0,fp);
break;
case SPECMDL_128K:
fputc(4,fp);
fputc(hwopt.BANKM,fp);
break;
case SPECMDL_PLUS2:
fputc(12,fp);
fputc(hwopt.BANKM,fp);
break;
case SPECMDL_PLUS3: // as there is no disk emuation, alwais save as +2A
fputc(13,fp);
fputc(hwopt.BANKM,fp);
default:
AS_printf("ERROR: HW Type Desconocido, nunca deberias ver esto.\n");
fputc(0,fp);
fputc(0,fp);
break;
}
fputc(0,fp); // if1 not paged.
if (hwopt.hw_model==SPECMDL_16K) fputc(0x81,fp);
else fputc(1,fp); //siempre emulamos el registro R (al menos que yo sepa)
for (c=0;c<(1+16+3+1+4+20+3);c++) fputc(0,fp);
if (hwopt.hw_model==SPECMDL_PLUS3) fputc(hwopt.BANK678,fp);
switch (hwopt.hw_model) {
case SPECMDL_16K:
writeZ80block(8,0x4000,fp);
break;
case SPECMDL_INVES:
writeZ80block(8,0x4000,fp);
writeZ80block(4,0x8000,fp);
writeZ80block(5,0xC000,fp);