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
37 void avr_init_irq(avr_irq_t * irq, uint32_t base, uint32_t count)
39 memset(irq, 0, sizeof(avr_irq_t) * count);
41 for (int i = 0; i < count; i++)
42 irq[i].irq = base + i;
45 avr_irq_t * avr_alloc_irq(uint32_t base, uint32_t count)
47 avr_irq_t * irq = (avr_irq_t*)malloc(sizeof(avr_irq_t) * count);
48 avr_init_irq(irq, base, count);
49 for (int i = 0; i < count; i++)
50 irq[i].flags |= IRQ_FLAG_ALLOC;
54 static avr_irq_hook_t * _avr_alloc_irq_hook(avr_irq_t * irq)
56 avr_irq_hook_t *hook = malloc(sizeof(avr_irq_hook_t));
57 memset(hook, 0, sizeof(avr_irq_hook_t));
58 hook->next = irq->hook;
63 void avr_free_irq(avr_irq_t * irq, uint32_t count)
65 for (int i = 0; i < count; i++) {
67 avr_irq_hook_t *hook = irq->hook;
69 avr_irq_hook_t * next = hook->next;
75 // if that irq list was allocated by us, free it
76 if (irq->flags & IRQ_FLAG_ALLOC)
80 void avr_irq_register_notify(avr_irq_t * irq, avr_irq_notify_t notify, void * param)
85 avr_irq_hook_t *hook = irq->hook;
87 if (hook->notify == notify && hook->param == param)
88 return; // already there
91 hook = _avr_alloc_irq_hook(irq);
92 hook->notify = notify;
96 void avr_raise_irq(avr_irq_t * irq, uint32_t value)
100 uint32_t output = (irq->flags & IRQ_FLAG_NOT) ? !value : value;
101 if (irq->value == output && (irq->flags & IRQ_FLAG_FILTERED))
103 avr_irq_hook_t *hook = irq->hook;
105 avr_irq_hook_t * next = hook->next;
106 // prevents reentrance / endless calling loops
107 if (hook->busy == 0) {
110 hook->notify(irq, output, hook->param);
112 avr_raise_irq(hook->chain, output);
117 // the value is set after the callbacks are called, so the callbacks
118 // can themselves compare for old/new values between their parameter
119 // they are passed (new value) and the previous irq->value
123 void avr_connect_irq(avr_irq_t * src, avr_irq_t * dst)
125 if (!src || !dst || src == dst) {
126 printf("avr_connect_irq invalid irq %p/%p", src, dst);
129 avr_irq_hook_t *hook = src->hook;
131 if (hook->chain == dst)
132 return; // already there
135 hook = _avr_alloc_irq_hook(src);