-
Notifications
You must be signed in to change notification settings - Fork 19
/
make.rb
3474 lines (3091 loc) · 121 KB
/
make.rb
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
# specialised make for Ozmoo
require 'fileutils'
require 'date'
require 'json'
$is_windows = (ENV['OS'] == 'Windows_NT')
$executables = nil
if $is_windows then
# Paths on Windows. Comment out X16 and/or MEGA65 if you don't have them installed.
$executables = {
'X16' => "C:\\ProgramsWoInstall\\x16emu\\x16emu",
'X64' => "C:\\ProgramsWoInstall\\GTK3VICE-3.7.1-win64\\bin\\x64sc.exe -autostart-warp", # -autostart-delay-random"
'X128' => "C:\\ProgramsWoInstall\\GTK3VICE-3.7.1-win64\\bin\\x128.exe -80 -autostart-delay-random",
'XPLUS4' => "C:\\ProgramsWoInstall\\GTK3VICE-3.7.1-win64\\bin\\xplus4.exe -autostart-delay-random",
'MEGA65' => "\"C:\\Program Files\\xemu\\xmega65.exe\" -syscon", # -syscon is a workaround for a serious xemu bug
'C1541' => "C:\\ProgramsWoInstall\\GTK3VICE-3.7.1-win64\\bin\\c1541.exe",
'EXOMIZER' => "C:\\ProgramsWoInstall\\Exomizer-3.1.0\\win32\\exomizer.exe",
'ACME' => "C:\\ProgramsWoInstall\\acme0.97win\\acme\\acme.exe",
'ZIP' => "\"C:\\Program Files\\7-Zip\\7z.exe\" a -bso0 -bse0",
}
$commandline_quotemark = "\""
else
# Paths on Linux. Comment out X16 and/or MEGA65 if you don't have them installed.
$executables = {
'X16' => "../x16-emulator/x16emu",
'X64' => "x64 -autostart-delay-random",
'X128' => "x128 -autostart-delay-random",
'XPLUS4' => "xplus4 -autostart-delay-random",
'MEGA65' => "xemu-xmega65 -besure",
'C1541' => "c1541",
'EXOMIZER' => __dir__ + "/exomizer/src/exomizer",
'ACME' => "acme",
'ZIP' => "zip -r",
}
$commandline_quotemark = "'"
end
# Use .ozmoorc file to override executables, contents could be e.g.
# (without the # characters):
#
# X16 = C:\myemu\x16emu
# ACME = C:\myacme\acme -v1
#
# make.rb will search for .ozmoorc in this order
# - folder in OZMOO_HOME, if defined
# - current working directory (cwd)
# - home directory ($HOME)
$settings_file = ""
if ENV.has_key?('OZMOO_HOME') then
$settings_file = ENV['OZMOO_HOME'] + '/.ozmoorc'
end
if $settings_file.empty? && File.exist?('.ozmoorc') then
$settings_file = '.ozmoorc'
end
if $settings_file.empty? && File.exist?(Dir.home + '/.ozmoorc') then
$settings_file = Dir.home + '/.ozmoorc'
end
unless $settings_file.empty?
File.foreach $settings_file do |line|
if line =~ /^\s*'?(\w+)'?\s*=>?(.*)/ then
name = $1.upcase
val = $2.chomp.strip
if name =~ /^(X16|X64|X128|XPLUS4|MEGA65|C1541|EXOMIZER|ACME|ZIP)$/ then
$executables[name] = val
end
end
end
end
$PRINT_DISK_MAP = false # Set to true to print which blocks are allocated
# Typically none should be enabled.
$GENERALFLAGS = [
# 'CHECK_ERRORS' # Check for all runtime errors, making code bigger and slower
# 'SLOW', # Remove some optimizations for speed. This makes the terp ~100 bytes smaller.
# 'NODARKMODE', # Disables darkmode support. This makes the terp ~100 bytes smaller.
# 'NOSCROLLBACK', # Disables scrollback support (MEGA65, C64, C128). This makes the terp ~1 KB smaller.
# 'REUBOOST', # Enables REU Boost (MEGA65, C64, C128). This makes the terp ~160 bytes larger.
# 'NO_DEFAULT_UNICODE_MAP' # Disables the default unicode output map, saving 83 bytes
# 'VICE_TRACE', # Send the last instructions executed to Vice, to aid in debugging
# 'TRACE', # Save a trace of the last instructions executed, to aid in debugging
# 'COUNT_SWAPS', # Keep track of how many vmem block reads have been done.
# 'TIMING', # Store the lowest word of the jiffy clock in 0-->2 in the Z-code header
# 'UNDO', # Support UNDO (using REU)
# 'X_FOR_EXAMINE', # Automatically change "x" (in a verb position) to "examine" in player input
]
# For a production build, none of these flags should be enabled.
$DEBUGFLAGS = [
# 'DEBUG', # This gives some debug capabilities, like informative error messages. It is automatically included if any other debug flags are used.
# 'VIEW_STACK_RECORDS',
# 'PRINTSPEED'
# 'BENCHMARK', # This can now be enabled with -bm
# 'VMEM_STRESS', # very slow but gives vmem a workout
# 'TRACE_FLOPPY',
# 'TRACE_VM',
# 'PRINT_SWAPS',
# 'TRACE_FLOPPY_VERBOSE',
# 'TRACE_PRINT_ARRAYS',
# 'TRACE_PROP',
# 'TRACE_READTEXT',
# 'TRACE_SHOW_DICT_ENTRIES',
# 'TRACE_TOKENISE',
# 'TRACE_HISTORY',
]
$CACHE_PAGES = 4 # Should normally be 2-8. Use 4 unless you have a good reason not to. One page will be added automatically if it would otherwise be wasted due to vmem alignment issues.
$CONFIG_TRACK = 1
MODE_P = 1
MODE_S1 = 2
MODE_S2 = 3
MODE_D2 = 4
MODE_D3 = 5
MODE_71 = 6
MODE_71D = 7
MODE_81 = 8
MODE_ZIP = 9
DISKNAME_BOOT = 128
DISKNAME_STORY = 129
DISKNAME_SAVE = 130
DISKNAME_DISK = 131
$BUILD_ID = Random.rand(0 .. 2**32-1)
$VMEM_BLOCKSIZE = 512
$ZEROBYTE = 0.chr
$EXECDIR = Dir.pwd
$SRCDIR = File.join(__dir__, 'asm')
$TEMPDIR = File.join(__dir__, 'temp')
Dir.mkdir($TEMPDIR) unless Dir.exist?($TEMPDIR)
$wrapper_labels_file = File.join($TEMPDIR, 'wrapper_labels.txt')
$wrapper_file = File.join($TEMPDIR, 'wrapper')
$labels_file = File.join($TEMPDIR, 'acme_labels.txt')
$loader_labels_file = File.join($TEMPDIR, 'acme_labels_loader.txt')
# $loader_pic_file = File.join($EXECDIR, 'loaderpic.kla')
$loader_file = File.join($TEMPDIR, 'loader')
$loader_zip_file = File.join($TEMPDIR, 'loader_zip')
$ozmoo_file = File.join($TEMPDIR, 'ozmoo')
$zip_file = File.join($TEMPDIR, 'ozmoo_zip')
$good_zip_file = File.join($TEMPDIR, 'ozmoo_zip_good')
$compmem_filename = File.join($TEMPDIR, 'compmem.tmp')
$universal_file = File.join($TEMPDIR, 'universal')
$config_filename = File.join($TEMPDIR, 'config.tmp')
$x_for_examine_releases = {
"r11-s860509" => "Trinity",
"r12-s860926" => "Trinity",
"r15-s870628" => "Trinity",
"r77-s850814" => "AMFV",
"r79-s851122" => "AMFV",
"r52-s871125" => "Zork 1 SG"
}
$trinity_releases = {
"r11-s860509" => "fddd 2058 01",
"r12-s860926" => "fddd 2048 01",
"r15-s870628" => "fd8d 2048 01"
}
$lurkinghorror_releases = {
"r203-s870506" => "",
"r219-s870912" => "",
"r221-s870918" => ""
}
$beyondzork_releases = {
"r47-s870915" => "f347 14c2 00 a6 0b 64 23 57 62 97 80 84 a0 02 ca b2 13 44 d4 a5 8c 00 09 b2 11 24 50 9c 92 65 e5 7f 5d b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1",
"r49-s870917" => "f2c0 14c2 00 a6 0b 64 23 57 62 97 80 84 a0 02 ca b2 13 44 d4 a5 8c 00 09 b2 11 24 50 9c 92 65 e5 7f 5d b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1",
"r51-s870923" => "f2a8 14c2 00 a6 0b 64 23 57 62 97 80 84 a0 02 ca b2 13 44 d4 a5 8c 00 09 b2 11 24 50 9c 92 65 e5 7f 5d b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1",
"r57-s871221" => "f384 14c2 00 a6 0b 64 23 57 62 97 80 84 a0 02 ca b2 13 44 d4 a5 8c 00 09 b2 11 24 50 9c 92 65 e5 7f 5d b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1",
"r60-s880610" => "f2dc 14c2 00 a6 0b 64 23 57 62 97 80 84 a0 02 ca b2 13 44 d4 a5 8c 00 09 b2 11 24 50 9c 92 65 e5 7f 5d b1 b1 b1 b1 b1 b1 b1 b1 b1 b1 b1"
}
$varicella_releases = {
"r1-s990831" => [
"11b48 0102 09 02",
"13577 0102 09 02",
"26a85 0102 09 02",
"27284 0102 09 02",
"27381 0102 09 02",
"268c3 0404 04 02",
"273bc 0404 04 02",
"268cc 0204 04 00"
]
}
$colour_names = {
'black' => 2,
'blk' => 2,
'red' => 3,
'green' => 4,
'grn' => 4,
'yellow' => 5,
'yel' => 5,
'blue' => 6,
'blu' => 6,
'purple' => 7,
'pur' => 7,
'violet' => 7,
'magenta' => 7,
'cyan' => 8,
'cyn' => 8,
'azure' => 8,
'turquoise' => 8,
'white' => 9,
'wht' => 9,
'orange' => 16,
'orng' => 16,
'brown' => 17,
'brn' => 17,
'lightred' => 18,
'lred' => 18,
'lgtred' => 18,
'ltred' => 18,
'darkgrey' => 19,
'dgry' => 19,
'drkgrey' => 19,
'dkgrey' => 19,
'dgrey' => 19,
'grey1' => 19,
'darkgray' => 19,
'drkgray' => 19,
'dkgray' => 19,
'dgray' => 19,
'gray1' => 19,
'mediumgrey' => 20,
'mgry' => 20,
'medgrey' => 20,
'midgrey' => 20,
'mdmgrey' => 20,
'mdgrey' => 20,
'mgrey' => 20,
'grey2' => 20,
'grey' => 20,
'mediumgray' => 20,
'medgray' => 20,
'midgray' => 20,
'mdmgray' => 20,
'mdgray' => 20,
'mgray' => 20,
'gray2' => 20,
'gray' => 20,
'lightgreen' => 21,
'lgrn' => 21,
'lgtgreen' => 21,
'ltgreen' => 21,
'lgreen' => 21,
'lightblue' => 22,
'lblu' => 22,
'lgtblue' => 22,
'ltblue' => 22,
'lblue' => 22,
'lightgrey' => 23,
'lgry' => 23,
'lgtgrey' => 23,
'ltgrey' => 23,
'lgrey' => 23,
'grey3' => 23,
'lightgray' => 23,
'lgtgray' => 23,
'ltgray' => 23,
'lgray' => 23,
'gray3' => 23,
}
$bgcol_names = [
'background', 'bg',
'backgroundcol', 'bgcol',
'backgroundcolour', 'bgcolour',
'backgroundcolor', 'bgcolor',
]
$fgcol_names = [
'foreground', 'fg',
'foregroundcol', 'fgcol',
'foregroundcolour', 'fgcolour',
'foregroundcolor', 'fgcolor',
]
$d81interleave = [
# 0:No interleave
{},
# 1: 0,1, 4,5, 8,9, 12,13, ...
{ 1 => 4, 5 => 8, 9 => 12, 13 => 16, 17 => 20, 21 => 24, 25 => 28, 29 => 32, 33 => 36, 37 => 0,
3 => 6, 7 => 10, 11 => 14, 15 => 18, 19 => 22, 23 => 26, 27 => 30, 31 => 34, 35 => 38, 39 => 2
},
# 2: 0, 4, 8, 12, ...
{ 0 => 4, 4 => 8, 8 => 12, 12 => 16, 16 => 20, 20 => 24, 24 => 28, 28 => 32, 32 => 36, 36 => 1,
1 => 5, 5 => 9, 9 => 13, 13 => 17, 17 => 21, 21 => 25, 25 => 29, 29 => 33, 33 => 37, 37 => 2,
2 => 6, 6 => 10, 10 => 14, 14 => 18, 18 => 22, 22 => 26, 26 => 30, 30 => 34, 34 => 38, 38 => 3,
3 => 7, 7 => 11, 11 => 15, 15 => 19, 19 => 23, 23 => 27, 27 => 31, 31 => 35, 35 => 39
},
]
$i81 = $d81interleave[1] # Optimal scheme for MEGA65, as far as we can tell. File copying is not done in make.rb for other platforms.
class Disk_image
def base_initialize
@reserve_dir_track = nil
@interleave = 1
@config_track = $CONFIG_TRACK
@skip_tracks = Array.new(@tracks)
offset = 0
@track_offset = @track_length.map {|len| k = offset; offset += len; k }
# puts "offset = #{@track_offset}"
@reserved_sectors = Array.new(@track_length.length, 0)
@config_track_map = []
@contents = Array.new(256 * @track_length.inject(0, :+), 0)
@storydata_start_track = 0
@storydata_end_track = 0
@storydata_blocks = 0
end
def calculate_initial_free_blocks
@free_blocks = @track_length.inject(0, :+) - @reserved_sectors.inject(0, :+)
puts "Free disk blocks at start: #{@free_blocks}" if $verbose
end
def free_blocks
@free_blocks
end
def interleave
@interleave
end
attr_accessor :interleave_scheme
# def interleave_scheme
# @interleave_scheme
# end
# def interleave_scheme = (interleave_scheme)
# @interleave_scheme = interleave_scheme
# end
def add_story_data(max_story_blocks:, add_at_end:)
max_story_blocks -= 1 if max_story_blocks % 2 != 0
story_data_length = $story_file_data.length - $story_file_cursor
num_sectors = [story_data_length / 256, max_story_blocks].min
first_story_track = 1
first_story_track_max_sectors = get_track_length(first_story_track) - get_reserved_sectors(first_story_track)
temp_sectors = num_sectors
if add_at_end then
@tracks.downto(1) do |track|
track_sectors = get_track_length(track) - get_reserved_sectors(track)
if temp_sectors > track_sectors then
temp_sectors -= track_sectors
else
first_story_track = track
first_story_track_max_sectors = temp_sectors
break
end
end
end
for track in 1 .. @tracks do
print "#{track}:" if $PRINT_DISK_MAP
if num_sectors > 0 then
reserved_sectors = get_reserved_sectors(track)
sector_count = get_track_length(track)
if track < first_story_track then
sector_count = reserved_sectors
elsif track == first_story_track
sector_count = first_story_track_max_sectors + reserved_sectors
elsif sector_count - reserved_sectors > num_sectors
sector_count = num_sectors + reserved_sectors
end
track_map = Array.new(sector_count, 0)
reserved_sectors.times do |i|
track_map[i] = 1
end
free_sectors_in_track = sector_count - reserved_sectors
last_story_sector = 0
# Find right sector.
# 1. Start at 0
# 2. Find next free sector
# 3. Decrease blocks to go. If < 0, we are done
# 4. Mark sector as used.
# 5. Add interleave, go back to 2
sector = 0
[free_sectors_in_track, num_sectors].min.times do
while track_map[sector] != 0
sector = (sector + 1) % sector_count
end
track_map[sector] = 1
allocate_sector(track, sector)
add_story_block(track, sector)
last_story_sector += 1
@free_blocks -= 1
num_sectors -= 1
@storydata_start_track = track if @storydata_start_track < 1
@storydata_end_track = track
@storydata_blocks += 1
sector = (sector + @interleave) % sector_count
end
if reserved_sectors == sector_count
@config_track_map.push 0
elsif reserved_sectors % 2 == 0 and reserved_sectors <= 6
@config_track_map.push(64 * reserved_sectors / 2 + last_story_sector)
else
puts "Incorrect number of reserved sectors on track #{track}: #{reserved_sectors}"
exit 1
end
else
@config_track_map.push 0
end # if num_sectors > 0
puts if $PRINT_DISK_MAP
end # for track
# puts num_sectors.to_s
add_directory()
@config_track_map = @config_track_map.reverse.drop_while{|i| i==0}.reverse # Drop trailing zero elements
@free_blocks
end
def config_track_map
@config_track_map
end
def set_config_data(data)
if !@is_boot_disk then
puts "ERROR: Tried to save config data on a non-boot disk."
exit 1
elsif !(data.is_a?(Array)) or data.length > 512 then
puts "ERROR: Config data array is not the right length."
exit 1
end
data[5] = @interleave
@contents[@track_offset[@config_track] * 256 .. @track_offset[@config_track] * 256 + data.length - 1] = data
end
def save
begin
diskimage_file = File.open(@diskimage_filename, "wb")
rescue
puts "ERROR: Can't open #{@diskimage_filename} for writing"
exit 1
end
diskimage_file.write @contents.pack("C*")
diskimage_file.close
end
def get_track_length(track)
@track_length[track]
end
def get_reserved_sectors(track)
@reserved_sectors[track]
end
def add_story_block(track, sector)
story_block_added = false
if $story_file_data.length > $story_file_cursor + 1
@contents[256 * (@track_offset[track] + sector) .. 256 * (@track_offset[track] + sector) + 255] =
$story_file_data[$story_file_cursor .. $story_file_cursor + 255].unpack("C*")
$story_file_cursor += 256
story_block_added = true
end
story_block_added
end
end # class Disk_image
class D64_image < Disk_image
def initialize(disk_title:, diskimage_filename:, is_boot_disk:, forty_tracks: nil, reserve_dir_track: nil)
puts "Creating disk image..." if $verbose
@disk_title = disk_title
@diskimage_filename = diskimage_filename
@is_boot_disk = is_boot_disk
@tracks = forty_tracks ? 40 : 35 # 35 or 40 are useful options
@track_length = Array.new(@tracks + 1) {|track|
track == 0 ? 0 :
track < 18 ? 21 :
track < 25 ? 19 :
track < 31 ? 18 :
17
}
# puts "Tracks: #{@track_length.to_s}"
base_initialize()
@interleave = 9
# NOTE: Blocks to skip can only be 0, 2, 4 or 6, or entire track.
@reserved_sectors[18] = reserve_dir_track ? @track_length[18] : 2 # 2: Skip BAM and 1 directory block, 19: Skip entire track
@reserved_sectors[@config_track] = 2 if @is_boot_disk and @config_track
calculate_initial_free_blocks()
# BAM
@track1800 = [
# $16500 = 91392 = 357 (18,0)
0x12,0x01, # track/sector
0x41, # DOS version
0x00, # unused
# <free sectors>,<0-7>,<8-15>,<16-?, remaining bits 0>
0x15,0xff,0xff,0x1f, # 16504, track 01 (21 sectors)
0x15,0xff,0xff,0x1f, # 16508, track 02
0x15,0xff,0xff,0x1f, # 1650c, track 03
0x15,0xff,0xff,0x1f, # 16510, track 04
0x15,0xff,0xff,0x1f, # 16514, track 05
0x15,0xff,0xff,0x1f, # 16518, track 06
0x15,0xff,0xff,0x1f, # 1651c, track 07
0x15,0xff,0xff,0x1f, # 16520, track 08
0x15,0xff,0xff,0x1f, # 16524, track 09
0x15,0xff,0xff,0x1f, # 16528, track 10
0x15,0xff,0xff,0x1f, # 1652c, track 11
0x15,0xff,0xff,0x1f, # 16530, track 12
0x15,0xff,0xff,0x1f, # 16534, track 13
0x15,0xff,0xff,0x1f, # 16538, track 14
0x15,0xff,0xff,0x1f, # 1653c, track 15
0x15,0xff,0xff,0x1f, # 16540, track 16
0x15,0xff,0xff,0x1f, # 16544, track 17
0x11,0xfc,0xff,0x07, # 16548, track 18 (19 sectors)
0x13,0xff,0xff,0x07, # 1654c, track 19
0x13,0xff,0xff,0x07, # 16550, track 20
0x13,0xff,0xff,0x07, # 16554, track 21
0x13,0xff,0xff,0x07, # 16558, track 22
0x13,0xff,0xff,0x07, # 1655c, track 23
0x13,0xff,0xff,0x07, # 16560, track 24
0x12,0xff,0xff,0x03, # 16564, track 25 (18 sectors)
0x12,0xff,0xff,0x03, # 16568, track 26
0x12,0xff,0xff,0x03, # 1656c, track 27
0x12,0xff,0xff,0x03, # 16570, track 28
0x12,0xff,0xff,0x03, # 16574, track 29
0x12,0xff,0xff,0x03, # 16578, track 30
0x11,0xff,0xff,0x01, # 1657c, track 31 (17 sectors)
0x11,0xff,0xff,0x01,0x11,0xff,0xff,0x01,
0x11,0xff,0xff,0x01,0x11,0xff,0xff,0x01,
0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0, # label (game name)
0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,
0xa0,0xa0,0x30,0x30,0xa0,0x32,0x41,0xa0,
0xa0,0xa0,0xa0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x11,0xff,0xff,0x01,0x11,0xff,0xff,0x01,
0x11,0xff,0xff,0x01,0x11,0xff,0xff,0x01,
0x11,0xff,0xff,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
]
# Create a disk image. Return number of free blocks, or -1 for failure.
for track in 36 .. 40 do
@track1800[0xc0 + 4 * (track - 36) .. 0xc0 + 4 * (track - 36) + 3] = (track > @tracks ? [0,0,0,0] : [0x11,0xff,0xff,0x01])
end
# Set disk title
c64_title = name_to_c64(disk_title)
@track1800[0x90 .. 0x9f] = Array.new(0x10, 0xa0)
[c64_title.length, 0x10].min.times do |charno|
@track1800[0x90 + charno] = c64_title[charno].ord
end
if @is_boot_disk and @config_track then
allocate_sector(@config_track, 0)
allocate_sector(@config_track, 1)
end
@free_blocks
end # initialize
private
def allocate_sector(track, sector)
print " #{sector}" if $PRINT_DISK_MAP
index1 = 4 * track
index2 = 4 * track + 1 + (sector / 8)
if track > 35 then # Use SpeedDOS 40-track BAM layout
index1 += 0x30
index2 += 0x30
end
# adjust number of free sectors
@track1800[index1] -= 1
# allocate sector
index3 = 255 - 2**(sector % 8)
@track1800[index2] &= index3
end
def add_directory()
# Add disk info and BAM at 18:00
@contents[@track_offset[18] * 256 .. @track_offset[18] * 256 + 255] = @track1800
# Add directory at 18:01
@contents[@track_offset[18] * 256 + 256] = 0
@contents[@track_offset[18] * 256 + 257] = 0xff
end
end # class D64_image
class D71_image < Disk_image
def initialize(disk_title:, diskimage_filename:, is_boot_disk:, reserve_dir_track: nil)
puts "Creating disk image..." if $verbose
@disk_title = disk_title
@diskimage_filename = diskimage_filename
@is_boot_disk = is_boot_disk
@tracks = 70
@track_length = Array.new(@tracks + 1) {|track|
track == 0 ? 0 :
track < 18 ? 21 :
track < 25 ? 19 :
track < 31 ? 18 :
track < 36 ? 17 :
track < 53 ? 21 :
track < 60 ? 19 :
track < 66 ? 18 :
17
}
# puts "Tracks: #{@track_length.to_s}"
base_initialize()
@interleave = 5
# NOTE: Blocks to skip can only be 0, 2, 4 or 6, or entire track.
@reserved_sectors[18] = reserve_dir_track ? @track_length[18] : 2 # 2: Skip BAM and 1 directory block, 19: Skip entire track
@reserved_sectors[53] = reserve_dir_track ? @track_length[53] : 2 # 2: Skip BAM and 1 extra block (we can't skip just 1 block)
@reserved_sectors[@config_track] = 2 if @is_boot_disk and @config_track
calculate_initial_free_blocks()
# BAM
@track1800 = [
# $16500 = 91392 = 357 (18,0)
0x12,0x01, # track/sector
0x41, # DOS version
0x80, # $80 = Double-sided
# <free sectors>,<0-7>,<8-15>,<16-?, remaining bits 0>
0x15,0xff,0xff,0x1f, # 16504, track 01 (21 sectors)
0x15,0xff,0xff,0x1f, # 16508, track 02
0x15,0xff,0xff,0x1f, # 1650c, track 03
0x15,0xff,0xff,0x1f, # 16510, track 04
0x15,0xff,0xff,0x1f, # 16514, track 05
0x15,0xff,0xff,0x1f, # 16518, track 06
0x15,0xff,0xff,0x1f, # 1651c, track 07
0x15,0xff,0xff,0x1f, # 16520, track 08
0x15,0xff,0xff,0x1f, # 16524, track 09
0x15,0xff,0xff,0x1f, # 16528, track 10
0x15,0xff,0xff,0x1f, # 1652c, track 11
0x15,0xff,0xff,0x1f, # 16530, track 12
0x15,0xff,0xff,0x1f, # 16534, track 13
0x15,0xff,0xff,0x1f, # 16538, track 14
0x15,0xff,0xff,0x1f, # 1653c, track 15
0x15,0xff,0xff,0x1f, # 16540, track 16
0x15,0xff,0xff,0x1f, # 16544, track 17
0x11,0xfc,0xff,0x07, # 16548, track 18 (19 sectors)
0x13,0xff,0xff,0x07, # 1654c, track 19
0x13,0xff,0xff,0x07, # 16550, track 20
0x13,0xff,0xff,0x07, # 16554, track 21
0x13,0xff,0xff,0x07, # 16558, track 22
0x13,0xff,0xff,0x07, # 1655c, track 23
0x13,0xff,0xff,0x07, # 16560, track 24
0x12,0xff,0xff,0x03, # 16564, track 25 (18 sectors)
0x12,0xff,0xff,0x03, # 16568, track 26
0x12,0xff,0xff,0x03, # 1656c, track 27
0x12,0xff,0xff,0x03, # 16570, track 28
0x12,0xff,0xff,0x03, # 16574, track 29
0x12,0xff,0xff,0x03, # 16578, track 30
0x11,0xff,0xff,0x01, # 1657c, track 31 (17 sectors)
0x11,0xff,0xff,0x01, # track 32
0x11,0xff,0xff,0x01, # track 33
0x11,0xff,0xff,0x01, # track 34
0x11,0xff,0xff,0x01, # track 35
0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0, # label (game name)
0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,0xa0,
0xa0,0xa0,0x30,0x30,0xa0,0x32,0x41,0xa0,
0xa0,0xa0,0xa0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00, # -$dc
0x15,0x15,0x15, # $dd- (free sector count for track 36-70)
0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15,
0x15,0x15,0x15,0x15,0x15,0x15,0x11,0x13,
0x13,0x13,0x13,0x13,0x13,0x12,0x12,0x12,
0x12,0x12,0x12,0x11,0x11,0x11,0x11,0x11
]
@track5300 = [
# <0-7>,<8-15>,<16-?, remaining bits 0>
0xff,0xff,0x1f, # track 36 (21 sectors)
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xff,0xff,0x1f,
0xfc,0xff,0x07, # track 53 (19 sectors, 2 used)
0xff,0xff,0x07, # track 54 (19 sectors)
0xff,0xff,0x07,
0xff,0xff,0x07,
0xff,0xff,0x07,
0xff,0xff,0x07,
0xff,0xff,0x07,
0xff,0xff,0x03, # track 60 (18 sectors)
0xff,0xff,0x03,
0xff,0xff,0x03,
0xff,0xff,0x03,
0xff,0xff,0x03,
0xff,0xff,0x03,
0xff,0xff,0x01, # track 66 (17 sectors)
0xff,0xff,0x01,
0xff,0xff,0x01,
0xff,0xff,0x01,
0xff,0xff,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00, # $69-$6f
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$70-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$80-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$90-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$a0-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$b0-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$c0-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$d0-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$e0-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, #$f0-
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
]
# Create a disk image. Return number of free blocks, or -1 for failure.
# for track in 36 .. 40 do
# @track1800[0xc0 + 4 * (track - 36) .. 0xc0 + 4 * (track - 36) + 3] = (track > @tracks ? [0,0,0,0] : [0x11,0xff,0xff,0x01])
# end
# Set disk title
c64_title = name_to_c64(disk_title)
@track1800[0x90 .. 0x9f] = Array.new(0x10, 0xa0)
[c64_title.length, 0x10].min.times do |charno|
@track1800[0x90 + charno] = c64_title[charno].ord
end
if @is_boot_disk and @config_track then
allocate_sector(@config_track, 0)
allocate_sector(@config_track, 1)
end
@free_blocks
end # initialize
private
def allocate_sector(track, sector)
print "*" if $PRINT_DISK_MAP
if track > 35 then # BAM is (mostly) in sector 53:0
index1 = 0xdd + track - 36
index2 = 3 * (track - 36) + (sector / 8)
# adjust number of free sectors
@track1800[index1] -= 1
# allocate sector
index3 = 255 - 2**(sector % 8)
@track5300[index2] &= index3
else
index1 = 4 * track
index2 = 4 * track + 1 + (sector / 8)
if track > 35 then # BAM is (mostly) in sector 53:0
index1 += 0x30
index2 += 0x30
end
# adjust number of free sectors
@track1800[index1] -= 1
# allocate sector
index3 = 255 - 2**(sector % 8)
@track1800[index2] &= index3
end
end
def add_directory()
# Add disk info and BAM at 18:00
@contents[@track_offset[18] * 256 .. @track_offset[18] * 256 + 255] = @track1800
# Add directory at 18:01
@contents[@track_offset[18] * 256 + 256] = 0
@contents[@track_offset[18] * 256 + 257] = 0xff
# Add BAM 53:00
@contents[@track_offset[53] * 256 .. @track_offset[53] * 256 + 255] = @track5300
end
end # class D71_image
class D81_image < Disk_image
def initialize(disk_title:, diskimage_filename:)
puts "Creating disk image..." if $verbose
@disk_title = disk_title
@diskimage_filename = diskimage_filename
@is_boot_disk = true
@tracks = 80
@track_length = Array.new(@tracks + 1, 40)
@track_length[0] = 0
@add_to_dir = []
base_initialize()
# NOTE: Blocks to skip can only be 0, 2, 4 or 6, or entire track.
@reserved_sectors[40] = 40 # 4: Skip BAM and 1 directory block, 6: Skip BAM and 3 directory blocks, 40: Skip entire track
if @config_track
@reserved_sectors[@config_track] = 2
end
calculate_initial_free_blocks()
# BAM
@track4000 = [
# $16500 = 91392 = 357 (18,0)
0x28, 0x03, # track/sector of first directory sector
0x44, # DOS version
0x00, # Not used, don't alter value
0x4F, 0x5A, 0x4D, 0x4F, 0x4F, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, # Disk name
0xA0, 0xA0, # Not used, don't alter value
0x31, 0x41, # Disk ID
0xA0, # Not used, don't alter value
0x33, # DOS version
0x44, # Disk version
0xA0, 0xA0 # Not used, don't alter value
]
@track4001 = [
0x28,0x02, # track/sector of next BAM sector
0x44, # Version#
0xBB, # One's complement of version#
0x31,0x41, # Disk ID (same as in 40:00)
0xC0, # I/O byte
0x00, # Auto-boot-loader flags
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, # Reserved for future use
0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 1
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 2-4
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 5-8
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 9-12
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 13-16
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 17-20
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 21-24
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 25-28
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 29-32
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 33-36
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x24,0xF0,0xFF,0xFF,0xFF,0xFF, # Track 37-40
# Sector 40:02
0x00,0xFF, # track/sector of next BAM sector
0x44, # Version#
0xBB, # One's complement of version#
0x31,0x41, # Disk ID (same as in 40:00)
0xC0, # I/O byte
0x00, # Auto-boot-loader flags
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, # Reserved for future use
0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 41
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 42-44
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 45-48
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 49-52
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 53-56
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 57-60
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 61-64
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 65-68
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 69-72
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF, # Track 73-76
0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF,0x28,0xFF,0xFF,0xFF,0xFF,0xFF # Track 77-80
]
# Add BAM at 40:01 and 40:02
@contents[(@track_offset[40] + 1) * 256 .. (@track_offset[40] + 1) * 256 + @track4001.length - 1] = @track4001
# Create a disk image. Return number of free blocks, or -1 for failure.
# Set disk title
c64_title = name_to_c64(disk_title)
@track4000[0x04 .. 0x13] = Array.new(0x10, 0xa0)
[c64_title.length, 0x10].min.times do |charno|
@track4000[0x04 + charno] = c64_title[charno].ord
end
if @config_track
allocate_sector(@config_track, 0)
allocate_sector(@config_track, 1)
end
@free_blocks
end # initialize
def create_story_partition
if @storydata_start_track > 0 and @storydata_end_track >= @storydata_start_track
sector = @contents[(@track_offset[40] + 3) * 256 .. (@track_offset[40] + 3) * 256 + 255]
@storydata_start_track -= 1 if @config_track == @storydata_start_track - 1
@storydata_end_track += 1 if @config_track == @storydata_end_track + 1
allocate_track(@storydata_end_track)
allocate_track(@config_track)
if @storydata_start_track < 40 and @storydata_end_track > 40
return false unless create_a_partition(sector, @storydata_start_track, 39, 'data-a') == true
@contents[(@track_offset[40] + 3) * 256 .. (@track_offset[40] + 3) * 256 + 255] = sector
return false unless create_a_partition(sector, 41, @storydata_end_track, 'data-b') == true
@contents[(@track_offset[40] + 3) * 256 .. (@track_offset[40] + 3) * 256 + 255] = sector
else
return false unless create_a_partition(sector, @storydata_start_track, @storydata_end_track, 'data') == true
@contents[(@track_offset[40] + 3) * 256 .. (@track_offset[40] + 3) * 256 + 255] = sector
end
if @config_track < @storydata_start_track or @config_track > @storydata_end_track
return false unless create_a_partition(sector, @config_track, @config_track, 'cfg') == true
@contents[(@track_offset[40] + 3) * 256 .. (@track_offset[40] + 3) * 256 + 255] = sector
end
true
else
false
end
end
def add_file(filename, filecontents, last_sector = nil) # Returns last sector used
sector_count = 0
last_sector_used = nil
if filecontents == nil or filecontents.length == 0
start_sector = [0,0]
last_sector_used = start_sector
else
# Find first free sector as close as possible to the last sector used by the last file
start_sector = find_free_file_start_sector(last_sector)
last_sector_used = start_sector
if start_sector == nil
puts "ERROR: No free blocks left on disk."
exit 1
end
sector_count += 1
this_sector = start_sector
while filecontents.length > 254
next_sector = find_next_free_sector(this_sector[0], this_sector[1])
if next_sector == nil
puts "ERROR: No free blocks left on disk."
exit 1
end
last_sector_used = next_sector
sector_count += 1
block_contents = next_sector.pack("CC") + filecontents[0 .. 253]
filecontents = filecontents[254 .. filecontents.size - 1]
@contents[256 * (@track_offset[this_sector[0]] + this_sector[1]) ..
256 * (@track_offset[this_sector[0]] + this_sector[1]) + 255] =
block_contents.unpack("C*")
this_sector = next_sector
end
block_contents = [0, filecontents.length + 2 - 1].pack("CC") + filecontents + Array.new(254 - filecontents.length).fill(0).pack("c*")
@contents[256 * (@track_offset[this_sector[0]] + this_sector[1]) ..
256 * (@track_offset[this_sector[0]] + this_sector[1]) + 255] =
block_contents.unpack("C*")
end
# Add file to directory
dir_entry = ([0x81] + start_sector).pack("CCC") +
name_to_c64(filename).ljust(16, 0xa0.chr) +
"".ljust(9, 0.chr) +
[sector_count % 256, sector_count / 256].pack("CC")
@add_to_dir.push dir_entry
return last_sector_used
end