dv-lm32cpu.c 6.4 KB

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