Skip to content

Commit

Permalink
Load binary ROMs directly
Browse files Browse the repository at this point in the history
  • Loading branch information
bfirsh committed Jun 6, 2010
1 parent b606f81 commit 4e33c42
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 58 deletions.
26 changes: 0 additions & 26 deletions bin2js.pl

This file was deleted.

14 changes: 11 additions & 3 deletions index.html
Expand Up @@ -158,9 +158,17 @@ <h3>7th January 2010</h3>
resetButtons();
var romName = $("#roms").val();
$("#status").text("Downloading "+romName);
$.getScript("http://benfirshman.com/projects/jsnes/roms/"+escape(romName)+".js", function(){
nes.loadRom(romName);
nes.start();
$.ajax({
url: "roms/"+escape(romName),
xhr: function() {
var xhr = $.ajaxSettings.xhr()
xhr.overrideMimeType('text/plain; charset=x-user-defined');
return xhr;
},
success: function(data) {
nes.loadRom(data);
nes.start();
}
});
});

Expand Down
35 changes: 16 additions & 19 deletions js.2/nes.js
Expand Up @@ -27,6 +27,7 @@ NES.prototype = {
isRunning: false,
fpsFrameCount: 0,
limitFrames: true,
romData: null,

// Resets the system.
reset: function() {
Expand All @@ -44,7 +45,6 @@ NES.prototype = {

if(this.rom != null && this.rom.valid) {
if (!this.isRunning) {
//$("#status").text("Running "+this.romFile)
this.isRunning = true;

this.frameInterval = setInterval(function() {
Expand Down Expand Up @@ -151,41 +151,38 @@ NES.prototype = {
},

reloadRom: function() {
if(this.romFile != null){
this.loadRom(this.romFile);
if(this.romData != null){
this.loadRom(this.romData);
}
},

// Loads a ROM file into the CPU and PPU.
// The ROM file is validated first.
loadRom: function(file){
// Can't load ROM while still running.
if(this.isRunning)
loadRom: function(data) {
if (this.isRunning) {
this.stop();
}

$("#status").text("Loading "+file);
$("#status").text("Loading...");

// Load ROM file:
this.rom = new NES.ROM(this);
this.rom.load(file);
if(this.rom.valid){

// The CPU will load
// the ROM into the CPU
// and PPU memory.

this.rom.load(data);

if (this.rom.valid) {
this.reset();

this.mmap = this.rom.createMapper();
if (!this.mmap) return;
if (!this.mmap) {
return;
}
this.mmap.loadROM();
this.ppu.setMirroring(this.rom.getMirroringType());
this.romFile = file;
this.romData = data;

$("#status").text(file+" successfully loaded. Ready to be started.");
$("#status").text("Successfully loaded. Ready to be started.");
}
else {
$("#status").text(file+" is an invalid ROM!");
$("#status").text("Invalid ROM!");
}
return this.rom.valid;
},
Expand Down
14 changes: 4 additions & 10 deletions js.2/rom.js
Expand Up @@ -70,20 +70,14 @@ NES.ROM.prototype = {
mapperType: null,
valid: false,

load: function(fileName) {
this.fileName = fileName;
if (!roms[fileName]) {
alert("ROM does not exist.");
return;
}
var data = roms[fileName];
load: function(data) {
if (data.indexOf("NES\x1a") == -1) {
alert("Not a valid NES ROM.");
return;
}
this.header = new Array(16);
for (var i = 0; i < 16; i++) {
this.header[i] = data.charCodeAt(i);
this.header[i] = data.charCodeAt(i) & 0xFF;
}
this.romCount = this.header[4];
this.vromCount = this.header[5]*2; // Get the number of 4kB banks, not 8kB
Expand Down Expand Up @@ -115,7 +109,7 @@ NES.ROM.prototype = {
if (offset+j >= data.length) {
break;
}
this.rom[i][j] = data.charCodeAt(offset + j);
this.rom[i][j] = data.charCodeAt(offset + j) & 0xFF;
}
offset += 16384;
}
Expand All @@ -127,7 +121,7 @@ NES.ROM.prototype = {
if (offset+j >= data.length){
break;
}
this.vrom[i][j] = data.charCodeAt(offset + j);
this.vrom[i][j] = data.charCodeAt(offset + j) & 0xFF;
}
offset += 4096;
}
Expand Down

0 comments on commit 4e33c42

Please sign in to comment.