This patch unifies the use of config options in v2 to all start with CONFIG_
[coreboot.git] / src / include / x86emu / regs.h
1 /****************************************************************************
2 *
3 *                                               Realmode X86 Emulator Library
4 *
5 *               Copyright (C) 1996-1999 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:  Header file for x86 register definitions.
36 *
37 ****************************************************************************/
38 /* $XFree86: xc/extras/x86emu/include/x86emu/regs.h,v 1.3 2001/10/28 03:32:25 tsi Exp $ */
39
40 #ifndef __X86EMU_REGS_H
41 #define __X86EMU_REGS_H
42
43 #if defined(CONFIG_DEBUG) && (CONFIG_DEBUG == 0)
44 #undef CONFIG_DEBUG
45 #endif
46
47 /*---------------------- Macros and type definitions ----------------------*/
48
49 #pragma pack(1)
50
51 /*
52  * General EAX, EBX, ECX, EDX type registers.  Note that for
53  * portability, and speed, the issue of byte swapping is not addressed
54  * in the registers.  All registers are stored in the default format
55  * available on the host machine.  The only critical issue is that the
56  * registers should line up EXACTLY in the same manner as they do in
57  * the 386.  That is:
58  *
59  * EAX & 0xff  === AL
60  * EAX & 0xffff == AX
61  *
62  * etc.  The result is that alot of the calculations can then be
63  * done using the native instruction set fully.
64  */
65
66 #ifdef  __BIG_ENDIAN__
67
68 typedef struct {
69     u32 e_reg;
70         } I32_reg_t;
71
72 typedef struct {
73         u16 filler0, x_reg;
74         } I16_reg_t;
75
76 typedef struct {
77         u8 filler0, filler1, h_reg, l_reg;
78         } I8_reg_t;
79
80 #else /* !__BIG_ENDIAN__ */
81
82 typedef struct {
83     u32 e_reg;
84         } I32_reg_t;
85
86 typedef struct {
87         u16 x_reg;
88         } I16_reg_t;
89
90 typedef struct {
91         u8 l_reg, h_reg;
92         } I8_reg_t;
93
94 #endif /* BIG_ENDIAN */
95
96 typedef union {
97         I32_reg_t   I32_reg;
98         I16_reg_t   I16_reg;
99         I8_reg_t    I8_reg;
100         } i386_general_register;
101
102 struct i386_general_regs {
103         i386_general_register A, B, C, D;
104         };
105
106 typedef struct i386_general_regs Gen_reg_t;
107
108 struct i386_special_regs {
109         i386_general_register SP, BP, SI, DI, IP;
110         u32 FLAGS;
111         };
112
113 /*  
114  * Segment registers here represent the 16 bit quantities
115  * CS, DS, ES, SS.
116  */
117
118 struct i386_segment_regs {
119     u16 CS, DS, SS, ES, FS, GS;
120         };
121
122 /* 8 bit registers */
123 #define R_AH  gen.A.I8_reg.h_reg
124 #define R_AL  gen.A.I8_reg.l_reg
125 #define R_BH  gen.B.I8_reg.h_reg
126 #define R_BL  gen.B.I8_reg.l_reg
127 #define R_CH  gen.C.I8_reg.h_reg
128 #define R_CL  gen.C.I8_reg.l_reg
129 #define R_DH  gen.D.I8_reg.h_reg
130 #define R_DL  gen.D.I8_reg.l_reg
131
132 /* 16 bit registers */
133 #define R_AX  gen.A.I16_reg.x_reg
134 #define R_BX  gen.B.I16_reg.x_reg
135 #define R_CX  gen.C.I16_reg.x_reg
136 #define R_DX  gen.D.I16_reg.x_reg
137
138 /* 32 bit extended registers */
139 #define R_EAX  gen.A.I32_reg.e_reg
140 #define R_EBX  gen.B.I32_reg.e_reg
141 #define R_ECX  gen.C.I32_reg.e_reg
142 #define R_EDX  gen.D.I32_reg.e_reg
143
144 /* special registers */
145 #define R_SP  spc.SP.I16_reg.x_reg
146 #define R_BP  spc.BP.I16_reg.x_reg
147 #define R_SI  spc.SI.I16_reg.x_reg
148 #define R_DI  spc.DI.I16_reg.x_reg
149 #define R_IP  spc.IP.I16_reg.x_reg
150 #define R_FLG spc.FLAGS
151
152 /* special registers */
153 #define R_SP  spc.SP.I16_reg.x_reg
154 #define R_BP  spc.BP.I16_reg.x_reg
155 #define R_SI  spc.SI.I16_reg.x_reg
156 #define R_DI  spc.DI.I16_reg.x_reg
157 #define R_IP  spc.IP.I16_reg.x_reg
158 #define R_FLG spc.FLAGS
159
160 /* special registers */
161 #define R_ESP  spc.SP.I32_reg.e_reg
162 #define R_EBP  spc.BP.I32_reg.e_reg
163 #define R_ESI  spc.SI.I32_reg.e_reg
164 #define R_EDI  spc.DI.I32_reg.e_reg
165 #define R_EIP  spc.IP.I32_reg.e_reg
166 #define R_EFLG spc.FLAGS
167
168 /* segment registers */
169 #define R_CS  seg.CS
170 #define R_DS  seg.DS
171 #define R_SS  seg.SS
172 #define R_ES  seg.ES
173 #define R_FS  seg.FS
174 #define R_GS  seg.GS
175
176 /* flag conditions   */
177 #define FB_CF 0x0001            /* CARRY flag  */
178 #define FB_PF 0x0004            /* PARITY flag */
179 #define FB_AF 0x0010            /* AUX  flag   */
180 #define FB_ZF 0x0040            /* ZERO flag   */
181 #define FB_SF 0x0080            /* SIGN flag   */
182 #define FB_TF 0x0100            /* TRAP flag   */
183 #define FB_IF 0x0200            /* INTERRUPT ENABLE flag */
184 #define FB_DF 0x0400            /* DIR flag    */
185 #define FB_OF 0x0800            /* OVERFLOW flag */
186
187 /* 80286 and above always have bit#1 set */
188 #define F_ALWAYS_ON  (0x0002)   /* flag bits always on */
189
190 /*
191  * Define a mask for only those flag bits we will ever pass back 
192  * (via PUSHF) 
193  */
194 #define F_MSK (FB_CF|FB_PF|FB_AF|FB_ZF|FB_SF|FB_TF|FB_IF|FB_DF|FB_OF)
195
196 /* following bits masked in to a 16bit quantity */
197
198 #define F_CF 0x0001             /* CARRY flag  */
199 #define F_PF 0x0004             /* PARITY flag */
200 #define F_AF 0x0010             /* AUX  flag   */
201 #define F_ZF 0x0040             /* ZERO flag   */
202 #define F_SF 0x0080             /* SIGN flag   */
203 #define F_TF 0x0100             /* TRAP flag   */
204 #define F_IF 0x0200             /* INTERRUPT ENABLE flag */
205 #define F_DF 0x0400             /* DIR flag    */
206 #define F_OF 0x0800             /* OVERFLOW flag */
207
208 #define TOGGLE_FLAG(flag)       (M.x86.R_FLG ^= (flag))
209 #define SET_FLAG(flag)          (M.x86.R_FLG |= (flag))
210 #define CLEAR_FLAG(flag)        (M.x86.R_FLG &= ~(flag))
211 #define ACCESS_FLAG(flag)       (M.x86.R_FLG & (flag))
212 #define CLEARALL_FLAG(m)        (M.x86.R_FLG = 0)
213
214 #define CONDITIONAL_SET_FLAG(COND,FLAG) \
215   if (COND) SET_FLAG(FLAG); else CLEAR_FLAG(FLAG)
216
217 #define F_PF_CALC 0x010000      /* PARITY flag has been calced    */
218 #define F_ZF_CALC 0x020000      /* ZERO flag has been calced      */
219 #define F_SF_CALC 0x040000      /* SIGN flag has been calced      */
220
221 #define F_ALL_CALC      0xff0000        /* All have been calced   */
222
223 /*
224  * Emulator machine state.
225  * Segment usage control.
226  */
227 #define SYSMODE_SEG_DS_SS       0x00000001
228 #define SYSMODE_SEGOVR_CS       0x00000002
229 #define SYSMODE_SEGOVR_DS       0x00000004
230 #define SYSMODE_SEGOVR_ES       0x00000008
231 #define SYSMODE_SEGOVR_FS       0x00000010
232 #define SYSMODE_SEGOVR_GS       0x00000020
233 #define SYSMODE_SEGOVR_SS       0x00000040
234 #define SYSMODE_PREFIX_REPE     0x00000080
235 #define SYSMODE_PREFIX_REPNE    0x00000100
236 #define SYSMODE_PREFIX_DATA     0x00000200
237 #define SYSMODE_PREFIX_ADDR     0x00000400
238 // for REP(E|NE) Instructions, we need to decide wether it should be using
239 // the 32bit ECX register as or the 16bit CX register as count register
240 #define SYSMODE_32BIT_REP       0x00000800
241 #define SYSMODE_INTR_PENDING    0x10000000
242 #define SYSMODE_EXTRN_INTR      0x20000000
243 #define SYSMODE_HALTED          0x40000000
244
245 #define SYSMODE_SEGMASK (SYSMODE_SEG_DS_SS      | \
246                                                  SYSMODE_SEGOVR_CS      | \
247                                                  SYSMODE_SEGOVR_DS      | \
248                                                  SYSMODE_SEGOVR_ES      | \
249                                                  SYSMODE_SEGOVR_FS      | \
250                                                  SYSMODE_SEGOVR_GS      | \
251                                                  SYSMODE_SEGOVR_SS)
252 #define SYSMODE_CLRMASK (SYSMODE_SEG_DS_SS      | \
253                                                  SYSMODE_SEGOVR_CS      | \
254                                                  SYSMODE_SEGOVR_DS      | \
255                                                  SYSMODE_SEGOVR_ES      | \
256                                                  SYSMODE_SEGOVR_FS      | \
257                                                  SYSMODE_SEGOVR_GS      | \
258                                                  SYSMODE_SEGOVR_SS      | \
259                                                  SYSMODE_PREFIX_DATA    | \
260                                                  SYSMODE_PREFIX_ADDR    | \
261                                                  SYSMODE_32BIT_REP)
262
263 #define  INTR_SYNCH           0x1
264 #define  INTR_ASYNCH          0x2
265 #define  INTR_HALTED          0x4
266
267 typedef struct {
268     struct i386_general_regs    gen;
269     struct i386_special_regs    spc;
270     struct i386_segment_regs    seg;
271     /*
272      * MODE contains information on:
273      *  REPE prefix             2 bits  repe,repne
274      *  SEGMENT overrides       5 bits  normal,DS,SS,CS,ES
275      *  Delayed flag set        3 bits  (zero, signed, parity)
276      *  reserved                6 bits
277      *  interrupt #             8 bits  instruction raised interrupt
278      *  BIOS video segregs      4 bits  
279      *  Interrupt Pending       1 bits  
280      *  Extern interrupt        1 bits
281      *  Halted                  1 bits
282      */
283     u32                         mode;
284     volatile int                intr;   /* mask of pending interrupts */
285     volatile int                         debug;
286 #ifdef CONFIG_DEBUG
287     int                         check;
288     u16                         saved_ip;
289     u16                         saved_cs;
290     int                         enc_pos;
291     int                         enc_str_pos;
292     char                        decode_buf[32]; /* encoded byte stream  */
293     char                        decoded_buf[256]; /* disassembled strings */
294 #endif
295     u8                          intno;
296     u8                          __pad[3];
297         } X86EMU_regs;
298
299 /****************************************************************************
300 REMARKS:
301 Structure maintaining the emulator machine state.
302
303 MEMBERS:
304 mem_base                - Base real mode memory for the emulator
305 abseg                   - Base for the absegment
306 mem_size                - Size of the real mode memory block for the emulator
307 private                 - private data pointer
308 x86                     - X86 registers
309 ****************************************************************************/
310 typedef struct {
311         unsigned long   mem_base;
312         unsigned long   mem_size;
313         unsigned long   abseg;
314         void*           private;
315         X86EMU_regs             x86;
316         } X86EMU_sysEnv;
317
318 #pragma pack()
319
320 /*----------------------------- Global Variables --------------------------*/
321
322 #ifdef  __cplusplus
323 extern "C" {                                    /* Use "C" linkage when in C++ mode */
324 #endif
325
326 /* Global emulator machine state.
327  *
328  * We keep it global to avoid pointer dereferences in the code for speed.
329  */
330
331 extern    X86EMU_sysEnv _X86EMU_env;
332 #define   M             _X86EMU_env
333
334 #define X86_EAX M.x86.R_EAX
335 #define X86_EBX M.x86.R_EBX
336 #define X86_ECX M.x86.R_ECX
337 #define X86_EDX M.x86.R_EDX
338 #define X86_ESI M.x86.R_ESI
339 #define X86_EDI M.x86.R_EDI
340 #define X86_EBP M.x86.R_EBP
341 #define X86_EIP M.x86.R_EIP
342 #define X86_ESP M.x86.R_ESP
343 #define X86_EFLAGS M.x86.R_EFLG
344
345 #define X86_FLAGS M.x86.R_FLG
346 #define X86_AX M.x86.R_AX
347 #define X86_BX M.x86.R_BX
348 #define X86_CX M.x86.R_CX
349 #define X86_DX M.x86.R_DX
350 #define X86_SI M.x86.R_SI
351 #define X86_DI M.x86.R_DI
352 #define X86_BP M.x86.R_BP
353 #define X86_IP M.x86.R_IP
354 #define X86_SP M.x86.R_SP
355 #define X86_CS M.x86.R_CS
356 #define X86_DS M.x86.R_DS
357 #define X86_ES M.x86.R_ES
358 #define X86_SS M.x86.R_SS
359 #define X86_FS M.x86.R_FS
360 #define X86_GS M.x86.R_GS
361
362 #define X86_AL M.x86.R_AL
363 #define X86_BL M.x86.R_BL
364 #define X86_CL M.x86.R_CL
365 #define X86_DL M.x86.R_DL
366
367 #define X86_AH M.x86.R_AH
368 #define X86_BH M.x86.R_BH
369 #define X86_CH M.x86.R_CH
370 #define X86_DH M.x86.R_DH
371
372                 
373 /*-------------------------- Function Prototypes --------------------------*/
374
375 /* Function to log information at runtime */
376
377 //void  printk(const char *fmt, ...);
378
379 #ifdef  __cplusplus
380 }                                               /* End of "C" linkage for C++           */
381 #endif
382
383 #endif /* __X86EMU_REGS_H */