4b4a659d00a759854dcf817f168cf286a00b3cd2
[reactos.git] / lib / fast486 / common.h
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * common.h
4 *
5 * Copyright (C) 2013 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #ifndef _COMMON_H_
23 #define _COMMON_H_
24
25 /* DEFINES ********************************************************************/
26
27 #ifndef FASTCALL
28 #define FASTCALL __fastcall
29 #endif
30
31 #define SIGN_FLAG_BYTE 0x80
32 #define SIGN_FLAG_WORD 0x8000
33 #define SIGN_FLAG_LONG 0x80000000
34 #define REAL_MODE_FLAGS_MASK 0x17FD5
35 #define PROT_MODE_FLAGS_MASK 0x10DD5
36
37 /* Block size for string operations */
38 #define STRING_BLOCK_SIZE 4096
39
40 #define GET_SEGMENT_RPL(s) ((s) & 3)
41 #define GET_SEGMENT_INDEX(s) ((s) & 0xFFF8)
42 #define EXCEPTION_HAS_ERROR_CODE(x) (((x) == 8) || ((x) >= 10 && (x) <= 14))
43
44 #define NO_LOCK_PREFIX() if (State->PrefixFlags & FAST486_PREFIX_LOCK)\
45 {\
46 Fast486Exception(State, FAST486_EXCEPTION_UD);\
47 return FALSE;\
48 }
49 #define TOGGLE_OPSIZE(x) if (State->PrefixFlags & FAST486_PREFIX_OPSIZE)\
50 {\
51 x = !x;\
52 }
53 #define TOGGLE_ADSIZE(x) if (State->PrefixFlags & FAST486_PREFIX_ADSIZE)\
54 {\
55 x = !x;\
56 }
57
58 #define PAGE_ALIGN(x) ((x) & 0xFFFFF000)
59 #define PAGE_OFFSET(x) ((x) & 0x00000FFF)
60
61 #ifndef PAGE_SIZE
62 #define PAGE_SIZE 4096
63 #endif
64
65 typedef struct _FAST486_MOD_REG_RM
66 {
67 FAST486_GEN_REGS Register;
68 BOOLEAN Memory;
69 union
70 {
71 FAST486_GEN_REGS SecondRegister;
72 ULONG MemoryAddress;
73 };
74 } FAST486_MOD_REG_RM, *PFAST486_MOD_REG_RM;
75
76 #pragma pack(push, 1)
77
78 typedef union _FAST486_PAGE_DIR
79 {
80 struct
81 {
82 ULONG Present : 1;
83 ULONG Writeable : 1;
84 ULONG Usermode : 1;
85 ULONG WriteThrough : 1;
86 ULONG NoCache : 1;
87 ULONG Accessed : 1;
88 ULONG AlwaysZero : 1;
89 ULONG Size : 1;
90 ULONG Unused : 4;
91 ULONG TableAddress : 20;
92 };
93 ULONG Value;
94 } FAST486_PAGE_DIR, *PFAST486_PAGE_DIR;
95
96 typedef union _FAST486_PAGE_TABLE
97 {
98 struct
99 {
100 ULONG Present : 1;
101 ULONG Writeable : 1;
102 ULONG Usermode : 1;
103 ULONG WriteThrough : 1;
104 ULONG NoCache : 1;
105 ULONG Accessed : 1;
106 ULONG Dirty : 1;
107 ULONG AlwaysZero : 1;
108 ULONG Global : 1;
109 ULONG Unused : 3;
110 ULONG Address : 20;
111 };
112 ULONG Value;
113 } FAST486_PAGE_TABLE, *PFAST486_PAGE_TABLE;
114
115 #pragma pack(pop)
116
117 /* FUNCTIONS ******************************************************************/
118
119 BOOLEAN
120 Fast486ReadMemory
121 (
122 PFAST486_STATE State,
123 FAST486_SEG_REGS SegmentReg,
124 ULONG Offset,
125 BOOLEAN InstFetch,
126 PVOID Buffer,
127 ULONG Size
128 );
129
130 BOOLEAN
131 Fast486WriteMemory
132 (
133 PFAST486_STATE State,
134 FAST486_SEG_REGS SegmentReg,
135 ULONG Offset,
136 PVOID Buffer,
137 ULONG Size
138 );
139
140 BOOLEAN
141 Fast486InterruptInternal
142 (
143 PFAST486_STATE State,
144 USHORT SegmentSelector,
145 ULONG Offset,
146 BOOLEAN InterruptGate
147 );
148
149 VOID
150 FASTCALL
151 Fast486ExceptionWithErrorCode
152 (
153 PFAST486_STATE State,
154 FAST486_EXCEPTIONS ExceptionCode,
155 ULONG ErrorCode
156 );
157
158 /* INLINED FUNCTIONS **********************************************************/
159
160 /* static */ FORCEINLINE
161 INT
162 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
163 {
164 return GET_SEGMENT_RPL(State->SegmentRegs[FAST486_REG_CS].Selector);
165 }
166
167 #include "common.inl"
168
169 #endif // _COMMON_H_
170
171 /* EOF */