forked from johanberntsson/ozmoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.rb
1536 lines (1313 loc) · 55.7 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'
$is_windows = (ENV['OS'] == 'Windows_NT')
if $is_windows then
# Paths on Windows
$X64 = "C:\\ProgramsWoInstall\\WinVICE-3.1-x64\\x64.exe -autostart-warp" # -autostart-delay-random"
$C1541 = "C:\\ProgramsWoInstall\\WinVICE-3.1-x64\\c1541.exe"
$EXOMIZER = "C:\\ProgramsWoInstall\\Exomizer-3.0.2\\win32\\exomizer.exe"
$ACME = "C:\\ProgramsWoInstall\\acme0.96.4win\\acme\\acme.exe"
else
# Paths on Linux
$X64 = "/usr/bin/x64 -autostart-delay-random"
$C1541 = "/usr/bin/c1541"
$EXOMIZER = "exomizer/src/exomizer"
$ACME = "acme"
end
$PRINT_DISK_MAP = false # Set to true to print which blocks are allocated
# Typically only SMALLBLOCK should be enabled.
$GENERALFLAGS = [
'SMALLBLOCK', # Use 512 byte blocks instead of 1024 byte blocks for virtual memory. NOTE: 1024 byte mode is slower and currently broken.
# 'UNSAFE', # Remove almost all runtime error checking. This makes the terp ~100 bytes smaller.
# 'SLOW', # Remove some optimizations for speed. This makes the terp ~100 bytes smaller.
# '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.
]
# 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',
# 'TRACE_FLOPPY',
# 'TRACE_VM',
# 'PRINT_SWAPS',
# 'TRACE_FLOPPY_VERBOSE',
# 'TRACE_PRINT_ARRAYS',
# 'TRACE_PROP',
# 'TRACE_READTEXT',
# 'TRACE_SHOW_DICT_ENTRIES',
# 'TRACE_TOKENISE',
]
$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_81 = 6
mode = MODE_S1
DISKNAME_BOOT = 128
DISKNAME_STORY = 129
DISKNAME_SAVE = 130
DISKNAME_DISK = 131
$BUILD_ID = Random.rand(0 .. 2**32-1)
$VMEM_BLOCKSIZE = $GENERALFLAGS.include?('SMALLBLOCK') ? 512 : 1024
$ZEROBYTE = 0.chr
$EXECDIR = Dir.pwd
$SRCDIR = File.join(__dir__, 'asm')
$TEMPDIR = File.join(__dir__, 'temp')
Dir.mkdir($TEMPDIR) unless Dir.exist?($TEMPDIR)
$labels_file = File.join($TEMPDIR, 'acme_labels.txt')
$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')
class Disk_image
def base_initialize
@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}"
end
def free_blocks
@free_blocks
end
def interleave
@interleave
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
@config_track_map.push(64 * reserved_sectors / 2 + last_story_sector)
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:)
puts "Creating disk image..."
@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] = 2 # 2: Skip BAM and 1 directory block, 19: Skip entire track
@reserved_sectors[@config_track] = 2 if @is_boot_disk
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 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
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 D81_image < Disk_image
def initialize(disk_title:, diskimage_filename:)
puts "Creating disk image..."
@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
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
@reserved_sectors[@config_track] = 2
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
]
# 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
allocate_sector(@config_track, 0)
allocate_sector(@config_track, 1)
@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
private
def allocate_sector(track, sector)
print "*" if $PRINT_DISK_MAP
index1 = (track > 40 ? 0x100: 0) + 0x10 + 6 * ((track - 1) % 40)
index2 = index1 + 1 + (sector / 8)
# adjust number of free sectors
@track4001[index1] -= 1
# allocate sector
index3 = 255 - 2**(sector % 8)
@track4001[index2] &= index3
end
def add_directory()
# Add disk info at 40:00
@contents[@track_offset[40] * 256 .. @track_offset[40] * 256 + @track4000.length - 1] = @track4000
# Add BAM at 40:01 and 40:02
@contents[(@track_offset[40] + 1) * 256 .. (@track_offset[40] + 1) * 256 + @track4001.length - 1] = @track4001
# Add directory at 40:03
@contents[(@track_offset[40] + 3) * 256] = 0
@contents[(@track_offset[40] + 3) * 256 + 1] = 0xff
end
def create_a_partition(sector, start_track, end_track, name)
entry = 1
while entry < 8 and sector[0x20 * entry + 2] > 0
entry += 1
end
return false if entry > 7
entrybase = 0x20 * entry
sector[entrybase + 2] = 0x85 # CBM type ( = partition)
sector[entrybase + 3] = start_track
sector[entrybase + 4] = 0 # Start sector
c64_title = name_to_c64(name)
sector[entrybase + 0x5 .. entrybase + 0x14] = Array.new(0x10, 0xa0)
[c64_title.length, 0x10].min.times do |charno|
sector[entrybase + 0x5 + charno] = c64_title[charno].ord
end
sector[entrybase + 0x15 .. entrybase + 0x1d] = Array.new(9, 0)
part_size = (end_track - start_track + 1) * 40
sector[entrybase + 0x1e] = part_size % 256
sector[entrybase + 0x1f] = part_size / 256
true
end
def allocate_track(track)
index1 = (track > 40 ? 0x100: 0) + 0x10 + 6 * ((track - 1) % 40)
@contents[(@track_offset[40] + 1) * 256 + index1 .. (@track_offset[40] + 1) * 256 + index1 + 5] = Array.new(6, 0)
end
end # class D81_image
################################## END Disk image classes
def filename_to_title(name, remove_the_if_longer_than)
# Convert camel case and underscore to spaces. Remove "The" or "A" at beginning if the name gets too long.
title = name.dup
camel_case = title =~ /[a-z]/ and title =~ /[A-Z]/ and title !~ / |_/
if camel_case then
title.gsub!(/([a-z])([A-Z])/,'\1 \2')
title.gsub!(/A([A-Z])/,'A \1')
end
title.gsub!(/_+/," ")
title.gsub!(/(^ +)|( +)$/,"")
if remove_the_if_longer_than
title.gsub!(/^(the|a) (.*)$/i,'\2') if title.length > remove_the_if_longer_than
end
title.capitalize! if title =~ /^[a-z]/
title
end
def name_to_c64(name)
c64_name = filename_to_title(name, 16)
c64_name.length.times do |charno|
code = c64_name[charno].ord
code &= 0xdf if code >= 0x61 and code <= 0x7a
c64_name[charno] = code.chr
end
c64_name
end
def build_interpreter()
necessarysettings = " --setpc #{$start_address} -DCACHE_PAGES=#{$CACHE_PAGES} -DSTACK_PAGES=#{$stack_pages} -D#{$ztype}=1 -DCONF_TRK=#{$CONFIG_TRACK}"
necessarysettings += " --cpu 6510 --format cbm"
optionalsettings = ""
optionalsettings += " -DSPLASHWAIT=#{$splash_wait}" if $splash_wait
generalflags = $GENERALFLAGS.empty? ? '' : " -D#{$GENERALFLAGS.join('=1 -D')}=1"
debugflags = $DEBUGFLAGS.empty? ? '' : " -D#{$DEBUGFLAGS.join('=1 -D')}=1"
colourflags = $colour_replacement_clause
unless $default_colours.empty? # or $zcode_version >= 5
colourflags += " -DBGCOL=#{$default_colours[0]} -DFGCOL=#{$default_colours[1]}"
end
if $border_colour
colourflags += " -DBORDERCOL=#{$border_colour}"
end
if $statusline_colour
colourflags += " -DSTATCOL=#{$statusline_colour}"
end
unless $default_colours_dm.empty? # or $zcode_version >= 5
colourflags += " -DBGCOLDM=#{$default_colours_dm[0]} -DFGCOLDM=#{$default_colours_dm[1]}"
end
if $border_colour_dm
colourflags += " -DBORDERCOLDM=#{$border_colour_dm}"
end
if $statusline_colour_dm
colourflags += " -DSTATCOLDM=#{$statusline_colour_dm}"
end
fontflag = $font_filename ? ' -DCUSTOM_FONT=1' : ''
compressionflags = ''
cmd = "#{$ACME}#{necessarysettings}#{optionalsettings}#{fontflag}#{colourflags}#{generalflags}" +
"#{debugflags}#{compressionflags} -l \"#{$labels_file}\" --outfile \"#{$ozmoo_file}\" ozmoo.asm"
puts cmd
Dir.chdir $SRCDIR
ret = system(cmd)
Dir.chdir $EXECDIR
unless ret
puts "ERROR: There was a problem calling Acme"
exit 1
end
read_labels($labels_file);
puts "Interpreter size: #{$program_end_address - $start_address} bytes."
end
def read_labels(label_file_name)
$storystart = 0
File.open(label_file_name).each do |line|
$storystart = $1.to_i(16) if line =~ /\tstory_start\t=\s*\$(\w{3,4})\b/;
$program_end_address = $1.to_i(16) if line =~ /\tprogram_end\t=\s*\$(\w{3,4})\b/;
end
end
def build_specific_boot_file(vmem_preload_blocks, vmem_contents)
compmem_clause = " \"#{$compmem_filename}\"@#{$storystart},0,#{[($dynmem_blocks + vmem_preload_blocks) * $VMEM_BLOCKSIZE, 0x10000 - $storystart, File.size($compmem_filename)].min}"
font_clause = ""
if $font_filename then
font_clause = " \"#{$font_filename}\"@2048"
end
# exomizer_cmd = "#{$EXOMIZER} sfx basic -B -X \'LDA $D012 STA $D020 STA $D418\' ozmoo #{$compmem_filename},#{$storystart} -o ozmoo_zip"
# exomizer_cmd = "#{$EXOMIZER} sfx #{$start_address} -B -M256 -C -x1 #{font_clause} \"#{$ozmoo_file}\"#{compmem_clause} -o \"#{$zip_file}\""
exomizer_cmd = "#{$EXOMIZER} sfx #{$start_address} -B -M256 -C #{font_clause} \"#{$ozmoo_file}\"#{compmem_clause} -o \"#{$zip_file}\""
puts exomizer_cmd
ret = system(exomizer_cmd)
unless ret
puts "ERROR: There was a problem calling Exomizer"
exit 1
end
# puts "Building with #{vmem_preload_blocks} blocks gives file size #{File.size($zip_file)}."
File.size($zip_file)
end
def save_good_boot_file()
File.delete($good_zip_file) if File.exist?($good_zip_file)
File.rename($zip_file, $good_zip_file)
end
def build_boot_file(vmem_preload_blocks, vmem_contents, free_blocks)
begin
compmem_filehandle = File.open($compmem_filename, "wb")
rescue
puts "ERROR: Can't open #{$compmem_filename} for writing"
exit 1
end
compmem_filehandle.write(vmem_contents[0 .. ($dynmem_blocks + vmem_preload_blocks) * $VMEM_BLOCKSIZE - 1])
compmem_filehandle.close
max_file_size = free_blocks * 254
puts "Max file size is #{max_file_size} bytes."
if build_specific_boot_file(vmem_preload_blocks, vmem_contents) <= max_file_size then
save_good_boot_file()
return vmem_preload_blocks
end
puts "##### Built loader/interpreter with #{vmem_preload_blocks} virtual memory blocks preloaded: Too big #####\n\n"
max_ok_blocks = -1 # If we never find a number of blocks which work, -1 will be returned to signal failure.
done = false
min_failed_blocks = vmem_preload_blocks
actual_blocks = -1
last_build = -2
until done
if min_failed_blocks - max_ok_blocks < 2
actual_blocks = max_ok_blocks
done = true
elsif min_failed_blocks < 1
actual_blocks = max_ok_blocks
done = true
else
mid = (min_failed_blocks + max_ok_blocks) / 2
# puts "Trying #{mid} blocks..."
size = build_specific_boot_file(mid, vmem_contents)
last_build = mid
if size > max_file_size then
puts "##### Built loader/interpreter with #{mid} virtual memory blocks preloaded: Too big #####\n\n"
min_failed_blocks = mid
else
save_good_boot_file()
puts "##### Built loader/interpreter with #{mid} virtual memory blocks preloaded: OK #####\n\n"
max_ok_blocks = mid
# max_ok_blocks = [mid + (1.25 * (max_file_size - size) / $VMEM_BLOCKSIZE).floor.to_i, min_failed_blocks - 1].min
end
end
end
# build_specific_boot_file(actual_blocks, vmem_contents) unless last_build == actual_blocks
puts "Picked #{actual_blocks} blocks." if max_ok_blocks >= 0
actual_blocks
end
def add_boot_file(finaldiskname, diskimage_filename)
ret = FileUtils.cp("#{diskimage_filename}", "#{finaldiskname}")
puts "#{$C1541} -attach \"#{finaldiskname}\" -write \"#{$good_zip_file}\" story"
system("#{$C1541} -attach \"#{finaldiskname}\" -write \"#{$good_zip_file}\" story")
end
def play(filename)
command = "#{$X64} #{filename}"
puts command
system(command)
end
def limit_vmem_data(vmem_data)
# puts "### #{$vmem_size} < #{(vmem_data[2] + $dynmem_blocks) * $VMEM_BLOCKSIZE} ###"
if $vmem_size < (vmem_data[2] + $dynmem_blocks) * $VMEM_BLOCKSIZE
vmem_data[2] = $vmem_size / $VMEM_BLOCKSIZE - $dynmem_blocks
end
end
def build_P(storyname, diskimage_filename, config_data, vmem_data, vmem_contents, preload_max_vmem_blocks, extended_tracks)
max_story_blocks = 0
boot_disk = false
diskfilename = "#{storyname}.d64"
if $vmem_size < $story_size
puts "#{$vmem_size} < #{$story_size}"
puts "ERROR: The whole story doesn't fit in memory. Please try another build mode."
exit 1
end
disk = D64_image.new(disk_title: storyname, diskimage_filename: diskimage_filename, is_boot_disk: boot_disk, forty_tracks: extended_tracks)
disk.add_story_data(max_story_blocks: max_story_blocks, add_at_end: extended_tracks) # Has to be run to finalize the disk
free_blocks = disk.free_blocks()
puts "Free disk blocks after story data has been written: #{free_blocks}"
# Build loader + terp + preloaded vmem blocks as a file
# puts "build_boot_file(#{preload_max_vmem_blocks}, #{vmem_contents.length}, #{free_blocks})"
build_boot_file(preload_max_vmem_blocks, vmem_contents, free_blocks)
disk.save()
# Add loader + terp + preloaded vmem blocks file to disk
if add_boot_file(diskfilename, diskimage_filename) != true
puts "ERROR: Failed to write loader/interpreter to disk."
exit 1
end
$bootdiskname = diskfilename
puts "Successfully built game as #{$bootdiskname}"
nil # Signal success
end
def build_S1(storyname, diskimage_filename, config_data, vmem_data, vmem_contents, preload_max_vmem_blocks, extended_tracks)
max_story_blocks = 9999
boot_disk = true
diskfilename = "#{storyname}.d64"
disk = D64_image.new(disk_title: storyname, diskimage_filename: diskimage_filename, is_boot_disk: boot_disk, forty_tracks: extended_tracks)
disk.add_story_data(max_story_blocks: max_story_blocks, add_at_end: extended_tracks)
if $story_file_cursor < $story_file_data.length
puts "ERROR: The whole story doesn't fit on the disk. Please try another build mode."
exit 1
end
free_blocks = disk.free_blocks()
puts "Free disk blocks after story data has been written: #{free_blocks}"
# Build loader + terp + preloaded vmem blocks as a file
# puts "build_boot_file(#{preload_max_vmem_blocks}, #{vmem_contents.length}, #{free_blocks})"
vmem_preload_blocks = build_boot_file(preload_max_vmem_blocks, vmem_contents, free_blocks)
# puts "vmem_preload_blocks(#{vmem_preload_blocks} < $dynmem_blocks#{$dynmem_blocks}"
if vmem_preload_blocks < 0
puts "ERROR: The story fits on the disk, but not the loader/interpreter. Please try another build mode."
exit 1
end
vmem_data[2] = vmem_preload_blocks
# Add config data about boot / story disk
disk_info_size = 11 + disk.config_track_map.length
last_block_plus_1 = 0
disk.config_track_map.each{|i| last_block_plus_1 += (i & 0x3f)}
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name = "Boot / Story disk"
config_data += [disk_info_size, 0, last_block_plus_1 / 256, last_block_plus_1 % 256,
disk.config_track_map.length] + disk.config_track_map
config_data += [DISKNAME_BOOT, "/".ord, " ".ord, DISKNAME_STORY, DISKNAME_DISK, 0] # Name: "Boot / Story disk"
config_data[4] += disk_info_size
config_data += vmem_data
# puts config_data
disk.set_config_data(config_data)
disk.save()
# Add loader + terp + preloaded vmem blocks file to disk
if add_boot_file(diskfilename, diskimage_filename) != true
puts "ERROR: Failed to write loader/interpreter to disk."
exit 1
end
$bootdiskname = "#{diskfilename}"
puts "Successfully built game as #{$bootdiskname}"
nil # Signal success
end
def build_S2(storyname, d64_filename_1, d64_filename_2, config_data, vmem_data, vmem_contents, preload_max_vmem_blocks, extended_tracks)
config_data[7] = 3 # 3 disks used in total
outfile1name = "#{storyname}_boot.d64"
outfile2name = "#{storyname}_story.d64"
max_story_blocks = 9999
disk1 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_1, is_boot_disk: true, forty_tracks: false)
disk2 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_2, is_boot_disk: false, forty_tracks: extended_tracks)
free_blocks = disk1.add_story_data(max_story_blocks: 0, add_at_end: false)
free_blocks = disk2.add_story_data(max_story_blocks: max_story_blocks, add_at_end: false)
puts "Free disk blocks after story data has been written: #{free_blocks}"
if $story_file_cursor < $story_file_data.length
puts "ERROR: The whole story doesn't fit on the disk. Please try another build mode."
exit 1
end
# Build loader + terp + preloaded vmem blocks as a file
vmem_preload_blocks = build_boot_file(preload_max_vmem_blocks, vmem_contents, 664)
vmem_data[2] = vmem_preload_blocks
# Add config data about boot disk
disk_info_size = 8
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name = "Boot disk"
config_data += [disk_info_size, 0, 0, 0, 0]
config_data += [DISKNAME_BOOT, DISKNAME_DISK, 0] # Name: "Boot disk"
config_data[4] += disk_info_size
# Add config data about story disk
disk_info_size = 8 + disk2.config_track_map.length
last_block_plus_1 = 0
disk2.config_track_map.each{|i| last_block_plus_1 += (i & 0x3f)}
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name = "Story disk"
config_data += [disk_info_size, 0, last_block_plus_1 / 256, last_block_plus_1 % 256,
disk2.config_track_map.length] + disk2.config_track_map
config_data += [DISKNAME_STORY, DISKNAME_DISK, 0] # Name: "Story disk"
config_data[4] += disk_info_size
config_data += vmem_data
# puts config_data
disk1.set_config_data(config_data)
disk1.save()
disk2.save()
# Add loader + terp + preloaded vmem blocks file to disk
if add_boot_file(outfile1name, d64_filename_1) != true
puts "ERROR: Failed to write loader/interpreter to disk."
exit 1
end
File.delete(outfile2name) if File.exist?(outfile2name)
File.rename(d64_filename_2, "./#{outfile2name}")
$bootdiskname = "#{outfile1name}"
puts "Successfully built game as #{$bootdiskname} + #{outfile2name}"
nil # Signal success
end
def build_D2(storyname, d64_filename_1, d64_filename_2, config_data, vmem_data, vmem_contents, preload_max_vmem_blocks, extended_tracks)
config_data[7] = 3 # 3 disks used in total
outfile1name = "#{storyname}_boot_story_1.d64"
outfile2name = "#{storyname}_story_2.d64"
disk1 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_1, is_boot_disk: true, forty_tracks: extended_tracks)
disk2 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_2, is_boot_disk: false, forty_tracks: extended_tracks)
# Figure out how to put story blocks on the disks in optimal way.
# Rule 1: Save 160 blocks for loader on boot disk, if possible.
# Rule 2: Spread story data as evenly as possible, so heads will move less.
max_story_blocks = 9999
total_raw_story_blocks = ($story_size - $story_file_cursor) / 256
if disk1.free_blocks() - 160 >= total_raw_story_blocks / 2 and disk2.free_blocks >= disk1.free_blocks
# Story data can be evenly spread over the two disks
max_story_blocks = total_raw_story_blocks / 2
elsif disk1.free_blocks() - 160 + disk2.free_blocks >= total_raw_story_blocks
# There is room for a full-size loader on boot disk, if we spread the data unevenly over the two disks
max_story_blocks = disk1.free_blocks() - 160
else
# Fill disk 2 with story data, put the rest on disk 1, and squeeze in the biggest loader that there is room for.
disk2_free = disk2.free_blocks()
disk2_free -= 1 if disk2_free % 2 > 0
max_story_blocks = total_raw_story_blocks - disk2_free
end
free_blocks_1 = disk1.add_story_data(max_story_blocks: max_story_blocks, add_at_end: extended_tracks)
puts "Free disk blocks on disk #1 after story data has been written: #{free_blocks_1}"
free_blocks_2 = disk2.add_story_data(max_story_blocks: 9999, add_at_end: false)
puts "Free disk blocks on disk #2 after story data has been written: #{free_blocks_2}"
if $story_file_cursor < $story_file_data.length
puts "ERROR: The whole story doesn't fit on the disk. Please try another build mode."
exit 1
end
# Build loader + terp + preloaded vmem blocks as a file
vmem_preload_blocks = build_boot_file(preload_max_vmem_blocks, vmem_contents, free_blocks_1)
if vmem_preload_blocks < 0
puts "ERROR: The story fits on the disk, but not the loader/interpreter. Please try another build mode."
exit 1
end
vmem_data[2] = vmem_preload_blocks
# Add config data about boot disk / story disk 1
disk_info_size = 13 + disk1.config_track_map.length
# last_block_plus_1 = $dynmem_blocks * $VMEM_BLOCKSIZE / 256
last_block_plus_1 = 0
disk1.config_track_map.each{|i| last_block_plus_1 += (i & 0x3f)}
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name
config_data += [disk_info_size, 0, last_block_plus_1 / 256, last_block_plus_1 % 256,
disk1.config_track_map.length] + disk1.config_track_map
config_data += [DISKNAME_BOOT, DISKNAME_DISK, "/".ord, " ".ord, DISKNAME_STORY, DISKNAME_DISK, "1".ord, 0] # Name: "Boot disk / Story disk 1"
config_data[4] += disk_info_size
# Add config data about story disk 2
disk_info_size = 9 + disk2.config_track_map.length
disk2.config_track_map.each{|i| last_block_plus_1 += (i & 0x3f)}
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name
config_data += [disk_info_size, 0, last_block_plus_1 / 256, last_block_plus_1 % 256,
disk2.config_track_map.length] + disk2.config_track_map
config_data += [DISKNAME_STORY, DISKNAME_DISK, "2".ord, 0] # Name: "Story disk 2"
config_data[4] += disk_info_size
config_data += vmem_data
# puts config_data
disk1.set_config_data(config_data)
disk1.save()
disk2.save()
# Add loader + terp + preloaded vmem blocks file to disk
if add_boot_file(outfile1name, d64_filename_1) != true
puts "ERROR: Failed to write loader/interpreter to disk."
exit 1
end
File.delete(outfile2name) if File.exist?(outfile2name)
File.rename(d64_filename_2, "./#{outfile2name}")
$bootdiskname = "#{outfile1name}"
puts "Successfully built game as #{$bootdiskname} + #{outfile2name}"
nil # Signal success
end
def build_D3(storyname, d64_filename_1, d64_filename_2, d64_filename_3, config_data, vmem_data, vmem_contents, preload_max_vmem_blocks, extended_tracks)
config_data[7] = 4 # 4 disks used in total
outfile1name = "#{storyname}_boot.d64"
outfile2name = "#{storyname}_story_1.d64"
outfile3name = "#{storyname}_story_2.d64"
disk1 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_1, is_boot_disk: true, forty_tracks: false)
disk2 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_2, is_boot_disk: false, forty_tracks: extended_tracks)
disk3 = D64_image.new(disk_title: storyname, diskimage_filename: d64_filename_3, is_boot_disk: false, forty_tracks: extended_tracks)
# Figure out how to put story blocks on the disks in optimal way.
# Rule: Spread story data as evenly as possible, so heads will move less.
total_raw_story_blocks = ($story_size - $story_file_cursor) / 256
max_story_blocks = total_raw_story_blocks / 2
free_blocks_1 = disk1.add_story_data(max_story_blocks: 0, add_at_end: false)
puts "Free disk blocks on disk #1 after story data has been written: #{free_blocks_1}"
free_blocks_2 = disk2.add_story_data(max_story_blocks: max_story_blocks, add_at_end: false)
puts "Free disk blocks on disk #2 after story data has been written: #{free_blocks_2}"
free_blocks_3 = disk3.add_story_data(max_story_blocks: 9999, add_at_end: false)
puts "Free disk blocks on disk #3 after story data has been written: #{free_blocks_3}"
if $story_file_cursor < $story_file_data.length
puts "ERROR: The whole story doesn't fit on the disk. Please try another build mode."
exit 1
end
# Build loader + terp + preloaded vmem blocks as a file
vmem_preload_blocks = build_boot_file(preload_max_vmem_blocks, vmem_contents, 664)
vmem_data[2] = vmem_preload_blocks
# Add config data about boot disk
disk_info_size = 8
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name = "Boot disk"
config_data += [disk_info_size, 0, 0, 0, 0]
config_data += [DISKNAME_BOOT, DISKNAME_DISK, 0] # Name: "Boot disk"
config_data[4] += disk_info_size
last_block_plus_1 = 0
# Add config data about story disk 1
disk_info_size = 9 + disk2.config_track_map.length
disk2.config_track_map.each{|i| last_block_plus_1 += (i & 0x3f)}
# Data for disk: bytes used, device# = 0 (auto), Last story data sector + 1 (word), tracks used for story data, name