-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathz80.c
1862 lines (1492 loc) · 59.5 KB
/
z80.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
/* z80.c: Routines for handling .z80 snapshots
Copyright (c) 2001-2016 Philip Kendall, Darren Salt, Fredrick Meunier
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "internals.h"
/* Length of the basic .z80 headers */
static const int LIBSPECTRUM_Z80_HEADER_LENGTH = 30;
/* Length of the v2 extensions */
#define LIBSPECTRUM_Z80_V2_LENGTH 23
/* Length of the v3 extensions */
#define LIBSPECTRUM_Z80_V3_LENGTH 54
/* Length of xzx's extensions */
#define LIBSPECTRUM_Z80_V3X_LENGTH 55
/* The constants used for each machine type */
enum {
/* v1 constants */
Z80_JOYSTICK_CURSOR_V1 = 0,
Z80_JOYSTICK_KEMPSTON_V1 = 1,
Z80_JOYSTICK_SINCLAIR_LEFT_V1 = 2,
Z80_JOYSTICK_SINCLAIR_RIGHT_V1 = 3,
/* v2 constants */
Z80_MACHINE_48_V2 = 0,
Z80_MACHINE_48_IF1_V2 = 1,
Z80_MACHINE_48_SAMRAM_V2 = 2,
Z80_MACHINE_128_V2 = 3,
Z80_MACHINE_128_IF1_V2 = 4,
/* v3 constants */
Z80_MACHINE_48 = 0,
Z80_MACHINE_48_IF1 = 1,
Z80_MACHINE_48_SAMRAM = 2,
Z80_MACHINE_48_MGT = 3,
Z80_MACHINE_128 = 4,
Z80_MACHINE_128_IF1 = 5,
Z80_MACHINE_128_MGT = 6,
Z80_MGT_DISCIPLE_EPSON = 0,
Z80_MGT_DISCIPLE_HP = 1,
Z80_MGT_PLUSD = 16,
Z80_JOYSTICK_CURSOR_V3 = 0,
Z80_JOYSTICK_KEMPSTON_V3 = 1,
Z80_JOYSTICK_USER_DEFINED_V3 = 2,
Z80_JOYSTICK_SINCLAIR_RIGHT_V3 = 3,
/* Extensions */
Z80_MACHINE_PLUS3 = 7,
Z80_MACHINE_PLUS3_XZX_ERROR = 8,
Z80_MACHINE_PENTAGON = 9,
Z80_MACHINE_SCORPION = 10,
Z80_MACHINE_PLUS2 = 12,
Z80_MACHINE_PLUS2A = 13,
Z80_MACHINE_TC2048 = 14,
Z80_MACHINE_TC2068 = 15,
Z80_MACHINE_TS2068 = 128,
/* The first extension ID; anything here or greater applies to both
v2 and v3 files */
Z80_MACHINE_FIRST_EXTENSION = Z80_MACHINE_PLUS3,
};
/* Constants representing a Sinclair Interface 2 port 1 joystick as a .z80
user defined joystick */
static const libspectrum_word if2_left_map_l = 0x0f03;
static const libspectrum_word if2_left_map_r = 0x0803;
static const libspectrum_word if2_left_map_d = 0x0403;
static const libspectrum_word if2_left_map_u = 0x0203;
static const libspectrum_word if2_left_map_f = 0x0103;
static const libspectrum_word if2_left_l = '1';
static const libspectrum_word if2_left_r = '2';
static const libspectrum_word if2_left_d = '3';
static const libspectrum_word if2_left_u = '4';
static const libspectrum_word if2_left_f = '5';
/* The signature used to designate the .slt extensions */
static libspectrum_byte slt_signature[] = "\0\0\0SLT";
static size_t slt_signature_length = 6;
/* Bits used in extended header to flag that a Fuller Box is in use */
static const libspectrum_byte fuller_box_flags = 0x44;
/* Bits used in extended header to flag that a Melodik is in use */
static const libspectrum_byte melodik_flags = 0x04;
static libspectrum_error
read_header( const libspectrum_byte *buffer, libspectrum_snap *snap,
const libspectrum_byte **data, int *version, int *compressed );
static libspectrum_error
get_machine_type( libspectrum_snap *snap, libspectrum_byte type,
libspectrum_byte mgt_type, int version );
static libspectrum_error
get_machine_type_v2( libspectrum_snap *snap, libspectrum_byte type );
static libspectrum_error
get_machine_type_v3( libspectrum_snap *snap, libspectrum_byte type,
libspectrum_byte mgt_type );
static libspectrum_error
get_machine_type_extension( libspectrum_snap *snap, libspectrum_byte type );
static libspectrum_error
get_joystick_type( libspectrum_snap *snap,
const libspectrum_byte *custom_joystick_base,
libspectrum_byte type, int version );
static libspectrum_error
get_joystick_type_v1( libspectrum_snap *snap, libspectrum_byte type );
static libspectrum_error
get_joystick_type_v3( libspectrum_snap *snap,
const libspectrum_byte *custom_joystick_base,
libspectrum_byte type );
static libspectrum_error
read_blocks( const libspectrum_byte *buffer, size_t buffer_length,
libspectrum_snap *snap, int version, int compressed );
static libspectrum_error
read_slt( libspectrum_snap *snap, const libspectrum_byte **next_block,
const libspectrum_byte *end );
static libspectrum_error
read_block( const libspectrum_byte *buffer, libspectrum_snap *snap,
const libspectrum_byte **next_block, const libspectrum_byte *end,
int version, int compressed );
static libspectrum_error
read_v1_block( const libspectrum_byte *buffer, int is_compressed,
libspectrum_byte **uncompressed,
const libspectrum_byte **next_block,
const libspectrum_byte *end );
static libspectrum_error
read_v2_block( const libspectrum_byte *buffer, libspectrum_byte **block,
size_t *length, int *page, const libspectrum_byte **next_block,
const libspectrum_byte *end );
static libspectrum_error
write_header( libspectrum_buffer *buffer, int *flags, libspectrum_snap *snap );
static void
write_base_header( libspectrum_buffer *buffer, int *flags,
libspectrum_snap *snap );
static libspectrum_error
write_extended_header( libspectrum_buffer *buffer, int *flags,
libspectrum_snap *snap );
static void
write_pages( libspectrum_buffer *buffer, libspectrum_snap *snap,
int compress );
static void
write_page( libspectrum_buffer *buffer, int page_num, libspectrum_byte *page,
int compress );
static void
write_slt( libspectrum_buffer *buffer, libspectrum_snap *snap );
static void
write_slt_entry( libspectrum_buffer *buffer, libspectrum_word type,
libspectrum_word id, libspectrum_dword slt_length );
static void
compress_block( libspectrum_byte **dest, size_t *dest_length,
const libspectrum_byte *src, size_t src_length);
static void
uncompress_block( libspectrum_byte **dest, size_t *dest_length,
const libspectrum_byte *src, size_t src_length);
/* The various things which can appear in the .slt data */
enum slt_type {
LIBSPECTRUM_SLT_TYPE_END = 0,
LIBSPECTRUM_SLT_TYPE_LEVEL,
LIBSPECTRUM_SLT_TYPE_INSTRUCTIONS,
LIBSPECTRUM_SLT_TYPE_SCREEN,
LIBSPECTRUM_SLT_TYPE_PICTURE,
LIBSPECTRUM_SLT_TYPE_POKE,
};
libspectrum_error
internal_z80_read( libspectrum_snap *snap,
const libspectrum_byte *buffer, size_t buffer_length )
{
libspectrum_error error;
const libspectrum_byte *data;
int version, compressed = 1;
error = read_header( buffer, snap, &data, &version, &compressed );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
error = read_blocks( data, buffer_length - (data - buffer), snap,
version, compressed );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
libspectrum_snap_set_beta_paged( snap, 0 );
if( libspectrum_snap_interface1_active( snap ) )
libspectrum_snap_set_interface1_drive_count( snap, 8 );
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
read_header( const libspectrum_byte *buffer, libspectrum_snap *snap,
const libspectrum_byte **data, int *version, int *compressed )
{
const libspectrum_byte *header = buffer;
int capabilities;
size_t i;
libspectrum_error error;
libspectrum_snap_set_a ( snap, header[ 0] );
libspectrum_snap_set_f ( snap, header[ 1] );
libspectrum_snap_set_bc ( snap, header[ 2] + header[ 3]*0x100 );
libspectrum_snap_set_de ( snap, header[13] + header[14]*0x100 );
libspectrum_snap_set_hl ( snap, header[ 4] + header[ 5]*0x100 );
libspectrum_snap_set_a_ ( snap, header[21] );
libspectrum_snap_set_f_ ( snap, header[22] );
libspectrum_snap_set_bc_( snap, header[15] + header[16]*0x100 );
libspectrum_snap_set_de_( snap, header[17] + header[18]*0x100 );
libspectrum_snap_set_hl_( snap, header[19] + header[20]*0x100 );
libspectrum_snap_set_ix ( snap, header[25] + header[26]*0x100 );
libspectrum_snap_set_iy ( snap, header[23] + header[24]*0x100 );
libspectrum_snap_set_i ( snap, header[10] );
libspectrum_snap_set_r ( snap, ( header[11] & 0x7f ) +
( ( header[12] & 0x01 ) << 7 ) );
libspectrum_snap_set_pc ( snap, header[ 6] + header[ 7]*0x100 );
libspectrum_snap_set_sp ( snap, header[ 8] + header[ 9]*0x100 );
libspectrum_snap_set_iff1( snap, header[27] ? 1 : 0 );
libspectrum_snap_set_iff2( snap, header[28] ? 1 : 0 );
libspectrum_snap_set_im( snap, header[29] & 0x03 );
libspectrum_snap_set_issue2( snap, header[29] & 0x04 );
libspectrum_snap_set_out_ula( snap, (header[12]&0x0e) >> 1 );
if( libspectrum_snap_pc( snap ) == 0 ) { /* PC == 0x0000 => v2 or greater */
size_t extra_length;
const libspectrum_byte *extra_header;
libspectrum_dword quarter_tstates;
extra_length = header[ LIBSPECTRUM_Z80_HEADER_LENGTH ] +
header[ LIBSPECTRUM_Z80_HEADER_LENGTH + 1 ] * 0x100;
switch( extra_length ) {
case LIBSPECTRUM_Z80_V2_LENGTH:
*version = 2;
break;
case LIBSPECTRUM_Z80_V3_LENGTH:
case LIBSPECTRUM_Z80_V3X_LENGTH:
*version = 3;
break;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_read_z80_header: unknown header length %d",
(int)extra_length
);
return LIBSPECTRUM_ERROR_UNKNOWN;
}
extra_header = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2;
error = get_joystick_type( snap,
extra_header + 31,
( header[29] & 0xc0 ) >> 6,
*version
);
if( error ) return error;
libspectrum_snap_set_pc( snap, extra_header[0] + extra_header[1] * 0x100 );
error = get_machine_type( snap, extra_header[2], extra_header[51], *version );
if( error ) return error;
if( extra_header[27] && libspectrum_snap_plusd_active( snap ) ) {
libspectrum_snap_set_plusd_paged( snap, 1 );
}
if( *version >= 3 ) {
quarter_tstates =
libspectrum_timings_tstates_per_frame(
libspectrum_snap_machine( snap )
) / 4;
/* This is correct, even if it does disagree with Z80 v3.05's
`z80dump'; thanks to Pedro Gimeno for pointing this out when
this code was part of SnapConv */
libspectrum_snap_set_tstates( snap,
( ( ( extra_header[25] + 1 ) % 4 ) + 1 ) * quarter_tstates -
( extra_header[23] + extra_header[24] * 0x100 + 1 )
);
/* Stop broken files from causing trouble... */
if( libspectrum_snap_tstates( snap ) >= quarter_tstates * 4 )
libspectrum_snap_set_tstates( snap, 0 );
}
/* I don't like this any more than you do but here is support for the
Spectaculator extensions:
Support for 16k/+2/+2A snapshots
If bit 7 of byte 37 is set, this modifies the value of the hardware
field (for both v2 and v3 snapshots) such that:
a.. Any valid 48k identifier should be taken as 16k
b.. Any valid 128k identifier should be taken as +2
c.. Any valid +3 identifier (7 or 8) should be taken as +2A
Support for Fuller Box / AY sound in 48k mode
Spectaculator recognises xzx's extension of setting bit 2 of byte 37 to
specify AY sound in 48k mode. In addition, if this and also bit 6 is
set, this specifies Fuller Box emulation.
*/
if( extra_header[5] & 0x80 ) {
switch( libspectrum_snap_machine( snap ) ) {
case LIBSPECTRUM_MACHINE_48:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_16 ); break;
case LIBSPECTRUM_MACHINE_128:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PLUS2 ); break;
case LIBSPECTRUM_MACHINE_PLUS3:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PLUS2A );
break;
default:
break; /* Do nothing */
}
}
if( ( extra_header[5] & fuller_box_flags ) == fuller_box_flags ) {
libspectrum_snap_set_fuller_box_active( snap, 1 );
} else if( ( extra_header[5] & melodik_flags ) == melodik_flags ) {
libspectrum_snap_set_melodik_active( snap, 1 );
}
capabilities =
libspectrum_machine_capabilities( libspectrum_snap_machine( snap ) );
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY ) {
libspectrum_snap_set_out_128_memoryport( snap, extra_header[3] );
} else if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY ) {
libspectrum_snap_set_out_scld_hsr( snap, extra_header[3] );
}
if( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_VIDEO ) {
libspectrum_snap_set_out_scld_dec( snap, extra_header[4] );
}
if( libspectrum_snap_fuller_box_active( snap ) ||
libspectrum_snap_melodik_active( snap ) ||
capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_AY ) {
libspectrum_snap_set_out_ay_registerport( snap, extra_header[ 6] );
for( i = 0; i < 16; i++ ) {
libspectrum_snap_set_ay_registers( snap, i, extra_header[ 7 + i ] );
}
}
if( extra_length == LIBSPECTRUM_Z80_V3X_LENGTH ) {
if( ( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_PLUS3_MEMORY ) ||
( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY ) ) {
libspectrum_snap_set_out_plus3_memoryport( snap, extra_header[54] );
}
}
(*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2 + extra_length;
} else { /* v1 .z80 file */
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 );
*version = 1;
libspectrum_snap_set_joystick_active_count( snap, 1 );
error = get_joystick_type_v1( snap, ( header[29] & 0xc0 ) >> 6 );
if( error ) return error;
/* Need to flag this for later */
*compressed = ( header[12] & 0x20 ) ? 1 : 0;
(*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_machine_type( libspectrum_snap *snap, libspectrum_byte type,
libspectrum_byte mgt_type, int version )
{
libspectrum_error error;
if( type < Z80_MACHINE_FIRST_EXTENSION ) {
switch( version ) {
case 2:
error = get_machine_type_v2( snap, type ); if( error ) return error;
break;
case 3:
error = get_machine_type_v3( snap, type, mgt_type ); if( error ) return error;
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"%s:get_machine_type: unknown version %d",
__FILE__, version );
return LIBSPECTRUM_ERROR_LOGIC;
}
} else {
error = get_machine_type_extension( snap, type ); if( error ) return error;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_machine_type_v2( libspectrum_snap *snap, libspectrum_byte type )
{
switch( type ) {
case Z80_MACHINE_48_V2:
case Z80_MACHINE_48_SAMRAM_V2:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 ); break;
case Z80_MACHINE_48_IF1_V2:
libspectrum_snap_set_interface1_active( snap, 1 );
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 ); break;
case Z80_MACHINE_128_V2:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_128 ); break;
case Z80_MACHINE_128_IF1_V2:
libspectrum_snap_set_interface1_active( snap, 1 );
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_128 ); break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"%s:get_machine_type: unknown v2 machine type %d",
__FILE__, type );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_mgt_type( libspectrum_snap *snap, libspectrum_byte type )
{
switch( type ) {
case Z80_MGT_DISCIPLE_EPSON:
case Z80_MGT_DISCIPLE_HP:
break;
case Z80_MGT_PLUSD:
libspectrum_snap_set_plusd_active( snap, 1 );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"%s:get_mgt_type: unknown mgt type %d",
__FILE__, type );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_machine_type_v3( libspectrum_snap *snap, libspectrum_byte type,
libspectrum_byte mgt_type )
{
libspectrum_error error;
switch( type ) {
case Z80_MACHINE_48:
case Z80_MACHINE_48_SAMRAM:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 ); break;
case Z80_MACHINE_48_IF1:
libspectrum_snap_set_interface1_active( snap, 1 );
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 ); break;
case Z80_MACHINE_48_MGT:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 );
error = get_mgt_type( snap, mgt_type );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
break;
case Z80_MACHINE_128:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_128 ); break;
case Z80_MACHINE_128_IF1:
libspectrum_snap_set_interface1_active( snap, 1 );
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_128 ); break;
case Z80_MACHINE_128_MGT:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_128 );
error = get_mgt_type( snap, mgt_type );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"%s:get_machine_type: unknown v3 machine type %d",
__FILE__, type );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_machine_type_extension( libspectrum_snap *snap, libspectrum_byte type )
{
switch( type ) {
case Z80_MACHINE_PLUS3:
case Z80_MACHINE_PLUS3_XZX_ERROR:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PLUS3 ); break;
case Z80_MACHINE_PENTAGON:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PENT ); break;
case Z80_MACHINE_SCORPION:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_SCORP ); break;
case Z80_MACHINE_PLUS2:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PLUS2 ); break;
case Z80_MACHINE_PLUS2A:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PLUS2A ); break;
case Z80_MACHINE_TC2048:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_TC2048 ); break;
case Z80_MACHINE_TC2068:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_TC2068 ); break;
case Z80_MACHINE_TS2068:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_TS2068 ); break;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"%s:get_machine_type: unknown extension machine type %d", __FILE__, type
);
return LIBSPECTRUM_ERROR_UNKNOWN;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_joystick_type( libspectrum_snap *snap,
const libspectrum_byte *custom_joystick_base,
libspectrum_byte type, int version )
{
libspectrum_error error;
libspectrum_snap_set_joystick_active_count( snap, 1 );
libspectrum_snap_set_joystick_inputs( snap, 0,
LIBSPECTRUM_JOYSTICK_INPUT_KEYBOARD |
LIBSPECTRUM_JOYSTICK_INPUT_JOYSTICK_1 );
switch( version ) {
case 1:
case 2:
error = get_joystick_type_v1( snap, type ); if( error ) return error;
break;
case 3:
error = get_joystick_type_v3( snap, custom_joystick_base, type );
if( error ) return error;
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"%s:get_joystick_type: unknown version %d",
__FILE__, version );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_joystick_type_v1( libspectrum_snap *snap, libspectrum_byte type )
{
switch( type ) {
case Z80_JOYSTICK_CURSOR_V1:
libspectrum_snap_set_joystick_list( snap, 0, LIBSPECTRUM_JOYSTICK_CURSOR );
break;
case Z80_JOYSTICK_KEMPSTON_V1:
libspectrum_snap_set_joystick_list( snap, 0, LIBSPECTRUM_JOYSTICK_KEMPSTON );
break;
case Z80_JOYSTICK_SINCLAIR_LEFT_V1:
libspectrum_snap_set_joystick_list( snap, 0, LIBSPECTRUM_JOYSTICK_SINCLAIR_2 );
break;
case Z80_JOYSTICK_SINCLAIR_RIGHT_V1:
libspectrum_snap_set_joystick_list( snap, 0, LIBSPECTRUM_JOYSTICK_SINCLAIR_1 );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"%s:get_joystick_type_v1: unknown v1 "
"joystick type %d",
__FILE__, type );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
get_joystick_type_v3( libspectrum_snap *snap,
const libspectrum_byte *custom_keys_base,
libspectrum_byte type )
{
switch( type ) {
case Z80_JOYSTICK_CURSOR_V3:
libspectrum_snap_set_joystick_list( snap, 0, LIBSPECTRUM_JOYSTICK_CURSOR );
break;
case Z80_JOYSTICK_KEMPSTON_V3:
libspectrum_snap_set_joystick_list( snap, 0, LIBSPECTRUM_JOYSTICK_KEMPSTON );
break;
case Z80_JOYSTICK_USER_DEFINED_V3:
/* User defined mapping, check to see if keys match Sinclair Interface 2
left port and return that if possible */
/* First 10 bytes are spectrum keyboard mappings for user defined keys
in this order: left, right, down, up, fire */
if( custom_keys_base[0] + custom_keys_base[1] * 0x100 == if2_left_map_l &&
custom_keys_base[2] + custom_keys_base[3] * 0x100 == if2_left_map_r &&
custom_keys_base[4] + custom_keys_base[5] * 0x100 == if2_left_map_d &&
custom_keys_base[6] + custom_keys_base[7] * 0x100 == if2_left_map_u &&
custom_keys_base[8] + custom_keys_base[9] * 0x100 == if2_left_map_f ) {
libspectrum_snap_set_joystick_list( snap, 0,
LIBSPECTRUM_JOYSTICK_SINCLAIR_2 );
} else {
libspectrum_snap_set_joystick_active_count( snap, 0 );
libspectrum_snap_set_joystick_inputs( snap, 0, 0 );
}
break;
case Z80_JOYSTICK_SINCLAIR_RIGHT_V3:
libspectrum_snap_set_joystick_list( snap, 0,
LIBSPECTRUM_JOYSTICK_SINCLAIR_1 );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"%s:get_joystick_type_v3: unknown v3 joystick "
"type %d",
__FILE__, type );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
read_blocks( const libspectrum_byte *buffer, size_t buffer_length,
libspectrum_snap *snap, int version, int compressed )
{
const libspectrum_byte *end, *next_block;
end = buffer + buffer_length; next_block = buffer;
while( next_block < end ) {
libspectrum_error error;
error = read_block( next_block, snap, &next_block, end, version,
compressed );
/* If it looks like some .slt data, try and parse that. That should
then be the end of the file */
if( error == LIBSPECTRUM_ERROR_SLT ) {
error = read_slt( snap, &next_block, end );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
/* If we haven't reached the end, return with error */
if( next_block != end ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_blocks: .slt data does not end file" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* If we have reached the end, that's OK */
return LIBSPECTRUM_ERROR_NONE;
}
/* Return immediately with any other errors */
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
read_slt( libspectrum_snap *snap, const libspectrum_byte **next_block,
const libspectrum_byte *end )
{
size_t slt_length[256], offsets[256];
size_t whence = 0;
size_t screen_length = 0, screen_offset = 0;
int i;
/* Zero all lengths to imply `not present' */
for( i=0; i<256; i++ ) slt_length[i]=0;
while( 1 ) {
int type, level, length;
/* Check we've got enough data left */
if( *next_block + 8 > end ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_slt: out of data in directory" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
type = (*next_block)[0] + (*next_block)[1] * 0x100;
level = (*next_block)[2] + (*next_block)[3] * 0x100;
(*next_block) += 4;
length = libspectrum_read_dword( next_block );
/* If this ends the table, exit */
if( type == LIBSPECTRUM_SLT_TYPE_END ) break;
/* Reset the pointer back to the start of the block */
(*next_block) -= 8;
switch( type ) {
case LIBSPECTRUM_SLT_TYPE_LEVEL: /* Level data */
if( level >= 0x100 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_slt: unexpected level number %d",
level );
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Each level should appear once only */
if( slt_length[ level ] ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_slt: level %d is duplicated", level );
return LIBSPECTRUM_ERROR_CORRUPT;
}
offsets[ level ] = whence; slt_length[ level ] = length;
break;
case LIBSPECTRUM_SLT_TYPE_SCREEN: /* Loading screen */
/* Allow only one loading screen per .slt file */
if( screen_length != 0 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_slt: duplicated loading screen" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
libspectrum_snap_set_slt_screen_level( snap, level );
screen_length = length; screen_offset = whence;
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"read_slt: unknown data type %d", type );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
/* and move both pointers along to the next block */
*next_block += 8; whence += length;
}
/* Read in the data for each level */
for( i=0; i<256; i++ ) {
libspectrum_byte *buffer; size_t length;
if( slt_length[i] ) {
/* Check this data actually exists */
if( *next_block + offsets[i] + slt_length[i] > end ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_slt: out of data reading level %d", i );
return LIBSPECTRUM_ERROR_CORRUPT;
}
length = 0; /* Tell uncompress_block to allocate memory for us */
uncompress_block( &buffer, &length, *next_block + offsets[i], slt_length[i] );
libspectrum_snap_set_slt( snap, i, buffer );
libspectrum_snap_set_slt_length( snap, i, length );
}
}
/* And the screen data */
if( screen_length ) {
libspectrum_byte *buffer;
/* Should expand to 6912 bytes, so give me a buffer that long */
buffer = libspectrum_new( libspectrum_byte, 6912 );
if( screen_length == 6912 ) { /* Not compressed */
memcpy( buffer, (*next_block) + screen_offset, 6912 );
} else { /* Compressed */
uncompress_block( &buffer, &screen_length, (*next_block) + screen_offset,
screen_length );
/* A screen should be 6912 bytes long */
if( screen_length != 6912 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_slt: screen is not 6912 bytes long" );
libspectrum_free( buffer );
return LIBSPECTRUM_ERROR_CORRUPT;
}
}
libspectrum_snap_set_slt_screen( snap, buffer );
}
/* Move past the data */
*next_block += whence;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
read_block( const libspectrum_byte *buffer, libspectrum_snap *snap,
const libspectrum_byte **next_block, const libspectrum_byte *end,
int version, int compressed )
{
libspectrum_error error;
libspectrum_byte *uncompressed;
int capabilities =
libspectrum_machine_capabilities( libspectrum_snap_machine( snap ) );
if( version == 1 ) {
error = read_v1_block( buffer, compressed, &uncompressed, next_block,
end );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
libspectrum_split_to_48k_pages( snap, uncompressed );
libspectrum_free( uncompressed );
} else {
size_t length;
int page;
error = read_v2_block( buffer, &uncompressed, &length, &page, next_block,
end );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
if( page <= 0 || page > 18 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"read_block: unknown page %d", page );
libspectrum_free( uncompressed );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
/* If it is an Interface 1 ROM page put it in the appropriate structure */
if( page == 1 && libspectrum_snap_interface1_active( snap ) ) {
libspectrum_byte *chunk = libspectrum_new( libspectrum_byte, 0x4000 );
memcpy( chunk, uncompressed, 0x4000 );
libspectrum_snap_set_interface1_custom_rom( snap, 1 );
libspectrum_snap_set_interface1_rom( snap, 0, chunk );
libspectrum_snap_set_interface1_rom_length( snap, 0, 0x4000 );
libspectrum_free( uncompressed );
return LIBSPECTRUM_ERROR_NONE;
}
/* If it is a +D ROM/RAM page put it in the appropriate structures */
if( page == 1 && libspectrum_snap_plusd_active( snap ) ) {
/* Bottom 8K of page is +D ROM, upper 8K is +D RAM */
libspectrum_byte *chunk = libspectrum_new( libspectrum_byte, 0x2000 );
memcpy( chunk, uncompressed, 0x2000 );
libspectrum_snap_set_plusd_rom( snap, 0, chunk );
chunk = libspectrum_new( libspectrum_byte, 0x2000 );
memcpy( chunk, uncompressed + 0x2000, 0x2000 );
libspectrum_snap_set_plusd_ram( snap, 0, chunk );
libspectrum_free( uncompressed );
return LIBSPECTRUM_ERROR_NONE;
}
/* If it's a ROM page, just throw it away */
if( page < 3 ) {
libspectrum_free( uncompressed );
return LIBSPECTRUM_ERROR_NONE;
}
/* Page 11 is the Multiface ROM unless we're emulating something
Scorpion-like */
if( page == 11 &&
!( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY ) ) {
libspectrum_free( uncompressed );
return LIBSPECTRUM_ERROR_NONE;
}
/* Deal with 48K snaps -- first, throw away page 3, as it's a ROM.
Then remap the numbers slightly */
if( !( capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY ) ) {
switch( page ) {
case 3:
libspectrum_free( uncompressed );
return LIBSPECTRUM_ERROR_NONE;
case 4:
page=5; break;
case 5:
page=3; break;
}
}
/* Now map onto RAM page numbers */
page -= 3;
if( libspectrum_snap_pages( snap, page ) == NULL ) {
libspectrum_snap_set_pages( snap, page, uncompressed );
} else {
libspectrum_free( uncompressed );
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_block: page %d duplicated", page );
return LIBSPECTRUM_ERROR_CORRUPT;
}
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
read_v1_block( const libspectrum_byte *buffer, int is_compressed,
libspectrum_byte **uncompressed,
const libspectrum_byte **next_block,
const libspectrum_byte *end )
{
if( is_compressed ) {
const libspectrum_byte *ptr;
int state;
size_t uncompressed_length = 0;
state = 0; ptr = buffer;
while( state != 4 ) {
if( ptr == end ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"read_v1_block: end marker not found" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
switch( state ) {
case 0:
switch( *ptr++ ) {
case 0x00: state = 1; break;
default: state = 0; break;
}
break;
case 1:
switch( *ptr++ ) {
case 0x00: state = 1; break;
case 0xed: state = 2; break;
default: state = 0; break;