cc4736f8f4d98c5453eee5cd61c62bd3c51f3156
[simavr] / simavr / sim / sim_interrupts.c
1 /*
2         sim_interrupts.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
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.
12
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.
17
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/>.
20  */
21
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "sim_interrupts.h"
27 #include "sim_core.h"
28
29 void avr_register_vector(avr_t *avr, avr_int_vector_t * vector)
30 {
31         if (vector->vector) {
32                 vector->irq.irq = vector->vector;
33                 avr->vector[vector->vector] = vector;
34                 if (vector->trace)
35                         printf("%s register vector %d (enabled %04x:%d)\n", __FUNCTION__, vector->vector, vector->enable.reg, vector->enable.bit);
36
37                 if (!vector->enable.reg)
38                         printf("avr_register_vector: No 'enable' bit on vector %d !\n", vector->vector);
39         }
40 }
41
42 int avr_has_pending_interrupts(avr_t * avr)
43 {
44         return avr->pending[0] || avr->pending[1];
45 }
46
47 int avr_is_interrupt_pending(avr_t * avr, avr_int_vector_t * vector)
48 {
49         return avr->pending[vector->vector >> 5] & (1 << (vector->vector & 0x1f));
50 }
51
52 int avr_is_interrupt_enabled(avr_t * avr, avr_int_vector_t * vector)
53 {
54         return avr_regbit_get(avr, vector->enable);
55 }
56
57 int avr_raise_interrupt(avr_t * avr, avr_int_vector_t * vector)
58 {
59         if (!vector || !vector->vector)
60                 return 0;
61         if (vector->trace)
62                 printf("%s raising %d (enabled %d)\n", __FUNCTION__, vector->vector, avr_regbit_get(avr, vector->enable));
63         // always mark the 'raised' flag to one, even if the interrupt is disabled
64         // this allow "pooling" for the "raised" flag, like for non-interrupt
65         // driven UART and so so. These flags are often "write one to clear"
66         if (vector->raised.reg)
67                 avr_regbit_set(avr, vector->raised);
68
69         // Mark the interrupt as pending
70         avr->pending[vector->vector >> 5] |= (1 << (vector->vector & 0x1f));
71         avr_raise_irq(&vector->irq, 1);
72
73         // If the interrupt is enabled, attempt to wake the core
74         if (avr_regbit_get(avr, vector->enable)) {
75                 if (!avr->pending_wait)
76                         avr->pending_wait = 1;          // latency on interrupts ??
77                 if (avr->state != cpu_Running) {
78                         if (vector->trace)
79                                 printf("Waking CPU due to interrupt\n");
80                         avr->state = cpu_Running;       // in case we were sleeping
81                 }
82         }
83         // return 'raised' even if it was already pending
84         return 1;
85 }
86
87 void avr_clear_interrupt(avr_t * avr, int v)
88 {
89         avr_int_vector_t * vector = avr->vector[v];
90         avr->pending[v >> 5] &= ~(1 << (v & 0x1f));
91         if (!vector)
92                 return;
93         if (vector->trace)
94                 printf("%s cleared %d\n", __FUNCTION__, vector->vector);
95         avr_raise_irq(&vector->irq, 0);
96         if (vector->raised.reg)
97                 avr_regbit_clear(avr, vector->raised);
98 }
99
100 int avr_clear_interupt_if(avr_t * avr, avr_int_vector_t * vector, uint8_t old)
101 {
102         if (avr_regbit_get(avr, vector->raised)) {
103                 avr_clear_interrupt(avr, vector->vector);
104                 avr_regbit_clear(avr, vector->raised);
105                 return 1;
106         }
107         avr_regbit_setto(avr, vector->raised, old);
108         return 0;
109 }
110
111 avr_irq_t * avr_get_interupt_irq(avr_t * avr, uint8_t v)
112 {
113         avr_int_vector_t * vector = avr->vector[v];
114         return vector ? &vector->irq : NULL;
115 }
116
117 /*
118  * check wether interrupts are pending. I so, check if the interrupt "latency" is reached,
119  * and if so triggers the handlers and jump to the vector.
120  */
121 void avr_service_interrupts(avr_t * avr)
122 {
123         if (!avr->sreg[S_I])
124                 return;
125
126         if (avr_has_pending_interrupts(avr)) {
127                 if (avr->pending_wait) {
128                         avr->pending_wait--;
129                         if (avr->pending_wait == 0) {
130                                 int done = 0;
131                                 for (int bi = 0; bi < 2 && !done; bi++) if (avr->pending[bi]) {
132                                         for (int ii = 0; ii < 32 && !done; ii++)
133                                                 if (avr->pending[bi] & (1 << ii)) {
134
135                                                         int v = (bi * 32) + ii; // vector
136                                                         avr_int_vector_t * vector = avr->vector[v];
137                                                         // if that single interupt is masked, ignore it and continue
138                                                         if (vector && !avr_regbit_get(avr, vector->enable))
139                                                                 continue;
140                                                         if (vector && vector->trace)
141                                                                 printf("%s calling %d\n", __FUNCTION__, v);
142                                                         _avr_push16(avr, avr->pc >> 1);
143                                                         avr->sreg[S_I] = 0;
144                                                         avr->pc = v * avr->vector_size;
145
146                                                         avr_clear_interrupt(avr, v);
147                                                         done++;
148                                                         break;
149                                                 }
150                                         break;
151                                 }
152                         }
153                 } else
154                         avr->pending_wait = 2;  // for next one...
155         }
156 }
157