[FAST486]
[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 #pragma once
26
27 /* DEFINES ********************************************************************/
28
29 #define SIGN_FLAG_BYTE 0x80
30 #define SIGN_FLAG_WORD 0x8000
31 #define SIGN_FLAG_LONG 0x80000000
32 #define REAL_MODE_FLAGS_MASK 0x17FD5
33 #define PROT_MODE_FLAGS_MASK 0x10DD5
34
35 /* Block size for string operations */
36 #define STRING_BLOCK_SIZE 4096
37
38 #define GET_SEGMENT_RPL(s) ((s) & 3)
39 #define GET_SEGMENT_INDEX(s) ((s) & 0xFFF8)
40 #define EXCEPTION_HAS_ERROR_CODE(x) (((x) == 8) || ((x) >= 10 && (x) <= 14))
41
42 #define NO_LOCK_PREFIX() if (State->PrefixFlags & FAST486_PREFIX_LOCK)\
43 {\
44 Fast486Exception(State, FAST486_EXCEPTION_UD);\
45 return FALSE;\
46 }
47 #define TOGGLE_OPSIZE(x) if (State->PrefixFlags & FAST486_PREFIX_OPSIZE)\
48 {\
49 x = !x;\
50 }
51 #define TOGGLE_ADSIZE(x) if (State->PrefixFlags & FAST486_PREFIX_ADSIZE)\
52 {\
53 x = !x;\
54 }
55 #define SWAP(x, y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
56
57 #define PAGE_ALIGN(x) ((x) & 0xFFFFF000)
58 #define PAGE_OFFSET(x) ((x) & 0x00000FFF)
59 #define GET_ADDR_PDE(x) ((x) >> 22)
60 #define GET_ADDR_PTE(x) (((x) >> 12) & 0x3FF)
61 #define INVALID_TLB_FIELD 0xFFFFFFFF
62
63 #ifndef PAGE_SIZE
64 #define PAGE_SIZE 4096
65 #endif
66
67 typedef struct _FAST486_MOD_REG_RM
68 {
69 FAST486_GEN_REGS Register;
70 BOOLEAN Memory;
71 union
72 {
73 FAST486_GEN_REGS SecondRegister;
74 ULONG MemoryAddress;
75 };
76 } FAST486_MOD_REG_RM, *PFAST486_MOD_REG_RM;
77
78 #pragma pack(push, 1)
79
80 typedef union _FAST486_PAGE_DIR
81 {
82 struct
83 {
84 ULONG Present : 1;
85 ULONG Writeable : 1;
86 ULONG Usermode : 1;
87 ULONG WriteThrough : 1;
88 ULONG NoCache : 1;
89 ULONG Accessed : 1;
90 ULONG AlwaysZero : 1;
91 ULONG Size : 1;
92 ULONG Unused : 4;
93 ULONG TableAddress : 20;
94 };
95 ULONG Value;
96 } FAST486_PAGE_DIR, *PFAST486_PAGE_DIR;
97
98 C_ASSERT(sizeof(FAST486_PAGE_DIR) == sizeof(ULONG));
99
100 typedef union _FAST486_PAGE_TABLE
101 {
102 struct
103 {
104 ULONG Present : 1;
105 ULONG Writeable : 1;
106 ULONG Usermode : 1;
107 ULONG WriteThrough : 1;
108 ULONG NoCache : 1;
109 ULONG Accessed : 1;
110 ULONG Dirty : 1;
111 ULONG AlwaysZero : 1;
112 ULONG Global : 1;
113 ULONG Unused : 3;
114 ULONG Address : 20;
115 };
116 ULONG Value;
117 } FAST486_PAGE_TABLE, *PFAST486_PAGE_TABLE;
118
119 C_ASSERT(sizeof(FAST486_PAGE_DIR) == sizeof(ULONG));
120
121 #pragma pack(pop)
122
123 /* FUNCTIONS ******************************************************************/
124
125 BOOLEAN
126 Fast486ReadMemory
127 (
128 PFAST486_STATE State,
129 FAST486_SEG_REGS SegmentReg,
130 ULONG Offset,
131 BOOLEAN InstFetch,
132 PVOID Buffer,
133 ULONG Size
134 );
135
136 BOOLEAN
137 Fast486WriteMemory
138 (
139 PFAST486_STATE State,
140 FAST486_SEG_REGS SegmentReg,
141 ULONG Offset,
142 PVOID Buffer,
143 ULONG Size
144 );
145
146 BOOLEAN
147 Fast486InterruptInternal
148 (
149 PFAST486_STATE State,
150 USHORT SegmentSelector,
151 ULONG Offset,
152 BOOLEAN InterruptGate
153 );
154
155 VOID
156 FASTCALL
157 Fast486ExceptionWithErrorCode
158 (
159 PFAST486_STATE State,
160 FAST486_EXCEPTIONS ExceptionCode,
161 ULONG ErrorCode
162 );
163
164 /* INLINED FUNCTIONS **********************************************************/
165
166 /* static */ FORCEINLINE
167 INT
168 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
169 {
170 if (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_PE)
171 {
172 /* In protected mode, return the RPL of the CS */
173 return GET_SEGMENT_RPL(State->SegmentRegs[FAST486_REG_CS].Selector);
174 }
175 else
176 {
177 /* Real mode is always in supervisor mode */
178 return 0;
179 }
180 }
181
182 #include "common.inl"
183
184 #endif // _COMMON_H_
185
186 /* EOF */