Browse Source

Preliminary implementations for memory modules and a memory mapping controller (MMC).

master
Bryan Miller 5 years ago
parent
commit
eed4178665
2 changed files with 89 additions and 0 deletions
  1. +41
    -0
      chip/memory.js
  2. +48
    -0
      chip/mmc.js

+ 41
- 0
chip/memory.js View File

@@ -0,0 +1,41 @@


class Memory{
constructor(size, ro){
this.__ro = (ro === true);
this.__map = null;
this.__addr = 0;
if (size > 0){
this.__map = new Uint8Array(size);
}
}

get size(){return (this.__map) ? this.__map.length : 0;}

get address(){return this.__addr;}
set address(a){
if (this.__map)
this.__addr = Math.min(this.__map.length, Math.max(0, a));
}

get byte(){return (this.__map) ? this.__map[this.__addr] : -1;}
set byte(b){
if (!this.__ro && this.__map)
this.__map[this.__addr] = b;
}

sysStore(address, data){
if (this.__map){
let dc = data;
if (address < 0 || address >= this.__map.length)
throw new RangeError("Memory address out of range.");
if (this.__map.length - address < dc.length)
dc = dc.slice(0, this.__map.length - address);
this.__map.set(address, dc);
return dc.length;
}
}
}


module.exports = Memory;

+ 48
- 0
chip/mmc.js View File

@@ -0,0 +1,48 @@

var Memory = require('chip/memory');

class Bank{
constructor(mem){
this.__mem = [mem];
this.__idx = 0;
}

get mem(){return this.__mem[this.__idx];}

get idx(){return this.__idx;}
set idx(i){
if (i >= 0 && i < this.__mem.length)
this.__idx = i;
}

addMemModule(mem){
if (!(mem instanceof Memory))
throw new ValueError("Only Memory instances can be added to MMC Banks.");
if (this.__mem.length >= 4)
throw new RangeError("Bank handling maximum memory modules.");
if (this.__mem.length > 0 && mem.size !== this.__mem[0].size)
throw new RangeError("Memory module does not match size of already connected memory modules on this bank.");
this.__mem.push(mem);
}
}

class MMC{
constructor(){
this.__banks = [];
}

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

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.
}

module.exports = MMC;

Loading…
Cancel
Save