Browse Source

Clock modified to no longer be a singleton.

master
Bryan Miller 5 years ago
parent
commit
93c57a9768
1 changed files with 18 additions and 20 deletions
  1. +18
    -20
      chip/clock.js

+ 18
- 20
chip/clock.js View File

@@ -1,35 +1,33 @@

var TickTime = 1000;
var DT = 0;
var LTime = 0;
var CPUCLK = null:

class Clock{
constructor(){}
constructor(){
this.__ticktime = 1000;
this.__dt = 0;
this.__ltime = 0;
this.__out = [];
}

get hz(){return Math.floor(1000 / TickTime);}
get hz(){return Math.floor(1000 / this.__ticktime);}
set hz(hz){
if(hz >= 1)
TickTime = 1000 / hz;
this.__ticktime = 1000 / hz;
}

get wired(){return (CPUCLK !== null);}
set wire(clk){
if(clk == null || typeof clk === 'function')
CPUCLK = clk;
clk(fn){
if (typeof(fn) === 'function')
this.__out.push(fn);
}

tick(){
let ctime = Date.getTime();
if(LTime !== 0){
DT += ctime - LTime;
while(DT > TickTime){
if (CPUCLK)
CPUCLK();
DT -= TickTime
let ctime = (new Date()).getTime();
if(this.__ltime !== 0){
this.__dt += ctime - this.__ltime;
while(this.__dt > this.__ticktime){
this.__out.forEach((fn)=>{fn();});
this.__dt -= this.__ticktime
}
}
LTime = ctime;
this.__ltime = ctime;
}
}


Loading…
Cancel
Save