4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr 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 simavr 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 simavr. If not, see <http://www.gnu.org/licenses/>.
26 #include "sim_interrupts.h"
29 void avr_register_vector(avr_t *avr, avr_int_vector_t * vector)
32 vector->irq.irq = vector->vector;
33 avr->vector[vector->vector] = vector;
37 int avr_has_pending_interrupts(avr_t * avr)
39 return avr->pending[0] || avr->pending[1];
42 int avr_is_interrupt_pending(avr_t * avr, avr_int_vector_t * vector)
44 return avr->pending[vector->vector >> 5] & (1 << (vector->vector & 0x1f));
47 int avr_raise_interrupt(avr_t * avr, avr_int_vector_t * vector)
49 if (!vector || !vector->vector)
52 printf("%s raising %d\n", __FUNCTION__, vector->vector);
53 // always mark the 'raised' flag to one, even if the interrupt is disabled
54 // this allow "pooling" for the "raised" flag, like for non-interrupt
55 // driven UART and so so. These flags are often "write one to clear"
56 if (vector->raised.reg)
57 avr_regbit_set(avr, vector->raised);
58 if (vector->enable.reg) {
59 if (!avr_regbit_get(avr, vector->enable))
62 if (!avr_is_interrupt_pending(avr, vector)) {
63 if (!avr->pending_wait)
64 avr->pending_wait = 2; // latency on interrupts ??
65 avr->pending[vector->vector >> 5] |= (1 << (vector->vector & 0x1f));
66 avr_raise_irq(&vector->irq, 1);
68 if (avr->state != cpu_Running) {
70 printf("Waking CPU due to interrupt\n");
71 avr->state = cpu_Running; // in case we were sleeping
74 // return 'raised' even if it was already pending
78 void avr_clear_interrupt(avr_t * avr, int v)
80 avr_int_vector_t * vector = avr->vector[v];
81 avr->pending[v >> 5] &= ~(1 << (v & 0x1f));
85 printf("%s cleared %d\n", __FUNCTION__, vector->vector);
86 avr_raise_irq(&vector->irq, 0);
87 if (vector->raised.reg)
88 avr_regbit_clear(avr, vector->raised);
91 avr_irq_t * avr_get_interupt_irq(avr_t * avr, uint8_t v)
93 avr_int_vector_t * vector = avr->vector[v];
94 return vector ? &vector->irq : NULL;
98 * check wether interrupts are pending. I so, check if the interrupt "latency" is reached,
99 * and if so triggers the handlers and jump to the vector.
101 void avr_service_interrupts(avr_t * avr)
106 if (avr_has_pending_interrupts(avr)) {
107 if (avr->pending_wait) {
109 if (avr->pending_wait == 0) {
111 for (int bi = 0; bi < 2 && !done; bi++) if (avr->pending[bi]) {
112 for (int ii = 0; ii < 32 && !done; ii++)
113 if (avr->pending[bi] & (1 << ii)) {
115 int v = (bi * 32) + ii; // vector
117 if (avr->vector[v] && avr->vector[v]->trace)
118 printf("%s calling %d\n", __FUNCTION__, v);
119 _avr_push16(avr, avr->pc >> 1);
121 avr->pc = v * avr->vector_size;
123 avr_clear_interrupt(avr, v);
131 avr->pending_wait = 2; // for next one...