summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Firshman <ben@firshman.co.uk>2010-06-06 23:07:22 +0100
committerBen Firshman <ben@firshman.co.uk>2010-06-06 23:07:22 +0100
commit4e33c42d25ee9d13ba61dd4c69765efc80a15399 (patch)
treef5804bfb76613796697782a187ee439530a096b7
parentb606f81397be158f5547c570de8fc568f68b0237 (diff)
downloadjsnes-4e33c42d25ee9d13ba61dd4c69765efc80a15399.zip
jsnes-4e33c42d25ee9d13ba61dd4c69765efc80a15399.tar.gz
jsnes-4e33c42d25ee9d13ba61dd4c69765efc80a15399.tar.bz2
Load binary ROMs directly
-rw-r--r--bin2js.pl26
-rw-r--r--index.html14
-rw-r--r--js.2/nes.js35
-rw-r--r--js.2/rom.js14
4 files changed, 31 insertions, 58 deletions
diff --git a/bin2js.pl b/bin2js.pl
deleted file mode 100644
index 56bb708..0000000
--- a/bin2js.pl
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/perl -w
-# By Matt Westcott, part of JSSpeccy: http://matt.west.co.tt/spectrum/jsspeccy/
-
-$dir = $ARGV[0];
-
-print "var ${dir}_list = new Array();\n";
-opendir(DIR, $dir) || die "can't opendir $dir: $!";
-while ($file = readdir(DIR)) {
- next if $file =~ /(^\.|\.js$)/;
- open(JSFILE, ">$dir/$file.js");
- $safe_file = $file;
- $safe_file =~ s/'/\\'/g;
- print JSFILE "${dir}['$safe_file'] = '";
- print "${dir}_list.push('$safe_file');\n";
- open(ROMFILE, "<$dir/$file") || die "can't open $file: $!";
- while (read(ROMFILE, $str, 512)) {
- $str =~ s/([0-9\x00-\x1f\x7f-\xff\\\'\"])/ sprintf("\\%o", ord($1)) /egs;
- #$str =~ s/([^A-Za-z])/ sprintf("\\%o", ord($1)) /egs;
- print JSFILE $str;
- }
- close(ROMFILE);
- print JSFILE "';\n";
- close (JSFILE);
-};
-# STDOUT << "\\#{b.to_s(8)}"
-closedir(DIR);
diff --git a/index.html b/index.html
index bc82c75..7b15064 100644
--- a/index.html
+++ b/index.html
@@ -158,9 +158,17 @@ digg_url = 'http://digg.com/playable_web_games/JSNES_A_NES_emulator_written_enti
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();
+ }
});
});
diff --git a/js.2/nes.js b/js.2/nes.js
index 6b55d92..3395ed2 100644
--- a/js.2/nes.js
+++ b/js.2/nes.js
@@ -27,6 +27,7 @@ NES.prototype = {
isRunning: false,
fpsFrameCount: 0,
limitFrames: true,
+ romData: null,
// Resets the system.
reset: function() {
@@ -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() {
@@ -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;
},
diff --git a/js.2/rom.js b/js.2/rom.js
index a32756b..17a5c8a 100644
--- a/js.2/rom.js
+++ b/js.2/rom.js
@@ -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
@@ -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;
}
@@ -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;
}