1 /* Lattice Mico32 CPU model.
4 Copyright (C) 2009-2022 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #define WANT_CPU lm32bf
22 #define WANT_CPU_LM32BF
24 /* This must come before any other includes. */
33 struct hw_event *event;
36 /* input port ID's. */
74 static const struct hw_port_descriptor lm32cpu_ports[] = {
75 /* interrupt inputs. */
76 {"int0", INT0_PORT, 0, input_port,},
77 {"int1", INT1_PORT, 0, input_port,},
78 {"int2", INT2_PORT, 0, input_port,},
79 {"int3", INT3_PORT, 0, input_port,},
80 {"int4", INT4_PORT, 0, input_port,},
81 {"int5", INT5_PORT, 0, input_port,},
82 {"int6", INT6_PORT, 0, input_port,},
83 {"int7", INT7_PORT, 0, input_port,},
84 {"int8", INT8_PORT, 0, input_port,},
85 {"int9", INT9_PORT, 0, input_port,},
86 {"int10", INT10_PORT, 0, input_port,},
87 {"int11", INT11_PORT, 0, input_port,},
88 {"int12", INT12_PORT, 0, input_port,},
89 {"int13", INT13_PORT, 0, input_port,},
90 {"int14", INT14_PORT, 0, input_port,},
91 {"int15", INT15_PORT, 0, input_port,},
92 {"int16", INT16_PORT, 0, input_port,},
93 {"int17", INT17_PORT, 0, input_port,},
94 {"int18", INT18_PORT, 0, input_port,},
95 {"int19", INT19_PORT, 0, input_port,},
96 {"int20", INT20_PORT, 0, input_port,},
97 {"int21", INT21_PORT, 0, input_port,},
98 {"int22", INT22_PORT, 0, input_port,},
99 {"int23", INT23_PORT, 0, input_port,},
100 {"int24", INT24_PORT, 0, input_port,},
101 {"int25", INT25_PORT, 0, input_port,},
102 {"int26", INT26_PORT, 0, input_port,},
103 {"int27", INT27_PORT, 0, input_port,},
104 {"int28", INT28_PORT, 0, input_port,},
105 {"int29", INT29_PORT, 0, input_port,},
106 {"int30", INT30_PORT, 0, input_port,},
107 {"int31", INT31_PORT, 0, input_port,},
114 * Finish off the partially created hw device. Attach our local
115 * callbacks. Wire up our port names etc.
117 static hw_port_event_method lm32cpu_port_event;
121 lm32cpu_finish (struct hw *me)
123 struct lm32cpu *controller;
125 controller = HW_ZALLOC (me, struct lm32cpu);
126 set_hw_data (me, controller);
127 set_hw_ports (me, lm32cpu_ports);
128 set_hw_port_event (me, lm32cpu_port_event);
130 /* Initialize the pending interrupt flags. */
131 controller->event = NULL;
135 /* An event arrives on an interrupt port. */
136 static unsigned int s_ui_ExtIntrs = 0;
140 deliver_lm32cpu_interrupt (struct hw *me, void *data)
142 static unsigned int ip, im, im_and_ip_result;
143 struct lm32cpu *controller = hw_data (me);
144 SIM_DESC sd = hw_system (me);
145 sim_cpu *cpu = STATE_CPU (sd, 0); /* NB: fix CPU 0. */
146 address_word cia = CPU_PC_GET (cpu);
147 int interrupt = (uintptr_t) data;
150 HW_TRACE ((me, "interrupt-check event"));
154 * Determine if an external interrupt is active
155 * and needs to cause an exception.
157 im = lm32bf_h_csr_get (cpu, LM32_CSR_IM);
158 ip = lm32bf_h_csr_get (cpu, LM32_CSR_IP);
159 im_and_ip_result = im & ip;
162 if ((lm32bf_h_csr_get (cpu, LM32_CSR_IE) & 1) && (im_and_ip_result != 0))
164 /* Save PC in exception address register. */
165 lm32bf_h_gr_set (cpu, 30, lm32bf_h_pc_get (cpu));
166 /* Restart at interrupt offset in handler exception table. */
167 lm32bf_h_pc_set (cpu,
168 lm32bf_h_csr_get (cpu,
170 LM32_EID_INTERRUPT * 32);
171 /* Save interrupt enable and then clear. */
172 lm32bf_h_csr_set (cpu, LM32_CSR_IE, 0x2);
175 /* reschedule soon. */
176 if (controller->event != NULL)
177 hw_event_queue_deschedule (me, controller->event);
178 controller->event = NULL;
181 /* if there are external interrupts, schedule an interrupt-check again.
182 * NOTE: THIS MAKES IT VERY INEFFICIENT. INSTEAD, TRIGGER THIS
183 * CHECk_EVENT WHEN THE USER ENABLES IE OR USER MODIFIES IM REGISTERS.
185 if (s_ui_ExtIntrs != 0)
187 hw_event_queue_schedule (me, 1, deliver_lm32cpu_interrupt, data);
192 /* Handle an event on one of the CPU's ports. */
194 lm32cpu_port_event (struct hw *me,
196 struct hw *source, int source_port, int level)
198 struct lm32cpu *controller = hw_data (me);
199 SIM_DESC sd = hw_system (me);
200 sim_cpu *cpu = STATE_CPU (sd, 0); /* NB: fix CPU 0. */
201 address_word cia = CPU_PC_GET (cpu);
204 HW_TRACE ((me, "interrupt event on port %d, level %d", my_port, level));
209 * Activate IP if the interrupt's activated; don't do anything if
210 * the interrupt's deactivated.
215 * save state of external interrupt.
217 s_ui_ExtIntrs |= (1 << my_port);
219 /* interrupt-activated so set IP. */
220 lm32bf_h_csr_set (cpu, LM32_CSR_IP,
221 lm32bf_h_csr_get (cpu, LM32_CSR_IP) | (1 << my_port));
224 * Since interrupt is activated, queue an immediate event
225 * to check if this interrupt is serviceable.
227 if (controller->event != NULL)
228 hw_event_queue_deschedule (me, controller->event);
232 * Queue an immediate event to check if this interrupt must be serviced;
233 * this will happen after the current instruction is complete.
235 controller->event = hw_event_queue_schedule (me,
237 deliver_lm32cpu_interrupt,
243 * save state of external interrupt.
245 s_ui_ExtIntrs &= ~(1 << my_port);
250 const struct hw_descriptor dv_lm32cpu_descriptor[] = {
251 {"lm32cpu", lm32cpu_finish,},