1 /**
2  * DCPU-16 hardware timer clock
3  *
4  * See_Also:
5  *  http://dcpu.com/highnerd/rc_1/clock.txt
6  */
7 module dcpu.clock;
8 
9 /+
10 import dcpu.hardware;
11 
12 class TimerClock: Hardware {
13 
14 protected:
15   ushort ticks;       /// How many clock ticks sinze the last call
16   ushort interval;    /// Clock divider
17   ushort int_msg;     /// Interrupt mesg to send to the CPU
18   long count;         /// Count clock ticks
19   
20 public:
21 
22   static this() {
23     id     = 0x12d0b402;
24     ver    = 1;
25     vendor = 0; // Not yet
26   }
27 
28   /**
29    * What to do when it's loaded in the dcpu machine
30    */
31   override void init() {
32     super.init();
33     int_msg = 0;
34     interval = 0;
35     ticks = 0;
36     count = 0;
37   }
38 
39   /**
40    * What to do when a Hardware interrupt to this hardware, has receive
41    * Params:
42    *  state   = CPU editable actual state
43    *  ram     = RAM of the machine
44    */
45   override void interrupt(ref CpuInfo state, ref ushort[0x10000] ram) {
46     super.interrupt(state, ram);
47     switch (state.a) {
48       case 0:
49         interval = state.b;
50         break;
51       case 1:
52         state.c = ticks;
53         break;
54       case 2:
55         int_msg = state.b;
56         break;
57       default:
58         // Do nothing
59     }
60     ticks = 0;
61   }
62 
63   /**
64    * What to do each clock tick (at 60 hz)
65    * Params:
66    *  state   = CPU editable actual state
67    *  cpu     = CPU
68    *  ram     = RAM of the machine
69    */
70   override void tick_60hz (ref CpuInfo state, ref DCpu cpu, ref ushort[0x10000] ram) {
71     if (f_hwi && interval != 0) {      
72       if (++count >= interval) {
73         debug {
74           import std.stdio;
75           stderr.writeln("\t60hz tick: ", count, " to: ",interval, " ticks: ", ticks);
76         }
77         ticks++;
78         count = 0;
79         if (int_msg > 0) { // Send Interrupt to DCPU
80           cpu.hardware_int(int_msg);
81         }
82         
83       }
84     }
85   }
86   
87 }
88 +/
89