Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

418 lines
10 KiB

  1. //
  2. // This is temporary code, and should be removed when Insignia supplies rom
  3. // support
  4. //
  5. /* x86 v1.0
  6. *
  7. * X86.H
  8. * Constants, macros, and common types
  9. * for the x86 emulator and related components
  10. *
  11. * History
  12. * Created 19-Oct-90 by Jeff Parsons
  13. * 17-Apr-91 by Dave Hastings trimmed for use in softpc (temprorary)
  14. *
  15. * COPYRIGHT NOTICE
  16. * This source file may not be distributed, modified or incorporated into
  17. * another product without prior approval from the author, Jeff Parsons.
  18. * This file may be copied to designated servers and machines authorized to
  19. * access those servers, but that does not imply any form of approval.
  20. */
  21. #ifdef DOS
  22. #define SIGNALS
  23. #endif
  24. #ifdef OS2_16
  25. #define OS2
  26. #define SIGNALS
  27. #endif
  28. #ifdef OS2_32
  29. #define OS2
  30. #define FLAT_32
  31. #endif
  32. #include <stdarg.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <malloc.h>
  37. #include <process.h>
  38. #ifdef WIN_16
  39. #define WIN
  40. #define API16
  41. #endif
  42. #ifdef WIN_32
  43. #ifndef WIN
  44. #define WIN
  45. #endif
  46. #define FLAT_32
  47. #define TRUE_IF_WIN32 1
  48. #define FIXHWND(h) ((HWND)((INT)(h) & 0x00ffffff))
  49. #include <nt.h>
  50. #include <ntrtl.h>
  51. #include <nturtl.h>
  52. #else
  53. #define TRUE_IF_WIN32 0
  54. #define FIXHWND(h) (h)
  55. #endif
  56. #ifdef FLAT_32
  57. #ifndef i386
  58. #define ALIGN_32
  59. #else
  60. #define NOALIGN_32
  61. #endif
  62. #endif
  63. #ifdef WIN
  64. #define _WINDOWS
  65. #include <windows.h>
  66. #endif
  67. #ifdef SIGNALS
  68. #include <conio.h>
  69. #include <signal.h>
  70. #endif
  71. #ifdef OS2_32
  72. #include <excpt.h>
  73. #define XCPT_SIGNAL 0xC0010003
  74. #endif
  75. #define SIGHIT(flChk) ((iSigCheck++ & 0x7FF)?(flSignals & (flChk)):(kbhit(),(flSignals & (flChk))))
  76. #ifndef CONST
  77. #define CONST const
  78. #endif
  79. #ifndef CDECL
  80. #define CDECL _cdecl
  81. #endif
  82. #ifndef PASCAL
  83. #define PASCAL
  84. #endif
  85. #ifdef FLAT_32
  86. #ifndef WIN
  87. #define FAR
  88. #endif
  89. #define HUGE
  90. #define HALLOC(n,s) malloc((n)*(s))
  91. #define HLOCK(h) h
  92. #define HUNLOCK(h) 0
  93. #define HFREE(h) free(h)
  94. #else
  95. #ifndef WIN
  96. #define FAR _far
  97. #define HUGE _huge
  98. #define HALLOC(n,s) halloc(n,s)
  99. #define HLOCK(h) h
  100. #define HUNLOCK(h) 0
  101. #define HFREE(h) hfree(h)
  102. #else
  103. #define HUGE _huge
  104. #define HALLOC(n,s) GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,(n)*(s))
  105. #define HLOCK(h) (HPVOID)GlobalLock(h)
  106. #define HUNLOCK(h) GlobalUnlock(h)
  107. #define HFREE(h) GlobalFree(h)
  108. #endif
  109. #endif
  110. #define BYTEOF(i,n) (((PBYTE)&(i))[n])
  111. #define LOB(i) BYTEOF(i,0)
  112. #define HIB(i) BYTEOF(i,1)
  113. #define WORDOF(i,n) (((PWORD)&(i))[n])
  114. #define LOW(l) WORDOF(l,0)
  115. #define HIW(l) WORDOF(l,1)
  116. #define INTOF(i,n) (((PINT)&(i))[n])
  117. #define UINTOF(i,n) (((PUINT)&(i))[n])
  118. #ifndef WIN
  119. #define LOWORD(l) ((WORD)(l))
  120. #define HIWORD(l) ((WORD)(((DWORD)(l) >> 16) & 0xFFFF))
  121. #define LOBYTE(w) ((BYTE)(w))
  122. #define HIBYTE(w) ((BYTE)(((WORD)(w) >> 8) & 0xFF))
  123. #endif
  124. #ifndef MAKEWORD
  125. #define MAKEWORD(l,h) ((WORD)((BYTE)(l)|((BYTE)(h)<<8)))
  126. #endif
  127. #define MAKEDWORD(l0,h0,l1,h1) ((DWORD)MAKEWORD(l0,h0)|((DWORD)MAKEWORD(l1,h1)<<16))
  128. #define GETBYTE(p) *((PBYTE)p)++
  129. #define GETBYTEPTR(p) ((PBYTE)p)++
  130. #define GETWORDPTR(pb) ((PWORD)pb)++
  131. #define GETDWORDPTR(pb) ((PDWORD)pb)++
  132. #ifndef ALIGN_32
  133. #define GETWORD(pb) (*((PWORD)pb)++)
  134. #define GETDWORD(pb) (*((PDWORD)pb)++)
  135. #define FETCHWORD(s) ((WORD)(s))
  136. #define FETCHDWORD(s) ((DWORD)(s))
  137. #define STOREWORD(d,s) (WORD)d=(WORD)s
  138. #define STOREDWORD(d,s) (DWORD)d=(DWORD)s
  139. #else
  140. #define GETWORD(pb) (pb+=2,MAKEWORD(*(pb-2),*(pb-1)))
  141. #define GETDWORD(pb) (pb+=4,MAKEDWORD(*(pb-4),*(pb-3),*(pb-2),*(pb-1)))
  142. #define FETCHWORD(s) MAKEWORD(LOB(s),HIB(s))
  143. #define FETCHDWORD(s) MAKEDWORD(BYTEOF(s,0),BYTEOF(s,1),BYTEOF(s,2),BYTEOF(s,3))
  144. #define STOREWORD(d,s) {BYTEOF(d,0)=LOBYTE(s);BYTEOF(d,1)=HIBYTE(s);}
  145. #define STOREDWORD(d,s) {BYTEOF(d,0)=LOBYTE(LOWORD(s));BYTEOF(d,1)=HIBYTE(LOWORD(s));BYTEOF(d,2)=LOBYTE(HIWORD(s));BYTEOF(d,3)=HIBYTE(HIWORD(s));}
  146. #endif
  147. #define SWAP(x,y) {INT t; t=y; y=x; x=t;}
  148. #define SWAPS(x,y) {SHORT t; t=y; y=x; x=t;}
  149. #define SWAPL(x,y) {LONG t; t=y; y=x; x=t;}
  150. #define SWAPBYTE(x,y) {BYTE t; t=y; y=x; x=t;}
  151. #define SWAPWORD(x,y) {WORD t; t=FETCHWORD(y); STOREWORD(y,FETCHWORD(x)); STOREWORD(x,t);}
  152. #define SWAPDWORD(x,y) {DWORD t; t=FETCHDWORD(y); STOREDWORD(y,FETCHDWORD(x)); STOREDWORD(x,t);}
  153. #define NUMEL(a) ((sizeof a)/(sizeof a[0]))
  154. #define SXBYTE(i) ((LONG)(SBYTE)(i))
  155. #define SXWORD(i) ((LONG)(SHORT)(i))
  156. #define SXSHORT(i) ((LONG)(SHORT)(i))
  157. #define ZXBYTE(i) ((ULONG)(BYTE)(i))
  158. #define ZXWORD(i) ((ULONG)(USHORT)(i))
  159. #define ZXSHORT(i) ((ULONG)(USHORT)(i))
  160. #define _Z2(m) ((m)&1?0:(m)&2?1:2)
  161. #define _Z4(m) ((m)&3?_Z2(m):_Z2((m)>>2)+2)
  162. #define _Z8(m) ((m)&15?_Z4(m):_Z4((m)>>4)+4)
  163. #define _Z16(m) ((m)&255?_Z8(m):_Z8((m)>>8)+8)
  164. #define _Z32(m) ((m)&65535?_Z16(m):_Z16((m)>>16)+16)
  165. #define SHIFTLEFT(i,m) (((i)<<_Z32(m))&(m))
  166. #define SHIFTRIGHT(i,m) (((i)&(m))>>_Z32(m))
  167. #define OFFSETOF(t,f) ((INT)&(((t *)0)->f))
  168. /* Universal constants
  169. */
  170. #define K 1024L
  171. #ifndef TRUE
  172. #define TRUE 1
  173. #define FALSE 0
  174. #endif
  175. #ifndef NULL
  176. #define NULL 0
  177. #endif
  178. #define UNDEFINED -1
  179. #define CTRL_A 1 // used by gets to repeat last line
  180. #define CTRL_C 3 // break in debug window
  181. #define CTRL_Q 17 // flow control
  182. #define CTRL_S 19 // flow control
  183. #define BELL 7 //
  184. #define BS 8 // backspace
  185. #define TAB 9 //
  186. #define LF 10 // linefeed
  187. #define CR 13 // return
  188. #define ESCAPE 27 //
  189. /* Program options
  190. */
  191. #define OPT_FONT 0x0004 // use small OEM font if available (/s)
  192. #define OPT_DOUBLE 0x0020 // use 50-line debug window w/small font (/50)
  193. #define OPT_CAPS 0x0002 // map ctrl keys to caps-lock (/c)
  194. #define OPT_TERMINAL 0x0010 // redirect all window output to terminal (/t)
  195. #define OPT_FLUSH 0x0100 // flush prefetch after every jump (/f)
  196. #define OPT_NOXLATE 0x0200 // disable built-in translations (/n)
  197. #define OPT_DEBUG 0x0008 // shadow all log output on debug terminal (/d)
  198. #define OPT_GO 0x0001 // do an initial "go" (/g)
  199. /* Signal flags
  200. */
  201. #define SIGNAL_BREAK 0x0001 // set whenever break has occurred
  202. #define SIGNAL_UNWIND 0x0002 // set whenever unwind has occurred
  203. #define SIGNAL_REBOOT 0x0004 // set whenever reboot has occurred
  204. #define SIGNAL_RUN 0x0008 // set whenever emulator is "running"
  205. #define SIGNAL_TRACE 0x0010 // set whenever debugger tracing
  206. #define SIGNAL_BRKPT 0x0020 // set whenever debugger breakpoints enabled
  207. #define SIGNAL_SSTEP 0x0040 // set whenever emulator single-step on
  208. #undef SIG_IGN // fix broken definition in (old) signal.h
  209. #define SIG_IGN (VOID (CDECL *)())1
  210. /* Exec flags (for HostInput/GuestInput)
  211. */
  212. #define EXEC_INPUT 0x0000 // wait for input
  213. #define EXEC_GO 0x0001 // execute immediately
  214. #define EXEC_FREEZE 0x0002 // execution frozen (guest only)
  215. /* Standard types
  216. */
  217. #ifndef WIN
  218. typedef void VOID;
  219. typedef unsigned char BYTE;
  220. typedef unsigned short WORD; // confusing - use where 16-bit req. only
  221. typedef unsigned long DWORD; // confusing - use where 32-bit req. only
  222. typedef long LONG; // use where 32-bit req. only
  223. typedef int BOOL;
  224. #endif
  225. typedef char CHAR;
  226. typedef signed char SBYTE;
  227. typedef short SHORT; // use where 16-bit req. only
  228. typedef unsigned short USHORT; // use where 16-bit req. only
  229. typedef int INT; // ints preferred
  230. typedef unsigned int UINT; // ints preferred
  231. typedef unsigned long ULONG; // use where 32-bit req. only
  232. #ifndef WIN
  233. typedef BYTE *PBYTE; // native pointers
  234. typedef WORD *PWORD;
  235. typedef DWORD *PDWORD;
  236. typedef INT *PINT;
  237. typedef LONG *PLONG;
  238. typedef CHAR *PSTR;
  239. #endif
  240. typedef PBYTE *PPBYTE;
  241. typedef PWORD *PPWORD;
  242. typedef PDWORD *PPDWORD;
  243. typedef CHAR SZ[];
  244. typedef VOID *PVOID;
  245. typedef CHAR *PCHAR;
  246. typedef SHORT *PSHORT;
  247. typedef USHORT *PUSHORT;
  248. typedef PUSHORT *PPUSHORT;
  249. typedef UINT *PUINT;
  250. typedef ULONG *PULONG;
  251. typedef PULONG *PPULONG;
  252. typedef BOOL *PBOOL;
  253. typedef CHAR *PSZ;
  254. typedef PSZ *PPSZ;
  255. typedef VOID FAR *FPVOID; // "far" (or "long" in Windows) pointers
  256. typedef CHAR FAR *FPCHAR;
  257. typedef BYTE FAR *FPBYTE;
  258. typedef SHORT FAR *FPSHORT;
  259. typedef USHORT FAR *FPUSHORT;
  260. typedef LONG FAR *FPLONG;
  261. typedef ULONG FAR *FPULONG;
  262. typedef CHAR FAR *FPSTR;
  263. typedef CHAR FAR *FPSZ;
  264. typedef VOID HUGE *HPVOID; // "huge" pointers
  265. typedef CHAR HUGE *HPCHAR;
  266. typedef BYTE HUGE *HPBYTE;
  267. typedef SHORT HUGE *HPSHORT;
  268. typedef USHORT HUGE *HPUSHORT;
  269. typedef LONG HUGE *HPLONG;
  270. typedef ULONG HUGE *HPULONG;
  271. typedef CHAR HUGE *HPSTR;
  272. typedef CHAR HUGE *HPSZ;
  273. #ifndef WIN
  274. typedef HPVOID HANDLE;
  275. #endif
  276. #ifdef WIN
  277. typedef INT (FAR PASCAL *INTPROC)(HWND, UINT, UINT, LONG);
  278. #endif
  279. #ifdef WIN_16
  280. typedef LONG (FAR PASCAL *WNDPROC)(HWND, WORD, UINT, LONG);
  281. #endif
  282. /* Global data
  283. */
  284. extern FILE *hfLog;
  285. extern INT flOptions; // command-line options (see OPT_*)
  286. extern INT flSignals; // signal flags (see SIGNAL_*)
  287. extern INT iSigCheck; // counter indicating when to make next check
  288. extern INT iSigLevel; // counter indicating whether to take default action
  289. extern INT iLogLevel; // logging level; 0 implies none
  290. extern BOOL fReinit; // set once first initialization has completed
  291. /* String macros
  292. */
  293. #define STRSKIP(psz,sz) psz += strspn(psz, sz)
  294. #define STRSKIPTO(psz,sz) psz += strcspn(psz, sz)
  295. #define STRSKIPNEXT(psz,sz) psz += strspn(psz+=strcspn(psz, sz), sz)
  296. #define ATOI(psz) (INT)szTOul(psz, 10, -1)
  297. /* Logging macros
  298. */
  299. #define IFLOG(l) if (l==iLogLevel && (iLogLevel&1) || l<=iLogLevel && !(iLogLevel&1))
  300. #define OPENLOG() (hfLog?hfLog:(hfLog=fopen("log", "w")))
  301. #define APPENDLOG() (hfLog?hfLog:(hfLog=fopen("log", "a")))
  302. #define CLOSELOG() if (hfLog) {fclose(hfLog); hfLog=NULL;}
  303. #ifdef NOLOG
  304. #define LOG(l,args)
  305. #else
  306. #define LOG(l,args) IFLOG(l) logprintf args; else
  307. #endif
  308. /* Debugging macros
  309. */
  310. #define MODNAME(module) static char szModule[] = __FILE__
  311. #define X86ERROR() terminate(ERR_ASSERT, szModule, __LINE__)
  312. #ifdef DEBUG
  313. #define STATIC
  314. #define INT3() _asm int 3
  315. #define IFDEBUG(f) if (f)
  316. #define ELSEDEBUG else
  317. #define LOGDEBUG(l,args) LOG(l,args)
  318. #define X86ASSERT(exp) if (!(exp)) X86ERROR()
  319. #else
  320. #define STATIC static
  321. #define INT3()
  322. #define IFDEBUG(f)
  323. #define ELSEDEBUG
  324. #define LOGDEBUG(l,args)
  325. #define X86ASSERT(exp)
  326. #endif // DEBUG
  327. /* Other common local include files
  328. */
  329. #ifdef X86
  330. #include "xerr.h"
  331. #include "xlib.h"
  332. #endif
  333. /* Windows goop
  334. */
  335. #define SZ_APP "x86"
  336. #define SZ_TITLE "x86 emulator v0.17"
  337. #define SZ_AUTHOR "by Jeff Parsons, (C) 1991"
  338. #define SZ_PCTITLE "x86 pc"
  339. #define IDM_DBBRK 100
  340. #define IDM_ABOUT 101
  341. /* Standard color definitions
  342. */
  343. #define CLR_BLACK 0x00000000
  344. #define CLR_RED 0x007F0000
  345. #define CLR_GREEN 0x00007F00
  346. #define CLR_BROWN 0x007F7F00
  347. #define CLR_BLUE 0x0000007F
  348. #define CLR_MAGENTA 0x007F007F
  349. #define CLR_CYAN 0x00007F7F
  350. #define CLR_LT_GRAY 0x00BFBFBF
  351. #define CLR_DK_GRAY 0x007F7F7F
  352. #define CLR_BR_RED 0x00FF0000
  353. #define CLR_BR_GREEN 0x0000FF00
  354. #define CLR_YELLOW 0x00FFFF00
  355. #define CLR_BR_BLUE 0x000000FF
  356. #define CLR_BR_MAGENTA 0x00FF00FF
  357. #define CLR_BR_CYAN 0x0000FFFF
  358. #define CLR_WHITE 0x00FFFFFF
  359. extern HANDLE hHostInstance;