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;
74 irq[i].flags = IRQ_FLAG_INIT;
76 _avr_irq_pool_add(pool, &irq[i]);
77 if (names && names[i])
78 irq[i].name = strdup(names[i]);
84 avr_irq_pool_t * pool,
87 const char ** names /* optional */)
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;
96 static avr_irq_hook_t *
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;
114 for (int i = 0; i < count; i++) {
115 avr_irq_t * iq = irq + i;
117 _avr_irq_pool_remove(iq->pool, iq);
119 free((char*)iq->name);
122 avr_irq_hook_t *hook = iq->hook;
124 avr_irq_hook_t * next = hook->next;
130 // if that irq list was allocated by us, free it
131 if (irq->flags & IRQ_FLAG_ALLOC)
136 avr_irq_register_notify(
138 avr_irq_notify_t notify,
144 avr_irq_hook_t *hook = irq->hook;
146 if (hook->notify == notify && hook->param == param)
147 return; // already there
150 hook = _avr_alloc_irq_hook(irq);
151 hook->notify = notify;
156 avr_irq_unregister_notify(
158 avr_irq_notify_t notify,
161 avr_irq_hook_t *hook, *prev;
168 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 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))
193 irq->flags &= ~IRQ_FLAG_INIT;
194 avr_irq_hook_t *hook = irq->hook;
196 avr_irq_hook_t * next = hook->next;
197 // prevents reentrance / endless calling loops
198 if (hook->busy == 0) {
201 hook->notify(irq, output, hook->param);
203 avr_raise_irq(hook->chain, output);
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
219 if (!src || !dst || src == dst) {
220 fprintf(stderr, "error: %s invalid irq %p/%p", __FUNCTION__, src, dst);
223 avr_irq_hook_t *hook = src->hook;
225 if (hook->chain == dst)
226 return; // already there
229 hook = _avr_alloc_irq_hook(src);
238 avr_irq_hook_t *hook, *prev;
240 if (!src || !dst || src == dst) {
241 fprintf(stderr, "error: %s invalid irq %p/%p", __FUNCTION__, src, dst);
247 if (hook->chain == dst) {
249 prev->next = hook->next;
251 src->hook = hook->next;