dv-tx3904cpu.c 5.8 KB

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