misc: Point to correct simavr include dirs
[simavr] / simavr / sim / sim_irq.c
1 /*
2         sim_irq.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 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "sim_irq.h"
26
27 // internal structure for a hook, never seen by the notify procs
28 typedef struct avr_irq_hook_t {
29         struct avr_irq_hook_t * next;
30         int busy;       // prevent reentrance of callbacks
31         
32         struct avr_irq_t * chain;       // raise the IRQ on this too - optional if "notify" is on
33         avr_irq_notify_t notify;        // called when IRQ is raised - optional if "chain" is on
34         void * param;                           // "notify" parameter
35 } avr_irq_hook_t;
36
37 static void
38 _avr_irq_pool_add(
39                 avr_irq_pool_t * pool,
40                 avr_irq_t * irq)
41 {
42         if ((pool->count & 0xf) == 0) {
43                 pool->irq = (avr_irq_t**)realloc(pool->irq,
44                                 (pool->count + 16) * sizeof(avr_irq_t *));
45         }
46         pool->irq[pool->count++] = irq;
47         irq->pool = pool;
48 }
49
50 static void
51 _avr_irq_pool_remove(
52                 avr_irq_pool_t * pool,
53                 avr_irq_t * irq)
54 {
55         for (int i = 0; i < pool->count; i++)
56                 if (pool->irq[i] == irq) {
57                         pool->irq[i] = 0;
58                         return;
59                 }
60 }
61
62 void
63 avr_init_irq(
64                 avr_irq_pool_t * pool,
65                 avr_irq_t * irq,
66                 uint32_t base,
67                 uint32_t count,
68                 const char ** names /* optional */)
69 {
70         memset(irq, 0, sizeof(avr_irq_t) * count);
71
72         for (int i = 0; i < count; i++) {
73                 irq[i].irq = base + i;
74                 irq[i].flags = IRQ_FLAG_INIT;
75                 if (pool)
76                         _avr_irq_pool_add(pool, &irq[i]);
77                 if (names && names[i])
78                         irq[i].name = strdup(names[i]);
79         }
80 }
81
82 avr_irq_t *
83 avr_alloc_irq(
84                 avr_irq_pool_t * pool,
85                 uint32_t base,
86                 uint32_t count,
87                 const char ** names /* optional */)
88 {
89         avr_irq_t * irq = (avr_irq_t*)malloc(sizeof(avr_irq_t) * count);
90         avr_init_irq(pool, irq, base, count, names);
91         for (int i = 0; i < count; i++)
92                 irq[i].flags |= IRQ_FLAG_ALLOC; 
93         return irq;
94 }
95
96 static avr_irq_hook_t *
97 _avr_alloc_irq_hook(
98                 avr_irq_t * irq)
99 {
100         avr_irq_hook_t *hook = malloc(sizeof(avr_irq_hook_t));
101         memset(hook, 0, sizeof(avr_irq_hook_t));
102         hook->next = irq->hook;
103         irq->hook = hook;
104         return hook;
105 }
106
107 void
108 avr_free_irq(
109                 avr_irq_t * irq,
110                 uint32_t count)
111 {
112         if (!irq || !count)
113                 return;
114         for (int i = 0; i < count; i++) {
115                 avr_irq_t * iq = irq + i;
116                 if (iq->pool)
117                         _avr_irq_pool_remove(iq->pool, iq);
118                 if (iq->name)
119                         free((char*)iq->name);
120                 iq->name = NULL;
121                 // purge hooks
122                 avr_irq_hook_t *hook = iq->hook;
123                 while (hook) {
124                         avr_irq_hook_t * next = hook->next;
125                         free(hook);
126                         hook = next;
127                 }
128                 iq->hook = NULL;
129         }
130         // if that irq list was allocated by us, free it
131         if (irq->flags & IRQ_FLAG_ALLOC)
132                 free(irq);
133 }
134
135 void
136 avr_irq_register_notify(
137                 avr_irq_t * irq,
138                 avr_irq_notify_t notify,
139                 void * param)
140 {
141         if (!irq || !notify)
142                 return;
143         
144         avr_irq_hook_t *hook = irq->hook;
145         while (hook) {
146                 if (hook->notify == notify && hook->param == param)
147                         return; // already there
148                 hook = hook->next;
149         }
150         hook = _avr_alloc_irq_hook(irq);
151         hook->notify = notify;
152         hook->param = param;
153 }
154
155 void
156 avr_irq_unregister_notify(
157                 avr_irq_t * irq,
158                 avr_irq_notify_t notify,
159                 void * param)
160 {
161         avr_irq_hook_t *hook, *prev;
162         if (!irq || !notify)
163                 return;
164
165         hook = irq->hook;
166         prev = NULL;
167         while (hook) {
168                 if (hook->notify == notify && hook->param == param) {
169                         if ( prev )
170                                 prev->next = hook->next;
171                         else
172                                 irq->hook = hook->next;
173                         free(hook);
174                         return;
175                 }
176                 prev = hook;
177                 hook = hook->next;
178         }
179 }
180
181 void
182 avr_raise_irq(
183                 avr_irq_t * irq,
184                 uint32_t value)
185 {
186         if (!irq)
187                 return ;
188         uint32_t output = (irq->flags & IRQ_FLAG_NOT) ? !value : value;
189         // if value is the same but it's the first time, raise it anyway
190         if (irq->value == output &&
191                         (irq->flags & IRQ_FLAG_FILTERED) && !(irq->flags & IRQ_FLAG_INIT))
192                 return;
193         irq->flags &= ~IRQ_FLAG_INIT;
194         avr_irq_hook_t *hook = irq->hook;
195         while (hook) {
196                 avr_irq_hook_t * next = hook->next;
197                         // prevents reentrance / endless calling loops
198                 if (hook->busy == 0) {
199                         hook->busy++;
200                         if (hook->notify)
201                                 hook->notify(irq, output,  hook->param);
202                         if (hook->chain)
203                                 avr_raise_irq(hook->chain, output);
204                         hook->busy--;
205                 }                       
206                 hook = next;
207         }
208         // the value is set after the callbacks are called, so the callbacks
209         // can themselves compare for old/new values between their parameter
210         // they are passed (new value) and the previous irq->value
211         irq->value = output;
212 }
213
214 void
215 avr_connect_irq(
216                 avr_irq_t * src,
217                 avr_irq_t * dst)
218 {
219         if (!src || !dst || src == dst) {
220                 fprintf(stderr, "error: %s invalid irq %p/%p", __FUNCTION__, src, dst);
221                 return;
222         }
223         avr_irq_hook_t *hook = src->hook;
224         while (hook) {
225                 if (hook->chain == dst)
226                         return; // already there
227                 hook = hook->next;
228         }
229         hook = _avr_alloc_irq_hook(src);
230         hook->chain = dst;
231 }
232
233 void
234 avr_unconnect_irq(
235                 avr_irq_t * src,
236                 avr_irq_t * dst)
237 {
238         avr_irq_hook_t *hook, *prev;
239
240         if (!src || !dst || src == dst) {
241                 fprintf(stderr, "error: %s invalid irq %p/%p", __FUNCTION__, src, dst);
242                 return;
243         }
244         hook = src->hook;
245         prev = NULL;
246         while (hook) {
247                 if (hook->chain == dst) {
248                         if ( prev )
249                                 prev->next = hook->next;
250                         else
251                                 src->hook = hook->next;
252                         free(hook);
253                         return;
254                 }
255                 prev = hook;
256                 hook = hook->next;
257         }
258 }