-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
253 lines (236 loc) · 9.18 KB
/
index.php
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
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
;Select VGM file to process (Must be .vgm, not .vgz) :<br/>
<input type="file" name="file" id="file" /><br/>
<input type="submit" name="submit" value="Process"/>
</form>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// get size of the binary file
if ( isset($_POST["submit"]) ) {
if ( isset($_FILES["file"])) {
//if there was an error uploading the file
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else {
//Print file details
echo ";Upload: " . $_FILES["file"]["name"] . "<br />";
//echo ";Type: " . $_FILES["file"]["type"] . "<br />";
echo ";Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /><br/>";
//echo ";Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
$storagename = "track.vgm";
move_uploaded_file($_FILES["file"]["tmp_name"], $storagename);
}
} else {
echo ";No file selected <br />";
}
}
$track = 'track.vgm';
$output = 'track.txt';
$filesize = filesize($track);
// open file for reading in binary mode
$fp = fopen($track, 'rb');
$fpout = fopen($output, 'w');
// read the entire file into a binary string
$binary = fread($fp, $filesize);
// finally close the file
fclose($fp);
// unpack the data - notice that we create a format code using 'C%d'
// that will unpack the size of the file in unsigned chars
$unpacked = unpack(sprintf('C%d', $filesize), $binary);
// reset array keys
$unpacked = array_values($unpacked);
// this variable holds the size of *one* structure in the file
//$block_size = 3;
// figure out the number of blocks in the file
//$block_count = $filesize/$block_size;
function hex2str($hex) {
$str = '';
for($i=0;$i<strlen($hex);$i+=2) $str .= chr(hexdec(substr($hex,$i,2)));
return $str;
}
// you now should have an array where each element represents a
// unsigned char from the binary file, so to display Day, Month and Year
$i = 52; //offset vgm data start
$datasize = 0;
$bank = 0;
$dataOffset = hexdec(dechex($unpacked[$i+3]).dechex($unpacked[$i+2]).dechex($unpacked[$i+1]).dechex($unpacked[$i+0]));
$GD3offset = 20+hexdec(dechex($unpacked[23]).dechex($unpacked[22]).dechex($unpacked[21]).dechex($unpacked[20]));
$GD3size = 12+hexdec(substr(0 . dechex($unpacked[$GD3offset+11]),-2).substr(0 . dechex($unpacked[$GD3offset+10]),-2).substr(0 . dechex($unpacked[$GD3offset+9]),-2).substr(0 . dechex($unpacked[$GD3offset+8]),-2));
//print substr(0 . dechex($unpacked[$GD3offset+11]),-2).substr(0 . dechex($unpacked[$GD3offset+10]),-2).substr(0 . dechex($unpacked[$GD3offset+9]),-2).substr(0 . dechex($unpacked[$GD3offset+8]),-2).'<br>';
//print $GD3size;
print "<br/>;";
fwrite($fpout,"\r\n;");
for($y=12;$y < ($GD3size);$y++){
//print '<b>'.substr(0 . dechex($unpacked[$y+$GD3offset]),-2).'</b>';
if(dechex($unpacked[$y+$GD3offset])!=0){
//print hex2str(substr(0 . dechex($unpacked[$y+$GD3offset]),-2));
$str = substr(0 . dechex($unpacked[$y+$GD3offset+1]),-2).substr(0 . dechex($unpacked[$y+$GD3offset]),-2);
print mb_convert_encoding(hex2bin($str), 'UTF-8', 'UCS-2BE');
fwrite($fpout,mb_convert_encoding(hex2bin($str), 'UTF-8', 'UCS-2BE'));
}
//if(dechex($unpacked[$y+$GD3offset+1])==0){print hex2str(substr(0 . dechex($unpacked[$y+$GD3offset]),-2).substr(0 . dechex($unpacked[$y+$GD3offset+1]),-2));}
if($y+1 < ($GD3size)){
if((dechex($unpacked[$y+$GD3offset]).dechex($unpacked[$y+$GD3offset+1]))==0){
print "<br/>;";
fwrite($fpout,"\r\n;");
}
}
}
$i = $i+$dataOffset;
/*print ';Start Offset: '.$i.'Data Offset: '.$dataOffset.'<br/>';*/
fwrite($fpout,';Start Offset: '.$i.'Data Offset: '.$dataOffset."\r\n");
while ($i < $filesize) {
/* if(($bank == 0)&&($datasize > 7500)){
print ' jmp $8000<br/>';
$bank++;
print '.segment "BANK'.$bank.'"<br/><br/>';
$datasize = 0;
}
else if ($datasize > 16000){
print ' jmp $8000<br/>';
$bank++;
print '.segment "BANK'.$bank.'"<br/><br/>';
$datasize = 0;
}*/
if ($datasize > 7900){
$bank++;/*
print '
<br/> lda #%0000011'. ($bank)%2 .'
<br/> STA MMC3_BANK_SELECT
<br/> LDA #'.$bank.'
<br/> STA MMC3_BANK_DATA
<br/>';*/
fwrite($fpout,' lda #%0000011'. ($bank)%2 .'
STA MMC3_BANK_SELECT
LDA #'.$bank.'
STA MMC3_BANK_DATA
');
if(($bank+1)%2){
/*print ' jmp $8000<br/>';*/
fwrite($fpout,"jmp $8000\r\n");
}
else{
/*print ' jmp $A000<br/>';*/
fwrite($fpout,'jmp $A000' . "\r\n");
}
if($bank < 10){
/*print '<br/><br/>.segment "BANK0'.$bank.'"<br/><br/>';*/
fwrite($fpout,'.segment "BANK0'.$bank.'"'."\r\n\r\n");
}
else {
/*print '<br/><br/>.segment "BANK'.$bank.'"<br/><br/>';*/
fwrite($fpout,'.segment "BANK'.$bank.'"'."\r\n\r\n");
}
$datasize = 0;
}
if($unpacked[$i] == hexdec(61)){
$delay = hexdec(dechex($unpacked[$i+2]) . dechex($unpacked[$i+1]));
while($delay > 0){
if($delay > 14306){
fwrite($fpout," LDY #0\r\n LDA #255\r\n jsr delayloop\r\n\r\n");
$delay = $delay - 14306;
}
else{
$delayY = (41*$delay-23)/9%256;
$delayA = floor((41*$delay-23)/9/256);
fwrite($fpout,' LDY #'.$delayY."\r\n LDA #".$delayA."\r\n jsr delayloop\r\n\r\n");
$delay = 0;
}
$datasize+=7;}
}
else if($unpacked[$i] == hexdec(62)){
//print ' jsr delayloop735<br /><br />';
fwrite($fpout," jsr delayloop735\r\n\r\n");
$i++;
$datasize+=3;
continue;
}
else if($unpacked[$i] == hexdec(63)){
//print ' jsr delayloop882<br /><br />';
fwrite($fpout," jsr delayloop882\r\n\r\n");
$i++;
$datasize+=3;
continue;
}
else if(($unpacked[$i] >= hexdec(70))&&($unpacked[$i] <= hexdec("7F"))){
//print ' jsr delayloop'.($unpacked[$i]-hexdec("6F")) .'<br /><br />';
fwrite($fpout,' jsr delayloop'.($unpacked[$i]-hexdec("6F")) ."\r\n\r\n");
$i++;
$datasize+=3;
continue;
}
else if(($unpacked[$i] == hexdec(57))|($unpacked[$i] == hexdec(56))|($unpacked[$i] == hexdec('A0'))|($unpacked[$i] == hexdec(55))|($unpacked[$i] == hexdec(58))|($unpacked[$i] == hexdec(59))|($unpacked[$i] == hexdec(51))|($unpacked[$i] == hexdec(53))|($unpacked[$i] == hexdec(52))){
$address = substr(0 . dechex($unpacked[$i+1]),-2);
$data = substr(0 . dechex($unpacked[$i+2]),-2);
//print ' LDA #$' . strtoupper($address) . ' <br />';
fwrite($fpout,' LDA #$' . strtoupper($address) . "\r\n");
//print ' LDY #$' . strtoupper($data) . ' <br />';
fwrite($fpout,' LDY #$' . strtoupper($data) . "\r\n");
if(($unpacked[$i] == hexdec(57))|($unpacked[$i] == hexdec(59))|($unpacked[$i] == hexdec(53))){
if($address < 16){
fwrite($fpout," jsr sendtoYM2LowDelay\r\n\r\n");
}
//print ' jsr sendtoYM2 <br /><br />';
else {
fwrite($fpout," jsr sendtoYM2\r\n\r\n");
}
}
else{
if($address < 16){
fwrite($fpout," jsr sendtoYMLowDelay\r\n\r\n");
}
//print ' jsr sendtoYM <br /><br />';
else {
fwrite($fpout," jsr sendtoYM\r\n\r\n");
}
}
$datasize+=7;
}
else if($unpacked[$i] == hexdec('B4')){
$address = substr(0 . dechex($unpacked[$i+1]),-2);
$data = substr(0 . dechex($unpacked[$i+2]),-2);
if($unpacked[$i+1] == hexdec('3F')){
$address = '23';
}
else if(($unpacked[$i+1] < hexdec('3F'))&($unpacked[$i+1] > hexdec('1F'))){
$address = dechex(hexdec($address) + hexdec('60')) ;
}
//print ' LDA #$' . strtoupper($data) . ' <br />';
fwrite($fpout,' LDA #$' . strtoupper($data) . "\r\n");
//print ' STA $40' . strtoupper($address) . ' <br /><br />';
fwrite($fpout,' STA $40' . strtoupper($address) . "\r\n\r\n");
$datasize+=5;
}
else if($unpacked[$i] == hexdec(66)) {
//print ';end';
fwrite($fpout,' jmp main_loop');
fwrite($fpout,';end');
break;
}
else if($unpacked[$i] == hexdec(67)) {
$datablocksize = hexdec(substr(0 . dechex($unpacked[$i+6]),-2) . substr(0 . dechex($unpacked[$i+5]),-2) . substr(0 . dechex($unpacked[$i+4]),-2) . substr(0 . dechex($unpacked[$i+3]),-2));
//print ';datablock start: size:'.$datablocksize.'bytes (ignored)<br/>;';
fwrite($fpout,';datablock start: size:'.$datablocksize."bytes (ignored)\r\n;");
for($y=0;$y < ($datablocksize+7);$y++){
//print substr(0 . dechex($unpacked[$y+$i]),-2);
fwrite($fpout,substr(0 . dechex($unpacked[$y+$i]),-2));
if((($y-7)%32)==0){
//print '<br/>;';
fwrite($fpout,"\r\n;");
}
}
//print '<br/>;datablock end<br/><br/>';
fwrite($fpout,"\r\n;datablock end\r\n\r\n");
$i = $i + $datablocksize + 7;
//$i = $i + 7;
continue;
}
//print ";".$i." " .substr(0 . dechex($unpacked[$i]),-2)."<br/>";
$i+=3;
}
fclose($fpout);
print '<br/><a href="track.txt">VGM ASM</a>';
?>