Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

emu_regs.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 *
00003 *                                               Realmode X86 Emulator Library
00004 *
00005 *               Copyright (C) 1996-1999 SciTech Software, Inc.
00006 *                                    Copyright (C) David Mosberger-Tang
00007 *                                          Copyright (C) 1999 Egbert Eich
00008 *
00009 *  ========================================================================
00010 *
00011 *  Permission to use, copy, modify, distribute, and sell this software and
00012 *  its documentation for any purpose is hereby granted without fee,
00013 *  provided that the above copyright notice appear in all copies and that
00014 *  both that copyright notice and this permission notice appear in
00015 *  supporting documentation, and that the name of the authors not be used
00016 *  in advertising or publicity pertaining to distribution of the software
00017 *  without specific, written prior permission.  The authors makes no
00018 *  representations about the suitability of this software for any purpose.
00019 *  It is provided "as is" without express or implied warranty.
00020 *
00021 *  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00022 *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
00023 *  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00024 *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
00025 *  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
00026 *  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00027 *  PERFORMANCE OF THIS SOFTWARE.
00028 *
00029 *  ========================================================================
00030 *
00031 * Language:             ANSI C
00032 * Environment:  Any
00033 * Developer:    Kendall Bennett
00034 *
00035 * Description:  Header file for x86 register definitions.
00036 *
00037 ****************************************************************************/
00038 /* $XFree86: xc/extras/x86emu/include/x86emu/regs.h,v 1.3 2001/10/28 03:32:25 tsi Exp $ */
00039 
00040 #ifndef __X86EMU_REGS_H
00041 #define __X86EMU_REGS_H
00042 
00043 /*---------------------- Macros and type definitions ----------------------*/
00044 
00045 #pragma pack(1)
00046 
00047 /*
00048  * General EAX, EBX, ECX, EDX type registers.  Note that for
00049  * portability, and speed, the issue of byte swapping is not addressed
00050  * in the registers.  All registers are stored in the default format
00051  * available on the host machine.  The only critical issue is that the
00052  * registers should line up EXACTLY in the same manner as they do in
00053  * the 386.  That is:
00054  *
00055  * EAX & 0xff  === AL
00056  * EAX & 0xffff == AX
00057  *
00058  * etc.  The result is that alot of the calculations can then be
00059  * done using the native instruction set fully.
00060  */
00061 
00062 #ifdef  __BIG_ENDIAN__
00063 
00064 typedef struct {
00065     u32 e_reg;
00066         } I32_reg_t;
00067 
00068 typedef struct {
00069         u16 filler0, x_reg;
00070         } I16_reg_t;
00071 
00072 typedef struct {
00073         u8 filler0, filler1, h_reg, l_reg;
00074         } I8_reg_t;
00075 
00076 #else /* !__BIG_ENDIAN__ */
00077 
00078 typedef struct {
00079     u32 e_reg;
00080         } I32_reg_t;
00081 
00082 typedef struct {
00083         u16 x_reg;
00084         } I16_reg_t;
00085 
00086 typedef struct {
00087         u8 l_reg, h_reg;
00088         } I8_reg_t;
00089 
00090 #endif /* BIG_ENDIAN */
00091 
00092 typedef union {
00093         I32_reg_t   I32_reg;
00094         I16_reg_t   I16_reg;
00095         I8_reg_t    I8_reg;
00096         } i386_general_register;
00097 
00098 struct i386_general_regs {
00099         i386_general_register A, B, C, D;
00100         };
00101 
00102 typedef struct i386_general_regs Gen_reg_t;
00103 
00104 struct i386_special_regs {
00105         i386_general_register SP, BP, SI, DI, IP;
00106         u32 FLAGS;
00107         };
00108 
00109 /*  
00110  * Segment registers here represent the 16 bit quantities
00111  * CS, DS, ES, SS.
00112  */
00113 
00114 struct i386_segment_regs {
00115     u16 CS, DS, SS, ES, FS, GS;
00116         };
00117 
00118 /* 8 bit registers */
00119 #define R_AH  gen.A.I8_reg.h_reg
00120 #define R_AL  gen.A.I8_reg.l_reg
00121 #define R_BH  gen.B.I8_reg.h_reg
00122 #define R_BL  gen.B.I8_reg.l_reg
00123 #define R_CH  gen.C.I8_reg.h_reg
00124 #define R_CL  gen.C.I8_reg.l_reg
00125 #define R_DH  gen.D.I8_reg.h_reg
00126 #define R_DL  gen.D.I8_reg.l_reg
00127 
00128 /* 16 bit registers */
00129 #define R_AX  gen.A.I16_reg.x_reg
00130 #define R_BX  gen.B.I16_reg.x_reg
00131 #define R_CX  gen.C.I16_reg.x_reg
00132 #define R_DX  gen.D.I16_reg.x_reg
00133 
00134 /* 32 bit extended registers */
00135 #define R_EAX  gen.A.I32_reg.e_reg
00136 #define R_EBX  gen.B.I32_reg.e_reg
00137 #define R_ECX  gen.C.I32_reg.e_reg
00138 #define R_EDX  gen.D.I32_reg.e_reg
00139 
00140 /* special registers */
00141 #define R_SP  spc.SP.I16_reg.x_reg
00142 #define R_BP  spc.BP.I16_reg.x_reg
00143 #define R_SI  spc.SI.I16_reg.x_reg
00144 #define R_DI  spc.DI.I16_reg.x_reg
00145 #define R_IP  spc.IP.I16_reg.x_reg
00146 #define R_FLG spc.FLAGS
00147 
00148 /* special registers */
00149 #define R_SP  spc.SP.I16_reg.x_reg
00150 #define R_BP  spc.BP.I16_reg.x_reg
00151 #define R_SI  spc.SI.I16_reg.x_reg
00152 #define R_DI  spc.DI.I16_reg.x_reg
00153 #define R_IP  spc.IP.I16_reg.x_reg
00154 #define R_FLG spc.FLAGS
00155 
00156 /* special registers */
00157 #define R_ESP  spc.SP.I32_reg.e_reg
00158 #define R_EBP  spc.BP.I32_reg.e_reg
00159 #define R_ESI  spc.SI.I32_reg.e_reg
00160 #define R_EDI  spc.DI.I32_reg.e_reg
00161 #define R_EIP  spc.IP.I32_reg.e_reg
00162 #define R_EFLG spc.FLAGS
00163 
00164 /* segment registers */
00165 #define R_CS  seg.CS
00166 #define R_DS  seg.DS
00167 #define R_SS  seg.SS
00168 #define R_ES  seg.ES
00169 #define R_FS  seg.FS
00170 #define R_GS  seg.GS
00171 
00172 /* flag conditions   */
00173 #define FB_CF 0x0001            /* CARRY flag  */
00174 #define FB_PF 0x0004            /* PARITY flag */
00175 #define FB_AF 0x0010            /* AUX  flag   */
00176 #define FB_ZF 0x0040            /* ZERO flag   */
00177 #define FB_SF 0x0080            /* SIGN flag   */
00178 #define FB_TF 0x0100            /* TRAP flag   */
00179 #define FB_IF 0x0200            /* INTERRUPT ENABLE flag */
00180 #define FB_DF 0x0400            /* DIR flag    */
00181 #define FB_OF 0x0800            /* OVERFLOW flag */
00182 
00183 /* 80286 and above always have bit#1 set */
00184 #define F_ALWAYS_ON  (0x0002)   /* flag bits always on */
00185 
00186 /*
00187  * Define a mask for only those flag bits we will ever pass back 
00188  * (via PUSHF) 
00189  */
00190 #define F_MSK (FB_CF|FB_PF|FB_AF|FB_ZF|FB_SF|FB_TF|FB_IF|FB_DF|FB_OF)
00191 
00192 /* following bits masked in to a 16bit quantity */
00193 
00194 #define F_CF 0x0001             /* CARRY flag  */
00195 #define F_PF 0x0004             /* PARITY flag */
00196 #define F_AF 0x0010             /* AUX  flag   */
00197 #define F_ZF 0x0040             /* ZERO flag   */
00198 #define F_SF 0x0080             /* SIGN flag   */
00199 #define F_TF 0x0100             /* TRAP flag   */
00200 #define F_IF 0x0200             /* INTERRUPT ENABLE flag */
00201 #define F_DF 0x0400             /* DIR flag    */
00202 #define F_OF 0x0800             /* OVERFLOW flag */
00203 
00204 #define TOGGLE_FLAG(flag)       (M.x86.R_FLG ^= (flag))
00205 #define SET_FLAG(flag)          (M.x86.R_FLG |= (flag))
00206 #define CLEAR_FLAG(flag)        (M.x86.R_FLG &= ~(flag))
00207 #define ACCESS_FLAG(flag)       (M.x86.R_FLG & (flag))
00208 #define CLEARALL_FLAG(m)        (M.x86.R_FLG = 0)
00209 
00210 #define CONDITIONAL_SET_FLAG(COND,FLAG) \
00211   if (COND) SET_FLAG(FLAG); else CLEAR_FLAG(FLAG)
00212 
00213 #define F_PF_CALC 0x010000      /* PARITY flag has been calced    */
00214 #define F_ZF_CALC 0x020000      /* ZERO flag has been calced      */
00215 #define F_SF_CALC 0x040000      /* SIGN flag has been calced      */
00216 
00217 #define F_ALL_CALC      0xff0000        /* All have been calced   */
00218 
00219 /*
00220  * Emulator machine state.
00221  * Segment usage control.
00222  */
00223 #define SYSMODE_SEG_DS_SS       0x00000001
00224 #define SYSMODE_SEGOVR_CS       0x00000002
00225 #define SYSMODE_SEGOVR_DS       0x00000004
00226 #define SYSMODE_SEGOVR_ES       0x00000008
00227 #define SYSMODE_SEGOVR_FS       0x00000010
00228 #define SYSMODE_SEGOVR_GS       0x00000020
00229 #define SYSMODE_SEGOVR_SS       0x00000040
00230 #define SYSMODE_PREFIX_REPE     0x00000080
00231 #define SYSMODE_PREFIX_REPNE    0x00000100
00232 #define SYSMODE_PREFIX_DATA     0x00000200
00233 #define SYSMODE_PREFIX_ADDR     0x00000400
00234 #define SYSMODE_INTR_PENDING    0x10000000
00235 #define SYSMODE_EXTRN_INTR      0x20000000
00236 #define SYSMODE_HALTED          0x40000000
00237 
00238 #define SYSMODE_SEGMASK (SYSMODE_SEG_DS_SS      | \
00239                                                  SYSMODE_SEGOVR_CS      | \
00240                                                  SYSMODE_SEGOVR_DS      | \
00241                                                  SYSMODE_SEGOVR_ES      | \
00242                                                  SYSMODE_SEGOVR_FS      | \
00243                                                  SYSMODE_SEGOVR_GS      | \
00244                                                  SYSMODE_SEGOVR_SS)
00245 #define SYSMODE_CLRMASK (SYSMODE_SEG_DS_SS      | \
00246                                                  SYSMODE_SEGOVR_CS      | \
00247                                                  SYSMODE_SEGOVR_DS      | \
00248                                                  SYSMODE_SEGOVR_ES      | \
00249                                                  SYSMODE_SEGOVR_FS      | \
00250                                                  SYSMODE_SEGOVR_GS      | \
00251                                                  SYSMODE_SEGOVR_SS      | \
00252                                                  SYSMODE_PREFIX_DATA    | \
00253                                                  SYSMODE_PREFIX_ADDR)
00254 
00255 #define  INTR_SYNCH           0x1
00256 #define  INTR_ASYNCH          0x2
00257 #define  INTR_HALTED          0x4
00258 
00259 typedef struct {
00260     struct i386_general_regs    gen;
00261     struct i386_special_regs    spc;
00262     struct i386_segment_regs    seg;
00263     /*
00264      * MODE contains information on:
00265      *  REPE prefix             2 bits  repe,repne
00266      *  SEGMENT overrides       5 bits  normal,DS,SS,CS,ES
00267      *  Delayed flag set        3 bits  (zero, signed, parity)
00268      *  reserved                6 bits
00269      *  interrupt #             8 bits  instruction raised interrupt
00270      *  BIOS video segregs      4 bits  
00271      *  Interrupt Pending       1 bits  
00272      *  Extern interrupt        1 bits
00273      *  Halted                  1 bits
00274      */
00275     u32                         mode;
00276     volatile int                intr;   /* mask of pending interrupts */
00277         int                         debug;
00278 #ifdef DEBUG
00279         int                         check;
00280     u16                         saved_ip;
00281     u16                         saved_cs;
00282     int                         enc_pos;
00283     int                         enc_str_pos;
00284     char                        decode_buf[32]; /* encoded byte stream  */
00285     char                        decoded_buf[256]; /* disassembled strings */
00286 #endif
00287     u8                          intno;
00288     u8                          __pad[3];
00289         } X86EMU_regs;
00290 
00291 /****************************************************************************
00292 REMARKS:
00293 Structure maintaining the emulator machine state.
00294 
00295 MEMBERS:
00296 mem_base                - Base real mode memory for the emulator
00297 mem_size                - Size of the real mode memory block for the emulator
00298 private                 - private data pointer
00299 x86                     - X86 registers
00300 ****************************************************************************/
00301 typedef struct {
00302         unsigned long   mem_base;
00303         unsigned long   mem_size;
00304         void*           private;
00305         X86EMU_regs             x86;
00306         } X86EMU_sysEnv;
00307 
00308 #pragma pack()
00309 
00310 /*----------------------------- Global Variables --------------------------*/
00311 
00312 /* Global emulator machine state.
00313  *
00314  * We keep it global to avoid pointer dereferences in the code for speed.
00315  */
00316 
00317 extern    X86EMU_sysEnv _X86EMU_env;
00318 #define   M             _X86EMU_env
00319                 
00320 /*-------------------------- Function Prototypes --------------------------*/
00321 
00322 /* Function to log information at runtime */
00323 
00324 void    printk(const char *fmt, ...);
00325 
00326 #endif /* __X86EMU_REGS_H */