forked from b-dmitry1/e86r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.h
351 lines (295 loc) · 7.66 KB
/
cpu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef CPU_H
#define CPU_H
#include "config.h"
#if (PC)
extern FILE *dasm;
extern FILE *c0;
#define D(...) if ((DEBUG && dasm != NULL && 1)) { fprintf(dasm, __VA_ARGS__); }
#ifndef GP
#define GP(cond, code) if ((cond)) { ex(EX_GP, code); if (DEBUG && dasm != NULL) fprintf(dasm, "\texception GP(%x): %s\n", code, #cond); return 0; }
#endif
#ifndef SS
#define SS(cond, code) if ((cond)) { ex(EX_STACK, code); if (DEBUG && dasm != NULL) fprintf(dasm, "\texception SS(%x): %s\n", code, #cond); return 0; }
#endif
#ifndef NP
#define NP(cond, code) if ((cond)) { ex(EX_SEGMENT_NOT_PRESENT, code); if (DEBUG && dasm != NULL) fprintf(dasm, "\texception NP(%x): %s\n", code, #cond); return 0; }
#endif
#ifndef TS
#define TS(cond, code) if ((cond)) { ex(EX_INVALID_TSS, code); if (DEBUG && dasm != NULL) fprintf(dasm, "\texception TS(%x): %s\n", code, #cond); return 0; }
#endif
#ifndef GPV
#define GPV(cond, code) if ((cond)) { ex(EX_GP, code); if (DEBUG && dasm != NULL) fprintf(dasm, "\texception GP(%x): %s\n", code, #cond); return; }
#endif
#else
#define D(...) {}
#ifndef GP
#define GP(cond, code) if ((cond)) { ex(EX_GP, code); return 0; }
#endif
#ifndef SS
#define SS(cond, code) if ((cond)) { ex(EX_STACK, code); return 0; }
#endif
#ifndef NP
#define NP(cond, code) if ((cond)) { ex(EX_SEGMENT_NOT_PRESENT, code); return 0; }
#endif
#ifndef TS
#define TS(cond, code) if ((cond)) { ex(EX_INVALID_TSS, code); return 0; }
#endif
#ifndef GPV
#define GPV(cond, code) if ((cond)) { ex(EX_GP, code); return; }
#endif
#endif
#define EX_DIVIDE 0
#define EX_DEBUG 1
#define EX_NMI 2
#define EX_BREAKPOINT 3
#define EX_OVERFLOW 4
#define EX_BOUND 5
#define EX_OPCODE 6
#define EX_COPROCESSOR_NA 7
#define EX_DOUBLE 8
#define EX_COPROCESSOR_SEG 9
#define EX_INVALID_TSS 10
#define EX_SEGMENT_NOT_PRESENT 11
#define EX_STACK 12
#define EX_GP 13
#define EX_PAGE 14
#define EX_COPROCESSOR 16
#define F_C 0x01
#define F_P 0x04
#define F_A 0x10
#define F_Z 0x40
#define F_S 0x80
#define F_T 0x100
#define F_I 0x200
#define F_D 0x400
#define F_O 0x800
#define F_IOPL 0x3000
#define F_NT 0x4000
#define F_RF 0x10000
#define F_VM 0x20000
#define F_AC 0x40000
#define F_ID 0x200000
#define F_ARITH (2|F_C|F_P|F_A|F_Z|F_S|F_O)
#define F_ALL (F_ARITH|F_T|F_I|F_D|F_AC)
#define F_FULL (F_ALL|F_IOPL|F_NT)
#define IOPL ((r.eflags >> 12) & 3)
#define CR0_PE 0x00000001
#define CR0_MP 0x00000002
#define CR0_EM 0x00000004
#define CR0_TS 0x00000008
#define CR0_ET 0x00000010
#define CR0_PG 0x80000000
#define DESCR_INVALID 0x00
#define DESCR_286_TSS_AVAIL 0x01
#define DESCR_LDT 0x02
#define DESCR_286_TSS_BUSY 0x03
#define DESCR_286_CALL_GATE 0x04
#define DESCR_TASK_GATE 0x05
#define DESCR_286_INT_GATE 0x06
#define DESCR_286_TRAP_GATE 0x07
#define DESCR_386_TSS_AVAIL 0x09
#define DESCR_386_TSS_BUSY 0x0B
#define DESCR_386_CALL_GATE 0x0C
#define DESCR_386_INT_GATE 0x0E
#define DESCR_386_TRAP_GATE 0x0F
#define DESCR_DATA_UP 0x10
#define DESCR_DATA_UP_A 0x11
#define DESCR_DATA_UP_W 0x12
#define DESCR_DATA_UP_W_A 0x13
#define DESCR_DATA_DN 0x14
#define DESCR_DATA_DN_A 0x15
#define DESCR_DATA_DN_W 0x16
#define DESCR_DATA_DN_W_A 0x17
#define DESCR_CODE 0x18
#define DESCR_CODE_A 0x19
#define DESCR_CODE_R 0x1A
#define DESCR_CODE_R_A 0x1B
#define DESCR_CODE_C 0x1C
#define DESCR_CODE_C_A 0x1D
#define DESCR_CODE_C_R 0x1E
#define DESCR_CODE_C_R_A 0x1F
union regs_t
{
struct
{
unsigned char al;
unsigned char ah;
unsigned char reg_fill_a[2];
unsigned char cl;
unsigned char ch;
unsigned char reg_fill_c[2];
unsigned char dl;
unsigned char dh;
unsigned char reg_fill_d[2];
unsigned char bl;
unsigned char bh;
unsigned char reg_fill_b[2];
};
struct
{
unsigned short ax;
unsigned short axh;
unsigned short cx;
unsigned short cxh;
unsigned short dx;
unsigned short dxh;
unsigned short bx;
unsigned short bxh;
unsigned short sp;
unsigned short sph;
unsigned short bp;
unsigned short bph;
unsigned short si;
unsigned short sih;
unsigned short di;
unsigned short dih;
unsigned short flags;
unsigned short flagsh;
unsigned short ip;
unsigned short iph;
};
struct
{
unsigned int eax;
unsigned int ecx;
unsigned int edx;
unsigned int ebx;
unsigned int esp;
unsigned int ebp;
unsigned int esi;
unsigned int edi;
unsigned int eflags;
unsigned int eip;
};
unsigned char r8[16];
unsigned short r16[20];
unsigned int r32[10];
};
typedef struct
{
unsigned short back;
unsigned short sp0;
unsigned short ss0;
unsigned short sp1;
unsigned short ss1;
unsigned short sp2;
unsigned short ss2;
unsigned short ip;
unsigned short flags;
unsigned short ax, cx, dx, bx;
unsigned short sp, bp, si, di;
unsigned short es, cs, ss, ds;
unsigned short ldt;
} tss286_t;
typedef struct
{
unsigned int back;
unsigned int esp0;
unsigned int ss0;
unsigned int esp1;
unsigned int ss1;
unsigned int esp2;
unsigned int ss2;
unsigned int cr3;
unsigned int eip;
unsigned int eflags;
unsigned int eax, ecx, edx, ebx;
unsigned int esp, ebp, esi, edi;
unsigned int es, cs, ss, ds, fs, gs;
unsigned int ldt;
unsigned int iomap;
} tss386_t;
typedef struct
{
unsigned int limit0_15 : 16;
unsigned int base0_15 : 16;
unsigned int base16_23 : 8;
unsigned int type : 5;
unsigned int dpl : 2;
unsigned int present : 1;
unsigned int limit16_19 : 4;
unsigned int user : 1;
unsigned int reserved : 1;
unsigned int big : 1;
unsigned int gran4k : 1;
unsigned int base24_31 : 8;
} descr_t;
typedef struct
{
unsigned int offset0_15 : 16;
unsigned int selector : 16;
unsigned int paramcount : 5;
unsigned int reserved : 3;
unsigned int type : 5;
unsigned int dpl : 2;
unsigned int present : 1;
unsigned int offset_16_31 : 16;
} gate_t;
typedef struct
{
unsigned short value;
unsigned int base;
unsigned int limit;
int big;
unsigned int dpl;
int type;
int present;
unsigned int mask;
const char *name;
} selector_t;
extern unsigned char *ram;
extern regs_t r;
extern selector_t *sel;
extern selector_t *ssel;
extern selector_t es, cs, ss, ds, fs, gs;
extern unsigned int cr[8];
extern unsigned int dr[8];
extern unsigned int tr[8];
extern selector_t instr_cs, instr_ss;
extern unsigned int instr_eip;
extern unsigned int instr_esp;
extern unsigned int instr_fl;
extern unsigned int *dir;
extern unsigned short ldtr;
extern unsigned int gdt_base;
extern unsigned int gdt_limit;
extern unsigned int ldt_base;
extern unsigned int ldt_limit;
extern unsigned int idt_base;
extern unsigned int idt_limit;
extern unsigned short tss;
extern int tss286;
extern unsigned int tssbase;
extern unsigned int tsslimit;
extern int pmode;
extern int paging;
extern unsigned int cpl;
extern unsigned int stack_mask;
extern unsigned int stack_not_mask;
extern int i32;
extern int a32;
extern unsigned int a32mask;
extern int num_pf;
extern int num_gp;
extern int num_ex;
extern int num_math;
extern int dir1;
extern int dir2;
extern int dir4;
extern int repne;
extern int repe;
extern unsigned char opcode;
extern int terminated;
extern int irqs;
extern int hlt;
extern int a20;
extern unsigned int a20mask;
void dump(unsigned int addr);
void dump_descr(unsigned short sel);
void set_flags(unsigned int value, unsigned int mask);
void set_flags16(unsigned short value, unsigned int mask);
void reset();
void step();
void check_irqs();
void undefined_instr();
#endif