diff options
author | Ben Firshman <ben@firshman.co.uk> | 2010-06-12 01:51:20 +0100 |
---|---|---|
committer | Ben Firshman <ben@firshman.co.uk> | 2010-06-12 01:51:20 +0100 |
commit | 2f76ad3f66b0c7b5fd2b8bde2d88ece362809a7e (patch) | |
tree | 023fb66febb07700ec18bed5b354fd48cba454c6 | |
parent | deca9c132e89b00b886a1fa211aedc823a1730bd (diff) | |
download | jsnes-2f76ad3f66b0c7b5fd2b8bde2d88ece362809a7e.zip jsnes-2f76ad3f66b0c7b5fd2b8bde2d88ece362809a7e.tar.gz jsnes-2f76ad3f66b0c7b5fd2b8bde2d88ece362809a7e.tar.bz2 |
All whipped into shape by JSLint except for CPU. Yummy.
-rw-r--r-- | js.2/keyboard.js | 36 | ||||
-rw-r--r-- | js.2/nes.js | 52 | ||||
-rw-r--r-- | js.2/papu.js | 509 | ||||
-rw-r--r-- | js.2/ppu.js | 411 | ||||
-rw-r--r-- | js.2/rom.js | 48 | ||||
-rw-r--r-- | js.2/ui.js | 24 | ||||
-rw-r--r-- | js.2/utils.js | 2 |
7 files changed, 556 insertions, 526 deletions
diff --git a/js.2/keyboard.js b/js.2/keyboard.js index c26afdf..1bf7619 100644 --- a/js.2/keyboard.js +++ b/js.2/keyboard.js @@ -1,5 +1,7 @@ JSNES.Keyboard = function() { + var i; + this.keys = { KEY_A: 0, KEY_B: 1, @@ -11,21 +13,21 @@ JSNES.Keyboard = function() { KEY_RIGHT: 7 }; - this.state1 = Array(8); - for (var i = 0; i < this.state1.length; i++) { + this.state1 = new Array(8); + for (i = 0; i < this.state1.length; i++) { this.state1[i] = 0x40; } - this.state2 = Array(8); - for (var i = 0; i < this.state2.length; i++) { + this.state2 = new Array(8); + for (i = 0; i < this.state2.length; i++) { this.state2[i] = 0x40; } var self = this; - $(document) - .bind('keydown', function(evt) {self.keyDown(evt)}) - .bind('keyup', function(evt) {self.keyUp(evt)}) - .bind('keypress', function(evt) {self.keyPress(evt)}); -} + $(document). + bind('keydown', function(evt) { self.keyDown(evt); }). + bind('keyup', function(evt) { self.keyUp(evt); }). + bind('keypress', function(evt) { self.keyPress(evt); }); +}; JSNES.Keyboard.prototype = { setKey: function(key, value) { @@ -53,16 +55,18 @@ JSNES.Keyboard.prototype = { }, keyDown: function(evt) { - if (!this.setKey(evt.keyCode, 0x41) && evt.preventDefault) + if (!this.setKey(evt.keyCode, 0x41) && evt.preventDefault) { evt.preventDefault(); - }, - - keyPress: function(evt) { - evt.preventDefault(); + } }, keyUp: function(evt) { - if (!this.setKey(evt.keyCode, 0x40) && evt.preventDefault) + if (!this.setKey(evt.keyCode, 0x40) && evt.preventDefault) { evt.preventDefault(); + } + }, + + keyPress: function(evt) { + evt.preventDefault(); } -} +}; diff --git a/js.2/nes.js b/js.2/nes.js index 74b71cb..e06f714 100644 --- a/js.2/nes.js +++ b/js.2/nes.js @@ -8,10 +8,10 @@ var JSNES = function(parent) { emulateSound: false, sampleRate: 44100, // Sound sample rate in hz - CPU_FREQ_NTSC: 1789772.5,//1789772.72727272d; - CPU_FREQ_PAL: 1773447.4, - } - this.frameTime = 1000/this.opts.preferredFrameRate; + CPU_FREQ_NTSC: 1789772.5, //1789772.72727272d; + CPU_FREQ_PAL: 1773447.4 + }; + this.frameTime = 1000 / this.opts.preferredFrameRate; this.ui = new JSNES.UI(this, parent); this.cpu = new JSNES.CPU(this); @@ -32,7 +32,7 @@ JSNES.prototype = { // Resets the system. reset: function() { - if(this.mmap != null) { + if (this.mmap !== null) { this.mmap.reset(); } @@ -44,13 +44,13 @@ JSNES.prototype = { start: function() { var self = this; - if(this.rom != null && this.rom.valid) { + if (this.rom !== null && this.rom.valid) { if (!this.isRunning) { this.isRunning = true; this.frameInterval = setInterval(function() { self.frame(); - }, this.frameTime/2); + }, this.frameTime / 2); this.resetFps(); this.printFps(); this.fpsInterval = setInterval(function() { @@ -59,7 +59,7 @@ JSNES.prototype = { } } else { - alert("There is no ROM loaded, or it is invalid."); + this.ui.updateStatus("There is no ROM loaded, or it is invalid."); } }, @@ -71,7 +71,7 @@ JSNES.prototype = { var ppu = this.ppu; var papu = this.papu; FRAMELOOP: for (;;) { - if (cpu.cyclesToHalt == 0) { + if (cpu.cyclesToHalt === 0) { // Execute a CPU instruction cycles = cpu.emulate(); if (emulateSound) { @@ -96,18 +96,17 @@ JSNES.prototype = { } } - for(;cycles>0;cycles--){ - - if(ppu.curX == ppu.spr0HitX - && ppu.f_spVisibility==1 - && ppu.scanline-21 == ppu.spr0HitY){ + for (; cycles > 0; cycles--) { + if(ppu.curX === ppu.spr0HitX && + ppu.f_spVisibility === 1 && + ppu.scanline - 21 === ppu.spr0HitY) { // Set sprite 0 hit flag: - ppu.setStatusFlag(ppu.STATUS_SPRITE0HIT,true); + ppu.setStatusFlag(ppu.STATUS_SPRITE0HIT, true); } - if(ppu.requestEndFrame){ + if (ppu.requestEndFrame) { ppu.nmiCounter--; - if(ppu.nmiCounter == 0){ + if (ppu.nmiCounter === 0) { ppu.requestEndFrame = false; ppu.startVBlank(); break FRAMELOOP; @@ -115,29 +114,30 @@ JSNES.prototype = { } ppu.curX++; - if(ppu.curX==341){ + if (ppu.curX === 341) { ppu.curX = 0; ppu.endScanline(); } - } } if (this.limitFrames) { if (this.lastFrameTime) { - while ((new Date()).getTime() - this.lastFrameTime < this.frameTime) { + while (+new Date() - this.lastFrameTime < this.frameTime) { // twiddle thumbs } } } this.fpsFrameCount++; - this.lastFrameTime = (new Date()).getTime(); + this.lastFrameTime = +new Date(); }, printFps: function() { - var now = (new Date()).getTime(); + var now = +new Date(); var s = 'Running'; if (this.lastFpsTime) { - s += ': '+(this.fpsFrameCount/((now-this.lastFpsTime)/1000)).toFixed(2)+' FPS'; + s += ': '+( + this.fpsFrameCount / ((now - this.lastFpsTime) / 1000) + ).toFixed(2)+' FPS'; } this.ui.updateStatus(s); this.fpsFrameCount = 0; @@ -151,7 +151,7 @@ JSNES.prototype = { }, reloadRom: function() { - if(this.romData != null){ + if (this.romData !== null) { this.loadRom(this.romData); } }, @@ -194,8 +194,8 @@ JSNES.prototype = { setFramerate: function(rate){ this.nes.opts.preferredFrameRate = rate; - this.nes.frameTime = 1000/rate; - papu.setSampleRate(this.opts.sampleRate, false); + this.nes.frameTime = 1000 / rate; + this.papu.setSampleRate(this.opts.sampleRate, false); }, setLimitFrames: function(limit) { diff --git a/js.2/papu.js b/js.2/papu.js index dcb2159..0cba989 100644 --- a/js.2/papu.js +++ b/js.2/papu.js @@ -25,8 +25,8 @@ JSNES.PAPU = function(nes) { this.sampleBuffer = new Array(this.bufferSize*2); this.frameIrqEnabled = false; - this.frameIrqActive; - this.frameClockNow; + this.frameIrqActive = null; + this.frameClockNow = null; this.startedPlaying=false; this.recordOutput = false; this.initingHardware = false; @@ -38,6 +38,7 @@ JSNES.PAPU = function(nes) { this.frameTime = null; this.sampleTimerMax = null; this.sampleCount = null; + this.triValue = 0; this.smpSquare1 = null; this.smpSquare2 = null; @@ -48,7 +49,7 @@ JSNES.PAPU = function(nes) { // DC removal vars: this.prevSampleL = 0; this.prevSampleR = 0; - this.smpAccumL = 0 + this.smpAccumL = 0; this.smpAccumR = 0; // DAC range: @@ -76,13 +77,7 @@ JSNES.PAPU = function(nes) { this.minSample = null; // Panning: - this.panning = new Array( - 80, - 170, - 100, - 150, - 128 - ); + this.panning = [80, 170, 100, 150, 128]; this.setPanning(this.panning); // Initialize lookup tables: @@ -92,30 +87,33 @@ JSNES.PAPU = function(nes) { this.initDACtables(); // Init sound registers: - for(var i=0;i<0x14;i++){ - if(i==0x10){ + for (var i = 0; i < 0x14; i++) { + if (i === 0x10){ this.writeReg(0x4010, 0x10); - }else{ - this.writeReg(0x4000+i, 0); + } + else { + this.writeReg(0x4000 + i, 0); } } this.dynamicaudio = new DynamicAudio(); this.reset(); -} +}; JSNES.PAPU.prototype = { reset: function() { this.sampleRate = this.nes.opts.sampleRate; this.sampleTimerMax = parseInt( - (1024.0 * this.nes.opts.CPU_FREQ_NTSC - * this.nes.opts.preferredFrameRate) / - (this.sampleRate * 60.0) + (1024.0 * this.nes.opts.CPU_FREQ_NTSC * + this.nes.opts.preferredFrameRate) / + (this.sampleRate * 60.0), + 10 ); this.frameTime = parseInt( - (14915.0 * this.nes.opts.preferredFrameRate) / 60.0 + (14915.0 * this.nes.opts.preferredFrameRate) / 60.0, + 10 ); this.sampleTimer = 0; @@ -167,75 +165,75 @@ JSNES.PAPU.prototype = { tmp |= (this.triangle.getLengthStatus()<<2); tmp |= (this.noise.getLengthStatus() <<3); tmp |= (this.dmc.getLengthStatus() <<4); - tmp |= (((this.frameIrqActive && this.frameIrqEnabled)?1:0)<<6); + tmp |= (((this.frameIrqActive && this.frameIrqEnabled)? 1 : 0) << 6); tmp |= (this.dmc.getIrqStatus() <<7); this.frameIrqActive = false; this.dmc.irqGenerated = false; - return tmp&0xFFFF; + return tmp & 0xFFFF; }, writeReg: function(address, value){ - if (address>=0x4000 && address<0x4004) { + if (address >= 0x4000 && address < 0x4004) { // Square Wave 1 Control - this.square1.writeReg(address,value); + this.square1.writeReg(address, value); ////System.out.println("Square Write"); } - else if (address>=0x4004 && address<0x4008) { + else if (address >= 0x4004 && address < 0x4008) { // Square 2 Control - this.square2.writeReg(address,value); + this.square2.writeReg(address, value); } - else if (address>=0x4008 && address<0x400C) { + else if (address >= 0x4008 && address < 0x400C) { // Triangle Control - this.triangle.writeReg(address,value); + this.triangle.writeReg(address, value); } - else if(address>=0x400C && address<=0x400F){ + else if (address >= 0x400C && address <= 0x400F) { // Noise Control - this.noise.writeReg(address,value); + this.noise.writeReg(address, value); } - else if(address == 0x4010){ + else if (address === 0x4010){ // DMC Play mode & DMA frequency - this.dmc.writeReg(address,value); + this.dmc.writeReg(address, value); } - else if(address == 0x4011){ + else if (address === 0x4011){ // DMC Delta Counter - this.dmc.writeReg(address,value); + this.dmc.writeReg(address, value); } - else if(address == 0x4012){ + else if (address === 0x4012){ // DMC Play code starting address - this.dmc.writeReg(address,value); + this.dmc.writeReg(address, value); } - else if(address == 0x4013){ + else if (address === 0x4013){ // DMC Play code length - this.dmc.writeReg(address,value); + this.dmc.writeReg(address, value); } - else if(address == 0x4015){ + else if (address === 0x4015){ // Channel enable this.updateChannelEnable(value); - if (value != 0 && this.initCounter > 0) { + if (value !== 0 && this.initCounter > 0) { // Start hardware initialization this.initingHardware = true; } // DMC/IRQ Status - this.dmc.writeReg(address,value); + this.dmc.writeReg(address, value); } - else if(address == 0x4017){ + else if (address === 0x4017) { // Frame counter control this.countSequence = (value>>7)&1; this.masterFrameCounter = 0; this.frameIrqActive = false; - if (((value>>6)&0x1)==0){ + if (((value>>6)&0x1)===0){ this.frameIrqEnabled = true; } else { this.frameIrqEnabled = false; } - if(this.countSequence == 0){ + if (this.countSequence === 0) { // NTSC: this.frameIrqCounterMax = 4; this.derivedFrameCounter = 4; @@ -250,7 +248,7 @@ JSNES.PAPU.prototype = { }, resetCounter: function(){ - if(this.countSequence==0){ + if (this.countSequence === 0) { this.derivedFrameCounter = 4; }else{ this.derivedFrameCounter = 0; @@ -264,11 +262,11 @@ JSNES.PAPU.prototype = { // in the GUI. updateChannelEnable: function(value){ this.channelEnableValue = value&0xFFFF; - this.square1.setEnabled((value&1)!=0); - this.square2.setEnabled((value&2)!=0); - this.triangle.setEnabled((value&4)!=0); - this.noise.setEnabled((value&8)!=0); - this.dmc.setEnabled((value&16)!=0); + this.square1.setEnabled((value&1) !== 0); + this.square2.setEnabled((value&2) !== 0); + this.triangle.setEnabled((value&4) !== 0); + this.noise.setEnabled((value&8) !== 0); + this.dmc.setEnabled((value&16) !== 0); }, // Clocks the frame counter. It should be clocked at @@ -276,10 +274,12 @@ JSNES.PAPU.prototype = { // divided by 2 for those counters that are // clocked at cpu speed. clockFrameCounter: function(nCycles){ - if(this.initCounter > 0){ - if(this.initingHardware){ + if (this.initCounter > 0) { + if (this.initingHardware) { this.initCounter -= nCycles; - if(this.initCounter<=0) this.initingHardware = false; + if (this.initCounter <= 0) { + this.initingHardware = false; + } return; } } @@ -287,7 +287,7 @@ JSNES.PAPU.prototype = { // Don't process ticks beyond next sampling: nCycles += this.extraCycles; var maxCycles = this.sampleTimerMax-this.sampleTimer; - if((nCycles<<10) > maxCycles){ + if ((nCycles<<10) > maxCycles) { this.extraCycles = ((nCycles<<10) - maxCycles)>>10; nCycles -= this.extraCycles; @@ -305,7 +305,7 @@ JSNES.PAPU.prototype = { var noise = this.noise; // Clock DMC: - if(dmc.isEnabled){ + if (dmc.isEnabled) { dmc.shiftCounter-=(nCycles<<3); while(dmc.shiftCounter<=0 && dmc.dmaFrequency>0){ @@ -316,19 +316,19 @@ JSNES.PAPU.prototype = { } // Clock Triangle channel Prog timer: - if(triangle.progTimerMax>0){ + if (triangle.progTimerMax>0) { triangle.progTimerCount -= nCycles; while(triangle.progTimerCount <= 0){ triangle.progTimerCount += triangle.progTimerMax+1; - if(triangle.linearCounter>0 && triangle.lengthCounter>0){ + if (triangle.linearCounter>0 && triangle.lengthCounter>0) { triangle.triangleCounter++; triangle.triangleCounter &= 0x1F; - if(triangle.isEnabled){ - if(triangle.triangleCounter>=0x10){ + if (triangle.isEnabled) { + if (triangle.triangleCounter>=0x10) { // Normal value. triangle.sampleValue = (triangle.triangleCounter&0xF); }else{ @@ -343,7 +343,7 @@ JSNES.PAPU.prototype = { // Clock Square channel 1 Prog timer: square1.progTimerCount -= nCycles; - if(square1.progTimerCount <= 0){ + if (square1.progTimerCount <= 0) { square1.progTimerCount += (square1.progTimerMax+1)<<1; @@ -355,7 +355,7 @@ JSNES.PAPU.prototype = { // Clock Square channel 2 Prog timer: square2.progTimerCount -= nCycles; - if(square2.progTimerCount <= 0){ + if (square2.progTimerCount <= 0) { square2.progTimerCount += (square2.progTimerMax+1)<<1; @@ -367,7 +367,7 @@ JSNES.PAPU.prototype = { // Clock noise channel Prog timer: var acc_c = nCycles; - if(noise.progTimerCount-acc_c > 0){ + if (noise.progTimerCount-acc_c > 0) { // Do all cycles at once: noise.progTimerCount -= acc_c; @@ -379,12 +379,12 @@ JSNES.PAPU.prototype = { // Slow-step: while((acc_c--) > 0){ - if(--noise.progTimerCount <= 0 && noise.progTimerMax>0){ + if (--noise.progTimerCount <= 0 && noise.progTimerMax>0) { // Update noise shift register: noise.shiftReg <<= 1; - noise.tmp = (((noise.shiftReg << (noise.randomMode==0?1:6)) ^ noise.shiftReg) & 0x8000 ); - if(noise.tmp!=0){ + noise.tmp = (((noise.shiftReg << (noise.randomMode===0?1:6)) ^ noise.shiftReg) & 0x8000 ); + if (noise.tmp !== 0) { // Sample value must be 0. noise.shiftReg |= 0x01; @@ -395,7 +395,7 @@ JSNES.PAPU.prototype = { // Find sample value: noise.randomBit = 1; - if(noise.isEnabled && noise.lengthCounter>0){ + if (noise.isEnabled && noise.lengthCounter>0) { noise.sampleValue = noise.masterVolume; }else{ noise.sampleValue = 0; @@ -432,41 +432,41 @@ JSNES.PAPU.prototype = { // Clock sample timer: this.sampleTimer += nCycles<<10; - if(this.sampleTimer>=this.sampleTimerMax){ + if (this.sampleTimer>=this.sampleTimerMax) { // Sample channels: this.sample(); this.sampleTimer -= this.sampleTimerMax; } }, - accSample: function(cycles){ - var triangle = this.triangle; - + accSample: function(cycles) { // Special treatment for triangle channel - need to interpolate. - if(triangle.sampleCondition){ - - var triValue = parseInt((triangle.progTimerCount<<4) / (triangle.progTimerMax+1)); - if(triValue>16) triValue = 16; - if(triangle.triangleCounter >= 16){ - triValue = 16-triValue; + if (this.triangle.sampleCondition) { + this.triValue = parseInt((this.triangle.progTimerCount<<4) / + (this.triangle.progTimerMax+1), 10); + if (this.triValue > 16) { + this.triValue = 16; + } + if (this.triangle.triangleCounter >= 16) { + this.triValue = 16 - this.triValue; } // Add non-interpolated sample value: - triValue += triangle.sampleValue; + this.triValue += this.triangle.sampleValue; } // Now sample normally: - if(cycles == 2){ + if (cycles === 2) { - this.smpTriangle += triValue << 1; + this.smpTriangle += this.triValue << 1; this.smpDmc += this.dmc.sample << 1; this.smpSquare1 += this.square1.sampleValue << 1; this.smpSquare2 += this.square2.sampleValue << 1; this.accCount += 2; - }else if(cycles == 4){ + }else if (cycles === 4) { - this.smpTriangle += triValue << 2; + this.smpTriangle += this.triValue << 2; this.smpDmc += this.dmc.sample << 2; this.smpSquare1 += this.square1.sampleValue << 2; this.smpSquare2 += this.square2.sampleValue << 2; @@ -474,7 +474,7 @@ JSNES.PAPU.prototype = { }else{ - this.smpTriangle += cycles * triValue; + this.smpTriangle += cycles * this.triValue; this.smpDmc += cycles * this.dmc.sample; this.smpSquare1 += cycles * this.square1.sampleValue; this.smpSquare2 += cycles * this.square2.sampleValue; @@ -487,11 +487,11 @@ JSNES.PAPU.prototype = { frameCounterTick: function(){ this.derivedFrameCounter++; - if(this.derivedFrameCounter >= this.frameIrqCounterMax){ + if (this.derivedFrameCounter >= this.frameIrqCounterMax) { this.derivedFrameCounter = 0; } - if(this.derivedFrameCounter==1 || this.derivedFrameCounter==3){ + if (this.derivedFrameCounter===1 || this.derivedFrameCounter===3) { // Clock length & sweep: this.triangle.clockLengthCounter(); @@ -503,7 +503,7 @@ JSNES.PAPU.prototype = { } - if(this.derivedFrameCounter >= 0 && this.derivedFrameCounter < 4){ + if (this.derivedFrameCounter >= 0 && this.derivedFrameCounter < 4) { // Clock linear & decay: this.square1.clockEnvDecay(); @@ -513,7 +513,7 @@ JSNES.PAPU.prototype = { } - if(this.derivedFrameCounter == 3 && this.countSequence==0){ + if (this.derivedFrameCounter === 3 && this.countSequence===0) { // Enable IRQ: this.frameIrqActive = true; @@ -529,58 +529,79 @@ JSNES.PAPU.prototype = { // Samples the channels, mixes the output together, // writes to buffer and (if enabled) file. sample: function(){ + var sq_index, tnd_index; + + if (this.accCount > 0) { - if(this.accCount>0){ - - this.smpSquare1 <<= 4; - this.smpSquare1 = parseInt(this.smpSquare1/this.accCount); + this.smpSquare1 <<= 4; + this.smpSquare1 = parseInt(this.smpSquare1 / this.accCount, 10); - this.smpSquare2 <<= 4; - this.smpSquare2 = parseInt(this.smpSquare2/this.accCount); + this.smpSquare2 <<= 4; + this.smpSquare2 = parseInt(this.smpSquare2 / this.accCount, 10); - this.smpTriangle = parseInt(this.smpTriangle/this.accCount); + this.smpTriangle = parseInt(this.smpTriangle / this.accCount, 10); - this.smpDmc <<= 4; - this.smpDmc = parseInt(this.smpDmc/this.accCount); + this.smpDmc <<= 4; + this.smpDmc = parseInt(this.smpDmc / this.accCount, 10); - this.accCount = 0; - - }else{ - - this.smpSquare1 = this.square1.sampleValue << 4; - this.smpSquare2 = this.square2.sampleValue << 4; - this.smpTriangle = this.triangle.sampleValue ; - this.smpDmc = this.dmc.sample << 4; - + this.accCount = 0; + } + else { + this.smpSquare1 = this.square1.sampleValue << 4; + this.smpSquare2 = this.square2.sampleValue << 4; + this.smpTriangle = this.triangle.sampleValue; + this.smpDmc = this.dmc.sample << 4; } - var smpNoise = parseInt((this.noise.accValue<<4)/this.noise.accCount); - this.noise.accValue = smpNoise>>4; + var smpNoise = parseInt((this.noise.accValue << 4) / + this.noise.accCount, 10); + this.noise.accValue = smpNoise >> 4; this.noise.accCount = 1; // Stereo sound. // Left channel: - var sq_index = ( this.smpSquare1 * this.stereoPosLSquare1 + this.smpSquare2 * this.stereoPosLSquare2 )>>8; - var tnd_index = (3*this.smpTriangle * this.stereoPosLTriangle + (smpNoise<<1) * this.stereoPosLNoise + this.smpDmc*this.stereoPosLDMC)>>8; - if(sq_index >= this.square_table.length)sq_index = this.square_table.length-1; - if(tnd_index >= this.tnd_table.length)tnd_index = this.tnd_table.length-1; - var sampleValueL = this.square_table[sq_index] + this.tnd_table[tnd_index] - this.dcValue; + sq_index = ( + this.smpSquare1 * this.stereoPosLSquare1 + + this.smpSquare2 * this.stereoPosLSquare2 + ) >> 8; + tnd_index = ( + 3 * this.smpTriangle * this.stereoPosLTriangle + + (smpNoise<<1) * this.stereoPosLNoise + this.smpDmc * + this.stereoPosLDMC + ) >> 8; + if (sq_index >= this.square_table.length) { + sq_index = this.square_table.length-1; + } + if (tnd_index >= this.tnd_table.length) { + tnd_index = this.tnd_table.length - 1; + } + var sampleValueL = this.square_table[sq_index] + + this.tnd_table[tnd_index] - this.dcValue; // Right channel: - var sq_index = ( this.smpSquare1 * this.stereoPosRSquare1 + this.smpSquare2 * this.stereoPosRSquare2 )>>8; - var tnd_index = (3*this.smpTriangle * this.stereoPosRTriangle + (smpNoise<<1)* this.stereoPosRNoise + this.smpDmc*this.stereoPosRDMC)>>8; - if(sq_index >= this.square_table.length)sq_index = this.square_table.length-1; - if(tnd_index >= this.tnd_table.length)tnd_index = this.tnd_table.length-1; - var sampleValueR = this.square_table[sq_index] + this.tnd_table[tnd_index] - this.dcValue; + sq_index = (this.smpSquare1 * this.stereoPosRSquare1 + + this.smpSquare2 * this.stereoPosRSquare2 + ) >> 8; + tnd_index = (3 * this.smpTriangle * this.stereoPosRTriangle + + (smpNoise << 1) * this.stereoPosRNoise + this.smpDmc * + this.stereoPosRDMC + ) >> 8; + if (sq_index >= this.square_table.length) { + sq_index = this.square_table.length - 1; + } + if (tnd_index >= this.tnd_table.length) { + tnd_index = this.tnd_table.length - 1; + } + var sampleValueR = this.square_table[sq_index] + + this.tnd_table[tnd_index] - this.dcValue; // Remove DC from left channel: - var smpDiffL = sampleValueL - this.prevSampleL; + var smpDiffL = sampleValueL - this.prevSampleL; this.prevSampleL += smpDiffL; - this.smpAccumL += smpDiffL - (this.smpAccumL >> 10); + this.smpAccumL += smpDiffL - (this.smpAccumL >> 10); sampleValueL = this.smpAccumL; - - + // Remove DC from right channel: var smpDiffR = sampleValueR - this.prevSampleR; this.prevSampleR += smpDiffR; @@ -588,75 +609,83 @@ JSNES.PAPU.prototype = { sampleValueR = this.smpAccumR; // Write: - if (sampleValueL > this.maxSample) this.maxSample = sampleValueL; - if (sampleValueL < this.minSample) this.minSample = sampleValueL; - this.sampleBuffer[this.bufferIndex++] = (sampleValueL ); - this.sampleBuffer[this.bufferIndex++] = (sampleValueR ); + if (sampleValueL > this.maxSample) { + this.maxSample = sampleValueL; + } + if (sampleValueL < this.minSample) { + this.minSample = sampleValueL; + } + this.sampleBuffer[this.bufferIndex++] = sampleValueL; + this.sampleBuffer[this.bufferIndex++] = sampleValueR; // Write full buffer - if (this.bufferIndex == this.sampleBuffer.length) { + if (this.bufferIndex === this.sampleBuffer.length) { this.dynamicaudio.writeInt(this.sampleBuffer); this.sampleBuffer = new Array(this.bufferSize*2); this.bufferIndex = 0; } // Reset sampled values: - this.smpSquare1 = 0; - this.smpSquare2 = 0; + this.smpSquare1 = 0; + this.smpSquare2 = 0; this.smpTriangle = 0; - this.smpDmc = 0; + this.smpDmc = 0; }, getLengthMax: function(value){ - return this.lengthLookup[value>>3]; + return this.lengthLookup[value >> 3]; }, getDmcFrequency: function(value){ - if(value>=0 && value<0x10){ + if (value >= 0 && value < 0x10) { return this.dmcFreqLookup[value]; } return 0; }, getNoiseWaveLength: function(value){ - if(value>=0 && value<0x10){ + if (value >= 0 && value < 0x10) { return this.noiseWavelengthLookup[value]; } return 0; }, setPanning: function(pos){ - for(var i=0;i<5;i++){ + for (var i = 0; i < 5; i++) { this.panning[i] = pos[i]; } this.updateStereoPos(); }, setMasterVolume: function(value){ - if(value<0)value=0; - if(value>256)value=256; + if (value < 0) { + value = 0; + } + if (value > 256) { + value = 256; + } this.masterVolume = value; this.updateStereoPos(); }, updateStereoPos: function(){ - this.stereoPosLSquare1 = (this.panning[0]*this.masterVolume)>>8; - this.stereoPosLSquare2 = (this.panning[1]*this.masterVolume)>>8; - this.stereoPosLTriangle = (this.panning[2]*this.masterVolume)>>8; - this.stereoPosLNoise = (this.panning[3]*this.masterVolume)>>8; - this.stereoPosLDMC = (this.panning[4]*this.masterVolume)>>8; + this.stereoPosLSquare1 = (this.panning[0] * this.masterVolume) >> 8; + this.stereoPosLSquare2 = (this.panning[1] * this.masterVolume) >> 8; + this.stereoPosLTriangle = (this.panning[2] * this.masterVolume) >> 8; + this.stereoPosLNoise = (this.panning[3] * this.masterVolume) >> 8; + this.stereoPosLDMC = (this.panning[4] * this.masterVolume) >> 8; - this.stereoPosRSquare1 = this.masterVolume - this.stereoPosLSquare1; - this.stereoPosRSquare2 = this.masterVolume - this.stereoPosLSquare2; - this.stereoPosRTriangle = this.masterVolume - this.stereoPosLTriangle; - this.stereoPosRNoise = this.masterVolume - this.stereoPosLNoise; - this.stereoPosRDMC = this.masterVolume - this.stereoPosLDMC; + this.stereoPosRSquare1 = this.masterVolume - this.stereoPosLSquare1; + this.stereoPosRSquare2 = this.masterVolume - this.stereoPosLSquare2; + this.stereoPosRTriangle = this.masterVolume - this.stereoPosLTriangle; + this.stereoPosRNoise = this.masterVolume - this.stereoPosLNoise; + this.stereoPosRDMC = this.masterVolume - this.stereoPosLDMC; }, initLengthLookup: function(){ - this.lengthLookup = new Array( + this.lengthLookup = [ 0x0A, 0xFE, 0x14, 0x02, 0x28, 0x04, @@ -673,8 +702,7 @@ JSNES.PAPU.prototype = { 0x48, 0x1A, 0x10, 0x1C, 0x20, 0x1E - ); - + ]; }, initDmcFrequencyLookup: function(){ @@ -725,39 +753,33 @@ JSNES.PAPU.prototype = { }, initDACtables: function(){ - - this.square_table = new Array(32*16); - this.tnd_table = new Array(204*16); - var value; - - var ival; + var value, ival, i; var max_sqr = 0; var max_tnd = 0; - - for(var i=0;i<32*16;i++){ - + this.square_table = new Array(32*16); + this.tnd_table = new Array(204*16); + + for (i = 0; i < 32 * 16; i++) { value = 95.52 / (8128.0 / (i/16.0) + 100.0); value *= 0.98411; value *= 50000.0; - ival = parseInt(value); + ival = parseInt(value, 10); this.square_table[i] = ival; - if(ival > max_sqr){ + if (ival > max_sqr) { max_sqr = ival; } - } - for(var i=0;i<204*16;i++){ - + for (i = 0; i < 204 * 16; i++) { value = 163.67 / (24329.0 / (i/16.0) + 100.0); value *= 0.98411; value *= 50000.0; - ival = parseInt(value); + ival = parseInt(value, 10); this.tnd_table[i] = ival; - if(ival > max_tnd){ + if (ival > max_tnd) { max_tnd = ival; } @@ -767,7 +789,7 @@ JSNES.PAPU.prototype = { this.dcValue = this.dacRange/2; } -} +}; JSNES.PAPU.ChannelDM = function(papu) { @@ -797,7 +819,7 @@ JSNES.PAPU.ChannelDM = function(papu) { this.data = null; this.reset(); -} +}; JSNES.PAPU.ChannelDM.prototype = { clockDmc: function() { @@ -830,7 +852,7 @@ JSNES.PAPU.ChannelDM.prototype = { } this.dmaCounter--; - if(this.dmaCounter <= 0){ + if (this.dmaCounter <= 0) { // No more sample bits. this.hasSample = false; @@ -839,14 +861,14 @@ JSNES.PAPU.ChannelDM.prototype = { } - if(this.irqGenerated){ + if (this.irqGenerated) { this.papu.nes.cpu.requestIrq(this.papu.nes.cpu.IRQ_NORMAL); } }, endOfSample: function() { - if(this.playLengthCounter===0 && this.playMode==this.MODE_LOOP){ + if (this.playLengthCounter===0 && this.playMode===this.MODE_LOOP) { // Start from beginning of sample: this.playAddress = this.playStartAddress; @@ -854,15 +876,15 @@ JSNES.PAPU.ChannelDM.prototype = { } - if(this.playLengthCounter > 0){ + if (this.playLengthCounter > 0) { // Fetch next sample: this.nextSample(); - if(this.playLengthCounter == 0){ + if (this.playLengthCounter === 0) { // Last byte of sample fetched, generate IRQ: - if(this.playMode == this.MODE_IRQ){ + if (this.playMode === this.MODE_IRQ) { // Generate IRQ: this.irqGenerated = true; @@ -882,7 +904,7 @@ JSNES.PAPU.ChannelDM.prototype = { this.playLengthCounter--; this.playAddress++; - if(this.playAddress>0xFFFF){ + if (this.playAddress>0xFFFF) { this.playAddress = 0x8000; } @@ -890,48 +912,48 @@ JSNES.PAPU.ChannelDM.prototype = { }, writeReg: function(address, value) { - if (address == 0x4010) { + if (address === 0x4010) { // Play mode, DMA Frequency - if((value>>6)==0) { + if((value>>6)===0) { this.playMode = this.MODE_NORMAL; - }else if(((value>>6)&1)==1) { + }else if(((value>>6)&1)===1) { this.playMode = this.MODE_LOOP; - }else if((value>>6)==2) { + }else if((value>>6)===2) { this.playMode = this.MODE_IRQ; } - if((value&0x80)==0) { + if((value&0x80)===0) { this.irqGenerated = false; } this.dmaFrequency = this.papu.getDmcFrequency(value&0xF); - }else if(address == 0x4011){ + }else if (address === 0x4011) { // Delta counter load register: this.deltaCounter = (value>>1)&63; this.dacLsb = value&1; this.sample = ((this.deltaCounter<<1)+this.dacLsb); // update sample value - }else if(address == 0x4012){ + }else if (address === 0x4012) { // DMA address load register this.playStartAddress = (value<<6)|0x0C000; this.playAddress = this.playStartAddress; this.reg4012 = value; - }else if(address == 0x4013){ + }else if (address === 0x4013) { // Length of play code this.playLength = (value<<4)+1; this.playLengthCounter = this.playLength; this.reg4013 = value; - }else if(address == 0x4015){ + }else if (address === 0x4015) { // DMC/IRQ Status - if(((value>>4)&1)==0){ + if (((value>>4)&1)===0) { // Disable: this.playLengthCounter = 0; }else{ @@ -944,14 +966,14 @@ JSNES.PAPU.ChannelDM.prototype = { }, setEnabled: function(value) { - if((!this.isEnabled) && value){ + if ((!this.isEnabled) && value) { this.playLengthCounter = this.playLength; } this.isEnabled = value; }, getLengthStatus: function(){ - return ((this.playLengthCounter==0 || !this.isEnabled)?0:1); + return ((this.playLengthCounter===0 || !this.isEnabled)?0:1); }, getIrqStatus: function(){ @@ -976,7 +998,7 @@ JSNES.PAPU.ChannelDM.prototype = { this.reg4013 = 0; this.data = 0; } -} +}; JSNES.PAPU.ChannelNoise = function(papu) { @@ -1005,7 +1027,7 @@ JSNES.PAPU.ChannelNoise = function(papu) { this.tmp = null; this.reset(); -} +}; JSNES.PAPU.ChannelNoise.prototype = { reset: function() { @@ -1031,7 +1053,7 @@ JSNES.PAPU.ChannelNoise.prototype = { clockLengthCounter: function(){ if (this.lengthCounterEnable && this.lengthCounter>0){ this.lengthCounter--; - if (this.lengthCounter == 0) { + if (this.lengthCounter === 0) { this.updateSampleValue(); } } @@ -1059,26 +1081,26 @@ JSNES.PAPU.ChannelNoise.prototype = { }, updateSampleValue: function() { - if(this.isEnabled && this.lengthCounter>0){ + if (this.isEnabled && this.lengthCounter>0) { this.sampleValue = this.randomBit * this.masterVolume; } }, writeReg: function(address, value){ - if(address == 0x400C) { + if(address === 0x400C) { // Volume/Envelope decay: - this.envDecayDisable = ((value&0x10)!=0); + this.envDecayDisable = ((value&0x10) !== 0); this.envDecayRate = value&0xF; - this.envDecayLoopEnable = ((value&0x20)!=0); - this.lengthCounterEnable = ((value&0x20)==0); + this.envDecayLoopEnable = ((value&0x20) !== 0); + this.lengthCounterEnable = ((value&0x20)===0); this.masterVolume = this.envDecayDisable?this.envDecayRate:this.envVolume; - }else if(address == 0x400E) { + }else if(address === 0x400E) { // Programmable timer: this.progTimerMax = this.papu.getNoiseWaveLength(value&0xF); this.randomMode = value>>7; - }else if(address == 0x400F) { + }else if(address === 0x400F) { // Length counter this.lengthCounter = this.papu.getLengthMax(value&248); this.envReset = true; @@ -1096,9 +1118,9 @@ JSNES.PAPU.ChannelNoise.prototype = { }, getLengthStatus: function() { - return ((this.lengthCounter==0 || !this.isEnabled)?0:1); + return ((this.lengthCounter===0 || !this.isEnabled)?0:1); } -} +}; JSNES.PAPU.ChannelSquare = function(papu, square1) { @@ -1145,7 +1167,7 @@ JSNES.PAPU.ChannelSquare = function(papu, square1) { this.vol = null; this.reset(); -} +}; JSNES.PAPU.ChannelSquare.prototype = { reset: function() { @@ -1173,22 +1195,24 @@ JSNES.PAPU.ChannelSquare.prototype = { }, clockLengthCounter: function() { - if (this.lengthCounterEnable && this.lengthCounter>0){ + if (this.lengthCounterEnable && this.lengthCounter > 0){ this.lengthCounter--; - if (this.lengthCounter==0) this.updateSampleValue(); + if (this.lengthCounter === 0) { + this.updateSampleValue(); + } } }, clockEnvDecay: function() { - if(this.envReset){ + if (this.envReset) { // Reset envelope: this.envReset = false; this.envDecayCounter = this.envDecayRate + 1; this.envVolume = 0xF; - }else if((--this.envDecayCounter) <= 0){ + }else if ((--this.envDecayCounter) <= 0) { // Normal handling: this.envDecayCounter = this.envDecayRate + 1; - if(this.envVolume>0){ + if (this.envVolume>0) { this.envVolume--; }else{ this.envVolume = this.envDecayLoopEnable ? 0xF : 0; @@ -1200,16 +1224,16 @@ JSNES.PAPU.ChannelSquare.prototype = { }, clockSweep: function() { - if(--this.sweepCounter<=0){ + if (--this.sweepCounter<=0) { this.sweepCounter = this.sweepCounterMax + 1; - if(this.sweepActive && this.sweepShiftAmount>0 && this.progTimerMax>7){ + if (this.sweepActive && this.sweepShiftAmount>0 && this.progTimerMax>7) { // Calculate result from shifter: this.sweepCarry = false; - if(this.sweepMode==0){ + if (this.sweepMode===0) { this.progTimerMax += (this.progTimerMax>>this.sweepShiftAmount); - if(this.progTimerMax > 4095){ + if (this.progTimerMax > 4095) { this.progTimerMax = 4095; this.sweepCarry = true; } @@ -1219,17 +1243,17 @@ JSNES.PAPU.ChannelSquare.prototype = { } } - if(this.updateSweepPeriod){ + if (this.updateSweepPeriod) { this.updateSweepPeriod = false; this.sweepCounter = this.sweepCounterMax + 1; } }, updateSampleValue: function() { - if(this.isEnabled && this.lengthCounter>0 && this.progTimerMax>7){ + if (this.isEnabled && this.lengthCounter>0 && this.progTimerMax>7) { - if(this.sweepMode==0 && (this.progTimerMax + (this.progTimerMax>>this.sweepShiftAmount)) > 4095){ - //if(this.sweepCarry){ + if (this.sweepMode===0 && (this.progTimerMax + (this.progTimerMax>>this.sweepShiftAmount)) > 4095) { + //if (this.sweepCarry) { this.sampleValue = 0; }else{ this.sampleValue = this.masterVolume*this.dutyLookup[(this.dutyMode<<3)+this.squareCounter]; @@ -1241,31 +1265,31 @@ JSNES.PAPU.ChannelSquare.prototype = { writeReg: function(address, value){ var addrAdd = (this.sqr1?0:4); - if (address == 0x4000 + addrAdd) { + if (address === 0x4000 + addrAdd) { // Volume/Envelope decay: - this.envDecayDisable = ((value&0x10)!=0); + this.envDecayDisable = ((value&0x10) !== 0); this.envDecayRate = value & 0xF; - this.envDecayLoopEnable = ((value&0x20)!=0); + this.envDecayLoopEnable = ((value&0x20) !== 0); this.dutyMode = (value>>6)&0x3; - this.lengthCounterEnable = ((value&0x20)==0); + this.lengthCounterEnable = ((value&0x20)===0); this.masterVolume = this.envDecayDisable?this.envDecayRate:this.envVolume; this.updateSampleValue(); } - else if (address == 0x4001+addrAdd) { + else if (address === 0x4001+addrAdd) { // Sweep: - this.sweepActive = ((value&0x80)!=0); + this.sweepActive = ((value&0x80) !== 0); this.sweepCounterMax = ((value>>4)&7); this.sweepMode = (value>>3)&1; this.sweepShiftAmount = value&7; this.updateSweepPeriod = true; } - else if (address == 0x4002+addrAdd){ + else if (address === 0x4002+addrAdd){ // Programmable timer: this.progTimerMax &= 0x700; this.progTimerMax |= value; } - else if (address == 0x4003+addrAdd) { + else if (address === 0x4003+addrAdd) { // Programmable timer, length counter this.progTimerMax &= 0xFF; this.progTimerMax |= ((value&0x7)<<8); @@ -1278,16 +1302,18 @@ JSNES.PAPU.ChannelSquare.prototype = { } }, - setEnabled: function(value){ + setEnabled: function(value) { this.isEnabled = value; - if(!value) this.lengthCounter = 0; + if (!value) { + this.lengthCounter = 0; + } this.updateSampleValue(); }, getLengthStatus: function() { - return ((this.lengthCounter==0 || !this.isEnabled)?0:1); + return ((this.lengthCounter === 0 || !this.isEnabled) ? 0 : 1); } -} +}; JSNES.PAPU.ChannelTriangle = function(papu) { @@ -1309,7 +1335,7 @@ JSNES.PAPU.ChannelTriangle = function(papu) { this.tmp = null; this.reset(); -} +}; JSNES.PAPU.ChannelTriangle.prototype = { reset: function(){ @@ -1329,9 +1355,9 @@ JSNES.PAPU.ChannelTriangle.prototype = { }, clockLengthCounter: function(){ - if(this.lengthCounterEnable && this.lengthCounter>0){ + if (this.lengthCounterEnable && this.lengthCounter>0) { this.lengthCounter--; - if(this.lengthCounter==0){ + if (this.lengthCounter===0) { this.updateSampleCondition(); } } @@ -1343,12 +1369,12 @@ JSNES.PAPU.ChannelTriangle.prototype = { this.linearCounter = this.lcLoadValue; this.updateSampleCondition(); } - else if(this.linearCounter > 0){ + else if (this.linearCounter > 0) { // Decrement: this.linearCounter--; this.updateSampleCondition(); } - if(!this.lcControl){ + if (!this.lcControl) { // Clear halt flag: this.lcHalt = false; } @@ -1363,7 +1389,7 @@ JSNES.PAPU.ChannelTriangle.prototype = { }, writeReg: function(address, value){ - if (address == 0x4008) { + if (address === 0x4008) { // New values for linear counter: this.lcControl = (value&0x80)!==0; this.lcLoadValue = value&0x7F; @@ -1371,13 +1397,13 @@ JSNES.PAPU.ChannelTriangle.prototype = { // Length counter enable: this.lengthCounterEnable = !this.lcControl; } - else if (address == 0x400A) { + else if (address === 0x400A) { // Programmable timer: this.progTimerMax &= 0x700; this.progTimerMax |= value; } - else if(address == 0x400B) { + else if(address === 0x400B) { // Programmable timer, length counter this.progTimerMax &= 0xFF; this.progTimerMax |= ((value&0x07)<<8); @@ -1389,11 +1415,13 @@ JSNES.PAPU.ChannelTriangle.prototype = { }, clockProgrammableTimer: function(nCycles){ - if(this.progTimerMax>0){ + if (this.progTimerMax>0) { this.progTimerCount += nCycles; - while(this.progTimerMax > 0 && this.progTimerCount >= this.progTimerMax){ - this.progTimerCount-=this.progTimerMax; - if(this.isEnabled && this.lengthCounter>0 && this.linearCounter>0){ + while (this.progTimerMax > 0 && + this.progTimerCount >= this.progTimerMax) { + this.progTimerCount -= this.progTimerMax; + if (this.isEnabled && this.lengthCounter>0 && + this.linearCounter > 0) { this.clockTriangleGenerator(); } } @@ -1407,16 +1435,17 @@ JSNES.PAPU.ChannelTriangle.prototype = { setEnabled: function(value) { this.isEnabled = value; - if(!value) this.lengthCounter = 0; + if(!value) { + this.lengthCounter = 0; + } this.updateSampleCondition(); }, updateSampleCondition: function() { - this.sampleCondition = - this.isEnabled - && this.progTimerMax > 7 - && this.linearCounter > 0 - && this.lengthCounter > 0; + this.sampleCondition = this.isEnabled && + this.progTimerMax > 7 && + this.linearCounter > 0 && + this.lengthCounter > 0; } -} +}; diff --git a/js.2/ppu.js b/js.2/ppu.js index cb92332..ec6ff77 100644 --- a/js.2/ppu.js +++ b/js.2/ppu.js @@ -231,7 +231,7 @@ JSNES.PPU.prototype = { // Sets Nametable mirroring. setMirroring: function(mirroring){ - if(mirroring == this.currentMirroring){ + if (mirroring == this.currentMirroring) { return; } @@ -256,7 +256,7 @@ JSNES.PPU.prototype = { this.defineMirrorRegion(0x3000,0x2000,0xf00); this.defineMirrorRegion(0x4000,0x0000,0x4000); - if(mirroring == this.nes.rom.HORIZONTAL_MIRRORING){ + if (mirroring == this.nes.rom.HORIZONTAL_MIRRORING) { // Horizontal mirroring. this.ntable1[0] = 0; @@ -267,7 +267,7 @@ JSNES.PPU.prototype = { this.defineMirrorRegion(0x2400,0x2000,0x400); this.defineMirrorRegion(0x2c00,0x2800,0x400); - }else if(mirroring == this.nes.rom.VERTICAL_MIRRORING){ + }else if (mirroring == this.nes.rom.VERTICAL_MIRRORING) { // Vertical mirroring. this.ntable1[0] = 0; @@ -278,7 +278,7 @@ JSNES.PPU.prototype = { this.defineMirrorRegion(0x2800,0x2000,0x400); this.defineMirrorRegion(0x2c00,0x2400,0x400); - }else if(mirroring == this.nes.rom.SINGLESCREEN_MIRRORING){ + }else if (mirroring == this.nes.rom.SINGLESCREEN_MIRRORING) { // Single Screen mirroring @@ -291,7 +291,7 @@ JSNES.PPU.prototype = { this.defineMirrorRegion(0x2800,0x2000,0x400); this.defineMirrorRegion(0x2c00,0x2000,0x400); - }else if(mirroring == this.nes.rom.SINGLESCREEN_MIRRORING2){ + }else if (mirroring == this.nes.rom.SINGLESCREEN_MIRRORING2) { this.ntable1[0] = 1; @@ -303,7 +303,7 @@ JSNES.PPU.prototype = { this.defineMirrorRegion(0x2800,0x2400,0x400); this.defineMirrorRegion(0x2c00,0x2400,0x400); - }else{ + }else { // Assume Four-screen mirroring. @@ -321,7 +321,7 @@ JSNES.PPU.prototype = { // Assumes the regions don't overlap. // The 'to' region is the region that is physically in memory. defineMirrorRegion: function(fromStart, toStart, size){ - for(var i=0;i<size;i++){ + for (var i=0;i<size;i++) { this.vramMirrorTable[fromStart+i] = toStart+i; } }, @@ -332,7 +332,7 @@ JSNES.PPU.prototype = { this.nes.cpu.requestIrq(this.nes.cpu.IRQ_NMI); // Make sure everything is rendered: - if(this.lastRenderedScanline < 239){ + if (this.lastRenderedScanline < 239) { this.renderFramePartially( this.lastRenderedScanline+1,240-this.lastRenderedScanline ); @@ -350,7 +350,7 @@ JSNES.PPU.prototype = { case 19: // Dummy scanline. // May be variable length: - if(this.dummyCycleToggle){ + if (this.dummyCycleToggle) { // Remove dead cycle at end of scanline, // for next scanline: @@ -370,7 +370,7 @@ JSNES.PPU.prototype = { this.spr0HitX = -1; this.spr0HitY = -1; - if(this.f_bgVisibility == 1 || this.f_spVisibility==1){ + if (this.f_bgVisibility == 1 || this.f_spVisibility==1) { // Update counters: this.cntFV = this.regFV; @@ -379,21 +379,21 @@ JSNES.PPU.prototype = { this.cntVT = this.regVT; this.cntHT = this.regHT; - if(this.f_bgVisibility==1){ + if (this.f_bgVisibility==1) { // Render dummy scanline: this.renderBgScanline(false,0); } } - if(this.f_bgVisibility==1 && this.f_spVisibility==1){ + if (this.f_bgVisibility==1 && this.f_spVisibility==1) { // Check sprite 0 hit for first scanline: this.checkSprite0(0); } - if(this.f_bgVisibility==1 || this.f_spVisibility==1){ + if (this.f_bgVisibility==1 || this.f_spVisibility==1) { // Clock mapper IRQ Counter: this.nes.mmap.clockIrqCounter(); } @@ -427,10 +427,10 @@ JSNES.PPU.prototype = { // Check for sprite 0 (next scanline): if (!this.hitSpr0 && this.f_spVisibility == 1) { - if (this.sprX[0] >= -7 - && this.sprX[0] < 256 - && this.sprY[0] + 1 <= (this.scanline - 20) - && (this.sprY[0] + 1 + ( + if (this.sprX[0] >= -7 && + this.sprX[0] < 256 && + this.sprY[0] + 1 <= (this.scanline - 20) && + (this.sprY[0] + 1 + ( this.f_spriteSize === 0 ? 8 : 16 )) >= (this.scanline - 20)) { if (this.checkSprite0(this.scanline - 20)) { @@ -441,7 +441,7 @@ JSNES.PPU.prototype = { } - if(this.f_bgVisibility==1 || this.f_spVisibility==1){ + if (this.f_bgVisibility==1 || this.f_spVisibility==1) { // Clock mapper IRQ Counter: this.nes.mmap.clockIrqCounter(); } @@ -512,9 +512,9 @@ JSNES.PPU.prototype = { // Draw spr#0 hit coordinates: if (this.showSpr0Hit) { // Spr 0 position: - if (this.sprX[0] >= 0 && this.sprX[0] < 256 - && this.sprY[0] >= 0 && this.sprY[0] < 240) { - for (i=0; i<256; i++){ + if (this.sprX[0] >= 0 && this.sprX[0] < 256 && + this.sprY[0] >= 0 && this.sprY[0] < 240) { + for (i=0; i<256; i++) { buffer[(this.sprY[0]<<8)+i] = 0xFF5555; } for (i=0; i<240; i++) { @@ -522,8 +522,8 @@ JSNES.PPU.prototype = { } } // Hit position: - if (this.spr0HitX >= 0 && this.spr0HitX < 256 - && this.spr0HitY >= 0 && this.spr0HitY < 240) { + if (this.spr0HitX >= 0 && this.spr0HitX < 256 && + this.spr0HitY >= 0 && this.spr0HitY < 240) { for (i=0; i<256; i++) { buffer[(this.spr0HitY<<8)+i] = 0x55FF55; } @@ -538,14 +538,14 @@ JSNES.PPU.prototype = { // both are clipped after rendering is finished. if (this.clipToTvSize || this.f_bgClipping === 0 || this.f_spClipping === 0) { // Clip left 8-pixels column: - for(y=0;y<240;y++){ - for(x=0;x<8;x++){ + for (y=0;y<240;y++) { + for (x=0;x<8;x++) { buffer[(y<<8)+x] = 0; } } } - if(this.clipToTvSize){ + if (this.clipToTvSize) { // Clip right 8-pixels column too: for (y=0; y<240; y++) { for (x=0; x<8; x++) { @@ -676,12 +676,12 @@ JSNES.PPU.prototype = { scrollWrite: function(value){ this.triggerRendering(); - if(this.firstWrite){ + if (this.firstWrite) { // First write, horizontal scroll: this.regHT = (value>>3)&31; this.regFH = value&7; - }else{ + }else { // Second write, vertical scroll: this.regFV = value&7; @@ -697,14 +697,14 @@ JSNES.PPU.prototype = { // The first write sets the high byte, the second the low byte. writeVRAMAddress: function(address){ - if(this.firstWrite){ + if (this.firstWrite) { this.regFV = (address>>4)&3; this.regV = (address>>3)&1; this.regH = (address>>2)&1; this.regVT = (this.regVT&7) | ((address&3)<<3); - }else{ + }else { this.triggerRendering(); this.regVT = (this.regVT&24) | ((address>>5)&7); @@ -724,7 +724,7 @@ JSNES.PPU.prototype = { // Invoke mapper latch: this.cntsToAddress(); - if(this.vramAddress < 0x2000){ + if (this.vramAddress < 0x2000) { this.nes.mmap.latchAccess(this.vramAddress); } }, @@ -785,10 +785,10 @@ JSNES.PPU.prototype = { this.cntsToAddress(); this.regsToAddress(); - if(this.vramAddress >= 0x2000){ + if (this.vramAddress >= 0x2000) { // Mirroring is used. this.mirroredWrite(this.vramAddress,value); - }else{ + }else { // Write normally. this.writeMem(this.vramAddress,value); @@ -811,7 +811,7 @@ JSNES.PPU.prototype = { sramDMA: function(value){ var baseAddress = value * 0x100; var data; - for(var i=this.sramAddress; i < 256; i++){ + for (var i=this.sramAddress; i < 256; i++) { data = this.nes.cpu.mem[baseAddress+i]; this.spriteMem[i] = data; this.spriteRamWriteUpdate(i, data); @@ -905,37 +905,37 @@ JSNES.PPU.prototype = { // Writes to memory, taking into account // mirroring/mapping of address ranges. mirroredWrite: function(address, value){ - if(address>=0x3f00 && address<0x3f20){ + if (address>=0x3f00 && address<0x3f20) { // Palette write mirroring. - if(address==0x3F00 || address==0x3F10){ + if (address==0x3F00 || address==0x3F10) { this.writeMem(0x3F00,value); this.writeMem(0x3F10,value); - }else if(address==0x3F04 || address==0x3F14){ + }else if (address==0x3F04 || address==0x3F14) { this.writeMem(0x3F04,value); this.writeMem(0x3F14,value); - }else if(address==0x3F08 || address==0x3F18){ + }else if (address==0x3F08 || address==0x3F18) { this.writeMem(0x3F08,value); this.writeMem(0x3F18,value); - }else if(address==0x3F0C || address==0x3F1C){ + }else if (address==0x3F0C || address==0x3F1C) { this.writeMem(0x3F0C,value); this.writeMem(0x3F1C,value); - }else{ + }else { this.writeMem(address,value); } - }else{ + }else { // Use lookup table for mirrored address: - if(address<this.vramMirrorTable.length){ + if (address<this.vramMirrorTable.length) { this.writeMem(this.vramMirrorTable[address],value); - }else{ + }else { // FIXME alert("Invalid VRAM address: "+address.toString(16)); } @@ -944,7 +944,7 @@ JSNES.PPU.prototype = { }, triggerRendering: function(){ - if(this.scanline >= 21 && this.scanline <= 260){ + if (this.scanline >= 21 && this.scanline <= 260) { // Render sprites, and combine: this.renderFramePartially( this.lastRenderedScanline+1, @@ -957,7 +957,7 @@ JSNES.PPU.prototype = { }, renderFramePartially: function(startScan, scanCount){ - if(this.f_spVisibility == 1){ + if (this.f_spVisibility == 1) { this.renderSpritesPartially(startScan,scanCount,true); } @@ -1007,17 +1007,17 @@ JSNES.PPU.prototype = { var t, tpix, att, col; - for(var tile=0;tile<32;tile++){ + for (var tile=0;tile<32;tile++) { - if(scan>=0){ + if (scan>=0) { // Fetch tile & attrib data: - if(this.validTileData){ + if (this.validTileData) { // Get data from array: t = scantile[tile]; tpix = t.pix; att = attrib[tile]; - }else{ + }else { // Fetch data: t = ptTile[baseTile+nameTable[this.curNt].getTileIndex(this.cntHT,this.cntVT)]; tpix = t.pix; @@ -1030,21 +1030,21 @@ JSNES.PPU.prototype = { var sx = 0; var x = (tile<<3)-this.regFH; - if(x>-8){ - if(x<0){ + if (x>-8) { + if (x<0) { destIndex-=x; sx = -x; } - if(t.opaque[this.cntFV]){ - for(;sx<8;sx++){ + if (t.opaque[this.cntFV]) { + for (;sx<8;sx++) { targetBuffer[destIndex] = imgPalette[ tpix[tscanoffset+sx]+att ]; pixrendered[destIndex] |= 256; destIndex++; } - }else{ - for(;sx<8;sx++){ + }else { + for (;sx<8;sx++) { col = tpix[tscanoffset+sx]; if(col !== 0) { targetBuffer[destIndex] = imgPalette[ @@ -1060,7 +1060,7 @@ JSNES.PPU.prototype = { } // Increase Horizontal Tile Counter: - if(++this.cntHT==32){ + if (++this.cntHT==32) { this.cntHT=0; this.cntH++; this.cntH%=2; @@ -1078,15 +1078,15 @@ JSNES.PPU.prototype = { // update vertical scroll: this.cntFV++; - if(this.cntFV==8){ + if (this.cntFV==8) { this.cntFV = 0; this.cntVT++; - if(this.cntVT==30){ + if (this.cntVT==30) { this.cntVT = 0; this.cntV++; this.cntV%=2; this.curNt = this.ntable1[(this.cntV<<1)+this.cntH]; - }else if(this.cntVT==32){ + }else if (this.cntVT==32) { this.cntVT = 0; } @@ -1097,52 +1097,52 @@ JSNES.PPU.prototype = { }, renderSpritesPartially: function(startscan, scancount, bgPri){ - if(this.f_spVisibility==1){ + if (this.f_spVisibility === 1) { - var sprT1,sprT2; - - for(var i=0;i<64;i++){ - if(this.bgPriority[i]==bgPri && this.sprX[i]>=0 && this.sprX[i]<256 && this.sprY[i]+8>=startscan && this.sprY[i]<startscan+scancount){ + for (var i=0;i<64;i++) { + if (this.bgPriority[i]==bgPri && this.sprX[i]>=0 && + this.sprX[i]<256 && this.sprY[i]+8>=startscan && + this.sprY[i]<startscan+scancount) { // Show sprite. - if(this.f_spriteSize === 0){ + if (this.f_spriteSize === 0) { // 8x8 sprites this.srcy1 = 0; this.srcy2 = 8; - if(this.sprY[i]<startscan){ + if (this.sprY[i]<startscan) { this.srcy1 = startscan - this.sprY[i]-1; } - if(this.sprY[i]+8 > startscan+scancount){ + if (this.sprY[i]+8 > startscan+scancount) { this.srcy2 = startscan+scancount-this.sprY[i]+1; } - if(this.f_spPatternTable===0){ + if (this.f_spPatternTable===0) { this.ptTile[this.sprTile[i]].render(this.buffer, 0, this.srcy1, 8, this.srcy2, this.sprX[i], this.sprY[i]+1, this.sprCol[i], this.sprPalette, this.horiFlip[i], this.vertFlip[i], i, this.pixrendered ); - }else{ + }else { this.ptTile[this.sprTile[i]+256].render(this.buffer, 0, this.srcy1, 8, this.srcy2, this.sprX[i], this.sprY[i]+1, this.sprCol[i], this.sprPalette, this.horiFlip[i], this.vertFlip[i], i, this.pixrendered); } - }else{ + }else { // 8x16 sprites var top = this.sprTile[i]; - if((top&1)!==0){ + if ((top&1)!==0) { top = this.sprTile[i]-1+256; } var srcy1 = 0; var srcy2 = 8; - if(this.sprY[i]<startscan){ + if (this.sprY[i]<startscan) { srcy1 = startscan - this.sprY[i]-1; } - if(this.sprY[i]+8 > startscan+scancount){ + if (this.sprY[i]+8 > startscan+scancount) { srcy2 = startscan+scancount-this.sprY[i]; } @@ -1162,14 +1162,14 @@ JSNES.PPU.prototype = { this.pixrendered ); - var srcy1 = 0; - var srcy2 = 8; + srcy1 = 0; + srcy2 = 8; - if(this.sprY[i]+8<startscan){ + if (this.sprY[i]+8<startscan) { srcy1 = startscan - (this.sprY[i]+8+1); } - if(this.sprY[i]+16 > startscan+scancount){ + if (this.sprY[i]+16 > startscan+scancount) { srcy2 = startscan+scancount-(this.sprY[i]+8); } @@ -1202,41 +1202,42 @@ JSNES.PPU.prototype = { var toffset; var tIndexAdd = (this.f_spPatternTable === 0?0:256); - var x,y; + var x, y, t, i; var bufferIndex; var col; var bgPri; - var t; x = this.sprX[0]; y = this.sprY[0]+1; - if(this.f_spriteSize === 0){ + if (this.f_spriteSize === 0) { // 8x8 sprites. // Check range: - if(y<=scan && y+8>scan && x>=-7 && x<256){ + if (y <= scan && y + 8 > scan && x >= -7 && x < 256) { // Sprite is in range. // Draw scanline: - t = this.ptTile[this.sprTile[0]+tIndexAdd]; + t = this.ptTile[this.sprTile[0] + tIndexAdd]; col = this.sprCol[0]; bgPri = this.bgPriority[0]; - if(this.vertFlip[0]){ - toffset = 7-(scan-y); - }else{ - toffset = scan-y; + if (this.vertFlip[0]) { + toffset = 7 - (scan -y); } - toffset*=8; + else { + toffset = scan - y; + } + toffset *= 8; - bufferIndex = scan*256+x; - if(this.horiFlip[0]){ - for(var i=7;i>=0;i--){ - if(x>=0 && x<256){ - if(bufferIndex>=0 && bufferIndex<61440 && this.pixrendered[bufferIndex]!==0){ - if(t.pix[toffset+i] !== 0){ - this.spr0HitX = bufferIndex%256; + bufferIndex = scan * 256 + x; + if (this.horiFlip[0]) { + for (i = 7; i >= 0; i--) { + if (x >= 0 && x < 256) { + if (bufferIndex>=0 && bufferIndex<61440 && + this.pixrendered[bufferIndex] !==0 ) { + if (t.pix[toffset+i] !== 0) { + this.spr0HitX = bufferIndex % 256; this.spr0HitY = scan; return true; } @@ -1245,13 +1246,14 @@ JSNES.PPU.prototype = { x++; bufferIndex++; } - - }else{ - for(var i=0;i<8;i++){ - if(x>=0 && x<256){ - if(bufferIndex>=0 && bufferIndex<61440 && this.pixrendered[bufferIndex]!==0){ - if(t.pix[toffset+i] != 0){ - this.spr0HitX = bufferIndex%256; + } + else { + for (i = 0; i < 8; i++) { + if (x >= 0 && x < 256) { + if (bufferIndex >= 0 && bufferIndex < 61440 && + this.pixrendered[bufferIndex] !==0 ) { + if (t.pix[toffset+i] !== 0) { + this.spr0HitX = bufferIndex % 256; this.spr0HitY = scan; return true; } @@ -1262,30 +1264,31 @@ JSNES.PPU.prototype = { } } } - }else{ + } + else { // 8x16 sprites: // Check range: - if(y<=scan && y+16>scan && x>=-7 && x<256){ - + if (y <= scan && y + 16 > scan && x >= -7 && x < 256) { // Sprite is in range. // Draw scanline: - if(this.vertFlip[0]){ + if (this.vertFlip[0]) { toffset = 15-(scan-y); - }else{ + }else { toffset = scan-y; } - if(toffset<8){ + if (toffset<8) { // first half of sprite. t = this.ptTile[this.sprTile[0]+(this.vertFlip[0]?1:0)+((this.sprTile[0]&1)!==0?255:0)]; - }else{ + }else { // second half of sprite. t = this.ptTile[this.sprTile[0]+(this.vertFlip[0]?0:1)+((this.sprTile[0]&1)!==0?255:0)]; - if(this.vertFlip[0]){ + if (this.vertFlip[0]) { toffset = 15-toffset; - }else{ + } + else { toffset -= 8; } } @@ -1294,11 +1297,11 @@ JSNES.PPU.prototype = { bgPri = this.bgPriority[0]; bufferIndex = scan*256+x; - if(this.horiFlip[0]){ - for(var i=7;i>=0;i--){ - if(x>=0 && x<256){ - if(bufferIndex>=0 && bufferIndex<61440 && this.pixrendered[bufferIndex]!==0){ - if(t.pix[toffset+i] != 0){ + if (this.horiFlip[0]) { + for (i=7;i>=0;i--) { + if (x>=0 && x<256) { + if (bufferIndex>=0 && bufferIndex<61440 && this.pixrendered[bufferIndex]!==0) { + if (t.pix[toffset+i] !== 0) { this.spr0HitX = bufferIndex%256; this.spr0HitY = scan; return true; @@ -1309,12 +1312,13 @@ JSNES.PPU.prototype = { bufferIndex++; } - }else{ + } + else { - for(var i=0;i<8;i++){ - if(x>=0 && x<256){ - if(bufferIndex>=0 && bufferIndex<61440 && this.pixrendered[bufferIndex]!==0){ - if(t.pix[toffset+i] != 0){ + for (i=0;i<8;i++) { + if (x>=0 && x<256) { + if (bufferIndex>=0 && bufferIndex<61440 && this.pixrendered[bufferIndex]!==0) { + if (t.pix[toffset+i] !== 0) { this.spr0HitX = bufferIndex%256; this.spr0HitY = scan; return true; @@ -1338,79 +1342,68 @@ JSNES.PPU.prototype = { // update internally buffered data // appropriately. writeMem: function(address, value){ - this.vramMem[address] = value; // Update internally buffered data: - if(address < 0x2000){ - + if (address < 0x2000) { this.vramMem[address] = value; this.patternWrite(address,value); - - }else if(address >=0x2000 && address <0x23c0){ - - this.nameTableWrite(this.ntable1[0],address-0x2000,value); - - }else if(address >=0x23c0 && address <0x2400){ - + } + else if (address >=0x2000 && address <0x23c0) { + this.nameTableWrite(this.ntable1[0], address - 0x2000, value); + } + else if (address >=0x23c0 && address <0x2400) { this.attribTableWrite(this.ntable1[0],address-0x23c0,value); - - }else if(address >=0x2400 && address <0x27c0){ - + } + else if (address >=0x2400 && address <0x27c0) { this.nameTableWrite(this.ntable1[1],address-0x2400,value); - - }else if(address >=0x27c0 && address <0x2800){ - + } + else if (address >=0x27c0 && address <0x2800) { this.attribTableWrite(this.ntable1[1],address-0x27c0,value); - - }else if(address >=0x2800 && address <0x2bc0){ - + } + else if (address >=0x2800 && address <0x2bc0) { this.nameTableWrite(this.ntable1[2],address-0x2800,value); - - }else if(address >=0x2bc0 && address <0x2c00){ - + } + else if (address >=0x2bc0 && address <0x2c00) { this.attribTableWrite(this.ntable1[2],address-0x2bc0,value); - - }else if(address >=0x2c00 && address <0x2fc0){ - + } + else if (address >=0x2c00 && address <0x2fc0) { this.nameTableWrite(this.ntable1[3],address-0x2c00,value); - - }else if(address >=0x2fc0 && address <0x3000){ - + } + else if (address >=0x2fc0 && address <0x3000) { this.attribTableWrite(this.ntable1[3],address-0x2fc0,value); - - }else if(address >=0x3f00 && address <0x3f20){ - + } + else if (address >=0x3f00 && address <0x3f20) { this.updatePalettes(); - } }, // Reads data from $3f00 to $f20 // into the two buffered palettes. updatePalettes: function(){ + var i; - for(var i=0;i<16;i++){ - if (this.f_dispType == 0) { + for (i = 0; i < 16; i++) { + if (this.f_dispType === 0) { this.imgPalette[i] = this.palTable.getEntry( - this.vramMem[0x3f00+i] & 63 + this.vramMem[0x3f00 + i] & 63 ); } else { this.imgPalette[i] = this.palTable.getEntry( - this.vramMem[0x3f00+i] & 32 + this.vramMem[0x3f00 + i] & 32 ); } } - for(var i=0;i<16;i++){ - if (this.f_dispType == 0) { + for (i = 0; i < 16; i++) { + if (this.f_dispType === 0) { this.sprPalette[i] = this.palTable.getEntry( - this.vramMem[0x3f10+i] & 63 + this.vramMem[0x3f10 + i] & 63 ); } else { this.sprPalette[i] = this.palTable.getEntry( - this.vramMem[0x3f10+i] & 32 + this.vramMem[0x3f10 + i] & 32 ); } } @@ -1420,7 +1413,7 @@ JSNES.PPU.prototype = { // table buffers with this new byte. // In vNES, there is a version of this with 4 arguments which isn't used. patternWrite: function(address, value){ - var tileIndex = parseInt(address/16); + var tileIndex = parseInt(address / 16, 10); var leftOver = address%16; if (leftOver<8) { this.ptTile[tileIndex].setScanline( @@ -1458,27 +1451,27 @@ JSNES.PPU.prototype = { // Updates the internally buffered sprite // data with this new byte of info. spriteRamWriteUpdate: function(address, value){ - var tIndex = parseInt(address/4); + var tIndex = parseInt(address / 4, 10); - if(tIndex == 0){ + if (tIndex === 0) { //updateSpr0Hit(); this.checkSprite0(this.scanline-20); } - if(address%4 == 0){ + if (address%4 === 0) { // Y coordinate this.sprY[tIndex] = value; - }else if(address%4 == 1){ + }else if (address%4 == 1) { // Tile index this.sprTile[tIndex] = value; - }else if(address%4 == 2){ + }else if (address%4 == 2) { // Attributes this.vertFlip[tIndex] = ((value&0x80)!==0); this.horiFlip[tIndex] = ((value&0x40)!==0); this.bgPriority[tIndex] = ((value&0x20)!==0); this.sprCol[tIndex] = (value&3)<<2; - }else if(address%4 == 3){ + }else if (address%4 == 3) { // X coordinate this.sprX[tIndex] = value; } @@ -1512,16 +1505,16 @@ JSNES.PPU.NameTable.prototype = { writeAttrib: function(index, value){ var basex = (index % 8) * 4; - var basey = parseInt(index / 8) * 4; + var basey = parseInt(index / 8, 10) * 4; var add; var tx, ty; var attindex; - for(var sqy=0;sqy<2;sqy++){ - for(var sqx=0;sqx<2;sqx++){ + for (var sqy=0;sqy<2;sqy++) { + for (var sqx=0;sqx<2;sqx++) { add = (value>>(2*(sqy*2+sqx)))&3; - for(var y=0;y<2;y++){ - for(var x=0;x<2;x++){ + for (var y=0;y<2;y++) { + for (var x=0;x<2;x++) { tx = basex+sqx*2+x; ty = basey+sqy*2+y; attindex = ty*this.width+tx; @@ -1561,19 +1554,19 @@ JSNES.PPU.PaletteTable.prototype = { var r,g,b,col; // Calculate a table for each possible emphasis setting: - for(var emph=0;emph<8;emph++){ + for (var emph=0;emph<8;emph++) { // Determine color component factors: var rFactor=1.0, gFactor=1.0, bFactor=1.0; - if((emph&1)!==0){ + if ((emph&1)!==0) { rFactor = 0.75; bFactor = 0.75; } - if((emph&2)!==0){ + if ((emph&2)!==0) { rFactor = 0.75; gFactor = 0.75; } - if((emph&4)!==0){ + if ((emph&4)!==0) { gFactor = 0.75; bFactor = 0.75; } @@ -1581,20 +1574,20 @@ JSNES.PPU.PaletteTable.prototype = { this.emphTable[emph] = new Array(64); // Calculate table: - for(var i=0;i<64;i++){ + for (var i=0;i<64;i++) { col = this.curTable[i]; - r = parseInt(this.getRed(col) * rFactor); - g = parseInt(this.getGreen(col) * gFactor); - b = parseInt(this.getBlue(col) * bFactor); + r = parseInt(this.getRed(col) * rFactor, 10); + g = parseInt(this.getGreen(col) * gFactor, 10); + b = parseInt(this.getBlue(col) * bFactor, 10); this.emphTable[emph][i] = this.getRgb(r,g,b); } } }, setEmphasis: function(emph){ - if(emph != this.currentEmph){ + if (emph != this.currentEmph) { this.currentEmph = emph; - for(var i=0;i<64;i++){ + for (var i=0;i<64;i++) { this.curTable[i] = this.emphTable[emph][i]; } } @@ -1712,7 +1705,7 @@ JSNES.PPU.Tile = function() { JSNES.PPU.Tile.prototype = { setBuffer: function(scanline){ - for(this.y=0;this.y<8;this.y++){ + for (this.y=0;this.y<8;this.y++) { this.setScanline(this.y,scanline[this.y],scanline[this.y+8]); } }, @@ -1720,9 +1713,9 @@ JSNES.PPU.Tile.prototype = { setScanline: function(sline, b1, b2){ this.initialized = true; this.tIndex = sline<<3; - for(this.x=0;this.x<8;this.x++){ - this.pix[this.tIndex+this.x] = ((b1>>(7-this.x))&1) - + (((b2>>(7-this.x))&1)<<1); + for (this.x = 0; this.x < 8; this.x++) { + this.pix[this.tIndex + this.x] = ((b1 >> (7 - this.x)) & 1) + + (((b2 >> (7 - this.x)) & 1) << 1); if(this.pix[this.tIndex+this.x] === 0) { this.opaque[sline] = false; } @@ -1731,37 +1724,37 @@ JSNES.PPU.Tile.prototype = { render: function(buffer, srcx1, srcy1, srcx2, srcy2, dx, dy, palAdd, palette, flipHorizontal, flipVertical, pri, priTable) { - if(dx<-7 || dx>=256 || dy<-7 || dy>=240){ + if (dx<-7 || dx>=256 || dy<-7 || dy>=240) { return; } this.w=srcx2-srcx1; this.h=srcy2-srcy1; - if(dx<0){ + if (dx<0) { srcx1-=dx; } - if(dx+srcx2>=256){ + if (dx+srcx2>=256) { srcx2=256-dx; } - if(dy<0){ + if (dy<0) { srcy1-=dy; } - if(dy+srcy2>=240){ + if (dy+srcy2>=240) { srcy2=240-dy; } - if(!flipHorizontal && !flipVertical){ + if (!flipHorizontal && !flipVertical) { this.fbIndex = (dy<<8)+dx; this.tIndex = 0; - for(this.y=0;this.y<8;this.y++){ - for(this.x=0;this.x<8;this.x++){ - if(this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2){ + for (this.y=0;this.y<8;this.y++) { + for (this.x=0;this.x<8;this.x++) { + if (this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2) { this.palIndex = this.pix[this.tIndex]; this.tpri = priTable[this.fbIndex]; - if(this.palIndex!==0 && pri<=(this.tpri&0xFF)){ + if (this.palIndex!==0 && pri<=(this.tpri&0xFF)) { //console.log("Rendering upright tile to buffer"); buffer[this.fbIndex] = palette[this.palIndex+palAdd]; this.tpri = (this.tpri&0xF00)|pri; @@ -1775,16 +1768,16 @@ JSNES.PPU.Tile.prototype = { this.fbIndex+=256; } - }else if(flipHorizontal && !flipVertical){ + }else if (flipHorizontal && !flipVertical) { this.fbIndex = (dy<<8)+dx; this.tIndex = 7; - for(this.y=0;this.y<8;this.y++){ - for(this.x=0;this.x<8;this.x++){ - if(this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2){ + for (this.y=0;this.y<8;this.y++) { + for (this.x=0;this.x<8;this.x++) { + if (this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2) { this.palIndex = this.pix[this.tIndex]; this.tpri = priTable[this.fbIndex]; - if(this.palIndex!==0 && pri<=(this.tpri&0xFF)){ + if (this.palIndex!==0 && pri<=(this.tpri&0xFF)) { buffer[this.fbIndex] = palette[this.palIndex+palAdd]; this.tpri = (this.tpri&0xF00)|pri; priTable[this.fbIndex] =this.tpri; @@ -1803,12 +1796,12 @@ JSNES.PPU.Tile.prototype = { this.fbIndex = (dy<<8)+dx; this.tIndex = 56; - for(this.y=0;this.y<8;this.y++){ - for(this.x=0;this.x<8;this.x++){ - if(this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2){ + for (this.y=0;this.y<8;this.y++) { + for (this.x=0;this.x<8;this.x++) { + if (this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2) { this.palIndex = this.pix[this.tIndex]; this.tpri = priTable[this.fbIndex]; - if(this.palIndex!==0 && pri<=(this.tpri&0xFF)){ + if (this.palIndex!==0 && pri<=(this.tpri&0xFF)) { buffer[this.fbIndex] = palette[this.palIndex+palAdd]; this.tpri = (this.tpri&0xF00)|pri; priTable[this.fbIndex] =this.tpri; @@ -1826,12 +1819,12 @@ JSNES.PPU.Tile.prototype = { else { this.fbIndex = (dy<<8)+dx; this.tIndex = 63; - for(this.y=0;this.y<8;this.y++){ - for(this.x=0;this.x<8;this.x++){ - if(this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2){ + for (this.y=0;this.y<8;this.y++) { + for (this.x=0;this.x<8;this.x++) { + if (this.x>=srcx1 && this.x<srcx2 && this.y>=srcy1 && this.y<srcy2) { this.palIndex = this.pix[this.tIndex]; this.tpri = priTable[this.fbIndex]; - if(this.palIndex!==0 && pri<=(this.tpri&0xFF)){ + if (this.palIndex!==0 && pri<=(this.tpri&0xFF)) { buffer[this.fbIndex] = palette[this.palIndex+palAdd]; this.tpri = (this.tpri&0xF00)|pri; priTable[this.fbIndex] =this.tpri; diff --git a/js.2/rom.js b/js.2/rom.js index db4a483..f42e3e6 100644 --- a/js.2/rom.js +++ b/js.2/rom.js @@ -43,7 +43,7 @@ JSNES.ROM = function(nes) { this.mapperName[71] = "Camerica chip"; this.mapperName[78] = "Irem 74HC161/32-based"; this.mapperName[91] = "Pirate HK-SF3 chip"; -} +}; JSNES.ROM.prototype = { // Mirroring types: @@ -71,41 +71,43 @@ JSNES.ROM.prototype = { valid: false, load: function(data) { - if (data.indexOf("NES\x1a") == -1) { - alert("Not a valid NES ROM."); + var i, j, v; + + if (data.indexOf("NES\x1a") === -1) { + this.nes.ui.updateStatus("Not a valid NES ROM."); return; } this.header = new Array(16); - for (var i = 0; i < 16; i++) { + for (i = 0; i < 16; 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 - this.mirroring = ((this.header[6] & 1) !=0 ? 1 : 0); - this.batteryRam = (this.header[6] & 2) != 0; - this.trainer = (this.header[6] & 4) != 0; - this.fourScreen = (this.header[6] & 8) != 0; + this.mirroring = ((this.header[6] & 1) !== 0 ? 1 : 0); + this.batteryRam = (this.header[6] & 2) !== 0; + this.trainer = (this.header[6] & 4) !== 0; + this.fourScreen = (this.header[6] & 8) !== 0; this.mapperType = (this.header[6] >> 4) | (this.header[7] & 0xF0); /* TODO if (this.batteryRam) this.loadBatteryRam();*/ // Check whether byte 8-15 are zero's: var foundError = false; - for (var i=8; i<16; i++) { - if (this.header[i] != 0) { + for (i=8; i<16; i++) { + if (this.header[i] !== 0) { foundError = true; break; } } if (foundError) { - mapperType &= 0xF; // Ignore byte 7 + this.mapperType &= 0xF; // Ignore byte 7 } // Load PRG-ROM banks: this.rom = new Array(this.romCount); var offset = 16; - for (var i=0; i < this.romCount; i++) { + for (i=0; i < this.romCount; i++) { this.rom[i] = new Array(16384); - for (var j=0; j < 16384; j++) { + for (j=0; j < 16384; j++) { if (offset+j >= data.length) { break; } @@ -115,9 +117,9 @@ JSNES.ROM.prototype = { } // Load CHR-ROM banks: this.vrom = new Array(this.vromCount); - for (var i=0; i < this.vromCount; i++) { + for (i=0; i < this.vromCount; i++) { this.vrom[i] = new Array(4096); - for (var j=0; j < 4096; j++) { + for (j=0; j < 4096; j++) { if (offset+j >= data.length){ break; } @@ -128,9 +130,9 @@ JSNES.ROM.prototype = { // Create VROM tiles: this.vromTile = new Array(this.vromCount); - for (var i=0; i < this.vromCount; i++) { + for (i=0; i < this.vromCount; i++) { this.vromTile[i] = new Array(256); - for (var j=0; j < 256; j++) { + for (j=0; j < 256; j++) { this.vromTile[i][j] = new JSNES.PPU.Tile(); } } @@ -138,8 +140,8 @@ JSNES.ROM.prototype = { // Convert CHR-ROM banks to tiles: var tileIndex; var leftOver; - for (var v=0; v < this.vromCount; v++) { - for (var i=0; i < 4096; i++) { + for (v=0; v < this.vromCount; v++) { + for (i=0; i < 4096; i++) { tileIndex = i >> 4; leftOver = i % 16; if (leftOver < 8) { @@ -166,7 +168,7 @@ JSNES.ROM.prototype = { if (this.fourScreen) { return this.FOURSCREEN_MIRRORING; } - if (this.mirroring == 0) { + if (this.mirroring === 0) { return this.HORIZONTAL_MIRRORING; } return this.VERTICAL_MIRRORING; @@ -180,7 +182,7 @@ JSNES.ROM.prototype = { }, mapperSupported: function() { - return new Boolean(JSNES.Mappers[this.mapperType]); + return typeof JSNES.Mappers[this.mapperType] !== 'undefined'; }, createMapper: function() { @@ -188,8 +190,8 @@ JSNES.ROM.prototype = { return new JSNES.Mappers[this.mapperType](this.nes); } else { - alert("Mapper not supported: "+this.mapperType); + this.nes.ui.updateStatus("This ROM uses a mapper not supported by JSNES: "+this.getMapperName()+"("+this.mapperType+")"); return null; } } -} +}; @@ -6,7 +6,7 @@ self.root = $('<div></div>'); self.screen = $('<canvas class="nes-screen" width="256" height="240"></canvas>').appendTo(self.root); self.controls = $('<div class="nes-controls"></div>').appendTo(self.root); - self.romSelect = $('<select class="nes-roms"></select>').appendTo(self.controls) + self.romSelect = $('<select class="nes-roms"></select>').appendTo(self.controls); self.buttons = { pause: $('<input type="button" value="pause" class="nes-pause" disabled="disabled">').appendTo(self.controls), restart: $('<input type="button" value="restart" class="nes-restart" disabled="disabled">').appendTo(self.controls), @@ -21,7 +21,7 @@ $.ajax({ url: escape(self.romSelect.val()), xhr: function() { - var xhr = $.ajaxSettings.xhr() + var xhr = $.ajaxSettings.xhr(); // Download as binary xhr.overrideMimeType('text/plain; charset=x-user-defined'); return xhr; @@ -88,8 +88,8 @@ if (self.nes.mmap) { self.nes.mmap.mousePressed = true; // FIXME: does not take into account zoom - self.nes.mmap.mouseX = e.pageX-self.screen.offset()["left"]; - self.nes.mmap.mouseY = e.pageY-self.screen.offset()["top"]; + self.nes.mmap.mouseX = e.pageX - self.screen.offset().left; + self.nes.mmap.mouseY = e.pageY - self.screen.offset().top; } }).mouseup(function() { setTimeout(function() { @@ -131,14 +131,16 @@ this.romSelect.children().remove(); $("<option>Select a ROM...</option>").appendTo(this.romSelect); for (var groupName in roms) { - var optgroup = $('<optgroup></optgroup>') - .attr("label", groupName); - for (var i = 0; i < roms[groupName].length; i++) { - $('<option>'+roms[groupName][i][0]+'</option>') - .attr("value", roms[groupName][i][1]) - .appendTo(optgroup); + if (roms.hasOwnProperty(groupName)) { + var optgroup = $('<optgroup></optgroup>'). + attr("label", groupName); + for (var i = 0; i < roms[groupName].length; i++) { + $('<option>'+roms[groupName][i][0]+'</option>') + .attr("value", roms[groupName][i][1]) + .appendTo(optgroup); + } + this.romSelect.append(optgroup); } - this.romSelect.append(optgroup); } } }; diff --git a/js.2/utils.js b/js.2/utils.js index 6eacce3..716e5da 100644 --- a/js.2/utils.js +++ b/js.2/utils.js @@ -5,4 +5,4 @@ JSNES.Utils = { dest[destPos+i] = src[srcPos+i]; } } -} +}; |