| } | } | ||||
| addMemModule(mem){ | addMemModule(mem){ | ||||
| if (!(mem instanceof Memory)) | |||||
| throw new ValueError("Only Memory instances can be added to MMC Banks."); | |||||
| if (!(mem instanceof IMem)) | |||||
| throw new ValueError("Only IMem instances can be added to MMC Banks."); | |||||
| if (this.__mem.length >= 4) | if (this.__mem.length >= 4) | ||||
| throw new RangeError("Bank handling maximum memory modules."); | throw new RangeError("Bank handling maximum memory modules."); | ||||
| if (this.__mem.length > 0 && mem.size !== this.__mem[0].size) | if (this.__mem.length > 0 && mem.size !== this.__mem[0].size) | ||||
| if (addroff < 0 || addroff === this.size){ | if (addroff < 0 || addroff === this.size){ | ||||
| this.__switches.push(new Switch(mem)); | this.__switches.push(new Switch(mem)); | ||||
| } else { | } else { | ||||
| offset = 0; | |||||
| let offset = 0; | |||||
| for (let s=0; s < this.__switches.length; s++){ | for (let s=0; s < this.__switches.length; s++){ | ||||
| if (addroff === offset){ | if (addroff === offset){ | ||||
| if (this.__switches[s].mem.size !== mem.size) | if (this.__switches[s].mem.size !== mem.size) | ||||
| } | } | ||||
| return this; | return this; | ||||
| } | } | ||||
| mmSwitchRegister(){ | |||||
| return (function(byte){ | |||||
| let mmcidx = (byte & 0xF0) >> 4; | |||||
| if (mmcidx < this.__switches.length){ | |||||
| this.__switches[mmcidx].idx = (byte & 0x0F); | |||||
| } | |||||
| }).bind(this); | |||||
| } | |||||
| switchBank(bank){ | |||||
| // The variable <bank> contains two 4 bit offsets. | |||||
| // The upper 4 bits is the bank switch offset. | |||||
| // The lower 4 bits is the bank offset within the switch. | |||||
| let mmcidx = (bank & 0xF0) >> 4; | |||||
| if (mmcidx < this.__switches.length){ | |||||
| this.__switches[mmcidx].idx = (bank & 0x0F); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| module.exports = MMC; | module.exports = MMC; |