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 overriden by the
41 ****************************************************************************/
42 /* $XFree86: xc/extras/x86emu/src/x86emu/sys.c,v 1.5 2000/08/23 22:10:01 tsi Exp $ */
45 #include <x86emu/x86emu.h>
46 #include <x86emu/regs.h>
51 #include "xf86_ansic.h"
55 /*------------------------- Global Variables ------------------------------*/
57 X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
58 X86EMU_intrFuncs _X86EMU_intrTab[256];
60 /*----------------------------- Implementation ----------------------------*/
62 /* compute a pointer. This replaces code scattered all over the place! */
63 static u8 *mem_ptr(u32 addr, int size)
67 if (addr > M.mem_size - size) {
68 DB(printf("mem_ptr: address %#x out of range!\n", addr);)
72 //printf("%x:%x updating int vector 0x%x\n",
73 // M.x86.R_CS, M.x86.R_IP, addr >> 2);
75 retaddr = (u8 *) (M.mem_base + addr);
80 /****************************************************************************
82 addr - Emulator memory address to read
85 Byte value read from emulator memory.
88 Reads a byte value from the emulator memory.
89 ****************************************************************************/
90 u8 X86API rdb(u32 addr)
95 ptr = mem_ptr(addr, 1);
98 DB(if (DEBUG_MEM_TRACE())
99 printf("%#08x 1 -> %#x\n", addr, val);)
103 /****************************************************************************
105 addr - Emulator memory address to read
108 Word value read from emulator memory.
111 Reads a word value from the emulator memory.
112 ****************************************************************************/
113 u16 X86API rdw(u32 addr)
118 ptr = mem_ptr(addr, 2);
119 val = *(u16 *) (ptr);
121 DB(if (DEBUG_MEM_TRACE())
122 printf("%#08x 2 -> %#x\n", addr, val);)
126 /****************************************************************************
128 addr - Emulator memory address to read
131 Long value read from emulator memory.
133 Reads a long value from the emulator memory.
134 ****************************************************************************/
135 u32 X86API rdl(u32 addr)
140 ptr = mem_ptr(addr, 4);
141 val = *(u32 *) (ptr);
143 DB(if (DEBUG_MEM_TRACE())
144 printf("%#08x 4 -> %#x\n", addr, val);)
148 /****************************************************************************
150 addr - Emulator memory address to read
154 Writes a byte value to emulator memory.
155 ****************************************************************************/
156 void X86API wrb(u32 addr, u8 val)
160 ptr = mem_ptr(addr, 1);
163 DB(if (DEBUG_MEM_TRACE())
164 printf("%#08x 1 <- %#x\n", addr, val);)
167 /****************************************************************************
169 addr - Emulator memory address to read
173 Writes a word value to emulator memory.
174 ****************************************************************************/
175 void X86API wrw(u32 addr, u16 val)
179 ptr = mem_ptr(addr, 2);
180 *(u16 *) (ptr) = val;
182 DB(if (DEBUG_MEM_TRACE())
183 printf("%#08x 2 <- %#x\n", addr, val);)
186 /****************************************************************************
188 addr - Emulator memory address to read
192 Writes a long value to emulator memory.
193 ****************************************************************************/
194 void X86API wrl(u32 addr, u32 val)
198 ptr = mem_ptr(addr, 4);
199 *(u32 *) (ptr) = val;
201 DB(if (DEBUG_MEM_TRACE())
202 printf("%#08x 4 <- %#x\n", addr, val);)
207 /****************************************************************************
209 addr - PIO address to read
213 Default PIO byte read function. Doesn't perform real inb.
214 ****************************************************************************/
215 static u8 X86API p_inb(X86EMU_pioAddr addr)
217 DB(if (DEBUG_IO_TRACE())
218 printf("inb %#04x \n", addr);)
222 /****************************************************************************
224 addr - PIO address to read
228 Default PIO word read function. Doesn't perform real inw.
229 ****************************************************************************/
230 static u16 X86API p_inw(X86EMU_pioAddr addr)
232 DB(if (DEBUG_IO_TRACE())
233 printf("inw %#04x \n", addr);)
237 /****************************************************************************
239 addr - PIO address to read
243 Default PIO long read function. Doesn't perform real inl.
244 ****************************************************************************/
245 static u32 X86API p_inl(X86EMU_pioAddr addr)
247 DB(if (DEBUG_IO_TRACE())
248 printf("inl %#04x \n", addr);)
252 /****************************************************************************
254 addr - PIO address to write
257 Default PIO byte write function. Doesn't perform real outb.
258 ****************************************************************************/
259 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
261 DB(if (DEBUG_IO_TRACE())
262 printf("outb %#02x -> %#04x \n", val, addr);)
267 /****************************************************************************
269 addr - PIO address to write
272 Default PIO word write function. Doesn't perform real outw.
273 ****************************************************************************/
274 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
276 DB(if (DEBUG_IO_TRACE())
277 printf("outw %#04x -> %#04x \n", val, addr);)
282 /****************************************************************************
284 addr - PIO address to write
287 Default PIO ;ong write function. Doesn't perform real outl.
288 ****************************************************************************/
289 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
291 DB(if (DEBUG_IO_TRACE())
292 printf("outl %#08x -> %#04x \n", val, addr);)
298 /*------------------------- Global Variables ------------------------------*/
300 u8(X86APIP sys_rdb) (u32 addr) = rdb;
301 u16(X86APIP sys_rdw) (u32 addr) = rdw;
302 u32(X86APIP sys_rdl) (u32 addr) = rdl;
303 void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
304 void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
305 void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
306 u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
307 u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
308 u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
309 void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
310 void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
311 void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
313 /*----------------------------- Setup -------------------------------------*/
315 /****************************************************************************
317 funcs - New memory function pointers to make active
320 This function is used to set the pointers to functions which access
321 memory space, allowing the user application to override these functions
322 and hook them out as necessary for their application.
323 ****************************************************************************/
324 void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
326 sys_rdb = funcs->rdb;
327 sys_rdw = funcs->rdw;
328 sys_rdl = funcs->rdl;
329 sys_wrb = funcs->wrb;
330 sys_wrw = funcs->wrw;
331 sys_wrl = funcs->wrl;
334 /****************************************************************************
336 funcs - New programmed I/O function pointers to make active
339 This function is used to set the pointers to functions which access
340 I/O space, allowing the user application to override these functions
341 and hook them out as necessary for their application.
342 ****************************************************************************/
343 void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
345 sys_inb = funcs->inb;
346 sys_inw = funcs->inw;
347 sys_inl = funcs->inl;
348 sys_outb = funcs->outb;
349 sys_outw = funcs->outw;
350 sys_outl = funcs->outl;
353 /****************************************************************************
355 funcs - New interrupt vector table to make active
358 This function is used to set the pointers to functions which handle
359 interrupt processing in the emulator, allowing the user application to
360 hook interrupts as necessary for their application. Any interrupts that
361 are not hooked by the user application, and reflected and handled internally
362 in the emulator via the interrupt vector table. This allows the application
363 to get control when the code being emulated executes specific software
365 ****************************************************************************/
366 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
370 for (i = 0; i < 256; i++)
371 _X86EMU_intrTab[i] = NULL;
373 for (i = 0; i < 256; i++)
374 _X86EMU_intrTab[i] = funcs[i];
378 /****************************************************************************
380 int - New software interrupt to prepare for
383 This function is used to set up the emulator state to exceute a software
384 interrupt. This can be used by the user application code to allow an
385 interrupt to be hooked, examined and then reflected back to the emulator
386 so that the code in the emulator will continue processing the software
387 interrupt as per normal. This essentially allows system code to actively
388 hook and handle certain software interrupts as necessary.
389 ****************************************************************************/
390 void X86EMU_prepareForInt(int num)
392 push_word((u16) M.x86.R_FLG);
395 push_word(M.x86.R_CS);
396 M.x86.R_CS = mem_access_word(num * 4 + 2);
397 push_word(M.x86.R_IP);
398 M.x86.R_IP = mem_access_word(num * 4);
402 void X86EMU_setMemBase(void *base, size_t size)
404 M.mem_base = (unsigned long) base;