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