pit.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.cpu.device_raise_irq(0);
  82. var mode = this.counter_mode[0];
  83. if(mode === 0)
  84. {
  85. this.counter_enabled[0] = 0;
  86. }
  87. }
  88. else
  89. {
  90. this.cpu.device_lower_irq(0);
  91. }
  92. }
  93. time_to_next_interrupt = 0;
  94. return time_to_next_interrupt;
  95. };
  96. PIT.prototype.get_counter_value = function(i, now)
  97. {
  98. if(!this.counter_enabled[i])
  99. {
  100. return 0;
  101. }
  102. var diff = now - this.counter_start_time[i];
  103. var diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);
  104. var value = this.counter_start_value[i] - diff_in_ticks;
  105. dbg_log("diff=" + diff + " dticks=" + diff_in_ticks + " value=" + value + " reload=" + this.counter_reload[i], LOG_PIT);
  106. if(value < 0)
  107. {
  108. var reload = this.counter_reload[i];
  109. value = value % reload + reload;
  110. }
  111. return value;
  112. };
  113. PIT.prototype.did_rollover = function(i, now)
  114. {
  115. var diff = now - this.counter_start_time[i];
  116. if(diff < 0)
  117. {
  118. // should only happen after restore_state
  119. dbg_log("Warning: PIT timer difference is negative, resetting");
  120. return true;
  121. }
  122. var diff_in_ticks = Math.floor(diff * OSCILLATOR_FREQ);
  123. //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);
  124. return this.counter_start_value[i] < diff_in_ticks;
  125. };
  126. PIT.prototype.counter_read = function(i)
  127. {
  128. var latch = this.counter_latch[i];
  129. if(latch)
  130. {
  131. this.counter_latch[i]--;
  132. if(latch === 2)
  133. {
  134. return this.counter_latch_value[i] & 0xFF;
  135. }
  136. else
  137. {
  138. return this.counter_latch_value[i] >> 8;
  139. }
  140. }
  141. else
  142. {
  143. var next_low = this.counter_next_low[i];
  144. if(this.counter_mode[i] === 3)
  145. {
  146. this.counter_next_low[i] ^= 1;
  147. }
  148. var value = this.get_counter_value(i, v86.microtick());
  149. if(next_low)
  150. {
  151. return value & 0xFF;
  152. }
  153. else
  154. {
  155. return value >> 8;
  156. }
  157. }
  158. };
  159. PIT.prototype.counter_write = function(i, value)
  160. {
  161. if(this.counter_next_low[i])
  162. {
  163. this.counter_reload[i] = this.counter_reload[i] & ~0xFF | value;
  164. }
  165. else
  166. {
  167. this.counter_reload[i] = this.counter_reload[i] & 0xFF | value << 8;
  168. }
  169. if(this.counter_read_mode[i] !== 3 || !this.counter_next_low[i])
  170. {
  171. if(!this.counter_reload[i])
  172. {
  173. this.counter_reload[i] = 0xFFFF;
  174. }
  175. // depends on the mode, should actually
  176. // happen on the first tick
  177. this.counter_start_value[i] = this.counter_reload[i];
  178. this.counter_enabled[i] = true;
  179. this.counter_start_time[i] = v86.microtick();
  180. dbg_log("counter" + i + " reload=" + h(this.counter_reload[i]) +
  181. " tick=" + (this.counter_reload[i] || 0x10000) / OSCILLATOR_FREQ + "ms", LOG_PIT);
  182. }
  183. if(this.counter_read_mode[i] === 3)
  184. {
  185. this.counter_next_low[i] ^= 1;
  186. }
  187. };
  188. PIT.prototype.port43_write = function(reg_byte)
  189. {
  190. var mode = reg_byte >> 1 & 7,
  191. binary_mode = reg_byte & 1,
  192. i = reg_byte >> 6 & 3,
  193. read_mode = reg_byte >> 4 & 3;
  194. if(i === 1)
  195. {
  196. dbg_log("Unimplemented timer1", LOG_PIT);
  197. }
  198. if(i === 3)
  199. {
  200. dbg_log("Unimplemented read back", LOG_PIT);
  201. return;
  202. }
  203. if(read_mode === 0)
  204. {
  205. // latch
  206. this.counter_latch[i] = 2;
  207. var value = this.get_counter_value(i, v86.microtick());
  208. dbg_log("latch: " + value, LOG_PIT);
  209. this.counter_latch_value[i] = value ? value - 1 : 0;
  210. return;
  211. }
  212. if(mode >= 6)
  213. {
  214. // 6 and 7 are aliased to 2 and 3
  215. mode &= ~4;
  216. }
  217. dbg_log("Control: mode=" + mode + " ctr=" + i +
  218. " read_mode=" + read_mode + " bcd=" + binary_mode, LOG_PIT);
  219. if(read_mode === 1)
  220. {
  221. // msb
  222. this.counter_next_low[i] = 0;
  223. }
  224. else if(read_mode === 2)
  225. {
  226. // lsb
  227. this.counter_next_low[i] = 1;
  228. }
  229. else
  230. {
  231. // first lsb then msb
  232. this.counter_next_low[i] = 1;
  233. }
  234. if(i === 0)
  235. {
  236. this.cpu.device_lower_irq(0);
  237. }
  238. if(mode === 0)
  239. {
  240. }
  241. else if(mode === 3 || mode === 2)
  242. {
  243. // what is the difference
  244. }
  245. else
  246. {
  247. dbg_log("Unimplemented counter mode: " + h(mode), LOG_PIT);
  248. }
  249. this.counter_mode[i] = mode;
  250. this.counter_read_mode[i] = read_mode;
  251. };