Browse Source

Added pages property to memory. Bringing MMC more inline with IMem interface.

master
Bryan Miller 5 years ago
parent
commit
9d773f6894
3 changed files with 61 additions and 1 deletions
  1. +1
    -0
      src/memory/imem.js
  2. +4
    -0
      src/memory/memory.js
  3. +56
    -1
      src/memory/mmc.js

+ 1
- 0
src/memory/imem.js View File

@@ -3,6 +3,7 @@
class IMem{
constructor(){}

get pages(){return 0;}
get size(){return 0;}
get writable(){return false;}


+ 4
- 0
src/memory/memory.js View File

@@ -33,6 +33,7 @@ class ROM extends IMem{
this.__rlisteners = new Listener();
}

get pages(){return this.__map.length / 256;}
get size(){return this.__map.length;}

get address(){return this.__addr;}
@@ -89,6 +90,7 @@ class RAM extends ROM {
this.__wlisteners = new Listener();
}

get pages(){return this.__map.length/256;}
get size(){return this.__map.length;}
get writable(){return true;}

@@ -129,7 +131,9 @@ class Shadow extends IMem {
this.__wlisteners = new Listener();
}

get pages(){return this.__size/256;}
get size(){return this.__size;}
get registers(){return this.__map.length;}
get writable(){return true;}

get address(){return this.__addr;}

+ 56
- 1
src/memory/mmc.js View File

@@ -36,12 +36,28 @@ class MMC extends IMem{
this.__sidx = 0;
}

get pages(){
return this.__switches.reduce((acc, s)=>{
return acc + s.mem.pages;
}, 0);
}

get size(){
return this.__switches.reduce((acc, s)=>{
acc += s.mem.size;
return acc + s.mem.size;
}, 0);
}

get writable(){
let iw = false;
for (let i=0; i < this.__switches.length; i++){
if (this.__switches[i].mem.writable){
iw = true; break;
}
}
return iw;
}

get switches(){return this.__switches.length;}

get address(){return this.__addr;}
@@ -68,6 +84,45 @@ class MMC extends IMem{
}
}

load(addr, data){
if (addr < 0 || addr > this.size)
throw new RangeError("Memory address out of range.");
let offset = 0;
for (let i = 0; i < this.__switches.length; i++){
let mem = this.__switches[i].mem;
let od = data;
if (addr >= offset && addr < offset + mem.size){
if ((addr - offset) + d.length >= mem.size)
od = data.slice(0, mem.size);
mem.load(addr - offset, od);
}
data = data.slice(mem.size);
offset += mem.size;
if (d.length > 0)
addr = offset;
}
return this;
}

clearPage(paddr){
for (let i=0; i < this.__switches.length; i++){
let pgs = this.__switches[i].mem.pages;
if (paddr < pgs){
this.__switches[i].mem.clearPage(addr);
break;
}
paddr -= pgs;
}
return this;
}

clear(){
this.__switches.forEach((s)=>{
s.clear();
});
return this;
}

connectMemory(mem, addroff){
addroff = (typeof(addroff) === 'number' && addroff >= 0) ? addroff : -1;
if (addroff < 0 || addroff === this.size){

Loading…
Cancel
Save