dv-lm32cpu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Lattice Mico32 CPU model.
  2. Contributed by Jon Beniston <jon@beniston.com>
  3. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "hw-main.h"
  16. #include "sim-main.h"
  17. struct lm32cpu
  18. {
  19. struct hw_event *event;
  20. };
  21. /* input port ID's. */
  22. enum
  23. {
  24. INT0_PORT,
  25. INT1_PORT,
  26. INT2_PORT,
  27. INT3_PORT,
  28. INT4_PORT,
  29. INT5_PORT,
  30. INT6_PORT,
  31. INT7_PORT,
  32. INT8_PORT,
  33. INT9_PORT,
  34. INT10_PORT,
  35. INT11_PORT,
  36. INT12_PORT,
  37. INT13_PORT,
  38. INT14_PORT,
  39. INT15_PORT,
  40. INT16_PORT,
  41. INT17_PORT,
  42. INT18_PORT,
  43. INT19_PORT,
  44. INT20_PORT,
  45. INT21_PORT,
  46. INT22_PORT,
  47. INT23_PORT,
  48. INT24_PORT,
  49. INT25_PORT,
  50. INT26_PORT,
  51. INT27_PORT,
  52. INT28_PORT,
  53. INT29_PORT,
  54. INT30_PORT,
  55. INT31_PORT,
  56. };
  57. static const struct hw_port_descriptor lm32cpu_ports[] = {
  58. /* interrupt inputs. */
  59. {"int0", INT0_PORT, 0, input_port,},
  60. {"int1", INT1_PORT, 0, input_port,},
  61. {"int2", INT2_PORT, 0, input_port,},
  62. {"int3", INT3_PORT, 0, input_port,},
  63. {"int4", INT4_PORT, 0, input_port,},
  64. {"int5", INT5_PORT, 0, input_port,},
  65. {"int6", INT6_PORT, 0, input_port,},
  66. {"int7", INT7_PORT, 0, input_port,},
  67. {"int8", INT8_PORT, 0, input_port,},
  68. {"int9", INT9_PORT, 0, input_port,},
  69. {"int10", INT10_PORT, 0, input_port,},
  70. {"int11", INT11_PORT, 0, input_port,},
  71. {"int12", INT12_PORT, 0, input_port,},
  72. {"int13", INT13_PORT, 0, input_port,},
  73. {"int14", INT14_PORT, 0, input_port,},
  74. {"int15", INT15_PORT, 0, input_port,},
  75. {"int16", INT16_PORT, 0, input_port,},
  76. {"int17", INT17_PORT, 0, input_port,},
  77. {"int18", INT18_PORT, 0, input_port,},
  78. {"int19", INT19_PORT, 0, input_port,},
  79. {"int20", INT20_PORT, 0, input_port,},
  80. {"int21", INT21_PORT, 0, input_port,},
  81. {"int22", INT22_PORT, 0, input_port,},
  82. {"int23", INT23_PORT, 0, input_port,},
  83. {"int24", INT24_PORT, 0, input_port,},
  84. {"int25", INT25_PORT, 0, input_port,},
  85. {"int26", INT26_PORT, 0, input_port,},
  86. {"int27", INT27_PORT, 0, input_port,},
  87. {"int28", INT28_PORT, 0, input_port,},
  88. {"int29", INT29_PORT, 0, input_port,},
  89. {"int30", INT30_PORT, 0, input_port,},
  90. {"int31", INT31_PORT, 0, input_port,},
  91. {NULL,},
  92. };
  93. /*
  94. * Finish off the partially created hw device. Attach our local
  95. * callbacks. Wire up our port names etc.
  96. */
  97. static hw_port_event_method lm32cpu_port_event;
  98. static void
  99. lm32cpu_finish (struct hw *me)
  100. {
  101. struct lm32cpu *controller;
  102. controller = HW_ZALLOC (me, struct lm32cpu);
  103. set_hw_data (me, controller);
  104. set_hw_ports (me, lm32cpu_ports);
  105. set_hw_port_event (me, lm32cpu_port_event);
  106. /* Initialize the pending interrupt flags. */
  107. controller->event = NULL;
  108. }
  109. /* An event arrives on an interrupt port. */
  110. static unsigned int s_ui_ExtIntrs = 0;
  111. static void
  112. deliver_lm32cpu_interrupt (struct hw *me, void *data)
  113. {
  114. static unsigned int ip, im, im_and_ip_result;
  115. struct lm32cpu *controller = hw_data (me);
  116. SIM_DESC sd = hw_system (me);
  117. sim_cpu *cpu = STATE_CPU (sd, 0); /* NB: fix CPU 0. */
  118. address_word cia = CPU_PC_GET (cpu);
  119. int interrupt = (int) data;
  120. HW_TRACE ((me, "interrupt-check event"));
  121. /*
  122. * Determine if an external interrupt is active
  123. * and needs to cause an exception.
  124. */
  125. im = lm32bf_h_csr_get (cpu, LM32_CSR_IM);
  126. ip = lm32bf_h_csr_get (cpu, LM32_CSR_IP);
  127. im_and_ip_result = im & ip;
  128. if ((lm32bf_h_csr_get (cpu, LM32_CSR_IE) & 1) && (im_and_ip_result != 0))
  129. {
  130. /* Save PC in exception address register. */
  131. lm32bf_h_gr_set (cpu, 30, lm32bf_h_pc_get (cpu));
  132. /* Restart at interrupt offset in handler exception table. */
  133. lm32bf_h_pc_set (cpu,
  134. lm32bf_h_csr_get (cpu,
  135. LM32_CSR_EBA) +
  136. LM32_EID_INTERRUPT * 32);
  137. /* Save interrupt enable and then clear. */
  138. lm32bf_h_csr_set (cpu, LM32_CSR_IE, 0x2);
  139. }
  140. /* reschedule soon. */
  141. if (controller->event != NULL)
  142. hw_event_queue_deschedule (me, controller->event);
  143. controller->event = NULL;
  144. /* if there are external interrupts, schedule an interrupt-check again.
  145. * NOTE: THIS MAKES IT VERY INEFFICIENT. INSTEAD, TRIGGER THIS
  146. * CHECk_EVENT WHEN THE USER ENABLES IE OR USER MODIFIES IM REGISTERS.
  147. */
  148. if (s_ui_ExtIntrs != 0)
  149. controller->event =
  150. hw_event_queue_schedule (me, 1, deliver_lm32cpu_interrupt, data);
  151. }
  152. /* Handle an event on one of the CPU's ports. */
  153. static void
  154. lm32cpu_port_event (struct hw *me,
  155. int my_port,
  156. struct hw *source, int source_port, int level)
  157. {
  158. struct lm32cpu *controller = hw_data (me);
  159. SIM_DESC sd = hw_system (me);
  160. sim_cpu *cpu = STATE_CPU (sd, 0); /* NB: fix CPU 0. */
  161. address_word cia = CPU_PC_GET (cpu);
  162. HW_TRACE ((me, "interrupt event on port %d, level %d", my_port, level));
  163. /*
  164. * Activate IP if the interrupt's activated; don't do anything if
  165. * the interrupt's deactivated.
  166. */
  167. if (level == 1)
  168. {
  169. /*
  170. * save state of external interrupt.
  171. */
  172. s_ui_ExtIntrs |= (1 << my_port);
  173. /* interrupt-activated so set IP. */
  174. lm32bf_h_csr_set (cpu, LM32_CSR_IP,
  175. lm32bf_h_csr_get (cpu, LM32_CSR_IP) | (1 << my_port));
  176. /*
  177. * Since interrupt is activated, queue an immediate event
  178. * to check if this interrupt is serviceable.
  179. */
  180. if (controller->event != NULL)
  181. hw_event_queue_deschedule (me, controller->event);
  182. /*
  183. * Queue an immediate event to check if this interrupt must be serviced;
  184. * this will happen after the current instruction is complete.
  185. */
  186. controller->event = hw_event_queue_schedule (me,
  187. 0,
  188. deliver_lm32cpu_interrupt,
  189. 0);
  190. }
  191. else
  192. {
  193. /*
  194. * save state of external interrupt.
  195. */
  196. s_ui_ExtIntrs &= ~(1 << my_port);
  197. }
  198. }
  199. const struct hw_descriptor dv_lm32cpu_descriptor[] = {
  200. {"lm32cpu", lm32cpu_finish,},
  201. {NULL},
  202. };