Statistics
| Branch: | Revision:

root / ioport.c @ d56dd6cf

History | View | Annotate | Download (7.1 kB)

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
/* size is the word size in byte */
125
int register_ioport_read(int start, int length, int size,
126
                         IOPortReadFunc *func, void *opaque)
127
{
128
    int i, bsize;
129

    
130
    if (size == 1) {
131
        bsize = 0;
132
    } else if (size == 2) {
133
        bsize = 1;
134
    } else if (size == 4) {
135
        bsize = 2;
136
    } else {
137
        hw_error("register_ioport_read: invalid size");
138
        return -1;
139
    }
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;
145
    }
146
    return 0;
147
}
148

    
149
/* size is the word size in byte */
150
int register_ioport_write(int start, int length, int size,
151
                          IOPortWriteFunc *func, void *opaque)
152
{
153
    int i, bsize;
154

    
155
    if (size == 1) {
156
        bsize = 0;
157
    } else if (size == 2) {
158
        bsize = 1;
159
    } else if (size == 4) {
160
        bsize = 2;
161
    } else {
162
        hw_error("register_ioport_write: invalid size");
163
        return -1;
164
    }
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;
170
    }
171
    return 0;
172
}
173

    
174
void isa_unassign_ioport(int start, int length)
175
{
176
    int i;
177

    
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;
182

    
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;
186

    
187
        ioport_opaque[i] = NULL;
188
    }
189
}
190

    
191
/***********************************************************/
192

    
193
void cpu_outb(CPUState *env, int addr, int val)
194
{
195
    LOG_IOPORT("outb: %04x %02x\n", addr, val);
196
    ioport_write(0, addr, val);
197
#ifdef CONFIG_KQEMU
198
    if (env)
199
        env->last_io_time = cpu_get_time_fast();
200
#endif
201
}
202

    
203
void cpu_outw(CPUState *env, int addr, int val)
204
{
205
    LOG_IOPORT("outw: %04x %04x\n", addr, val);
206
    ioport_write(1, addr, val);
207
#ifdef CONFIG_KQEMU
208
    if (env)
209
        env->last_io_time = cpu_get_time_fast();
210
#endif
211
}
212

    
213
void cpu_outl(CPUState *env, int addr, int val)
214
{
215
    LOG_IOPORT("outl: %04x %08x\n", addr, val);
216
    ioport_write(2, addr, val);
217
#ifdef CONFIG_KQEMU
218
    if (env)
219
        env->last_io_time = cpu_get_time_fast();
220
#endif
221
}
222

    
223
int cpu_inb(CPUState *env, int addr)
224
{
225
    int val;
226
    val = ioport_read(0, addr);
227
    LOG_IOPORT("inb : %04x %02x\n", addr, val);
228
#ifdef CONFIG_KQEMU
229
    if (env)
230
        env->last_io_time = cpu_get_time_fast();
231
#endif
232
    return val;
233
}
234

    
235
int cpu_inw(CPUState *env, int addr)
236
{
237
    int val;
238
    val = ioport_read(1, addr);
239
    LOG_IOPORT("inw : %04x %04x\n", addr, val);
240
#ifdef CONFIG_KQEMU
241
    if (env)
242
        env->last_io_time = cpu_get_time_fast();
243
#endif
244
    return val;
245
}
246

    
247
int cpu_inl(CPUState *env, int addr)
248
{
249
    int val;
250
    val = ioport_read(2, addr);
251
    LOG_IOPORT("inl : %04x %08x\n", addr, val);
252
#ifdef CONFIG_KQEMU
253
    if (env)
254
        env->last_io_time = cpu_get_time_fast();
255
#endif
256
    return val;
257
}
258