diff options
author | Ben Firshman <ben@firshman.co.uk> | 2010-05-22 20:15:49 +0100 |
---|---|---|
committer | Ben Firshman <ben@firshman.co.uk> | 2010-05-22 20:15:49 +0100 |
commit | 70f56a3953bdbd746c244f3610badc1acdb935f1 (patch) | |
tree | 0239b64633f0e689a0fdae10315794f3854eb387 | |
parent | 2cd1d924dd2494a84c9b899b8000e8ab452f2ec6 (diff) | |
download | jsnes-70f56a3953bdbd746c244f3610badc1acdb935f1.zip jsnes-70f56a3953bdbd746c244f3610badc1acdb935f1.tar.gz jsnes-70f56a3953bdbd746c244f3610badc1acdb935f1.tar.bz2 |
Moved memory to CPU and PPU
-rw-r--r-- | js/cpu.js | 27 | ||||
-rw-r--r-- | js/mappers.js | 32 | ||||
-rw-r--r-- | js/nes.js | 46 | ||||
-rw-r--r-- | js/ppu.js | 80 |
4 files changed, 98 insertions, 87 deletions
@@ -10,6 +10,23 @@ NES.CPU.prototype = { IRQ_RESET: 2, reset: function() { + // Main memory + this.mem = new Array(0x10000); + + for (var i=0; i < 0x2000; i++) { + this.mem[i] = 0xFF; + } + for (var p=0; p < 4; p++) { + var i = p*0x800; + this.mem[i+0x008] = 0xF7; + this.mem[i+0x009] = 0xEF; + this.mem[i+0x00A] = 0xDF; + this.mem[i+0x00F] = 0xBF; + } + for (var i=0x2001; i < this.mem.length; i++) { + this.mem[i] = 0; + } + // CPU Registers: this.REG_ACC = 0; this.REG_X = 0; @@ -204,7 +221,7 @@ NES.CPU.prototype = { // at the given location. addr = this.load16bit(opaddr+2);// Find op if(addr < 0x1FFF) { - addr = this.nes.cpuMem[addr] + (this.nes.cpuMem[(addr & 0xFF00) | (((addr & 0xFF) + 1) & 0xFF)] << 8);// Read from address given in op + addr = this.mem[addr] + (this.mem[(addr & 0xFF00) | (((addr & 0xFF) + 1) & 0xFF)] << 8);// Read from address given in op } else{ addr = this.nes.mmap.load(addr) + (this.nes.mmap.load((addr & 0xFF00) | (((addr & 0xFF) + 1) & 0xFF)) << 8); @@ -1026,7 +1043,7 @@ NES.CPU.prototype = { load: function(addr){ if (addr < 0x2000) { - return this.nes.cpuMem[addr & 0x7FF]; + return this.mem[addr & 0x7FF]; } else { return this.nes.mmap.load(addr); @@ -1035,8 +1052,8 @@ NES.CPU.prototype = { load16bit: function(addr){ if (addr < 0x1FFF) { - return this.nes.cpuMem[addr&0x7FF] - | (this.nes.cpuMem[(addr+1)&0x7FF]<<8); + return this.mem[addr&0x7FF] + | (this.mem[(addr+1)&0x7FF]<<8); } else { return this.nes.mmap.load(addr) | (this.nes.mmap.load(addr+1) << 8); @@ -1045,7 +1062,7 @@ NES.CPU.prototype = { write: function(addr, val){ if(addr < 0x2000) { - this.nes.cpuMem[addr&0x7FF] = val; + this.mem[addr&0x7FF] = val; } else { this.nes.mmap.write(addr,val); diff --git a/js/mappers.js b/js/mappers.js index 49eb1dd..9530473 100644 --- a/js/mappers.js +++ b/js/mappers.js @@ -18,11 +18,11 @@ NES.Mappers[0].prototype = { write: function(address, value) { if (address < 0x2000) { // Mirroring of RAM: - this.nes.cpuMem[address & 0x7FF] = value; + this.nes.cpu.mem[address & 0x7FF] = value; } else if (address > 0x4017) { - this.nes.cpuMem[address] = value; + this.nes.cpu.mem[address] = value; if (address >= 0x6000 && address < 0x8000) { // Write to SaveRAM. Store in file: // TODO: not yet @@ -45,7 +45,7 @@ NES.Mappers[0].prototype = { // Check address range: if (address > 0x4017) { // ROM: - return this.nes.cpuMem[address]; + return this.nes.cpu.mem[address]; } else if (address >= 0x2000) { // I/O Ports. @@ -53,7 +53,7 @@ NES.Mappers[0].prototype = { } else { // RAM (mirrored) - return this.nes.cpuMem[address&0x7FF]; + return this.nes.cpu.mem[address&0x7FF]; } }, @@ -78,7 +78,7 @@ NES.Mappers[0].prototype = { // in main memory and in the // PPU as flags): // (not in the real NES) - return this.nes.cpuMem[0x2000]; + return this.nes.cpu.mem[0x2000]; } case 0x1: { // 0x2001: @@ -87,7 +87,7 @@ NES.Mappers[0].prototype = { // in main memory and in the // PPU as flags): // (not in the real NES) - return this.nes.cpuMem[0x2001]; + return this.nes.cpu.mem[0x2001]; } case 0x2:{ @@ -187,14 +187,14 @@ NES.Mappers[0].prototype = { case 0x2000:{ // PPU Control register 1 - this.nes.cpuMem[address] = value; + this.nes.cpu.mem[address] = value; this.nes.ppu.updateControlReg1(value); break; }case 0x2001:{ // PPU Control register 2 - this.nes.cpuMem[address] = value; + this.nes.cpu.mem[address] = value; this.nes.ppu.updateControlReg2(value); break; @@ -410,7 +410,7 @@ NES.Mappers[0].prototype = { var ram = this.nes.rom.batteryRam; if (ram!=null && ram.length==0x2000) { // Load Battery RAM into memory: - NES.Utils.arraycopy(ram, 0, this.nes.cpuMem, 0x6000, 0x2000); + NES.Utils.arraycopy(ram, 0, this.nes.cpu.mem, 0x6000, 0x2000); } } }, @@ -420,7 +420,7 @@ NES.Mappers[0].prototype = { bank %= this.nes.rom.romCount; //var data = this.nes.rom.rom[bank]; //cpuMem.write(address,data,data.length); - NES.Utils.arraycopy(this.nes.rom.rom[bank], 0, this.nes.cpuMem, address, 16384); + NES.Utils.arraycopy(this.nes.rom.rom[bank], 0, this.nes.cpu.mem, address, 16384); }, loadVromBank: function(bank, address) { @@ -429,8 +429,8 @@ NES.Mappers[0].prototype = { } this.nes.ppu.triggerRendering(); - NES.Utils.arraycopy(this.nes.rom.vrom[bank%this.nes.rom.vromCount],0, - this.nes.ppuMem,address,4096); + NES.Utils.arraycopy(this.nes.rom.vrom[bank%this.nes.rom.vromCount], 0, + this.nes.ppu.vramMem, address, 4096); var vromTile = this.nes.rom.vromTile[bank%this.nes.rom.vromCount]; NES.Utils.arraycopy(vromTile, 0, this.nes.ppu.ptTile,address >> 4, 256); @@ -459,7 +459,7 @@ NES.Mappers[0].prototype = { var bank4k = parseInt(bank1k/4) % this.nes.rom.vromCount; var bankoffset = (bank1k%4)*1024; - NES.Utils.arraycopy(this.nes.rom.vrom[bank4k], 0, this.nes.ppuMem, + NES.Utils.arraycopy(this.nes.rom.vrom[bank4k], 0, this.nes.ppu.vramMem, bankoffset, 1024); // Update tiles: @@ -479,7 +479,7 @@ NES.Mappers[0].prototype = { var bank4k = parseInt(bank2k/2)%this.nes.rom.vromCount; var bankoffset = (bank2k%2)*2048; NES.Utils.arraycopy(this.nes.rom.vrom[bank4k], bankoffset, - this.nes.ppuMem,address,2048); + this.nes.ppu.vramMem, address, 2048); // Update tiles: var vromTile = this.nes.rom.vromTile[bank4k]; @@ -494,9 +494,9 @@ NES.Mappers[0].prototype = { 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.cpu.mem.write(address,this.nes.rom.rom[bank16k],offset,8192); NES.Utils.arraycopy(this.nes.rom.rom[bank16k], offset, - this.nes.cpuMem, address, 8192); + this.nes.cpu.mem, address, 8192); }, clockIrqCounter: function() { @@ -13,11 +13,6 @@ function NES() { } this.frameTime = 1000/this.opts.preferredFrameRate; - // TODO: move - 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 NES.CPU(this); this.ppu = new NES.PPU(this); this.papu = new NES.PAPU(this); @@ -36,8 +31,6 @@ function NES() { this.imageData.data[i] = 0xFF; } - this.clearCPUMemory(); - $("#status").text("Initialised. Ready to load a ROM."); } @@ -47,6 +40,17 @@ NES.prototype = { fpsFrameCount: 0, limitFrames: true, + // Resets the system. + reset: function() { + if(this.mmap != null) { + this.mmap.reset(); + } + + this.cpu.reset(); + this.ppu.reset(); + this.papu.reset(); + }, + start: function() { var self = this; @@ -165,19 +169,6 @@ NES.prototype = { } }, - clearCPUMemory: function() { - for(var i=0;i<0x2000;i++) { - this.cpuMem[i] = 0xFF; - } - for(var p=0;p<4;p++){ - var i = p*0x800; - this.cpuMem[i+0x008] = 0xF7; - this.cpuMem[i+0x009] = 0xEF; - this.cpuMem[i+0x00A] = 0xDF; - this.cpuMem[i+0x00F] = 0xBF; - } - }, - // Loads a ROM file into the CPU and PPU. // The ROM file is validated first. loadRom: function(file){ @@ -212,21 +203,6 @@ NES.prototype = { return this.rom.valid; }, - // Resets the system. - reset: function() { - if(this.mmap != null) { - this.mmap.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.ppu.reset(); - - this.papu.reset(); - }, - resetFps: function() { this.lastFpsTime = null; this.fpsFrameCount = 0; @@ -3,7 +3,6 @@ NES.PPU = function(nes) { // Rendering Options: this.showSpr0Hit = false; - this.showSoundBuffer = false; this.clipToTvSize = true; this.reset(); @@ -17,6 +16,16 @@ NES.PPU.prototype = { STATUS_VBLANK: 7, reset: function() { + // Memory + this.vramMem = new Array(0x8000); + this.spriteMem = new Array(0x100); + for (var i=0; i<this.vramMem.length; i++) { + this.vramMem[i] = 0; + } + for (var i=0; i<this.spriteMem.length; i++) { + this.spriteMem[i] = 0; + } + // VRAM I/O: this.vramAddress = null; this.vramTmpAddress = null; @@ -67,10 +76,6 @@ NES.PPU.prototype = { this.regFH = 0; this.regS = 0; - // Get the memory: - this.ppuMem = this.nes.ppuMem; - this.sprMem = this.nes.sprMem; - // These are temporary variables used in rendering and sound procedures. // Their states outside of those procedures can be ignored. // TODO: the use of this is a bit weird, investigate @@ -529,15 +534,15 @@ NES.PPU.prototype = { setStatusFlag: function(flag, value){ var n = 1<<flag; - this.nes.cpuMem[0x2002] = - ((this.nes.cpuMem[0x2002]&(255-n))|(value?n:0)); + this.nes.cpu.mem[0x2002] = + ((this.nes.cpu.mem[0x2002] & (255-n)) | (value?n:0)); }, // CPU Register $2002: // Read the Status Register. readStatusRegister: function(){ - var tmp = this.nes.cpuMem[0x2002]; + var tmp = this.nes.cpu.mem[0x2002]; // Reset scroll & VRAM Address toggle: this.firstWrite = true; @@ -564,17 +569,17 @@ NES.PPU.prototype = { sramAddress++; // Increment address sramAddress%=0x100; return tmp;*/ - return this.sprMem[this.sramAddress] + return this.spriteMem[this.sramAddress] }, // CPU Register $2004 (W): // Write to SPR-RAM (Sprite RAM). // The address should be set first. sramWrite: function(value){ - this.sprMem[this.sramAddress] = value; + this.spriteMem[this.sramAddress] = value; this.spriteRamWriteUpdate(this.sramAddress,value); this.sramAddress++; // Increment address - this.sramAddress%=0x100; + this.sramAddress %= 0x100; }, // CPU Register $2005: @@ -651,7 +656,7 @@ NES.PPU.prototype = { // Update buffered value: if(this.vramAddress < 0x2000){ - this.vramBufferedReadValue = this.ppuMem[this.vramAddress]; + this.vramBufferedReadValue = this.vramMem[this.vramAddress]; }else{ this.vramBufferedReadValue = this.mirroredLoad(this.vramAddress); } @@ -717,9 +722,9 @@ NES.PPU.prototype = { sramDMA: function(value){ var baseAddress = value * 0x100; var data; - for(var i=this.sramAddress;i<256;i++){ - data = this.nes.cpuMem[baseAddress+i]; - this.sprMem[i] = data; + for(var i=this.sramAddress; i < 256; i++){ + data = this.nes.cpu.mem[baseAddress+i]; + this.spriteMem[i] = data; this.spriteRamWriteUpdate(i, data); } @@ -804,8 +809,8 @@ NES.PPU.prototype = { // Reads from memory, taking into account // mirroring/mapping of address ranges. - mirroredLoad: function(address){ - return this.ppuMem[this.vramMirrorTable[address]]; + mirroredLoad: function(address) { + return this.vramMem[this.vramMirrorTable[address]]; }, // Writes to memory, taking into account @@ -1242,12 +1247,12 @@ NES.PPU.prototype = { // appropriately. writeMem: function(address, value){ - this.ppuMem[address] = value; + this.vramMem[address] = value; // Update internally buffered data: if(address < 0x2000){ - this.ppuMem[address] = value; + this.vramMem[address] = value; this.patternWrite(address,value); }else if(address >=0x2000 && address <0x23c0){ @@ -1294,21 +1299,27 @@ NES.PPU.prototype = { updatePalettes: function(){ for(var i=0;i<16;i++){ - if(this.f_dispType == 0){ + if (this.f_dispType == 0) { this.imgPalette[i] = this.palTable.getEntry( - this.ppuMem[0x3f00+i]&63); - }else{ + this.vramMem[0x3f00+i] & 63 + ); + } + else { this.imgPalette[i] = this.palTable.getEntry( - this.ppuMem[0x3f00+i]&32); + this.vramMem[0x3f00+i] & 32 + ); } } for(var i=0;i<16;i++){ - if(this.f_dispType == 0){ + if (this.f_dispType == 0) { this.sprPalette[i] = this.palTable.getEntry( - this.ppuMem[0x3f10+i]&63); - }else{ + this.vramMem[0x3f10+i] & 63 + ); + } + else { this.sprPalette[i] = this.palTable.getEntry( - this.ppuMem[0x3f10+i]&32); + this.vramMem[0x3f10+i] & 32 + ); } } }, @@ -1319,12 +1330,19 @@ NES.PPU.prototype = { patternWrite: function(address, value){ var tileIndex = parseInt(address/16); var leftOver = address%16; - if(leftOver<8){ + if (leftOver<8) { this.ptTile[tileIndex].setScanline( - leftOver, value, this.ppuMem[address+8]); - }else{ + leftOver, + value, + this.vramMem[address+8] + ); + } + else { this.ptTile[tileIndex].setScanline( - leftOver-8, this.ppuMem[address-8], value); + leftOver-8, + this.vramMem[address-8], + value + ); } }, |