1/* $NetBSD: x86emu_util.c,v 1.2 2007/12/04 17:32:22 joerg Exp $ */
2
3/****************************************************************************
4*
5* Realmode X86 Emulator Library
6*
7* Copyright (C) 1996-1999 SciTech Software, Inc.
8* Copyright (C) David Mosberger-Tang
9* Copyright (C) 1999 Egbert Eich
10* Copyright (C) 2007 Joerg Sonnenberger
11*
12* ========================================================================
13*
14* Permission to use, copy, modify, distribute, and sell this software and
15* its documentation for any purpose is hereby granted without fee,
16* provided that the above copyright notice appear in all copies and that
17* both that copyright notice and this permission notice appear in
18* supporting documentation, and that the name of the authors not be used
19* in advertising or publicity pertaining to distribution of the software
20* without specific, written prior permission. The authors makes no
21* representations about the suitability of this software for any purpose.
22* It is provided "as is" without express or implied warranty.
23*
24* THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
25* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
26* EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
27* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
28* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
29* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
30* PERFORMANCE OF THIS SOFTWARE.
31*
32****************************************************************************/
33
34#include <x86emu/x86emu.h>
35#include <x86emu/x86emu_regs.h>
36
37#include <sys/endian.h>
38#include <sys/null.h>
39
40/****************************************************************************
41PARAMETERS:
42addr - Emulator memory address to read
43
44RETURNS:
45Byte value read from emulator memory.
46
47REMARKS:
48Reads a byte value from the emulator memory.
49****************************************************************************/
50static uint8_t
51rdb(struct X86EMU *emu, uint32_t addr)
52{
53 if (addr > emu->mem_size - 1)
54 X86EMU_halt_sys(emu);
55 return emu->mem_base[addr];
56}
57/****************************************************************************
58PARAMETERS:
59addr - Emulator memory address to read
60
61RETURNS:
62Word value read from emulator memory.
63
64REMARKS:
65Reads a word value from the emulator memory.
66****************************************************************************/
67static uint16_t
68rdw(struct X86EMU *emu, uint32_t addr)
69{
70 if (addr > emu->mem_size - 2)
71 X86EMU_halt_sys(emu);
72 return le16dec(emu->mem_base + addr);
73}
74/****************************************************************************
75PARAMETERS:
76addr - Emulator memory address to read
77
78RETURNS:
79Long value read from emulator memory.
80REMARKS:
81Reads a long value from the emulator memory.
82****************************************************************************/
83static uint32_t
84rdl(struct X86EMU *emu, uint32_t addr)
85{
86 if (addr > emu->mem_size - 4)
87 X86EMU_halt_sys(emu);
88 return le32dec(emu->mem_base + addr);
89}
90/****************************************************************************
91PARAMETERS:
92addr - Emulator memory address to read
93val - Value to store
94
95REMARKS:
96Writes a byte value to emulator memory.
97****************************************************************************/
98static void
99wrb(struct X86EMU *emu, uint32_t addr, uint8_t val)
100{
101 if (addr > emu->mem_size - 1)
102 X86EMU_halt_sys(emu);
103 emu->mem_base[addr] = val;
104}
105/****************************************************************************
106PARAMETERS:
107addr - Emulator memory address to read
108val - Value to store
109
110REMARKS:
111Writes a word value to emulator memory.
112****************************************************************************/
113static void
114wrw(struct X86EMU *emu, uint32_t addr, uint16_t val)
115{
116 if (addr > emu->mem_size - 2)
117 X86EMU_halt_sys(emu);
118 le16enc(emu->mem_base + addr, val);
119}
120/****************************************************************************
121PARAMETERS:
122addr - Emulator memory address to read
123val - Value to store
124
125REMARKS:
126Writes a long value to emulator memory.
127****************************************************************************/
128static void
129wrl(struct X86EMU *emu, uint32_t addr, uint32_t val)
130{
131 if (addr > emu->mem_size - 4)
132 X86EMU_halt_sys(emu);
133 le32enc(emu->mem_base + addr, val);
134}
135
136/*----------------------------- Setup -------------------------------------*/
137
138void
139X86EMU_init_default(struct X86EMU *emu)
140{
141 int i;
142
143 emu->emu_rdb = rdb;
144 emu->emu_rdw = rdw;
145 emu->emu_rdl = rdl;
146 emu->emu_wrb = wrb;
147 emu->emu_wrw = wrw;
148 emu->emu_wrl = wrl;
149
150 for (i = 0; i < 256; i++)
151 emu->_X86EMU_intrTab[i] = NULL;
152}
153