-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2cas.pl
2330 lines (1981 loc) · 57.1 KB
/
bin2cas.pl
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
#!/usr/bin/perl -w
# bin2cas.pl - convert binary data to CAS or WAV with optional autorun
# Copyright 2017-2022 Ciaran Anscomb
# License: GNU GPL version 3 or later <http://www.gnu.org/licenses/gpl-3.0.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
#
# Special exception: Code included in output (such as "dunzip" or "unzx0") is
# not considered to be part of this program, so the GPL doesn't apply to the
# results.
# See the output of "bin2cas.pl --help" for detailed usage information, with
# examples.
# Pre-v2.0: really rubbish
# Pre-v3.0: getting better, but still limited
# v3.0: complete overhaul
# supports multiple parts
# builds reasonably arbitrary autorun loader
# update VDG & SAM configs during loading
# manual cursor flasher for when video offset changed
# v3.0a: tweak default samplerate to 12kHz if fast mode used
# slightly increase speed in fast mode
# v3.1: default timing uses varying pulse widths to account for ROM delays
# --timing option selects between "rom" and "simple" timing
# fast cycle timing better at 9600Hz, so dropped default back
# slightly faster WAV creation by caching runs of samples
# v3.2: don't redirect STDOUT
# new option --lds inserts stack adjustment code into autorun
# license foo, --version
# v3.3: actually drop default to 9600Hz
# dzip using temp files under windows
# v3.4: add --no-delay, --block-size, --gapped, --pause, --wav-bits
# --cas, --wav deprecated, use --mode or rely on file extension
# fix --no-eof
# tweak waveform output
# v3.5: add --poke
# v3.6: use Getopt::Long and Pod::Usage
# remove deprecated --dunzip
# accept $ as well as 0x to indicate hex argument
# v3.7: add --vxor
# v3.8: dzipped data needs *3* bytes clear of overlap!
# v3.9: actually, dzip offset depends on number of runs at end
# v3.10: include load_part, dunzip pointers at known address
# v3.11: add --omit
# v3.12: add --cue
# v3.13: add --memset
# v3.14: allow --timing double
# new block formats: namefile, data, eof
# add --zx0 to support ZX0 compression using salvador
use strict;
require v5.10;
use Getopt::Long;
use Pod::Usage;
use File::Temp qw/tempfile tempdir/;
use Math::Trig;
require bytes;
use constant VERSION => "3.14";
use constant {
TYPE_BASIC => 0,
TYPE_DATA => 1,
TYPE_BINARY => 2,
};
use constant {
DDOS_TYPE_BASIC => 1,
DDOS_TYPE_BINARY => 2,
};
use constant {
ENCODING_BINARY => 0,
ENCODING_ASCII => 0xff,
};
use constant {
GAP_FALSE => 0,
GAP_TRUE => 0xff,
};
use constant {
BLOCK_NAMEFILE => 0,
BLOCK_DATA => 1,
BLOCK_EOF => 0xff,
};
use constant {
INPUT_RAW => 0,
INPUT_DRAGONDOS => 1,
INPUT_COCO => 2,
};
use constant {
CUE_TIMING => 0xf0,
CUE_SILENCE => 0x00,
CUE_DATA => 0x0d,
CUE_COMMENT => 0x22,
};
use constant {
ZX0_COPY_LITERALS => 0,
ZX0_COPY_FROM_LAST_OFFSET => 1,
ZX0_COPY_FROM_NEW_OFFSET => 2,
};
use constant DEFAULT_SAMPLE_RATE => 9600;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Sekr1t options support writing specific games as accurately as possible:
# Tube Way Army:
# Loader: --name "TWA" --load 0 --exec 0 --gapped --timing rom
# Delay: --pause --pause
# Game: --no-filename --block-size 128 --block-fmt twa --timing twa
# Superkid:
# Loader: --name "SUPERKID" --load 0x012c --exec 0x4000 --timing rom
# Block0: --no-filename --no-eof --timing sk.b0
# Screen0: --no-filename --no-delay --no-eof --timing sk.s0
# --block-fmt bare --block-size 0
# Screen1: --no-filename --no-delay --no-eof --timing sk.s1
# --block-fmt bare --block-size 0
# Game: --no-filename --no-delay --no-eof --timing sk.g
# --block-fmt bare --block-size 0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Map DragonDOS filetypes to tape filetypes.
my %ddos_to_tape_type = (
DDOS_TYPE_BASIC => TYPE_BASIC,
DDOS_TYPE_BINARY => TYPE_BINARY,
);
# Wave data.
my @wav_header = (
0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00,
0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20,
0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x08, 0x00, 0x64, 0x61, 0x74, 0x61,
0x00, 0x00, 0x00, 0x00
);
# Timings
# 'cycles' is the nominal number of CPU cycles (SAM / 16) in the waveform for
# each bit (0, 1), the rest are pulse specs.
# Pulse spec is three pairs of pulse delays (first pulse, second pulse):
# - first bit of first byte
# - remaining bits in each byte
# - first bit of subsequent bytes
# 'leader' is pulse spec while writing leader bytes and the sync byte
# 'first' is pulse spec for the first byte in a block
# 'rest' is pulse spec for remaining bytes in a block (until next leader)
# The "rom" spec is intended to be the "best fit" for the Dragon ROM -
# that is, the ROM will count an "ideal" pulse width for each bit. This should
# improve reliability.
my %timing_simple = (
'cycles' => [ 813, 435 ],
'leader0' => [ 0, 0, 0, 0 ],
'leader' => [ 0, 0, 0, 0 ],
'type' => [ 46, 0, 0, 0 ],
'size' => [ 46, 0, 0, 0 ],
'data0' => [ 46, 0, 0, 0 ],
'data' => [ 46, 0, 0, 0 ], # 818.75, 440.75 -> 629.75
'sum' => [ 46, 0, 0, 0 ],
);
my %timing_rom = (
'cycles' => [ 26*28, 13*28 ],
'leader0' => [ 205, 5, 68, 5 ],
'leader' => [ 68, 5, 68, 5 ],
'type' => [ 81, 5, 67, 5 ],
'size' => [ 120, 5, 67, 5 ],
'data0' => [ 120, 5, 67, 5 ],
'data' => [ 120, 5, 67, 5 ], # 806.625, 442.625 -> 625.625
'sum' => [ 120, 5, 67, 5 ],
);
my %timing_double = (
'cycles' => [ 13*28, 6.5*28 ],
'leader0' => [ 103, 3, 34, 3 ],
'leader' => [ 34, 3, 34, 3 ],
'type' => [ 41, 3, 34, 4 ],
'size' => [ 60,3, 34, 4 ],
'data0' => [ 60,3, 34, 4 ],
'data' => [ 60,3, 34, 4 ],
'sum' => [ 60,3, 34, 4 ],
);
my %timing_fast = (
'cycles' => [ 18*28, 7*28 ],
'leader0' => [ 205, 5, 68, 5 ],
'leader' => [ 68, 5, 68, 5 ],
'type' => [ 81, 5, 67, 5 ],
'size' => [ 120, 5, 67, 5 ],
'data0' => [ 120, 5, 67, 5 ],
'data' => [ 120, 5, 67, 5 ], # 582.625, 274.625 -> 428.125
'sum' => [ 120, 5, 67, 5 ],
);
my %timing_twa = (
'cycles' => [ 26*28, 13*28 ],
'leader0' => [ 114, 5, 54, 5 ],
'leader' => [ 54, 5, 54, 5 ],
'size' => [ 67, 5, 58, 5 ],
'type' => [ 82, 5, 58, 5 ],
'data0' => [ 101, 5, 58, 5 ],
'data' => [ 106, 5, 58, 5 ], # 797.0, 433.0 -> 615.0
'sum' => [ 106, 5, 58, 5 ],
);
my %timing_sk_b0 = (
'cycles' => [ 25*28, 11*28 ],
'leader0' => [ 205, 5, 68, 5 ],
'leader' => [ 68, 5, 68, 5 ],
'type' => [ 81, 5, 67, 5 ],
'size' => [ 120, 5, 67, 5 ],
'data0' => [ 120, 5, 67, 5 ],
'data' => [ 120, 5, 67, 5 ],
'sum' => [ 120, 5, 67, 5 ],
);
my %timing_sk_s0 = (
'cycles' => [ 25*28, 11*28 ],
'data0' => [ 89, 5, 67, 5 ],
'data' => [ 112, 5, 67, 5 ],
);
my %timing_sk_s1 = (
'cycles' => [ 25*28, 11*28 ],
'data0' => [ 115, 5, 67, 5 ],
'data' => [ 112, 5, 67, 5 ],
);
my %timing_sk_g = (
'cycles' => [ 25*28, 11*28 ],
'leader' => [ 132, 5, 67, 5 ],
'data0' => [ 125, 5, 67, 5 ],
'data' => [ 132, 5, 67, 5 ],
'adj_period' => 8,
'adj' => 6,
);
my %timing_by_name = (
'simple' => \%timing_simple,
'rom' => \%timing_rom,
'fast' => \%timing_fast,
'double' => \%timing_double,
'twa' => \%timing_twa,
'sk.b0' => \%timing_sk_b0,
'sk.s0' => \%timing_sk_s0,
'sk.s1' => \%timing_sk_s1,
'sk.g' => \%timing_sk_g,
);
# Block format strings
my %fmt_strings = (
'bare' => 'd',
'twa' => 'iSTdCo',
'normal' => 'itsdco',
'namefile' => 'insdco',
'data' => 'ibsdco',
'eof' => 'iesdco',
);
# Autorun headers can include optional parts, concatenated and subject to
# linking.
my @code_load_0 = (
"load_part",
0x9f, 0x7e, # stx <$7e
0xad, 0x9f, 0xa0, 0x04, # jsr [CSRDON]
"l0",
);
my @code_load_flash = (
0xb6, ">flash_addr", # lda >flash_addr
0x88, 0x40, # eora #$40
# flash code starts off disabled (first load will still be in text mode)
"mod_flash",
0x8c, ">flash_addr", # cmpx >flash_addr
);
my @code_load_1 = (
0xad, 0x9f, 0xa0, 0x06, # jsr [BLKIN]
0x26, "\&<do_io_error", # bne do_io_error
0x9f, 0x7e, # stx <$7e
0x96, 0x7c, # lda <$7c
0x4c, # inca
0x26, "\&<l0", # bne l0
"cas_off",
0xb6, 0xff, 0x21, # lda >$ff21
0x84, 0xf7, # anda #$f7
0xb7, 0xff, 0x21, # sta >$ff21
"do_rts",
0x39, # rts
"do_io_error",
0x0f, 0x71, # clr <$71
0x6e, 0x9f, 0xff, 0xfe, # jmp [$fffe]
);
my @code_disable_flasher = (
0x86, 0x8c, # lda #$8c ; cmpx
0xb7, ">mod_flash", # sta mod_flash
);
my @code_enable_flasher = (
0x86, 0xb7, # lda #$8c ; sta
0xb7, ">mod_flash", # sta mod_flash
);
my @code_test_arch = (
0xb6, 0xa0, 0x00, # lda $a000
0x84, 0x20, # anda #$20
0x97, 0x10, # sta <$10
);
my @code_fast = (
0xcc, ">fast_pw", # ldd #fast_pw
0x0d, 0x10, # tst <$10
0x26, "\&<fl1", # bne fl1
0xdd, 0x92, # std <$92
0x97, 0x94, # sta <$94
0x20, "\&<fl2", # bra fl2
"fl1",
0xdd, 0x90, # std <$90
0x97, 0x8f, # sta <$8f
"fl2",
);
my @code_dunzip = (
"dunzip",
0x34, 0x06, # pshs d
"dunz_loop",
0xec, 0x81, # ldd ,x++
0x2a, "\&<dunz_run", # bpl dunz_run
0x5d, # tstb
0x2a, "\&<dunz_7_7", # bpl dunz_7_7
"dunz_14_8",
0x58, # lslb
0x47, # asra
0x56, # rorb
0x31, 0xcb, # leay d,u
0xe6, 0x80, # ldb ,x+
0x20, "\&<dl0", # bra dl0
"dunz_7_7",
0x31, 0xc6, # leay a,u
"dl0",
0xa6, 0xa0, # lda ,y+
0xa7, 0xc0, # sta ,u+
0x5c, # incb
0x28, "\&<dl0", # bvc dl0
0x20, "\&<dl2", # bra dl2
"dl1",
0xe6, 0x80, # ldb ,x+
"dunz_run",
0xe7, 0xc0, # stb ,u+
0x4c, # inca
0x28, "\&<dl1", # bvc dl1
"dl2",
0xac, 0xe4, # cmpx ,s
0x25, "\&<dunz_loop", # blo dunz_loop
0x35, 0x86, # puls d,pc
);
# Reverse dunzip. Ok to reuse labels as only one will be included.
my @code_dunzipr = (
"dunzip",
0x34, 0x06, # pshs d
"dunz_loop",
0xec, 0x83, # ldd ,--x
0x2b, "\&<dunz_run", # bmi dunz_run
0x5d, # tstb
0x2a, "\&<dunz_7_7", # bpl dunz_7_7
"dunz_14_8",
0x58, # lslb
0x44, # lsra
0x56, # rorb
0x31, 0xcb, # leay d,u
0xe6, 0x82, # ldb ,-x
0x20, "\&<dj0", # bra dj0
"dunz_7_7",
0x31, 0xc6, # leay a,u
"dj0",
0x31, 0x21, # leay 1,y
"dl0",
0xa6, 0xa2, # lda ,-y
0xa7, 0xc2, # sta ,-u
0x5c, # incb
0x28, "\&<dl0", # bvc dl0
0x20, "\&<dl2", # bra dl2
"dl1",
0xe6, 0x82, # ldb ,-x
"dunz_run",
0xe7, 0xc2, # stb ,-u
0x4c, # inca
0x26, "\&<dl1", # bne dl1
"dl2",
0xac, 0xe4, # cmpx ,s
0x22, "\&<dunz_loop", # bhi dunz_loop
0x35, 0x86, # puls d,pc
);
# unzx0 - decompress ZX0 compressed data. This is derived from code by Doug
# Masten; please see COPYING.zx0-6x09 for licence, but be aware that his code
# HAS BEEN MODIFIED for inclusion here.
# zx0_bit = 0x0011
# zx0_offset = 0x0012
my @code_unzx0 = (
'unzx0',
0xcc, 0xff, 0xff, # ldd #$ffff
0xdd, 0x12, # std zx0_offset
0x86, 0x80, # lda #$80
0x97, 0x11, # sta zx0_bit
'zx0_literals',
0x8d, '&<zx0_elias', # bsr zx0_elias
0x1f, 0x02, # tfr d,y
0x8d, '&<zx0_copy_bytes', # bsr zx0_copy_bytes
0x25, '&<zx0_new_offset', # bcs zx0_new_offset
0x8d, '&<zx0_elias', # bsr zx0_elias
'zx0_copy',
0x34, 0x10, # pshs x
0x1f, 0x02, # tfr d,y
0xdc, 0x12, # ldd zx0_offset
0x30, 0xcb, # leax d,u
0x8d, '&<zx0_copy_bytes', # bsr zx0_copy_bytes
0x35, 0x10, # puls x
0x24, '&<zx0_literals', # bcc zx0_literals
'zx0_new_offset',
0x8d, '&<zx0_elias', # bsr zx0_elias
0x50, # negb
0x27, '&<zx0_eof', # beq zx0_eof
0x1f, 0x98, # tfr b,a
0xe6, 0x80, # ldb ,x+
0x46, # rora
0x56, # rorb
0xdd, 0x12, # std zx0_offset
0xcc, 0x00, 0x01, # ldd #1
0x25, '&<zj0', # bcs zj0
0x8d, '&<zx0_backtrace',# bsr zx0_backtrace
'zj0',
0xc3, 0x00, 0x01, # addd #$0001
0x20, '&<zx0_copy', # bra zx0_copy
'zx0_elias',
0xcc, 0x00, 0x01, # ldd #1
0x20, '&<zj1', # bra zj1
'zx0_backtrace',
'zl1',
0x08, 0x11, # lsl zx0_bit
0x59, # rolb
0x49, # rola
'zj1',
0x08, 0x11, # lsl zx0_bit
0x26, '&<zj2', # bne zj2
0x34, 0x02, # pshs a
0xa6, 0x80, # lda ,x+
0x49, # rola
0x97, 0x11, # sta zx0_bit
0x35, 0x02, # puls a
'zj2',
0x24, '&<zl1', # bcc zl1
'zx0_eof',
0x39, # rts
'zx0_copy_bytes',
'zl0',
0xe6, 0x80, # ldb ,x+
0xe7, 0xc0, # stb ,u+
0x31, 0x3f, # leay -1,y
0x26, '&<zl0', # bne zl0
0x08, 0x11, # lsl zx0_bit
0x39, # rts
);
# memset
my @code_memset = (
"memset",
0x34, 0x40, # pshs u
0xa7, 0x80, # ! sta ,x+
0xac, 0xe4, # cmpx ,s
0x25, 0xfa, # blo <
0x35, 0xc0, # puls u,pc
);
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Output options
my $out_filename;
my $wav_out;
my $sample_rate;
my $bits_per_sample = 8;
# Global options
my $autorun = 0;
my $leader = 256;
my $cue = 0;
# Per-file options
my $input_mode = INPUT_RAW;
my $want_name;
my $want_load;
my $want_exec;
my $want_zload;
my $want_fnblock;
my $want_delay = 1;
my $block_size = 255;
my $block_fmt = 'normal';
my $want_dzip = 0;
my $want_zx0 = 0;
my $want_vxor = 0;
my $want_eof = 1;
my $want_eof_data = 0;
my $want_gapped = 0;
my $want_timing;
my $want_omit = 0;
my $want_flasher = 0;
# Persistent
my $autorun_name;
my $autorun_exec;
my $any_dzip = 0;
my $any_zx0 = 0;
my $any_fast = 0;
my $any_flasher = 0;
my $any_memset = 0;
my $reverse_dzip = 0;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
my $out_fd;
# Relocation
my $mc_org;
my $mc_pc;
my %mc_label = ();
my @mc_link;
my $mc_data;
# WAV
my $num_channels = 1;
my $sample_count = 0;
my $ao_error = 0;
my %sinewaves = ();
my $normal_timing = 'rom';
my $timing;
my $adj_period = 0;
my $adj = 0;
my $adj_count = 0;
# this combination of slow cycle pulse width boundaries seems to allow reliable
# tape speed variance of at least +/-7% when emitting 30% faster data to a
# 9600Hz WAV
$mc_label{'fast_pw'} = 0x0c06;
# de-taint PATH for call out to dzip or zx0
if ($ENV{'PATH'} =~ /^(.*)$/) {
$ENV{'PATH'} = $1;
}
my @files = ();
my %file = ();
Getopt::Long::Configure("gnu_getopt", "pass_through");
# helper to parse integer options - allow hex values prefixed by $ or 0x
sub eval_int {
my $string = shift;
$string =~ s/^\$/0x/;
if ($string =~ /^(\d+|0x[\da-f]+)$/i) {
return eval $1;
}
die "invalid number format: $string\n";
}
GetOptions(
# Output options:
"output|o=s" => \$out_filename,
"mode|m=s" => sub {
shift;
my $v = lc(shift @ARGV);
$wav_out = ($v eq 'wav');
},
"cas" => sub { $wav_out = 0; },
"wav-out|wav" => sub { $wav_out = 1; },
"wav-rate|r=s" => sub { shift; $sample_rate = eval_int(shift) },
"wav-bits|b=s" => sub { shift; $bits_per_sample = eval_int(shift) },
# Global options:
"autorun|A!" => \$autorun,
"leader=s" => sub { shift; $leader = eval_int(shift) },
"cue!" => \$cue,
# Per-file options:
"B" => sub { $input_mode = INPUT_RAW; },
"D" => sub { $input_mode = INPUT_DRAGONDOS; },
"C" => sub { $input_mode = INPUT_COCO; },
"name|n=s" => sub {
shift;
$want_name = shift;
$autorun_name = $want_name;
},
"load|l=s" => sub { shift; $want_load = eval_int(shift) },
"exec|e=s" => sub {
shift;
$want_exec = eval_int(shift);
$autorun_exec = $want_exec;
},
"zload=s" => sub { shift; $want_zload = eval_int(shift) },
"filename!" => \$want_fnblock,
"delay!" => \$want_delay,
"block-size=s" => sub { shift; $block_size = eval_int(shift) },
"block-fmt=s" => \$block_fmt,
"eof!" => \$want_eof,
"gapped!" => \$want_gapped,
"timing|T=s" => sub {
shift;
$want_timing = shift;
$any_fast = 1 if ($want_timing eq 'fast');
},
"fast" => sub {
$want_timing = 'fast';
$any_fast = 1;
},
"no-fast" => sub { undef $want_timing; },
"omit!" => \$want_omit,
"no-flasher|noflasher" => sub { $want_flasher = 0; },
"flasher" => sub {
$want_flasher = 1;
$any_flasher = 1;
},
"vxor|x=s" => sub {
shift;
$want_vxor = shift;
},
"no-vxor|novxor" => sub { $want_vxor = 0; },
"i=s" => sub { shift; push @files, input_file(shift); },
# Per-file persistent options:
"dzip|z" => sub {
$want_dzip = 1;
$any_dzip = 1;
$want_zx0 = 0;
},
"zx0" => sub {
$want_zx0 = 1;
$any_zx0 = 1;
$want_dzip = 0;
},
"no-dzip|nodzip" => sub { $want_dzip = 0; },
"no-zx0|nozx0" => sub { $want_zx0 = 0; },
"reverse-dzip" => sub { $reverse_dzip = 1; },
"eof-data|E!" => \$want_eof_data,
# Inter-file options:
"lds=s" => sub {
shift;
push @files, { 'code' => "lds", 'value' => eval_int(shift) };
},
"pause" => sub {
push @files, { 'code' => "pause", 'value' => 0 };
},
"sam-f=s" => sub {
shift;
push @files, { 'code' => "set_sam_f", 'value' => eval_int(shift) };
},
"sam-v=s" => sub {
shift;
my $v = eval_int(shift);
push @files, { 'code' => "set_sam_v", 'value' => $v };
},
"vdg=s" => sub {
shift;
push @files, { 'code' => "set_vdg", 'value' => eval_int(shift) };
},
"poke=s" => sub {
shift;
my $v = shift;
push @files, { 'code' => "poke", 'value' => $v };
},
"memset=s" => sub {
shift;
my $v = shift;
push @files, { 'code' => "memset", 'value' => $v };
$any_memset = 1;
},
# Other options:
"version" => \&version_text,
"help|?" => sub { pod2usage(-verbose => 2); },
# pass_through option means anything else hits this, so die if
# it looks like an option we don't handle, else treat it as an
# input file:
"<>" => sub {
my $file = shift;
die "Unrecognised option: $file\n" if ($file =~ /^-/);
push @files, input_file($file);
},
) or exit(2);
die "invalid bits per sample (choose 8 or 16)\n" if ($bits_per_sample != 8 && $bits_per_sample != 16);
if (defined $out_filename && $out_filename =~ /(.*)/) {
$out_filename = $1; # de-taint
} else {
die "No output filename specified\n";
}
if (@ARGV) {
shift @ARGV; # discard --
while (@ARGV) {
push @files, input_file(shift @ARGV);
}
}
# Prepare output stream.
open($out_fd, ">", $out_filename) or die $!;
binmode $out_fd;
if ($out_filename =~ /\.cas$/i) {
$wav_out //= 0;
} elsif ($out_filename =~ /\.wav$/i) {
$wav_out //= 1;
}
$sample_rate //= DEFAULT_SAMPLE_RATE;
my $sam_rate = 14318180;
my $bytes_per_sample = $bits_per_sample >> 3;
my $cue_data = "";
my $cue_cas_data_start = 0;
my $cue_bit0_length = -1;
my $cue_bit1_length = -1;
# WAV header?
if ($wav_out) {
# No CUE for WAV
$cue = 0;
# NumChannels
$wav_header[22] = $num_channels;
$wav_header[23] = ($num_channels >> 8) & 0xff;
# SampleRate
$wav_header[24] = $sample_rate & 0xff;
$wav_header[25] = ($sample_rate >> 8) & 0xff;
$wav_header[26] = ($sample_rate >> 16) & 0xff;
$wav_header[27] = ($sample_rate >> 24) & 0xff;
# ByteRate
my $byte_rate = $sample_rate * $num_channels * $bytes_per_sample;
$wav_header[28] = $byte_rate & 0xff;
$wav_header[29] = ($byte_rate >> 8) & 0xff;
$wav_header[30] = ($byte_rate >> 16) & 0xff;
$wav_header[31] = ($byte_rate >> 24) & 0xff;
# BlockAlign
my $block_align = ($num_channels * $bits_per_sample) / 8;
$wav_header[32] = $block_align & 0xff;
$wav_header[33] = ($block_align >> 8) & 0xff;
# BitsPerSample
$wav_header[34] = $bits_per_sample & 0xff;
$wav_header[35] = ($bits_per_sample >> 8) & 0xff;
print $out_fd pack("C*", @wav_header);
}
# Write file(s).
write_autorun(\@files) if ($autorun);
write_all_files(\@files);
write_autorun_post(\@files) if ($autorun);
# Close output.
if ($wav_out) {
# rewrite Subchunk2Size
$sample_count *= $bytes_per_sample;
seek($out_fd, 40, 0);
print $out_fd pack("C", $sample_count & 0xff);
print $out_fd pack("C", ($sample_count >> 8) & 0xff);
print $out_fd pack("C", ($sample_count >> 16) & 0xff);
print $out_fd pack("C", ($sample_count >> 24) & 0xff);
# rewrite ChunkSize
$sample_count += 36;
seek($out_fd, 4, 0);
print $out_fd pack("C", $sample_count & 0xff);
print $out_fd pack("C", ($sample_count >> 8) & 0xff);
print $out_fd pack("C", ($sample_count >> 16) & 0xff);
print $out_fd pack("C", ($sample_count >> 24) & 0xff);
}
if ($cue) {
cue_finish_cas_data();
my $cue_start = tell($out_fd);
print $out_fd "[CUE";
print $out_fd $cue_data;
print $out_fd pack("N", $cue_start);
print $out_fd "CUE]";
}
exit 0;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub reset_default_options {
$input_mode = INPUT_RAW;
undef $want_name;
undef $want_load;
undef $want_exec;
undef $want_zload;
undef $want_fnblock;
$want_delay = 1;
$block_size = 255;
$block_fmt = 'normal';
$want_eof = 1;
$want_gapped = 0;
undef $want_timing;
$want_flasher = 0;
$want_vxor = 0;
}
# Read a file, applying current default options, reset default options.
sub input_file {
my ($filename,$file) = @_;
$file //= {};
# defaults
$file->{'fnblock'} //= $want_fnblock;
$file->{'delay'} //= $want_delay;
$file->{'block_size'} //= $block_size;
$file->{'block_fmt'} //= $block_fmt;
$file->{'eof'} //= $want_eof;
$file->{'eof_data'} //= $want_eof_data;
$file->{'gapped'} //= $want_gapped;
$file->{'timing'} //= $want_timing;
$file->{'flasher'} //= $want_flasher;
$file->{'omit'} //= $want_omit;
# read in appropriate mode
$file = do {
if ($input_mode == INPUT_DRAGONDOS) {
read_dragondos($filename, $file);
} elsif ($input_mode == INPUT_COCO) {
read_coco($filename, $file);
} else {
read_raw($filename, $file);
}
};
# overrides
$file->{'name'} = $want_name if (defined $want_name);
$file->{'load'} = $want_load if (defined $want_load);
$file->{'exec'} = $want_exec if (defined $want_exec);
$file->{'zload'} = $want_zload if (defined $want_zload);
die "No data\n" unless exists $file->{'segments'};
# XXX only deal with single-segment binaries for now
coalesce_file($file);
vxor_file($file) if ($want_vxor);
dzip_file($file) if ($want_dzip);
zx0_file($file) if ($want_zx0);
reset_default_options();
return $file;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# File readers
sub open_file {
my ($filename,$file) = @_;
$file //= {};
open(my $in, "<", $filename) or die "Failed to open $filename: $!\n";
binmode $in;
if ($filename =~ /^([^\.]{1,8})/) {
$file->{'name'} //= uc $1;
}
return $in;
}
# Raw binary: just slurp data into one segment
sub read_raw {
my ($filename,$file) = @_;
$file //= {};
my $in = open_file($filename, $file);
my %segment = (
'start' => 0,
'size' => 0,
'data' => "",
);
my $data;
my $rsize;
do {
$rsize = read $in, $data, 0x10000;
$segment{'data'} .= $data;
$segment{'size'} += $rsize;
} while ($rsize == 0x10000);
$file->{'segments'} = [ \%segment ];
close $in;
return $file;
}
# DragonDOS binary - single segment only
sub read_dragondos {
my ($filename,$file) = @_;
my $in = open_file($filename, $file);
getc($in); # skip $55
my $type = unpack("C", getc($in));
my $start = (unpack("C", getc($in)) << 8) | unpack("C", getc($in));
my $size = (unpack("C", getc($in)) << 8) | unpack("C", getc($in));
my $exec = (unpack("C", getc($in)) << 8) | unpack("C", getc($in));
getc($in); # skip $aa
my $data;
my $rsize = read $in, $data, $size;
if ($rsize != $size) {
print STDERR "Warning: short read from DragonDOS binary\n";
}
my %segment = (
'start' => $start,
'data' => $data,
'size' => $rsize,
);
$file->{'segments'} = [ \%segment ];
close $in;
$file->{'type'} //= $ddos_to_tape_type{$type} // TYPE_BINARY;
$file->{'exec'} //= $exec;
return $file;
}
# CoCo (DECB) - binaries can contain multiple segments
# BASIC files are: $ff size>>8 size data*
# BINARY files are: ($00 size>>8 size data*)+ $ff 00 00 exec>>8 exec
# (binaries can contain multiple segments)
sub read_coco {
my ($filename,$file) = @_;
my $in = open_file($filename, $file);
my $type;
my $exec;
while (my $stype = getc($in)) {
$stype = unpack("C", $stype);
my $start;
my $size = (unpack("C", getc($in)) << 8) | unpack("C", getc($in));
if ($stype == 0x00) {
$type //= TYPE_BINARY;
$start = (unpack("C", getc($in)) << 8) | unpack("C", getc($in));
} elsif (!defined $type && $stype == 0xff) {
$type = TYPE_BASIC;
$start = 0;
$exec = 0;
} elsif ($stype == 0xff) {
if ($size != 0) {
# XXX is this dodgy?
printf STDERR "Warning: EXEC segment with non-zero size in CoCo binary\n";
}
$exec = (unpack("C", getc($in)) << 8) | unpack("C", getc($in));
} else {