uart.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. "use strict";
  2. /*
  3. * Serial ports
  4. * http://wiki.osdev.org/UART
  5. * https://github.com/s-macke/jor1k/blob/master/js/worker/dev/uart.js
  6. * https://www.freebsd.org/doc/en/articles/serial-uart/
  7. */
  8. /** @const */
  9. var DLAB = 0x80;
  10. /** @const */ var UART_IER_MSI = 0x08; /* Modem Status Changed int. */
  11. /** @const */ var UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */
  12. /** @const */ var UART_IER_RDI = 0x01; /* Enable receiver data interrupt */
  13. /** @const */var UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority) */
  14. /** @const */var UART_IIR_NO_INT = 0x01;
  15. /** @const */var UART_IIR_THRI = 0x02; /* Transmitter holding register empty */
  16. /** @const */var UART_IIR_RDI = 0x04; /* Receiver data interrupt */
  17. /** @const */var UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.) */
  18. /** @const */var UART_IIR_CTI = 0x0c; /* Character timeout */
  19. /** @const */ var UART_LSR_DATA_READY = 0x1; // data available
  20. /** @const */ var UART_LSR_TX_EMPTY = 0x20; // TX (THR) buffer is empty
  21. /** @const */ var UART_LSR_TRANSMITTER_EMPTY = 0x40; // TX empty and line is idle
  22. // Modem status register
  23. /** @const */ var UART_MSR_DCD = 0x7; // Data Carrier Detect
  24. /** @const */ var UART_MSR_RI = 0x6; // Ring Indicator
  25. /** @const */ var UART_MSR_DSR = 0x5; // Data Set Ready
  26. /** @const */ var UART_MSR_CTS = 0x4; // Clear To Send
  27. // Delta bits
  28. /** @const */ var UART_MSR_DDCD = 0x3; // Delta DCD
  29. /** @const */ var UART_MSR_TERI = 0x2; // Trailing Edge RI
  30. /** @const */ var UART_MSR_DDSR = 0x1; // Delta DSR
  31. /** @const */ var UART_MSR_DCTS = 0x0; // Delta CTS
  32. /**
  33. * @constructor
  34. * @param {CPU} cpu
  35. * @param {number} port
  36. * @param {BusConnector} bus
  37. */
  38. function UART(cpu, port, bus)
  39. {
  40. /** @const @type {BusConnector} */
  41. this.bus = bus;
  42. /** @const @type {CPU} */
  43. this.cpu = cpu;
  44. this.ints = 1 << UART_IIR_THRI;
  45. this.baud_rate = 0;
  46. this.line_control = 0;
  47. // line status register
  48. this.lsr = UART_LSR_TRANSMITTER_EMPTY | UART_LSR_TX_EMPTY;
  49. this.fifo_control = 0;
  50. // interrupts enable
  51. this.ier = 0;
  52. // interrupt identification register
  53. this.iir = UART_IIR_NO_INT;
  54. this.modem_control = 0;
  55. this.modem_status = 0;
  56. this.scratch_register = 0;
  57. this.irq = 0;
  58. this.input = [];
  59. this.current_line = "";
  60. switch(port)
  61. {
  62. case 0x3F8:
  63. this.com = 0;
  64. this.irq = 4;
  65. break;
  66. case 0x2F8:
  67. this.com = 1;
  68. this.irq = 3;
  69. break;
  70. case 0x3E8:
  71. this.com = 2;
  72. this.irq = 4;
  73. break;
  74. case 0x2E8:
  75. this.com = 3;
  76. this.irq = 3;
  77. break;
  78. default:
  79. dbg_log("Invalid serial port: " + h(port), LOG_SERIAL);
  80. this.com = 0;
  81. this.irq = 4;
  82. }
  83. this.bus.register("serial" + this.com + "-input", function(data)
  84. {
  85. this.data_received(data);
  86. }, this);
  87. this.bus.register("serial" + this.com + "-modem-status-input", function(data)
  88. {
  89. this.set_modem_status(data);
  90. }, this);
  91. // Set individual modem status bits
  92. this.bus.register("serial" + this.com + "-carrier-detect-input", function(data)
  93. {
  94. const status = data ?
  95. this.modem_status | (1 << UART_MSR_DCD) | (1 << UART_MSR_DDCD) :
  96. this.modem_status & ~(1 << UART_MSR_DCD) & ~(1 << UART_MSR_DDCD);
  97. this.set_modem_status(status);
  98. }, this);
  99. this.bus.register("serial" + this.com + "-ring-indicator-input", function(data)
  100. {
  101. const status = data ?
  102. this.modem_status | (1 << UART_MSR_RI) | (1 << UART_MSR_TERI) :
  103. this.modem_status & ~(1 << UART_MSR_RI) & ~(1 << UART_MSR_TERI);
  104. this.set_modem_status(status);
  105. }, this);
  106. this.bus.register("serial" + this.com + "-data-set-ready-input", function(data)
  107. {
  108. const status = data ?
  109. this.modem_status | (1 << UART_MSR_DSR) | (1 << UART_MSR_DDSR) :
  110. this.modem_status & ~(1 << UART_MSR_DSR) & ~(1 << UART_MSR_DDSR);
  111. this.set_modem_status(status);
  112. }, this);
  113. this.bus.register("serial" + this.com + "-clear-to-send-input", function(data)
  114. {
  115. const status = data ?
  116. this.modem_status | (1 << UART_MSR_CTS) | (1 << UART_MSR_DCTS) :
  117. this.modem_status & ~(1 << UART_MSR_CTS) & ~(1 << UART_MSR_DCTS);
  118. this.set_modem_status(status);
  119. }, this);
  120. var io = cpu.io;
  121. io.register_write(port, this, function(out_byte)
  122. {
  123. this.write_data(out_byte);
  124. }, function(out_word)
  125. {
  126. this.write_data(out_word & 0xFF);
  127. this.write_data(out_word >> 8);
  128. });
  129. io.register_write(port | 1, this, function(out_byte)
  130. {
  131. if(this.line_control & DLAB)
  132. {
  133. this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;
  134. dbg_log("baud rate: " + h(this.baud_rate), LOG_SERIAL);
  135. }
  136. else
  137. {
  138. if((this.ier & UART_IIR_THRI) === 0 && (out_byte & UART_IIR_THRI))
  139. {
  140. // re-throw THRI if it was masked
  141. this.ThrowInterrupt(UART_IIR_THRI);
  142. }
  143. this.ier = out_byte & 0xF;
  144. dbg_log("interrupt enable: " + h(out_byte), LOG_SERIAL);
  145. this.CheckInterrupt();
  146. }
  147. });
  148. io.register_read(port, this, function()
  149. {
  150. if(this.line_control & DLAB)
  151. {
  152. return this.baud_rate & 0xFF;
  153. }
  154. else
  155. {
  156. let data = 0;
  157. if(this.input.length === 0)
  158. {
  159. dbg_log("Read input empty", LOG_SERIAL);
  160. }
  161. else
  162. {
  163. data = this.input.shift();
  164. dbg_log("Read input: " + h(data), LOG_SERIAL);
  165. }
  166. if(this.input.length === 0)
  167. {
  168. this.lsr &= ~UART_LSR_DATA_READY;
  169. this.ClearInterrupt(UART_IIR_CTI);
  170. this.ClearInterrupt(UART_IIR_RDI);
  171. }
  172. return data;
  173. }
  174. });
  175. io.register_read(port | 1, this, function()
  176. {
  177. if(this.line_control & DLAB)
  178. {
  179. return this.baud_rate >> 8;
  180. }
  181. else
  182. {
  183. return this.ier & 0xF;
  184. }
  185. });
  186. io.register_read(port | 2, this, function()
  187. {
  188. var ret = this.iir & 0xF;
  189. dbg_log("read interrupt identification: " + h(this.iir), LOG_SERIAL);
  190. if (this.iir == UART_IIR_THRI) {
  191. this.ClearInterrupt(UART_IIR_THRI);
  192. }
  193. if(this.fifo_control & 1) ret |= 0xC0;
  194. return ret;
  195. });
  196. io.register_write(port | 2, this, function(out_byte)
  197. {
  198. dbg_log("fifo control: " + h(out_byte), LOG_SERIAL);
  199. this.fifo_control = out_byte;
  200. });
  201. io.register_read(port | 3, this, function()
  202. {
  203. dbg_log("read line control: " + h(this.line_control), LOG_SERIAL);
  204. return this.line_control;
  205. });
  206. io.register_write(port | 3, this, function(out_byte)
  207. {
  208. dbg_log("line control: " + h(out_byte), LOG_SERIAL);
  209. this.line_control = out_byte;
  210. });
  211. io.register_read(port | 4, this, function()
  212. {
  213. return this.modem_control;
  214. });
  215. io.register_write(port | 4, this, function(out_byte)
  216. {
  217. dbg_log("modem control: " + h(out_byte), LOG_SERIAL);
  218. this.modem_control = out_byte;
  219. });
  220. io.register_read(port | 5, this, function()
  221. {
  222. dbg_log("read line status: " + h(this.lsr), LOG_SERIAL);
  223. return this.lsr;
  224. });
  225. io.register_write(port | 5, this, function(out_byte)
  226. {
  227. dbg_log("Factory test write", LOG_SERIAL);
  228. });
  229. io.register_read(port | 6, this, function()
  230. {
  231. dbg_log("read modem status: " + h(this.modem_status), LOG_SERIAL);
  232. // Clear delta bits
  233. this.modem_status &= 0xF0;
  234. return this.modem_status;
  235. });
  236. io.register_write(port | 6, this, function(out_byte)
  237. {
  238. dbg_log("write modem status: " + h(out_byte), LOG_SERIAL);
  239. this.set_modem_status(out_byte);
  240. });
  241. io.register_read(port | 7, this, function()
  242. {
  243. return this.scratch_register;
  244. });
  245. io.register_write(port | 7, this, function(out_byte)
  246. {
  247. this.scratch_register = out_byte;
  248. });
  249. }
  250. UART.prototype.get_state = function()
  251. {
  252. var state = [];
  253. state[0] = this.ints;
  254. state[1] = this.baud_rate;
  255. state[2] = this.line_control;
  256. state[3] = this.lsr;
  257. state[4] = this.fifo_control;
  258. state[5] = this.ier;
  259. state[6] = this.iir;
  260. state[7] = this.modem_control;
  261. state[8] = this.modem_status;
  262. state[9] = this.scratch_register;
  263. state[10] = this.irq;
  264. return state;
  265. };
  266. UART.prototype.set_state = function(state)
  267. {
  268. this.ints = state[0];
  269. this.baud_rate = state[1];
  270. this.line_control = state[2];
  271. this.lsr = state[3];
  272. this.fifo_control = state[4];
  273. this.ier = state[5];
  274. this.iir = state[6];
  275. this.modem_control = state[7];
  276. this.modem_status = state[8];
  277. this.scratch_register = state[9];
  278. this.irq = state[10];
  279. };
  280. UART.prototype.CheckInterrupt = function() {
  281. if ((this.ints & (1 << UART_IIR_CTI)) && (this.ier & UART_IER_RDI)) {
  282. this.iir = UART_IIR_CTI;
  283. this.cpu.device_raise_irq(this.irq);
  284. } else
  285. if ((this.ints & (1 << UART_IIR_RDI)) && (this.ier & UART_IER_RDI)) {
  286. this.iir = UART_IIR_RDI;
  287. this.cpu.device_raise_irq(this.irq);
  288. } else
  289. if ((this.ints & (1 << UART_IIR_THRI)) && (this.ier & UART_IER_THRI)) {
  290. this.iir = UART_IIR_THRI;
  291. this.cpu.device_raise_irq(this.irq);
  292. } else
  293. if ((this.ints & (1 << UART_IIR_MSI)) && (this.ier & UART_IER_MSI)) {
  294. this.iir = UART_IIR_MSI;
  295. this.cpu.device_raise_irq(this.irq);
  296. } else {
  297. this.iir = UART_IIR_NO_INT;
  298. this.cpu.device_lower_irq(this.irq);
  299. }
  300. };
  301. UART.prototype.ThrowInterrupt = function(line) {
  302. this.ints |= (1 << line);
  303. this.CheckInterrupt();
  304. };
  305. UART.prototype.ClearInterrupt = function(line) {
  306. this.ints &= ~(1 << line);
  307. this.CheckInterrupt();
  308. };
  309. /**
  310. * @param {number} data
  311. */
  312. UART.prototype.data_received = function(data)
  313. {
  314. dbg_log("input: " + h(data), LOG_SERIAL);
  315. this.input.push(data);
  316. this.lsr |= UART_LSR_DATA_READY;
  317. if(this.fifo_control & 1)
  318. {
  319. this.ThrowInterrupt(UART_IIR_CTI);
  320. }
  321. else
  322. {
  323. this.ThrowInterrupt(UART_IIR_RDI);
  324. }
  325. };
  326. UART.prototype.write_data = function(out_byte)
  327. {
  328. if(this.line_control & DLAB)
  329. {
  330. this.baud_rate = this.baud_rate & ~0xFF | out_byte;
  331. return;
  332. }
  333. dbg_log("data: " + h(out_byte), LOG_SERIAL);
  334. this.ThrowInterrupt(UART_IIR_THRI);
  335. this.bus.send("serial" + this.com + "-output-byte", out_byte);
  336. if(DEBUG)
  337. {
  338. var char = String.fromCharCode(out_byte);
  339. this.current_line += char;
  340. if(char === "\n")
  341. {
  342. const line = this.current_line.trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g, "");
  343. dbg_log("SERIAL: " + line);
  344. this.current_line = "";
  345. }
  346. }
  347. };
  348. UART.prototype.set_modem_status = function(status)
  349. {
  350. dbg_log("modem status: " + h(status), LOG_SERIAL);
  351. const prev_delta_bits = this.modem_status & 0x0F;
  352. // compare the bits that have changed and shift them into the delta bits
  353. let delta = (this.modem_status ^ status) >> 4;
  354. // The delta should stay set if they were previously set
  355. delta |= prev_delta_bits;
  356. // update the current modem status
  357. this.modem_status = status;
  358. // update the delta bits based on the changes and previous
  359. // values, but also leave the delta bits set if they were
  360. // passed in as part of the status
  361. this.modem_status |= delta;
  362. };