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)
67 for (int i = 0; i < count; i++) {
69 avr_irq_hook_t *hook = irq->hook;
71 avr_irq_hook_t * next = hook->next;
77 // if that irq list was allocated by us, free it
78 if (irq->flags & IRQ_FLAG_ALLOC)
82 void avr_irq_register_notify(avr_irq_t * irq, avr_irq_notify_t notify, void * param)
87 avr_irq_hook_t *hook = irq->hook;
89 if (hook->notify == notify && hook->param == param)
90 return; // already there
93 hook = _avr_alloc_irq_hook(irq);
94 hook->notify = notify;
98 void avr_raise_irq(avr_irq_t * irq, uint32_t value)
102 uint32_t output = (irq->flags & IRQ_FLAG_NOT) ? !value : value;
103 if (irq->value == output && (irq->flags & IRQ_FLAG_FILTERED))
105 avr_irq_hook_t *hook = irq->hook;
107 avr_irq_hook_t * next = hook->next;
108 // prevents reentrance / endless calling loops
109 if (hook->busy == 0) {
112 hook->notify(irq, output, hook->param);
114 avr_raise_irq(hook->chain, output);
119 // the value is set after the callbacks are called, so the callbacks
120 // can themselves compare for old/new values between their parameter
121 // they are passed (new value) and the previous irq->value
125 void avr_connect_irq(avr_irq_t * src, avr_irq_t * dst)
127 if (!src || !dst || src == dst) {
128 printf("avr_connect_irq invalid irq %p/%p", src, dst);
131 avr_irq_hook_t *hook = src->hook;
133 if (hook->chain == dst)
134 return; // already there
137 hook = _avr_alloc_irq_hook(src);