uart.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /**
  23. * @constructor
  24. * @param {CPU} cpu
  25. * @param {number} port
  26. * @param {BusConnector} bus
  27. */
  28. function UART(cpu, port, bus)
  29. {
  30. /** @const @type {BusConnector} */
  31. this.bus = bus;
  32. /** @const @type {CPU} */
  33. this.cpu = cpu;
  34. this.ints = 1 << UART_IIR_THRI;
  35. this.baud_rate = 0;
  36. this.line_control = 0;
  37. // line status register
  38. this.lsr = UART_LSR_TRANSMITTER_EMPTY | UART_LSR_TX_EMPTY;
  39. this.fifo_control = 0;
  40. // interrupts enable
  41. this.ier = 0;
  42. // interrupt identification register
  43. this.iir = UART_IIR_NO_INT;
  44. this.modem_control = 0;
  45. this.modem_status = 0;
  46. this.scratch_register = 0;
  47. this.irq = 0;
  48. this.input = new ByteQueue(4096);
  49. this.current_line = [];
  50. switch(port)
  51. {
  52. case 0x3F8:
  53. this.com = 0;
  54. this.irq = 4;
  55. break;
  56. case 0x2F8:
  57. this.com = 1;
  58. this.irq = 3;
  59. break;
  60. case 0x3E8:
  61. this.com = 2;
  62. this.irq = 4;
  63. break;
  64. case 0x2E8:
  65. this.com = 3;
  66. this.irq = 3;
  67. break;
  68. default:
  69. dbg_log("Invalid serial port: " + h(port), LOG_SERIAL);
  70. this.com = 0;
  71. this.irq = 4;
  72. }
  73. this.bus.register("serial" + this.com + "-input", function(data)
  74. {
  75. this.data_received(data);
  76. }, this);
  77. var io = cpu.io;
  78. io.register_write(port, this, function(out_byte)
  79. {
  80. this.write_data(out_byte);
  81. }, function(out_word)
  82. {
  83. this.write_data(out_word & 0xFF);
  84. this.write_data(out_word >> 8);
  85. });
  86. io.register_write(port | 1, this, function(out_byte)
  87. {
  88. if(this.line_control & DLAB)
  89. {
  90. this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;
  91. dbg_log("baud rate: " + h(this.baud_rate), LOG_SERIAL);
  92. }
  93. else
  94. {
  95. this.ier = out_byte & 0xF;
  96. dbg_log("interrupt enable: " + h(out_byte), LOG_SERIAL);
  97. this.CheckInterrupt();
  98. }
  99. });
  100. io.register_read(port, this, function()
  101. {
  102. if(this.line_control & DLAB)
  103. {
  104. return this.baud_rate & 0xFF;
  105. }
  106. else
  107. {
  108. var data = this.input.shift();
  109. if(data === -1)
  110. {
  111. dbg_log("Read input empty", LOG_SERIAL);
  112. }
  113. else
  114. {
  115. dbg_log("Read input: " + h(data), LOG_SERIAL);
  116. }
  117. if(this.input.length === 0)
  118. {
  119. this.lsr &= ~UART_LSR_DATA_READY;
  120. this.ClearInterrupt(UART_IIR_CTI);
  121. }
  122. return data;
  123. }
  124. });
  125. io.register_read(port | 1, this, function()
  126. {
  127. if(this.line_control & DLAB)
  128. {
  129. return this.baud_rate >> 8;
  130. }
  131. else
  132. {
  133. return this.ier & 0xF;
  134. }
  135. });
  136. io.register_read(port | 2, this, function()
  137. {
  138. var ret = this.iir & 0xF | 0xC0;
  139. dbg_log("read interrupt identification: " + h(this.iir), LOG_SERIAL);
  140. if (this.iir == UART_IIR_THRI) {
  141. this.ClearInterrupt(UART_IIR_THRI);
  142. }
  143. return ret;
  144. });
  145. io.register_write(port | 2, this, function(out_byte)
  146. {
  147. dbg_log("fifo control: " + h(out_byte), LOG_SERIAL);
  148. this.fifo_control = out_byte;
  149. });
  150. io.register_read(port | 3, this, function()
  151. {
  152. dbg_log("read line control: " + h(this.line_control), LOG_SERIAL);
  153. return this.line_control;
  154. });
  155. io.register_write(port | 3, this, function(out_byte)
  156. {
  157. dbg_log("line control: " + h(out_byte), LOG_SERIAL);
  158. this.line_control = out_byte;
  159. });
  160. io.register_read(port | 4, this, function()
  161. {
  162. return this.modem_control;
  163. });
  164. io.register_write(port | 4, this, function(out_byte)
  165. {
  166. dbg_log("modem control: " + h(out_byte), LOG_SERIAL);
  167. this.modem_control = out_byte;
  168. });
  169. io.register_read(port | 5, this, function()
  170. {
  171. dbg_log("read line status: " + h(this.lsr), LOG_SERIAL);
  172. return this.lsr;
  173. });
  174. io.register_write(port | 5, this, function(out_byte)
  175. {
  176. dbg_log("Factory test write", LOG_SERIAL);
  177. });
  178. io.register_read(port | 6, this, function()
  179. {
  180. dbg_log("read modem status: " + h(this.modem_status), LOG_SERIAL);
  181. return this.modem_status;
  182. });
  183. io.register_write(port | 6, this, function(out_byte)
  184. {
  185. dbg_log("Unkown register write (base+6)", LOG_SERIAL);
  186. });
  187. io.register_read(port | 7, this, function()
  188. {
  189. return this.scratch_register;
  190. });
  191. io.register_write(port | 7, this, function(out_byte)
  192. {
  193. this.scratch_register = out_byte;
  194. });
  195. }
  196. UART.prototype.get_state = function()
  197. {
  198. var state = [];
  199. state[0] = this.ints;
  200. state[1] = this.baud_rate;
  201. state[2] = this.line_control;
  202. state[3] = this.lsr;
  203. state[4] = this.fifo_control;
  204. state[5] = this.ier;
  205. state[6] = this.iir;
  206. state[7] = this.modem_control;
  207. state[8] = this.modem_status;
  208. state[9] = this.scratch_register;
  209. state[10] = this.irq;
  210. return state;
  211. };
  212. UART.prototype.set_state = function(state)
  213. {
  214. this.ints = state[0];
  215. this.baud_rate = state[1];
  216. this.line_control = state[2];
  217. this.lsr = state[3];
  218. this.fifo_control = state[4];
  219. this.ier = state[5];
  220. this.iir = state[6];
  221. this.modem_control = state[7];
  222. this.modem_status = state[8];
  223. this.scratch_register = state[9];
  224. this.irq = state[10];
  225. };
  226. UART.prototype.CheckInterrupt = function() {
  227. if ((this.ints & (1 << UART_IIR_CTI)) && (this.ier & UART_IER_RDI)) {
  228. this.iir = UART_IIR_CTI;
  229. this.cpu.device_raise_irq(this.irq);
  230. } else
  231. if ((this.ints & (1 << UART_IIR_THRI)) && (this.ier & UART_IER_THRI)) {
  232. this.iir = UART_IIR_THRI;
  233. this.cpu.device_raise_irq(this.irq);
  234. } else
  235. if ((this.ints & (1 << UART_IIR_MSI)) && (this.ier & UART_IER_MSI)) {
  236. this.iir = UART_IIR_MSI;
  237. this.cpu.device_raise_irq(this.irq);
  238. } else {
  239. this.iir = UART_IIR_NO_INT;
  240. this.cpu.device_lower_irq(this.irq);
  241. }
  242. };
  243. UART.prototype.ThrowInterrupt = function(line) {
  244. this.ints |= (1 << line);
  245. this.CheckInterrupt();
  246. };
  247. UART.prototype.ClearInterrupt = function(line) {
  248. this.ints &= ~(1 << line);
  249. this.CheckInterrupt();
  250. };
  251. /**
  252. * @param {number} data
  253. */
  254. UART.prototype.data_received = function(data)
  255. {
  256. dbg_log("input: " + h(data), LOG_SERIAL);
  257. this.input.push(data);
  258. this.lsr |= UART_LSR_DATA_READY;
  259. this.ThrowInterrupt(UART_IIR_CTI);
  260. };
  261. UART.prototype.write_data = function(out_byte)
  262. {
  263. if(this.line_control & DLAB)
  264. {
  265. this.baud_rate = this.baud_rate & ~0xFF | out_byte;
  266. return;
  267. }
  268. dbg_log("data: " + h(out_byte), LOG_SERIAL);
  269. this.ThrowInterrupt(UART_IIR_THRI);
  270. if(out_byte === 0xFF)
  271. {
  272. return;
  273. }
  274. var char = String.fromCharCode(out_byte);
  275. this.bus.send("serial" + this.com + "-output-char", char);
  276. this.current_line.push(out_byte);
  277. if(char === "\n")
  278. {
  279. const line = String.fromCharCode.apply("", this.current_line).trimRight().replace(/[\x00-\x08\x0b-\x1f\x7f\x80-\xff]/g, "");
  280. dbg_log("SERIAL: " + line);
  281. this.bus.send("serial" + this.com + "-output-line", String.fromCharCode.apply("", this.current_line));
  282. this.current_line = [];
  283. }
  284. };