-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSongRipper.pas
1387 lines (1279 loc) · 40 KB
/
SongRipper.pas
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
unit SongRipper;
interface
uses Classes;
type
TSongDataType = (ERROR,TheMusicBox,Phaser1Digital,Phaser1Synth,SFX,P1D,P1S,TMB,MSD,SVG);
TSongData = record
SongLoc: Word;
DataType: TSongDataType;
end;
type
TSongRipper = class(TObject)
private
FFileName: string;
FSongs: array of TSongData;
Mem: array [0..65535] of byte;
function GetSongCount: integer;
function GetSongDataType(i: integer): TSongDataType;
function GetSongDataTypeString(i: integer): string;
function GetSongLocation(i: integer): integer;
function ScanTheMusicBox: integer;
procedure RipTheMusicBox(iLoc: integer; iPatLen: integer);
function ScanPhaser1DLinear: integer;
function ScanPhaser1SLinear: integer;
procedure RipPhaser1Linear(iLoc, iPatLen: integer);
function GetPhaser1MinRender(i: integer): integer;
procedure ReadZ80V1Snap(F: TFileStream);
procedure ReadZ80V2orV3Snap(F: TFileStream);
function ScanP1D: integer;
function ScanP1S: integer;
procedure RipPhaser1Patterns(iLoc, iPatLen: integer);
function GetSongPatternNum(wAddr: word; dwLayout: array of Word): byte;
public
function Scan(): integer;
function ConvertSong(iSong: integer; DefPatternLen: byte = 64): integer;
function LoadTAP(sFileName: string): integer;
function LoadZ80(sFileName: string): integer;
function LoadSNA(sFileName: string): integer;
property SongCount: integer read GetSongCount;
property SongLocation[i: integer]: integer read GetSongLocation;
property SongDataType[i: integer]: TSongDataType read GetSongDataType;
property SongDataTypeString[i: integer]: string read GetSongDataTypeString;
property FileName: string read FFileName write FFileName;
constructor Create;
end;
implementation
{ TSongRipper }
uses SysUtils, Forms, Windows, frmSTMainWnd;
procedure TSongRipper.RipTheMusicBox(iLoc: integer; iPatLen: integer);
var
i, j, iPat, iRow, iTempo: integer;
cNote1, cNote2: byte;
begin
if (Mem[iLoc] <> 33) or (Mem[iLoc + 6] <> 33) then
exit;
iTempo := Mem[iLoc + 35];
iTempo := (iTempo - 210) div 2;
if iTempo < 1 then iTempo := 1;
if iTempo > 20 then iTempo := 20;
i := Mem[iLoc+1] + 256 * Mem[iLoc+2]+1; // Ch1 data
j := Mem[iLoc+7] + 256 * Mem[iLoc+8]+1; // Ch2 data
iPat := 0; iRow := 1;
while (Mem[i] <> $40) and (Mem[j] <> $40) and (iPat < 126) do
begin
STMainWnd.Song.SongLayout[iPat] := iPat;
STMainWnd.Song.Pattern[iPat].Length := iPatLen;
STMainWnd.Song.Pattern[iPat].Tempo := iTempo;
cNote1 := Mem[i] + 12;
cNote2 := Mem[j] + 12;
if cNote1 = $35 then cNote1 := $FF;
if cNote2 = $35 then cNote2 := $FF;
if (Mem[i] < $40) or (Mem[i] >= $F4) then
begin
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := cNote1;
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := cNote2;
end
else if (Mem[i] = $B4) then
begin
// Kick Drum
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := 255;
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := 255;
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $89;
end
else if (Mem[i] >= $E4) and (Mem[i] <= $F3) then
begin
// White Noise (Custom Drum)
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := 255;
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := 255;
STMainWnd.Song.Pattern[iPat].Sustain[2][iRow] := Mem[i] - 228;
case Mem[j] of
$01: // Wave1
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $81;
$03: // Wave2
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $82;
$07: // Wave3
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $83;
$0F: // Wave4
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $84;
$1F: // Wave5
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $85;
$3F: // Wave6
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $86;
$7F: // Wave7
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $87;
$FF: // Wave8
STMainWnd.Song.Pattern[iPat].Drum[iRow] := $88;
end;
end;
inc(iRow);
inc(i);
inc(j);
if iRow > iPatLen then
begin
inc(iPat);
iRow := 1;
end;
end;
STMainWnd.Song.Pattern[iPat].Length := iRow-1;
STMainWnd.Song.PreferredEngine := 'TMB';
STMainWnd.mnuTGTheMusicBoxClick(nil);
end;
function TSongRipper.GetPhaser1MinRender(i: integer): integer;
var
iLowest, iCurrent: integer;
begin
iLowest := 9999; iCurrent := 0;
while (Mem[i] <> $0) and (i < 65536) do
begin
if (Mem[i] = $BD) then
inc(i)
else if Mem[i] >= $80 then
begin
if (iCurrent < iLowest) and (iCurrent > 0) then
iLowest := iCurrent;
iCurrent := 0;
end
else if (Mem[i] >= $76) and (Mem[i] <= $7F) then
inc(iCurrent)
else if (Mem[i] < $76) then
inc(iCurrent,Mem[i]);
inc(i);
end;
Result := iLowest;
end;
procedure TSongRipper.RipPhaser1Linear(iLoc: integer; iPatLen: integer);
var
i, iPat, iRow, iInstCount, iTempo, iSkip: integer;
cNote1: byte;
begin
iInstCount := (Mem[iLoc] + 256 * Mem[iLoc+1]) div 4;
iRow := iLoc + 2;
for i := 0 to iInstCount - 1 do
begin
STMainWnd.Song.Phaser1Instrument[i].Multiple := Mem[iRow];
STMainWnd.Song.Phaser1Instrument[i].Detune := Mem[iRow+1] + 256 * Mem[iRow + 2];
STMainWnd.Song.Phaser1Instrument[i].Phase := Mem[iRow + 3];
inc(iRow,4);
end;
i := iLoc + iInstCount * 4 + 2;
iPat := 0; iRow := 1;
iTempo := GetPhaser1MinRender(i);
iSkip := 0;
while (Mem[i] <> $00) and (iPat < 126) do
begin
STMainWnd.Song.SongLayout[iPat] := iPat;
STMainWnd.Song.Pattern[iPat].Length := iPatLen;
STMainWnd.Song.Pattern[iPat].Tempo := 17 - iTempo;
if (Mem[i] = $BF) then
begin
// Loop Start Marker
if (iRow <> 1) then
begin
inc(iPat);
iRow := 1;
end;
STMainWnd.Song.LoopStart := iPat;
Inc(i);
end
else if (Mem[i] = $BD) then
begin
// Instrument Change
inc(i);
STMainWnd.Song.Pattern[iPat].Sustain[2][iRow] := Mem[i] div 2;
Inc(i);
end
else if ((Mem[i] and $80) = $80) then
begin
cNote1 := Mem[i] and $3F;
if cNote1 <= 6 then Inc(cNote1,101) else dec(cNote1,6);
if (Mem[i] and $3F) = 60 then
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := $82
else if (Mem[i] and $3F) = 62 then
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := 255
else
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := cNote1;
if ((Mem[i] and $40) = $40) then
STMainWnd.Song.Pattern[iPat].Sustain[1][iRow] := 1
else
STMainWnd.Song.Pattern[iPat].Sustain[1][iRow] := 255;
inc(i);
// Chan 2
cNote1 := Mem[i] and $3F;
if cNote1 <= 6 then Inc(cNote1,101) else dec(cNote1,6);
if ((Mem[i] and $80) = $80) and ((Mem[i] and $3F) = 60) then
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := $82
else if ((Mem[i] and $80) = $80) and ((Mem[i] and $3F) = 62) then
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := 255
else if ((Mem[i] and $80) = $80) then
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := cNote1;
if ((Mem[i] and $80) = $80) then inc(i);
end;
// Drum/skiplines
if (Mem[i] >= $76) and (Mem[i] <= $7E) then
begin
STMainWnd.Song.Pattern[iPat].Drum[iRow] := Mem[i] - $75 + $80;
inc(i);
iSkip := 1;
end;
if (Mem[i] <= $75) then
begin
inc(iSkip, Mem[i]);
iSkip := iSkip div iTempo;
inc(iRow,iSkip);
iSkip := 0;
inc(i);
while (iRow > iPatLen) do
begin
inc(iPat);
STMainWnd.Song.SongLayout[iPat] := iPat;
STMainWnd.Song.Pattern[iPat].Length := iPatLen;
STMainWnd.Song.Pattern[iPat].Tempo := 17 - iTempo;
Dec(iRow,iPatLen);
end;
end
else if (Mem[i] < $80) then
begin
iSkip := iSkip div iTempo;
inc(iRow,iSkip);
iSkip := 0;
end;
if iRow > iPatLen then
begin
inc(iPat);
iRow := 1;
end;
end;
STMainWnd.Song.Pattern[iPat].Length := iRow-1;
end;
function TSongRipper.GetSongPatternNum(wAddr: word; dwLayout: array of Word): byte;
var
i: integer;
begin
Result := 255;
for i := 0 to Length(dwLayout) - 1 do
begin
if dwLayout[i] = wAddr then
begin
Result := STMainWnd.Song.SongLayout[i];
break;
end;
end;
end;
procedure TSongRipper.RipPhaser1Patterns(iLoc: integer; iPatLen: integer);
var
i, iPat, iRow, iInstCount, iTempo, iSkip: integer;
cNote1: byte;
cSongLength, cNextPat: byte;
dwLayout: array of Word;
begin
STMainWnd.Song.LoopStart := Mem[iLoc] div 2;
cSongLength := Mem[iLoc+1] div 2;
inc(iLoc,2);
iInstCount := (Mem[iLoc] + 256 * Mem[iLoc+1]) div 4;
iRow := iLoc + 2;
for i := 0 to iInstCount - 1 do
begin
STMainWnd.Song.Phaser1Instrument[i].Multiple := Mem[iRow];
STMainWnd.Song.Phaser1Instrument[i].Detune := Mem[iRow+1] + 256 * Mem[iRow + 2];
STMainWnd.Song.Phaser1Instrument[i].Phase := Mem[iRow + 3];
inc(iRow,4);
end;
inc(iLoc, iInstCount * 4 + 2);
// Get Song Layout
SetLength(dwLayout,0);
cNextPat := 0;
for i := 0 to cSongLength - 1 do
begin
iRow := Mem[iLoc] + 256 * Mem[iLoc+1];
STMainWnd.Song.SongLayout[i] := GetSongPatternNum(iRow and $FFFF,dwLayout);
if STMainWnd.Song.SongLayout[i] = 255 then
begin
STMainWnd.Song.SongLayout[i] := cNextPat;
inc(cNextPat);
end;
SetLength(dwLayout,i+1);
dwLayout[i] := iRow and $FFFF;
inc(iLoc,2);
end;
for i := 0 to cSongLength - 1 do
begin
iPat := STMainWnd.Song.SongLayout[i];
iLoc := dwLayout[i];
iTempo := GetPhaser1MinRender(iLoc);
STMainWnd.Song.Pattern[iPat].Tempo := 17 - iTempo;
iSkip := 0;
iRow := 1;
while (Mem[iLoc] <> $00) and (iRow < 126) do
begin
if (Mem[iLoc] = $BF) then
Inc(iLoc)
else if (Mem[iLoc] = $BD) then
begin
// Instrument Change
inc(iLoc);
STMainWnd.Song.Pattern[iPat].Sustain[2][iRow] := Mem[iLoc] div 2;
Inc(iLoc);
end
else if ((Mem[iLoc] and $80) = $80) then
begin
cNote1 := Mem[iLoc] and $3F;
if cNote1 <= 6 then Inc(cNote1,101) else dec(cNote1,6);
if (Mem[iLoc] and $3F) = 60 then
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := $82
else if (Mem[iLoc] and $3F) = 62 then
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := 255
else
STMainWnd.Song.Pattern[iPat].Chan[2][iRow] := cNote1;
if ((Mem[iLoc] and $40) = $40) then
STMainWnd.Song.Pattern[iPat].Sustain[1][iRow] := 1
else
STMainWnd.Song.Pattern[iPat].Sustain[1][iRow] := 255;
inc(iLoc);
// Chan 2
cNote1 := Mem[iLoc] and $3F;
if cNote1 <= 6 then Inc(cNote1,101) else dec(cNote1,6);
if ((Mem[iLoc] and $80) = $80) and ((Mem[iLoc] and $3F) = 60) then
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := $82
else if ((Mem[iLoc] and $80) = $80) and ((Mem[iLoc] and $3F) = 62) then
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := 255
else if ((Mem[iLoc] and $80) = $80) then
STMainWnd.Song.Pattern[iPat].Chan[1][iRow] := cNote1;
if ((Mem[iLoc] and $80) = $80) then inc(iLoc);
end;
// Drum/skiplines
if (Mem[iLoc] >= $76) and (Mem[iLoc] <= $7E) then
begin
STMainWnd.Song.Pattern[iPat].Drum[iRow] := Mem[iLoc] - $75 + $80;
inc(iLoc);
iSkip := 1;
end;
if (Mem[iLoc] <= $75) then
begin
inc(iSkip, Mem[iLoc]);
iSkip := iSkip div iTempo;
inc(iRow,iSkip);
iSkip := 0;
inc(iLoc);
end
else if (Mem[iLoc] < $80) then
begin
iSkip := iSkip div iTempo;
inc(iRow,iSkip);
iSkip := 0;
end;
end;
STMainWnd.Song.Pattern[iPat].Length := iRow-1;
end;
end;
function TSongRipper.ConvertSong(iSong: integer; DefPatternLen: byte): integer;
begin
if (iSong < 0) or (iSong >= Length(FSongs)) then
Result := -1
else
begin
// Rip Song #iSong
STMainWnd.Song.Clear;
case FSongs[iSong].DataType of
TheMusicBox:
RipTheMusicBox(FSongs[iSong].SongLoc, DefPatternLen);
Phaser1Digital:
begin
RipPhaser1Linear(FSongs[iSong].SongLoc, DefPatternLen);
STMainWnd.Song.PreferredEngine := 'P1D';
STMainWnd.mnuTGPhaser1DigitalClick(nil);
end;
Phaser1Synth:
begin
RipPhaser1Linear(FSongs[iSong].SongLoc, DefPatternLen);
STMainWnd.Song.PreferredEngine := 'P1S';
STMainWnd.mnuTGPhaser1DigitalClick(nil);
end;
P1D:
begin
RipPhaser1Patterns(FSongs[iSong].SongLoc, DefPatternLen);
STMainWnd.Song.PreferredEngine := 'P1D';
STMainWnd.mnuTGPhaser1DigitalClick(nil);
end;
P1S:
begin
RipPhaser1Patterns(FSongs[iSong].SongLoc, DefPatternLen);
STMainWnd.Song.PreferredEngine := 'P1S';
STMainWnd.mnuTGPhaser1DigitalClick(nil);
end;
//SFX,TMB,MSD,SVG);
end;
Result := 0;
end;
end;
constructor TSongRipper.Create;
begin
inherited;
end;
function TSongRipper.GetSongCount: integer;
begin
Result := Length(FSongs);
end;
function TSongRipper.GetSongDataType(i: integer): TSongDataType;
begin
if (i >= 0) and (i < Length(FSongs)) then
Result := FSongs[i].DataType
else
Result := ERROR;
end;
function TSongRipper.GetSongDataTypeString(i: integer): string;
var
DT: TSongDataType;
begin
if (i >= 0) and (i < Length(FSongs)) then
DT := FSongs[i].DataType
else
DT := ERROR;
case DT of
ERROR: Result := 'ERROR';
TheMusicBox: Result := 'The Music Box (original)' ;
Phaser1Digital: Result := 'Phaser1 Digital (linear)';
Phaser1Synth: Result := 'Phaser1 Synth (linear)';
SFX: Result := 'Special FX (Beepola)';
P1D: Result := 'Phaser1 Digital (Beepola)';
P1S: Result := 'Phaser1 Synth (Beepola)';
TMB: Result := 'The Music Box (Beepola)';
MSD: Result := 'Music Studio (Beepola)';
SVG: Result := 'Savage (Beepola)';
end;
end;
function TSongRipper.GetSongLocation(i: integer): integer;
begin
if (i < 0) or (i >= Length(FSongs)) then
Result := -1
else
begin
Result := FSongs[i].SongLoc;
end;
end;
function TSongRipper.LoadSNA(sFileName: string): integer;
begin
Result := -1;
end;
function TSongRipper.LoadTAP(sFileName: string): integer;
var
F: TFileStream;
wBlockLen: Word;
cBytes: array of byte;
c00Header: byte;
cBlockType: byte;
wCodeStart, wCodeLen: Word;
i: Integer;
begin
Result := -1; // Error
try
F := TFileStream.Create(sFileName,fmOpenRead or fmShareDenyNone);
while F.Position < F.Size do
begin
F.Read(wBlockLen,2);
SetLength(cBytes,wBlockLen);
F.Read(cBytes[0],wBlockLen);
c00Header := cBytes[0];
cBlockType := cBytes[1];
if c00Header = 00 then
begin
// This is a header block, get the properties of the following data block
// and either LOAD it into Mem[] if it's code, or skip it
if cBlockType = 3 then
begin
wCodeLen := cBytes[12] + 256 * cBytes[13];
wCodeStart := cBytes[14] + 256 * cBytes[15];
F.Read(wBlockLen,2);
if wBlockLen <> wCodeLen + 2 then
break;
SetLength(cBytes,wBlockLen);
F.Read(cBytes[0],wBlockLen);
if (cBytes[0] = $FF) then
begin
Result := 0; // OK - Some TAP data was loaded
// Stick the code block data into Mem
for i := 0 to wCodeLen - 1 do
Mem[i+wCodeStart] := cBytes[i+1];
end;
end
else
begin
// Skip next block
F.Read(wBlockLen,2);
SetLength(cBytes,wBlockLen);
F.Read(cBytes[0],wBlockLen);
end;
end;
end;
finally
FreeAndNil(F);
end;
end;
procedure TSongRipper.ReadZ80V1Snap(F: TFileStream);
var
Buf: array of byte;
iDataLen, iBlockLen, iCounter, iMemPos, iBlockCounter: integer;
begin
iDataLen := F.Size - F.Position;
SetLength(Buf,iDataLen);
// read the compressed data into Buf
F.Read(Buf[0],iDataLen);
// Uncompress the block to memory
iCounter := 1;
iMemPos := 16384;
repeat
if Buf[iCounter] = $ED then
begin
if Buf[iCounter + 1] = $ED then
begin
// This is an encoded block
inc(iCounter,2);
iBlockLen := Buf[iCounter];
inc(iCounter);
for iBlockCounter := 1 to iBlockLen do
begin
Mem[iMemPos] := Buf[iCounter];
inc(iMemPos);
end;
end
else
begin
// Just a single ED, write it out
Mem[iMemPos] := $ED;
inc(iMemPos);
end;
end
else
begin
Mem[iMemPos] := Buf[iCounter];
inc(iMemPos);
end;
inc(iCounter);
until iCounter > iDataLen - 5;
if (Buf[iCounter] <> 0) or (Buf[iCounter+1] <> $ED) or
(Buf[iCounter+2] <> $ED) or (Buf[iCounter+3] <> 0) then
Application.MessageBox('Error in compressed Z80 file. Block end marker 0x00EDED00 is not present.',
PAnsiChar(Application.Title),MB_ICONEXCLAMATION);
end;
procedure TSongRipper.ReadZ80V2orV3Snap(F: TFileStream);
var
iHeaderLen, iCounter, iMemStart, iBlockCounter, iMemPos: integer;
Buf: array of byte;
b128KMap: boolean;
cOut7FFD, cTop16KPage: byte;
begin
SetLength(Buf,32768);
F.Read(Buf[0],2);
iHeaderLen := Buf[0] + 256 * Buf[1];
F.Read(Buf[0],iHeaderLen);
b128KMap := false;
cOut7FFD := Buf[3] and 7;
if (iHeaderLen = 23) then
begin
// V2
if (Buf[2] = 3) or (Buf[2] = 4) or (Buf[2] = 7) or (Buf[2] = 8) or
(Buf[2] = 9) or (Buf[2] = 10) or (Buf[2] = 11) or (Buf[2] = 12) or
(Buf[2] = 13) then
b128KMap := true;
end
else if (iHeaderLen = 54) or (iHeaderLen = 55) then
begin
// V3
if (Buf[2] = 4) or (Buf[2] = 5) or (Buf[2] = 6) or (Buf[2] = 7) or
(Buf[2] = 8) or (Buf[2] = 9) or (Buf[2] = 10) or (Buf[2] = 11) or
(Buf[2] = 12) or (Buf[2] = 13) then
b128KMap := true;
end
else
begin
Application.MessageBox(PAnsiChar(
'The specified Z80 file contains an invalid extended header length of ' + IntToHex(iHeaderLen,4) + ' bytes'#13#10#13#10 +
'Only valid V1, V2 or V3 Z80 snapshots are supported.'),
(PAnsiChar(Application.Title)),
MB_ICONINFORMATION or MB_OK);
exit;
end;
if b128KMap then
cTop16KPage := (cOut7FFD and 7)+3 // 128/+2 memory page operation
else
cTop16KPage := 5; // In 48K snaps, always put Page 5 into top 16K
while F.Position < F.Size do
begin
// read a block
F.Read(Buf[0],3);
iHeaderLen := Buf[0] + 256 * Buf[1];
if b128KMap then
begin
case Buf[2] of
0: iMemPos := $0000; // 48K ROM
// ROMs and 128K pages - unused by 48K speccy
1,2,3,4,6,7,9,10,11:
if (Buf[2] = cTop16KPage) then iMemPos := $C000 else iMemPos := -1;
5: iMemPos := $8000; // Page 1 RAM at $8000
8: iMemPos := $4000; // Page 5 RAM at $4000
else
iMemPos := -1;
end;
end
else
begin
case Buf[2] of
0: iMemPos := $0000; // 48K ROM
5: iMemPos := $C000;
4: iMemPos := $8000; // Page 1 RAM at $8000
8: iMemPos := $4000; // Page 5 RAM at $4000
else
iMemPos := -1;
end;
end;
if (iHeaderLen = $FFFF) then
begin
// Uncompressed block, just read it into RAM
if iMemPos < 0 then
F.Read(Buf[0],16384)
else
F.Read(Mem[iMemPos],16384);
end
else
begin
// Uncompress the block to memory
SetLength(Buf,iHeaderLen);
F.Read(Buf[0],iHeaderLen);
iCounter := 0;
if iMemPos > -1 then
begin
iMemStart := iMemPos;
repeat
if (Buf[iCounter] = $ED) then
begin
if Buf[iCounter + 1] = $ED then
begin
// This is an encoded block
inc(iCounter,2);
iHeaderLen := Buf[iCounter];
inc(iCounter);
for iBlockCounter := 0 To iHeaderLen - 1 do
Mem[iMemPos + iBlockCounter] := Buf[iCounter];
inc(iMemPos, iHeaderLen);
end
else
begin
// Just a single ED, write it out
Mem[iMemPos] := $ED;
inc(iMemPos);
end;
end
else
begin
Mem[iMemPos] := Buf[iCounter];
inc(iMemPos);
end;
inc(iCounter);
until (iCounter >= Length(Buf));
if iMemPos < iMemStart + 16384 then
Application.MessageBox('Z80 block error','',0);
end;
end;
end;
end;
function TSongRipper.LoadZ80(sFileName: string): integer;
var
F: TFileStream;
Buf: array of byte;
regPC: word;
bCompressed: boolean;
begin
try
F := TFileStream.Create(sFileName,fmOpenRead or fmShareDenyNone);
while F.Position < F.Size do
begin
SetLength(Buf,65536);
F.Read(Buf[0],6);
F.Read(Buf[0],2);
regPC := Buf[0] + 256 * Buf[1];
F.Read(Buf[0],22);
if (Buf[4] and $20) = $20 then bCompressed := true else bCompressed := false;
if regPC = 0 then
begin
// Read V2 or V3 file
ReadZ80V2orV3Snap(F);
end
else
begin
// Read V1 file
if bCompressed then
begin
ReadZ80V1Snap(F);
end
else
F.Read(Mem[16384],49152);
end;
end;
finally
FreeAndNil(F);
end;
Result := 0;
end;
function TSongRipper.Scan: integer;
var
iRet: integer;
begin
SetLength(FSongs,0);
iRet := ScanTheMusicBox();
if iRet >= 0 then
begin
SetLength(FSongs,Length(FSongs)+1);
FSongs[Length(FSongs)-1].SongLoc := iRet;
FSongs[Length(FSongs)-1].DataType := TheMusicBox;
end;
iRet := ScanPhaser1DLinear();
if iRet >= 0 then
begin
SetLength(FSongs,Length(FSongs)+1);
FSongs[Length(FSongs)-1].SongLoc := iRet;
FSongs[Length(FSongs)-1].DataType := Phaser1Digital;
end;
iRet := ScanPhaser1SLinear();
if iRet >= 0 then
begin
SetLength(FSongs,Length(FSongs)+1);
FSongs[Length(FSongs)-1].SongLoc := iRet;
FSongs[Length(FSongs)-1].DataType := Phaser1Synth;
end;
iRet := ScanP1D();
if iRet >= 0 then
begin
SetLength(FSongs,Length(FSongs)+1);
FSongs[Length(FSongs)-1].SongLoc := iRet;
FSongs[Length(FSongs)-1].DataType := P1D;
end;
iRet := ScanP1S();
if iRet >= 0 then
begin
SetLength(FSongs,Length(FSongs)+1);
FSongs[Length(FSongs)-1].SongLoc := iRet;
FSongs[Length(FSongs)-1].DataType := P1S;
end;
Result := Length(FSongs);
end;
function TSongRipper.ScanTheMusicBox(): integer;
var
MatchBin: array [0..33] of word;
i,j: integer;
begin
MatchBin[0] := 94; // LD E,(HL)
MatchBin[1] := 35; // INC HL
MatchBin[2] := 86; // LD D,(HL)
MatchBin[3] := 19; // INC DE
MatchBin[4] := 26; // LD A,(DE)
MatchBin[5] := 254; // CP n
MatchBin[6] := 64; // $40
MatchBin[7] := 40; // JR Z,d
MatchBin[8] := 256; // CHANNEL_END
MatchBin[9] := 114; // LD (HL),D
MatchBin[10] := 43; // DEC HL
MatchBin[11] := 115; // LD (HL),E
MatchBin[12] := 201; // RET
MatchBin[13] := 126; // LD A,(HL) ; A = value of next note
MatchBin[14] := 198; // ADD A,n
MatchBin[15] := 12; // #0C ; Add 12 to it
MatchBin[16] := 95; // LD E,A ; store it in E
MatchBin[17] := 22; // LD D,n
MatchBin[18] := 0; // #00 ; LD D,0
MatchBin[19] := 33; // LD HL,nn
MatchBin[20] := 256; // #EB
MatchBin[21] := 256; // 34 ; LD HL with a pointer to a frequency table???
MatchBin[22] := 25; // ADD HL,DE ; ADD DE to it, to get value for our note
MatchBin[23] := 102; // LD H,(HL) ; LD H with value
MatchBin[24] := 46; // LD L,n
MatchBin[25] := 1; // 1
MatchBin[26] := 201; // RET
MatchBin[27] := 35; // INC HL
MatchBin[28] := 94; // LD E,(HL)
MatchBin[29] := 35; // INC HL
MatchBin[30] := 86; // LD D,(HL)
MatchBin[31] := 43; // DEC HL
MatchBin[32] := 43; // DEC HL
MatchBin[33] := 24; // JR d
for i := 0 to 65535 - Length(MatchBin) do
begin
Result := i;
for j := 0 to Length(MatchBin)-1 do
begin
if (MatchBin[j] < 256) and (Mem[i+j] <> MatchBin[j]) then
begin
Result := -1;
break;
end;
end;
if (Result > -1) then break;
end;
if Result > 100 then
begin
Result := Result - 36;
if Mem[Result] <> 33 then
Result := -1;
end;
end;
function TSongRipper.ScanPhaser1DLinear(): integer;
var
MatchBin: array [0..98] of word;
i,j: integer;
begin
MatchBin[0] := 33; // LD HL,nn
MatchBin[1] := 256; // n1
MatchBin[2] := 256; // n2
MatchBin[3] := 62; // LD A,n
MatchBin[4] := 256; // n
MatchBin[5] := 6; // LD B,n
MatchBin[6] := 256; // n
MatchBin[7] := 183; // OR A
MatchBin[8] := 14; // LD C,n
MatchBin[9] := 256; // n
MatchBin[10] := 32; // JR NZ,+2
MatchBin[11] := 02; // dis
MatchBin[12] := 14; // LD C,n
MatchBin[13] := 256; // n
MatchBin[14] := $79; // LD A,C
MatchBin[15] := 50; // LD (nn),A
MatchBin[16] := 256; // n1
MatchBin[17] := 256; // n2
MatchBin[18] := 120; // LD A,B
MatchBin[19] := 183; // OR A
MatchBin[20] := 1; // LD BC,nn
MatchBin[21] := 256; // n1
MatchBin[22] := 256; // n2
MatchBin[23] := 40; // JR Z,dis
MatchBin[24] := 256; // dis
MatchBin[25] := 1; // LD BC,exitplayer
MatchBin[26] := 256; // n1
MatchBin[27] := 256; // n2
MatchBin[28] := 237; // prefix ED
MatchBin[29] := 67; // LD (nn),BC
MatchBin[30] := 256; // n1
MatchBin[31] := 256; // n2
MatchBin[32] := 243; // DI
MatchBin[33] := 253; // prefix IY
MatchBin[34] := 229; // PUSH IY
MatchBin[35] := 94; // LD E,(HL)
MatchBin[36] := 35; // INC HL
MatchBin[37] := 86; // LD D,(HL)
MatchBin[38] := 35; // INC HL
MatchBin[39] := 34; // LD (nn),HL
MatchBin[40] := 256; // INSTRUM_TBL_1
MatchBin[41] := 256; // INSTRUM_TBL_2
MatchBin[42] := 34; // LD (nn),HL
MatchBin[43] := 256; // CURRENT_INST_1
MatchBin[44] := 256; // CURRENT_INST_2
MatchBin[45] := 25; // ADD HL,DE
MatchBin[46] := 229; // PUSH HL
MatchBin[47] := 58; // LD A,(nn)
MatchBin[48] := 256; // n1
MatchBin[49] := 256; // n2
MatchBin[50] := 31; // RRA
MatchBin[51] := 31; // RRA
MatchBin[52] := 31; // RRA
MatchBin[53] := 230; // AND n
MatchBin[54] := 7; // $07
MatchBin[55] := 50; // LD (nn),A
MatchBin[56] := 256; // n1
MatchBin[57] := 256; // n2
MatchBin[58] := 38; // LD H,n
MatchBin[59] := 0; // $00
MatchBin[60] := 111; // LD L,A
MatchBin[61] := 34; // LD (nn),HL
MatchBin[62] := 256; // CNT_1A_1
MatchBin[63] := 256; // CNT_1A_2
MatchBin[64] := 34; // LD (nn),HL
MatchBin[65] := 256; // CNT_1B_1
MatchBin[66] := 256; // CNT_1B_2
MatchBin[67] := 34; // LD (nn),HL
MatchBin[68] := 256; // DIV_1A_1
MatchBin[69] := 256; // DIV_1A_2
MatchBin[70] := 34; // LD (nn),HL
MatchBin[71] := 256; // DIV_1B_1
MatchBin[72] := 256; // DIV_1B_2
MatchBin[73] := 34; // LD (nn),HL
MatchBin[74] := 256; // CNT_2_1
MatchBin[75] := 256; // CNT_2_2
MatchBin[76] := 34; // LD (nn),HL
MatchBin[77] := 256; // DIV_2_1
MatchBin[78] := 256; // DIV_2_2
MatchBin[79] := 62; // LD A,n
MatchBin[80] := 256; // DIV_2_2
MatchBin[81] := 50; // LD (nn),A
MatchBin[82] := 256; // OUT_1_1
MatchBin[83] := 256; // OUT_1_2
MatchBin[84] := 50; // LD (nn),A
MatchBin[85] := 256; // OUT_2_1