summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cpu.js8
-rw-r--r--index.html1
-rw-r--r--mappers.js38
-rw-r--r--memory.js25
-rw-r--r--nametable.js4
-rw-r--r--nes.js23
-rw-r--r--ppu.js45
7 files changed, 56 insertions, 88 deletions
diff --git a/cpu.js b/cpu.js
index ff30596..fcee562 100644
--- a/cpu.js
+++ b/cpu.js
@@ -246,7 +246,7 @@ function CPU(nes) {
// at the given location.
addr = this.load16bit(opaddr+2);// Find op
if(addr < 0x1FFF){
- addr = this.nes.cpuMem.mem[addr] + (this.nes.cpuMem.mem[(addr&0xFF00)|(((addr&0xFF)+1)&0xFF)]<<8);// Read from address given in op
+ addr = this.nes.cpuMem[addr] + (this.nes.cpuMem[(addr&0xFF00)|(((addr&0xFF)+1)&0xFF)]<<8);// Read from address given in op
}else{
addr = this.mmap.load(addr)+(this.mmap.load((addr&0xFF00)|(((addr&0xFF)+1)&0xFF))<<8);
}
@@ -1086,12 +1086,12 @@ function CPU(nes) {
}
this.load = function(addr){
- return addr<0x2000 ? this.nes.cpuMem.mem[addr&0x7FF] : this.mmap.load(addr);
+ return addr<0x2000 ? this.nes.cpuMem[addr&0x7FF] : this.mmap.load(addr);
}
this.load16bit = function(addr){
return addr<0x1FFF ?
- this.nes.cpuMem.mem[addr&0x7FF] | (this.nes.cpuMem.mem[(addr+1)&0x7FF]<<8)
+ this.nes.cpuMem[addr&0x7FF] | (this.nes.cpuMem[(addr+1)&0x7FF]<<8)
:
this.mmap.load(addr) | (this.mmap.load(addr+1)<<8)
;
@@ -1099,7 +1099,7 @@ function CPU(nes) {
this.write = function(addr, val){
if(addr < 0x2000){
- this.nes.cpuMem.mem[addr&0x7FF] = val;
+ this.nes.cpuMem[addr&0x7FF] = val;
}else{
this.mmap.write(addr,val);
}
diff --git a/index.html b/index.html
index 3f19a5f..faf5ca3 100644
--- a/index.html
+++ b/index.html
@@ -13,7 +13,6 @@
<script src="globals.js" type="text/javascript" charset="utf-8"></script>
<script src="keyboard.js" type="text/javascript" charset="utf-8"></script>
<script src="mappers.js" type="text/javascript" charset="utf-8"></script>
- <script src="memory.js" type="text/javascript" charset="utf-8"></script>
<script src="nametable.js" type="text/javascript" charset="utf-8"></script>
<script src="nes.js" type="text/javascript" charset="utf-8"></script>
<script src="palettetable.js" type="text/javascript" charset="utf-8"></script>
diff --git a/mappers.js b/mappers.js
index 181957e..3e99fb8 100644
--- a/mappers.js
+++ b/mappers.js
@@ -18,11 +18,11 @@ function MapperDefault(nes) {
if(address<0x2000){
// Mirroring of RAM:
- this.nes.cpuMem.mem[address & 0x7FF] = value;
+ this.nes.cpuMem[address & 0x7FF] = value;
}else if(address>0x4017){
- this.nes.cpuMem.mem[address] = value;
+ this.nes.cpuMem[address] = value;
if(address>=0x6000 && address<0x8000){
// Write to SaveRAM. Store in file:
@@ -60,7 +60,7 @@ function MapperDefault(nes) {
}else{
// Check whether the actual value equals the compare value:
- if(nes.cpuMem.mem[address] == nes.gameGenie.getCodeCompare(tmp)){
+ if(nes.cpuMem[address] == nes.gameGenie.getCodeCompare(tmp)){
// The values match, so use the supplied game genie value:
return (short)nes.gameGenie.getCodeValue(tmp);
@@ -78,7 +78,7 @@ function MapperDefault(nes) {
if(address > 0x4017){
// ROM:
- return this.nes.cpuMem.mem[address];
+ return this.nes.cpuMem[address];
}else if(address >= 0x2000){
@@ -88,7 +88,7 @@ function MapperDefault(nes) {
}else{
// RAM (mirrored)
- return this.nes.cpuMem.mem[address&0x7FF];
+ return this.nes.cpuMem[address&0x7FF];
}
@@ -118,7 +118,7 @@ function MapperDefault(nes) {
// in main memory and in the
// PPU as flags):
// (not in the real NES)
- return this.nes.cpuMem.mem[0x2000];
+ return this.nes.cpuMem[0x2000];
}case 0x1:{
@@ -128,7 +128,7 @@ function MapperDefault(nes) {
// in main memory and in the
// PPU as flags):
// (not in the real NES)
- return this.nes.cpuMem.mem[0x2001];
+ return this.nes.cpuMem[0x2001];
}case 0x2:{
@@ -233,14 +233,14 @@ function MapperDefault(nes) {
case 0x2000:{
// PPU Control register 1
- this.nes.cpuMem.write(address,value);
+ this.nes.cpuMem[address] = value;
this.nes.ppu.updateControlReg1(value);
break;
}case 0x2001:{
// PPU Control register 2
- this.nes.cpuMem.write(address,value);
+ this.nes.cpuMem[address] = value;
this.nes.ppu.updateControlReg2(value);
break;
@@ -486,7 +486,7 @@ function MapperDefault(nes) {
var ram = this.nes.rom.batteryRam;
if(ram!=null && ram.length==0x2000){
// Load Battery RAM into memory:
- arraycopy(ram,0,this.nes.cpuMem.mem,0x6000,0x2000);
+ arraycopy(ram, 0, this.nes.cpuMem, 0x6000, 0x2000);
}
}
}
@@ -496,7 +496,7 @@ function MapperDefault(nes) {
bank %= this.nes.rom.romCount;
//var data = this.nes.rom.rom[bank];
//cpuMem.write(address,data,data.length);
- arraycopy(this.nes.rom.rom[bank],0,this.nes.cpuMem.mem,address,16384);
+ arraycopy(this.nes.rom.rom[bank], 0, this.nes.cpuMem, address, 16384);
}
@@ -506,7 +506,7 @@ function MapperDefault(nes) {
this.nes.ppu.triggerRendering();
arraycopy(this.nes.rom.vrom[bank%this.nes.rom.vromCount],0,
- this.nes.ppuMem.mem,address,4096);
+ this.nes.ppuMem,address,4096);
var vromTile = this.nes.rom.vromTile[bank%this.nes.rom.vromCount];
arraycopy(vromTile,0,this.nes.ppu.ptTile,address>>4,256);
@@ -534,9 +534,9 @@ function MapperDefault(nes) {
if(this.nes.rom.vromCount == 0)return;
this.nes.ppu.triggerRendering();
- var bank4k = (bank1k/4)%rom.vromCount;
+ var bank4k = parseInt(bank1k/4)%rom.vromCount;
var bankoffset = (bank1k%4)*1024;
- arraycopy(this.nes.rom.vrom[bank4k],0,this.nes.ppuMem.mem,
+ arraycopy(this.nes.rom.vrom[bank4k],0,this.nes.ppuMem,
bankoffset,1024);
// Update tiles:
@@ -553,10 +553,10 @@ function MapperDefault(nes) {
if(this.nes.rom.vromCount == 0)return;
this.nes.ppu.triggerRendering();
- var bank4k = (bank2k/2)%this.nes.rom.vromCount;
+ var bank4k = parseInt(bank2k/2)%this.nes.rom.vromCount;
var bankoffset = (bank2k%2)*2048;
arraycopy(this.nes.rom.getVromBank(bank4k),bankoffset,
- this.nes.ppuMem.mem,address,2048);
+ this.nes.ppuMem,address,2048);
// Update tiles:
var vromTile = this.nes.rom.vromTile[bank4k];
@@ -569,10 +569,12 @@ function MapperDefault(nes) {
this.load8kRomBank = function(bank8k, address){
- var bank16k = (bank8k/2)%this.nes.rom.romCount;
+ var bank16k = parseInt(bank8k/2)%this.nes.rom.romCount;
var offset = (bank8k%2)*8192;
- this.nes.cpuMem.write(address,this.nes.rom.rom[bank16k],offset,8192);
+ //this.nes.cpuMem.write(address,this.nes.rom.rom[bank16k],offset,8192);
+ arraycopy(this.nes.rom.rom[bank16k], offset,
+ this.nes.cpuMem, address, 8192);
}
diff --git a/memory.js b/memory.js
deleted file mode 100644
index 497deae..0000000
--- a/memory.js
+++ /dev/null
@@ -1,25 +0,0 @@
-function Memory(nes, byteCount) {
- this.nes = nes;
- this.mem = new Array(byteCount);
- this.memLength = byteCount;
-
- this.reset = function() {
- for(var i=0;i<this.mem.length;i++) this.mem[i] = 0;
- }
-
- this.write = function(address, value, length) {
- if (typeof length == 'undefined') {
- this.mem[address] = value;
- }
- else {
- if (address+length > this.mem.length) return;
- arraycopy(value, 0, this.mem, address, length);
- }
- }
-
- this.load = function(address) {
- return this.mem[address];
- }
-
-
-} \ No newline at end of file
diff --git a/nametable.js b/nametable.js
index e2a536c..d129032 100644
--- a/nametable.js
+++ b/nametable.js
@@ -17,10 +17,6 @@ function NameTable(width, height, name) {
return this.attrib[y*this.width+x];
}
- this.writeTileIndex = function(index, value){
- this.tile[index] = value;
- }
-
this.writeAttrib = function(index, value){
var basex,basey;
diff --git a/nes.js b/nes.js
index 5d60422..b0339e1 100644
--- a/nes.js
+++ b/nes.js
@@ -1,9 +1,10 @@
function NES() {
Globals.nes = this;
- this.cpuMem = new Memory(this, 0x10000); // Main memory (internal to CPU)
- this.ppuMem = new Memory(this, 0x8000); // VRAM memory (internal to PPU)
- this.sprMem = new Memory(this, 0x100); // Sprite RAM (internal to PPU)
+ this.cpuMem = new Array(0x10000); // Main memory (internal to CPU)
+ this.ppuMem = new Array(0x8000); // VRAM memory (internal to PPU)
+ this.sprMem = new Array(0x100); // Sprite RAM (internal to PPU)
+
this.cpu = new CPU(this);
this.ppu = new PPU(this);
this.memMapper = null;
@@ -140,14 +141,14 @@ function NES() {
this.clearCPUMemory = function() {
var flushval = Globals.memoryFlushValue;
for(var i=0;i<0x2000;i++) {
- this.cpuMem.mem[i] = flushval;
+ this.cpuMem[i] = flushval;
}
for(var p=0;p<4;p++){
var i = p*0x800;
- this.cpuMem.mem[i+0x008] = 0xF7;
- this.cpuMem.mem[i+0x009] = 0xEF;
- this.cpuMem.mem[i+0x00A] = 0xDF;
- this.cpuMem.mem[i+0x00F] = 0xBF;
+ this.cpuMem[i+0x008] = 0xF7;
+ this.cpuMem[i+0x009] = 0xEF;
+ this.cpuMem[i+0x00A] = 0xDF;
+ this.cpuMem[i+0x00F] = 0xBF;
}
}
@@ -191,9 +192,9 @@ function NES() {
this.rom.closeRom();
if(this.memMapper != null)
this.memMapper.reset();
- this.cpuMem.reset();
- this.ppuMem.reset();
- this.sprMem.reset();
+ for (var i=0; i<this.cpuMem.length; i++) this.cpuMem[i] = 0;
+ for (var i=0; i<this.ppuMem.length; i++) this.ppuMem[i] = 0;
+ for (var i=0; i<this.sprMem.length; i++) this.sprMem[i] = 0;
this.clearCPUMemory();
this.cpu.reset();
this.cpu.init();
diff --git a/ppu.js b/ppu.js
index 4488703..0a06e2a 100644
--- a/ppu.js
+++ b/ppu.js
@@ -120,7 +120,7 @@ function PPU(nes) {
this.dummyPixPriTable = new Array(256*240);
this.oldFrame = new Array(256*240);
this.buffer = new Array(256*240);
- this.tpix;
+ this.tpix = null;
this.scanlineChanged = Array(240);
this.requestRenderAll=false;
@@ -596,16 +596,15 @@ function PPU(nes) {
this.setStatusFlag = function(flag, value){
var n = 1<<flag;
- var memValue = this.nes.cpuMem.load(0x2002);
- memValue = ((memValue&(255-n))|(value?n:0));
- this.nes.cpuMem.write(0x2002,memValue);
+ this.nes.cpuMem[0x2002] =
+ ((this.nes.cpuMem[0x2002]&(255-n))|(value?n:0));
}
// CPU Register $2002:
// Read the Status Register.
this.readStatusRegister = function(){
- this.tmp = this.nes.cpuMem.load(0x2002);
+ this.tmp = this.nes.cpuMem[0x2002];
// Reset scroll & VRAM Address toggle:
this.firstWrite = true;
@@ -635,7 +634,7 @@ function PPU(nes) {
sramAddress++; // Increment address
sramAddress%=0x100;
return tmp;*/
- return this.sprMem.load(this.sramAddress)
+ return this.sprMem[this.sramAddress]
}
@@ -643,7 +642,7 @@ function PPU(nes) {
// Write to SPR-RAM (Sprite RAM).
// The address should be set first.
this.sramWrite = function(value){
- this.sprMem.write(this.sramAddress,value);
+ this.sprMem[this.sramAddress] = value;
this.spriteRamWriteUpdate(this.sramAddress,value);
this.sramAddress++; // Increment address
this.sramAddress%=0x100;
@@ -727,7 +726,7 @@ function PPU(nes) {
// Update buffered value:
if(this.vramAddress < 0x2000){
- this.vramBufferedReadValue = this.ppuMem.load(this.vramAddress);
+ this.vramBufferedReadValue = this.ppuMem[this.vramAddress];
}else{
this.vramBufferedReadValue = this.mirroredLoad(this.vramAddress);
}
@@ -794,9 +793,9 @@ function PPU(nes) {
var baseAddress = value * 0x100;
var data;
for(var i=this.sramAddress;i<256;i++){
- data = this.nes.cpuMem.load(baseAddress+i);
- this.sprMem.write(i,data);
- this.spriteRamWriteUpdate(i,data);
+ data = this.nes.cpuMem[baseAddress+i];
+ this.sprMem[i] = data;
+ this.spriteRamWriteUpdate(i, data);
}
this.nes.cpu.haltCycles(513);
@@ -883,7 +882,7 @@ function PPU(nes) {
// Reads from memory, taking into account
// mirroring/mapping of address ranges.
this.mirroredLoad = function(address){
- return this.ppuMem.load(this.vramMirrorTable[address]);
+ return this.ppuMem[this.vramMirrorTable[address]];
}
// Writes to memory, taking into account
@@ -1041,7 +1040,6 @@ function PPU(nes) {
if(this.t.opaque[this.cntFV]){
for(;this.sx<8;this.sx++){
buffer[this.destIndex] = this.imgPalette[this.tpix[this.tscanoffset+this.sx]+this.att];
- //this.writePixel(this.destIndex, this.imgPalette[this.tpix[this.tscanoffset+this.sx]+this.att]);
this.pixrendered[destIndex] |= 256;
this.destIndex++;
}
@@ -1051,7 +1049,6 @@ function PPU(nes) {
if(this.col != 0){
//console.log("Writing "+this.imgPalette[this.col+this.att].toString(16)+" to buffer at "+this.destIndex.toString(16));
buffer[this.destIndex] = this.imgPalette[this.col+this.att];
- //this.writePixel(this.destIndex, this.imgPalette[this.col+this.att]);
this.pixrendered[this.destIndex] |= 256;
}
this.destIndex++;
@@ -1339,12 +1336,12 @@ function PPU(nes) {
// appropriately.
this.writeMem = function(address, value){
- this.ppuMem.write(address,value);
+ this.ppuMem[address] = value;
// Update internally buffered data:
if(address < 0x2000){
- this.ppuMem.write(address,value);
+ this.ppuMem[address] = value;
this.patternWrite(address,value);
}else if(address >=0x2000 && address <0x23c0){
@@ -1394,19 +1391,19 @@ function PPU(nes) {
for(var i=0;i<16;i++){
if(this.f_dispType == 0){
this.imgPalette[i] = this.nes.palTable.getEntry(
- this.ppuMem.load(0x3f00+i)&63);
+ this.ppuMem[0x3f00+i]&63);
}else{
this.imgPalette[i] = this.nes.palTable.getEntry(
- this.ppuMem.load(0x3f00+i)&32);
+ this.ppuMem[0x3f00+i]&32);
}
}
for(var i=0;i<16;i++){
if(this.f_dispType == 0){
this.sprPalette[i] = this.nes.palTable.getEntry(
- this.ppuMem.load(0x3f10+i)&63);
+ this.ppuMem[0x3f10+i]&63);
}else{
this.sprPalette[i] = this.nes.palTable.getEntry(
- this.ppuMem.load(0x3f10+i)&32);
+ this.ppuMem[0x3f10+i]&32);
}
}
@@ -1422,10 +1419,10 @@ function PPU(nes) {
var leftOver = address%16;
if(leftOver<8){
this.ptTile[tileIndex].setScanline(
- leftOver,value,this.ppuMem.load(address+8));
+ leftOver, value, this.ppuMem[address+8]);
}else{
this.ptTile[tileIndex].setScanline(
- leftOver-8,this.ppuMem.load(address-8),value);
+ leftOver-8, this.ppuMem[address-8], value);
}
}
@@ -1442,7 +1439,7 @@ function PPU(nes) {
// Updates the internal name table buffers
// with this new byte.
this.nameTableWrite = function(index, address, value){
- this.nameTable[index].writeTileIndex(address,value);
+ this.nameTable[index].tile[address] = value;
// Update Sprite #0 hit:
//updateSpr0Hit();
@@ -1505,8 +1502,6 @@ function PPU(nes) {
}
this.reset = function() {
- this.ppuMem.reset();
- this.sprMem.reset();
this.vramBufferedReadValue = 0;
this.sramAddress = 0;