8faff3f3ff534dd9bf818dc84ca4af84e55fb74a
[simavr] / simavr / sim / sim_irq.c
1 /*
2         sim_irq.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
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.
12
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.
17
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/>.
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "sim_irq.h"
26
27
28 void avr_init_irq(avr_irq_t * irq, uint32_t base, uint32_t count)
29 {
30         memset(irq, 0, sizeof(avr_irq_t) * count);
31
32         for (int i = 0; i < count; i++)
33                 irq[i].irq = base + i;
34 }
35
36 avr_irq_t * avr_alloc_irq(uint32_t base, uint32_t count)
37 {
38         avr_irq_t * irq = (avr_irq_t*)malloc(sizeof(avr_irq_t) * count);
39         avr_init_irq(irq, base, count);
40         for (int i = 0; i < count; i++)
41                 irq[i].flags |= IRQ_FLAG_ALLOC; 
42         return irq;
43 }
44
45 static avr_irq_hook_t * _avr_alloc_irq_hook(avr_irq_t * irq)
46 {
47         avr_irq_hook_t *hook = malloc(sizeof(avr_irq_hook_t));
48         memset(hook, 0, sizeof(avr_irq_hook_t));
49         hook->next = irq->hook;
50         irq->hook = hook;
51         return hook;
52 }
53
54 void avr_free_irq(avr_irq_t * irq, uint32_t count)
55 {
56         for (int i = 0; i < count; i++) {
57                 // purge hooks
58                 avr_irq_hook_t *hook = irq->hook;
59                 while (hook) {
60                         avr_irq_hook_t * next = hook->next;
61                         free(hook);
62                         hook = next;
63                 }
64                 irq->hook = NULL;
65         }
66         // if that irq list was allocated by us, free it
67         if (irq->flags & IRQ_FLAG_ALLOC)
68                 free(irq);
69 }
70
71 void avr_irq_register_notify(avr_irq_t * irq, avr_irq_notify_t notify, void * param)
72 {
73         if (!irq || !notify)
74                 return;
75         
76         avr_irq_hook_t *hook = irq->hook;
77         while (hook) {
78                 if (hook->notify == notify && hook->param == param)
79                         return; // already there
80                 hook = hook->next;
81         }
82         hook = _avr_alloc_irq_hook(irq);
83         hook->notify = notify;
84         hook->param = param;
85 }
86
87 void avr_raise_irq(avr_irq_t * irq, uint32_t value)
88 {
89         if (!irq)
90                 return ;
91         uint32_t output = (irq->flags & IRQ_FLAG_NOT) ? !value : value;
92         if (irq->value == output && (irq->flags & IRQ_FLAG_FILTERED))
93                 return;
94         avr_irq_hook_t *hook = irq->hook;
95         while (hook) {
96                 avr_irq_hook_t * next = hook->next;
97                         // prevents reentrance / endless calling loops
98                 if (hook->busy == 0) {
99                         hook->busy++;
100                         if (hook->notify)
101                                 hook->notify(irq, output,  hook->param);
102                         if (hook->chain)
103                                 avr_raise_irq(hook->chain, output);
104                         hook->busy--;
105                 }                       
106                 hook = next;
107         }
108         // the value is set after the callbacks are called, so the callbacks
109         // can themselves compare for old/new values between their parameter
110         // they are passed (new value) and the previous irq->value
111         irq->value = output;
112 }
113
114 void avr_connect_irq(avr_irq_t * src, avr_irq_t * dst)
115 {
116         if (!src || !dst || src == dst) {
117                 printf("avr_connect_irq invalid irq %p/%p", src, dst);
118                 return;
119         }
120         avr_irq_hook_t *hook = src->hook;
121         while (hook) {
122                 if (hook->chain == dst)
123                         return; // already there
124                 hook = hook->next;
125         }
126         hook = _avr_alloc_irq_hook(src);
127         hook->chain = dst;
128 }