hw_com.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_COM_C_
  15. #define _HW_COM_C_
  16. #ifndef STATIC_INLINE_HW_COM
  17. #define STATIC_INLINE_HW_COM STATIC_INLINE
  18. #endif
  19. #include "device_table.h"
  20. #ifdef HAVE_STRING_H
  21. #include <string.h>
  22. #else
  23. #ifdef HAVE_STRINGS_H
  24. #include <strings.h>
  25. #endif
  26. #endif
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. /* DEVICE
  34. com - '550 compatible serial device
  35. DESCRIPTION
  36. Models the basics of the 8 register '550 serial device. The model
  37. includes an interrupt line, input and output fifos, and status
  38. information.
  39. Independent configuration of the devices input and output streams is
  40. allowed: use either the console or a file (buffered or unbuffered) as
  41. the data source/sink; specify the real-time delay between each character
  42. transfer.
  43. When the devices input stream is being taken from a file, the end of
  44. file is signaled by a loss of carrier (the loss of carrier may be
  45. incorrectly proceeded by a single null character).
  46. PROPERTIES
  47. reg = <address> <size> ... (optional - note 1)
  48. List of <address> <size> pairs. Each pair specifies an address for
  49. the devices 8 registers. The address should be 8 byte aligned.
  50. alternate-reg = <address> <size> ... (optional - note 1)
  51. Alternative addreses for the registers.
  52. assigned-addresses = <address> <size> ... (optional - note 1)
  53. On a PCI bus, this property specifies the addresses assigned to the
  54. device. The values reflect the devices configuration base registers.
  55. Note 1: At least one of "assigned-addresses", "reg" or "alternative-reg"
  56. must be specified. If "assigned-addresses" is specified the other
  57. address specifications are ignored.
  58. input-file = <file-name> (optional)
  59. File to take all serial port input from (instead of the simulation
  60. console).
  61. output-file = <file-name> (optional)
  62. File to send all output to (instead of the simulation console).
  63. input-buffering = "unbuffered" (optional)
  64. Specifying "unbuffered" buffering disables buffering on the serial
  65. devices input stream (all data is immediately read). In the future,
  66. this option may be used to provide input buffering alternatives.
  67. output-buffering = "unbuffered" (optional)
  68. Specifying "unbuffered" buffering disables buffering on the serial
  69. devices output stream (all data is immediately written). In the future,
  70. this option may be extended to include other buffering alternatives.
  71. input-delay = <integer-delay> (optional)
  72. Specify the number of ticks after the current character has been
  73. read from the serial port that the next character becomes
  74. available.
  75. output-delay = <integer-delay> (optional)
  76. Specify the number of ticks after a character has been written to
  77. the empty output fifo that the fifo finishes draining. Any
  78. characters written to the output fifo before it has drained will
  79. not be lost and will still be displayed.
  80. EXAMPLES
  81. | /iobus@0xf0000000/com@0x3000/reg 0x3000 8
  82. Create a simple console device at address <<0x3000>> within
  83. <<iobus>>. Since iobus starts at address <<0xf0000000>> the
  84. absolute address of the serial port will be <<0xf0003000>>.
  85. The device will always be ready for I/O (no delay properties specified)
  86. and both the input and output streams will use the simulation console
  87. (no file properties).
  88. | $ psim \
  89. | -o '/cpus/cpu@0' \
  90. | -o '/iobus@0xf0000000/com@0x4000/reg 0x4000 8' \
  91. | -o '/iobus@0xf0000000/com@0x4000/input-file /etc/passwd' \
  92. | -o '/iobus@0xf0000000/com@0x4000/input-delay 1000' \
  93. | -o '/iobus@0xf0000000/com@0x4000 > 0 int /cpus/cpu@0x0' \
  94. | psim-test/hw-com/cat.be 0xf0004000
  95. The serial port (at address <<0xf0004000>> is configured so that it
  96. takes its input from the file <</etc/passwd>> while its output is
  97. allowed to appear on the simulation console.
  98. The node <</cpus/cpu@0>> was explicitly specified to ensure that it had
  99. been created before any interrupts were attached to it.
  100. The program <<psim-test/hw-com/cat>> copies any characters on the serial
  101. port's input (<</etc/passwd>>) to its output (the console).
  102. Consequently, the aove program will display the contents of the file
  103. <</etc/passwd>> on the screen.
  104. BUGS
  105. IEEE 1275 requires that a device on a PCI bus have, as its first reg
  106. entry, the address of its configuration space registers. Currently,
  107. this device does not even implement configuration registers.
  108. This model does not attempt to model the '550's input and output fifos.
  109. Instead, the input fifo is limited to a single character at a time,
  110. while the output fifo is effectivly infinite. Consequently, unlike the
  111. '550, this device will not discard output characters once a stream of 16
  112. have been written to the data output register.
  113. The input and output can only be taken from a file (or the current
  114. terminal device). In the future, the <<com>> device should allow the
  115. specification of other data streams (such as an xterm or TK window).
  116. The input blocks if no data is available.
  117. Interrupts have not been tested.
  118. */
  119. enum {
  120. max_hw_com_registers = 8,
  121. };
  122. typedef struct _com_port {
  123. int ready;
  124. int delay;
  125. int interrupting;
  126. FILE *file;
  127. } com_port;
  128. typedef struct _com_modem {
  129. int carrier;
  130. int carrier_changed;
  131. int interrupting;
  132. } com_modem;
  133. typedef struct _hw_com_device {
  134. com_port input;
  135. com_port output;
  136. com_modem modem;
  137. char dlab[2];
  138. char reg[max_hw_com_registers];
  139. int interrupting;
  140. } hw_com_device;
  141. static void
  142. hw_com_device_init_data(device *me)
  143. {
  144. hw_com_device *com = (hw_com_device*)device_data(me);
  145. /* clean up */
  146. if (com->output.file != NULL)
  147. fclose(com->output.file);
  148. if (com->input.file != NULL)
  149. fclose(com->input.file);
  150. memset(com, 0, sizeof(hw_com_device));
  151. /* the fifo speed */
  152. com->output.delay = (device_find_property(me, "output-delay") != NULL
  153. ? device_find_integer_property(me, "output-delay")
  154. : 0);
  155. com->input.delay = (device_find_property(me, "input-delay") != NULL
  156. ? device_find_integer_property(me, "input-delay")
  157. : 0);
  158. /* the data source/sink */
  159. if (device_find_property(me, "input-file") != NULL) {
  160. const char *input_file = device_find_string_property(me, "input-file");
  161. com->input.file = fopen(input_file, "r");
  162. if (com->input.file == NULL)
  163. device_error(me, "Problem opening input file %s\n", input_file);
  164. if (device_find_property(me, "input-buffering") != NULL) {
  165. const char *buffering = device_find_string_property(me, "input-buffering");
  166. if (strcmp(buffering, "unbuffered") == 0)
  167. setbuf(com->input.file, NULL);
  168. }
  169. }
  170. if (device_find_property(me, "output-file") != NULL) {
  171. const char *output_file = device_find_string_property(me, "output-file");
  172. com->output.file = fopen(output_file, "w");
  173. if (com->output.file == NULL)
  174. device_error(me, "Problem opening output file %s\n", output_file);
  175. if (device_find_property(me, "output-buffering") != NULL) {
  176. const char *buffering = device_find_string_property(me, "output-buffering");
  177. if (strcmp(buffering, "unbuffered") == 0)
  178. setbuf(com->output.file, NULL);
  179. }
  180. }
  181. /* ready from the start */
  182. com->input.ready = 1;
  183. com->modem.carrier = 1;
  184. com->output.ready = 1;
  185. }
  186. static void
  187. update_com_interrupts(device *me,
  188. hw_com_device *com)
  189. {
  190. int interrupting;
  191. com->modem.interrupting = (com->modem.carrier_changed && (com->reg[1] & 0x80));
  192. com->input.interrupting = (com->input.ready && (com->reg[1] & 0x1));
  193. com->output.interrupting = (com->output.ready && (com->reg[1] & 0x2));
  194. interrupting = (com->input.interrupting
  195. || com->output.interrupting
  196. || com->modem.interrupting);
  197. if (interrupting) {
  198. if (!com->interrupting) {
  199. device_interrupt_event(me, 0 /*port*/, 1 /*value*/, NULL, 0);
  200. }
  201. }
  202. else /*!interrupting*/ {
  203. if (com->interrupting)
  204. device_interrupt_event(me, 0 /*port*/, 0 /*value*/, NULL, 0);
  205. }
  206. com->interrupting = interrupting;
  207. }
  208. static void
  209. make_read_ready(void *data)
  210. {
  211. device *me = (device*)data;
  212. hw_com_device *com = (hw_com_device*)device_data(me);
  213. com->input.ready = 1;
  214. update_com_interrupts(me, com);
  215. }
  216. static void
  217. read_com(device *me,
  218. hw_com_device *com,
  219. unsigned_word a,
  220. char val[1])
  221. {
  222. unsigned_word addr = a % 8;
  223. /* the divisor latch is special */
  224. if (com->reg[3] & 0x8 && addr < 2) {
  225. *val = com->dlab[addr];
  226. return;
  227. }
  228. switch (addr) {
  229. case 0:
  230. /* fifo */
  231. if (!com->modem.carrier)
  232. *val = '\0';
  233. if (com->input.ready) {
  234. /* read the char in */
  235. if (com->input.file == NULL) {
  236. if (sim_io_read_stdin(val, 1) < 0)
  237. com->modem.carrier_changed = 1;
  238. }
  239. else {
  240. if (fread(val, 1, 1, com->input.file) == 0)
  241. com->modem.carrier_changed = 1;
  242. }
  243. /* setup for next read */
  244. if (com->modem.carrier_changed) {
  245. /* once lost carrier, never ready */
  246. com->modem.carrier = 0;
  247. com->input.ready = 0;
  248. *val = '\0';
  249. }
  250. else if (com->input.delay > 0) {
  251. com->input.ready = 0;
  252. device_event_queue_schedule(me, com->input.delay, make_read_ready, me);
  253. }
  254. }
  255. else {
  256. /* discard it? */
  257. /* overflow input fifo? */
  258. *val = '\0';
  259. }
  260. break;
  261. case 2:
  262. /* interrupt ident */
  263. if (com->interrupting) {
  264. if (com->input.interrupting)
  265. *val = 0x4;
  266. else if (com->output.interrupting)
  267. *val = 0x2;
  268. else if (com->modem.interrupting == 0)
  269. *val = 0;
  270. else
  271. device_error(me, "bad elif for interrupts\n");
  272. }
  273. else
  274. *val = 0x1;
  275. break;
  276. case 5:
  277. /* line status */
  278. *val = ((com->input.ready ? 0x1 : 0)
  279. | (com->output.ready ? 0x60 : 0)
  280. );
  281. break;
  282. case 6:
  283. /* modem status */
  284. *val = ((com->modem.carrier_changed ? 0x08 : 0)
  285. | (com->modem.carrier ? 0x80 : 0)
  286. );
  287. com->modem.carrier_changed = 0;
  288. break;
  289. default:
  290. *val = com->reg[addr];
  291. break;
  292. }
  293. update_com_interrupts(me, com);
  294. }
  295. static unsigned
  296. hw_com_io_read_buffer_callback(device *me,
  297. void *dest,
  298. int space,
  299. unsigned_word addr,
  300. unsigned nr_bytes,
  301. cpu *processor,
  302. unsigned_word cia)
  303. {
  304. hw_com_device *com = device_data(me);
  305. int i;
  306. for (i = 0; i < nr_bytes; i++) {
  307. read_com(me, com, addr + i, &((char*)dest)[i]);
  308. }
  309. return nr_bytes;
  310. }
  311. static void
  312. make_write_ready(void *data)
  313. {
  314. device *me = (device*)data;
  315. hw_com_device *com = (hw_com_device*)device_data(me);
  316. com->output.ready = 1;
  317. update_com_interrupts(me, com);
  318. }
  319. static void
  320. write_com(device *me,
  321. hw_com_device *com,
  322. unsigned_word a,
  323. char val)
  324. {
  325. unsigned_word addr = a % 8;
  326. /* the divisor latch is special */
  327. if (com->reg[3] & 0x8 && addr < 2) {
  328. com->dlab[addr] = val;
  329. return;
  330. }
  331. switch (addr) {
  332. case 0:
  333. /* fifo */
  334. if (com->output.file == NULL) {
  335. sim_io_write_stdout(&val, 1);
  336. }
  337. else {
  338. fwrite(&val, 1, 1, com->output.file);
  339. }
  340. /* setup for next write */
  341. if (com->output.ready && com->output.delay > 0) {
  342. com->output.ready = 0;
  343. device_event_queue_schedule(me, com->output.delay, make_write_ready, me);
  344. }
  345. break;
  346. default:
  347. com->reg[addr] = val;
  348. break;
  349. }
  350. update_com_interrupts(me, com);
  351. }
  352. static unsigned
  353. hw_com_io_write_buffer_callback(device *me,
  354. const void *source,
  355. int space,
  356. unsigned_word addr,
  357. unsigned nr_bytes,
  358. cpu *processor,
  359. unsigned_word cia)
  360. {
  361. hw_com_device *com = device_data(me);
  362. int i;
  363. for (i = 0; i < nr_bytes; i++) {
  364. write_com(me, com, addr + i, ((char*)source)[i]);
  365. }
  366. return nr_bytes;
  367. }
  368. /* instances of the hw_com device */
  369. static void
  370. hw_com_instance_delete(device_instance *instance)
  371. {
  372. /* nothing to delete, the hw_com is attached to the device */
  373. return;
  374. }
  375. static int
  376. hw_com_instance_read(device_instance *instance,
  377. void *buf,
  378. unsigned_word len)
  379. {
  380. device *me = device_instance_device(instance);
  381. hw_com_device *com = device_data(me);
  382. if (com->input.file == NULL)
  383. return sim_io_read_stdin(buf, len);
  384. else {
  385. return fread(buf, 1, len, com->input.file);
  386. }
  387. }
  388. static int
  389. hw_com_instance_write(device_instance *instance,
  390. const void *buf,
  391. unsigned_word len)
  392. {
  393. device *me = device_instance_device(instance);
  394. hw_com_device *com = device_data(me);
  395. if (com->output.file == NULL)
  396. return sim_io_write_stdout(buf, len);
  397. else {
  398. return fwrite(buf, 1, len, com->output.file);
  399. }
  400. }
  401. static const device_instance_callbacks hw_com_instance_callbacks = {
  402. hw_com_instance_delete,
  403. hw_com_instance_read,
  404. hw_com_instance_write,
  405. };
  406. static device_instance *
  407. hw_com_create_instance(device *me,
  408. const char *path,
  409. const char *args)
  410. {
  411. /* point an instance directly at the device */
  412. return device_create_instance_from(me, NULL,
  413. device_data(me),
  414. path, args,
  415. &hw_com_instance_callbacks);
  416. }
  417. static device_callbacks const hw_com_callbacks = {
  418. { generic_device_init_address,
  419. hw_com_device_init_data },
  420. { NULL, }, /* address */
  421. { hw_com_io_read_buffer_callback,
  422. hw_com_io_write_buffer_callback, },
  423. { NULL, }, /* DMA */
  424. { NULL, }, /* interrupt */
  425. { NULL, }, /* unit */
  426. hw_com_create_instance,
  427. };
  428. static void *
  429. hw_com_create(const char *name,
  430. const device_unit *unit_address,
  431. const char *args)
  432. {
  433. /* create the descriptor */
  434. hw_com_device *hw_com = ZALLOC(hw_com_device);
  435. return hw_com;
  436. }
  437. const device_descriptor hw_com_device_descriptor[] = {
  438. { "com", hw_com_create, &hw_com_callbacks },
  439. { NULL },
  440. };
  441. #endif /* _HW_COM_C_ */