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/>.
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
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
39 avr_irq_pool_t * pool,
42 if ((pool->count & 0xf) == 0) {
43 pool->irq = (avr_irq_t**)realloc(pool->irq,
44 (pool->count + 16) * sizeof(avr_irq_t *));
46 pool->irq[pool->count++] = irq;
52 avr_irq_pool_t * pool,
55 for (int i = 0; i < pool->count; i++)
56 if (pool->irq[i] == irq) {
64 avr_irq_pool_t * pool,
68 const char ** names /* optional */)
70 memset(irq, 0, sizeof(avr_irq_t) * count);
72 for (int i = 0; i < count; i++) {
73 irq[i].irq = base + i;
75 _avr_irq_pool_add(pool, &irq[i]);
76 if (names && names[i])
77 irq[i].name = strdup(names[i]);
83 avr_irq_pool_t * pool,
86 const char ** names /* optional */)
88 avr_irq_t * irq = (avr_irq_t*)malloc(sizeof(avr_irq_t) * count);
89 avr_init_irq(pool, irq, base, count, names);
90 for (int i = 0; i < count; i++)
91 irq[i].flags |= IRQ_FLAG_ALLOC;
95 static avr_irq_hook_t *
99 avr_irq_hook_t *hook = malloc(sizeof(avr_irq_hook_t));
100 memset(hook, 0, sizeof(avr_irq_hook_t));
101 hook->next = irq->hook;
113 for (int i = 0; i < count; i++) {
114 avr_irq_t * iq = irq + i;
116 _avr_irq_pool_remove(iq->pool, iq);
118 free((char*)iq->name);
121 avr_irq_hook_t *hook = iq->hook;
123 avr_irq_hook_t * next = hook->next;
129 // if that irq list was allocated by us, free it
130 if (irq->flags & IRQ_FLAG_ALLOC)
135 avr_irq_register_notify(
137 avr_irq_notify_t notify,
143 avr_irq_hook_t *hook = irq->hook;
145 if (hook->notify == notify && hook->param == param)
146 return; // already there
149 hook = _avr_alloc_irq_hook(irq);
150 hook->notify = notify;
155 avr_irq_unregister_notify(
157 avr_irq_notify_t notify,
160 avr_irq_hook_t *hook, *prev;
167 if (hook->notify == notify && hook->param == param)
170 prev->next = hook->next;
172 irq->hook = hook->next;
188 uint32_t output = (irq->flags & IRQ_FLAG_NOT) ? !value : value;
189 if (irq->value == output && (irq->flags & IRQ_FLAG_FILTERED))
191 avr_irq_hook_t *hook = irq->hook;
193 avr_irq_hook_t * next = hook->next;
194 // prevents reentrance / endless calling loops
195 if (hook->busy == 0) {
198 hook->notify(irq, output, hook->param);
200 avr_raise_irq(hook->chain, output);
205 // the value is set after the callbacks are called, so the callbacks
206 // can themselves compare for old/new values between their parameter
207 // they are passed (new value) and the previous irq->value
216 if (!src || !dst || src == dst) {
217 printf("avr_connect_irq invalid irq %p/%p", src, dst);
220 avr_irq_hook_t *hook = src->hook;
222 if (hook->chain == dst)
223 return; // already there
226 hook = _avr_alloc_irq_hook(src);
235 avr_irq_hook_t *hook, *prev;
237 if (!src || !dst || src == dst) {
238 printf("error: avr_connect_irq invalid irq %p/%p", src, dst); fflush(stdout);
244 if (hook->chain == dst)
247 prev->next = hook->next;
249 src->hook = hook->next;