v6502/vm/machine.js

101 lines
4.1 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.
*/
// Try it out!
// Declare components of our computer.
console.log("Creating components.");
var memory = new ram(64 * 1024);
var cpu = new cpu6502();
var monitor = new textDisplay();
var keys = new keyboard();
var random = new prng();
var timer = new clock();
console.log("Creating components - success!");
// Memory Map (64k):
var MM_ZERO_PAGE = 0x0000;
var MM_STACK = 0x0100;
var MM_CODE_START = 0x0200;
var MM_HEAP_END = 0xdfff;
var MM_DISPLAY_ADAPTER = 0xe000;
var MM_DISPLAY_DATA = MM_DISPLAY_ADAPTER + monitor.getMemoryNeeded() - 2;
var MM_DISPLAY_COMMAND = MM_DISPLAY_DATA + 1;
var MM_KEYBOARD_CHARACTER = MM_DISPLAY_COMMAND + 1;
var MM_KEYBOARD_META = MM_KEYBOARD_CHARACTER + 1;
var MM_PRNG = MM_KEYBOARD_META + 1;
var MM_CLOCK = MM_PRNG + 1;
var MM_ROM_START = 0xf000;
var MM_ROM_END = 0xfff9;
var MM_NMI_VECTOR = 0xfffa;
var MM_RESET_VECTOR = 0xfffc;
var MM_IRQ_VECTOR = 0xfffe;
console.log("MM_IRQ_VECTOR = 0x" + MM_IRQ_VECTOR.toString(16));
console.log("MM_RESET_VECTOR = 0x" + MM_RESET_VECTOR.toString(16));
console.log("MM_NMI_VECTOR = 0x" + MM_NMI_VECTOR.toString(16));
console.log("MM_ROM_END = 0x" + MM_ROM_END.toString(16));
console.log("MM_ROM_START = 0x" + MM_ROM_START.toString(16));
console.log("MM_CLOCK = 0x" + MM_CLOCK.toString(16));
console.log("MM_PRNG = 0x" + MM_PRNG.toString(16));
console.log("MM_KEYBOARD_META = 0x" + MM_KEYBOARD_META.toString(16));
console.log("MM_KEYBOARD_CHARACTER = 0x" + MM_KEYBOARD_CHARACTER.toString(16));
console.log("MM_DISPLAY_COMMAND = 0x" + MM_DISPLAY_COMMAND.toString(16));
console.log("MM_DISPLAY_DATA = 0x" + MM_DISPLAY_DATA.toString(16));
console.log("MM_DISPLAY_ADAPTER = 0x" + MM_DISPLAY_ADAPTER.toString(16));
console.log("MM_HEAP_END = 0x" + MM_HEAP_END.toString(16));
console.log("MM_CODE_START = 0x" + MM_CODE_START.toString(16));
console.log("MM_STACK = 0x" + MM_STACK.toString(16));
console.log("MM_ZERO_PAGE = 0x" + MM_ZERO_PAGE.toString(16));
// Configure components & connect them together.
cpu.attach(memory, MM_CODE_START, function(event){
// We don't care about events just yet.
var message = "Event " + event + " @ 0x" + cpu.getPC().toString(16) + " = " + memory.readByte(cpu.getPC()).toString(16);
console.log(message);
alert(message);
});
monitor.attach(memory, MM_DISPLAY_ADAPTER, "display");
keys.attach(memory, MM_KEYBOARD_CHARACTER);
random.attach(memory, MM_PRNG);
timer.attach(memory, MM_CLOCK, 0x100);
// Power on!
memory.reset();
cpu.reset();
// Make it do something!
var HELLO_WORLD = [0xA2, 0x00, 0xBD, 0x14, 0x02, 0xF0, 0x0B, 0x8D, 0xA0, 0xEF, 0xA9, 0x07,
0x8D, 0xA1, 0xEF, 0xE8, 0xD0, 0xF0, 0x00, 0x00, 0x48, 0x65, 0x6C,
0x6C, 0x6F, 0x2C, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00];
for (var x=0; x<HELLO_WORLD.length; x++) {
memory.writeByte(MM_CODE_START + x, HELLO_WORLD[x]);
}
console.log("Code loaded.");
/*
// Load EhBASIC into RAM at 0xD000.
for (var x=0; x<PROGRAM.length; x++) {
memory.writeByte(0xD000 + x, PROGRAM[x]);
}
console.log("Code loaded.");
*/
cpu.run();