| @@ -26,9 +26,13 @@ class Bank{ | |||
| } | |||
| } | |||
| class MMC{ | |||
| constructor(){ | |||
| this.__banks = []; | |||
| this.__addr = 0; | |||
| this.__bnkidx = 0; | |||
| } | |||
| get size(){ | |||
| @@ -39,10 +43,58 @@ class MMC{ | |||
| get banks(){return this.__banks.length;} | |||
| // TODO: Add a get/set for the memory address. This should handle banking and memory address offsets. | |||
| // TODO: Add a get/set for memory data at the current address. | |||
| // TODO: Add method for adding memory modules} | |||
| // TODO: Add a connection between a memory address and the triggering of bank switching. | |||
| get address(){return this.__addr;} | |||
| set address(a){ | |||
| if (a >= 0 && a < this.size){ | |||
| this.__addr = a; | |||
| offset = 0; | |||
| for (let b=0; b < this.__banks.length; b++){ | |||
| if (a >= offset && a < offset + this.__banks[b].mem.size){ | |||
| this.__bnkidx = b; | |||
| this.__banks[b].mem.address = a - offset; | |||
| break; | |||
| } else { | |||
| offset += this.__banks[b].mem.size; | |||
| } | |||
| } | |||
| } | |||
| } | |||
| get byte(){return (this.__banks.length > 0) ? this.__banks[this.__bnkidx].mem.byte : -1;} | |||
| set byte(b){ | |||
| if (this.__banks.length > 0){ | |||
| this.__banks[this.__bnkidx].mem.byte = b; | |||
| } | |||
| } | |||
| connectMemory(mem, addroff){ | |||
| addroff = (typeof(addroff) === 'number' && addroff >= 0) ? addroff : -1; | |||
| if (addroff < 0 || addroff === this.size){ | |||
| this.__banks.push(new Bank(mem)); | |||
| } else { | |||
| offset = 0; | |||
| for (let b=0; b < this.__banks.length; b++){ | |||
| if (addroff === offset){ | |||
| if (this.__banks[b].mem.size !== mem.size) | |||
| throw new RangeError("Memory modules assigned to the same bank must be the same byte size."); | |||
| this.__banks[b].addMemModule(mem); | |||
| offset = -1; | |||
| break; | |||
| } else { | |||
| offset += this.__banks[b].mem.size; | |||
| } | |||
| } | |||
| if (offset >= 0) | |||
| throw new RangeError("Cannot align memory module to bank at address " + addroff); | |||
| } | |||
| return this; | |||
| } | |||
| mmSwitchCallback(){ | |||
| return (function(byte){ | |||
| // TODO: Check the bits to determin which bank and bank index is being switched on. | |||
| }).bind(this); | |||
| } | |||
| } | |||
| module.exports = MMC; | |||