-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgbsinfo.js
268 lines (241 loc) · 8.01 KB
/
gbsinfo.js
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
"use strict";
/*
* This file is part of gbsinfo
*
* gbsinfo 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 version 3.
*
* gbsinfo 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, see <https://www.gnu.org/licenses/>.
*
* ----------------------------
* gbsplay is a Gameboy sound player
*
* (C) 2003-2021 by Tobias Diedrich <[email protected]>
* Christian Garbs <[email protected]>
* Maximilian Rehkopf <[email protected]>
* Vegard Nossum <[email protected]>
* ----------------------------
*
* The following code is a derivative work of the code from the gbsplay project,
* which is licensed GPLv3.
*
* This code therefore is licensed under the terms of the
* GNU Public License, version 3.
*
* GPL-3.0-only
*/
const GBS_HEADER_LENGTH = 0x70;
const GBHW_CLOCK = 1 << 22;
const DEBUG = false;
const log = { "debug" : function print( ...message ) {
if (DEBUG) {
console.log("debug - ", message);
}
}
};
if ( isNode() ) {
var process = require("process");
var fs = require("fs");
var args = process.argv;
log.debug( args );
var files = args.slice(2);
if (files) {
files.forEach( readBinaryFile );
}
}
function readBinaryFile( file ) {
fs.open( file, "r", function(status, fd) {
if (status) {
console.error( status.message );
process.exit(-1);
}
var buffer = Buffer.alloc( GBS_HEADER_LENGTH );
fs.read(fd, buffer, 0, GBS_HEADER_LENGTH, 0, function read(err, bytes, buffer) {
if (err) {
console.error( err );
process.exit(-1);
}
if (bytes > 0) {
log.debug("bytes read:", bytes);
buffer.arrayBuffer = function arrayBuffer() {
return Promise.resolve(buffer.buffer);
};
readFile( { "files" : [ buffer ] } );
}
fs.close(fd, function handle(err) {
if (err) {
console.error( err );
process.exit(-1);
}
});
log.debug("File closed successfully");
});
});
}
function readFile(input) {
var file = input.files[0];
var utf8 = isNode() ? false : document.getElementById("encoding").checked;
file.arrayBuffer().then(function parseHeader(wholeFile) {
var readChar = utf8 ? readUtf8 : readAscii;
log.debug("utf8", utf8);
var LITTLE_ENDIAN = true;
var fileSize = wholeFile.byteLength;
var header = wholeFile.slice(0, GBS_HEADER_LENGTH);
var view = new DataView(header);
var timerModulo = view.getUint8(14);
var timerControl = view.getUint8(15);
log.debug("tac:", timerControl);
log.debug("tma:", timerModulo);
var gbsHeader = {
identifier : readChar(header.slice(0,3)),
version : view.getUint8(3),
songs : view.getUint8(4),
firstSong : view.getUint8(5),
loadAddress : view.getUint16(6, LITTLE_ENDIAN),
initAddress : view.getUint16(8, LITTLE_ENDIAN),
playAddress : view.getUint16(10, LITTLE_ENDIAN),
stackPointer : view.getUint16(12, LITTLE_ENDIAN),
title : readChar(header.slice(16, 16+32)),
author : readChar(header.slice(48, 48+32)),
copyright : readChar(header.slice(80, 80+32)),
timing : interruptRate(
{
tac: timerControl,
tma: timerModulo
})
};
gbsHeader.file = {
size: fileSize,
romSize : romSize(fileSize, gbsHeader.loadAddress),
};
gbsHeader.file.banks = banks( gbsHeader.file.romSize );
if (!validIdentifier( gbsHeader.identifier )) {
var error = `Not a GBS-File: ${file.name}`;
}
if ( isNode() ) {
if (error) {
console.error(error);
process.exit(-1);
}
console.log( JSON.stringify( gbsHeader ) );
}
else {
setTextarea( gbsHeader, error ); // browser
}
}
).catch( function handle(err) {
console.error(err);
});
}
function validIdentifier( identifier ) {
return identifier === "GBS";
}
function romSize( fileSizeInBytes, loadAddress ) {
var codelen = fileSizeInBytes - GBS_HEADER_LENGTH;
var magic = 0x3FFF;
var size = (codelen + loadAddress + magic) & ~magic;
return size;
}
function banks( romSizeBytes ) {
var MAX_ROM_SIZE = 1 << 14; // 4 megabit (4 mebibytes)
return romSizeBytes / MAX_ROM_SIZE;
}
function interruptRate( { tac, tma } ) {
if (tac & 0x04) {
var result = gbhwCalcTimerHz(tac, tma);
var ugetab = (tac & 0x78) === 0x40;
var val = formatTimer( result );
return ugetab ? `${val} + VBlank (ugetab)` : val;
}
else if ( (tac & 0x80) === 0) {
return "59.7Hz VBlank";
}
else {
throw new Error("Unknown interrupt rate");
}
}
function formatTimer( result ) {
return `${result.toFixed(2)}Hz timer`;
}
function gbhwCalcTimerHz(tac, tma) {
var timertc = tacToCycles( tac );
return GBHW_CLOCK / timertc / (256 - tma);
}
function tacToCycles( tac ) {
var lookup = [
GBHW_CLOCK / 4096, /* 1024 CPU cycles per TIMA tick */
GBHW_CLOCK / 262144, /* 16 CPU cycles per TIMA tick */
GBHW_CLOCK / 65536, /* 64 CPU cycles per TIMA tick */
GBHW_CLOCK / 16384, /* 256 CPU cycles per TIMA tick */
];
var timertc = lookup[ tac & 3 ];
return (tac & 0xF0) == 0x80 ? timertc / 2 : timertc; // emulate GBC mode
}
function setTextarea(tags, err) {
var textArea = document.getElementById("gbsHeader");
if (err) {
textArea.value = err;
}
else {
var {
version, title, author, copyright, loadAddress, initAddress,
playAddress, stackPointer, file, songs, firstSong, timing } = tags || {};
var fileSize = file.size.toString(16).padStart(8, 0);
var paddedRomSize = file.romSize.toString(16).padStart(8, 0);
textArea.value = `
GBSVersion: ${version}
Title: ${title}
Author: ${author}
Copyright: ${copyright}
Load address: 0x${loadAddress.toString(16).padStart(4, 0)}
Init address: 0x${initAddress.toString(16).padStart(4, 0)}
Play address: 0x${playAddress.toString(16)}
Stack pointer: 0x${stackPointer.toString(16)}
File size: 0x${fileSize}
ROM size: 0x${paddedRomSize} (${file.banks} banks)
Subsongs: ${songs}
Default subsong: ${firstSong}
Timing: ${timing}
`.trimStart();
}
}
function readAscii(buffer) {
var view = new Uint8Array(buffer);
var len = length(view);
var text = "";
for (var i = 0; i < len; i++) {
var ch = String.fromCharCode( view[i] );
text = text + ch;
}
return text;
}
function readUtf8(buffer) {
var decoder = new TextDecoder("utf-8");
var view = new Uint8Array(buffer);
var nullOffset = length(view);
return decoder.decode(view.slice(0, nullOffset));
}
// returns offset for NULL-character if found
// else, return length
function length(view) {
var NULL = 0;
var nullPos = view.indexOf(NULL);
return ~nullPos ? nullPos : view.length;
}
function isNode() {
return typeof window === "undefined";
}
// required for node unit tests
if (isNode() ) {
module.exports = {
readUtf8, readAscii, romSize, banks, formatTimer, length, tacToCycles,
interruptRate, validIdentifier
};
}