1ece92f85SJason Jin /****************************************************************************
2ece92f85SJason Jin *
3ece92f85SJason Jin * Realmode X86 Emulator Library
4ece92f85SJason Jin *
5ece92f85SJason Jin * Copyright (C) 1991-2004 SciTech Software, Inc.
6ece92f85SJason Jin * Copyright (C) David Mosberger-Tang
7ece92f85SJason Jin * Copyright (C) 1999 Egbert Eich
8ece92f85SJason Jin *
9ece92f85SJason Jin * ========================================================================
10ece92f85SJason Jin *
11ece92f85SJason Jin * Permission to use, copy, modify, distribute, and sell this software and
12ece92f85SJason Jin * its documentation for any purpose is hereby granted without fee,
13ece92f85SJason Jin * provided that the above copyright notice appear in all copies and that
14ece92f85SJason Jin * both that copyright notice and this permission notice appear in
15ece92f85SJason Jin * supporting documentation, and that the name of the authors not be used
16ece92f85SJason Jin * in advertising or publicity pertaining to distribution of the software
17ece92f85SJason Jin * without specific, written prior permission. The authors makes no
18ece92f85SJason Jin * representations about the suitability of this software for any purpose.
19ece92f85SJason Jin * It is provided "as is" without express or implied warranty.
20ece92f85SJason Jin *
21ece92f85SJason Jin * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22ece92f85SJason Jin * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23ece92f85SJason Jin * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24ece92f85SJason Jin * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25ece92f85SJason Jin * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26ece92f85SJason Jin * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27ece92f85SJason Jin * PERFORMANCE OF THIS SOFTWARE.
28ece92f85SJason Jin *
29ece92f85SJason Jin * ========================================================================
30ece92f85SJason Jin *
31ece92f85SJason Jin * Language: ANSI C
32ece92f85SJason Jin * Environment: Any
33ece92f85SJason Jin * Developer: Kendall Bennett
34ece92f85SJason Jin *
35ece92f85SJason Jin * Description: This file includes subroutines which are related to
36ece92f85SJason Jin * programmed I/O and memory access. Included in this module
37ece92f85SJason Jin * are default functions that do nothing. For real uses these
38*62a3b7ddSRobert P. J. Day * functions will have to be overridden by the user library.
39ece92f85SJason Jin *
40ece92f85SJason Jin ****************************************************************************/
41ece92f85SJason Jin
4278cff50eSMichal Simek #include <common.h>
435b4de930SMichal Simek #include "x86emu/x86emui.h"
445b4de930SMichal Simek
45ece92f85SJason Jin /*------------------------- Global Variables ------------------------------*/
46ece92f85SJason Jin
47ece92f85SJason Jin X86EMU_sysEnv _X86EMU_env; /* Global emulator machine state */
48ece92f85SJason Jin X86EMU_intrFuncs _X86EMU_intrTab[256];
49ece92f85SJason Jin
50ece92f85SJason Jin int debug_intr;
51ece92f85SJason Jin
52ece92f85SJason Jin /*----------------------------- Implementation ----------------------------*/
53ece92f85SJason Jin
54ece92f85SJason Jin /****************************************************************************
55ece92f85SJason Jin PARAMETERS:
56ece92f85SJason Jin addr - Emulator memory address to read
57ece92f85SJason Jin
58ece92f85SJason Jin RETURNS:
59ece92f85SJason Jin Byte value read from emulator memory.
60ece92f85SJason Jin
61ece92f85SJason Jin REMARKS:
62ece92f85SJason Jin Reads a byte value from the emulator memory.
63ece92f85SJason Jin ****************************************************************************/
rdb(u32 addr)64ece92f85SJason Jin u8 X86API rdb(u32 addr)
65ece92f85SJason Jin {
66ece92f85SJason Jin return 0;
67ece92f85SJason Jin }
68ece92f85SJason Jin
69ece92f85SJason Jin /****************************************************************************
70ece92f85SJason Jin PARAMETERS:
71ece92f85SJason Jin addr - Emulator memory address to read
72ece92f85SJason Jin
73ece92f85SJason Jin RETURNS:
74ece92f85SJason Jin Word value read from emulator memory.
75ece92f85SJason Jin
76ece92f85SJason Jin REMARKS:
77ece92f85SJason Jin Reads a word value from the emulator memory.
78ece92f85SJason Jin ****************************************************************************/
rdw(u32 addr)79ece92f85SJason Jin u16 X86API rdw(u32 addr)
80ece92f85SJason Jin {
81ece92f85SJason Jin return 0;
82ece92f85SJason Jin }
83ece92f85SJason Jin
84ece92f85SJason Jin /****************************************************************************
85ece92f85SJason Jin PARAMETERS:
86ece92f85SJason Jin addr - Emulator memory address to read
87ece92f85SJason Jin
88ece92f85SJason Jin RETURNS:
89ece92f85SJason Jin Long value read from emulator memory.
90ece92f85SJason Jin REMARKS:
91ece92f85SJason Jin Reads a long value from the emulator memory.
92ece92f85SJason Jin ****************************************************************************/
rdl(u32 addr)93ece92f85SJason Jin u32 X86API rdl(u32 addr)
94ece92f85SJason Jin {
95ece92f85SJason Jin return 0;
96ece92f85SJason Jin }
97ece92f85SJason Jin
98ece92f85SJason Jin /****************************************************************************
99ece92f85SJason Jin PARAMETERS:
100ece92f85SJason Jin addr - Emulator memory address to read
101ece92f85SJason Jin val - Value to store
102ece92f85SJason Jin
103ece92f85SJason Jin REMARKS:
104ece92f85SJason Jin Writes a byte value to emulator memory.
105ece92f85SJason Jin ****************************************************************************/
wrb(u32 addr,u8 val)106ece92f85SJason Jin void X86API wrb(u32 addr, u8 val)
107ece92f85SJason Jin {
108ece92f85SJason Jin }
109ece92f85SJason Jin
110ece92f85SJason Jin /****************************************************************************
111ece92f85SJason Jin PARAMETERS:
112ece92f85SJason Jin addr - Emulator memory address to read
113ece92f85SJason Jin val - Value to store
114ece92f85SJason Jin
115ece92f85SJason Jin REMARKS:
116ece92f85SJason Jin Writes a word value to emulator memory.
117ece92f85SJason Jin ****************************************************************************/
wrw(u32 addr,u16 val)118ece92f85SJason Jin void X86API wrw(u32 addr, u16 val)
119ece92f85SJason Jin {
120ece92f85SJason Jin }
121ece92f85SJason Jin
122ece92f85SJason Jin /****************************************************************************
123ece92f85SJason Jin PARAMETERS:
124ece92f85SJason Jin addr - Emulator memory address to read
125ece92f85SJason Jin val - Value to store
126ece92f85SJason Jin
127ece92f85SJason Jin REMARKS:
128ece92f85SJason Jin Writes a long value to emulator memory.
129ece92f85SJason Jin ****************************************************************************/
wrl(u32 addr,u32 val)130ece92f85SJason Jin void X86API wrl(u32 addr, u32 val)
131ece92f85SJason Jin {
132ece92f85SJason Jin }
133ece92f85SJason Jin
134ece92f85SJason Jin /****************************************************************************
135ece92f85SJason Jin PARAMETERS:
136ece92f85SJason Jin addr - PIO address to read
137ece92f85SJason Jin RETURN:
138ece92f85SJason Jin 0
139ece92f85SJason Jin REMARKS:
140ece92f85SJason Jin Default PIO byte read function. Doesn't perform real inb.
141ece92f85SJason Jin ****************************************************************************/
p_inb(X86EMU_pioAddr addr)142ece92f85SJason Jin static u8 X86API p_inb(X86EMU_pioAddr addr)
143ece92f85SJason Jin {
144ece92f85SJason Jin DB(if (DEBUG_IO_TRACE())
145ece92f85SJason Jin printk("inb %#04x \n", addr);)
146ece92f85SJason Jin return 0;
147ece92f85SJason Jin }
148ece92f85SJason Jin
149ece92f85SJason Jin /****************************************************************************
150ece92f85SJason Jin PARAMETERS:
151ece92f85SJason Jin addr - PIO address to read
152ece92f85SJason Jin RETURN:
153ece92f85SJason Jin 0
154ece92f85SJason Jin REMARKS:
155ece92f85SJason Jin Default PIO word read function. Doesn't perform real inw.
156ece92f85SJason Jin ****************************************************************************/
p_inw(X86EMU_pioAddr addr)157ece92f85SJason Jin static u16 X86API p_inw(X86EMU_pioAddr addr)
158ece92f85SJason Jin {
159ece92f85SJason Jin DB(if (DEBUG_IO_TRACE())
160ece92f85SJason Jin printk("inw %#04x \n", addr);)
161ece92f85SJason Jin return 0;
162ece92f85SJason Jin }
163ece92f85SJason Jin
164ece92f85SJason Jin /****************************************************************************
165ece92f85SJason Jin PARAMETERS:
166ece92f85SJason Jin addr - PIO address to read
167ece92f85SJason Jin RETURN:
168ece92f85SJason Jin 0
169ece92f85SJason Jin REMARKS:
170ece92f85SJason Jin Default PIO long read function. Doesn't perform real inl.
171ece92f85SJason Jin ****************************************************************************/
p_inl(X86EMU_pioAddr addr)172ece92f85SJason Jin static u32 X86API p_inl(X86EMU_pioAddr addr)
173ece92f85SJason Jin {
174ece92f85SJason Jin DB(if (DEBUG_IO_TRACE())
175ece92f85SJason Jin printk("inl %#04x \n", addr);)
176ece92f85SJason Jin return 0;
177ece92f85SJason Jin }
178ece92f85SJason Jin
179ece92f85SJason Jin /****************************************************************************
180ece92f85SJason Jin PARAMETERS:
181ece92f85SJason Jin addr - PIO address to write
182ece92f85SJason Jin val - Value to store
183ece92f85SJason Jin REMARKS:
184ece92f85SJason Jin Default PIO byte write function. Doesn't perform real outb.
185ece92f85SJason Jin ****************************************************************************/
p_outb(X86EMU_pioAddr addr,u8 val)186ece92f85SJason Jin static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
187ece92f85SJason Jin {
188ece92f85SJason Jin DB(if (DEBUG_IO_TRACE())
189ece92f85SJason Jin printk("outb %#02x -> %#04x \n", val, addr);)
190ece92f85SJason Jin return;
191ece92f85SJason Jin }
192ece92f85SJason Jin
193ece92f85SJason Jin /****************************************************************************
194ece92f85SJason Jin PARAMETERS:
195ece92f85SJason Jin addr - PIO address to write
196ece92f85SJason Jin val - Value to store
197ece92f85SJason Jin REMARKS:
198ece92f85SJason Jin Default PIO word write function. Doesn't perform real outw.
199ece92f85SJason Jin ****************************************************************************/
p_outw(X86EMU_pioAddr addr,u16 val)200ece92f85SJason Jin static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
201ece92f85SJason Jin {
202ece92f85SJason Jin DB(if (DEBUG_IO_TRACE())
203ece92f85SJason Jin printk("outw %#04x -> %#04x \n", val, addr);)
204ece92f85SJason Jin return;
205ece92f85SJason Jin }
206ece92f85SJason Jin
207ece92f85SJason Jin /****************************************************************************
208ece92f85SJason Jin PARAMETERS:
209ece92f85SJason Jin addr - PIO address to write
210ece92f85SJason Jin val - Value to store
211ece92f85SJason Jin REMARKS:
212ece92f85SJason Jin Default PIO ;ong write function. Doesn't perform real outl.
213ece92f85SJason Jin ****************************************************************************/
p_outl(X86EMU_pioAddr addr,u32 val)214ece92f85SJason Jin static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
215ece92f85SJason Jin {
216ece92f85SJason Jin DB(if (DEBUG_IO_TRACE())
217ece92f85SJason Jin printk("outl %#08x -> %#04x \n", val, addr);)
218ece92f85SJason Jin return;
219ece92f85SJason Jin }
220ece92f85SJason Jin
221ece92f85SJason Jin /*------------------------- Global Variables ------------------------------*/
222ece92f85SJason Jin
223ece92f85SJason Jin u8(X86APIP sys_rdb) (u32 addr) = rdb;
224ece92f85SJason Jin u16(X86APIP sys_rdw) (u32 addr) = rdw;
225ece92f85SJason Jin u32(X86APIP sys_rdl) (u32 addr) = rdl;
226ece92f85SJason Jin void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
227ece92f85SJason Jin void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
228ece92f85SJason Jin void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
229ece92f85SJason Jin u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
230ece92f85SJason Jin u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
231ece92f85SJason Jin u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
232ece92f85SJason Jin void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
233ece92f85SJason Jin void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
234ece92f85SJason Jin void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
235ece92f85SJason Jin
236ece92f85SJason Jin /*----------------------------- Setup -------------------------------------*/
237ece92f85SJason Jin
238ece92f85SJason Jin /****************************************************************************
239ece92f85SJason Jin PARAMETERS:
240ece92f85SJason Jin funcs - New memory function pointers to make active
241ece92f85SJason Jin
242ece92f85SJason Jin REMARKS:
243ece92f85SJason Jin This function is used to set the pointers to functions which access
244ece92f85SJason Jin memory space, allowing the user application to override these functions
245ece92f85SJason Jin and hook them out as necessary for their application.
246ece92f85SJason Jin ****************************************************************************/
X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)247ece92f85SJason Jin void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
248ece92f85SJason Jin {
249ece92f85SJason Jin sys_rdb = funcs->rdb;
250ece92f85SJason Jin sys_rdw = funcs->rdw;
251ece92f85SJason Jin sys_rdl = funcs->rdl;
252ece92f85SJason Jin sys_wrb = funcs->wrb;
253ece92f85SJason Jin sys_wrw = funcs->wrw;
254ece92f85SJason Jin sys_wrl = funcs->wrl;
255ece92f85SJason Jin }
256ece92f85SJason Jin
257ece92f85SJason Jin /****************************************************************************
258ece92f85SJason Jin PARAMETERS:
259ece92f85SJason Jin funcs - New programmed I/O function pointers to make active
260ece92f85SJason Jin
261ece92f85SJason Jin REMARKS:
262ece92f85SJason Jin This function is used to set the pointers to functions which access
263ece92f85SJason Jin I/O space, allowing the user application to override these functions
264ece92f85SJason Jin and hook them out as necessary for their application.
265ece92f85SJason Jin ****************************************************************************/
X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)266ece92f85SJason Jin void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
267ece92f85SJason Jin {
268ece92f85SJason Jin sys_inb = funcs->inb;
269ece92f85SJason Jin sys_inw = funcs->inw;
270ece92f85SJason Jin sys_inl = funcs->inl;
271ece92f85SJason Jin sys_outb = funcs->outb;
272ece92f85SJason Jin sys_outw = funcs->outw;
273ece92f85SJason Jin sys_outl = funcs->outl;
274ece92f85SJason Jin }
275ece92f85SJason Jin
X86EMU_setupIntrFunc(int intnum,X86EMU_intrFuncs func)276a3c700ecSSimon Glass void X86EMU_setupIntrFunc(int intnum, X86EMU_intrFuncs func)
277a3c700ecSSimon Glass {
278a3c700ecSSimon Glass _X86EMU_intrTab[intnum] = func;
279a3c700ecSSimon Glass }
280a3c700ecSSimon Glass
281ece92f85SJason Jin /****************************************************************************
282ece92f85SJason Jin PARAMETERS:
283ece92f85SJason Jin funcs - New interrupt vector table to make active
284ece92f85SJason Jin
285ece92f85SJason Jin REMARKS:
286ece92f85SJason Jin This function is used to set the pointers to functions which handle
287ece92f85SJason Jin interrupt processing in the emulator, allowing the user application to
288ece92f85SJason Jin hook interrupts as necessary for their application. Any interrupts that
289ece92f85SJason Jin are not hooked by the user application, and reflected and handled internally
290ece92f85SJason Jin in the emulator via the interrupt vector table. This allows the application
291ece92f85SJason Jin to get control when the code being emulated executes specific software
292ece92f85SJason Jin interrupts.
293ece92f85SJason Jin ****************************************************************************/
X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])294ece92f85SJason Jin void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
295ece92f85SJason Jin {
296ece92f85SJason Jin int i;
297ece92f85SJason Jin
298ece92f85SJason Jin for (i = 0; i < 256; i++)
299ece92f85SJason Jin _X86EMU_intrTab[i] = NULL;
300ece92f85SJason Jin if (funcs) {
301ece92f85SJason Jin for (i = 0; i < 256; i++)
302ece92f85SJason Jin _X86EMU_intrTab[i] = funcs[i];
303ece92f85SJason Jin }
304ece92f85SJason Jin }
305ece92f85SJason Jin
306ece92f85SJason Jin /****************************************************************************
307ece92f85SJason Jin PARAMETERS:
308ece92f85SJason Jin int - New software interrupt to prepare for
309ece92f85SJason Jin
310ece92f85SJason Jin REMARKS:
311ece92f85SJason Jin This function is used to set up the emulator state to exceute a software
312ece92f85SJason Jin interrupt. This can be used by the user application code to allow an
313ece92f85SJason Jin interrupt to be hooked, examined and then reflected back to the emulator
314ece92f85SJason Jin so that the code in the emulator will continue processing the software
315ece92f85SJason Jin interrupt as per normal. This essentially allows system code to actively
316ece92f85SJason Jin hook and handle certain software interrupts as necessary.
317ece92f85SJason Jin ****************************************************************************/
X86EMU_prepareForInt(int num)318ece92f85SJason Jin void X86EMU_prepareForInt(int num)
319ece92f85SJason Jin {
320ece92f85SJason Jin push_word((u16) M.x86.R_FLG);
321ece92f85SJason Jin CLEAR_FLAG(F_IF);
322ece92f85SJason Jin CLEAR_FLAG(F_TF);
323ece92f85SJason Jin push_word(M.x86.R_CS);
324ece92f85SJason Jin M.x86.R_CS = mem_access_word(num * 4 + 2);
325ece92f85SJason Jin push_word(M.x86.R_IP);
326ece92f85SJason Jin M.x86.R_IP = mem_access_word(num * 4);
327ece92f85SJason Jin M.x86.intr = 0;
328ece92f85SJason Jin }
329