-
Notifications
You must be signed in to change notification settings - Fork 2
/
scdbrm
executable file
·225 lines (193 loc) · 6.74 KB
/
scdbrm
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
#!/usr/bin/perl
###############################################################################
# Copyright (c) 2014 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.
###############################################################################
use strict;
use warnings;
my $blockSize = 0x40;
my $bramSize = 0x2000;
if ( $#ARGV < 0 ) {
&Help();
}
my $prefix = '';
my $tableLine = "+----------+-------------+----+-------+------+\n";
my $tableHead = "| DRIVE | FILENAME | DP | INDEX | SIZE |\n";
my $tableStarted = 0;
foreach my $fileIn (@ARGV) {
my $fileInData = '';
my $fileInLength = -s $fileIn;
$prefix = $fileIn;
$prefix =~ s/\.[^\.]+$//;
$prefix =~ s/^[^[\\\/]*[\\\/]//g;
$prefix .= '_';
my $fileOut = 'out.bin';
my $fileOutData = '';
open(FILEIN, '<'.$fileIn);
binmode FILEIN;
read(FILEIN,$fileInData,$fileInLength);
close FILEIN;
my $out = '';
my $internal = '';
my $cart = '';
if ( $fileIn =~ /\.crm/i ) {
# RAM cartridge
$cart = &BRAMDecodeDirectory('CART ',$fileInData);
} else {
# internal RAM
if ( $fileInLength >= $bramSize ) {
my $bramData = substr($fileInData,0,$bramSize);
$internal = &BRAMDecodeDirectory('INTERNAL',$bramData);
}
# optional RAM cartridge
if ( $fileInLength > $bramSize ) {
my $cartData = substr($fileInData,$bramSize,$fileInLength-$bramSize);
$cart = &BRAMDecodeDirectory('CART ',$cartData);
}
}
if ( $internal ne '' ) {
if ( ! $tableStarted ) {
$out .= $tableLine.$tableHead.$tableLine;
$tableStarted = 1;
}
$out .= $internal.$tableLine;
}
if ( $cart ne '' ) {
if ( ! $tableStarted ) {
$out .= $tableLine.$tableHead.$tableLine;
$tableStarted = 1;
}
$out .= $cart.$tableLine;
}
print $out;
for ( my $index = 0; $index + $blockSize <= $fileInLength; $index += $blockSize ) {
my $block = substr($fileInData,$index,$blockSize);
my $decoded = &BRAMDecodeDataProtectedBlock($block);
$fileOutData .= $decoded;
}
open(FILEOUT, '>'.$fileOut);
binmode FILEOUT;
print FILEOUT $fileOutData;
close FILEOUT;
}
sub BRAMDecodeDirectory {
my ($drive,$bramData) = @_;
# TODO format check?
my $bramDataLength = length($bramData);
if ( $bramDataLength & 0x3F ) {
print STDERR "Bad bram size\n";
return '';
}
my $lastBlockIndex = $bramDataLength - $blockSize;
if ( substr($bramData,$lastBlockIndex) =~ /^[\x00]+$/ ) {
return sprintf("| %s | %s | | | |%s\n",$drive,' ',' (unformatted drive)');
}
my $blocksFree = unpack("n",substr($bramData,$lastBlockIndex+0x10,2));
if ( $blocksFree != unpack("n",substr($bramData,$lastBlockIndex+0x12,2)) ||
$blocksFree != unpack("n",substr($bramData,$lastBlockIndex+0x14,2)) ||
$blocksFree != unpack("n",substr($bramData,$lastBlockIndex+0x16,2)) ) {
print STDERR "Inconsistent blocks free value\n";
return '';
}
my $filesUsed = unpack("n",substr($bramData,$lastBlockIndex+0x18,2));
if ( $filesUsed != unpack("n",substr($bramData,$lastBlockIndex+0x1A,2)) ||
$filesUsed != unpack("n",substr($bramData,$lastBlockIndex+0x1C,2)) ||
$filesUsed != unpack("n",substr($bramData,$lastBlockIndex+0x1E,2)) ) {
print STDERR "Inconsistent files used value\n";
return '';
}
if ( $blocksFree == 0 && $filesUsed == 0 ) {
print STDERR "Zero files used and zero blocks free makes no sense\n";
return '';
}
my $directoryIndex = $lastBlockIndex - $filesUsed * 0x20;
if ( $directoryIndex < 0x40 + $filesUsed * 0x40 ) {
print STDERR "Directory table too big";
return '';
}
my $table = '';
if ( $filesUsed == 0 ) {
$table .= sprintf("| %s | %s | | | |%s\n",$drive,' ',' (empty drive)');
} else {
for ( my $i = $lastBlockIndex-0x20; $i >= $directoryIndex; $i -= 0x20 ) {
my $block = substr($bramData,$i & 0xFFFFC0,$blockSize);
my $decoded = &BRAMDecodeDataProtectedBlock($block);
my $entry = substr($decoded,($i & 0x3F) >> 1,0x10);
my $fileName = substr($entry,0,11);
my $fileDatProt = ord(substr($entry,11,1));
my $fileBlockStart = unpack("n",substr($entry,12,2));
my $fileBlockSize = unpack("n",substr($entry,14,2));
my $fileData = '';
my $extra = '';
if ( ( $fileBlockStart + $fileBlockSize ) * $blockSize <= $directoryIndex ) {
for ( my $j = 0; $j < $fileBlockSize; $j++ ) {
my $block = substr($bramData,($j+$fileBlockStart) * $blockSize,$blockSize);
if ( $fileDatProt ) {
$block = &BRAMDecodeDataProtectedBlock($block);
}
$fileData .= $block;
}
if ( open(SAV, '>'.$prefix.$fileName.'.SAV') ) {
binmode SAV;
print SAV $fileData;
}
close SAV;
if ( $fileData !~ /[^\x00]/ ) {
$extra = ' (data is all zeros)';
}
} else {
$extra = ' (invalid start/offset)';
}
$table .= sprintf("| %s | %s | %2.2X | %4.4X | %4.4X |%s\n",$drive,$fileName,$fileDatProt,$fileBlockStart,$fileBlockSize,$extra);
}
}
return $table;
}
sub BRAMDecodeDataProtectedBlock {
my ($dataIn) = @_;
if ( length($dataIn) != $blockSize ) {
die "Bad block\n";
}
my $dataOut = '';
my $bitsCollector = 0;
my $bitsCollected = 0;
my $dataInIndex = 2;
my $bytesCollected = 0;
$bitsCollector = ( ord(substr($dataIn,$dataInIndex++,1)) & 0x0C ) << 12;
$bitsCollected = 2;
while ( $bytesCollected < 0x20 ) {
$bitsCollector = $bitsCollector | ( ( ord(substr($dataIn,$dataInIndex++,1)) & 0xFC ) << ( 8 - $bitsCollected ) );
$bitsCollected += 6;
if ( $bitsCollected >= 8 ) {
$dataOut .= chr( ( $bitsCollector >> 8 ) & 0x00FF );
$bytesCollected++;
$bitsCollector = $bitsCollector << 8;
$bitsCollected -= 8;
}
}
return $dataOut;
}
sub Help {
die '
scdbrm <brmfiles>
[description]
Decodes directory to screen and outputs each file (decodes data protected).
Also generates out.bin which is every block decoded as if it was data
protected. Treats *.crm and *.CRM files as RAM catridge only data.
All other files are treated as Gens style brm files (first 8k represents
internal RAM and any data after that is the optional RAM cartridge data).
';
}