f0d44e64dc4bbb42f624530caf2d0ae7ed0901ad
[simavr] / simavr / sim / sim_io.h
1 /*
2         sim_io.h
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 #ifndef __SIM_IO_H__
23 #define __SIM_IO_H__
24
25 #include "sim_avr.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /*
32  * used by the ioports to implement their own features
33  * see avr_eeprom.* for an example, and avr_ioctl().
34  */
35 #define AVR_IOCTL_DEF(_a,_b,_c,_d) \
36         (((_a) << 24)|((_b) << 16)|((_c) << 8)|((_d)))
37
38 /*
39  * IO module base struct
40  * Modules uses that as their first member in their own struct
41  */
42 typedef struct avr_io_t {
43         struct avr_io_t *       next;
44         avr_t *                         avr;            // avr we are attached to
45         const char *            kind;           // pretty name, for debug
46
47         const char ** irq_names;                // IRQ names
48
49         uint32_t                        irq_ioctl_get;  // used to get irqs from this module
50         int                                     irq_count;      // number of (optional) irqs
51         struct avr_irq_t *      irq;            // optional external IRQs
52         // called at reset time
53         void (*reset)(struct avr_io_t *io);
54         // called externally. allow access to io modules and so on
55         int (*ioctl)(struct avr_io_t *io, uint32_t ctl, void *io_param);
56
57         // optional, a function to free up allocated system resources
58         void (*dealloc)(struct avr_io_t *io);
59 } avr_io_t;
60
61 /*
62  * IO modules helper functions
63  */
64
65 // registers an IO module, so it's run(), reset() etc are called
66 // this is called by the AVR core init functions, you /could/ register an external
67 // one after instantiation, for whatever purpose...
68 void
69 avr_register_io(
70                 avr_t *avr,
71                 avr_io_t * io);
72 // Sets an IO module "official" IRQs and the ioctl used to get to them. if 'irqs' is NULL,
73 // 'count' will be allocated
74 avr_irq_t *
75 avr_io_setirqs(
76                 avr_io_t * io,
77                 uint32_t ctl,
78                 int count,
79                 avr_irq_t * irqs );
80
81 // register a callback for when IO register "addr" is read
82 void
83 avr_register_io_read(
84                 avr_t *avr,
85                 avr_io_addr_t addr,
86                 avr_io_read_t read,
87                 void * param);
88 // register a callback for when the IO register is written. callback has to set the memory itself
89 void
90 avr_register_io_write(
91                 avr_t *avr,
92                 avr_io_addr_t addr,
93                 avr_io_write_t write,
94                 void * param);
95 // call every IO modules until one responds to this
96 int
97 avr_ioctl(
98                 avr_t *avr,
99                 uint32_t ctl,
100                 void * io_param);
101 // get the specific irq for a module, check AVR_IOCTL_IOPORT_GETIRQ for example
102 struct avr_irq_t * avr_io_getirq(avr_t * avr, uint32_t ctl, int index);
103
104 // get the IRQ for an absolute IO address
105 // this allows any code to hook an IRQ in any io address, for example
106 // tracing changes of values into a register
107 // Note that the values do not "magically" change, they change only
108 // when the AVR code attempt to read and write at that address
109 // 
110 // the "index" is a bit number, or ALL bits if index == 8
111 #define AVR_IOMEM_IRQ_ALL 8
112 avr_irq_t *
113 avr_iomem_getirq(
114                 avr_t * avr,
115                 avr_io_addr_t addr,
116                 int index);
117
118 // Terminates all IOs and remove from them from the io chain
119 void
120 avr_deallocate_ios(
121                 avr_t *avr);
122
123 #ifdef __cplusplus
124 };
125 #endif
126
127 #endif /* __SIM_IO_H__ */