0e0c0c8d426bb43821731461f30192e97e942a9e
[reactos.git] / lib / fast486 / common.h
1 /*
2 * Fast486 386/486 CPU Emulation Library
3 * common.h
4 *
5 * Copyright (C) 2014 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 0x57FD5
33 #define PROT_MODE_FLAGS_MASK 0x50DD5
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 SEGMENT_TABLE_INDICATOR (1 << 2)
41 #define EXCEPTION_HAS_ERROR_CODE(x) (((x) == 8) || ((x) >= 10 && (x) <= 14))
42
43 #define NO_LOCK_PREFIX()\
44 if (State->PrefixFlags & FAST486_PREFIX_LOCK)\
45 {\
46 Fast486Exception(State, FAST486_EXCEPTION_UD);\
47 return FALSE;\
48 }
49
50 #define TOGGLE_OPSIZE(x)\
51 if (State->PrefixFlags & FAST486_PREFIX_OPSIZE) x = !x;
52
53 #define TOGGLE_ADSIZE(x)\
54 if (State->PrefixFlags & FAST486_PREFIX_ADSIZE) x = !x;
55
56 #define SWAP(x, y) { (x) ^= (y); (y) ^= (x); (x) ^= (y); }
57
58 #define ALIGNMENT_CHECK(x, a) if (State->Flags.Ac \
59 && (State->ControlRegisters[FAST486_REG_CR0] & FAST486_CR0_AM)\
60 && (State->Cpl == 3)\
61 && (((x) % (a)) != 0))\
62 {\
63 Fast486Exception(State, FAST486_EXCEPTION_AC);\
64 return FALSE;\
65 }
66
67 #define PAGE_ALIGN(x) ((x) & 0xFFFFF000)
68 #define PAGE_OFFSET(x) ((x) & 0x00000FFF)
69 #define GET_ADDR_PDE(x) ((x) >> 22)
70 #define GET_ADDR_PTE(x) (((x) >> 12) & 0x3FF)
71 #define INVALID_TLB_FIELD 0xFFFFFFFF
72
73 #ifndef PAGE_SIZE
74 #define PAGE_SIZE 4096
75 #endif
76
77 typedef struct _FAST486_MOD_REG_RM
78 {
79 FAST486_GEN_REGS Register;
80 BOOLEAN Memory;
81 union
82 {
83 FAST486_GEN_REGS SecondRegister;
84 ULONG MemoryAddress;
85 };
86 } FAST486_MOD_REG_RM, *PFAST486_MOD_REG_RM;
87
88 #include <pshpack1.h>
89
90 typedef union _FAST486_PAGE_DIR
91 {
92 struct
93 {
94 ULONG Present : 1;
95 ULONG Writeable : 1;
96 ULONG Usermode : 1;
97 ULONG WriteThrough : 1;
98 ULONG NoCache : 1;
99 ULONG Accessed : 1;
100 ULONG AlwaysZero : 1;
101 ULONG Size : 1;
102 ULONG Unused : 4;
103 ULONG TableAddress : 20;
104 };
105 ULONG Value;
106 } FAST486_PAGE_DIR, *PFAST486_PAGE_DIR;
107
108 C_ASSERT(sizeof(FAST486_PAGE_DIR) == sizeof(ULONG));
109
110 typedef union _FAST486_PAGE_TABLE
111 {
112 struct
113 {
114 ULONG Present : 1;
115 ULONG Writeable : 1;
116 ULONG Usermode : 1;
117 ULONG WriteThrough : 1;
118 ULONG NoCache : 1;
119 ULONG Accessed : 1;
120 ULONG Dirty : 1;
121 ULONG AlwaysZero : 1;
122 ULONG Global : 1;
123 ULONG Unused : 3;
124 ULONG Address : 20;
125 };
126 ULONG Value;
127 } FAST486_PAGE_TABLE, *PFAST486_PAGE_TABLE;
128
129 C_ASSERT(sizeof(FAST486_PAGE_DIR) == sizeof(ULONG));
130
131 #include <poppack.h>
132
133 /* FUNCTIONS ******************************************************************/
134
135 BOOLEAN
136 Fast486ReadMemory
137 (
138 PFAST486_STATE State,
139 FAST486_SEG_REGS SegmentReg,
140 ULONG Offset,
141 BOOLEAN InstFetch,
142 PVOID Buffer,
143 ULONG Size
144 );
145
146 BOOLEAN
147 Fast486WriteMemory
148 (
149 PFAST486_STATE State,
150 FAST486_SEG_REGS SegmentReg,
151 ULONG Offset,
152 PVOID Buffer,
153 ULONG Size
154 );
155
156 BOOLEAN
157 FASTCALL
158 Fast486PerformInterrupt
159 (
160 PFAST486_STATE State,
161 UCHAR Number
162 );
163
164 VOID
165 FASTCALL
166 Fast486ExceptionWithErrorCode
167 (
168 PFAST486_STATE State,
169 FAST486_EXCEPTIONS ExceptionCode,
170 ULONG ErrorCode
171 );
172
173 /* INLINED FUNCTIONS **********************************************************/
174
175 #include "common.inl"
176
177 #endif // _COMMON_H_
178
179 /* EOF */