regs.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifndef __X86EMU_REGS_H
  39. #define __X86EMU_REGS_H
  40. /*---------------------- Macros and type definitions ----------------------*/
  41. #pragma pack(1)
  42. /*
  43. * General EAX, EBX, ECX, EDX type registers. Note that for
  44. * portability, and speed, the issue of byte swapping is not addressed
  45. * in the registers. All registers are stored in the default format
  46. * available on the host machine. The only critical issue is that the
  47. * registers should line up EXACTLY in the same manner as they do in
  48. * the 386. That is:
  49. *
  50. * EAX & 0xff === AL
  51. * EAX & 0xffff == AX
  52. *
  53. * etc. The result is that alot of the calculations can then be
  54. * done using the native instruction set fully.
  55. */
  56. #ifdef __BIG_ENDIAN__
  57. typedef struct {
  58. u32 e_reg;
  59. } I32_reg_t;
  60. typedef struct {
  61. u16 filler0, x_reg;
  62. } I16_reg_t;
  63. typedef struct {
  64. u8 filler0, filler1, h_reg, l_reg;
  65. } I8_reg_t;
  66. #else /* !__BIG_ENDIAN__ */
  67. typedef struct {
  68. u32 e_reg;
  69. } I32_reg_t;
  70. typedef struct {
  71. u16 x_reg;
  72. } I16_reg_t;
  73. typedef struct {
  74. u8 l_reg, h_reg;
  75. } I8_reg_t;
  76. #endif /* BIG_ENDIAN */
  77. typedef union {
  78. I32_reg_t I32_reg;
  79. I16_reg_t I16_reg;
  80. I8_reg_t I8_reg;
  81. } i386_general_register;
  82. struct i386_general_regs {
  83. i386_general_register A, B, C, D;
  84. };
  85. typedef struct i386_general_regs Gen_reg_t;
  86. struct i386_special_regs {
  87. i386_general_register SP, BP, SI, DI, IP;
  88. u32 FLAGS;
  89. };
  90. /*
  91. * Segment registers here represent the 16 bit quantities
  92. * CS, DS, ES, SS.
  93. */
  94. struct i386_segment_regs {
  95. u16 CS, DS, SS, ES, FS, GS;
  96. };
  97. /* 8 bit registers */
  98. #define R_AH gen.A.I8_reg.h_reg
  99. #define R_AL gen.A.I8_reg.l_reg
  100. #define R_BH gen.B.I8_reg.h_reg
  101. #define R_BL gen.B.I8_reg.l_reg
  102. #define R_CH gen.C.I8_reg.h_reg
  103. #define R_CL gen.C.I8_reg.l_reg
  104. #define R_DH gen.D.I8_reg.h_reg
  105. #define R_DL gen.D.I8_reg.l_reg
  106. /* 16 bit registers */
  107. #define R_AX gen.A.I16_reg.x_reg
  108. #define R_BX gen.B.I16_reg.x_reg
  109. #define R_CX gen.C.I16_reg.x_reg
  110. #define R_DX gen.D.I16_reg.x_reg
  111. /* 32 bit extended registers */
  112. #define R_EAX gen.A.I32_reg.e_reg
  113. #define R_EBX gen.B.I32_reg.e_reg
  114. #define R_ECX gen.C.I32_reg.e_reg
  115. #define R_EDX gen.D.I32_reg.e_reg
  116. /* special registers */
  117. #define R_SP spc.SP.I16_reg.x_reg
  118. #define R_BP spc.BP.I16_reg.x_reg
  119. #define R_SI spc.SI.I16_reg.x_reg
  120. #define R_DI spc.DI.I16_reg.x_reg
  121. #define R_IP spc.IP.I16_reg.x_reg
  122. #define R_FLG spc.FLAGS
  123. /* special registers */
  124. #define R_SP spc.SP.I16_reg.x_reg
  125. #define R_BP spc.BP.I16_reg.x_reg
  126. #define R_SI spc.SI.I16_reg.x_reg
  127. #define R_DI spc.DI.I16_reg.x_reg
  128. #define R_IP spc.IP.I16_reg.x_reg
  129. #define R_FLG spc.FLAGS
  130. /* special registers */
  131. #define R_ESP spc.SP.I32_reg.e_reg
  132. #define R_EBP spc.BP.I32_reg.e_reg
  133. #define R_ESI spc.SI.I32_reg.e_reg
  134. #define R_EDI spc.DI.I32_reg.e_reg
  135. #define R_EIP spc.IP.I32_reg.e_reg
  136. #define R_EFLG spc.FLAGS
  137. /* segment registers */
  138. #define R_CS seg.CS
  139. #define R_DS seg.DS
  140. #define R_SS seg.SS
  141. #define R_ES seg.ES
  142. #define R_FS seg.FS
  143. #define R_GS seg.GS
  144. /* flag conditions */
  145. #define FB_CF 0x0001 /* CARRY flag */
  146. #define FB_PF 0x0004 /* PARITY flag */
  147. #define FB_AF 0x0010 /* AUX flag */
  148. #define FB_ZF 0x0040 /* ZERO flag */
  149. #define FB_SF 0x0080 /* SIGN flag */
  150. #define FB_TF 0x0100 /* TRAP flag */
  151. #define FB_IF 0x0200 /* INTERRUPT ENABLE flag */
  152. #define FB_DF 0x0400 /* DIR flag */
  153. #define FB_OF 0x0800 /* OVERFLOW flag */
  154. /* 80286 and above always have bit#1 set */
  155. #define F_ALWAYS_ON (0x0002) /* flag bits always on */
  156. /*
  157. * Define a mask for only those flag bits we will ever pass back
  158. * (via PUSHF)
  159. */
  160. #define F_MSK (FB_CF|FB_PF|FB_AF|FB_ZF|FB_SF|FB_TF|FB_IF|FB_DF|FB_OF)
  161. /* following bits masked in to a 16bit quantity */
  162. #define F_CF 0x0001 /* CARRY flag */
  163. #define F_PF 0x0004 /* PARITY flag */
  164. #define F_AF 0x0010 /* AUX flag */
  165. #define F_ZF 0x0040 /* ZERO flag */
  166. #define F_SF 0x0080 /* SIGN flag */
  167. #define F_TF 0x0100 /* TRAP flag */
  168. #define F_IF 0x0200 /* INTERRUPT ENABLE flag */
  169. #define F_DF 0x0400 /* DIR flag */
  170. #define F_OF 0x0800 /* OVERFLOW flag */
  171. #define TOGGLE_FLAG(flag) (M.x86.R_FLG ^= (flag))
  172. #define SET_FLAG(flag) (M.x86.R_FLG |= (flag))
  173. #define CLEAR_FLAG(flag) (M.x86.R_FLG &= ~(flag))
  174. #define ACCESS_FLAG(flag) (M.x86.R_FLG & (flag))
  175. #define CLEARALL_FLAG(m) (M.x86.R_FLG = 0)
  176. #define CONDITIONAL_SET_FLAG(COND,FLAG) \
  177. if (COND) SET_FLAG(FLAG); else CLEAR_FLAG(FLAG)
  178. #define F_PF_CALC 0x010000 /* PARITY flag has been calced */
  179. #define F_ZF_CALC 0x020000 /* ZERO flag has been calced */
  180. #define F_SF_CALC 0x040000 /* SIGN flag has been calced */
  181. #define F_ALL_CALC 0xff0000 /* All have been calced */
  182. /*
  183. * Emulator machine state.
  184. * Segment usage control.
  185. */
  186. #define SYSMODE_SEG_DS_SS 0x00000001
  187. #define SYSMODE_SEGOVR_CS 0x00000002
  188. #define SYSMODE_SEGOVR_DS 0x00000004
  189. #define SYSMODE_SEGOVR_ES 0x00000008
  190. #define SYSMODE_SEGOVR_FS 0x00000010
  191. #define SYSMODE_SEGOVR_GS 0x00000020
  192. #define SYSMODE_SEGOVR_SS 0x00000040
  193. #define SYSMODE_PREFIX_REPE 0x00000080
  194. #define SYSMODE_PREFIX_REPNE 0x00000100
  195. #define SYSMODE_PREFIX_DATA 0x00000200
  196. #define SYSMODE_PREFIX_ADDR 0x00000400
  197. #define SYSMODE_INTR_PENDING 0x10000000
  198. #define SYSMODE_EXTRN_INTR 0x20000000
  199. #define SYSMODE_HALTED 0x40000000
  200. #define SYSMODE_SEGMASK (SYSMODE_SEG_DS_SS | \
  201. SYSMODE_SEGOVR_CS | \
  202. SYSMODE_SEGOVR_DS | \
  203. SYSMODE_SEGOVR_ES | \
  204. SYSMODE_SEGOVR_FS | \
  205. SYSMODE_SEGOVR_GS | \
  206. SYSMODE_SEGOVR_SS)
  207. #define SYSMODE_CLRMASK (SYSMODE_SEG_DS_SS | \
  208. SYSMODE_SEGOVR_CS | \
  209. SYSMODE_SEGOVR_DS | \
  210. SYSMODE_SEGOVR_ES | \
  211. SYSMODE_SEGOVR_FS | \
  212. SYSMODE_SEGOVR_GS | \
  213. SYSMODE_SEGOVR_SS | \
  214. SYSMODE_PREFIX_DATA | \
  215. SYSMODE_PREFIX_ADDR)
  216. #define INTR_SYNCH 0x1
  217. #define INTR_ASYNCH 0x2
  218. #define INTR_HALTED 0x4
  219. typedef struct {
  220. struct i386_general_regs gen;
  221. struct i386_special_regs spc;
  222. struct i386_segment_regs seg;
  223. /*
  224. * MODE contains information on:
  225. * REPE prefix 2 bits repe,repne
  226. * SEGMENT overrides 5 bits normal,DS,SS,CS,ES
  227. * Delayed flag set 3 bits (zero, signed, parity)
  228. * reserved 6 bits
  229. * interrupt # 8 bits instruction raised interrupt
  230. * BIOS video segregs 4 bits
  231. * Interrupt Pending 1 bits
  232. * Extern interrupt 1 bits
  233. * Halted 1 bits
  234. */
  235. long mode;
  236. u8 intno;
  237. volatile int intr; /* mask of pending interrupts */
  238. int debug;
  239. #ifdef DEBUG
  240. int check;
  241. u16 saved_ip;
  242. u16 saved_cs;
  243. int enc_pos;
  244. int enc_str_pos;
  245. char decode_buf[32]; /* encoded byte stream */
  246. char decoded_buf[256]; /* disassembled strings */
  247. #endif
  248. } X86EMU_regs;
  249. /****************************************************************************
  250. REMARKS:
  251. Structure maintaining the emulator machine state.
  252. MEMBERS:
  253. x86 - X86 registers
  254. mem_base - Base real mode memory for the emulator
  255. mem_size - Size of the real mode memory block for the emulator
  256. ****************************************************************************/
  257. typedef struct {
  258. X86EMU_regs x86;
  259. unsigned long mem_base;
  260. unsigned long mem_size;
  261. void* private;
  262. } X86EMU_sysEnv;
  263. #pragma pack()
  264. /*----------------------------- Global Variables --------------------------*/
  265. #ifdef __cplusplus
  266. extern "C" { /* Use "C" linkage when in C++ mode */
  267. #endif
  268. /* Global emulator machine state.
  269. *
  270. * We keep it global to avoid pointer dereferences in the code for speed.
  271. */
  272. extern X86EMU_sysEnv _X86EMU_env;
  273. #define M _X86EMU_env
  274. /*-------------------------- Function Prototypes --------------------------*/
  275. /* Function to log information at runtime */
  276. /*void printk(const char *fmt, ...); */
  277. #ifdef __cplusplus
  278. } /* End of "C" linkage for C++ */
  279. #endif
  280. #endif /* __X86EMU_REGS_H */