Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add clock hardware.

  • Loading branch information...
commit e688b133784b6db1b42ee224508593e557a4ac8c 1 parent 4c17d4c
Robey Pointer authored
Showing with 68 additions and 0 deletions.
  1. +1 −0  dcpu.htm
  2. +67 −0 js/clock.js
View
1  dcpu.htm
@@ -6,6 +6,7 @@
<script type="text/javascript" src="js/assembler.js?v=4"></script>
<script type="text/javascript" src="js/disassembler.js?v=1"></script>
<script type="text/javascript" src="js/emulator.js?v=1"></script>
+ <script type="text/javascript" src="js/clock.js?v=1"></script>
<script type="text/javascript" src="js/screen.js?v=1"></script>
<script type="text/javascript" src="js/keyboard.js?v=1"></script>
<script type="text/javascript">
View
67 js/clock.js
@@ -0,0 +1,67 @@
+// Generic Clock (compatible)
+
+var Clock = {
+ type: 0x12d0b402,
+ revision: 1,
+ manufacturer: 0x904b3115,
+
+ SET_FREQUENCY: 0,
+ GET_TICKS: 1,
+ SET_INT: 2,
+
+ reset: function(queueInterrupt) {
+ if (queueInterrupt) {
+ this.queueInterrupt = queueInterrupt;
+ }
+ // heartbeat: # of msec between clock ticks
+ this.heartbeat = 0;
+ this.ticks = 0;
+ },
+
+ start: function() {
+ this.stop();
+ var self = this;
+ this.timer = setInterval(function() { self.tick(self); }, 2);
+ this.last = new Date().getTime();
+ },
+
+ stop: function() {
+ if (this.timer) {
+ clearInterval(this.timer);
+ this.timer = undefined;
+ }
+ },
+
+ tick: function(self) {
+ if (!self.heartbeat) return;
+ var now = new Date().getTime();
+ while (now - self.last >= self.heartbeat) {
+ self.ticks++;
+ if (self.message) {
+ self.queueInterrupt(function(memory, registers, state, hardware) {
+ registers.A = self.message;
+ });
+ }
+ self.last += self.heartbeat;
+ }
+ },
+
+ interrupt: function(memory, registers) {
+ switch (registers.A) {
+ case this.SET_FREQUENCY: {
+ this.reset();
+ this.heartbeat = 100 * registers.B / 6;
+ break;
+ }
+ case this.GET_TICKS: {
+ registers.C = this.ticks;
+ break;
+ }
+ case this.SET_INT: {
+ this.message = registers.B;
+ break;
+ }
+ }
+ return 0;
+ },
+};
Please sign in to comment.
Something went wrong with that request. Please try again.