-
Notifications
You must be signed in to change notification settings - Fork 4
/
BeebUtils.pm
executable file
·1391 lines (1198 loc) · 36.1 KB
/
BeebUtils.pm
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
package BeebUtils;
# Beeb Utilities to manipulate MMB and SSD files
# Copyright (C) 2012 Stephen Harris
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
use warnings;
use strict;
use FileHandle;
use POSIX;
# Constants
my $SecSize=256;
my $DiskTableSize=32*$SecSize;
my $DisksPerCatalog = ($DiskTableSize/16)-1; # slot 0 isn't a real disk
my $MaxDisks = $DisksPerCatalog;
my $NumTables = 1; # Multiple tables
my $DiskCatalogueSize = 2 * $SecSize ; # DFS
my $DiskSectors = 800 ; # Only size supported! 80track single density
my $DiskSize = $DiskSectors * $SecSize;
our $MMBSize = $DisksPerCatalog*$DiskSize+$DiskTableSize;
my $CatalogueMaxFiles = 31; # Acorn maximum. Solidisk can exceed this
# but we only read that format
# Disk table values
my $DiskReadOnly = 0;
my $DiskReadWrite = 0xF;
my $DiskUnformatted = 0xF0;
my $DiskInvalid = 0xFF;
# Almost a constant. What we're gonna do here is nasty, but it means
# that any program that uses this module will automatically gain an
# optional "-f filename"
my $DEFAULT_BBC_FILE=defined $ENV{'BBC_FILE'} ? $ENV{'BBC_FILE'} : "BEEB.MMB";
our $BBC_FILE="";
# These values extracted from BASIC 2 ROM
# starting at &8071 and ending at &836C
# (see extract_tokens for how this was built)
# This is "our" so external programs can see it
our %basic_tokens;
our %extended_tokens;
# z80 uses different line definitions
# Basic 1/2/4 is <OD><lineno-high><lineno-low><len> and ends with linehi=ff
# z80 and Basic 4 windows is <FF><lineno-low><lineno-high> and ends with
# len=0 lineno=ffff
our %alt_line;
$basic_tokens{"_BASE_"}=
{
128 => ['AND',0x00], 192 => ['LEFT$(',0x00],
129 => ['DIV',0x00], 193 => ['MID$(',0x00],
130 => ['EOR',0x00], 194 => ['RIGHT$(',0x00],
131 => ['MOD',0x00], 195 => ['STR$',0x00],
132 => ['OR',0x00], 196 => ['STRING$(',0x00],
133 => ['ERROR',0x04], 197 => ['EOF',0x01],
134 => ['LINE',0x00], 198 => ['AUTO',0x10],
135 => ['OFF',0x00], 199 => ['DELETE',0x10],
136 => ['STEP',0x00], 200 => ['LOAD',0x02],
137 => ['SPC',0x00], 201 => ['LIST',0x10],
138 => ['TAB(',0x00], 202 => ['NEW',0x01],
139 => ['ELSE',0x14], 203 => ['OLD',0x01],
140 => ['THEN',0x14], 204 => ['RENUMBER',0x10],
142 => ['OPENIN',0x00], 205 => ['SAVE',0x02],
143 => ['PTR',0x43], 207 => ['PTR',0x00],
144 => ['PAGE',0x43], 208 => ['PAGE',0x00],
145 => ['TIME',0x43], 209 => ['TIME',0x00],
146 => ['LOMEM',0x43], 210 => ['LOMEM',0x00],
147 => ['HIMEM',0x43], 211 => ['HIMEM',0x00],
148 => ['ABS',0x00], 212 => ['SOUND',0x02],
149 => ['ACS',0x00], 213 => ['BPUT',0x03],
150 => ['ADVAL',0x00], 214 => ['CALL',0x02],
151 => ['ASC',0x00], 215 => ['CHAIN',0x02],
152 => ['ASN',0x00], 216 => ['CLEAR',0x01],
153 => ['ATN',0x00], 217 => ['CLOSE',0x03],
154 => ['BGET',0x01], 218 => ['CLG',0x01],
155 => ['COS',0x00], 219 => ['CLS',0x01],
156 => ['COUNT',0x01], 220 => ['DATA',0x20],
157 => ['DEG',0x00], 221 => ['DEF',0x00],
158 => ['ERL',0x01], 222 => ['DIM',0x02],
159 => ['ERR',0x01], 223 => ['DRAW',0x02],
160 => ['EVAL',0x00], 224 => ['END',0x01],
161 => ['EXP',0x00], 225 => ['ENDPROC',0x01],
162 => ['EXT',0x01], 226 => ['ENVELOPE',0x02],
163 => ['FALSE',0x01], 227 => ['FOR',0x02],
164 => ['FN',0x08], 228 => ['GOSUB',0x12],
165 => ['GET',0x00], 229 => ['GOTO',0x12],
166 => ['INKEY',0x00], 230 => ['GCOL',0x02],
167 => ['INSTR(',0x00], 231 => ['IF',0x02],
168 => ['INT',0x00], 232 => ['INPUT',0x02],
169 => ['LEN',0x00], 233 => ['LET',0x04],
170 => ['LN',0x00], 234 => ['LOCAL',0x02],
171 => ['LOG',0x00], 235 => ['MODE',0x02],
172 => ['NOT',0x00], 236 => ['MOVE',0x02],
173 => ['OPENUP',0x00], 237 => ['NEXT',0x02],
174 => ['OPENOUT',0x00], 238 => ['ON',0x02],
175 => ['PI',0x01], 239 => ['VDU',0x02],
176 => ['POINT(',0x00], 240 => ['PLOT',0x02],
177 => ['POS',0x01], 241 => ['PRINT',0x02],
178 => ['RAD',0x00], 242 => ['PROC',0x0A],
179 => ['RND',0x01], 243 => ['READ',0x02],
180 => ['SGN',0x00], 244 => ['REM',0x20],
181 => ['SIN',0x00], 245 => ['REPEAT',0x00],
182 => ['SQR',0x00], 246 => ['REPORT',0x01],
183 => ['TAN',0x00], 247 => ['RESTORE',0x12],
184 => ['TO',0x00], 248 => ['RETURN',0x01],
185 => ['TRUE',0x01], 249 => ['RUN',0x01],
186 => ['USR',0x00], 250 => ['STOP',0x01],
187 => ['VAL',0x00], 251 => ['COLOUR',0x02],
188 => ['VPOS',0x01], 252 => ['TRACE',0x12],
189 => ['CHR$',0x00], 253 => ['UNTIL',0x02],
190 => ['GET$',0x00], 254 => ['WIDTH',0x02],
191 => ['INKEY$',0x00], 255 => ['OSCLI',0x02]
};
$basic_tokens{"basic2"}={};
$extended_tokens{"basic2"}={};
# These extensions from http://mdfs.net/Docs/Comp/BBCBasic/Tokens
$basic_tokens{"basic4"}= { 206 => ['EDIT',0] };
$extended_tokens{"basic4"}={};
$basic_tokens{"z80"}= { 206 => ['PUT',0] };
$extended_tokens{"z80"}={};
$alt_line{"z80"}=1;
$basic_tokens{"arm"}=
{
127 => ['OTHERWISE',0], 204 => ['ELSE',0],
201 => ['WHEN',0], 205 => ['ENDIF',0],
202 => ['OF',0], 206 => ['ENDWHILE',0],
203 => ['ENDCASE',0]
};
$extended_tokens{"arm"}{198}={ 142 => ['SUM',0], 143 => ['BEAT',0] };
$extended_tokens{"arm"}{199}=
{
142 => ['APPEND',0], 151 => ['NEW',0],
143 => ['AUTO',0], 152 => ['OLD',0],
144 => ['CRUNCH',0], 153 => ['RENUMBER',0],
145 => ['DELETE',0], 154 => ['SAVE',0],
146 => ['EDIT',0], 155 => ['TEXTLOAD',0],
147 => ['HELP',0], 156 => ['TEXTSAVE',0],
148 => ['LIST',0], 157 => ['TWIN',0],
149 => ['LOAD',0], 158 => ['TWINO',0],
150 => ['LVAR',0], 159 => ['INSTALL',0]
};
$extended_tokens{"arm"}{200}=
{
142 => ['CASE',0], 155 => ['LIBRARY',0],
143 => ['CIRCLE',0], 156 => ['TINT',0],
144 => ['FILL',0], 157 => ['ELLIPSE',0],
145 => ['ORIGIN',0], 158 => ['BEATS',0],
146 => ['POINT',0], 159 => ['TEMPO',0],
147 => ['RECTANGLE',0], 160 => ['VOICES',0],
148 => ['SWAP',0], 161 => ['VOICE',0],
149 => ['WHILE',0], 162 => ['STEREO',0],
150 => ['WAIT',0], 163 => ['OVERLAY',0],
151 => ['MOUSE',0], 164 => ['MANDEL',0],
152 => ['QUIT',0], 165 => ['PRIVATE',0],
153 => ['SYS',0], 166 => ['EXIT',0],
154 => ['INSTALL',0]
};
$basic_tokens{"b4w"}=
{
1 => ['CIRCLE',0], 198 => ['SUM',0],
2 => ['ELLIPSE',0], 199 => ['WHILE',0],
3 => ['FILL',0], 200 => ['CASE',0],
4 => ['MOUSE',0], 201 => ['WHEN',0],
5 => ['ORIGIN',0], 202 => ['OF',0],
6 => ['QUIT',0], 203 => ['ENDCASE',0],
7 => ['RECTANGLE',0], 204 => ['ELSE',0],
8 => ['SWAP',0], 205 => ['ENDIF',0],
9 => ['SYS',0], 206 => ['ENDWHILE',0],
10 => ['TINT',0],
11 => ['WAIT',0],
12 => ['INSTALL',0],
14 => ['PRIVATE',0],
15 => ['BY',0],
16 => ['EXIT',0],
};
$extended_tokens{"b4w"}={};
$alt_line{"b4w"}=1;
my $SOLIDISK=0;
my $WATFORD=0;
my $OPUS=0;
my $DISK_DOCTOR=0;
sub init(@)
{
my (@arg)=@_;
if (@arg >=2 && $arg[0] eq '-f')
{
$BBC_FILE=$arg[1];
shift @arg;
shift @arg;
}
elsif (@arg && $arg[0] =~ /^-f(.+)$/)
{
$BBC_FILE=$1;
shift @arg;
}
elsif (@arg==1 && $arg[0] eq '-f')
{
die "Missing filename argument to -f\n";
}
# We need to check the MMB file to see if it's an extended one
# and if so, set NumTables and MaxDisks
# An entry of 0xA# at offset 8 in the table (previously unused)
# means there's # additional tables
my $fname=$BBC_FILE;
$fname=$DEFAULT_BBC_FILE unless $fname;
my $fh = new FileHandle("+< $fname");
if ($fh)
{
binmode($fh);
sysseek($fh,8,0);
my $ext_byte;
sysread($fh,$ext_byte,1);
close($fh);
$ext_byte=ord($ext_byte);
if ($ext_byte > 160 && $ext_byte < 176)
{
# We have 1-15 catalogues
$NumTables = $ext_byte-159;
$MaxDisks = $NumTables * $MaxDisks;
}
}
return(@arg);
}
sub init_ssd(@)
{
my (@arg)=@_;
if (@arg)
{
my $f=$arg[0];
if ($f=~/^([^:]+):(.+)$/)
{
$f=$2;
check_and_set_type($1);
}
$BBC_FILE=$f;
shift @arg;
}
return (@arg);
}
my $file_handle=undef;
# Open BBC_FILE if it's not already open
sub OpenFile()
{
return if $file_handle;
$BBC_FILE=$DEFAULT_BBC_FILE unless $BBC_FILE;
die "$BBC_FILE is not a file!\n" unless -f $BBC_FILE;
$file_handle = new FileHandle("+< $BBC_FILE");
die "Could not open $BBC_FILE: $!\n" unless $file_handle;
binmode($file_handle);
return;
}
sub LoadDiskTable(;$)
{
my ($extent)=@_;
$extent=0 unless $extent;
my ($disktable,$thistable);
OpenFile;
sysseek($file_handle,$extent*$MMBSize,0);
sysread($file_handle,$disktable,$DiskTableSize);
# Add any secondary catalogues
foreach my $i (1..$NumTables-1)
{
# We skip the first 16 bytes since they're unused in secondary catalogues
sysseek($file_handle,$i*$MMBSize+16,0);
sysread($file_handle,$thistable,$DiskTableSize-16);
# If the MMB is short then abort
if (length($thistable) != $DiskTableSize-16)
{
printf("Unable to read MMB Catalogue %d at location %d\n Got %d bytes but expected %d\n Is this a corrupted MMB?\n",$i,$i*$MMBSize+16,length($thistable),$DiskTableSize-16);
exit(-1);
}
$disktable .= $thistable
}
return $disktable;
}
sub SaveDiskTable($)
{
my ($disktable)=@_;
OpenFile;
my $thistable=$$disktable;
sysseek($file_handle,0,0);
syswrite($file_handle,$thistable,$DiskTableSize);
$thistable=substr($thistable,$DiskTableSize);
foreach my $i (1..$NumTables-1)
{
# We skip the first 16 bytes since they're unused in secondary catalogues
sysseek($file_handle,$i*$MMBSize+16,0);
syswrite($file_handle,$thistable,$DiskTableSize-16);
$thistable=substr($thistable,$DiskTableSize-16);
}
}
# Disk slot and new title and table
sub ChangeDiskName($$$)
{
my ($slot,$title,$disktable)=@_;
return if $slot < 0 || $slot >= $MaxDisks;
$title.=("\0"x15); $title=substr($title,0,15);
substr($$disktable,$slot*16+16,15)=$title;
}
# Delete slot
sub DeleteSlot($$$)
{
my ($slot,$restore,$disktable)=@_;
return if $slot < 0 || $slot >= $MaxDisks;
substr($$disktable,$slot*16+16+15,1)=chr($restore?$DiskReadWrite:$DiskUnformatted);
}
# Lock/Unlock a slot
sub lock_disk($$$)
{
my ($slot,$lock,$disktable)=@_;
return if $slot < 0 || $slot >= $MaxDisks;
substr($$disktable,$slot*16+16+15,1)=chr($lock?$DiskReadOnly:$DiskReadWrite);
}
# disk is disk slot on the MMB (0->$MaxDisks-1)
# disktable is a reference to a already loaded disktable
# eg
# my $disktable=LoadDiskTable;
# my ($title,$type)=GetDskName(10,\$disktable);
sub GetDskName($$)
{
my ($disk,$disktable)=@_;
my $offset=$disk*16+16;
my $title=substr($$disktable,$offset,12); $title =~ s/\0.*$//;
# Convert any non-ascii character to a '?'
$title =~ s/[\x01-\x1f\x7f-\xff]/?/g;
# Ensure title is 12 characters long
$title .= " "x(12-length($title));
my $type=substr($$disktable,$offset+15,1); $type=ord($type);
return ($title,$type);
}
# Returns a simple hashref of boot image and the disk catalog
sub load_onboot(;$)
{
my ($extent)=@_;
my $disktable=LoadDiskTable($extent);
my %boot;
foreach (0..3)
{
$boot{$_}=ord(substr($disktable,$_,1))+ord(substr($disktable,$_+4,1))*256;
}
return ($disktable,%boot);
}
sub save_onboot($%)
{
my ($disktable,%boot)=@_;
foreach (0..3)
{
substr($$disktable,$_,1)=chr($boot{$_} & 0xff);
substr($$disktable,$_+4,1)=chr(($boot{$_} & 0xff00) >> 8);
}
SaveDiskTable($disktable);
}
sub DiskPtr($;$)
{
my ($DiskNo,$Sec)=@_;
$Sec=0 unless $Sec;
# If the image is in a secondary catalogue then we need to add extra
# DiskTableSize offsets
my $offset=1+int($DiskNo/$DisksPerCatalog);
return ($DiskTableSize*$offset)+($DiskNo*$DiskSize)+($Sec*$SecSize);
}
sub BootOpt($)
{
my ($bytOption)=@_;
if ($bytOption == 0) { return 'None'; }
if ($bytOption == 1) { return 'LOAD'; }
if ($bytOption == 2) { return 'RUN'; }
if ($bytOption == 3) { return 'EXEC'; }
return "";
}
sub DiskSize($)
{
my ($size)=@_;
# If we want more detail, comment this line out...
return int($size/4) . "K";
if ($size == 0x190) { return "100K - 40x10 - SD"; }
if ($size == 0x280) { return "160K - 40x16 - DD"; }
if ($size == 0x2d0) { return "180K - 40x18 - DD"; }
if ($size == 0x320) { return "200K - 80x10 - SD"; }
if ($size == 0x500) { return "320K - 80x16 - DD"; }
if ($size == 0x5a0) { return "360K - 80x18 - DD"; }
return int($size/4) . "K";
}
# Loads the main catalog from the MMB file
# (optionally pass an already loaded disktable)
sub load_dcat(;$)
{
my ($tbl)=@_;
my ($disktable,%disk);
if ($tbl)
{
$disktable=$$tbl;
}
else
{
$disktable=LoadDiskTable;
}
foreach (0..$MaxDisks-1)
{
$disk{$_}{ValidDisk}=0;
$disk{$_}{Formatted}=0;
$disk{$_}{ReadOnly}=0;
$disk{$_}{DiskTitle}="";
my ($title,$type)=GetDskName($_,\$disktable);
if ($type == $DiskReadOnly || $type == $DiskReadWrite)
{
$disk{$_}{ValidDisk}=1;
$disk{$_}{Formatted}=1;
$disk{$_}{ReadOnly}=($type == $DiskReadOnly)?1:0;
$disk{$_}{DiskTitle}=$title;
}
elsif ($type == $DiskUnformatted)
{
$disk{$_}{ValidDisk}=1;
$disk{$_}{DiskTitle}=$title;
}
}
return %disk;
}
sub blank_mmb()
{
my $image="\0" x $MMBSize;
substr($image,0,4)="\0\1\2\3"; # Default onboot disks
foreach (1..$DisksPerCatalog)
{
substr($image,$_*16+15,1)=chr($DiskUnformatted);
}
return($image);
}
sub blank_ssd()
{
my $image="\xE5" x $DiskSize;
substr($image,0,512)="\x0" x 512;
substr($image,0x104,4)="\x01\x00\x03\x20"; # 200K disk
return($image);
}
# Reads an SSD image from an MMB
sub read_ssd(;$)
{
my ($disk)=@_;
OpenFile;
sysseek($file_handle,DiskPtr($disk),0) if defined($disk);
my $image;
sysread($file_handle,$image,$DiskSize);
# Ensure the image is at least the right size
die "Image is too small; not even 2 sectors!\n" if length($image)<512;
$image .= blank_ssd();
$image=substr($image,0,$DiskSize);
return($image);
}
# Reads an SSD from an external file
sub load_external_ssd(;$$)
{
my ($fname,$size_check)=@_;
my $target=$fname||$BBC_FILE;
die "$target is not a file!\n" unless -f $target || $target eq '-';
my $f=new FileHandle "<$target";
die "Could not open $target: $!\n" unless $f;
binmode($f);
my $image;
# We won't read more than 400K, regardless
sysread($f,$image,409600);
if ($size_check && length($image)> $DiskSize)
{
die "File $target is over $DiskSize in size\n";
}
close($f);
# Ensure the image is at least the right size
die "Image is too small; not even 2 sectors!\n" if length($image)<512;
if (length($image) < $DiskSize)
{
$image .= blank_ssd();
$image=substr($image,0,$DiskSize);
}
return($image);
}
sub put_ssd($$)
{
my ($image,$disk)=@_;
die "$disk is greater than maximum allowed of " . ($MaxDisks-1) . " for this MMB \n" if $disk >= $MaxDisks;
OpenFile;
sysseek($file_handle,DiskPtr($disk),0);
syswrite($file_handle,$image,$DiskSize);
}
sub write_ssd($$)
{
my ($image,$filename)=@_;
my $fh=new FileHandle ">$filename";
die "Can not open $filename for saving\n" unless $fh;
binmode($fh);
print $fh $$image;
close($fh);
}
sub set_ssd_title($$)
{
my ($image,$title)=@_;
$title .= ("\0"x12);
my $t1=substr($title,0,8);
my $t2=substr($title,8,4);
# If Solidisk secondary catalog then cut down title by 2
my @b=unpack("C",substr($$image,0x102,1));
$t2=substr($t2,0,2) if ($b[0] & 192) == 192;
substr($$image,0,8)=$t1;
substr($$image,0x100,length($t2))=$t2;
}
# Reads a standard BBC DFS catalogue.
# $image is a ref to a loaded disk image
# $start is a sector offset into the image to find the catalog
sub _read_ssd_cat($$)
{
my ($image,$start)=@_;
my $offset=$start*256;
my %files;
# BBC disk format: First 8 bytes are part of the disk title; null terminated
my $disk_title=substr($$image,$offset,8);
# We'll grab the filecount from sector 1 'cos that makes it easier...
my $filecount=ord(substr($$image,$offset+256+5,1))/8;
# Next lot of 8 data are filenames
foreach (0..$filecount-1)
{
my $t=substr($$image,$offset+$_*8+8,7); $t=~s/ .*$//;
my $d=substr($$image,$offset+$_*8+8+7,1); $d=ord($d);
my $locked=($d>127)?1:0;
$d=chr($d%128);
$t="$d.$t"; $t=~s/\0.*$//;
$files{$_}{locked}=$locked;
$files{$_}{name}="$t";
$files{$_}{cat_sector}=$start;
}
# Second sector.
# First 4 bytes are also part of the title
my $t2=substr($$image,$offset+256,4); $disk_title .= $t2;
# Disk title is null terminated. But quick check to see if's a solidisk
# chained catalogue. No one should use high-bit characters in disk
# titles, so this is relative safe to do.
my $chained;
if ($SOLIDISK)
{
my @b=unpack("C2",substr($disk_title,10,2));
if (($b[0] & 192) == 192)
{
$chained=($b[0]&63)*256+$b[1];
$disk_title=substr($disk_title,0,10);
}
}
$disk_title=~s/\0.*$//;
# next byte is BCD cycle
my $cycle=substr($$image,$offset+256+4,1); $cycle=ord($cycle);
$cycle=int($cycle/16)*10+($cycle%16);
# (We'd already got the filecount, earlier)
# Now the last next two bytes encode both *OPT4 value and disk size
my @b=unpack("C2",substr($$image,$offset+256+6,2));
my $opt4=int($b[0]/16) & 3;
# Really this should only be &3 but DD disks need 11 bits, so...
# (relatively safe to do everywhere - bit 3 was unused, otherwise)
my $disk_size=($b[0]&7)*256+$b[1];
# Now we have entries for load/exec/size/start-sec
# LL LL EE EE SS SS XX YY
# Load Exec Size Sector
# The first 6 bytes are load/exec/size bottom 16 bits.
# The 8th byte encodes the low 8 bits for start-sec
# The 7th byte is special. For Acorn DFS:
# bits 0+1 are high bits of sector start
# bits 2+3 are high bits of load address
# bits 4+5 are high bits of size
# bits 6+7 are high bits of exec address
#
# That makes 10 bits for start sector and 18 bits for size.
#
# But on a 320K disk you need 11 bits and 19 bits. So Solidisk steals
# bits 2 and 3 ("load address"). bit 2 is added to the sector, bit 3 to
# the size. This means we have now only have 8 bits for the load address.
# Solidisk re-uses the exec high bits for the load high bits.
foreach (0..$filecount-1)
{
my ($load,$exec,$size,$sec);
@b=unpack("C2",substr($$image,$offset+256+$_*8+8 ,2)); $load=$b[1]*256+$b[0];
@b=unpack("C2",substr($$image,$offset+256+$_*8+8+2,2)); $exec=$b[1]*256+$b[0];
@b=unpack("C2",substr($$image,$offset+256+$_*8+8+4,2)); $size=$b[1]*256+$b[0];
# Higher bits are encoded
@b=unpack("C",substr($$image,$offset+256+$_*8+8+6,1));
my $highsec = ($b[0]&3); # bits 0 and 1
my $highload = ($b[0] & 0xc) >> 2; # bits 2 and 3
my $highsize = ($b[0] & 0x30) >> 4; # bits 4 and 5
my $highexec = ($b[0] & 0xc0) >> 6; # bits 6 and 7
if ($SOLIDISK && $disk_size == 0x500)
{
if ($highload & 1) { $highsec += 4; }
if ($highload & 2) { $highsize += 4; }
$highload=$highexec;
}
# Watford does it slightly differently and uses high bits in the
# filename which means we'll need to recalculate it.
if ($WATFORD)
{
my $name=$files{$_}{name};
@b=unpack("C2",substr($$image,$offset+$_*8+8+5,2));
if ($b[0]>127)
{
substr($name,5,1)=chr($b[0] & 127) if length($name)>5;
$highsize += 4;
}
if ($b[1]>127)
{
substr($name,6,1)=chr($b[1] & 127) if length($name)>6;
$highsec += 4;
}
$files{$_}{name}=$name;
}
$highload=0xff if $highload == 3;
$highexec=0xff if $highexec == 3;
@b=unpack("C",substr($$image,$offset+256+$_*8+8+7,1));
$sec=$b[0];
$files{$_}{load}=$load+$highload*65536;
$files{$_}{exec}=$exec+$highexec*65536;
$files{$_}{size}=$size+$highsize*65536;
$files{$_}{start}=$sec+$highsec*256;
}
# Do we have multi-catalogues? We've already checked for solidisk, earlier
# what about the others?
if ($WATFORD && $start==0)
{
if (substr($$image,512,8) eq "\xAA"x8)
{
$chained=2; # Watford DFS allows another catalogue at sector 2
}
}
if ($start == 0)
{
$files{""}{title}=$disk_title;
$files{""}{cycle}=$cycle;
$files{""}{disk_size}=$disk_size;
$files{""}{opt4}=$opt4;
}
$files{""}{filecount}=$filecount;
$files{""}{chain}=$chained;
return %files;
}
# Read disk catalgue. Follow chain if multi-catalogues
# If $0 is a reference to a BBC disk image
sub read_cat($)
{
my ($image)=@_;
# OPUS is sufficiently different...
if ($OPUS) { return read_cat_opus($image); }
my %files;
my $max_file=0;
my $chain=0;
my $deleted=0;
while ($chain ne "")
{
my %this_cat=_read_ssd_cat($image,$chain);
$files{""}{last_cat}=$chain;
if ($chain == 0)
{
foreach my $k (keys %{$this_cat{""}})
{
$files{""}{$k}=$this_cat{""}{$k};
}
}
else
{
$files{""}{filecount}+=$this_cat{""}{filecount};
}
my $nextchain=$this_cat{""}{chain} || "";
delete($this_cat{""});
foreach (sort { $a <=> $b } keys %this_cat)
{
next if $_ eq "";
# Skip deleted files from multi-catalog (directory is 0x7F
# and file is locked
if ($SOLIDISK && $this_cat{$_}{name} =~ /^\x7f\./ && $this_cat{$_}{locked})
{
$deleted++;
next;
}
# Is this a hidden file that "blocks out" data from first catalogue?
# If so, it's the end of this 2nd catalogue
if ($SOLIDISK && $this_cat{$_}{name} eq '?.???????' && $this_cat{$_}{locked} && $chain)
{
$deleted++;
next;
}
#Disk doctor?
if ($DISK_DOCTOR && $this_cat{$_}{name} eq '!.!!!!!!!' && $this_cat{$_}{locked})
{
$deleted++;
$nextchain=2 unless $chain;
next;
}
foreach my $k (keys %{$this_cat{$_}})
{
$files{$max_file}{$k}=$this_cat{$_}{$k};
}
$max_file++ unless $_ eq "";
}
$chain=$nextchain;
}
$files{""}{deleted}=$deleted;
return %files;
}
# OPUS...
sub read_cat_opus($)
{
my ($image)=@_;
# if this isn't an opus image then abort 'cos it could
# be almost anything
# The four bytes should be 20 05 a0 12
if (substr($$image,0x1000,4) ne "\x20\x05\xa0\x12")
{
die "This does not appear to be an OPUS DDOS image\n";
}
my %catstart;
foreach my $cat (0..7)
{
my @b=unpack("C2",substr($$image,0x1008+$cat*2,2));
# This is measured in tracks, so *18 for sector
$catstart{$cat}=($b[0]+256*$b[1])*18;
# printf "debug: %d at %03X\n",$cat,$catstart{$cat};
}
my %files;
my $max_file=0;
$files{""}{last_cat}=7; # DDOS always has multi-cat even if they're empty
foreach my $chain (0..7)
{
next unless $catstart{$chain};
my %this_cat=_read_ssd_cat($image,$chain*2);
if ($chain == 0)
{
foreach my $k (keys %{$this_cat{""}})
{
$files{""}{$k}=$this_cat{""}{$k};
}
$files{""}{disk_size}=0x5a0;
}
else
{
$files{""}{filecount}+=$this_cat{""}{filecount};
}
delete($this_cat{""});
foreach (sort { $a <=> $b } keys %this_cat)
{
next if $_ eq "";
$this_cat{$_}{start}+=$catstart{$chain};
foreach my $k (keys %{$this_cat{$_}})
{
$files{$max_file}{$k}=$this_cat{$_}{$k};
}
$max_file++ unless $_ eq "";
}
}
$files{""}{deleted}=0;
return %files;
}
# Reference to image; generate a CRC
sub CalcCrc($)
{
my ($image)=@_;
my $crc=0;
foreach (0..length($$image)-1)
{
$crc ^= (256*ord(substr($$image,$_,1)));
foreach my $x (0..7)
{
$crc *= 2;
if ($crc > 65535)
{
$crc-=65535; $crc ^= 0x1020;
}
}
}
return($crc);
}
# Get a file from an SSD image
# $0 is ref to an image
# $1 is the filename
# $2 is a catalogue; we'll build one if not provided
# If you provide a catalogue for a different image then you get
# all you deserve
sub ExtractFile($$;%)
{
my ($image,$filename,%cat)=@_;
my %files;
if (%cat)
{
%files=%cat;
}
else
{
%files=read_cat($image);
}
my $file=undef;
foreach (sort keys %files)
{
my $f=$files{$_}{name};
next unless $f;
$file=$_ if lc($filename) eq lc($f);
}
die "Can not find $filename in image\n" unless defined($file);
my $start=$files{$file}{start}*256;
my $size=$files{$file}{size};
return substr($$image,$start,$size);
}
# SSD image saved to current directory
sub save_all_files_from_ssd($;$)
{
my ($image,$verbose)=@_;
my %files=read_cat($image);
foreach (keys %files)
{
my $n=$files{$_}{name};
next unless $n;
my $file=ExtractFile($image,$n,%files);
my $crc=CalcCrc(\$file);
# These keep Unix filenames at least a little sane
$n=~tr/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+.!@$%,-/_/c;
$n=~s/^\$\.// unless $n eq '$.';
if ( -e $n )
{
my $n1=$n;
my $cnt=0;
while ( -e $n1 ) { $n1 = $n . "-" . ($cnt++); }
$n=$n1;
}
print "Saving $files{$_}{name} as $n\n" if $verbose;
my $fh=new FileHandle ">$n";
die "Can not open $n for saving\n" unless $fh;
binmode($fh);
print $fh $file;
close($fh);
$fh=new FileHandle ">$n.inf";
die "Can not open $n.inf for saving\n" unless $fh;
printf $fh "%-9s %6X %6X %sCRC=%04X",
$files{$_}{name},
$files{$_}{load},
$files{$_}{exec},
$files{$_}{locked}?"Locked ":"",
$crc;
close($fh);
}
}
sub add_content_to_ssd($$$$$$;$)
{
my ($image,$fname,$data,$load,$exec,$locked,$filename)=@_;
$filename=$fname unless defined($filename);
die "Bad filename for $filename ($fname)\n" unless $fname=~/^.\./ && length($fname)<=9;
my %files=read_cat($image);
die "Can not operate on large/multi-catalogue disks\n" if $files{""}{chain};
die "Can only operate on 200Kb disks\n" if $files{""}{disk_size} != 0x320;
my $cnt=$files{""}{filecount};
die "Catalogue full. Can not add $filename\n" if $cnt == 31;
# Calculate first free sector
my $first_sect=$files{0}{start};
my $len=$files{0}{size};
# If we have no files...
if ($cnt == 0)
{
$first_sect=2;
$len=0;
}
# Convert to sectors;
$len=int($len/256)+(($len%256)?1:0);