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 avr->vector[vector->vector] = vector;
35 int avr_has_pending_interrupts(avr_t * avr)
37 return avr->pending[0] || avr->pending[1];
40 int avr_is_interrupt_pending(avr_t * avr, avr_int_vector_t * vector)
42 return avr->pending[vector->vector >> 5] & (1 << (vector->vector & 0x1f));
45 int avr_raise_interrupt(avr_t * avr, avr_int_vector_t * vector)
47 if (!vector || !vector->vector)
50 printf("%s raising %d\n", __FUNCTION__, vector->vector);
51 // always mark the 'raised' flag to one, even if the interrupt is disabled
52 // this allow "pooling" for the "raised" flag, like for non-interrupt
53 // driven UART and so so. These flags are often "write one to clear"
54 if (vector->raised.reg)
55 avr_regbit_set(avr, vector->raised);
56 if (vector->enable.reg) {
57 if (!avr_regbit_get(avr, vector->enable))
60 if (!avr_is_interrupt_pending(avr, vector)) {
61 if (!avr->pending_wait)
62 avr->pending_wait = 2; // latency on interrupts ??
63 avr->pending[vector->vector >> 5] |= (1 << (vector->vector & 0x1f));
65 if (avr->state != cpu_Running) {
67 printf("Waking CPU due to interrupt\n");
68 avr->state = cpu_Running; // in case we were sleeping
71 // return 'raised' even if it was already pending
75 void avr_clear_interrupt(avr_t * avr, int v)
77 avr_int_vector_t * vector = avr->vector[v];
78 avr->pending[v >> 5] &= ~(1 << (v & 0x1f));
82 printf("%s cleared %d\n", __FUNCTION__, vector->vector);
83 if (vector->raised.reg)
84 avr_regbit_clear(avr, vector->raised);
88 * check wether interrupts are pending. I so, check if the interrupt "latency" is reached,
89 * and if so triggers the handlers and jump to the vector.
91 void avr_service_interrupts(avr_t * avr)
96 if (avr_has_pending_interrupts(avr)) {
97 if (avr->pending_wait) {
99 if (avr->pending_wait == 0) {
101 for (int bi = 0; bi < 2 && !done; bi++) if (avr->pending[bi]) {
102 for (int ii = 0; ii < 32 && !done; ii++)
103 if (avr->pending[bi] & (1 << ii)) {
105 int v = (bi * 32) + ii; // vector
107 if (avr->vector[v] && avr->vector[v]->trace)
108 printf("%s calling %d\n", __FUNCTION__, v);
109 _avr_push16(avr, avr->pc >> 1);
111 avr->pc = v * avr->vector_size;
113 avr_clear_interrupt(avr, v);
121 avr->pending_wait = 2; // for next one...