1*e982f872Sjoerg /* $NetBSD: x86emu_util.c,v 1.2 2007/12/04 17:32:22 joerg Exp $ */
222ac6fc0Sjoerg
322ac6fc0Sjoerg /****************************************************************************
422ac6fc0Sjoerg *
522ac6fc0Sjoerg * Realmode X86 Emulator Library
622ac6fc0Sjoerg *
722ac6fc0Sjoerg * Copyright (C) 1996-1999 SciTech Software, Inc.
822ac6fc0Sjoerg * Copyright (C) David Mosberger-Tang
922ac6fc0Sjoerg * Copyright (C) 1999 Egbert Eich
1022ac6fc0Sjoerg * Copyright (C) 2007 Joerg Sonnenberger
1122ac6fc0Sjoerg *
1222ac6fc0Sjoerg * ========================================================================
1322ac6fc0Sjoerg *
1422ac6fc0Sjoerg * Permission to use, copy, modify, distribute, and sell this software and
1522ac6fc0Sjoerg * its documentation for any purpose is hereby granted without fee,
1622ac6fc0Sjoerg * provided that the above copyright notice appear in all copies and that
1722ac6fc0Sjoerg * both that copyright notice and this permission notice appear in
1822ac6fc0Sjoerg * supporting documentation, and that the name of the authors not be used
1922ac6fc0Sjoerg * in advertising or publicity pertaining to distribution of the software
2022ac6fc0Sjoerg * without specific, written prior permission. The authors makes no
2122ac6fc0Sjoerg * representations about the suitability of this software for any purpose.
2222ac6fc0Sjoerg * It is provided "as is" without express or implied warranty.
2322ac6fc0Sjoerg *
2422ac6fc0Sjoerg * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
2522ac6fc0Sjoerg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
2622ac6fc0Sjoerg * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
2722ac6fc0Sjoerg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
2822ac6fc0Sjoerg * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
2922ac6fc0Sjoerg * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3022ac6fc0Sjoerg * PERFORMANCE OF THIS SOFTWARE.
3122ac6fc0Sjoerg *
3222ac6fc0Sjoerg ****************************************************************************/
3322ac6fc0Sjoerg
34*e982f872Sjoerg #include <x86emu/x86emu.h>
35*e982f872Sjoerg #include <x86emu/x86emu_regs.h>
3622ac6fc0Sjoerg
3722ac6fc0Sjoerg #include <sys/endian.h>
3822ac6fc0Sjoerg #include <sys/null.h>
3922ac6fc0Sjoerg
4022ac6fc0Sjoerg /****************************************************************************
4122ac6fc0Sjoerg PARAMETERS:
4222ac6fc0Sjoerg addr - Emulator memory address to read
4322ac6fc0Sjoerg
4422ac6fc0Sjoerg RETURNS:
4522ac6fc0Sjoerg Byte value read from emulator memory.
4622ac6fc0Sjoerg
4722ac6fc0Sjoerg REMARKS:
4822ac6fc0Sjoerg Reads a byte value from the emulator memory.
4922ac6fc0Sjoerg ****************************************************************************/
5022ac6fc0Sjoerg static uint8_t
rdb(struct X86EMU * emu,uint32_t addr)5122ac6fc0Sjoerg rdb(struct X86EMU *emu, uint32_t addr)
5222ac6fc0Sjoerg {
5322ac6fc0Sjoerg if (addr > emu->mem_size - 1)
5422ac6fc0Sjoerg X86EMU_halt_sys(emu);
5522ac6fc0Sjoerg return emu->mem_base[addr];
5622ac6fc0Sjoerg }
5722ac6fc0Sjoerg /****************************************************************************
5822ac6fc0Sjoerg PARAMETERS:
5922ac6fc0Sjoerg addr - Emulator memory address to read
6022ac6fc0Sjoerg
6122ac6fc0Sjoerg RETURNS:
6222ac6fc0Sjoerg Word value read from emulator memory.
6322ac6fc0Sjoerg
6422ac6fc0Sjoerg REMARKS:
6522ac6fc0Sjoerg Reads a word value from the emulator memory.
6622ac6fc0Sjoerg ****************************************************************************/
6722ac6fc0Sjoerg static uint16_t
rdw(struct X86EMU * emu,uint32_t addr)6822ac6fc0Sjoerg rdw(struct X86EMU *emu, uint32_t addr)
6922ac6fc0Sjoerg {
7022ac6fc0Sjoerg if (addr > emu->mem_size - 2)
7122ac6fc0Sjoerg X86EMU_halt_sys(emu);
7222ac6fc0Sjoerg return le16dec(emu->mem_base + addr);
7322ac6fc0Sjoerg }
7422ac6fc0Sjoerg /****************************************************************************
7522ac6fc0Sjoerg PARAMETERS:
7622ac6fc0Sjoerg addr - Emulator memory address to read
7722ac6fc0Sjoerg
7822ac6fc0Sjoerg RETURNS:
7922ac6fc0Sjoerg Long value read from emulator memory.
8022ac6fc0Sjoerg REMARKS:
8122ac6fc0Sjoerg Reads a long value from the emulator memory.
8222ac6fc0Sjoerg ****************************************************************************/
8322ac6fc0Sjoerg static uint32_t
rdl(struct X86EMU * emu,uint32_t addr)8422ac6fc0Sjoerg rdl(struct X86EMU *emu, uint32_t addr)
8522ac6fc0Sjoerg {
8622ac6fc0Sjoerg if (addr > emu->mem_size - 4)
8722ac6fc0Sjoerg X86EMU_halt_sys(emu);
8822ac6fc0Sjoerg return le32dec(emu->mem_base + addr);
8922ac6fc0Sjoerg }
9022ac6fc0Sjoerg /****************************************************************************
9122ac6fc0Sjoerg PARAMETERS:
9222ac6fc0Sjoerg addr - Emulator memory address to read
9322ac6fc0Sjoerg val - Value to store
9422ac6fc0Sjoerg
9522ac6fc0Sjoerg REMARKS:
9622ac6fc0Sjoerg Writes a byte value to emulator memory.
9722ac6fc0Sjoerg ****************************************************************************/
9822ac6fc0Sjoerg static void
wrb(struct X86EMU * emu,uint32_t addr,uint8_t val)9922ac6fc0Sjoerg wrb(struct X86EMU *emu, uint32_t addr, uint8_t val)
10022ac6fc0Sjoerg {
10122ac6fc0Sjoerg if (addr > emu->mem_size - 1)
10222ac6fc0Sjoerg X86EMU_halt_sys(emu);
10322ac6fc0Sjoerg emu->mem_base[addr] = val;
10422ac6fc0Sjoerg }
10522ac6fc0Sjoerg /****************************************************************************
10622ac6fc0Sjoerg PARAMETERS:
10722ac6fc0Sjoerg addr - Emulator memory address to read
10822ac6fc0Sjoerg val - Value to store
10922ac6fc0Sjoerg
11022ac6fc0Sjoerg REMARKS:
11122ac6fc0Sjoerg Writes a word value to emulator memory.
11222ac6fc0Sjoerg ****************************************************************************/
11322ac6fc0Sjoerg static void
wrw(struct X86EMU * emu,uint32_t addr,uint16_t val)11422ac6fc0Sjoerg wrw(struct X86EMU *emu, uint32_t addr, uint16_t val)
11522ac6fc0Sjoerg {
11622ac6fc0Sjoerg if (addr > emu->mem_size - 2)
11722ac6fc0Sjoerg X86EMU_halt_sys(emu);
11822ac6fc0Sjoerg le16enc(emu->mem_base + addr, val);
11922ac6fc0Sjoerg }
12022ac6fc0Sjoerg /****************************************************************************
12122ac6fc0Sjoerg PARAMETERS:
12222ac6fc0Sjoerg addr - Emulator memory address to read
12322ac6fc0Sjoerg val - Value to store
12422ac6fc0Sjoerg
12522ac6fc0Sjoerg REMARKS:
12622ac6fc0Sjoerg Writes a long value to emulator memory.
12722ac6fc0Sjoerg ****************************************************************************/
12822ac6fc0Sjoerg static void
wrl(struct X86EMU * emu,uint32_t addr,uint32_t val)12922ac6fc0Sjoerg wrl(struct X86EMU *emu, uint32_t addr, uint32_t val)
13022ac6fc0Sjoerg {
13122ac6fc0Sjoerg if (addr > emu->mem_size - 4)
13222ac6fc0Sjoerg X86EMU_halt_sys(emu);
13322ac6fc0Sjoerg le32enc(emu->mem_base + addr, val);
13422ac6fc0Sjoerg }
13522ac6fc0Sjoerg
13622ac6fc0Sjoerg /*----------------------------- Setup -------------------------------------*/
13722ac6fc0Sjoerg
13822ac6fc0Sjoerg void
X86EMU_init_default(struct X86EMU * emu)13922ac6fc0Sjoerg X86EMU_init_default(struct X86EMU *emu)
14022ac6fc0Sjoerg {
14122ac6fc0Sjoerg int i;
14222ac6fc0Sjoerg
14322ac6fc0Sjoerg emu->emu_rdb = rdb;
14422ac6fc0Sjoerg emu->emu_rdw = rdw;
14522ac6fc0Sjoerg emu->emu_rdl = rdl;
14622ac6fc0Sjoerg emu->emu_wrb = wrb;
14722ac6fc0Sjoerg emu->emu_wrw = wrw;
14822ac6fc0Sjoerg emu->emu_wrl = wrl;
14922ac6fc0Sjoerg
15022ac6fc0Sjoerg for (i = 0; i < 256; i++)
15122ac6fc0Sjoerg emu->_X86EMU_intrTab[i] = NULL;
15222ac6fc0Sjoerg }
153