[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 /* 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 #define SWAP(x, y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
58
59 #define PAGE_ALIGN(x) ((x) & 0xFFFFF000)
60 #define PAGE_OFFSET(x) ((x) & 0x00000FFF)
61
62 #ifndef PAGE_SIZE
63 #define PAGE_SIZE 4096
64 #endif
65
66 typedef struct _FAST486_MOD_REG_RM
67 {
68 FAST486_GEN_REGS Register;
69 BOOLEAN Memory;
70 union
71 {
72 FAST486_GEN_REGS SecondRegister;
73 ULONG MemoryAddress;
74 };
75 } FAST486_MOD_REG_RM, *PFAST486_MOD_REG_RM;
76
77 #pragma pack(push, 1)
78
79 typedef union _FAST486_PAGE_DIR
80 {
81 struct
82 {
83 ULONG Present : 1;
84 ULONG Writeable : 1;
85 ULONG Usermode : 1;
86 ULONG WriteThrough : 1;
87 ULONG NoCache : 1;
88 ULONG Accessed : 1;
89 ULONG AlwaysZero : 1;
90 ULONG Size : 1;
91 ULONG Unused : 4;
92 ULONG TableAddress : 20;
93 };
94 ULONG Value;
95 } FAST486_PAGE_DIR, *PFAST486_PAGE_DIR;
96
97 typedef union _FAST486_PAGE_TABLE
98 {
99 struct
100 {
101 ULONG Present : 1;
102 ULONG Writeable : 1;
103 ULONG Usermode : 1;
104 ULONG WriteThrough : 1;
105 ULONG NoCache : 1;
106 ULONG Accessed : 1;
107 ULONG Dirty : 1;
108 ULONG AlwaysZero : 1;
109 ULONG Global : 1;
110 ULONG Unused : 3;
111 ULONG Address : 20;
112 };
113 ULONG Value;
114 } FAST486_PAGE_TABLE, *PFAST486_PAGE_TABLE;
115
116 #pragma pack(pop)
117
118 /* FUNCTIONS ******************************************************************/
119
120 BOOLEAN
121 Fast486ReadMemory
122 (
123 PFAST486_STATE State,
124 FAST486_SEG_REGS SegmentReg,
125 ULONG Offset,
126 BOOLEAN InstFetch,
127 PVOID Buffer,
128 ULONG Size
129 );
130
131 BOOLEAN
132 Fast486WriteMemory
133 (
134 PFAST486_STATE State,
135 FAST486_SEG_REGS SegmentReg,
136 ULONG Offset,
137 PVOID Buffer,
138 ULONG Size
139 );
140
141 BOOLEAN
142 Fast486InterruptInternal
143 (
144 PFAST486_STATE State,
145 USHORT SegmentSelector,
146 ULONG Offset,
147 BOOLEAN InterruptGate
148 );
149
150 VOID
151 FASTCALL
152 Fast486ExceptionWithErrorCode
153 (
154 PFAST486_STATE State,
155 FAST486_EXCEPTIONS ExceptionCode,
156 ULONG ErrorCode
157 );
158
159 /* INLINED FUNCTIONS **********************************************************/
160
161 /* static */ FORCEINLINE
162 INT
163 Fast486GetCurrentPrivLevel(PFAST486_STATE State)
164 {
165 return GET_SEGMENT_RPL(State->SegmentRegs[FAST486_REG_CS].Selector);
166 }
167
168 #include "common.inl"
169
170 #endif // _COMMON_H_
171
172 /* EOF */