-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdr.pl
1422 lines (1193 loc) · 54.7 KB
/
mdr.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
=pod
mdr.pl
rev.2022.12.01
\ by Matteo Vitturi, 2016-2022
\ Copying, modifying and distributing this software is allowed
\ provided this copyright notice is kept.
\ This work is available as-is with no whatsoever warranty.
This is a script that allows you to manipulate .MDR cartrige files.
This script needs a .MDR filename that is read entirely at start.
Then some operations will be performed based on options given.
Usage is:
perl mdr.pl [switch] <cartridge> [key=value ...]
where [switch] can be
-h for help
-l show catalog (optional)
-f used with -l switch: Show free sectors.
-b used with -l switch: Hide bad sectors.
-s with -l show sectors used instead of file details
-r removes autorun LINE from programs in cartridge.
-v verbose, show much more details at start
-x fix bad sectors
-c create empty if not exists
-g used with tape to get all .mdr content to .tap
<cartridge> is the filename of a .MDR file.
Normally operation will be done "in-place" unless an out option is specified.
[key=value] available are listed here below:
* Cartridge only options:
rename=name rename "name". New name is given via "to" option
copy=name copy "name". New name given via "to" option
to=newname destination "name" in .MDR.
erase=name erase "name" from .MDR
label=name change label of cartridge.
noautorun=name remove autorun of a single file "name" inside .MDR
autorun=name set autorun of a single file "name" inside .MDR
line=number used with autorun option to specify run
* Host file options:
out=output.MDR create a new cartridge file, original is unchanged.
* From-to cartridge options:
get=name gets a file from .MDR and write it to "host" file
dump=name same as get, but output is just a dump file
dump32=name same as dump, but adds/takes a new-line every 32 characters.
dumpblock=name same as dump32, but adds a "Screen#" header for each 16 rows.
text=name same as dump, but converts LF to CR, or CR+LF to CR.
put=name reads "host" files and creates files to .MDR
Examples:
-l xyz.mdr noautorun=run remove autorun from "run"
-l xyz.mdr erase=run erase "run" file
-l xyz.mdr rename=abc to=ABC rename "abc" to "ABC"
-l xyz.mdr -p tape=afile.tap put afile.tap content to mdr
-l xyz.mdr put=f1 host=file1.bin copy host file1.bin to mdr "f1"
-l xyz.mdr -g tape=afile.tap get mdr content to afile.tap
-l xyz.mdr get=prnt dump=prnt.txt get prnt file from mdr to dump
From Z80 tecnical documentation
.MDR FILES:
-----------
The emulator uses a cartridge file format identical to the 'Microdrive
File' format of Carlo Delhez' Spectrum emulator Spectator for the QL,
who devised the format. This format is now also supported by XZX of
Des Harriot. The following information is adapted from Carlo's
documentation. It can also be found in the 'Spectrum Microdrive Book',
by Ian Logan (co-writer of the excellent 'Complete Spectrum ROM
Disassembly').
A cartridge file contains 254 'sectors' of 543 bytes each, and a final
byte flag which is non-zero is the cartridge is write protected, so the
total length is 137923 bytes. On the cartridge tape, after a GAP of
some time the Interface I writes 10 zeros and 2 FF bytes (the
preamble), and then a fifteen byte header-block-with-checksum. After
another GAP, it writes a preamble again, with a 15-byte record-
descriptor-with-checksum (which has a structure very much like the
header block), immediately followed by the data block of 512 bytes, and
a final checksum of those 512 bytes. The preamble is used by the
Interface I hardware to synchronise, and is not explicitly used by the
software. The preamble is not saved to the microdrive file:
offset length name contents
0 1 HDFLAG Value 1, to indicate header block
1 1 HDNUMB sector number (values 254 down to 1)
2 2 not used
4 10 HDNAME microdrive cartridge name (blank padded)
14 1 HDCHK header checksum (of first 14 bytes)
15 1 RECFLG - bit 0: always 0 to indicate record block
- bit 1: set for the EOF block
- bit 2: reset for a PRINT file
- bits 3-7: not used (value 0)
16 1 RECNUM data block sequence number (value starts at 0)
17 2 RECLEN data block length (<=512, LSB first)
19 10 RECNAM filename (blank padded)
29 1 DESCHK record descriptor checksum (of previous 14 bytes)
30 512 data block
542 1 DCHK data block checksum (of all 512 bytes of data
block, even when not all bytes are used)
---------
254 times
(Actually, this information is 'transparent' to the emulator. All it
does is store 2 times 254 blocks in the .MDR file as it is OUTed,
alternatingly of length 15 and 528 bytes. The emulator does check
checksums, see below; the other fields are dealt with by the emulated
Interface I software.)
A used record block is either an EOF block (bit 1 of RECFLG is 1) or
contains 512 bytes of data (RECLEN=512, i.e. bit 1 of MSB is 1). An
empty record block has a zero in bit 1 of RECFLG and also RECLEN=0. An
unusable block (as determined by the FORMAT command) is an EOF block
with RECLEN=0.
The three checksums are calculated by adding all the bytes together
modulo 255; this will never produce a checksum of 255. Possibly, this
is the value that is read by the Interface I if there's no or bad data
on the tape.
In normal operation, all first-fifteen-byte blocks of each header or
record block will have the right checksum. If the checksum is not
right, the block will be treated as a GAP. For instance, if you type
OUT 239,0 on a normal Spectrum with interface I, the microdrive motor
starts running and the cartridge will be erased completely in 7
seconds. CAT 1 will respond with 'microdrive not ready'. Try it on the
emulator...
.TAP FILES:
-----------
The .TAP files contain blocks of tape-saved data. All blocks start
with two bytes specifying how many bytes will follow (not counting the
two length bytes). Then raw tape data follows, including the flag and
checksum bytes. The checksum is the bitwise XOR of all bytes including
the flag byte. For example, when you execute the line SAVE "ROM" CODE
0,2 this will result:
|------ Spectrum-generated data -------| |---------|
13 00 00 03 52 4f 4d 7x20 02 00 00 00 00 80 f1 04 00 ff f3 af a3
^^^^^...... first block is 19 bytes (17 bytes+flag+checksum)
^^... flag byte (A reg, 00 for headers, ff for data blocks)
^^ first byte of header, indicating a code block
file name ..^^^^^^^^^^^^^
header info ..............^^^^^^^^^^^^^^^^^
checksum of header .........................^^
length of second block ........................^^^^^
flag byte ............................................^^
first two bytes of rom .................................^^^^^
checksum (checkbittoggle would be a better name!).............^^
The emulator will always start reading bytes at the beginning of a
block. If less bytes are loaded than are available, the other bytes
are skipped, and the last byte loaded is used as checksum. If more
bytes are asked for than exist in the block, the loading routine will
terminate with the usual tape-loading-error flags set, leaving the
error handling to the calling Z80 program.
Note that it is possible to join .TAP files by simply stringing them
together, for example COPY /B FILE1.TAP + FILE2.TAP ALL.TAP
For completeness, I'll include the structure of a tape header. A
header always consists of 17 bytes:
Byte Length Description
0 1 Type (0,1,2 or 3)
1 10 Filename (padded with blanks)
11 2 Length of data block
13 2 Parameter 1
15 2 Parameter 2
The type is 0,1,2 or 3 for a Program, Number array, Character array or
Code file. A screen$ file is regarded as a Code file with start
address 16384 and length 6912 decimal. If the file is a Program file,
parameter 1 holds the autostart line number (or a number >=32768 if no
LINE parameter was given) and parameter 2 holds the start of the
variable area relative to the start of the program. If it's a Code
file, parameter 1 holds the start of the code block when saved, and
parameter 2 holds 32768. For data files finally, the byte at position
14 decimal holds the variable name.
=cut
use strict ;
use warnings ;
umask 011 ;
# ____________________________________________________________________________
my @object_list = () ;
my %option = (
shorthelp => 0,
help => 0,
showcat => 0,
showdeleted => 0,
showbad => 1,
sectors => 0,
noautorun => '',
autorun => '',
line => 0,
get => '',
getall => 0,
put => '',
verbose => 0,
fix => 0,
create => 0,
cartridge => '',
tape => '',
dump => '',
dump32 => '',
dumpblock => '',
text => '',
out => '',
rename => '',
to => '',
copy => '',
erase => '',
unerase => '',
label => '',
) ;
for my $switch ( @ARGV ) {
if ( $switch =~ /^-\w+$/ ) {
my @switch = split(//, $switch) ;
for my $ch ( @switch ) {
$option{ help } = 1 if $ch =~ /h/ ;
$option{ showcat } = 1 if $ch =~ /l/ ;
$option{ showdeleted } = 1 if $ch =~ /f/ ;
$option{ showbad } = 0 if $ch =~ /b/ ;
$option{ sectors } = 1 if $ch =~ /s/ ;
$option{ noautorun } = '*' if $ch =~ /r/ ;
$option{ getall } = '*' if $ch =~ /g/ ;
$option{ put } = '*' if $ch =~ /p/ ;
$option{ verbose } = 1 if $ch =~ /v/ ;
$option{ fix } = 1 if $ch =~ /x/ ;
$option{ create } = 1 if $ch =~ /c/ ;
$option{ shorthelp } = 1 unless $ch =~ /[-hlfbsrgpvxc]/ ; # catch-all
}
}
# switch key=[list]
elsif ( $switch =~ m/^(\w+)=\[(\S+)\]\s*$/ ) {
my ( $key, $value ) = ( $1, $2 ) ; #'''
my @ary = split /[,;]/, $value ;
$option{ $key } = \@ary ;
}
# switch key="value"
elsif ( $switch =~ m/^(\w+)=(\S+)\s*$/ ) {
my ( $key, $value ) = ( $1, $2 ) ; #'''
$option{ $key } = $value ;
}
# switch key="value"
elsif ( $switch =~ m/^(\w+)=(.+)\s*$/ ) {
my ( $key, $value ) = ( $1, $2 ) ; #'''
$option{ $key } = $value ;
}
# the first of the remaining parameters is intended as cartridge filename
else {
if ( $option{ cartridge } ) {
push @object_list, $switch ;
}
else {
$option{ cartridge } = $switch ;
}
}
}
# ____________________________________________________________________________
$option{ dump32 } = $option{ dumpblock } if $option{ dumpblock } ;
$option{ dump } = $option{ dump32 } if $option{ dump32 } ;
# ____________________________________________________________________________
if ( $option{ text2tap } && $option{ tape } ) {
text_to_tape( $option{ text2tap }, $option{ tape } ) ;
}
# ____________________________________________________________________________
if ( $option{ cartridge } && scalar(@ARGV) == 1 ) {
$option{ showcat } = 1 ;
}
# ____________________________________________________________________________
if ( $option{ cartridge } && $option{ cartridge } !~ m/\.mdr$/i ) {
$option{ cartridge } .= '.mdr' ;
}
# ____________________________________________________________________________
if ( $option{ shorthelp } or ( !$option{ cartridge } and not $option{ help } ) ) {
print qq(
Syntax: perl mdr.pl [switch] <cartridge> [key=value ...]
Help: perl mdr.pl -h
) ;
exit ;
}
# ____________________________________________________________________________
if ( $option{ help }) {
print qq(
mdr.pl
This is a script that allows you to manipulate .MDR cartrige files.
This script needs a .MDR filename that is read entirely at start.
Then some operations will be performed based on options given.
Usage is:
perl mdr.pl [switch] <cartridge> [key=value ...]
where [switch] can be
-h for help
-l show catalog (optional)
-f used with -l switch: Show free sectors.
-b used with -l switch: Hide bad sectors.
-s with -l show sectors used instead of file details
-r removes autorun LINE from programs in cartridge.
-v verbose, show much more details at start
-x fix bad sectors
-c create empty if not exists
-g used with tape to get all .mdr content to .tap
<cartridge> is the filename of a .MDR file.
Normally operation will be done "in-place" unless an out option is specified.
[key=value] available are listed here below
* Cartridge only options:
rename=name rename "name". New name is given via "to" option
copy=name copy "name". New name given via "to" option
to=newname destination "name" in .MDR.
erase=name erase "name" from .MDR
label=name change label of cartridge.
noautorun=name remove autorun of a single file "name" inside .MDR
autorun=name set autorun of a single file "name" inside .MDR
line=number used with autorun option to specify run
* Host file options:
out=output.MDR create a new cartridge file, original is unchanged.
* From-to cartridge options:
get=name gets a file from .MDR and write it to "host" file
dump=name same as get, but output is just a dump file
dump32=name same as dump, but adds/takes a new-line every 32 characters.
dumpblock=name same as dump32, but adds a "Screen#" header for each 16 rows.
text=name same as dump, but converts LF to CR, or CR+LF to CR.
put=name reads "host" files and creates files to .MDR
Examples:
-l xyz.mdr noautorun=run remove autorun from "run"
-l xyz.mdr erase=run erase "run" file
-l xyz.mdr rename=abc to=ABC rename "abc" to "ABC"
-l xyz.mdr -p tape=afile.tap put afile.tap content to mdr
-l xyz.mdr put=f1 host=file1.bin copy host file1.bin to mdr "f1"
-l xyz.mdr -g tape=afile.tap get mdr content to afile.tap
-l xyz.mdr get=prnt dump=prnt.txt get prnt file from mdr to dump
\n) ;
exit ;
}
# ____________________________________________________________________________
# mdr style checksum
sub checksum {
my $string = shift ;
my $length = shift ;
my $sum = 0 ;
for my $ch ( unpack("C$length", $string) ) {
$sum += $ch ;
$sum %= 255 ;
}
return $sum ;
}
# ____________________________________________________________________________
# tape style checksum
sub checkbittoggle {
my $string = shift ;
my $length = shift ;
my $sum = 0 ;
for my $ch ( unpack("C$length", $string) ) {
$sum ^= $ch ;
}
return $sum ;
}
# ____________________________________________________________________________
my $whole = '' ;
my $record = { } ;
my $readonly = 0 ;
my $unformatted = 0;
my $free = { } ;
my %cart_label = () ;
my %cart_catalog = () ;
my %cart_cat_sec = () ;
my %cart_unusable = () ;
my $master_label = '' ;
my $master_count = 0 ;
my $SSIZE = 543 ; # constant
my $realsize = 0;
my $max_hdnumb = 0 ;
# ____________________________________________________________________________
# find first free sector.
my $current_head = 0 ;
sub first_free {
my $pass = 0 ;
my $i = $current_head ;
while ( 0 == $pass || $i != $current_head ) {
my $key = sprintf( "%03d", $i ) ;
if ($record->{ $key }->{ unusable } ) {
$i -= 1 ;
} else {
if ( $record->{ $key }->{ empty } ) {
$current_head = $i - 2; # motor latency
$current_head += $max_hdnumb if $current_head < 1 ;
return $key ;
}
$i -= 1 ;
if ( $i < 1 ) {
$pass++ ;
$i = $max_hdnumb ;
}
}
}
die "Microdrive full" ;
return $max_hdnumb + 1 ; # MDR is full
}
# ____________________________________________________________________________
sub slurp {
open ( MDR, '<', $option{ cartridge } ) || warn "Cannot open mdr $option{ cartridge } " ;
$realsize = sysread( MDR, $whole, 254 * $SSIZE + 1 ) ;
close MDR ;
}
# ____________________________________________________________________________
sub burp {
open ( MDR, '>', $option{ out } ) || die "Cannot write mdr $option{ out } " ;
syswrite( MDR, $whole, $realsize ) ;
close MDR ;
}
# ____________________________________________________________________________
# change label of mdr. Label is repeated in each sector.
sub new_label {
my $newname = shift ;
for ( my $i = 0 ; $i < 254 ; ++$i ) {
my $offset = $i * $SSIZE ;
my $header = substr( $whole, $offset, 15 ) ; #sysread( MDR, $sector, 15 + 528 ) ;
my ( $hdflag, $hdnumb, $ntused, $hdname, $hdchk ) = unpack( 'CC S A10 C', $header ) ;
$hdname = $newname . (' ' x 10);
$header = pack( 'CC S A10', $hdflag, $hdnumb, $ntused, $hdname ) ;
$hdchk = checksum( $header, 14 ) ;
$header = pack( 'CC S A10 C', $hdflag, $hdnumb, $ntused, $hdname, $hdchk ) ;
substr( $whole, $offset, 15 ) = $header ;
}
$option{ out } = $option{ cartridge } unless $option{ out };
}
# ____________________________________________________________________________
sub analyze {
my $offset = 0 ;
for ( my $i = 0 ; $i < 254 ; ++$i ) {
$offset = $i * $SSIZE ;
my $sector = substr( $whole, $offset, $SSIZE ) ; #sysread( MDR, $sector, 15 + 528 ) ;
if ( $offset + 1 >= $realsize || length( $sector ) < 15 + 528 ) {
last ;
}
my ( $hdflag, $hdnumb, $ntused, $hdname, $hdchk ) = unpack( 'CC S A10 C' , $sector ) ;
my ( $recflg, $recnum, $reclen, $recnam, $deschk ) = unpack( 'x15 CC S A10 C', $sector ) ;
my $data = substr( $sector, 30, 512 ) ;
my $dchk = unpack( "x15 x15 x512 C", $sector ) ;
my $eofblk = ($recflg & 2) ? 1 : 0 ;
my $empty = (0 == ($recflg & 2) and 0 == ($reclen & 512)) ? 1 : 0 ;
my $fhdchk = checksum( substr( $sector, 0, 14), 14) ;
my $fdeschk = checksum( substr( $sector, 15, 14), 14) ;
my $fdchk = checksum( substr( $sector, 30,512),512) ;
my $noise = checkbittoggle( substr( $sector, 30,512),512) ;
my $unusable = ( 255 == $hdnumb || ($fdchk != $dchk && 0 == $noise ) ) ? 1 : 0 ; # or ($eofblk and ( 0 == $reclen || $reclen > 512 ) ) # || ($fdchk != $dchk && $dchk
if ( $fhdchk != $hdchk || $fdeschk != $deschk || $fdchk != $dchk ) {
if ( $option{ fix } ) {
if ( $fhdchk != $hdchk ) {
$hdchk = $fhdchk ;
substr( $sector, 14, 1 ) = pack( 'C', $hdchk ) ;
}
if ( $fdeschk != $deschk ) {
$deschk = $fdeschk ;
substr( $sector, 15 + 14 , 1 ) = pack( 'C', $deschk ) ;
}
if ( $fdchk != $dchk && $unusable ) {
$dchk = $fdchk ;
substr( $sector, 15 +15 + 512, 1 ) = pack( 'C', $dchk ) ;
}
substr( $whole, $offset, $SSIZE ) = $sector ;
$option{ out } = $option{ cartridge } unless $option{ out };
}
unless ( $unusable && !$option{ verbose }) {
warn "hdnumb $hdnumb | recnum $recnum | bad hdchk checksum expected $fhdchk found $hdchk \n" if $fhdchk != $hdchk ;
warn "hdnumb $hdnumb | recnum $recnum | bad deschk checksum expected $fdeschk found $deschk\n" if $fdeschk != $deschk ;
warn "hdnumb $hdnumb | recnum $recnum | bad dchk checksum expected $fdchk found $dchk \n" if $fdchk != $dchk && !($empty or $unusable) ;
warn "Invalid checksum. Unformatted cartridge\n" if ( $dchk == 255 || $deschk == 255 || $dchk == 255 ) ;
}
}
# offset to sector
printf ( "%5Xh : ", $SSIZE * $i ) if $option{ verbose } ;
# any checksum will never be 255
if ( $dchk == 255 || $deschk == 255 || $dchk == 255 || $hdnumb == 255 ) {
unless ( $unusable ) {
print "Unformatted" if $option{ verbose } ;
$unformatted++ ;
}
}
# header
if ( $option{ verbose } ){
printf ( "%03d : ", $hdnumb ) ;
printf ( "%02X %02X %04X %-10s : ", $hdflag, $hdnumb, $ntused, $hdname ) ;
# record
# $recnam = substr( $sector, 20, 10 ) unless $recnam ;
printf ( "%03d : ", $recnum ) ;
printf ( "%02X %02X %04X %-10s", $recflg, $recnum, $reclen, $recnam ) ;
# flags meaning
if ( $unusable ) {
print ": unusable "
}
else {
print ": empty " if !($recflg & 2) && ($reclen==0 ) ;
print ": not used " if !(($recflg & 2) || ($reclen==512));
print ": PRN" if !($recflg & 4) ;
print ": EOF" if ($recflg & 2) ;
# show wrong checksum
printf ( "[%02X!%02x]", $fhdchk, $hdchk ) if $fhdchk != $hdchk ;
printf ( "[%02X!%02x]", $fdeschk, $deschk) if $fdeschk != $deschk ;
printf ( "[%02X!%02x]", $fdchk, $dchk ) if $fdchk != $dchk ;
}
# show first 32 bytes data dump
1;
printf ( ": " . ("%02X" x 8 . ' ') x 4, unpack("C32", substr( $data,0,32 ) ) ) if 2 == $option{ verbose } ;
printf ( ": " . ("%02X" x 8 . ' ') x 64, unpack("C512", substr( $data,0,512 ) ) ) if 3 == $option{ verbose } ;
##print substr( $data,0,32 ) ;
}
# collect header name, catalog name etc.
if ( $unusable ) {
push @{ $cart_unusable{ $hdnumb } }, sprintf( "%03d", $hdnumb ) ;
}
elsif ( $empty ) {
my $key = sprintf( '%-10s', $recnam ) . '-' ;
$cart_catalog{ $key } += $reclen ;
push @{ $cart_cat_sec{ $key } }, sprintf( "%03d", $hdnumb ) ;
$cart_label{ $hdname } ++ ;
}
else {
$cart_catalog{ $recnam } += $reclen ;
push @{ $cart_cat_sec{ $recnam } }, sprintf( "%03d", $hdnumb ) ;
$cart_label{ $hdname } ++ ;
}
print "\n" if $option{ verbose } ;
my $temp = {
hdflag => $hdflag ,
hdnumb => $hdnumb ,
ntused => $ntused ,
hdname => $hdname ,
hdchk => $hdchk ,
recflg => $recflg ,
recnum => $recnum ,
reclen => $reclen ,
recnam => $recnam ,
deschk => $deschk ,
data => $data ,
dchk => $dchk ,
fhdchk => $fhdchk ,
fdeschk => $fdeschk ,
fdchk => $fdchk ,
eofblk => $eofblk ,
empty => $empty ,
unusable=> $unusable,
sector => $sector ,
offset => $offset
} ;
$record->{ sprintf( "%03d", $hdnumb ) } = $temp ;
$max_hdnumb = $hdnumb if $max_hdnumb < $hdnumb ;
$current_head = $max_hdnumb ;
1 ; # breakpoint
}
$readonly = substr( $whole, $offset, 1 ) ; # sysread( MDR, $readonly, 1 ) ;
$readonly = unpack( 'C', $readonly ) ;
for my $label (sort keys %cart_label) {
if ( $master_count < $cart_label{$label} ) {
$master_label = $label ;
$master_count = $cart_label{$label} ;
}
}
1;
}
# ____________________________________________________________________________
sub showcat {
print "\n";
printf ( "Header: \"%-10s\"\n", $master_label ) ;
if ( scalar( keys %cart_label ) > 1 ) {
for my $label (sort keys %cart_label) {
printf ( " \"%-10s\" (%d times)\n", $label, $cart_label{$label} ) ;
}
}
my $fmt = "%-4s %7d %-11s " ;
print "\n";
print "\n";
print "Type Bytes Filename ";
print "Sector used\n" if $option{ sectors } ;
print "____ _______ ___________ _______________________________________________\n" if $option{ sectors } ;
print " Addr. Actual Vars Line\n" unless $option{ sectors } ;
print "____ _______ ___________ ______ ______ ______ ______\n" unless $option{ sectors } ;
print "\n";
my $used = 0 ;
my $deleted = 0 ;
my $bad = 0 ;
for my $name (sort keys %cart_catalog) {
next unless $name ;
my $size = $cart_catalog{$name} ;
next unless $size ;
$used += $size ;
my $first_record = -1 ;
my @temp = @{ $cart_cat_sec{ $name } } ;
for my $nrec ( @temp ) {
$first_record = $nrec if 0 == $record->{ $nrec }->{ recnum } ;
}
my @list = sort { $b cmp $a } @temp ;
my $reclen = $record->{ $first_record }->{ reclen } || 0 ;
my $recflg = $record->{ $first_record }->{ recflg } || -1 ;
my $data = $record->{ $first_record }->{ data } || '00000000' ;
my @detail = unpack("CSSSS", substr( $data,0,9 ) ) ;
my $type = ( $reclen && !($recflg & 4)) ? 'Prnt' : 'Norm' ;
if ( $first_record == -1 ) {
$type = 'Bad!' ;
$detail[1] = 512 * scalar( @temp ) ;
}
$type = "Prog" if $type eq 'Norm' && 0 == $detail[0] ;
$type = "Numb" if $type eq 'Norm' && 1 == $detail[0] ;
$type = "Char" if $type eq 'Norm' && 2 == $detail[0] ;
$type = "Code" if $type eq 'Norm' && 3 == $detail[0] ;
$type = "Scrn" if $type eq 'Code' && $detail[1] == 6912 && $detail[2] == 16384 ;
$type = 'Prnt' if $type eq 'Norm' ; # default ?
printf ( $fmt, $type, $detail[1], $name ) if $type ne 'Prnt';
printf ( $fmt, $type, $size , $name ) if $type eq 'Prnt';
if ( $option{ sectors } or $first_record == -1 ) {
for ( my $i = 0 ; $i <= $#list ; $i++ ) {
print ',' if $i > 0 ;
print $list[$i] if $i <= 7 ;
print ' ... ' if $i == 7 && $#list > 7 ;
last if $i == 7 && $#list > 7 ;
}
}
elsif ( $list[0] ) {
my $data = $record->{ $list[0] }->{ data } ;
printf ( '%6d %6d %+6d %6s', $detail[2], $size, $detail[3], ($detail[4]<32768?$detail[4]:'') ) if $type eq "Prog" ;
printf ( '%6d %6d %6s', $detail[2], $size, chr(127&$detail[3]+64).'()' ) if $type eq "Numb" ;
printf ( '%6d %6d %6s', $detail[2], $size, chr(127&$detail[3]) .'$()' ) if $type eq "Char" ;
printf ( '%6d %6d', $detail[2], $size ) if $type eq "Code" ;
printf ( '%6d %6d', $detail[2], $size ) if $type eq "Scrn" ;
# printf ( ' ' x 14 . '%02X ' x 16, unpack("C16", substr( $data,0,16 ) ) ) if $type eq "Prnt" ;
}
print "\n" ;
}
print "\n" if $option{ showdeleted } ;
for my $name (sort keys %cart_catalog) {
my $size = $cart_catalog{$name} ;
next if $name && $size ;
next unless $option{ showdeleted } ;
my @list = sort { $b <=> $a } @{ $cart_cat_sec{ $name } } ;
$size = 512 * scalar(@list) ;
$deleted += $size ;
my $type = 'Free';
printf ( $fmt, $type, $size, $name ) ;
print " sect: " ;
for ( my $i = 0 ; $i <= $#list ; $i++ ) {
print ',' if $i > 0 ;
print $list[$i] if $i <= 7 ;
print ' ... ' if $i == 7 && $#list > 7 ;
last if $i == 7 && $#list > 7 ;
}
print "\n" ;
}
if ( $option{ showbad } ) {
print "\n" ;
for my $hdnumb (sort keys %cart_unusable) {
my $size = 512 ;
$bad += $size ;
my $recflg = 0 ;
my $type = 'Bad' ;
printf ( $fmt, $type, $size, '' ) ;
print "$hdnumb" ;
print " tape-junction" if $hdnumb == $max_hdnumb ;
print "\n" ;
}
}
my $free = 0 ;
for ( my $i = 254 ; $i >= 0 ; $i --) {
my $key = sprintf( "%03d", $i ) ;
my $empty = $record->{ $key }->{ empty } ;
my $unusable = $record->{ $key }->{ unusable } ;
$free += 512 if $empty && !$unusable ;
}
print "____ _______ ___________ ___________________________________________\n";
print "\n" ;
printf ( "$fmt %3d K\n", '', $used, 'total used ' , int(0.5+$used/1024) ) ;
printf ( "$fmt %3d K\n", '', $free, 'total erased ' , int(0.5+$free/1024) ) if $option{ showdeleted } ;
printf ( "$fmt %3d K\n", '', $bad , 'total bad ' , int(0.5+$bad /1024) ) if $option{ showbad } ;
printf ( "$fmt %3d K\n", '', $free, 'total free ' , int(0.5+$free/1024) ) ;
print "\n" ;
}
# ____________________________________________________________________________
# erase a single filename from .MDR
sub erasefile {
my $name = shift ;
unless ( defined $cart_catalog{ $name } && $cart_catalog{$name} > 0 ) {
warn "File '$name' does not exist in cartridge " . $option{ cartridge } . "\n";
return ;
}
my @list = sort { $b <=> $a } @{ $cart_cat_sec{ $name } } ;
for ( my $i = 0 ; $i <= $#list ; $i++ ) {
my $key = $list[$i] ;
my $sector = $record->{ $key }->{ sector } ;
my ( $recflg, $recnum, $reclen, $recnam, $deschk ) = unpack( 'x15 CC S A10 C', $sector ) ;
$recflg = 0 ;
#$recnum = 1 ;
$reclen = 0 ;
$deschk = checksum( pack( 'CC S A10', $recflg, $recnum, $reclen, $recnam ) , 14 ) ;
substr( $sector, 15, 15 ) = pack( 'CC S A10 C', $recflg, $recnum, $reclen, $recnam, $deschk ) ;
substr( $sector, 30, 513) = chr(0) x 30 . chr(1) . chr(0) x 482 ;
$record->{ $key }->{ sector } = $sector ;
$record->{ $key }->{ empty } = 1 ;
substr( $whole, $record->{ $key }->{ offset }, $SSIZE ) = $sector ;
}
$option{ out } = $option{ cartridge } unless $option{ out };
}
# ____________________________________________________________________________
# copy a single filename inside .MDR to a new name
sub copyfile {
my $name = shift ;
my $to = shift ;
unless ( defined $cart_catalog{ $name } && $cart_catalog{$name} > 0 ) {
warn "File '$name' does not exist in cartridge " . $option{ cartridge } ."\n" ;
return ;
}
die "'copy' requires 'to' option" if $option{ copy } and !$option{ to } ;
my $newname = substr( sprintf( '%-10s', $to ), 0, 10) ;
if ( defined $cart_catalog{ $newname } && $cart_catalog{$newname} > 0 ) {
warn "File '$newname' already exists in cartridge " . $option{ cartridge } ."\n" ;
return ;
}
my @list = sort { # sort by recnum of each sector of file.
$record->{ $a }->{ recnum }
<=>
$record->{ $b }->{ recnum }
} @{ $cart_cat_sec{ $name } } ;
for my $h (@list) {
my $key = first_free() ;
die "Microdrive full " if $key > $max_hdnumb ;
my $sector= $record->{$key}->{ sector } ;
my ( $hdflag, $hdnumb, $ntused, $hdname, $hdchk ) = unpack( 'CC S A10 C' , $record->{$key}->{ sector } ) ;
my ( $recflg, $recnum, $reclen, $recnam, $deschk ) = unpack( 'x15 CC S A10 C', $record->{ $h }->{ sector } ) ;
my $data = substr( $record->{ $h }->{ sector }, 30, 512+1 ) ;
$recnam = $newname ;
$deschk = checksum( pack( 'CC S A10', $recflg, $recnum, $reclen, $recnam ) , 14 ) ;
substr( $sector, 15, 15 ) = pack( 'CC S A10 C', $recflg, $recnum, $reclen, $recnam, $deschk ) ;
substr( $sector, 30, 512+1 ) = $data ;
$record->{ $key }->{ sector } = $sector ;
substr( $whole, $record->{ $key }->{ offset }, $SSIZE ) = $sector ;
$record->{ $key }->{ empty } = 0 ;
}
$option{ out } = $option{ cartridge } unless $option{ out };
}
# ____________________________________________________________________________
# rename a single filename inside .MDR to a new name
sub renamefile {
my $name = shift ;
my $to = shift ;
unless( defined $cart_catalog{ $name } && $cart_catalog{$name} > 0 ) {
warn "File '$name' does not exist in cartridge " . $option{ cartridge } . "\n";
return ;
}
die "'rename' requires 'to' option" if $option{ rename } and !$option{ to } ;
my $newname = substr( sprintf( '%-10s', $to ), 0, 10) ;
if ( defined $cart_catalog{ $newname } && $cart_catalog{$newname} > 0 ) {
warn "File '$newname' already exists in cartridge " . $option{ cartridge } . "\n";
return ;
}
my @list = sort { $b <=> $a } @{ $cart_cat_sec{ $name } } ;
for ( my $i = 0 ; $i <= $#list ; $i++ ) {
my $key = $list[$i] ;
my $sector = $record->{ $key }->{ sector } ;
my ( $recflg, $recnum, $reclen, $recnam, $deschk ) = unpack( 'x15 CC S A10 C', $sector ) ;
$recnam = $newname ;
$deschk = checksum( pack( 'CC S A10', $recflg, $recnum, $reclen, $recnam ) , 14 ) ;
substr( $sector, 15, 15 ) = pack( 'CC S A10 C', $recflg, $recnum, $reclen, $recnam, $deschk ) ;
$record->{ $key }->{ sector } = $sector ;
substr( $whole, $record->{ $key }->{ offset }, $SSIZE ) = $sector ;
}
$option{ out } = $option{ cartridge } unless $option{ out };
}
# ____________________________________________________________________________
# remove autorun LINE from a single filename inside .MDR
sub noautorun {
my $name = shift ;
unless ( defined $cart_catalog{ $name } && $cart_catalog{$name} > 0 ) {
warn "File '$name' does not exist in cartridge " . $option{ cartridge } . "\n";
return ;
}
my @list = sort { $b <=> $a } @{ $cart_cat_sec{ $name } } ;
for ( my $i = 0 ; $i <= $#list ; $i++ ) {
my $key = "$list[$i]" ;
next unless 0 == $record->{ $key }->{ recnum } ;
my $sector = $record->{ $key }->{ sector } ;
my $dchk = unpack( "x15 x15 x512 C", $sector ) ;
my $data = substr( $sector, 30, 512 ) ;
my @detail = unpack("CSSSS", substr( $data,0,9 ) ) ;
next if 0 != $detail[0] && $option{ noautorun } eq '*' ;
unless ( 0 == $detail[0] ) {
warn "File '$name' is not a program in cartridge " . $option{ cartridge } . "\n";
return;
}
if ( $detail[4]<32768 ) {
substr( $data, 7, 2 ) = pack( 'S', 65535 ) ;
$dchk = checksum( $data, 512 ) ;
substr( $record->{ $key }->{ sector }, 30+7, 2 ) = pack( 'S', 65535 ) ;
substr( $record->{ $key }->{ sector }, 30+512, 1 ) = pack( 'C', $dchk ) ;
substr( $whole, $record->{ $key }->{ offset }, $SSIZE ) = $record->{ $key }->{ sector } ;
$option{ out } = $option{ cartridge } unless $option{ out } ;
print "File '$name' Autorun removed in cartridge " . $option{ cartridge } . "\n";
return ;
}
}
}
# ____________________________________________________________________________
# remove autorun LINE from a single filename inside .MDR
sub autorun {
my $name = shift ;
my $line = shift ;
unless ( defined $cart_catalog{ $name } && $cart_catalog{$name} > 0 ) {
warn "File '$name' does not exist in cartridge " . $option{ cartridge } . "\n" ;
return ;
}
die "Usage mdr.pl <cartridge.mdr> autorun=filename line=n" unless $line ;
my @list = sort { $b <=> $a } @{ $cart_cat_sec{ $name } } ;
for ( my $i = 0 ; $i <= $#list ; $i++ ) {
my $key = "$list[$i]" ;
next unless 0 == $record->{ $key }->{ recnum } ; # find first recnum
my $sector = $record->{ $key }->{ sector } ;
my $dchk = unpack( "x15 x15 x512 C", $sector ) ;
my $data = substr( $sector, 30, 512 ) ;
my @detail = unpack("CSSSS", substr( $data,0,9 ) ) ;
unless ( 0 == $detail[0] ) {
warn "File '$name' is not a program " . $option{ cartridge } . "\n";
return ;
}
substr( $data, 7, 2 ) = pack( 'S', ( $line & 65535 ) ) ; # set 4th S
$dchk = checksum( $data, 512 ) ;
substr( $record->{ $key }->{ sector }, 30+7, 2 ) = pack( 'S', $line ) ;
substr( $record->{ $key }->{ sector }, 30+512, 1 ) = pack( 'C', $dchk ) ;
substr( $whole, $record->{ $key }->{ offset }, $SSIZE ) = $record->{ $key }->{ sector } ;
$option{ out } = $option{ cartridge } unless $option{ out } ;
print "Autorun set for $name to line $line\n" ;
return ;
}
}
# ____________________________________________________________________________
# remove autorun LINE to all possible filename inside .MDR
sub multi_noautorun {
for my $name (sort keys %cart_catalog) {
next unless $name ;
my $size = $cart_catalog{$name} ;
next unless $size ;
noautorun( $name ) ;
}
$option{ noautorun } = '' ;
}
# ____________________________________________________________________________
# reads a file from MDR and returns "header" and "content"
sub getfile {
my $name = shift ;
my $data = '' ;
warn "File '$name' does not exist in cartridge " . $option{ cartridge } . "\n" unless defined $cart_catalog{ $name } && $cart_catalog{$name} > 0 ;
my @list = sort { # sort by recnum of each sector of file.
$record->{ $a }->{ recnum }
<=>
$record->{ $b }->{ recnum }
} @{ $cart_cat_sec{ $name } } ;
map { $data .= $record->{ $_ }->{ data } } @list ;
my $reclen = $record->{ $list[0] }->{ reclen } ;
my $recflg = $record->{ $list[0] }->{ recflg } ;
my @detail = unpack("CSSSS", substr( $data,0,9 ) ) ;
my $type = ( $reclen && !($recflg & 4)) ? 'Prnt' : 'Norm' ;