dv-tx3904cpu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* This file is part of the program GDB, the GNU debugger.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "hw-main.h"
  19. /* DEVICE
  20. tx3904cpu - tx3904 cpu virtual device
  21. DESCRIPTION
  22. Implements the external tx3904 functionality. This includes the
  23. delivery of of interrupts generated from other devices and the
  24. handling of device specific registers.
  25. PROPERTIES
  26. none
  27. PORTS
  28. reset (input)
  29. Currently ignored.
  30. nmi (input)
  31. Deliver a non-maskable interrupt to the processor.
  32. level (input)
  33. Deliver a maskable interrupt of given level, corresponding to
  34. IP[5:0], to processor.
  35. BUGS
  36. When delivering an interrupt, this code assumes that there is only
  37. one processor (number 0).
  38. This code does not attempt to be efficient at handling pending
  39. interrupts. It simply schedules the interrupt delivery handler
  40. every instruction cycle until all pending interrupts go away. An
  41. alternative implementation might modify instructions that change
  42. the PSW and have them check to see if the change makes an interrupt
  43. delivery possible.
  44. */
  45. struct tx3904cpu {
  46. /* Pending interrupts for delivery by event handler */
  47. int pending_reset, pending_nmi, pending_level;
  48. struct hw_event* event;
  49. };
  50. /* input port ID's */
  51. enum {
  52. RESET_PORT,
  53. NMI_PORT,
  54. LEVEL_PORT,
  55. };
  56. static const struct hw_port_descriptor tx3904cpu_ports[] = {
  57. /* interrupt inputs */
  58. { "reset", RESET_PORT, 0, input_port, },
  59. { "nmi", NMI_PORT, 0, input_port, },
  60. { "level", LEVEL_PORT, 0, input_port, },
  61. { NULL, },
  62. };
  63. /* Finish off the partially created hw device. Attach our local
  64. callbacks. Wire up our port names etc */
  65. static hw_port_event_method tx3904cpu_port_event;
  66. static void
  67. tx3904cpu_finish (struct hw *me)
  68. {
  69. struct tx3904cpu *controller;
  70. controller = HW_ZALLOC (me, struct tx3904cpu);
  71. set_hw_data (me, controller);
  72. set_hw_ports (me, tx3904cpu_ports);
  73. set_hw_port_event (me, tx3904cpu_port_event);
  74. /* Initialize the pending interrupt flags */
  75. controller->pending_level = 0;
  76. controller->pending_reset = 0;
  77. controller->pending_nmi = 0;
  78. controller->event = NULL;
  79. }
  80. /* An event arrives on an interrupt port */
  81. static void
  82. deliver_tx3904cpu_interrupt (struct hw *me,
  83. void *data)
  84. {
  85. struct tx3904cpu *controller = hw_data (me);
  86. SIM_DESC sd = hw_system (me);
  87. sim_cpu *cpu = STATE_CPU (sd, 0); /* NB: fix CPU 0. */
  88. address_word cia = CPU_PC_GET (cpu);
  89. #define CPU cpu
  90. #define SD sd
  91. if (controller->pending_reset)
  92. {
  93. controller->pending_reset = 0;
  94. HW_TRACE ((me, "reset pc=0x%08lx", (long) CPU_PC_GET (cpu)));
  95. SignalExceptionNMIReset();
  96. }
  97. else if (controller->pending_nmi)
  98. {
  99. controller->pending_nmi = 0;
  100. HW_TRACE ((me, "nmi pc=0x%08lx", (long) CPU_PC_GET (cpu)));
  101. SignalExceptionNMIReset();
  102. }
  103. else if (controller->pending_level)
  104. {
  105. HW_TRACE ((me, "interrupt level=%d pc=0x%08lx sr=0x%08lx",
  106. controller->pending_level,
  107. (long) CPU_PC_GET (cpu), (long) SR));
  108. /* Clear CAUSE register. It may stay this way if the interrupt
  109. was cleared with a negative pending_level. */
  110. CAUSE &= ~ (cause_IP_mask << cause_IP_shift);
  111. if (controller->pending_level > 0) /* interrupt set */
  112. {
  113. /* set hardware-interrupt subfields of CAUSE register */
  114. CAUSE |= (controller->pending_level & cause_IP_mask) << cause_IP_shift;
  115. /* check for enabled / unmasked interrupts */
  116. if ((SR & status_IEc) &&
  117. (controller->pending_level & ((SR >> status_IM_shift) & status_IM_mask)))
  118. {
  119. controller->pending_level = 0;
  120. SignalExceptionInterrupt(0 /* dummy value */);
  121. }
  122. else
  123. {
  124. /* reschedule soon */
  125. if (controller->event != NULL)
  126. hw_event_queue_deschedule(me, controller->event);
  127. controller->event =
  128. hw_event_queue_schedule (me, 1, deliver_tx3904cpu_interrupt, NULL);
  129. }
  130. } /* interrupt set */
  131. }
  132. #undef CPU
  133. #undef SD
  134. }
  135. static void
  136. tx3904cpu_port_event (struct hw *me,
  137. int my_port,
  138. struct hw *source,
  139. int source_port,
  140. int level)
  141. {
  142. struct tx3904cpu *controller = hw_data (me);
  143. switch (my_port)
  144. {
  145. case RESET_PORT:
  146. controller->pending_reset = 1;
  147. HW_TRACE ((me, "port-in reset"));
  148. break;
  149. case NMI_PORT:
  150. controller->pending_nmi = 1;
  151. HW_TRACE ((me, "port-in nmi"));
  152. break;
  153. case LEVEL_PORT:
  154. /* level == 0 means that the interrupt was cleared */
  155. if (level == 0)
  156. controller->pending_level = -1; /* signal end of interrupt */
  157. else
  158. controller->pending_level = level;
  159. HW_TRACE ((me, "port-in level=%d", level));
  160. break;
  161. default:
  162. hw_abort (me, "bad switch");
  163. break;
  164. }
  165. /* Schedule an event to be delivered immediately after current
  166. instruction. */
  167. if (controller->event != NULL)
  168. hw_event_queue_deschedule(me, controller->event);
  169. controller->event =
  170. hw_event_queue_schedule (me, 0, deliver_tx3904cpu_interrupt, NULL);
  171. }
  172. const struct hw_descriptor dv_tx3904cpu_descriptor[] = {
  173. { "tx3904cpu", tx3904cpu_finish, },
  174. { NULL },
  175. };