-
Notifications
You must be signed in to change notification settings - Fork 2
/
scdmake
executable file
·2191 lines (1882 loc) · 65.8 KB
/
scdmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
###############################################################################
# Copyright (c) 2011 by bgvanbur
#
# 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.
###############################################################################
# TODO would be cool to be able to make a ROM using scdmake
# right now can do it in a hacky sort of fashion
# and for more complex ISOs this would be tricky automatically
# TODO support making bin/cue format (the EDC done, RS not started)
# TODO handle files in SCDMAKE.INC that are in subdir that do not exist
# TODO strict order (not joliet ones are either all or nothing)
# 1) SCD 2) PVD 3) joliet SVD 4) little path table 5) big path table
# 6) joliet little path table 7) joliet big path table 8) root extent
# 9) joliet root extent 10) files
# TODO does not optimize duplicated files
# TODO finish joliet support (only supports 8.3 [A-Za-z0-9] in most places)
# TODO option to put each file extent in front of files in directory
# Jeopardy does this so iso match won't work
###############################################################################
use strict;
use warnings;
use Carp ();
# for more verbose warnings and such
local $SIG{__WARN__} = \&Carp::cluck;
use Cwd;
use File::Spec;
# the dir/file that will potentially be in the ISO 9660 part
my %dirs;
my %files;
# information to track current iso index, and to buffer the output
# buffer will empty on a sector boundary
my $isoIndex = 0;
my $isoBuffer = '';
my $isoBufferLength = 0;
###############################################################################
# variables for SCDMAKE.CFG
###############################################################################
# 0: none, 1: some, 2: most
my $verbosity = 2; # not in config by default
my $verbositySet = 0; # not in condig by default
my $verbositySetCommandLine = 0; # not in condig by default
# config should use &SetVerbosityIfNotAlreadySet(<value>);
# a value different than 0x800 (2048) has never been tested or desired
# change at your own risk
my $sectorSize = 0x800; # not in config by default
# 300 sector minimum for burning
my $sectorCount = 300; # not in config by default
# the amount to pad at the end of the data sector
# Sega CD needs at least 1 (since zero padding makes last sector unreadable)
# Linux needs at least 15
# http://www.troubleshooters.com/linux/coasterless.htm recommends 63
my $padSize = 63; # not in config by default
# 0 for ISO
# 1 for BIN/CUE (TODO this is a work in progress since it puts in zero for the parity fields)
my $isoType = 0; # not in config by default
# if 0, will make CUE for ISO files
my $audioTracks = 0; # not in config by default
# Level 1: File names are limited to eight characters with a three-character extension, using upper case letters, numbers and underscore only. The maximum depth of directories is eight.
# Level 2: File names are not limited to 11 characters (the 8.3 format) but can be up to the maximum allowed by the 1 byte counter in the directory entry and the filename length byte counter. Typically, this is close to 180 characters, depending on how many extended attributes are present.
# Level 3: Files are allowed to be non-contiguous (i.e., fragmented), principally to allow packet writing or incremental CD recording).
my $isoForceLevel1 = 1; # not in config by default
# sega cd: ids for system id
my $scddiscid;
my $scdvolumename;
my $scdvolumeversion;
my $scdvolumetype = 0x0001; # not in config by default
my $scdsystemname;
my $scdsystemversion;
my $scdipstart = 0x00000800;
my $scdipsize; # not in config by default
my $scdspstart; # not in config by default
my $scdspsize; # not in config by default
# sega cd: ids for disc id
my $scdhardware; # not in config by default
my $scdcompanyname;
my $scdgametitledomestic;
my $scdgametitleoverseas;
my $scddisctype;
my $scdproductcode;
my $scdproductversion;
my $scdio;
my $scdmodem;
my $scdcountry; # not in config by default
my $scdcountryAllRequiresFullRebuild = 0;
# ISO 9660: ids for primary volume descriptor (pvd)
my $pvdsystemid; # not in config by default
my $pvdvolumeid;
my $pvdvolumesetid;
my $pvdpublisherid;
my $pvdpreparerid;
my $pvdapplicationid = "SCDMAKE"; # not in config by default
my $pvdcopyrightfileid;
my $pvdabstractfileid;
my $pvdbibliographicalfileid;
my $joliet = 0; # not in config by default
# Joliet: ids for supplementary volume descriptor (svd)
# these joliet values default to pvd values if not set
my $jolietsystemid; # not in config by default
my $jolietvolumeid; # not in config by default
my $jolietvolumesetid; # not in config by default
my $jolietpublisherid; # not in config by default
my $jolietpreparerid; # not in config by default
my $jolietapplicationid = "SCDMAKE"; # not in config by default
my $jolietcopyrightfileid; # not in config by default
my $jolietabstractfileid; # not in config by default
my $jolietbibliographicalfileid; # not in config by default
# ISO 9660: table locations (as sector locations)
my $pathTableLittleEndian; # not in config by default
my $pathTableBigEndian; # not in config by default
my $jolietPathTableLittleEndian; # not in config by default
my $jolietPathTableBigEndian; # not in config by default
my $rootExtent; # not in config by default
my $jolietRootExtent; # not in config by default
my $fileExtent; # not in config by default
my $fileExtentCurrentOffset = 0; # not in config by default
# sega cd dev manual says to fill with 0xFF
# sonic cd fills with 0x00
my $fillUnusedFileWithZero = 0; # not in config by default
# files
my $ipprefix;
my $spprefix;
my $outprefix;
my $outprefixsuffix = '';
# ippad=0 will put the SP in the same place as if ippad=1
# so the SP will not be efficiently placed
my $ippad = 1; # not in config by default
my $ipassemble = 1; # not in config by default
my $spassemble = 1; # not in config by default
# if desire a specific ISO file layout
# can use a file with .isoinfo suffix as static output of "isoinfo -l -i <iso>"
my $isoMatch = ''; # not in config by default
my $isoMatchFileExtent = -1; # not in config by default
# files to put at beginning of file data and info put in SCDMAKE.INC file
my @scdmakeinc; # not in config by default
# 0: longword equs, 1: dc.w, 2: dc.l, 3: word sector equs
my $scdmakeincType = 0; # not in config by default
# 0: not used, 1: hidden from path tables / extents, 2: public
my $isoPublicDefault = 2; # not in config by default
# besides default, fine grain control of not used / hidden / public files
my @isoNotUsedFiles; # not in config by default
my @isoHiddenFiles; # not in config by default
my @isoPublicFiles; # not in config by default
my @tmpFilesToRemove; # not in config by default
# specify a YYYYHHMMDDHHMMSS00 to set the date used for everything
my $date = ''; # not in config by default
my $defaults = '# scdmake configuration
$scddiscid = \'SEGADISCSYSTEM\';
$scdvolumename = \'DEMO\';
$scdvolumeversion = \'0000\';
$scdsystemname = \'SCDMAKE\';
$scdsystemversion = \'0000\';
$scdcompanyname = \'T-00\';
$scdgametitledomestic = \'DEMO\';
$scdgametitleoverseas = \'DEMO\';
$scddisctype = \'GM\';
$scdproductcode = \'00000\';
$scdproductversion = \'00\';
$scdio = \'J\';
$scdmodem = \'\';
$pvdvolumeid = \'DEMO\';
$pvdvolumesetid = \'\';
$pvdpublisherid = \'\';
$pvdpreparerid = \'\';
$pvdcopyrightfileid = \'\';
$pvdabstractfileid = \'\';
$pvdbibliographicalfileid = \'\';
$ipprefix = \'IP\';
$spprefix = \'SP\';
$outprefix = \'SCD\';
&SetVerbosityIfNotAlreadySet(2);
&SetCountryIfNotAlreadySet(\'U\');
';
eval($defaults);
warn $@ if $@;
$verbositySet = 0;
$scdcountry = undef;
my $cfg = 'SCDMAKE.CFG';
my $inc = 'SCDMAKE.INC';
my $loop;
foreach my $arg (@ARGV) {
if ( $arg =~ m/^-country=([uje]|all)$/i ) {
$scdcountry = $1;
} elsif ( $arg =~ m/^-v=(\d+)$/i ) {
$verbosity = $1;
$verbositySet++;
$verbositySetCommandLine++;
} elsif ( $arg eq '-make' ) {
if ( -e $cfg ) {
print STDERR "Will not override existing $cfg\n";
} else {
print STDERR "Making a default $cfg config file\n";
if ( open( CFG, ">$cfg" ) ) {
print CFG $defaults;
}
close CFG;
}
exit;
} elsif ( $arg =~ m/^-date=(.+)$/i ) {
$date = $1;
} elsif ( $arg =~ m/^-outprefixsuffix=(_US|_JP|_EU)$/i ) {
# hidden option intended for -country=all which requires rebuilding
$outprefixsuffix = $1;
} else {
print "Bad argument: $arg\n";
&Help();
}
}
if ( -e $cfg ) {
if ( open( CFG, $cfg ) ) {
binmode CFG;
my $cfgBuffer = '';
if ( read(CFG,$cfgBuffer,-s $cfg) ) {
eval($cfgBuffer);
print $@ if $@;
}
}
close CFG;
} else {
print "Run \"scdmake -make\" to make a default SCDMAKE.CFG file\n";
&Help();
}
# PVD date format
# TODO get date in perl...
if ( $date eq '' ) {
chomp($date = (`date -u +%Y%m%d%H%M%S00`)[0]);
}
# extent entry date format
if ( $date !~ m/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})00$/ ) {
die "Could not parse date: $date\n";
}
my $year = $1;
my $month = $2;
my $day = $3;
my $hour = $4;
my $min = $5;
my $sec = $6;
my $dateExtent = chr($year-1900).chr($month).chr($day).chr($hour).chr($min).chr($sec).chr(0x00);
# rebuild full should not build anything, directly start into calling scdmake
# recursively for each country
if ( $scdcountryAllRequiresFullRebuild ) {
# pass date so they all have the same date
my $args = " -date=$date ";
# pass verbosity if passed on command line
if ( $verbositySetCommandLine ) {
$args .= " -v=$verbosity ";
}
print "Calling scdmake -country=U ...\n";
system("scdmake -country=U -outprefixsuffix=_US $args");
print "Calling scdmake -country=J ...\n";
system("scdmake -country=J -outprefixsuffix=_JP $args");
print "Calling scdmake -country=E ...\n";
system("scdmake -country=E -outprefixsuffix=_EU $args");
exit;
}
if ( $joliet ) {
if ( ! defined $jolietsystemid && defined $pvdsystemid ) {
$jolietsystemid = $pvdsystemid;
}
if ( ! defined $jolietvolumeid && defined $pvdvolumeid ) {
$jolietvolumeid = $pvdvolumeid;
}
if ( ! defined $jolietvolumesetid && defined $pvdvolumesetid ) {
$jolietvolumesetid = $pvdvolumesetid;
}
if ( ! defined $jolietpublisherid && defined $pvdpublisherid ) {
$jolietpublisherid = $pvdpublisherid;
}
if ( ! defined $jolietpreparerid && defined $pvdpreparerid ) {
$jolietpreparerid = $pvdpreparerid;
}
if ( ! defined $jolietapplicationid && defined $pvdapplicationid ) {
$jolietapplicationid = $pvdapplicationid;
}
if ( ! defined $jolietcopyrightfileid && defined $pvdcopyrightfileid ) {
$jolietcopyrightfileid = $pvdcopyrightfileid;
}
if ( ! defined $jolietabstractfileid && defined $pvdabstractfileid ) {
$jolietabstractfileid = $pvdabstractfileid;
}
if ( ! defined $jolietbibliographicalfileid && defined $pvdbibliographicalfileid ) {
$jolietbibliographicalfileid = $pvdbibliographicalfileid;
}
}
# IP/SP filenames
my $ipasm = $ipprefix .'.ASM';
my $iplst = $ipprefix .'.LST';
my $ipbin = $ipprefix .'.BIN';
my $spasm = $spprefix .'.ASM';
my $splst = $spprefix .'.LST';
my $spbin = $spprefix .'.BIN';
# this ensures the path tables and extents size can be precisely determined
if ( $ipassemble ) {
&Touch($iplst,$ipbin);
}
if ( $spassemble ) {
&Touch($splst,$spbin);
}
if ( $#scdmakeinc >= 0 ) {
&Touch($inc);
}
# remove temporary files and such
foreach my $file (@tmpFilesToRemove) {
if ( -e $file ) {
unlink($file);
}
}
# determine files/dirs that will be used
&DetermineSCDMakeIncFileInformationForIso();
&DetermineFileInformationForIso();
foreach my $file (@isoNotUsedFiles) {
&SetPublic($file,0);
}
foreach my $file (@isoHiddenFiles) {
&SetPublic($file,1);
}
foreach my $file (@isoPublicFiles) {
&SetPublic($file,2);
}
# ensure root dir is public
$dirs{''}{'public'} = 2;
# determine path table locations and extent locations
my $location = 0x10 + 2;
if ( $joliet ) {
$location++;
}
if ( ! defined $pathTableLittleEndian ) {
$pathTableLittleEndian = $location;
}
&DeterminePathTableIndexes();
my $pathTableByteSize = &GetPathTableByteSize(0);
if ( $pathTableLittleEndian ) {
$location = $pathTableLittleEndian + &NumberOfSectorsForSize($pathTableByteSize);
}
if ( ! defined $pathTableBigEndian ) {
$pathTableBigEndian = $location;
}
if ( $pathTableBigEndian ) {
$location = $pathTableBigEndian + &NumberOfSectorsForSize($pathTableByteSize);
}
if ( $joliet ) {
if ( ! defined $jolietPathTableLittleEndian ) {
$jolietPathTableLittleEndian = $location;
}
if ( $jolietPathTableLittleEndian ) {
$location = $jolietPathTableLittleEndian + &NumberOfSectorsForSize($pathTableByteSize);
}
if ( ! defined $jolietPathTableBigEndian ) {
$jolietPathTableBigEndian = $jolietPathTableLittleEndian + &NumberOfSectorsForSize($pathTableByteSize);
}
if ( $jolietPathTableBigEndian ) {
$location = $jolietPathTableBigEndian + &NumberOfSectorsForSize($pathTableByteSize);
}
}
if ( ! defined $rootExtent ) {
$rootExtent = $location;
}
# determine extent tables length (non-joliet)
my $rootExtentSectorSize = &GetExtentTablesSectorSize(0);
my $jolietRootExtentSectorSize = 0;
if ( $joliet ) {
if ( ! defined $jolietRootExtent ) {
$jolietRootExtent = $rootExtent + $rootExtentSectorSize;
}
$jolietRootExtentSectorSize = &GetExtentTablesSectorSize(1);
}
if ( ! defined $fileExtent ) {
if ( $joliet ) {
$fileExtent = $jolietRootExtent + $jolietRootExtentSectorSize;
} else {
$fileExtent = $rootExtent + $rootExtentSectorSize;
}
}
# makes an include file for the SP (to simplify CD reading)
if ( $#scdmakeinc >= 0 ) {
if ( open( SCDMAKEINC, ">$inc" ) ) {
foreach my $file ( @scdmakeinc ) {
if ( $spassemble &&
( $file eq $spbin ||
$file eq $splst ||
$file eq $inc ) ) {
print "Should not specify $file in \@scdmakeinc\n";
}
my $fileLabel = uc($file);
$fileLabel =~ s/[^A-Z0-9]/_/g;
my $fileStartSectors = $fileExtent + $files{$file}{'offset'};
my $fileSizeBytes = $files{$file}{'sizeBytes'};
my $fileStartBytes = $sectorSize * $fileStartSectors;
my $fileSizeSectors = &NumberOfSectorsForSize($fileSizeBytes);
# by making them %8.8X numbers, changes in the numbers
# will not affect the size of the SCDMAKE.INC file
my $type = 8;
if ( $scdmakeincType & 1 ) {
$type = 4;
if ( $fileStartSectors + $fileSizeSectors > 0x800 * 0x10000 ) {
print STDERR "Using scdmakeincType == 1 and went over 128K for SCDMAKE.INC files, switch to scdmakeincType == 2 and adjust usage for the longword instead of word usage\n";
}
}
my $fileStartSectorsNice = sprintf("\$%${type}.${type}X",$fileStartSectors);
my $fileSizeSectorsNice = sprintf("\$%${type}.${type}X",$fileSizeSectors);
if ( $scdmakeincType == 1 ) {
print SCDMAKEINC " ;; ${fileLabel} sector start and sector size\n";
print SCDMAKEINC " dc.w ${fileStartSectorsNice}, ${fileSizeSectorsNice}\n";
} elsif ( $scdmakeincType == 2 ) {
print SCDMAKEINC " ;; ${fileLabel} sector start and sector size\n";
print SCDMAKEINC " dc.l ${fileStartSectorsNice}, ${fileSizeSectorsNice}\n";
} else {
my $fileStartBytesNice = sprintf("\$%${type}.${type}X",$fileStartBytes);
my $fileSizeBytesNice = sprintf("\$%${type}.${type}X",$fileSizeBytes);
print SCDMAKEINC "FILE_${fileLabel}_START_BYTE: equ ${fileStartBytesNice}\n";
print SCDMAKEINC "FILE_${fileLabel}_SIZE_BYTES: equ ${fileSizeBytesNice}\n";
print SCDMAKEINC "FILE_${fileLabel}_START_SECTOR: equ ${fileStartSectorsNice}\n";
print SCDMAKEINC "FILE_${fileLabel}_SIZE_SECTORS: equ ${fileSizeSectorsNice}\n";
}
}
}
close SCDMAKEINC;
}
# now that SCDMAKE.INC made, can assemble IP and SP
if ( $ipassemble ) {
system("scdasm -v=$verbosity $ipasm $ipbin $iplst");
}
if ( $spassemble ) {
system("scdasm -v=$verbosity $spasm $spbin $splst");
}
if ( ! defined $scdipsize ) {
# some of IP.BIN will be put in 0x200-0x800
my $ipbinSize = -e $ipbin ? -s $ipbin : 0;
# default is max size of US version
# by padding, makes it easier for people to use scdconv or convscd to
# change regions
my $scdiprealstart = 0x200 + 1412;
if ( ! $ippad &&
lc($scdcountry) ne 'all' &&
$scdcountry !~ m/U/i ) {
if ( $scdcountry =~ m/J/i ) {
$scdiprealstart = 0x200 + 342;
} elsif ( $scdcountry =~ m/E/i ) {
$scdiprealstart = 0x200 + 1390;
}
}
my $sizeBytes = $ipbinSize - ($sectorSize - $scdiprealstart);
if ( $sizeBytes > 24*1024 ) {
print STDERR "Sega CD development guide says somewhere around 24K gets in trouble\n";
}
my $sizeSectors = &NumberOfSectorsForSize($sizeBytes);
$scdipsize = $sizeSectors * $sectorSize;
}
if ( ! defined $scdspsize ) {
my $sizeBytes = -e $spbin ? -s $spbin : 0;
if ( $sizeBytes < $sectorSize ) {
$sizeBytes = $sectorSize;
}
my $sizeSectors = &NumberOfSectorsForSize($sizeBytes);
$scdspsize = $sizeSectors * $sectorSize;
}
if ( ! defined $scdspstart ) {
if ( $scdipstart + $scdipsize + $scdspsize > 16 * $sectorSize ) {
die "Could not fit both IP and SP in first 16 sectors, (you can change some variables in SCDMAKE.CFG to do this)\n";
}
$scdspstart = $scdipstart + $scdipsize;
}
# all modified files should be done now, safe to place all the remaining files
&PlaceUnplacedFiles();
if ( $fileExtent + $fileExtentCurrentOffset + $padSize > $sectorCount ) {
$sectorCount = $fileExtent + $fileExtentCurrentOffset + $padSize;
}
my $isoSuffix = 'ISO';
if ( $isoType == 1 ) {
$isoSuffix = 'BIN';
}
# support making all three country scd iso at once
if ( lc($scdcountry) eq 'all' ) {
my $scdhardwareDefined = ( defined $scdhardware );
my $pvdsystemidDefined = ( defined $pvdsystemid );
my $jolietsystemidDefined = ( defined $jolietsystemid );
$scdcountry = 'U';
&MakeISO($outprefix.'_US.'.$isoSuffix);
if ( ! $scdhardwareDefined ) {
$scdhardware = undef;
}
if ( ! $pvdsystemidDefined ) {
$pvdsystemid = undef;
}
if ( ! $jolietsystemidDefined ) {
$jolietsystemid = undef;
}
$scdcountry = 'J';
&MakeISO($outprefix.'_JP.'.$isoSuffix);
if ( ! $scdhardwareDefined ) {
$scdhardware = undef;
}
if ( ! $pvdsystemidDefined ) {
$pvdsystemid = undef;
}
if ( ! $jolietsystemidDefined ) {
$jolietsystemid = undef;
}
$scdcountry = 'E';
&MakeISO($outprefix.'_EU.'.$isoSuffix);
} else {
&MakeISO($outprefix.$outprefixsuffix.'.'.$isoSuffix);
}
###############################################################################
sub MakeISO {
my ($iso) = @_;
if ( $verbosity >= 2 ) {
print "SCD country: $scdcountry\n";
}
# country based defaults if not set up
if ( ! defined $scdhardware ) {
$scdhardware = $scdcountry =~ m/U/i ? 'SEGA GENESIS' : 'SEGA MEGA DRIVE';
}
if ( ! defined $pvdsystemid ) {
$pvdsystemid = $scdcountry =~ m/U/i ? 'SEGA_CD' : 'MEGA_CD';
}
if ( ! defined $jolietsystemid ) {
$jolietsystemid = $pvdsystemid;
}
# how to make a sample iso for the ISO 9660 structure
# mkisofs -iso-level 1 -no-bak -no-pad -o a.iso files/
$isoIndex = 0;
open(ISO, ">$iso") or die "Cannot write $iso: $!\n";
binmode ISO;
if ( $verbosity >= 2 ) {
print "Sector 0: sega cd boot sector and some ip\n";
}
&PrintSegaCDSystemID();
&PrintSegaCDDiscID();
my $scdmakeDir = $0;
$scdmakeDir =~ s/([\\\/\:])[^\\\/\:]+$/$1/;
# region info, and pad so that all region take up 1412 (U is longest)
# makes hexdiff of iso easier to look at
my $propType;
if ( $scdcountry =~ m/U/i ) {
$propType = 'us';
} elsif ( $scdcountry =~ m/J/i ) {
$propType = 'jp';
} else {
$propType = 'eu';
}
&PrintFileToIso("${scdmakeDir}scdmake_${propType}_prop.bin");
if ( $ippad ) {
&PrintFileToIso("${scdmakeDir}scdmake_${propType}_pad.bin");
}
&PrintFileToIso($ipbin);
&FillIsoToEndOfSector(chr(0x00));
if ( $verbosity >= 2 ) {
print "Sector 1-".(&GetIsoSector()-1).": sega cd ip\n";
}
# TODO doesn't play nice when real small?? or when real big?? idk anymore
&FillIsoToIsoByte(chr(0x00),$scdspstart);
&PrintFileToIso($spbin);
&FillIsoToEndOfSector(chr(0x00));
if ( $verbosity >= 2 ) {
print "Sector ".int($scdspstart/$sectorSize)."-".(&GetIsoSector()-1).": sega cd sp\n";
}
# fill with 0x00 to the end of first 16 sectors
&FillIsoToIsoByte(chr(0x00),0x8000);
&PrintPrimaryVolumeDescriptorToIso();
if ( $verbosity >= 2 ) {
print "Sector 16-16: sega cd iso 9660 primary volume descriptor\n";
}
if ( $joliet ) {
&PrintJolietSupplementraryVolumeDescriptorToIso();
if ( $verbosity >= 2 ) {
print "Sector 17-17: sega cd iso 9660 joliet supplementary volume descriptor\n";
}
}
&PrintVolumeDescriptorSetTerminatorToIso();
if ( $verbosity >= 2 ) {
print "Sector ".(&GetIsoSector()-1)."-".(&GetIsoSector()-1).": sega cd iso 9660 volume descriptor set terminator\n";
}
if ( $pathTableLittleEndian ) {
&FillIsoToIsoSector(chr(0x00),$pathTableLittleEndian);
if ( &GetIsoSector() != $pathTableLittleEndian ) {
die "Bad little endian path table start\n";
}
&PrintLittleEndianPathTable(0);
if ( $verbosity >= 2 ) {
print "Sector $pathTableLittleEndian-".(&GetIsoSector()-1).": sega cd iso 9660 little endian path table\n";
}
}
if ( $pathTableBigEndian ) {
&FillIsoToIsoSector(chr(0x00),$pathTableBigEndian);
if ( &GetIsoSector() != $pathTableBigEndian ) {
die "Bad big endian path table start\n";
}
&PrintBigEndianPathTable(0);
if ( $verbosity >= 2 ) {
print "Sector $pathTableBigEndian-".(&GetIsoSector()-1).": sega cd iso 9660 big endian path table\n";
}
}
if ( $joliet ) {
if ( $jolietPathTableLittleEndian ) {
&FillIsoToIsoSector(chr(0x00),$jolietPathTableLittleEndian);
if ( &GetIsoSector() != $jolietPathTableLittleEndian ) {
die "Bad little endian path table start\n";
}
&PrintLittleEndianPathTable(1);
if ( $verbosity >= 2 ) {
print "Sector $jolietPathTableLittleEndian-".(&GetIsoSector()-1).": sega cd iso 9660 joliet little endian path table\n";
}
}
if ( $jolietPathTableBigEndian ) {
&FillIsoToIsoSector(chr(0x00),$jolietPathTableBigEndian);
if ( &GetIsoSector() != $jolietPathTableBigEndian ) {
die "Bad big endian path table start\n";
}
&PrintBigEndianPathTable(1);
if ( $verbosity >= 2 ) {
print "Sector $jolietPathTableBigEndian-".(&GetIsoSector()-1).": sega cd iso 9660 joliet big endian path table\n";
}
}
}
&FillIsoToIsoSector(chr(0x00),$rootExtent);
if ( &GetIsoSector() != $rootExtent ) {
die "Bad root extent table start\n";
}
# (non-joliet)
&PrintExtentTablesToIso(0);
if ( $verbosity >= 2 ) {
print "Sector $rootExtent-".(&GetIsoSector()-1).": sega cd iso 9660 extent tables\n";
}
&FillIsoToEndOfSector(chr(0x00));
if ( $joliet ) {
&FillIsoToIsoSector(chr(0x00),$jolietRootExtent);
if ( &GetIsoSector() != $jolietRootExtent ) {
die "Bad Joliet root extent table start\n";
}
# (joliet)
&PrintExtentTablesToIso(1);
if ( $verbosity >= 2 ) {
print "Sector $jolietRootExtent-".(&GetIsoSector()-1).": sega cd iso 9660 joliet extent tables\n";
}
&FillIsoToEndOfSector(chr(0x00));
}
&FillIsoToIsoSector(chr(0x00),$fileExtent);
if ( &GetIsoSector() != $fileExtent ) {
die "Bad file extent table start, you can change \$fileExtent in SCDMAKE.CFG to ".&GetIsoSector()."\n";
}
&PrintFilesToIso();
if ( $verbosity >= 2 ) {
print "Sector $fileExtent-".(&GetIsoSector()-1).": sega cd iso 9660 file data\n";
}
&FillIsoToEndOfSector(chr(0x00));
&PrintToIso(chr(0x00) x ($padSize * $sectorSize));
&FillIsoToIsoByte(chr(0x00),$sectorCount * $sectorSize);
close ISO;
if ( $isoIndex != $sectorCount * $sectorSize ) {
die "Too many sectors, fix sectorCount handling\n";
}
my $guideText = '';
if ( $isoType == 0 && $audioTracks == 0 ) {
$guideText .= "FILE \"$iso\" BINARY\n";
$guideText .= " TRACK 01 MODE1/2048\n";
$guideText .= " INDEX 01 00:00:00\n";
} elsif ( $isoType == 1 ) {
my $guideText = '';
$guideText .= "FILE \"$iso\" BINARY\n";
$guideText .= " TRACK 01 MODE1/2352\n";
$guideText .= " INDEX 01 00:00:00\n";
}
if ( $guideText ne '' ) {
my $guide = $iso;
$guide =~ s/\.(ISO|BIN)$/.CUE/;
if ( open( GUIDE, ">$guide" ) ) {
print GUIDE $guideText;
} else {
print STDERR "Could not write cue file: $guide\n";
}
close GUIDE;
}
}
sub PrintSegaCDSystemID {
if ( &GetIsoByteIndex() != 0x0000 ) {
die "Cannot start sega cd disc id anywhere except 0x0000\n";
}
# set 16 bytes to disc id
&PrintToIso(&PadString($scddiscid,chr(0x20),16));
# set 11 bytes to volume name
&PrintToIso(&PadString($scdvolumename,chr(0x20),11));
# set 1 byte to zero
&PrintToIso(chr(0x00));
# set 2 bytes to BCD of volume version (0x0100 means released)
&PrintBCDBigEndianDoubleByteToIso($scdvolumeversion);
# set 2 bytes to volume type (0x0001 means CD-ROM)
&PrintBigEndianDoubleByteToIso($scdvolumetype);
# set 11 bytes to system name
&PrintToIso(&PadString($scdsystemname,chr(0x20),11));
# set 1 byte to zero
&PrintToIso(chr(0x00));
# set 2 bytes to BCD of volume version (0x0100 means released)
&PrintBCDBigEndianDoubleByteToIso($scdsystemversion);
# set 2 bytes to zero
&PrintToIso(chr(0x00)x2);
# set 16 bytes for IP information
&PrintBigEndianQuadByteToIso($scdipstart);
&PrintBigEndianQuadByteToIso($scdipsize);
&PrintBigEndianQuadByteToIso(0x00000000);
&PrintBigEndianQuadByteToIso(0x00000000);
# set 16 bytes for SP information
&PrintBigEndianQuadByteToIso($scdspstart);
&PrintBigEndianQuadByteToIso($scdspsize);
&PrintBigEndianQuadByteToIso(0x00000000);
&PrintBigEndianQuadByteToIso(0x00000000);
# set 0xB0 bytes to space
&PrintToIso(chr(0x20)x0xB0);
}
sub PrintSegaCDDiscID {
if ( &GetIsoByteIndex() != 0x0100 ) {
die "Cannot start sega cd disc id anywhere except 0x0100\n";
}
# set 16 bytes to disc id
&PrintToIso(&PadString($scdhardware,chr(0x20),16));
# set 16 bytes to company code and date
&PrintToIso('(C)');
&PrintToIso(&PadString($scdcompanyname,chr(0x20),4));
if ( $date !~ m/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})00$/ ) {
die "Could not parse date: $date\n";
}
my $year = $1;
my $month = $2;
my @months = qw( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC );
&PrintToIso(' ');
&PrintToIso($year);
if ( $month >= 1 && $month <= 12 ) {
&PrintToIso('.');
&PrintToIso($months[$month-1]);
} else {
&PrintToIso(' ');
}
# set 48 bytes to domestic game title
&PrintToIso(&PadString($scdgametitledomestic,chr(0x20),48));
# set 48 bytes to overseas game title
&PrintToIso(&PadString($scdgametitleoverseas,chr(0x20),48));
# set 2 bytes to disc type
&PrintToIso(&PadString($scddisctype,chr(0x20),2));
# set 10 bytes to product code
if ( $scdcompanyname eq 'SEGA' && $scdproductcode =~ m/^[0-9]{4}$/ ) {
if ( $scdcountry !~ m/U/i && $scdcountry =~ m/J/i ) {
&PrintToIso(" G-$scdproductcode -");
} else {
&PrintToIso(" MK-$scdproductcode -");
}
} elsif ( $scdcompanyname =~ m/^T\-..$/ && $scdproductcode =~ m/^[0-9]{5}$/ ) {
&PrintToIso(" T-$scdproductcode -");
} else {
print STDERR "Unsupported scdcompanyname and scdproductcode: $scdcompanyname and $scdproductcode\n";
}
# set 2 bytes to product version
&PrintToIso(&PadString($scdproductversion,chr(0x20),2));
# set 2 bytes to space
&PrintToIso(' ');
# set 16 bytes to IO
&PrintToIso(&PadString($scdio,chr(0x20),16));
# set 30 bytes to space
&PrintToIso(chr(0x20)x30);
# set 10 bytes to modem
&PrintToIso(&PadString($scdmodem,chr(0x20),10));
# set 40 bytes to space
&PrintToIso(chr(0x20)x40);
# set 16 bytes to country
&PrintToIso(&PadString($scdcountry,chr(0x20),16));
}
sub PrintPrimaryVolumeDescriptorToIso {
if ( &GetIsoSectorIndex() != 0 ) {
die "Cannot start primary volume descriptor sector on a sector offset\n";
}
my $space = chr(0x20);
# set 1 byte volume descriptor type to primary volume descriptor
&PrintByteToIso(0x01);
# set 6 bytes standard identifier (CD001 for ISO 9660)
&PrintToIso("CD001");
# set 1 byte to volume descriptor version
&PrintByteToIso(0x01);
# set 1 byte to zero
&PrintByteToIso(0x00);
# set 32 bytes to system identifier
&PrintToIso(&PadString(&CheckACharacters($pvdsystemid),$space,32));
# set 32 bytes to volume identifier
&PrintToIso(&PadString(&CheckDCharacters($pvdvolumeid),$space,32));
# set 8 bytes to zero
&PrintToIso(chr(0x00)x8);
# set 8 bytes as both endian double word total number of sectors
&PrintBothEndianQuadByteToIso($sectorCount);
# set 32 bytes to zero
&PrintToIso(chr(0x00)x32);
# set 4 bytes as both endian word volume set size
&PrintBothEndianDoubleByteToIso(0x0001);
# set 4 bytes as both endian word volume sequence number
&PrintBothEndianDoubleByteToIso(0x0001);
# set 4 bytes as both endian word sector size
&PrintBothEndianDoubleByteToIso($sectorSize);
# set 8 bytes as both double endian word path table size (already computed)
&PrintBothEndianQuadByteToIso($pathTableByteSize);
# set 4 bytes as little endian double word little endian path table
&PrintLittleEndianQuadByteToIso($pathTableLittleEndian);
# set 4 bytes as little endian double word 2nd little endian path table
&PrintLittleEndianQuadByteToIso(0x00000000);
# set 4 bytes as big endian double word big endian path table
&PrintBigEndianQuadByteToIso($pathTableBigEndian);
# set 4 bytes as big endian double word 2nd big endian path table
&PrintBigEndianQuadByteToIso(0x00000000);
# print root extent
&PrintExtentToIso($dirs{''}{'extentStart'}{0},$dirs{''}{'extentSize'}{0},chr(0x02),chr(0x00),0,0);
# set 128 bytes to volume set identifier
&PrintToIso(&PadString(&CheckDCharacters($pvdvolumesetid),$space,128));
# set 128 bytes to publisher identifier
&PrintToIso(&PadString(&CheckACharacters($pvdpublisherid),$space,128));
# set 128 bytes to data preparer identifier
&PrintToIso(&PadString(&CheckACharacters($pvdpreparerid),$space,128));
# set 128 bytes to application identifier
&PrintToIso(&PadString(&CheckACharacters($pvdapplicationid),$space,128));
# set 37 bytes to copyright file identifier
&PrintToIso(&PadString(&CheckFileName($pvdcopyrightfileid),$space,37));
# set 37 bytes to abstract file identifier
&PrintToIso(&PadString(&CheckFileName($pvdabstractfileid),$space,37));
# set 37 bytes to bibliographical file identifier
&PrintToIso(&PadString(&CheckFileName($pvdbibliographicalfileid),$space,37));
# set 17 bytes to date and time of volume creation
&PrintCurrentTimeVDToIso();
# set 17 bytes to date and time of most recent modification
&PrintCurrentTimeVDToIso();
# set 17 bytes to date and time of volume expires
&PrintUnspecifiedTimeVDToIso();
# set 17 bytes to date and time of volume is effective
&PrintCurrentTimeVDToIso();
# set 1 byte to 1
&PrintByteToIso(0x01);
# set 1 byte to 0
&PrintByteToIso(0x00);
# set 512 bytes reserved for application use
&PrintToIso(chr(0x00)x512);
# set 653 bytes to zeros
&PrintToIso(chr(0x00)x653);
if ( &GetIsoSectorIndex() != 0 ) {
die "Did not write full sector for primary volume descriptor\n";
}
}
sub PrintJolietSupplementraryVolumeDescriptorToIso {