v6502/vm/computer/ram.js

118 lines
3.7 KiB
JavaScript

/*
* 6502 Based Virtual Computer
* Copyright (C) 2011 Scott C. Duensing <scott@jaegertech.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// Memory
function ram(newSize) {
// --- Private Types.
function T_EVENT() {
this.callback = null;
this.startAddress = 0;
this.endAddress = 0;
this.anyData = null;
};
// --- Private variables.
var MEMORY = new Array(newSize);
var WRITE_EVENTS = new Array();
var READ_EVENTS = new Array();
var EVENTS_ENABLED = true;
var EVENTS_ENABLED_COUNTER = 0;
// --- Public methods.
// Add callback for memory read event.
this.addReadCallback = function(newStart, newEnd, newData, newCallback) {
var event = new T_EVENT();
event.callback = newCallback;
event.startAddress = newStart;
event.endAddress = newEnd;
event.anyData = newData;
READ_EVENTS.push(event);
console.log("Read event " + READ_EVENTS.length + " added from 0x" + newStart.toString(16) + " to 0x" + newEnd.toString(16));
};
// Add callback for memory write event.
this.addWriteCallback = function(newStart, newEnd, newData, newCallback) {
var event = new T_EVENT();
event.callback = newCallback;
event.startAddress = newStart;
event.endAddress = newEnd;
event.anyData = newData;
WRITE_EVENTS.push(event);
console.log("Write event " + WRITE_EVENTS.length + " added from 0x" + newStart.toString(16) + " to 0x" + newEnd.toString(16));
};
// Are we performing callbacks?
this.callbacksEnabled = function(trueFalse) {
EVENTS_ENABLED_COUNTER += (trueFalse ? -1 : 1);
EVENTS_ENABLED = (EVENTS_ENABLED_COUNTER == 0);
};
// See how much RAM we have.
this.getSize = function() {
return MEMORY.length;
};
// Read a byte and return it.
this.readByte = function(address) {
if ((address < MEMORY.length) && (address >= 0)) {
if ((READ_EVENTS.length > 0) && (EVENTS_ENABLED)) {
for (var x=0; x<READ_EVENTS.length; x++) {
var event = READ_EVENTS[x];
if ((address >= event.startAddress) && (address <= event.endAddress))
event.callback(address, MEMORY[address] & 0xff, event.anyData);
}
}
return MEMORY[address] & 0xff;
}
};
// Remove registered events.
this.removeEvents = function() {
READ_EVENTS = new Array();
WRITE_EVENTS = new Array();
};
// Set all the memory to zero. Does NOT fire events.
this.reset = function() {
for (var x=0; x<MEMORY.length; x++) {
MEMORY[x] = 0;
}
};
// Write a byte.
this.writeByte = function(address, value) {
if ((address < MEMORY.length) && (address >= 0)) {
MEMORY[address] = value & 0xff;
if ((WRITE_EVENTS.length > 0) && (EVENTS_ENABLED)) {
for (var x=0; x<WRITE_EVENTS.length; x++) {
var event = WRITE_EVENTS[x];
if ((address >= event.startAddress) && (address <= event.endAddress))
event.callback(address, MEMORY[address] & 0xff, event.anyData);
}
}
}
};
}