1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1996-1999 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
9 * ========================================================================
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation for any purpose is hereby granted without fee,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation, and that the name of the authors not be used
16 * in advertising or publicity pertaining to distribution of the software
17 * without specific, written prior permission. The authors makes no
18 * representations about the suitability of this software for any purpose.
19 * It is provided "as is" without express or implied warranty.
21 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27 * PERFORMANCE OF THIS SOFTWARE.
29 * ========================================================================
33 * Developer: Kendall Bennett
35 * Description: This file includes subroutines which are related to
36 * programmed I/O and memory access. Included in this module
37 * are default functions with limited usefulness. For real
38 * uses these functions will most likely be overridden by the
41 ****************************************************************************/
44 #include <x86emu/x86emu.h>
45 #include <x86emu/regs.h>
46 #include <device/oprom/include/io.h>
51 #include "xf86_ansic.h"
54 /*------------------------- Global Variables ------------------------------*/
56 X86EMU_sysEnv _X86EMU_env
; /* Global emulator machine state */
57 X86EMU_intrFuncs _X86EMU_intrTab
[256];
59 /*----------------------------- Implementation ----------------------------*/
61 /* compute a pointer. This replaces code scattered all over the place! */
62 static u8
*mem_ptr(u32 addr
, int size
)
66 if (addr
> M
.mem_size
- size
) {
67 DB(printf("%s: address %#x out of range!\n", __func__
, addr
);)
71 //printf("%x:%x updating int vector 0x%x\n",
72 // M.x86.R_CS, M.x86.R_IP, addr >> 2);
74 retaddr
= (u8
*) (M
.mem_base
+ addr
);
79 /****************************************************************************
81 addr - Emulator memory address to read
84 Byte value read from emulator memory.
87 Reads a byte value from the emulator memory.
88 ****************************************************************************/
89 u8 X86API
rdb(u32 addr
)
94 ptr
= mem_ptr(addr
, 1);
97 DB(if (DEBUG_MEM_TRACE())
98 printf("%#08x 1 -> %#x\n", addr
, val
);)
102 /****************************************************************************
104 addr - Emulator memory address to read
107 Word value read from emulator memory.
110 Reads a word value from the emulator memory.
111 ****************************************************************************/
112 u16 X86API
rdw(u32 addr
)
117 ptr
= mem_ptr(addr
, 2);
118 val
= *(u16
*) (ptr
);
120 DB(if (DEBUG_MEM_TRACE())
121 printf("%#08x 2 -> %#x\n", addr
, val
);)
125 /****************************************************************************
127 addr - Emulator memory address to read
130 Long value read from emulator memory.
132 Reads a long value from the emulator memory.
133 ****************************************************************************/
134 u32 X86API
rdl(u32 addr
)
139 ptr
= mem_ptr(addr
, 4);
140 val
= *(u32
*) (ptr
);
142 DB(if (DEBUG_MEM_TRACE())
143 printf("%#08x 4 -> %#x\n", addr
, val
);)
147 /****************************************************************************
149 addr - Emulator memory address to read
153 Writes a byte value to emulator memory.
154 ****************************************************************************/
155 void X86API
wrb(u32 addr
, u8 val
)
159 ptr
= mem_ptr(addr
, 1);
162 DB(if (DEBUG_MEM_TRACE())
163 printf("%#08x 1 <- %#x\n", addr
, val
);)
166 /****************************************************************************
168 addr - Emulator memory address to read
172 Writes a word value to emulator memory.
173 ****************************************************************************/
174 void X86API
wrw(u32 addr
, u16 val
)
178 ptr
= mem_ptr(addr
, 2);
179 *(u16
*) (ptr
) = val
;
181 DB(if (DEBUG_MEM_TRACE())
182 printf("%#08x 2 <- %#x\n", addr
, val
);)
185 /****************************************************************************
187 addr - Emulator memory address to read
191 Writes a long value to emulator memory.
192 ****************************************************************************/
193 void X86API
wrl(u32 addr
, u32 val
)
197 ptr
= mem_ptr(addr
, 4);
198 *(u32
*) (ptr
) = val
;
200 DB(if (DEBUG_MEM_TRACE())
201 printf("%#08x 4 <- %#x\n", addr
, val
);)
204 /****************************************************************************
206 addr - PIO address to read
210 Default PIO byte read function. Doesn't perform real inb.
211 ****************************************************************************/
212 static u8 X86API
p_inb(X86EMU_pioAddr addr
)
214 DB(if (DEBUG_IO_TRACE())
215 printf("inb %#04x\n", addr
);)
219 /****************************************************************************
221 addr - PIO address to read
225 Default PIO word read function. Doesn't perform real inw.
226 ****************************************************************************/
227 static u16 X86API
p_inw(X86EMU_pioAddr addr
)
229 DB(if (DEBUG_IO_TRACE())
230 printf("inw %#04x\n", addr
);)
234 /****************************************************************************
236 addr - PIO address to read
240 Default PIO long read function. Doesn't perform real inl.
241 ****************************************************************************/
242 static u32 X86API
p_inl(X86EMU_pioAddr addr
)
244 DB(if (DEBUG_IO_TRACE())
245 printf("inl %#04x\n", addr
);)
249 /****************************************************************************
251 addr - PIO address to write
254 Default PIO byte write function. Doesn't perform real outb.
255 ****************************************************************************/
256 static void X86API
p_outb(X86EMU_pioAddr addr
, u8 val
)
258 DB(if (DEBUG_IO_TRACE())
259 printf("outb %#02x -> %#04x\n", val
, addr
);)
264 /****************************************************************************
266 addr - PIO address to write
269 Default PIO word write function. Doesn't perform real outw.
270 ****************************************************************************/
271 static void X86API
p_outw(X86EMU_pioAddr addr
, u16 val
)
273 DB(if (DEBUG_IO_TRACE())
274 printf("outw %#04x -> %#04x\n", val
, addr
);)
279 /****************************************************************************
281 addr - PIO address to write
284 Default PIO ;ong write function. Doesn't perform real outl.
285 ****************************************************************************/
286 static void X86API
p_outl(X86EMU_pioAddr addr
, u32 val
)
288 DB(if (DEBUG_IO_TRACE())
289 printf("outl %#08x -> %#04x\n", val
, addr
);)
295 /*------------------------- Global Variables ------------------------------*/
297 u8(X86APIP sys_rdb
) (u32 addr
) = rdb
;
298 u16(X86APIP sys_rdw
) (u32 addr
) = rdw
;
299 u32(X86APIP sys_rdl
) (u32 addr
) = rdl
;
300 void (X86APIP sys_wrb
) (u32 addr
, u8 val
) = wrb
;
301 void (X86APIP sys_wrw
) (u32 addr
, u16 val
) = wrw
;
302 void (X86APIP sys_wrl
) (u32 addr
, u32 val
) = wrl
;
303 u8(X86APIP sys_inb
) (X86EMU_pioAddr addr
) = p_inb
;
304 u16(X86APIP sys_inw
) (X86EMU_pioAddr addr
) = p_inw
;
305 u32(X86APIP sys_inl
) (X86EMU_pioAddr addr
) = p_inl
;
306 void (X86APIP sys_outb
) (X86EMU_pioAddr addr
, u8 val
) = p_outb
;
307 void (X86APIP sys_outw
) (X86EMU_pioAddr addr
, u16 val
) = p_outw
;
308 void (X86APIP sys_outl
) (X86EMU_pioAddr addr
, u32 val
) = p_outl
;
310 /*----------------------------- Setup -------------------------------------*/
312 /****************************************************************************
314 funcs - New memory function pointers to make active
317 This function is used to set the pointers to functions which access
318 memory space, allowing the user application to override these functions
319 and hook them out as necessary for their application.
320 ****************************************************************************/
321 void X86EMU_setupMemFuncs(X86EMU_memFuncs
* funcs
)
323 sys_rdb
= funcs
->rdb
;
324 sys_rdw
= funcs
->rdw
;
325 sys_rdl
= funcs
->rdl
;
326 sys_wrb
= funcs
->wrb
;
327 sys_wrw
= funcs
->wrw
;
328 sys_wrl
= funcs
->wrl
;
331 /****************************************************************************
333 funcs - New programmed I/O function pointers to make active
336 This function is used to set the pointers to functions which access
337 I/O space, allowing the user application to override these functions
338 and hook them out as necessary for their application.
339 ****************************************************************************/
340 void X86EMU_setupPioFuncs(X86EMU_pioFuncs
* funcs
)
342 sys_inb
= funcs
->inb
;
343 sys_inw
= funcs
->inw
;
344 sys_inl
= funcs
->inl
;
345 sys_outb
= funcs
->outb
;
346 sys_outw
= funcs
->outw
;
347 sys_outl
= funcs
->outl
;
350 /****************************************************************************
352 funcs - New interrupt vector table to make active
355 This function is used to set the pointers to functions which handle
356 interrupt processing in the emulator, allowing the user application to
357 hook interrupts as necessary for their application. Any interrupts that
358 are not hooked by the user application, and reflected and handled internally
359 in the emulator via the interrupt vector table. This allows the application
360 to get control when the code being emulated executes specific software
362 ****************************************************************************/
363 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs
[])
367 for (i
= 0; i
< 256; i
++)
368 _X86EMU_intrTab
[i
] = NULL
;
370 for (i
= 0; i
< 256; i
++)
371 _X86EMU_intrTab
[i
] = funcs
[i
];
375 /****************************************************************************
377 int - New software interrupt to prepare for
380 This function is used to set up the emulator state to execute a software
381 interrupt. This can be used by the user application code to allow an
382 interrupt to be hooked, examined and then reflected back to the emulator
383 so that the code in the emulator will continue processing the software
384 interrupt as per normal. This essentially allows system code to actively
385 hook and handle certain software interrupts as necessary.
386 ****************************************************************************/
387 void X86EMU_prepareForInt(int num
)
389 push_word((u16
) M
.x86
.R_FLG
);
392 push_word(M
.x86
.R_CS
);
393 M
.x86
.R_CS
= mem_access_word(num
* 4 + 2);
394 push_word(M
.x86
.R_IP
);
395 M
.x86
.R_IP
= mem_access_word(num
* 4);
399 void X86EMU_setMemBase(void *base
, size_t size
)
401 M
.mem_base
= (unsigned long) base
;