hw_pal.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_PAL_C_
  15. #define _HW_PAL_C_
  16. #ifndef STATIC_INLINE_HW_PAL
  17. #define STATIC_INLINE_HW_PAL STATIC_INLINE
  18. #endif
  19. #include "device_table.h"
  20. #include "cpu.h"
  21. #ifdef HAVE_STRING_H
  22. #include <string.h>
  23. #else
  24. #ifdef HAVE_STRINGS_H
  25. #include <strings.h>
  26. #endif
  27. #endif
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. /* DEVICE
  35. pal - glue logic device containing assorted junk
  36. DESCRIPTION
  37. Typical hardware dependant hack. This device allows the firmware
  38. to gain access to all the things the firmware needs (but the OS
  39. doesn't).
  40. The pal contains the following registers. Except for the interrupt
  41. level register, each of the below is 8 bytes in size and must be
  42. accessed using correct alignment. For 16 and 32 bit accesses the
  43. bytes not directed to the register are ignored:
  44. |0 reset register (write)
  45. |4 processor id register (read)
  46. |8 interrupt port (write)
  47. |9 interrupt level (write)
  48. |12 processor count register (read)
  49. |16 tty input fifo register (read)
  50. |20 tty input status register (read)
  51. |24 tty output fifo register (write)
  52. |28 tty output status register (read)
  53. Reset register (write) halts the simulator exiting with the
  54. value written.
  55. Processor id register (read) returns the processor number (0
  56. .. N-1) of the processor performing the read.
  57. The interrupt registers should be accessed as a pair (using a 16 or
  58. 32 bit store). The low byte specifies the interrupt port while the
  59. high byte specifies the level to drive that port at. By
  60. convention, the pal's interrupt ports (int0, int1, ...) are wired
  61. up to the corresponding processor's level sensative external
  62. interrupt pin. Eg: A two byte write to address 8 of 0x0102
  63. (big-endian) will result in processor 2's external interrupt pin to
  64. be asserted.
  65. Processor count register (read) returns the total number of
  66. processors active in the current simulation.
  67. TTY input fifo register (read), if the TTY input status register
  68. indicates a character is available by being nonzero, returns the
  69. next available character from the pal's tty input port.
  70. Similarly, the TTY output fifo register (write), if the TTY output
  71. status register indicates the output fifo is not full by being
  72. nonzero, outputs the character written to the tty's output port.
  73. PROPERTIES
  74. reg = <address> <size> (required)
  75. Specify the address (within the parent bus) that this device is to
  76. live.
  77. */
  78. enum {
  79. hw_pal_reset_register = 0x0,
  80. hw_pal_cpu_nr_register = 0x4,
  81. hw_pal_int_register = 0x8,
  82. hw_pal_nr_cpu_register = 0xa,
  83. hw_pal_read_fifo = 0x10,
  84. hw_pal_read_status = 0x14,
  85. hw_pal_write_fifo = 0x18,
  86. hw_pal_write_status = 0x1a,
  87. hw_pal_address_mask = 0x1f,
  88. };
  89. typedef struct _hw_pal_console_buffer {
  90. char buffer;
  91. int status;
  92. } hw_pal_console_buffer;
  93. typedef struct _hw_pal_device {
  94. hw_pal_console_buffer input;
  95. hw_pal_console_buffer output;
  96. device *disk;
  97. } hw_pal_device;
  98. /* check the console for an available character */
  99. static void
  100. scan_hw_pal(hw_pal_device *hw_pal)
  101. {
  102. char c;
  103. int count;
  104. count = sim_io_read_stdin(&c, sizeof(c));
  105. switch (count) {
  106. case sim_io_not_ready:
  107. case sim_io_eof:
  108. hw_pal->input.buffer = 0;
  109. hw_pal->input.status = 0;
  110. break;
  111. default:
  112. hw_pal->input.buffer = c;
  113. hw_pal->input.status = 1;
  114. }
  115. }
  116. /* write the character to the hw_pal */
  117. static void
  118. write_hw_pal(hw_pal_device *hw_pal,
  119. char val)
  120. {
  121. sim_io_write_stdout(&val, 1);
  122. hw_pal->output.buffer = val;
  123. hw_pal->output.status = 1;
  124. }
  125. static unsigned
  126. hw_pal_io_read_buffer_callback(device *me,
  127. void *dest,
  128. int space,
  129. unsigned_word addr,
  130. unsigned nr_bytes,
  131. cpu *processor,
  132. unsigned_word cia)
  133. {
  134. hw_pal_device *hw_pal = (hw_pal_device*)device_data(me);
  135. unsigned_1 val;
  136. switch (addr & hw_pal_address_mask) {
  137. case hw_pal_cpu_nr_register:
  138. val = cpu_nr(processor);
  139. DTRACE(pal, ("read - cpu-nr %d\n", val));
  140. break;
  141. case hw_pal_nr_cpu_register:
  142. val = tree_find_integer_property(me, "/openprom/options/smp");
  143. DTRACE(pal, ("read - nr-cpu %d\n", val));
  144. break;
  145. case hw_pal_read_fifo:
  146. val = hw_pal->input.buffer;
  147. DTRACE(pal, ("read - input-fifo %d\n", val));
  148. break;
  149. case hw_pal_read_status:
  150. scan_hw_pal(hw_pal);
  151. val = hw_pal->input.status;
  152. DTRACE(pal, ("read - input-status %d\n", val));
  153. break;
  154. case hw_pal_write_fifo:
  155. val = hw_pal->output.buffer;
  156. DTRACE(pal, ("read - output-fifo %d\n", val));
  157. break;
  158. case hw_pal_write_status:
  159. val = hw_pal->output.status;
  160. DTRACE(pal, ("read - output-status %d\n", val));
  161. break;
  162. default:
  163. val = 0;
  164. DTRACE(pal, ("read - ???\n"));
  165. }
  166. memset(dest, 0, nr_bytes);
  167. *(unsigned_1*)dest = val;
  168. return nr_bytes;
  169. }
  170. static unsigned
  171. hw_pal_io_write_buffer_callback(device *me,
  172. const void *source,
  173. int space,
  174. unsigned_word addr,
  175. unsigned nr_bytes,
  176. cpu *processor,
  177. unsigned_word cia)
  178. {
  179. hw_pal_device *hw_pal = (hw_pal_device*)device_data(me);
  180. unsigned_1 *byte = (unsigned_1*)source;
  181. switch (addr & hw_pal_address_mask) {
  182. case hw_pal_reset_register:
  183. cpu_halt(processor, cia, was_exited, byte[0]);
  184. break;
  185. case hw_pal_int_register:
  186. device_interrupt_event(me,
  187. byte[0], /*port*/
  188. (nr_bytes > 1 ? byte[1] : 0), /* val */
  189. processor, cia);
  190. break;
  191. case hw_pal_read_fifo:
  192. hw_pal->input.buffer = byte[0];
  193. DTRACE(pal, ("write - input-fifo %d\n", byte[0]));
  194. break;
  195. case hw_pal_read_status:
  196. hw_pal->input.status = byte[0];
  197. DTRACE(pal, ("write - input-status %d\n", byte[0]));
  198. break;
  199. case hw_pal_write_fifo:
  200. write_hw_pal(hw_pal, byte[0]);
  201. DTRACE(pal, ("write - output-fifo %d\n", byte[0]));
  202. break;
  203. case hw_pal_write_status:
  204. hw_pal->output.status = byte[0];
  205. DTRACE(pal, ("write - output-status %d\n", byte[0]));
  206. break;
  207. }
  208. return nr_bytes;
  209. }
  210. /* instances of the hw_pal device */
  211. static void
  212. hw_pal_instance_delete_callback(device_instance *instance)
  213. {
  214. /* nothing to delete, the hw_pal is attached to the device */
  215. return;
  216. }
  217. static int
  218. hw_pal_instance_read_callback(device_instance *instance,
  219. void *buf,
  220. unsigned_word len)
  221. {
  222. DITRACE(pal, ("read - %s (%ld)", (const char*)buf, (long int)len));
  223. return sim_io_read_stdin(buf, len);
  224. }
  225. static int
  226. hw_pal_instance_write_callback(device_instance *instance,
  227. const void *buf,
  228. unsigned_word len)
  229. {
  230. int i;
  231. const char *chp = buf;
  232. hw_pal_device *hw_pal = device_instance_data(instance);
  233. DITRACE(pal, ("write - %s (%ld)", (const char*)buf, (long int)len));
  234. for (i = 0; i < len; i++)
  235. write_hw_pal(hw_pal, chp[i]);
  236. sim_io_flush_stdoutput();
  237. return i;
  238. }
  239. static const device_instance_callbacks hw_pal_instance_callbacks = {
  240. hw_pal_instance_delete_callback,
  241. hw_pal_instance_read_callback,
  242. hw_pal_instance_write_callback,
  243. };
  244. static device_instance *
  245. hw_pal_create_instance(device *me,
  246. const char *path,
  247. const char *args)
  248. {
  249. return device_create_instance_from(me, NULL,
  250. device_data(me),
  251. path, args,
  252. &hw_pal_instance_callbacks);
  253. }
  254. static const device_interrupt_port_descriptor hw_pal_interrupt_ports[] = {
  255. { "int", 0, MAX_NR_PROCESSORS },
  256. { NULL }
  257. };
  258. static void
  259. hw_pal_attach_address(device *me,
  260. attach_type attach,
  261. int space,
  262. unsigned_word addr,
  263. unsigned nr_bytes,
  264. access_type access,
  265. device *client)
  266. {
  267. hw_pal_device *pal = (hw_pal_device*)device_data(me);
  268. pal->disk = client;
  269. }
  270. static device_callbacks const hw_pal_callbacks = {
  271. { generic_device_init_address, },
  272. { hw_pal_attach_address, }, /* address */
  273. { hw_pal_io_read_buffer_callback,
  274. hw_pal_io_write_buffer_callback, },
  275. { NULL, }, /* DMA */
  276. { NULL, NULL, hw_pal_interrupt_ports }, /* interrupt */
  277. { generic_device_unit_decode,
  278. generic_device_unit_encode,
  279. generic_device_address_to_attach_address,
  280. generic_device_size_to_attach_size },
  281. hw_pal_create_instance,
  282. };
  283. static void *
  284. hw_pal_create(const char *name,
  285. const device_unit *unit_address,
  286. const char *args)
  287. {
  288. /* create the descriptor */
  289. hw_pal_device *hw_pal = ZALLOC(hw_pal_device);
  290. hw_pal->output.status = 1;
  291. hw_pal->output.buffer = '\0';
  292. hw_pal->input.status = 0;
  293. hw_pal->input.buffer = '\0';
  294. return hw_pal;
  295. }
  296. const device_descriptor hw_pal_device_descriptor[] = {
  297. { "pal", hw_pal_create, &hw_pal_callbacks },
  298. { NULL },
  299. };
  300. #endif /* _HW_PAL_C_ */