-
Notifications
You must be signed in to change notification settings - Fork 1
/
zx_filetyp_z80.c
731 lines (651 loc) · 17.1 KB
/
zx_filetyp_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
// File : zx_filetyp_z80.c
// Datum : 27.01.2014
// Version : 1.0
// Autor : UB
// EMail : mc-4u(@)t-online.de
// Web : www.mikrocontroller-4u.de
// CPU : STM32F429
// IDE : CooCox CoIDE 1.7.4
// GCC : 4.7 2012q4
// Module : keine
// Funktion : Handle von ZX-Spectrum Files im Format "*.Z80"
//
// Hinweis : Die Entpack-Funktion prueft NICHT das Fileformat
// beim falschem Format event. Systemabsturz !!
// Translation : makapuf
//--------------------------------------------------------------
// Includes
//--------------------------------------------------------------
#include "zx_filetyp_z80.h"
//-------------------------------------------------------------
extern uint8_t out_ram;
//--------------------------------------------------------------
// interne Funktionen
//--------------------------------------------------------------
const uint8_t* p_decompFlashBlock(const uint8_t *block_adr);
uint8_t* p_decompRamBlock(uint8_t *block_adr);
uint8_t p_cntEqualBytes(uint8_t wert, uint32_t adr);
//--------------------------------------------------------------
// Unpack data from a file ( type = * .Z80 ) from flash
// And copy them to the memory of the ZX - Spectrum
//
// Data = pointer to the start of data
// Length = number of bytes
//--------------------------------------------------------------
void ZX_ReadFromFlash_Z80(Z80 *R, const uint8_t *data, uint16_t length)
{
const uint8_t *ptr;
const uint8_t *akt_block,*next_block;
uint8_t value1,value2;
uint8_t flag_version=0;
uint8_t flag_compressed=0;
uint16_t header_len;
uint16_t cur_addr;
if(length==0) return;
if(length>0xC020) return;
//----------------------------------
// parsing header
// Byte : [0...29]
//----------------------------------
// Set pointer to data beginning
ptr=data;
R->AF.B.h=*(ptr++); // A [0]
R->AF.B.l=*(ptr++); // F [1]
R->BC.B.l=*(ptr++); // C [2]
R->BC.B.h=*(ptr++); // B [3]
R->HL.B.l=*(ptr++); // L [4]
R->HL.B.h=*(ptr++); // H [5]
// PC [6+7]
value1=*(ptr++);
value2=*(ptr++);
R->PC.W=(value2<<8)|value1;
if(R->PC.W==0x0000) {
flag_version=1;
}
else {
flag_version=0;
}
// SP [8+9]
value1=*(ptr++);
value2=*(ptr++);
R->SP.W=(value2<<8)|value1;
R->I=*(ptr++); // I [10]
R->R=*(ptr++); // R [11]
// Comressed-Flag & Border [12]
value1=*(ptr++);
value2=((value1&0x0E)>>1);
OutZ80(0xFE, value2); // BorderColor
if((value1&0x20)!=0) {
flag_compressed=1;
} else {
flag_compressed=0;
}
R->DE.B.l=*(ptr++); // E [13]
R->DE.B.h=*(ptr++); // D [14]
R->BC1.B.l=*(ptr++); // C1 [15]
R->BC1.B.h=*(ptr++); // B1 [16]
R->DE1.B.l=*(ptr++); // E1 [17]
R->DE1.B.h=*(ptr++); // D1 [18]
R->HL1.B.l=*(ptr++); // L1 [19]
R->HL1.B.h=*(ptr++); // H1 [20]
R->AF1.B.h=*(ptr++); // A1 [21]
R->AF1.B.l=*(ptr++); // F1 [22]
R->IY.B.l=*(ptr++); // Y [23]
R->IY.B.h=*(ptr++); // I [24]
R->IX.B.l=*(ptr++); // X [25]
R->IX.B.h=*(ptr++); // I [26]
// Interrupt-Flag [27]
value1=*(ptr++);
if(value1!=0) {
// EI
R->IFF|=IFF_2|IFF_EI;
}
else {
// DI
R->IFF&=~(IFF_1|IFF_2|IFF_EI);
}
value1=*(ptr++); // nc [28]
// Interrupt-Mode [29]
value1=*(ptr++);
if((value1&0x01)!=0) {
R->IFF|=IFF_IM1;
}
else {
R->IFF&=~IFF_IM1;
}
if((value1&0x02)!=0) {
R->IFF|=IFF_IM2;
}
else {
R->IFF&=~IFF_IM2;
}
// restliche Register
R->ICount = R->IPeriod;
R->IRequest = INT_NONE;
R->IBackup = 0;
//----------------------------------
// save the data in RAM
// Byte : [30...n]
//----------------------------------
cur_addr=0x4000; // RAM start
if(flag_version==0) {
//-----------------------
// old Version
//-----------------------
if(flag_compressed==1) {
//-----------------------
// compressed
//-----------------------
while(ptr<(data+length)) {
value1=*(ptr++);
if(value1!=0xED) {
WrZ80(cur_addr++, value1);
}
else {
value2=*(ptr++);
if(value2!=0xED) {
WrZ80(cur_addr++, value1);
WrZ80(cur_addr++, value2);
}
else {
value1=*(ptr++);
value2=*(ptr++);
while(value1--) {
WrZ80(cur_addr++, value2);
}
}
}
}
}
else {
//-----------------------
// raw (uncompressed)
//-----------------------
while(ptr<(data+length)) {
value1=*(ptr++);
WrZ80(cur_addr++, value1);
}
}
}
else {
//-----------------------
// new Version
//-----------------------
// Header Laenge [30+31]
value1=*(ptr++);
value2=*(ptr++);
header_len=(value2<<8)|value1;
akt_block=(uint8_t*)(ptr+header_len);
// PC [32+33]
value1=*(ptr++);
value2=*(ptr++);
R->PC.W=(value2<<8)|value1;
//------------------------
// 1st block parsing
//------------------------
next_block=p_decompFlashBlock(akt_block);
//------------------------
// all other parsing
//------------------------
while(next_block<data+length) {
akt_block=next_block;
next_block=p_decompFlashBlock(akt_block);
}
}
}
//--------------------------------------------------------------
// Internal function
// Unpack and store a block of data
// ( From flash ) the new version
//--------------------------------------------------------------
const uint8_t* p_decompFlashBlock(const uint8_t *block_adr)
{
const uint8_t *ptr;
const uint8_t *next_block;
uint8_t value1,value2;
uint16_t block_len;
uint8_t flag_compressed=0;
uint8_t flag_page=0;
uint16_t cur_addr=0;
// pointer auf Blockanfang setzen
ptr=block_adr;
// Laenge vom Block
value1=*(ptr++);
value2=*(ptr++);
block_len=(value2<<8)|value1;
if(block_len==0xFFFF) {
block_len=0x4000;
flag_compressed=0;
}
else {
flag_compressed=1;
}
// Page vom Block
flag_page=*(ptr++);
// next Block ausrechnen
next_block=(uint8_t*)(ptr+block_len);
// Startadresse setzen
if(flag_page==4) cur_addr=0x8000;
if(flag_page==5) cur_addr=0xC000;
if(flag_page==8) cur_addr=0x4000;
if(flag_compressed==1) {
//-----------------------
// komprimiert
//-----------------------
while(ptr<(block_adr+3+block_len)) {
value1=*(ptr++);
if(value1!=0xED) {
WrZ80(cur_addr++, value1);
}
else {
value2=*(ptr++);
if(value2!=0xED) {
WrZ80(cur_addr++, value1);
WrZ80(cur_addr++, value2);
}
else {
value1=*(ptr++);
value2=*(ptr++);
while(value1--) {
WrZ80(cur_addr++, value2);
}
}
}
}
}
else {
//-----------------------
// nicht komprimiert
//-----------------------
while(ptr<(block_adr+3+block_len)) {
value1=*(ptr++);
WrZ80(cur_addr++, value1);
}
}
return(next_block);
}
//--------------------------------------------------------------
// Unpack data from a file ( type = * .Z80 ) from the transfer RAM
// And copy them to the memory of the ZX - Spectrum
//
// Data = pointer to the start of data
// Length = number of bytes
//--------------------------------------------------------------
void ZX_ReadFromTransfer_Z80(Z80 *R, uint8_t *data, uint16_t length)
{
uint8_t *ptr;
uint8_t *akt_block,*next_block;
uint8_t value1,value2;
uint8_t flag_version=0;
uint8_t flag_compressed=0;
uint16_t header_len;
uint16_t cur_addr;
if(length==0) return;
if(length>0xC020) return;
//----------------------------------
// Auswertung vom Header
// Byte : [0...29]
//----------------------------------
// pointer auf Datenanfang setzen
ptr=data;
R->AF.B.h=*(ptr++); // A [0]
R->AF.B.l=*(ptr++); // F [1]
R->BC.B.l=*(ptr++); // C [2]
R->BC.B.h=*(ptr++); // B [3]
R->HL.B.l=*(ptr++); // L [4]
R->HL.B.h=*(ptr++); // H [5]
// PC [6+7]
value1=*(ptr++);
value2=*(ptr++);
R->PC.W=(value2<<8)|value1;
if(R->PC.W==0x0000) {
flag_version=1;
}
else {
flag_version=0;
}
// SP [8+9]
value1=*(ptr++);
value2=*(ptr++);
R->SP.W=(value2<<8)|value1;
R->I=*(ptr++); // I [10]
R->R=*(ptr++); // R [11]
// Comressed-Flag und Border [12]
value1=*(ptr++);
value2=((value1&0x0E)>>1);
OutZ80(0xFE, value2); // BorderColor
if((value1&0x20)!=0) {
flag_compressed=1;
}
else {
flag_compressed=0;
}
R->DE.B.l=*(ptr++); // E [13]
R->DE.B.h=*(ptr++); // D [14]
R->BC1.B.l=*(ptr++); // C1 [15]
R->BC1.B.h=*(ptr++); // B1 [16]
R->DE1.B.l=*(ptr++); // E1 [17]
R->DE1.B.h=*(ptr++); // D1 [18]
R->HL1.B.l=*(ptr++); // L1 [19]
R->HL1.B.h=*(ptr++); // H1 [20]
R->AF1.B.h=*(ptr++); // A1 [21]
R->AF1.B.l=*(ptr++); // F1 [22]
R->IY.B.l=*(ptr++); // Y [23]
R->IY.B.h=*(ptr++); // I [24]
R->IX.B.l=*(ptr++); // X [25]
R->IX.B.h=*(ptr++); // I [26]
// Interrupt-Flag [27]
value1=*(ptr++);
if(value1!=0) {
// EI
R->IFF|=IFF_2|IFF_EI;
}
else {
// DI
R->IFF&=~(IFF_1|IFF_2|IFF_EI);
}
value1=*(ptr++); // nc [28]
// Interrupt-Mode [29]
value1=*(ptr++);
if((value1&0x01)!=0) {
R->IFF|=IFF_IM1;
}
else {
R->IFF&=~IFF_IM1;
}
if((value1&0x02)!=0) {
R->IFF|=IFF_IM2;
}
else {
R->IFF&=~IFF_IM2;
}
// restliche Register
R->ICount = R->IPeriod;
R->IRequest = INT_NONE;
R->IBackup = 0;
//----------------------------------
// speichern der Daten im RAM
// Byte : [30...n]
//----------------------------------
cur_addr=0x4000; // start vom RAM
if(flag_version==0) {
//-----------------------
// alte Version
//-----------------------
if(flag_compressed==1) {
//-----------------------
// komprimiert
//-----------------------
while(ptr<(data+length)) {
value1=*(ptr++);
if(value1!=0xED) {
WrZ80(cur_addr++, value1);
}
else {
value2=*(ptr++);
if(value2!=0xED) {
WrZ80(cur_addr++, value1);
WrZ80(cur_addr++, value2);
}
else {
value1=*(ptr++);
value2=*(ptr++);
while(value1--) {
WrZ80(cur_addr++, value2);
}
}
}
}
}
else {
//-----------------------
// nicht komprimiert
//-----------------------
while(ptr<(data+length)) {
value1=*(ptr++);
WrZ80(cur_addr++, value1);
}
}
}
else {
//-----------------------
// neue Version
//-----------------------
// Header Laenge [30+31]
value1=*(ptr++);
value2=*(ptr++);
header_len=(value2<<8)|value1;
akt_block=(uint8_t*)(ptr+header_len);
// PC [32+33]
value1=*(ptr++);
value2=*(ptr++);
R->PC.W=(value2<<8)|value1;
//------------------------
// ersten block auswerten
//------------------------
next_block=p_decompRamBlock(akt_block);
//------------------------
// alle anderen auswerten
//------------------------
while(next_block<data+length) {
akt_block=next_block;
next_block=p_decompRamBlock(akt_block);
}
}
}
//--------------------------------------------------------------
// Internal function
// Unpack and store a block of data
// ( From transfer - RAM ) of the new version
//--------------------------------------------------------------
uint8_t* p_decompRamBlock(uint8_t *block_adr)
{
uint8_t *ptr;
uint8_t *next_block;
uint8_t value1,value2;
uint16_t block_len;
uint8_t flag_compressed=0;
uint8_t flag_page=0;
uint16_t cur_addr=0;
// pointer auf Blockanfang setzen
ptr=block_adr;
// Laenge vom Block
value1=*(ptr++);
value2=*(ptr++);
block_len=(value2<<8)|value1;
if(block_len==0xFFFF) {
block_len=0x4000;
flag_compressed=0;
}
else {
flag_compressed=1;
}
// Page vom Block
flag_page=*(ptr++);
// next Block ausrechnen
next_block=(uint8_t*)(ptr+block_len);
// Startadresse setzen
if(flag_page==4) cur_addr=0x8000;
if(flag_page==5) cur_addr=0xC000;
if(flag_page==8) cur_addr=0x4000;
if(flag_compressed==1) {
//-----------------------
// compressed
//-----------------------
while(ptr<(block_adr+3+block_len)) {
value1=*(ptr++);
if(value1!=0xED) {
WrZ80(cur_addr++, value1);
}
else {
value2=*(ptr++);
if(value2!=0xED) {
WrZ80(cur_addr++, value1);
WrZ80(cur_addr++, value2);
}
else {
value1=*(ptr++);
value2=*(ptr++);
while(value1--) {
WrZ80(cur_addr++, value2);
}
}
}
}
}
else {
//-----------------------
// uncompressed
//-----------------------
while(ptr<(block_adr+3+block_len)) {
value1=*(ptr++);
WrZ80(cur_addr++, value1);
}
}
return(next_block);
}
//--------------------------------------------------------------
// Read data from the memory by the ZX - Spectrum , and pack
// Copy the file type ( type = * .Z80 ) in a transfer buffer
//
// Buffer_start = pointer to the start of the transfer buffer
// Return_wert = number of bytes in transfer buffer
//
// Note : Z80 version = old version , compression = ON
//--------------------------------------------------------------
uint16_t ZX_WriteToTransfer_Z80(Z80 *R, uint8_t *buffer_start)
{
uint16_t ret_wert=0;
uint8_t *ptr;
uint8_t wert,anz;
uint32_t cur_addr; // muss 32bit sein !!
//----------------------------------
// Schreiben vom Header
// Byte : [0...29]
//----------------------------------
// Startadresse setzen
ptr=buffer_start;
*(ptr++)=R->AF.B.h; // A [0]
*(ptr++)=R->AF.B.l; // F [1]
*(ptr++)=R->BC.B.l; // C [2]
*(ptr++)=R->BC.B.h; // B [3]
*(ptr++)=R->HL.B.l; // L [4]
*(ptr++)=R->HL.B.h; // H [5]
// PC [6+7] => fuer Version=0
*(ptr++)=R->PC.B.l;
*(ptr++)=R->PC.B.h;
// SP [8+9]
*(ptr++)=R->SP.B.l;
*(ptr++)=R->SP.B.h;
*(ptr++)=R->I; // I [10]
*(ptr++)=R->R; // R [11]
// Comressed-Flag und Border [12]
wert=((out_ram&0x07)<<1);
wert|=0x20; // fuer Compressed=1
*(ptr++)=wert;
*(ptr++)=R->DE.B.l; // E [13]
*(ptr++)=R->DE.B.h; // D [14]
*(ptr++)=R->BC1.B.l; // C1 [15]
*(ptr++)=R->BC1.B.h; // B1 [16]
*(ptr++)=R->DE1.B.l; // E1 [17]
*(ptr++)=R->DE1.B.h; // D1 [18]
*(ptr++)=R->HL1.B.l; // L1 [19]
*(ptr++)=R->HL1.B.h; // H1 [20]
*(ptr++)=R->AF1.B.h; // A1 [21]
*(ptr++)=R->AF1.B.l; // F1 [22]
*(ptr++)=R->IY.B.l; // Y [23]
*(ptr++)=R->IY.B.h; // I [24]
*(ptr++)=R->IX.B.l; // X [25]
*(ptr++)=R->IX.B.h; // I [26]
// Interrupt-Flag [27]
if((R->IFF&IFF_EI)!=0) {
// EI
wert=0x01;
}
else {
// DI
wert=0x00;
}
*(ptr++)=wert;
*(ptr++)=0x00; // nc [28]
// Interrupt-Mode [29]
wert=0x00;
if((R->IFF&IFF_IM1)!=0) wert|=01;
if((R->IFF&IFF_IM2)!=0) wert|=02;
*(ptr++)=wert;
//----------------------------------
// schreiben der Daten vom RAM
// Byte : [30...n]
//----------------------------------
// Startadresse setzen
cur_addr=0x4000; // start vom RAM
//-----------------------
// Old version, compressed //
//-----------------------
do {
wert=RdZ80(cur_addr);
if(wert==0xED) {
anz=p_cntEqualBytes(wert,cur_addr);
if(anz>=2) {
*(ptr++)=0xED;
*(ptr++)=0xED;
*(ptr++)=anz;
*(ptr++)=wert;
cur_addr+=anz;
}
else {
*(ptr++)=wert;
cur_addr++;
if(cur_addr<=0xFFFF) {
wert=RdZ80(cur_addr);
*(ptr++)=wert;
cur_addr++;
}
}
}
else {
anz=p_cntEqualBytes(wert,cur_addr);
if(anz>=5) {
*(ptr++)=0xED;
*(ptr++)=0xED;
*(ptr++)=anz;
*(ptr++)=wert;
cur_addr+=anz;
}
else {
*(ptr++)=wert;
cur_addr++;
}
}
}while(cur_addr<=0xFFFF);
// Save end identifier
*(ptr++)=0x00;
*(ptr++)=0xED;
*(ptr++)=0xED;
*(ptr++)=0x00;
// Number of bytes to calculate ( header + data )
ret_wert=ptr-buffer_start;
return(ret_wert);
}
//--------------------------------------------------------------
// Internal Function
// Determine the number of bytes equal
//--------------------------------------------------------------
uint8_t p_cntEqualBytes(uint8_t wert, uint32_t adr)
{
uint8_t ret_wert=1;
uint8_t n,test;
// max 255 gleiche Werte
for(n=0;n<254;n++) {
adr++;
if(adr>=0xFFFF) break;
test=RdZ80(adr);
if(test==wert) {
ret_wert++;
}
else {
break;
}
}
return(ret_wert);
}