pit.js 8.8 KB

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