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,
43 const char ** names /* optional */)
45 memset(irq, 0, sizeof(avr_irq_t) * count);
47 for (int i = 0; i < count; i++) {
48 irq[i].irq = base + i;
54 avr_irq_pool_t * pool,
57 const char ** names /* optional */)
59 avr_irq_t * irq = (avr_irq_t*)malloc(sizeof(avr_irq_t) * count);
60 avr_init_irq(pool, irq, base, count, names);
61 for (int i = 0; i < count; i++)
62 irq[i].flags |= IRQ_FLAG_ALLOC;
66 static avr_irq_hook_t *
70 avr_irq_hook_t *hook = malloc(sizeof(avr_irq_hook_t));
71 memset(hook, 0, sizeof(avr_irq_hook_t));
72 hook->next = irq->hook;
84 for (int i = 0; i < count; i++) {
86 avr_irq_hook_t *hook = irq->hook;
88 avr_irq_hook_t * next = hook->next;
94 // if that irq list was allocated by us, free it
95 if (irq->flags & IRQ_FLAG_ALLOC)
100 avr_irq_register_notify(
102 avr_irq_notify_t notify,
108 avr_irq_hook_t *hook = irq->hook;
110 if (hook->notify == notify && hook->param == param)
111 return; // already there
114 hook = _avr_alloc_irq_hook(irq);
115 hook->notify = notify;
126 uint32_t output = (irq->flags & IRQ_FLAG_NOT) ? !value : value;
127 if (irq->value == output && (irq->flags & IRQ_FLAG_FILTERED))
129 avr_irq_hook_t *hook = irq->hook;
131 avr_irq_hook_t * next = hook->next;
132 // prevents reentrance / endless calling loops
133 if (hook->busy == 0) {
136 hook->notify(irq, output, hook->param);
138 avr_raise_irq(hook->chain, output);
143 // the value is set after the callbacks are called, so the callbacks
144 // can themselves compare for old/new values between their parameter
145 // they are passed (new value) and the previous irq->value
154 if (!src || !dst || src == dst) {
155 printf("avr_connect_irq invalid irq %p/%p", src, dst);
158 avr_irq_hook_t *hook = src->hook;
160 if (hook->chain == dst)
161 return; // already there
164 hook = _avr_alloc_irq_hook(src);