[SOFT386]
[reactos.git] / lib / soft386 / common.h
1 /*
2 * Soft386 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 PAGE_ALIGN(x) ((x) & 0xFFFFF000)
45 #define PAGE_OFFSET(x) ((x) & 0x00000FFF)
46
47 #ifndef PAGE_SIZE
48 #define PAGE_SIZE 4096
49 #endif
50
51 typedef struct _SOFT386_MOD_REG_RM
52 {
53 SOFT386_GEN_REGS Register;
54 BOOLEAN Memory;
55 union
56 {
57 SOFT386_GEN_REGS SecondRegister;
58 ULONG MemoryAddress;
59 };
60 } SOFT386_MOD_REG_RM, *PSOFT386_MOD_REG_RM;
61
62 #pragma pack(push, 1)
63
64 typedef union _SOFT386_PAGE_DIR
65 {
66 struct
67 {
68 ULONG Present : 1;
69 ULONG Writeable : 1;
70 ULONG Usermode : 1;
71 ULONG WriteThrough : 1;
72 ULONG NoCache : 1;
73 ULONG Accessed : 1;
74 ULONG AlwaysZero : 1;
75 ULONG Size : 1;
76 ULONG Unused : 4;
77 ULONG TableAddress : 20;
78 };
79 ULONG Value;
80 } SOFT386_PAGE_DIR, *PSOFT386_PAGE_DIR;
81
82 typedef union _SOFT386_PAGE_TABLE
83 {
84 struct
85 {
86 ULONG Present : 1;
87 ULONG Writeable : 1;
88 ULONG Usermode : 1;
89 ULONG WriteThrough : 1;
90 ULONG NoCache : 1;
91 ULONG Accessed : 1;
92 ULONG Dirty : 1;
93 ULONG AlwaysZero : 1;
94 ULONG Global : 1;
95 ULONG Unused : 3;
96 ULONG Address : 20;
97 };
98 ULONG Value;
99 } SOFT386_PAGE_TABLE, *PSOFT386_PAGE_TABLE;
100
101 #pragma pack(pop)
102
103 /* FUNCTIONS ******************************************************************/
104
105 BOOLEAN
106 Soft386ReadMemory
107 (
108 PSOFT386_STATE State,
109 SOFT386_SEG_REGS SegmentReg,
110 ULONG Offset,
111 BOOLEAN InstFetch,
112 PVOID Buffer,
113 ULONG Size
114 );
115
116 BOOLEAN
117 Soft386WriteMemory
118 (
119 PSOFT386_STATE State,
120 SOFT386_SEG_REGS SegmentReg,
121 ULONG Offset,
122 PVOID Buffer,
123 ULONG Size
124 );
125
126 BOOLEAN
127 Soft386InterruptInternal
128 (
129 PSOFT386_STATE State,
130 USHORT SegmentSelector,
131 ULONG Offset,
132 BOOLEAN InterruptGate
133 );
134
135 VOID
136 FASTCALL
137 Soft386ExceptionWithErrorCode
138 (
139 PSOFT386_STATE State,
140 SOFT386_EXCEPTIONS ExceptionCode,
141 ULONG ErrorCode
142 );
143
144 /* INLINED FUNCTIONS **********************************************************/
145
146 /* static */ FORCEINLINE
147 INT
148 Soft386GetCurrentPrivLevel(PSOFT386_STATE State)
149 {
150 return GET_SEGMENT_RPL(State->SegmentRegs[SOFT386_REG_CS].Selector);
151 }
152
153 #include "common.inl"
154
155 #endif // _COMMON_H_
156
157 /* EOF */