65 lines
2 KiB
JavaScript
65 lines
2 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.
|
|
*/
|
|
|
|
// Our Psuedo-Random Number Generator object.
|
|
function prng() {
|
|
|
|
// --- Private variables.
|
|
|
|
var MAX = Math.pow(2, 32);
|
|
var SEED = Math.round(Math.random() * MAX);
|
|
|
|
// Memory location for PRNG value.
|
|
var MEMORY = null;
|
|
var MEMORY_START = 0;
|
|
var PRNG_ADDRESS = 0;
|
|
|
|
|
|
// --- Public methods.
|
|
|
|
// Attach PRNG to DOM.
|
|
this.attach = function(newMemory, newStartPosition) {
|
|
// We create our own PRN to ensure recreatability between sessions and browsers.
|
|
// http://weblog.bocoup.com/random-numbers/ https://gist.github.com/Protonk/5367430
|
|
MEMORY = newMemory;
|
|
MEMORY_START = newStartPosition;
|
|
PRNG_ADDRESS = MEMORY_START;
|
|
LAST_RANDOM = Math.random();
|
|
MEMORY.addReadCallback(PRNG_ADDRESS, PRNG_ADDRESS, null, function(address, value, data){
|
|
// On read, change the random value. Based on WebKit2's invertible mapping generator.
|
|
SEED += (SEED * SEED) | 5;
|
|
var value = (SEED >>> 32) / MAX;
|
|
MEMORY.writeByte(address, Math.floor(value * 256));
|
|
});
|
|
};
|
|
|
|
// Deserialize PRNG state.
|
|
this.deserialize = function(state) {
|
|
SEED = state.S;
|
|
};
|
|
|
|
// Serialize PRNG state.
|
|
this.serialize = function() {
|
|
var state = {
|
|
S: SEED
|
|
};
|
|
return state;
|
|
};
|
|
|
|
}
|