/* * 6502 Based Virtual Computer * Copyright (C) 2011 Scott C. Duensing * * 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. */ // Our Clock object. function clock() { // --- Private variables. // Memory location for clock value. var MEMORY = null; var MEMORY_START = 0; var CLOCK_ADDRESS = 0; // Clock values. var MILLISECONDS = 0; var CLOCK = 0; var CLOCK_RUNNING = true; // --- Private methods. var clockTick = function() { if (CLOCK_RUNNING) { MEMORY.writeByte(CLOCK_ADDRESS, CLOCK++); if (CLOCK > 255) CLOCK = 0; } setTimeout(function(){ clockTick(); }, MILLISECONDS); }; // --- Public methods. // Attach clock to DOM. this.attach = function(newMemory, newStartPosition, newMilliseconds) { MEMORY = newMemory; MEMORY_START = newStartPosition; CLOCK_ADDRESS = MEMORY_START; MILLISECONDS = newMilliseconds; clockTick(); }; // Deserialize clock state. this.deserialize = function(state) { CLOCK = state.C; CLOCK_RUNNING = state.R; }; // Pause the clock. this.pause = function() { console.log("Pausing Clock."); CLOCK_RUNNING = false; }; // Start the clock. this.run = function() { console.log("Starting Clock."); CLOCK_RUNNING = true; }; // Serialize clock state. this.serialize = function() { var state = { C: CLOCK, R: CLOCK_RUNNING }; return state; }; }