pit.js 8.8 KB

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