1 /****************************************************************************
2 *
3 *                       Realmode X86 Emulator Library
4 *
5 *               Copyright (C) 1991-2004 SciTech Software, Inc.
6 *                    Copyright (C) David Mosberger-Tang
7 *                      Copyright (C) 1999 Egbert Eich
8 *
9 *  ========================================================================
10 *
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.
20 *
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.
28 *
29 *  ========================================================================
30 *
31 * Language:     ANSI C
32 * Environment:  Any
33 * Developer:    Kendall Bennett
34 *
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 that do nothing. For real uses these
38 *               functions will have to be overriden by the user library.
39 *
40 ****************************************************************************/
41 
42 #include <common.h>
43 
44 #if defined(CONFIG_BIOSEMU)
45 
46 #include "x86emu/x86emui.h"
47 
48 /*------------------------- Global Variables ------------------------------*/
49 
50 X86EMU_sysEnv _X86EMU_env;	/* Global emulator machine state */
51 X86EMU_intrFuncs _X86EMU_intrTab[256];
52 
53 int debug_intr;
54 
55 /*----------------------------- Implementation ----------------------------*/
56 
57 /****************************************************************************
58 PARAMETERS:
59 addr    - Emulator memory address to read
60 
61 RETURNS:
62 Byte value read from emulator memory.
63 
64 REMARKS:
65 Reads a byte value from the emulator memory.
66 ****************************************************************************/
67 u8 X86API rdb(u32 addr)
68 {
69 	return 0;
70 }
71 
72 /****************************************************************************
73 PARAMETERS:
74 addr    - Emulator memory address to read
75 
76 RETURNS:
77 Word value read from emulator memory.
78 
79 REMARKS:
80 Reads a word value from the emulator memory.
81 ****************************************************************************/
82 u16 X86API rdw(u32 addr)
83 {
84 	return 0;
85 }
86 
87 /****************************************************************************
88 PARAMETERS:
89 addr    - Emulator memory address to read
90 
91 RETURNS:
92 Long value read from emulator memory.
93 REMARKS:
94 Reads a long value from the emulator memory.
95 ****************************************************************************/
96 u32 X86API rdl(u32 addr)
97 {
98 	return 0;
99 }
100 
101 /****************************************************************************
102 PARAMETERS:
103 addr    - Emulator memory address to read
104 val     - Value to store
105 
106 REMARKS:
107 Writes a byte value to emulator memory.
108 ****************************************************************************/
109 void X86API wrb(u32 addr, u8 val)
110 {
111 }
112 
113 /****************************************************************************
114 PARAMETERS:
115 addr    - Emulator memory address to read
116 val     - Value to store
117 
118 REMARKS:
119 Writes a word value to emulator memory.
120 ****************************************************************************/
121 void X86API wrw(u32 addr, u16 val)
122 {
123 }
124 
125 /****************************************************************************
126 PARAMETERS:
127 addr    - Emulator memory address to read
128 val     - Value to store
129 
130 REMARKS:
131 Writes a long value to emulator memory.
132 ****************************************************************************/
133 void X86API wrl(u32 addr, u32 val)
134 {
135 }
136 
137 /****************************************************************************
138 PARAMETERS:
139 addr    - PIO address to read
140 RETURN:
141 0
142 REMARKS:
143 Default PIO byte read function. Doesn't perform real inb.
144 ****************************************************************************/
145 static u8 X86API p_inb(X86EMU_pioAddr addr)
146 {
147 	DB(if (DEBUG_IO_TRACE())
148 	   printk("inb %#04x \n", addr);)
149 		return 0;
150 }
151 
152 /****************************************************************************
153 PARAMETERS:
154 addr    - PIO address to read
155 RETURN:
156 0
157 REMARKS:
158 Default PIO word read function. Doesn't perform real inw.
159 ****************************************************************************/
160 static u16 X86API p_inw(X86EMU_pioAddr addr)
161 {
162 	DB(if (DEBUG_IO_TRACE())
163 	   printk("inw %#04x \n", addr);)
164 		return 0;
165 }
166 
167 /****************************************************************************
168 PARAMETERS:
169 addr    - PIO address to read
170 RETURN:
171 0
172 REMARKS:
173 Default PIO long read function. Doesn't perform real inl.
174 ****************************************************************************/
175 static u32 X86API p_inl(X86EMU_pioAddr addr)
176 {
177 	DB(if (DEBUG_IO_TRACE())
178 	   printk("inl %#04x \n", addr);)
179 		return 0;
180 }
181 
182 /****************************************************************************
183 PARAMETERS:
184 addr    - PIO address to write
185 val     - Value to store
186 REMARKS:
187 Default PIO byte write function. Doesn't perform real outb.
188 ****************************************************************************/
189 static void X86API p_outb(X86EMU_pioAddr addr, u8 val)
190 {
191 	DB(if (DEBUG_IO_TRACE())
192 	   printk("outb %#02x -> %#04x \n", val, addr);)
193 		return;
194 }
195 
196 /****************************************************************************
197 PARAMETERS:
198 addr    - PIO address to write
199 val     - Value to store
200 REMARKS:
201 Default PIO word write function. Doesn't perform real outw.
202 ****************************************************************************/
203 static void X86API p_outw(X86EMU_pioAddr addr, u16 val)
204 {
205 	DB(if (DEBUG_IO_TRACE())
206 	   printk("outw %#04x -> %#04x \n", val, addr);)
207 		return;
208 }
209 
210 /****************************************************************************
211 PARAMETERS:
212 addr    - PIO address to write
213 val     - Value to store
214 REMARKS:
215 Default PIO ;ong write function. Doesn't perform real outl.
216 ****************************************************************************/
217 static void X86API p_outl(X86EMU_pioAddr addr, u32 val)
218 {
219 	DB(if (DEBUG_IO_TRACE())
220 	   printk("outl %#08x -> %#04x \n", val, addr);)
221 		return;
222 }
223 
224 /*------------------------- Global Variables ------------------------------*/
225 
226 u8(X86APIP sys_rdb) (u32 addr) = rdb;
227 u16(X86APIP sys_rdw) (u32 addr) = rdw;
228 u32(X86APIP sys_rdl) (u32 addr) = rdl;
229 void (X86APIP sys_wrb) (u32 addr, u8 val) = wrb;
230 void (X86APIP sys_wrw) (u32 addr, u16 val) = wrw;
231 void (X86APIP sys_wrl) (u32 addr, u32 val) = wrl;
232 u8(X86APIP sys_inb) (X86EMU_pioAddr addr) = p_inb;
233 u16(X86APIP sys_inw) (X86EMU_pioAddr addr) = p_inw;
234 u32(X86APIP sys_inl) (X86EMU_pioAddr addr) = p_inl;
235 void (X86APIP sys_outb) (X86EMU_pioAddr addr, u8 val) = p_outb;
236 void (X86APIP sys_outw) (X86EMU_pioAddr addr, u16 val) = p_outw;
237 void (X86APIP sys_outl) (X86EMU_pioAddr addr, u32 val) = p_outl;
238 
239 /*----------------------------- Setup -------------------------------------*/
240 
241 /****************************************************************************
242 PARAMETERS:
243 funcs   - New memory function pointers to make active
244 
245 REMARKS:
246 This function is used to set the pointers to functions which access
247 memory space, allowing the user application to override these functions
248 and hook them out as necessary for their application.
249 ****************************************************************************/
250 void X86EMU_setupMemFuncs(X86EMU_memFuncs * funcs)
251 {
252 	sys_rdb = funcs->rdb;
253 	sys_rdw = funcs->rdw;
254 	sys_rdl = funcs->rdl;
255 	sys_wrb = funcs->wrb;
256 	sys_wrw = funcs->wrw;
257 	sys_wrl = funcs->wrl;
258 }
259 
260 /****************************************************************************
261 PARAMETERS:
262 funcs   - New programmed I/O function pointers to make active
263 
264 REMARKS:
265 This function is used to set the pointers to functions which access
266 I/O space, allowing the user application to override these functions
267 and hook them out as necessary for their application.
268 ****************************************************************************/
269 void X86EMU_setupPioFuncs(X86EMU_pioFuncs * funcs)
270 {
271 	sys_inb = funcs->inb;
272 	sys_inw = funcs->inw;
273 	sys_inl = funcs->inl;
274 	sys_outb = funcs->outb;
275 	sys_outw = funcs->outw;
276 	sys_outl = funcs->outl;
277 }
278 
279 /****************************************************************************
280 PARAMETERS:
281 funcs   - New interrupt vector table to make active
282 
283 REMARKS:
284 This function is used to set the pointers to functions which handle
285 interrupt processing in the emulator, allowing the user application to
286 hook interrupts as necessary for their application. Any interrupts that
287 are not hooked by the user application, and reflected and handled internally
288 in the emulator via the interrupt vector table. This allows the application
289 to get control when the code being emulated executes specific software
290 interrupts.
291 ****************************************************************************/
292 void X86EMU_setupIntrFuncs(X86EMU_intrFuncs funcs[])
293 {
294 	int i;
295 
296 	for (i = 0; i < 256; i++)
297 		_X86EMU_intrTab[i] = NULL;
298 	if (funcs) {
299 		for (i = 0; i < 256; i++)
300 			_X86EMU_intrTab[i] = funcs[i];
301 	}
302 }
303 
304 /****************************************************************************
305 PARAMETERS:
306 int - New software interrupt to prepare for
307 
308 REMARKS:
309 This function is used to set up the emulator state to exceute a software
310 interrupt. This can be used by the user application code to allow an
311 interrupt to be hooked, examined and then reflected back to the emulator
312 so that the code in the emulator will continue processing the software
313 interrupt as per normal. This essentially allows system code to actively
314 hook and handle certain software interrupts as necessary.
315 ****************************************************************************/
316 void X86EMU_prepareForInt(int num)
317 {
318 	push_word((u16) M.x86.R_FLG);
319 	CLEAR_FLAG(F_IF);
320 	CLEAR_FLAG(F_TF);
321 	push_word(M.x86.R_CS);
322 	M.x86.R_CS = mem_access_word(num * 4 + 2);
323 	push_word(M.x86.R_IP);
324 	M.x86.R_IP = mem_access_word(num * 4);
325 	M.x86.intr = 0;
326 }
327 
328 #endif
329