1 extern uint16_t ea_rseg;
2 
3 #undef readmemb
4 #undef writememb
5 
6 
7 #define readmemb(s,a) ((readlookup2[(uint32_t)((s)+(a))>>12]==-1 || (s)==0xFFFFFFFF)?readmemb386l(s,a): *(uint8_t *)(readlookup2[(uint32_t)((s)+(a))>>12] + (uint32_t)((s) + (a))) )
8 #define readmemq(s,a) ((readlookup2[(uint32_t)((s)+(a))>>12]==-1 || (s)==0xFFFFFFFF || (((s)+(a)) & 7))?readmemql(s,a):*(uint64_t *)(readlookup2[(uint32_t)((s)+(a))>>12]+(uint32_t)((s)+(a))))
9 
10 #define writememb(s,a,v) if (writelookup2[(uint32_t)((s)+(a))>>12]==-1 || (s)==0xFFFFFFFF) writememb386l(s,a,v); else *(uint8_t *)(writelookup2[(uint32_t)((s) + (a)) >> 12] + (uint32_t)((s) + (a))) = v
11 
12 #define writememw(s,a,v) if (writelookup2[(uint32_t)((s)+(a))>>12]==-1 || (s)==0xFFFFFFFF || (((s)+(a)) & 1)) writememwl(s,a,v); else *(uint16_t *)(writelookup2[(uint32_t)((s) + (a)) >> 12] + (uint32_t)((s) + (a))) = v
13 #define writememl(s,a,v) if (writelookup2[(uint32_t)((s)+(a))>>12]==-1 || (s)==0xFFFFFFFF || (((s)+(a)) & 3)) writememll(s,a,v); else *(uint32_t *)(writelookup2[(uint32_t)((s) + (a)) >> 12] + (uint32_t)((s) + (a))) = v
14 #define writememq(s,a,v) if (writelookup2[(uint32_t)((s)+(a))>>12]==-1 || (s)==0xFFFFFFFF || (((s)+(a)) & 7)) writememql(s,a,v); else *(uint64_t *)(writelookup2[(uint32_t)((s) + (a)) >> 12] + (uint32_t)((s) + (a))) = v
15 
16 int checkio(int port);
17 
18 #define check_io_perm(port) if (!IOPLp || (eflags&VM_FLAG)) \
19                         { \
20                                 int tempi = checkio(port); \
21                                 if (cpu_state.abrt) return 1; \
22                                 if (tempi) \
23                                 { \
24                                         x86gpf(NULL,0); \
25                                         return 1; \
26                                 } \
27                         }
28 
29 #define checkio_perm(port) if (!IOPLp || (eflags&VM_FLAG)) \
30                         { \
31                                 int tempi = checkio(port); \
32                                 if (cpu_state.abrt) break; \
33                                 if (tempi) \
34                                 { \
35                                         x86gpf(NULL,0); \
36                                         break; \
37                                 } \
38                         }
39 
40 #define CHECK_READ(seg, low, high)  \
41         if ((low < (seg)->limit_low) || (high > (seg)->limit_high) || ((msw & 1) && !(eflags & VM_FLAG) && (((seg)->access & 10) == 8)))       \
42         {                                       \
43                 x86gpf("Limit check", 0);       \
44                 return 1;                       \
45         }
46 
47 #define CHECK_WRITE(seg, low, high)  \
48         if ((low < (seg)->limit_low) || (high > (seg)->limit_high) || !((seg)->access & 2) || ((msw & 1) && !(eflags & VM_FLAG) && ((seg)->access & 8)))       \
49         {                                       \
50                 x86gpf("Limit check", 0);       \
51                 return 1;                       \
52         }
53 
54 #define CHECK_WRITE_REP(seg, low, high)  \
55         if ((low < (seg)->limit_low) || (high > (seg)->limit_high))       \
56         {                                       \
57                 x86gpf("Limit check", 0);       \
58                 break;                       \
59         }
60 
61 
62 #define NOTRM   if (!(msw & 1) || (eflags & VM_FLAG))\
63                 { \
64                         x86_int(6); \
65                         return 1; \
66                 }
67 
68 
69 
70 
fastreadb(uint32_t a)71 static inline uint8_t fastreadb(uint32_t a)
72 {
73         uint8_t *t;
74 
75         if ((a >> 12) == pccache)
76                 return *((uint8_t *)&pccache2[a]);
77         t = getpccache(a);
78         if (cpu_state.abrt)
79                 return 0;
80         pccache = a >> 12;
81         pccache2 = t;
82         return *((uint8_t *)&pccache2[a]);
83 }
84 
fastreadw(uint32_t a)85 static inline uint16_t fastreadw(uint32_t a)
86 {
87         uint8_t *t;
88         uint16_t val;
89         if ((a&0xFFF)>0xFFE)
90         {
91                 val = fastreadb(a);
92                 val |= (fastreadb(a + 1) << 8);
93                 return val;
94         }
95         if ((a>>12)==pccache) return *((uint16_t *)&pccache2[a]);
96         t = getpccache(a);
97         if (cpu_state.abrt)
98                 return 0;
99 
100         pccache = a >> 12;
101         pccache2 = t;
102         return *((uint16_t *)&pccache2[a]);
103 }
104 
fastreadl(uint32_t a)105 static inline uint32_t fastreadl(uint32_t a)
106 {
107         uint8_t *t;
108         uint32_t val;
109         if ((a&0xFFF)<0xFFD)
110         {
111                 if ((a>>12)!=pccache)
112                 {
113                         t = getpccache(a);
114                         if (cpu_state.abrt)
115                                 return 0;
116                         pccache2 = t;
117                         pccache=a>>12;
118                         //return *((uint32_t *)&pccache2[a]);
119                 }
120                 return *((uint32_t *)&pccache2[a]);
121         }
122         val  = fastreadw(a);
123         val |= (fastreadw(a + 2) << 16);
124         return val;
125 }
126 
getbyte()127 static inline uint8_t getbyte()
128 {
129         cpu_state.pc++;
130         return fastreadb(cs + (cpu_state.pc - 1));
131 }
132 
getword()133 static inline uint16_t getword()
134 {
135         cpu_state.pc+=2;
136         return fastreadw(cs+(cpu_state.pc-2));
137 }
138 
getlong()139 static inline uint32_t getlong()
140 {
141         cpu_state.pc+=4;
142         return fastreadl(cs+(cpu_state.pc-4));
143 }
144 
getquad()145 static inline uint64_t getquad()
146 {
147         cpu_state.pc+=8;
148         return fastreadl(cs+(cpu_state.pc-8)) | ((uint64_t)fastreadl(cs+(cpu_state.pc-4)) << 32);
149 }
150 
151 
152 
geteab()153 static inline uint8_t geteab()
154 {
155         if (cpu_mod == 3)
156                 return (cpu_rm & 4) ? cpu_state.regs[cpu_rm & 3].b.h : cpu_state.regs[cpu_rm&3].b.l;
157         if (eal_r)
158                 return *(uint8_t *)eal_r;
159         return readmemb(easeg, cpu_state.eaaddr);
160 }
161 
geteaw()162 static inline uint16_t geteaw()
163 {
164         if (cpu_mod == 3)
165                 return cpu_state.regs[cpu_rm].w;
166 //        cycles-=3;
167         if (eal_r)
168                 return *(uint16_t *)eal_r;
169         return readmemw(easeg, cpu_state.eaaddr);
170 }
171 
geteal()172 static inline uint32_t geteal()
173 {
174         if (cpu_mod == 3)
175                 return cpu_state.regs[cpu_rm].l;
176 //        cycles-=3;
177         if (eal_r)
178                 return *eal_r;
179         return readmeml(easeg, cpu_state.eaaddr);
180 }
181 
geteaq()182 static inline uint64_t geteaq()
183 {
184         return readmemq(easeg, cpu_state.eaaddr);
185 }
186 
geteab_mem()187 static inline uint8_t geteab_mem()
188 {
189         if (eal_r) return *(uint8_t *)eal_r;
190         return readmemb(easeg,cpu_state.eaaddr);
191 }
geteaw_mem()192 static inline uint16_t geteaw_mem()
193 {
194         if (eal_r) return *(uint16_t *)eal_r;
195         return readmemw(easeg,cpu_state.eaaddr);
196 }
geteal_mem()197 static inline uint32_t geteal_mem()
198 {
199         if (eal_r) return *eal_r;
200         return readmeml(easeg,cpu_state.eaaddr);
201 }
202 
seteaq(uint64_t v)203 static inline void seteaq(uint64_t v)
204 {
205         writememql(easeg, cpu_state.eaaddr, v);
206 }
207 
208 #define seteab(v) if (cpu_mod!=3) { if (eal_w) *(uint8_t *)eal_w=v;  else writememb386l(easeg,cpu_state.eaaddr,v); } else if (cpu_rm&4) cpu_state.regs[cpu_rm&3].b.h=v; else cpu_state.regs[cpu_rm].b.l=v
209 #define seteaw(v) if (cpu_mod!=3) { if (eal_w) *(uint16_t *)eal_w=v; else writememwl(easeg,cpu_state.eaaddr,v);    } else cpu_state.regs[cpu_rm].w=v
210 #define seteal(v) if (cpu_mod!=3) { if (eal_w) *eal_w=v;             else writememll(easeg,cpu_state.eaaddr,v);    } else cpu_state.regs[cpu_rm].l=v
211 
212 #define seteab_mem(v) if (eal_w) *(uint8_t *)eal_w=v;  else writememb386l(easeg,cpu_state.eaaddr,v);
213 #define seteaw_mem(v) if (eal_w) *(uint16_t *)eal_w=v; else writememwl(easeg,cpu_state.eaaddr,v);
214 #define seteal_mem(v) if (eal_w) *eal_w=v;             else writememll(easeg,cpu_state.eaaddr,v);
215 
216 #define getbytef() ((uint8_t)(fetchdat)); cpu_state.pc++
217 #define getwordf() ((uint16_t)(fetchdat)); cpu_state.pc+=2
218 #define getbyte2f() ((uint8_t)(fetchdat>>8)); cpu_state.pc++
219 #define getword2f() ((uint16_t)(fetchdat>>8)); cpu_state.pc+=2
220 
221 
222 #define rmdat rmdat32
223 #define fetchdat rmdat32
224 
225 void x86_int(int num);
226