pit.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. "use strict";
  2. /**
  3. * @const
  4. * In kHz
  5. */
  6. var OSCILLATOR_FREQ = 1193.1816666; // 1.193182 MHz
  7. /**
  8. * @constructor
  9. *
  10. * Programmable Interval Timer
  11. */
  12. function PIT(cpu)
  13. {
  14. /** @const @type {CPU} */
  15. this.cpu = cpu;
  16. this.counter_start_time = new Float64Array(3);
  17. this.counter_start_value = new Uint16Array(3);
  18. this.counter_next_low = new Uint8Array(4);
  19. this.counter_enabled = new Uint8Array(4);
  20. this.counter_mode = new Uint8Array(4);
  21. this.counter_read_mode = new Uint8Array(4);
  22. // 2 = latch low, 1 = latch high, 0 = no latch
  23. this.counter_latch = new Uint8Array(4);
  24. this.counter_latch_value = new Uint16Array(3);
  25. this.counter_reload = new Uint16Array(3);
  26. // TODO:
  27. // - counter2 can be controlled by an input
  28. cpu.io.register_read(0x61, this, function()
  29. {
  30. var now = v86.microtick();
  31. var ref_toggle = (now * (1000 * 1000 / 15000)) & 1;
  32. var counter2_out = this.did_rollover(2, now);
  33. return ref_toggle << 4 | counter2_out << 5;
  34. });
  35. cpu.io.register_read(0x40, this, function() { return this.counter_read(0); });
  36. cpu.io.register_read(0x41, this, function() { return this.counter_read(1); });
  37. cpu.io.register_read(0x42, this, function() { return this.counter_read(2); });
  38. cpu.io.register_write(0x40, this, function(data) { this.counter_write(0, data); });
  39. cpu.io.register_write(0x41, this, function(data) { this.counter_write(1, data); });
  40. cpu.io.register_write(0x42, this, function(data) { this.counter_write(2, data); });
  41. cpu.io.register_write(0x43, this, this.port43_write);
  42. }
  43. PIT.prototype.get_state = function()
  44. {
  45. var state = [];
  46. state[0] = this.counter_next_low;
  47. state[1] = this.counter_enabled;
  48. state[2] = this.counter_mode;
  49. state[3] = this.counter_read_mode;
  50. state[4] = this.counter_latch;
  51. state[5] = this.counter_latch_value;
  52. state[6] = this.counter_reload;
  53. state[7] = this.counter_start_time;
  54. state[8] = this.counter_start_value;
  55. return state;
  56. };
  57. PIT.prototype.set_state = function(state)
  58. {
  59. this.counter_next_low = state[0];
  60. this.counter_enabled = state[1];
  61. this.counter_mode = state[2];
  62. this.counter_read_mode = state[3];
  63. this.counter_latch = state[4];
  64. this.counter_latch_value = state[5];
  65. this.counter_reload = state[6];
  66. this.counter_start_time = state[7];
  67. this.counter_start_value = state[8];
  68. };
  69. PIT.prototype.timer = function(now, no_irq)
  70. {
  71. var time_to_next_interrupt = 100;
  72. // counter 0 produces interrupts
  73. if(!no_irq)
  74. {
  75. if(this.counter_enabled[0] && this.did_rollover(0, now))
  76. {
  77. time_to_next_interrupt = 0;
  78. this.counter_start_value[0] = this.get_counter_value(0, now);
  79. this.counter_start_time[0] = now;
  80. dbg_log("pit interrupt. new value: " + this.counter_start_value[0], LOG_PIT);
  81. // This isn't strictly correct, but it's necessary since browsers
  82. // may sleep longer than necessary to trigger the else branch below
  83. // and clear the irq
  84. this.cpu.device_lower_irq(0);
  85. this.cpu.device_raise_irq(0);
  86. var mode = this.counter_mode[0];
  87. if(mode === 0)
  88. {
  89. this.counter_enabled[0] = 0;
  90. }
  91. }
  92. else
  93. {
  94. this.cpu.device_lower_irq(0);
  95. }
  96. }
  97. time_to_next_interrupt = 0;
  98. return time_to_next_interrupt;
  99. };
  100. PIT.prototype.get_counter_value = function(i, now)
  101. {
  102. if(!this.counter_enabled[i])
  103. {
  104. return 0;
  105. }
  106. var diff = now - this.counter_start_time[i];
  107. var diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);
  108. var value = this.counter_start_value[i] - diff_in_ticks;
  109. dbg_log("diff=" + diff + " dticks=" + diff_in_ticks + " value=" + value + " reload=" + this.counter_reload[i], LOG_PIT);
  110. if(value < 0)
  111. {
  112. var reload = this.counter_reload[i];
  113. value = value % reload + reload;
  114. }
  115. return value;
  116. };
  117. PIT.prototype.did_rollover = function(i, now)
  118. {
  119. var diff = now - this.counter_start_time[i];
  120. if(diff < 0)
  121. {
  122. // should only happen after restore_state
  123. dbg_log("Warning: PIT timer difference is negative, resetting");
  124. return true;
  125. }
  126. var diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);
  127. //dbg_log(i + ": diff=" + diff + " start_time=" + this.counter_start_time[i] + " diff_in_ticks=" + diff_in_ticks + " (" + diff * OSCILLATOR_FREQ + ") start_value=" + this.counter_start_value[i] + " did_rollover=" + (this.counter_start_value[i] < diff_in_ticks), LOG_PIT);
  128. return this.counter_start_value[i] < diff_in_ticks;
  129. };
  130. PIT.prototype.counter_read = function(i)
  131. {
  132. var latch = this.counter_latch[i];
  133. if(latch)
  134. {
  135. this.counter_latch[i]--;
  136. if(latch === 2)
  137. {
  138. return this.counter_latch_value[i] & 0xFF;
  139. }
  140. else
  141. {
  142. return this.counter_latch_value[i] >> 8;
  143. }
  144. }
  145. else
  146. {
  147. var next_low = this.counter_next_low[i];
  148. if(this.counter_mode[i] === 3)
  149. {
  150. this.counter_next_low[i] ^= 1;
  151. }
  152. var value = this.get_counter_value(i, v86.microtick());
  153. if(next_low)
  154. {
  155. return value & 0xFF;
  156. }
  157. else
  158. {
  159. return value >> 8;
  160. }
  161. }
  162. };
  163. PIT.prototype.counter_write = function(i, value)
  164. {
  165. if(this.counter_next_low[i])
  166. {
  167. this.counter_reload[i] = this.counter_reload[i] & ~0xFF | value;
  168. }
  169. else
  170. {
  171. this.counter_reload[i] = this.counter_reload[i] & 0xFF | value << 8;
  172. }
  173. if(this.counter_read_mode[i] !== 3 || !this.counter_next_low[i])
  174. {
  175. if(!this.counter_reload[i])
  176. {
  177. this.counter_reload[i] = 0xFFFF;
  178. }
  179. // depends on the mode, should actually
  180. // happen on the first tick
  181. this.counter_start_value[i] = this.counter_reload[i];
  182. this.counter_enabled[i] = true;
  183. this.counter_start_time[i] = v86.microtick();
  184. dbg_log("counter" + i + " reload=" + h(this.counter_reload[i]) +
  185. " tick=" + (this.counter_reload[i] || 0x10000) / OSCILLATOR_FREQ + "ms", LOG_PIT);
  186. }
  187. if(this.counter_read_mode[i] === 3)
  188. {
  189. this.counter_next_low[i] ^= 1;
  190. }
  191. };
  192. PIT.prototype.port43_write = function(reg_byte)
  193. {
  194. var mode = reg_byte >> 1 & 7,
  195. binary_mode = reg_byte & 1,
  196. i = reg_byte >> 6 & 3,
  197. read_mode = reg_byte >> 4 & 3;
  198. if(i === 1)
  199. {
  200. dbg_log("Unimplemented timer1", LOG_PIT);
  201. }
  202. if(i === 3)
  203. {
  204. dbg_log("Unimplemented read back", LOG_PIT);
  205. return;
  206. }
  207. if(read_mode === 0)
  208. {
  209. // latch
  210. this.counter_latch[i] = 2;
  211. var value = this.get_counter_value(i, v86.microtick());
  212. dbg_log("latch: " + value, LOG_PIT);
  213. this.counter_latch_value[i] = value ? value - 1 : 0;
  214. return;
  215. }
  216. if(mode >= 6)
  217. {
  218. // 6 and 7 are aliased to 2 and 3
  219. mode &= ~4;
  220. }
  221. dbg_log("Control: mode=" + mode + " ctr=" + i +
  222. " read_mode=" + read_mode + " bcd=" + binary_mode, LOG_PIT);
  223. if(read_mode === 1)
  224. {
  225. // msb
  226. this.counter_next_low[i] = 0;
  227. }
  228. else if(read_mode === 2)
  229. {
  230. // lsb
  231. this.counter_next_low[i] = 1;
  232. }
  233. else
  234. {
  235. // first lsb then msb
  236. this.counter_next_low[i] = 1;
  237. }
  238. if(i === 0)
  239. {
  240. this.cpu.device_lower_irq(0);
  241. }
  242. if(mode === 0)
  243. {
  244. }
  245. else if(mode === 3 || mode === 2)
  246. {
  247. // what is the difference
  248. }
  249. else
  250. {
  251. dbg_log("Unimplemented counter mode: " + h(mode), LOG_PIT);
  252. }
  253. this.counter_mode[i] = mode;
  254. this.counter_read_mode[i] = read_mode;
  255. };
  256. PIT.prototype.dump = function()
  257. {
  258. const reload = this.counter_reload[0];
  259. const time = (reload || 0x10000) / OSCILLATOR_FREQ;
  260. dbg_log("counter0 ticks every " + time + "ms (reload=" + reload + ")");
  261. };