v6502/vm/utils.js

62 lines
2 KiB
JavaScript

/*
* 6502 Based Virtual Computer
* Copyright © 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.
*/
// Pretend we're a singleton.
var util = new utils();
function utils() {
GLOBAL_DATA = {};
// --- Public methods.
this.hexToRgb = function(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
// alert( hexToRgb("#0033ff").g ); // "51";
};
this.loadPasteBox = function(controlId) {
var t = document.getElementById(controlId).value;
GLOBAL_DATA.pasteData = t;
GLOBAL_DATA.pastePos = 0;
GLOBAL_DATA.pasteTimer = window.setInterval(function() {
if (computer.memory.readByte(MM_KEYBOARD_CHARACTER) === 0) {
var char = GLOBAL_DATA.pasteData.charCodeAt(GLOBAL_DATA.pastePos++);
if (char == 10) char = 13;
computer.memory.writeByte(MM_KEYBOARD_CHARACTER, char);
}
if (GLOBAL_DATA.pastePos == GLOBAL_DATA.pasteData.length) {
window.clearInterval(GLOBAL_DATA.pasteTimer);
}
}, 3);
};
}