]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - ioport.c
ioport: consolidate duplicated logic in register_ioport_{read, write}().
[lisovros/qemu_apohw.git] / ioport.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 /*
25  * splitted out ioport related stuffs from vl.c.
26  */
27
28 #include "ioport.h"
29
30 /***********************************************************/
31 /* IO Port */
32
33 //#define DEBUG_UNUSED_IOPORT
34 //#define DEBUG_IOPORT
35
36 #ifdef DEBUG_IOPORT
37 #  define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
38 #else
39 #  define LOG_IOPORT(...) do { } while (0)
40 #endif
41
42 /* XXX: use a two level table to limit memory usage */
43
44 static void *ioport_opaque[MAX_IOPORTS];
45 static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
46 static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
47
48 static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
49 static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
50
51 static uint32_t ioport_read(int index, uint32_t address)
52 {
53     static IOPortReadFunc *default_func[3] = {
54         default_ioport_readb,
55         default_ioport_readw,
56         default_ioport_readl
57     };
58     IOPortReadFunc *func = ioport_read_table[index][address];
59     if (!func)
60         func = default_func[index];
61     return func(ioport_opaque[address], address);
62 }
63
64 static void ioport_write(int index, uint32_t address, uint32_t data)
65 {
66     static IOPortWriteFunc *default_func[3] = {
67         default_ioport_writeb,
68         default_ioport_writew,
69         default_ioport_writel
70     };
71     IOPortWriteFunc *func = ioport_write_table[index][address];
72     if (!func)
73         func = default_func[index];
74     func(ioport_opaque[address], address, data);
75 }
76
77 static uint32_t default_ioport_readb(void *opaque, uint32_t address)
78 {
79 #ifdef DEBUG_UNUSED_IOPORT
80     fprintf(stderr, "unused inb: port=0x%04x\n", address);
81 #endif
82     return 0xff;
83 }
84
85 static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
86 {
87 #ifdef DEBUG_UNUSED_IOPORT
88     fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
89 #endif
90 }
91
92 /* default is to make two byte accesses */
93 static uint32_t default_ioport_readw(void *opaque, uint32_t address)
94 {
95     uint32_t data;
96     data = ioport_read(0, address);
97     address = (address + 1) & IOPORTS_MASK;
98     data |= ioport_read(0, address) << 8;
99     return data;
100 }
101
102 static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
103 {
104     ioport_write(0, address, data & 0xff);
105     address = (address + 1) & IOPORTS_MASK;
106     ioport_write(0, address, (data >> 8) & 0xff);
107 }
108
109 static uint32_t default_ioport_readl(void *opaque, uint32_t address)
110 {
111 #ifdef DEBUG_UNUSED_IOPORT
112     fprintf(stderr, "unused inl: port=0x%04x\n", address);
113 #endif
114     return 0xffffffff;
115 }
116
117 static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
118 {
119 #ifdef DEBUG_UNUSED_IOPORT
120     fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
121 #endif
122 }
123
124 static int ioport_bsize(int size, int *bsize)
125 {
126     if (size == 1) {
127         *bsize = 0;
128     } else if (size == 2) {
129         *bsize = 1;
130     } else if (size == 4) {
131         *bsize = 2;
132     } else {
133         return -1;
134     }
135     return 0;
136 }
137
138 /* size is the word size in byte */
139 int register_ioport_read(int start, int length, int size,
140                          IOPortReadFunc *func, void *opaque)
141 {
142     int i, bsize;
143
144     if (ioport_bsize(size, &bsize)) {
145         hw_error("register_ioport_read: invalid size");
146         return -1;
147     }
148     for(i = start; i < start + length; i += size) {
149         ioport_read_table[bsize][i] = func;
150         if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
151             hw_error("register_ioport_read: invalid opaque");
152         ioport_opaque[i] = opaque;
153     }
154     return 0;
155 }
156
157 /* size is the word size in byte */
158 int register_ioport_write(int start, int length, int size,
159                           IOPortWriteFunc *func, void *opaque)
160 {
161     int i, bsize;
162
163     if (ioport_bsize(size, &bsize)) {
164         hw_error("register_ioport_write: invalid size");
165         return -1;
166     }
167     for(i = start; i < start + length; i += size) {
168         ioport_write_table[bsize][i] = func;
169         if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
170             hw_error("register_ioport_write: invalid opaque");
171         ioport_opaque[i] = opaque;
172     }
173     return 0;
174 }
175
176 void isa_unassign_ioport(int start, int length)
177 {
178     int i;
179
180     for(i = start; i < start + length; i++) {
181         ioport_read_table[0][i] = default_ioport_readb;
182         ioport_read_table[1][i] = default_ioport_readw;
183         ioport_read_table[2][i] = default_ioport_readl;
184
185         ioport_write_table[0][i] = default_ioport_writeb;
186         ioport_write_table[1][i] = default_ioport_writew;
187         ioport_write_table[2][i] = default_ioport_writel;
188
189         ioport_opaque[i] = NULL;
190     }
191 }
192
193 /***********************************************************/
194
195 void cpu_outb(CPUState *env, int addr, int val)
196 {
197     LOG_IOPORT("outb: %04x %02x\n", addr, val);
198     ioport_write(0, addr, val);
199 #ifdef CONFIG_KQEMU
200     if (env)
201         env->last_io_time = cpu_get_time_fast();
202 #endif
203 }
204
205 void cpu_outw(CPUState *env, int addr, int val)
206 {
207     LOG_IOPORT("outw: %04x %04x\n", addr, val);
208     ioport_write(1, addr, val);
209 #ifdef CONFIG_KQEMU
210     if (env)
211         env->last_io_time = cpu_get_time_fast();
212 #endif
213 }
214
215 void cpu_outl(CPUState *env, int addr, int val)
216 {
217     LOG_IOPORT("outl: %04x %08x\n", addr, val);
218     ioport_write(2, addr, val);
219 #ifdef CONFIG_KQEMU
220     if (env)
221         env->last_io_time = cpu_get_time_fast();
222 #endif
223 }
224
225 int cpu_inb(CPUState *env, int addr)
226 {
227     int val;
228     val = ioport_read(0, addr);
229     LOG_IOPORT("inb : %04x %02x\n", addr, val);
230 #ifdef CONFIG_KQEMU
231     if (env)
232         env->last_io_time = cpu_get_time_fast();
233 #endif
234     return val;
235 }
236
237 int cpu_inw(CPUState *env, int addr)
238 {
239     int val;
240     val = ioport_read(1, addr);
241     LOG_IOPORT("inw : %04x %04x\n", addr, val);
242 #ifdef CONFIG_KQEMU
243     if (env)
244         env->last_io_time = cpu_get_time_fast();
245 #endif
246     return val;
247 }
248
249 int cpu_inl(CPUState *env, int addr)
250 {
251     int val;
252     val = ioport_read(2, addr);
253     LOG_IOPORT("inl : %04x %08x\n", addr, val);
254 #ifdef CONFIG_KQEMU
255     if (env)
256         env->last_io_time = cpu_get_time_fast();
257 #endif
258     return val;
259 }
260