Skip to content

Commit

Permalink
Refactoring. Create object. Reduce variable scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ullenius committed Jul 19, 2022
1 parent d4176d2 commit edc8873
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions gbsinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@
function readFile(input) {
var file = input.files[0];

var utf8 = document.getElementById("encoding").checked;
var readChar = utf8 ? readUtf8 : readAscii;
console.log("DEBUG utf8", utf8);
var LITTLE_ENDIAN = true;

file.arrayBuffer().then(function parseHeader(wholeFile) {
var utf8 = document.getElementById("encoding").checked;
var readChar = utf8 ? readUtf8 : readAscii;
console.log("DEBUG utf8", utf8);
var LITTLE_ENDIAN = true;

var header = wholeFile.slice(0, 0x70);
var view = new DataView(header);

var identifier = readChar(header.slice(0,3)); // unused
var version = view.getUint8(3);
var songs = view.getUint8(4);
var firstSong = view.getUint8(5);

var loadAddress = view.getUint16(6, LITTLE_ENDIAN);
var initAddress = view.getUint16(8, LITTLE_ENDIAN);
var playAddress = view.getUint16(10, LITTLE_ENDIAN);
var stackPointer = view.getUint16(12, LITTLE_ENDIAN);

var timerModulo = view.getUint8(14); // unused
var timerControl = view.getUint8(15); // unused
console.log("timerModulo", timerModulo, "timerControl", timerControl);
var gbsHeader = {
identifier : readChar(header.slice(0,3)), // unused
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),
timerModulo : view.getUint8(14), // unused
timerControl : view.getUint8(15), // unused
title : readChar(header.slice(16, 16+32)),
author : readChar(header.slice(48, 48+32)),
copyright : readChar(header.slice(80, 80+32))
};
console.log("timerModulo", gbsHeader.timerModulo);
console.log("timerControl", gbsHeader.timerControl);

var title = readChar(header.slice(16,16+32));
var author = readChar(header.slice(48, 48+32));
var copyright = readChar(header.slice(80, 80+32));
setTextarea( {
version, title, author, copyright, loadAddress, initAddress,
playAddress, stackPointer, songs, firstSong });
setTextarea( gbsHeader );
}
).catch( function handle(err) {
console.log(err);
Expand Down

0 comments on commit edc8873

Please sign in to comment.