4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 * splitted out ioport related stuffs from vl.c.
30 /***********************************************************/
33 //#define DEBUG_UNUSED_IOPORT
34 //#define DEBUG_IOPORT
37 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
39 # define LOG_IOPORT(...) do { } while (0)
42 /* XXX: use a two level table to limit memory usage */
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
];
48 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
49 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
51 static uint32_t ioport_read(int index
, uint32_t address
)
53 static IOPortReadFunc
*default_func
[3] = {
58 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
60 func
= default_func
[index
];
61 return func(ioport_opaque
[address
], address
);
64 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
66 static IOPortWriteFunc
*default_func
[3] = {
67 default_ioport_writeb
,
68 default_ioport_writew
,
71 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
73 func
= default_func
[index
];
74 func(ioport_opaque
[address
], address
, data
);
77 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
79 #ifdef DEBUG_UNUSED_IOPORT
80 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
85 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
87 #ifdef DEBUG_UNUSED_IOPORT
88 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
92 /* default is to make two byte accesses */
93 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
96 data
= ioport_read(0, address
);
97 address
= (address
+ 1) & IOPORTS_MASK
;
98 data
|= ioport_read(0, address
) << 8;
102 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
104 ioport_write(0, address
, data
& 0xff);
105 address
= (address
+ 1) & IOPORTS_MASK
;
106 ioport_write(0, address
, (data
>> 8) & 0xff);
109 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
111 #ifdef DEBUG_UNUSED_IOPORT
112 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
117 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
119 #ifdef DEBUG_UNUSED_IOPORT
120 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
124 /* size is the word size in byte */
125 int register_ioport_read(int start
, int length
, int size
,
126 IOPortReadFunc
*func
, void *opaque
)
132 } else if (size
== 2) {
134 } else if (size
== 4) {
137 hw_error("register_ioport_read: invalid size");
140 for(i
= start
; i
< start
+ length
; i
+= size
) {
141 ioport_read_table
[bsize
][i
] = func
;
142 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
143 hw_error("register_ioport_read: invalid opaque");
144 ioport_opaque
[i
] = opaque
;
149 /* size is the word size in byte */
150 int register_ioport_write(int start
, int length
, int size
,
151 IOPortWriteFunc
*func
, void *opaque
)
157 } else if (size
== 2) {
159 } else if (size
== 4) {
162 hw_error("register_ioport_write: invalid size");
165 for(i
= start
; i
< start
+ length
; i
+= size
) {
166 ioport_write_table
[bsize
][i
] = func
;
167 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
168 hw_error("register_ioport_write: invalid opaque");
169 ioport_opaque
[i
] = opaque
;
174 void isa_unassign_ioport(int start
, int length
)
178 for(i
= start
; i
< start
+ length
; i
++) {
179 ioport_read_table
[0][i
] = default_ioport_readb
;
180 ioport_read_table
[1][i
] = default_ioport_readw
;
181 ioport_read_table
[2][i
] = default_ioport_readl
;
183 ioport_write_table
[0][i
] = default_ioport_writeb
;
184 ioport_write_table
[1][i
] = default_ioport_writew
;
185 ioport_write_table
[2][i
] = default_ioport_writel
;
187 ioport_opaque
[i
] = NULL
;
191 /***********************************************************/
193 void cpu_outb(CPUState
*env
, int addr
, int val
)
195 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
196 ioport_write(0, addr
, val
);
199 env
->last_io_time
= cpu_get_time_fast();
203 void cpu_outw(CPUState
*env
, int addr
, int val
)
205 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
206 ioport_write(1, addr
, val
);
209 env
->last_io_time
= cpu_get_time_fast();
213 void cpu_outl(CPUState
*env
, int addr
, int val
)
215 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
216 ioport_write(2, addr
, val
);
219 env
->last_io_time
= cpu_get_time_fast();
223 int cpu_inb(CPUState
*env
, int addr
)
226 val
= ioport_read(0, addr
);
227 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
230 env
->last_io_time
= cpu_get_time_fast();
235 int cpu_inw(CPUState
*env
, int addr
)
238 val
= ioport_read(1, addr
);
239 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
242 env
->last_io_time
= cpu_get_time_fast();
247 int cpu_inl(CPUState
*env
, int addr
)
250 val
= ioport_read(2, addr
);
251 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
254 env
->last_io_time
= cpu_get_time_fast();