Commit | Line | Data |
---|---|---|
196ba1fc PH |
1 | /*\r |
2 | * Dos/PC Emulator\r | |
3 | * Copyright (C) 1991 Jim Hudgens\r | |
4 | *\r | |
5 | *\r | |
6 | * The file is part of GDE.\r | |
7 | *\r | |
8 | * GDE is free software; you can redistribute it and/or modify\r | |
9 | * it under the terms of the GNU General Public License as published by\r | |
10 | * the Free Software Foundation; either version 1, or (at your option)\r | |
11 | * any later version.\r | |
12 | *\r | |
13 | * GDE is distributed in the hope that it will be useful,\r | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r | |
16 | * GNU General Public License for more details.\r | |
17 | *\r | |
18 | * You should have received a copy of the GNU General Public License\r | |
19 | * along with GDE; see the file COPYING. If not, write to\r | |
20 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.\r | |
21 | *\r | |
22 | */\r | |
23 | \r | |
24 | #include "altairz80_defs.h"\r | |
25 | #include "i86.h"\r | |
26 | \r | |
27 | extern uint32 GetBYTEExtended(register uint32 Addr);\r | |
28 | extern void PutBYTEExtended(register uint32 Addr, const register uint32 Value);\r | |
29 | \r | |
30 | /* $Log: i86_prim_ops.c,v $\r | |
31 | * Revision 0.9 2003-01-10 23:33:10 jce\r | |
32 | * fixed some more warnings under gcc -Wall\r | |
33 | *\r | |
34 | * Revision 0.8 1992/04/11 21:58:13 hudgens\r | |
35 | * fixed some code causing warnings under gcc -Wall\r | |
36 | *\r | |
37 | * Revision 0.7 1991/07/30 02:04:34 hudgens\r | |
38 | * added copyright.\r | |
39 | *\r | |
40 | * Revision 0.6 1991/07/21 16:50:37 hudgens\r | |
41 | * fixed all flags in the bit shift and rotate instructions so that they\r | |
42 | * finally agree.\r | |
43 | * Also fixed the flags associated with IMUL and MUL instructions.\r | |
44 | *\r | |
45 | * Revision 0.5 1991/07/21 01:28:16 hudgens\r | |
46 | * added support for aad and aam primitives.\r | |
47 | *\r | |
48 | * Revision 0.4 1991/07/20 22:26:25 hudgens\r | |
49 | * fixed problem with sign extension in subroutine mem_access_word.\r | |
50 | *\r | |
51 | * Revision 0.3 1991/07/17 03:48:22 hudgens\r | |
52 | * fixed bugs having to do with the setting of flags in the\r | |
53 | * shift and rotate operations. Also, fixed sign extension problem\r | |
54 | * with push_word and pop_word.\r | |
55 | *\r | |
56 | * Revision 0.2 1991/04/01 02:36:00 hudgens\r | |
57 | * Fixed several nasty bugs dealing with flag setting in the subroutines\r | |
58 | * sub_byte, sub_word, sbb_byte, sbb_word, and test_word. The results\r | |
59 | * now agree with the PC on both of the testaopb and testaopw tests.\r | |
60 | *\r | |
61 | * Revision 0.1 1991/03/30 21:13:37 hudgens\r | |
62 | * Initial checkin.\r | |
63 | *\r | |
64 | *\r | |
65 | */\r | |
66 | \r | |
67 | /* [JCE] Stop gcc -Wall complaining */\r | |
68 | void i86_intr_raise(PC_ENV *m, uint8 intrnum);\r | |
69 | \r | |
70 | /* the following table was generated using the following\r | |
71 | code (running on an IBM AT, Turbo C++ 2.0), for all values of i\r | |
72 | between 0 and 255. AL is loaded with i's value, and then the\r | |
73 | operation "and al,al" sets the parity flag. The flags are pushed\r | |
74 | onto the stack, and then popped back into AX. Then AX is\r | |
75 | returned. So the value of each table entry represents the\r | |
76 | parity of its index into the table. This results in a somewhat\r | |
77 | faster mechanism for parity calculations than the straightforward\r | |
78 | method.\r | |
79 | andflags(i,res) int *res; {\r | |
80 | int flags;\r | |
81 | _AX = i; asm and al,al; asm pushf; *res = _AX;\r | |
82 | asm pop ax; flags = _AX; return flags;\r | |
83 | }\r | |
84 | */\r | |
85 | \r | |
86 | char parity_tab[] = {\r | |
87 | /*0*/ 1, /*1*/ 0, /*2*/ 0, /*3*/ 1,\r | |
88 | /*4*/ 0, /*5*/ 1, /*6*/ 1, /*7*/ 0,\r | |
89 | /*8*/ 0, /*9*/ 1, /*a*/ 1, /*b*/ 0,\r | |
90 | /*c*/ 1, /*d*/ 0, /*e*/ 0, /*f*/ 1,\r | |
91 | /*10*/ 0, /*11*/ 1, /*12*/ 1, /*13*/ 0,\r | |
92 | /*14*/ 1, /*15*/ 0, /*16*/ 0, /*17*/ 1,\r | |
93 | /*18*/ 1, /*19*/ 0, /*1a*/ 0, /*1b*/ 1,\r | |
94 | /*1c*/ 0, /*1d*/ 1, /*1e*/ 1, /*1f*/ 0,\r | |
95 | /*20*/ 0, /*21*/ 1, /*22*/ 1, /*23*/ 0,\r | |
96 | /*24*/ 1, /*25*/ 0, /*26*/ 0, /*27*/ 1,\r | |
97 | /*28*/ 1, /*29*/ 0, /*2a*/ 0, /*2b*/ 1,\r | |
98 | /*2c*/ 0, /*2d*/ 1, /*2e*/ 1, /*2f*/ 0,\r | |
99 | /*30*/ 1, /*31*/ 0, /*32*/ 0, /*33*/ 1,\r | |
100 | /*34*/ 0, /*35*/ 1, /*36*/ 1, /*37*/ 0,\r | |
101 | /*38*/ 0, /*39*/ 1, /*3a*/ 1, /*3b*/ 0,\r | |
102 | /*3c*/ 1, /*3d*/ 0, /*3e*/ 0, /*3f*/ 1,\r | |
103 | /*40*/ 0, /*41*/ 1, /*42*/ 1, /*43*/ 0,\r | |
104 | /*44*/ 1, /*45*/ 0, /*46*/ 0, /*47*/ 1,\r | |
105 | /*48*/ 1, /*49*/ 0, /*4a*/ 0, /*4b*/ 1,\r | |
106 | /*4c*/ 0, /*4d*/ 1, /*4e*/ 1, /*4f*/ 0,\r | |
107 | /*50*/ 1, /*51*/ 0, /*52*/ 0, /*53*/ 1,\r | |
108 | /*54*/ 0, /*55*/ 1, /*56*/ 1, /*57*/ 0,\r | |
109 | /*58*/ 0, /*59*/ 1, /*5a*/ 1, /*5b*/ 0,\r | |
110 | /*5c*/ 1, /*5d*/ 0, /*5e*/ 0, /*5f*/ 1,\r | |
111 | /*60*/ 1, /*61*/ 0, /*62*/ 0, /*63*/ 1,\r | |
112 | /*64*/ 0, /*65*/ 1, /*66*/ 1, /*67*/ 0,\r | |
113 | /*68*/ 0, /*69*/ 1, /*6a*/ 1, /*6b*/ 0,\r | |
114 | /*6c*/ 1, /*6d*/ 0, /*6e*/ 0, /*6f*/ 1,\r | |
115 | /*70*/ 0, /*71*/ 1, /*72*/ 1, /*73*/ 0,\r | |
116 | /*74*/ 1, /*75*/ 0, /*76*/ 0, /*77*/ 1,\r | |
117 | /*78*/ 1, /*79*/ 0, /*7a*/ 0, /*7b*/ 1,\r | |
118 | /*7c*/ 0, /*7d*/ 1, /*7e*/ 1, /*7f*/ 0,\r | |
119 | /*80*/ 0, /*81*/ 1, /*82*/ 1, /*83*/ 0,\r | |
120 | /*84*/ 1, /*85*/ 0, /*86*/ 0, /*87*/ 1,\r | |
121 | /*88*/ 1, /*89*/ 0, /*8a*/ 0, /*8b*/ 1,\r | |
122 | /*8c*/ 0, /*8d*/ 1, /*8e*/ 1, /*8f*/ 0,\r | |
123 | /*90*/ 1, /*91*/ 0, /*92*/ 0, /*93*/ 1,\r | |
124 | /*94*/ 0, /*95*/ 1, /*96*/ 1, /*97*/ 0,\r | |
125 | /*98*/ 0, /*99*/ 1, /*9a*/ 1, /*9b*/ 0,\r | |
126 | /*9c*/ 1, /*9d*/ 0, /*9e*/ 0, /*9f*/ 1,\r | |
127 | /*a0*/ 1, /*a1*/ 0, /*a2*/ 0, /*a3*/ 1,\r | |
128 | /*a4*/ 0, /*a5*/ 1, /*a6*/ 1, /*a7*/ 0,\r | |
129 | /*a8*/ 0, /*a9*/ 1, /*aa*/ 1, /*ab*/ 0,\r | |
130 | /*ac*/ 1, /*ad*/ 0, /*ae*/ 0, /*af*/ 1,\r | |
131 | /*b0*/ 0, /*b1*/ 1, /*b2*/ 1, /*b3*/ 0,\r | |
132 | /*b4*/ 1, /*b5*/ 0, /*b6*/ 0, /*b7*/ 1,\r | |
133 | /*b8*/ 1, /*b9*/ 0, /*ba*/ 0, /*bb*/ 1,\r | |
134 | /*bc*/ 0, /*bd*/ 1, /*be*/ 1, /*bf*/ 0,\r | |
135 | /*c0*/ 1, /*c1*/ 0, /*c2*/ 0, /*c3*/ 1,\r | |
136 | /*c4*/ 0, /*c5*/ 1, /*c6*/ 1, /*c7*/ 0,\r | |
137 | /*c8*/ 0, /*c9*/ 1, /*ca*/ 1, /*cb*/ 0,\r | |
138 | /*cc*/ 1, /*cd*/ 0, /*ce*/ 0, /*cf*/ 1,\r | |
139 | /*d0*/ 0, /*d1*/ 1, /*d2*/ 1, /*d3*/ 0,\r | |
140 | /*d4*/ 1, /*d5*/ 0, /*d6*/ 0, /*d7*/ 1,\r | |
141 | /*d8*/ 1, /*d9*/ 0, /*da*/ 0, /*db*/ 1,\r | |
142 | /*dc*/ 0, /*dd*/ 1, /*de*/ 1, /*df*/ 0,\r | |
143 | /*e0*/ 0, /*e1*/ 1, /*e2*/ 1, /*e3*/ 0,\r | |
144 | /*e4*/ 1, /*e5*/ 0, /*e6*/ 0, /*e7*/ 1,\r | |
145 | /*e8*/ 1, /*e9*/ 0, /*ea*/ 0, /*eb*/ 1,\r | |
146 | /*ec*/ 0, /*ed*/ 1, /*ee*/ 1, /*ef*/ 0,\r | |
147 | /*f0*/ 1, /*f1*/ 0, /*f2*/ 0, /*f3*/ 1,\r | |
148 | /*f4*/ 0, /*f5*/ 1, /*f6*/ 1, /*f7*/ 0,\r | |
149 | /*f8*/ 0, /*f9*/ 1, /*fa*/ 1, /*fb*/ 0,\r | |
150 | /*fc*/ 1, /*fd*/ 0, /*fe*/ 0, /*ff*/ 1,\r | |
151 | };\r | |
152 | \r | |
153 | char xor_0x3_tab[] = { 0, 1, 1, 0 };\r | |
154 | \r | |
155 | /* CARRY CHAIN CALCULATION.\r | |
156 | This represents a somewhat expensive calculation which is\r | |
157 | apparently required to emulate the setting of the OF and\r | |
158 | AF flag. The latter is not so important, but the former is.\r | |
159 | The overflow flag is the XOR of the top two bits of the\r | |
160 | carry chain for an addition (similar for subtraction).\r | |
161 | Since we do not want to simulate the addition in a bitwise\r | |
162 | manner, we try to calculate the carry chain given the\r | |
163 | two operands and the result.\r | |
164 | \r | |
165 | So, given the following table, which represents the\r | |
166 | addition of two bits, we can derive a formula for\r | |
167 | the carry chain.\r | |
168 | \r | |
169 | a b cin r cout\r | |
170 | 0 0 0 0 0\r | |
171 | 0 0 1 1 0\r | |
172 | 0 1 0 1 0\r | |
173 | 0 1 1 0 1\r | |
174 | 1 0 0 1 0\r | |
175 | 1 0 1 0 1\r | |
176 | 1 1 0 0 1\r | |
177 | 1 1 1 1 1\r | |
178 | \r | |
179 | Construction of table for cout:\r | |
180 | \r | |
181 | ab\r | |
182 | r \ 00 01 11 10\r | |
183 | |------------------\r | |
184 | 0 | 0 1 1 1\r | |
185 | 1 | 0 0 1 0\r | |
186 | \r | |
187 | By inspection, one gets: cc = ab + r'(a + b)\r | |
188 | \r | |
189 | That represents alot of operations, but NO CHOICE....\r | |
190 | \r | |
191 | BORROW CHAIN CALCULATION.\r | |
192 | The following table represents the\r | |
193 | subtraction of two bits, from which we can derive a formula for\r | |
194 | the borrow chain.\r | |
195 | \r | |
196 | a b bin r bout\r | |
197 | 0 0 0 0 0\r | |
198 | 0 0 1 1 1\r | |
199 | 0 1 0 1 1\r | |
200 | 0 1 1 0 1\r | |
201 | 1 0 0 1 0\r | |
202 | 1 0 1 0 0\r | |
203 | 1 1 0 0 0\r | |
204 | 1 1 1 1 1\r | |
205 | \r | |
206 | Construction of table for cout:\r | |
207 | \r | |
208 | ab\r | |
209 | r \ 00 01 11 10\r | |
210 | |------------------\r | |
211 | 0 | 0 1 0 0\r | |
212 | 1 | 1 1 1 0\r | |
213 | \r | |
214 | By inspection, one gets: bc = a'b + r(a' + b)\r | |
215 | \r | |
216 | */\r | |
217 | \r | |
218 | uint8 aad_word(PC_ENV *m, uint16 d)\r | |
219 | {\r | |
220 | uint16 l;\r | |
221 | uint8 hb,lb;\r | |
222 | hb = (d>>8)&0xff;\r | |
223 | lb = (d&0xff);\r | |
224 | l = lb + 10 * hb;\r | |
225 | CONDITIONAL_SET_FLAG(l & 0x80, m, F_SF);\r | |
226 | CONDITIONAL_SET_FLAG(l == 0, m, F_ZF);\r | |
227 | CONDITIONAL_SET_FLAG(parity_tab[l & 0xff], m, F_PF);\r | |
228 | return (uint8) l;\r | |
229 | }\r | |
230 | \r | |
231 | uint16 aam_word(PC_ENV *m, uint8 d)\r | |
232 | {\r | |
233 | uint16 h,l;\r | |
234 | h = d / 10;\r | |
235 | l = d % 10;\r | |
236 | l |= (h<<8);\r | |
237 | CONDITIONAL_SET_FLAG(l & 0x80, m, F_SF);\r | |
238 | CONDITIONAL_SET_FLAG(l == 0, m, F_ZF);\r | |
239 | CONDITIONAL_SET_FLAG(parity_tab[l & 0xff], m, F_PF);\r | |
240 | return l;\r | |
241 | }\r | |
242 | \r | |
243 | uint8 adc_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
244 | {\r | |
245 | register uint16 res; /* all operands in native machine order */\r | |
246 | register uint16 cc;\r | |
247 | if (ACCESS_FLAG(m,F_CF) )\r | |
248 | res = 1 + d + s;\r | |
249 | else\r | |
250 | res = d + s;\r | |
251 | CONDITIONAL_SET_FLAG(res & 0x100, m, F_CF);\r | |
252 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
253 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
254 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
255 | /* calculate the carry chain SEE NOTE AT TOP.*/\r | |
256 | cc = (s & d) | ((~res) & (s | d));\r | |
257 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(cc>>6)&0x3], m, F_OF);\r | |
258 | CONDITIONAL_SET_FLAG(cc&0x8, m, F_AF);\r | |
259 | return (uint8) res;\r | |
260 | }\r | |
261 | \r | |
262 | uint16 adc_word(PC_ENV *m, uint16 d, uint16 s)\r | |
263 | {\r | |
264 | register uint32 res; /* all operands in native machine order */\r | |
265 | register uint32 cc;\r | |
266 | if (ACCESS_FLAG(m,F_CF) )\r | |
267 | res = 1 + d + s;\r | |
268 | else\r | |
269 | res = d + s;\r | |
270 | /* set the carry flag to be bit 8 */\r | |
271 | CONDITIONAL_SET_FLAG(res & 0x10000, m, F_CF);\r | |
272 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
273 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
274 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
275 | /* calculate the carry chain SEE NOTE AT TOP.*/\r | |
276 | cc = (s & d) | ((~res) & (s | d));\r | |
277 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(cc>>14)&0x3], m, F_OF);\r | |
278 | CONDITIONAL_SET_FLAG(cc&0x8, m, F_AF);\r | |
279 | return res;\r | |
280 | }\r | |
281 | \r | |
282 | /* Given flags=f, and bytes d (dest) and s (source)\r | |
283 | perform the add and set the flags and the result back to\r | |
284 | *d. USE NATIVE MACHINE ORDER...\r | |
285 | */\r | |
286 | uint8 add_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
287 | {\r | |
288 | register uint16 res; /* all operands in native machine order */\r | |
289 | register uint16 cc;\r | |
290 | res = d + s;\r | |
291 | /* set the carry flag to be bit 8 */\r | |
292 | CONDITIONAL_SET_FLAG(res & 0x100, m, F_CF);\r | |
293 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
294 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
295 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
296 | /* calculate the carry chain SEE NOTE AT TOP.*/\r | |
297 | cc = (s & d) | ((~res) & (s | d));\r | |
298 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(cc>>6)&0x3], m, F_OF);\r | |
299 | CONDITIONAL_SET_FLAG(cc&0x8, m, F_AF);\r | |
300 | return (uint8) res;\r | |
301 | }\r | |
302 | \r | |
303 | /* Given flags=f, and bytes d (dest) and s (source)\r | |
304 | perform the add and set the flags and the result back to\r | |
305 | *d. USE NATIVE MACHINE ORDER...\r | |
306 | */\r | |
307 | uint16 add_word(PC_ENV *m, uint16 d, uint16 s)\r | |
308 | {\r | |
309 | register uint32 res; /* all operands in native machine order */\r | |
310 | register uint32 cc;\r | |
311 | res = d + s;\r | |
312 | /* set the carry flag to be bit 8 */\r | |
313 | CONDITIONAL_SET_FLAG(res & 0x10000, m, F_CF);\r | |
314 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
315 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
316 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
317 | /* calculate the carry chain SEE NOTE AT TOP.*/\r | |
318 | cc = (s & d) | ((~res) & (s | d));\r | |
319 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(cc>>14)&0x3], m, F_OF);\r | |
320 | CONDITIONAL_SET_FLAG(cc&0x8, m, F_AF);\r | |
321 | return res;\r | |
322 | }\r | |
323 | \r | |
324 | /*\r | |
325 | Flags m->R_FLG, dest *d, source *s, do a bitwise and of the\r | |
326 | source and destination, and then store back to the\r | |
327 | destination. Size=byte.\r | |
328 | */\r | |
329 | uint8 and_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
330 | {\r | |
331 | register uint8 res; /* all operands in native machine order */\r | |
332 | res = d & s;\r | |
333 | /* set the flags */\r | |
334 | CLEAR_FLAG(m, F_OF);\r | |
335 | CLEAR_FLAG(m, F_CF);\r | |
336 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
337 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
338 | CONDITIONAL_SET_FLAG(parity_tab[res], m, F_PF);\r | |
339 | return res;\r | |
340 | }\r | |
341 | \r | |
342 | /*\r | |
343 | Flags m->R_FLG, dest *d, source *s, do a bitwise and of the\r | |
344 | source and destination, and then store back to the\r | |
345 | destination. Size=byte.\r | |
346 | */\r | |
347 | uint16 and_word(PC_ENV *m, uint16 d, uint16 s)\r | |
348 | {\r | |
349 | register uint16 res; /* all operands in native machine order */\r | |
350 | res = d & s;\r | |
351 | /* set the flags */\r | |
352 | CLEAR_FLAG(m, F_OF);\r | |
353 | CLEAR_FLAG(m, F_CF);\r | |
354 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
355 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
356 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
357 | return res;\r | |
358 | }\r | |
359 | \r | |
360 | uint8 cmp_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
361 | {\r | |
362 | register uint32 res; /* all operands in native machine order */\r | |
363 | register uint32 bc;\r | |
364 | res = d - s;\r | |
365 | CLEAR_FLAG(m, F_CF);\r | |
366 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
367 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
368 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
369 | /* calculate the borrow chain. See note at top */\r | |
370 | bc= (res&(~d|s))|(~d&s);\r | |
371 | CONDITIONAL_SET_FLAG(bc&0x80,m, F_CF);\r | |
372 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>6)&0x3], m, F_OF);\r | |
373 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
374 | return d; /* long story why this is needed. Look at opcode\r | |
375 | 0x80 in ops.c, for an idea why this is necessary.*/\r | |
376 | }\r | |
377 | \r | |
378 | uint16 cmp_word(PC_ENV *m, uint16 d, uint16 s)\r | |
379 | {\r | |
380 | register uint32 res; /* all operands in native machine order */\r | |
381 | register uint32 bc;\r | |
382 | res = d - s;\r | |
383 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
384 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
385 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
386 | /* calculate the borrow chain. See note at top */\r | |
387 | bc= (res&(~d|s))|(~d&s);\r | |
388 | CONDITIONAL_SET_FLAG(bc&0x8000,m, F_CF);\r | |
389 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>14)&0x3], m, F_OF);\r | |
390 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
391 | return d;\r | |
392 | }\r | |
393 | \r | |
394 | uint8 dec_byte(PC_ENV *m, uint8 d)\r | |
395 | {\r | |
396 | register uint32 res; /* all operands in native machine order */\r | |
397 | register uint32 bc;\r | |
398 | res = d - 1;\r | |
399 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
400 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
401 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
402 | /* calculate the borrow chain. See note at top */\r | |
403 | /* based on sub_byte, uses s==1. */\r | |
404 | bc= (res&(~d|1))|(~d&1);\r | |
405 | /* carry flag unchanged */\r | |
406 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>6)&0x3], m, F_OF);\r | |
407 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
408 | return res;\r | |
409 | }\r | |
410 | \r | |
411 | uint16 dec_word(PC_ENV *m, uint16 d)\r | |
412 | {\r | |
413 | register uint32 res; /* all operands in native machine order */\r | |
414 | register uint32 bc;\r | |
415 | res = d - 1;\r | |
416 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
417 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
418 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
419 | /* calculate the borrow chain. See note at top */\r | |
420 | /* based on the sub_byte routine, with s==1 */\r | |
421 | bc= (res&(~d|1))|(~d&1);\r | |
422 | /* carry flag unchanged */\r | |
423 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>14)&0x3], m, F_OF);\r | |
424 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
425 | return res;\r | |
426 | }\r | |
427 | \r | |
428 | /* Given flags=f, and byte d (dest)\r | |
429 | perform the inc and set the flags and the result back to\r | |
430 | d. USE NATIVE MACHINE ORDER...\r | |
431 | */\r | |
432 | uint8 inc_byte(PC_ENV *m, uint8 d)\r | |
433 | {\r | |
434 | register uint32 res; /* all operands in native machine order */\r | |
435 | register uint32 cc;\r | |
436 | res = d + 1;\r | |
437 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
438 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
439 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
440 | /* calculate the carry chain SEE NOTE AT TOP.*/\r | |
441 | cc = ((1 & d) | (~res)) & (1 | d);\r | |
442 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(cc>>6)&0x3], m, F_OF);\r | |
443 | CONDITIONAL_SET_FLAG(cc&0x8, m, F_AF);\r | |
444 | return res;\r | |
445 | }\r | |
446 | \r | |
447 | /* Given flags=f, and byte d (dest)\r | |
448 | perform the inc and set the flags and the result back to\r | |
449 | *d. USE NATIVE MACHINE ORDER...\r | |
450 | */\r | |
451 | uint16 inc_word(PC_ENV *m, uint16 d)\r | |
452 | {\r | |
453 | register uint32 res; /* all operands in native machine order */\r | |
454 | register uint32 cc;\r | |
455 | res = d + 1;\r | |
456 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
457 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
458 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
459 | /* calculate the carry chain SEE NOTE AT TOP.*/\r | |
460 | cc = (1 & d) | ((~res) & (1 | d));\r | |
461 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(cc>>14)&0x3], m, F_OF);\r | |
462 | CONDITIONAL_SET_FLAG(cc&0x8, m, F_AF);\r | |
463 | return res ;\r | |
464 | }\r | |
465 | \r | |
466 | uint8 or_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
467 | {\r | |
468 | register uint8 res; /* all operands in native machine order */\r | |
469 | res = d | s;\r | |
470 | CLEAR_FLAG(m, F_OF);\r | |
471 | CLEAR_FLAG(m, F_CF);\r | |
472 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
473 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
474 | CONDITIONAL_SET_FLAG(parity_tab[res], m, F_PF);\r | |
475 | return res;\r | |
476 | }\r | |
477 | \r | |
478 | uint16 or_word(PC_ENV *m, uint16 d, uint16 s)\r | |
479 | {\r | |
480 | register uint16 res; /* all operands in native machine order */\r | |
481 | res = d | s;\r | |
482 | /* set the carry flag to be bit 8 */\r | |
483 | CLEAR_FLAG(m, F_OF);\r | |
484 | CLEAR_FLAG(m, F_CF);\r | |
485 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
486 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
487 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
488 | return res;\r | |
489 | }\r | |
490 | \r | |
491 | uint8 neg_byte(PC_ENV *m, uint8 s)\r | |
492 | {\r | |
493 | register uint8 res;\r | |
494 | register uint8 bc;\r | |
495 | CONDITIONAL_SET_FLAG(s!=0, m, F_CF);\r | |
496 | res = -s;\r | |
497 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
498 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
499 | CONDITIONAL_SET_FLAG(parity_tab[res], m, F_PF);\r | |
500 | /* calculate the borrow chain --- modified such that d=0.\r | |
501 | substitutiing d=0 into bc= res&(~d|s)|(~d&s);\r | |
502 | (the one used for sub) and simplifying, since ~d=0xff...,\r | |
503 | ~d|s == 0xffff..., and res&0xfff... == res. Similarly\r | |
504 | ~d&s == s. So the simplified result is:*/\r | |
505 | bc= res|s;\r | |
506 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>6)&0x3], m, F_OF);\r | |
507 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
508 | return res;\r | |
509 | }\r | |
510 | \r | |
511 | uint16 neg_word(PC_ENV *m, uint16 s)\r | |
512 | {\r | |
513 | register uint16 res;\r | |
514 | register uint16 bc;\r | |
515 | CONDITIONAL_SET_FLAG(s!=0, m, F_CF);\r | |
516 | res = -s;\r | |
517 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
518 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
519 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
520 | /* calculate the borrow chain --- modified such that d=0.\r | |
521 | substitutiing d=0 into bc= res&(~d|s)|(~d&s);\r | |
522 | (the one used for sub) and simplifying, since ~d=0xff...,\r | |
523 | ~d|s == 0xffff..., and res&0xfff... == res. Similarly\r | |
524 | ~d&s == s. So the simplified result is:*/\r | |
525 | bc= res|s;\r | |
526 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>6)&0x3], m, F_OF);\r | |
527 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
528 | return res;\r | |
529 | }\r | |
530 | \r | |
531 | uint8 not_byte(PC_ENV *m, uint8 s)\r | |
532 | {\r | |
533 | return ~s;\r | |
534 | }\r | |
535 | \r | |
536 | uint16 not_word(PC_ENV *m, uint16 s)\r | |
537 | {\r | |
538 | return ~s;\r | |
539 | }\r | |
540 | \r | |
541 | /* access stuff from absolute location in memory.\r | |
542 | no segment registers are involved.\r | |
543 | */\r | |
544 | uint16 mem_access_word(PC_ENV *m, int addr)\r | |
545 | {\r | |
546 | /* Load in two steps. Native byte order independent */\r | |
547 | return GetBYTEExtended(addr) | (GetBYTEExtended(addr + 1) << 8);\r | |
548 | }\r | |
549 | \r | |
550 | /* given the register_set r, and memory descriptor m,\r | |
551 | and word w, push w onto the stack.\r | |
552 | w ASSUMED IN NATIVE MACHINE ORDER. Doesn't matter in this case???\r | |
553 | */\r | |
554 | void push_word(PC_ENV *m, uint16 w)\r | |
555 | {\r | |
556 | m->R_SP --;\r | |
557 | PutBYTEExtended((m->R_SS << 4) + m->R_SP, w >> 8);\r | |
558 | m->R_SP --;\r | |
559 | PutBYTEExtended((m->R_SS << 4) + m->R_SP, w & 0xff);\r | |
560 | }\r | |
561 | \r | |
562 | /* given the memory descriptor m,\r | |
563 | and word w, pop word from the stack.\r | |
564 | */\r | |
565 | uint16 pop_word(PC_ENV *m)\r | |
566 | {\r | |
567 | register uint16 res;\r | |
568 | res = GetBYTEExtended((m->R_SS << 4) + m->R_SP);\r | |
569 | m->R_SP++;\r | |
570 | res |= GetBYTEExtended((m->R_SS << 4) + m->R_SP) << 8;\r | |
571 | m->R_SP++;\r | |
572 | return res;\r | |
573 | }\r | |
574 | \r | |
575 | /*****************************************************************\r | |
576 | BEGIN region consisting of bit shifts and rotates,\r | |
577 | much of which may be wrong. Large hirsute factor.\r | |
578 | *****************************************************************/\r | |
579 | uint8 rcl_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
580 | {\r | |
581 | register uint32 res, cnt, mask,cf;\r | |
582 | /* s is the rotate distance. It varies from 0 - 8. */\r | |
583 | /* have\r | |
584 | CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0\r | |
585 | want to rotate through the carry by "s" bits. We could\r | |
586 | loop, but that's inefficient. So the width is 9,\r | |
587 | and we split into three parts:\r | |
588 | The new carry flag (was B_n)\r | |
589 | the stuff in B_n-1 .. B_0\r | |
590 | the stuff in B_7 .. B_n+1\r | |
591 | The new rotate is done mod 9, and given this,\r | |
592 | for a rotation of n bits (mod 9) the new carry flag is\r | |
593 | then located n bits from the MSB. The low part is\r | |
594 | then shifted up cnt bits, and the high part is or'd\r | |
595 | in. Using CAPS for new values, and lowercase for the\r | |
596 | original values, this can be expressed as:\r | |
597 | IF n > 0\r | |
598 | 1) CF <- b_(8-n)\r | |
599 | 2) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0\r | |
600 | 3) B_(n-1) <- cf\r | |
601 | 4) B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1))\r | |
602 | I think this is correct.\r | |
603 | */\r | |
604 | res = d;\r | |
605 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
606 | if ((cnt = s % 9)) /* not a typo, do nada if cnt==0 */\r | |
607 | {\r | |
608 | /* extract the new CARRY FLAG. */\r | |
609 | /* CF <- b_(8-n) */\r | |
610 | cf = (d >> (8-cnt)) & 0x1;\r | |
611 | /* get the low stuff which rotated\r | |
612 | into the range B_7 .. B_cnt */\r | |
613 | /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_0 */\r | |
614 | /* note that the right hand side done by the mask */\r | |
615 | res = (d << cnt) & 0xff;\r | |
616 | /* now the high stuff which rotated around\r | |
617 | into the positions B_cnt-2 .. B_0 */\r | |
618 | /* B_(n-2) .. B_0 <- b_7 .. b_(8-(n-1)) */\r | |
619 | /* shift it downward, 7-(n-2) = 9-n positions.\r | |
620 | and mask off the result before or'ing in.\r | |
621 | */\r | |
622 | mask = (1<<(cnt-1)) - 1;\r | |
623 | res |= (d >> (9-cnt)) & mask;\r | |
624 | /* if the carry flag was set, or it in. */\r | |
625 | if (ACCESS_FLAG(m,F_CF)) /* carry flag is set */\r | |
626 | {\r | |
627 | /* B_(n-1) <- cf */\r | |
628 | res |= 1 << (cnt-1);\r | |
629 | }\r | |
630 | /* set the new carry flag, based on the variable "cf" */\r | |
631 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
632 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
633 | xor of CF and the most significant bit. Blecck. */\r | |
634 | /* parenthesized this expression since it appears to\r | |
635 | be causing OF to be misset */\r | |
636 | CONDITIONAL_SET_FLAG(cnt==1&&\r | |
637 | xor_0x3_tab[cf+((res>>6)&0x2)],\r | |
638 | m, F_OF);\r | |
639 | }\r | |
640 | return res & 0xff;\r | |
641 | }\r | |
642 | \r | |
643 | uint16 rcl_word(PC_ENV *m, uint16 d, uint16 s)\r | |
644 | {\r | |
645 | register uint32 res, cnt, mask,cf;\r | |
646 | /* see analysis above. */\r | |
647 | /* width here is 16 bits + carry bit */\r | |
648 | res = d;\r | |
649 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
650 | if ((cnt = s % 17)) /* not a typo, do nada if cnt==0 */\r | |
651 | {\r | |
652 | /* extract the new CARRY FLAG. */\r | |
653 | /* CF <- b_(16-n) */\r | |
654 | cf = (d >> (16-cnt)) & 0x1;\r | |
655 | /* get the low stuff which rotated\r | |
656 | into the range B_15 .. B_cnt */\r | |
657 | /* B_(15) .. B_(n) <- b_(16-(n+1)) .. b_0 */\r | |
658 | /* note that the right hand side done by the mask */\r | |
659 | res = (d << cnt) & 0xffff;\r | |
660 | /* now the high stuff which rotated around\r | |
661 | into the positions B_cnt-2 .. B_0 */\r | |
662 | /* B_(n-2) .. B_0 <- b_15 .. b_(16-(n-1)) */\r | |
663 | /* shift it downward, 15-(n-2) = 17-n positions.\r | |
664 | and mask off the result before or'ing in.\r | |
665 | */\r | |
666 | mask = (1<<(cnt-1)) - 1;\r | |
667 | res |= (d >> (17-cnt)) & mask;\r | |
668 | /* if the carry flag was set, or it in. */\r | |
669 | if (ACCESS_FLAG(m, F_CF)) /* carry flag is set */\r | |
670 | {\r | |
671 | /* B_(n-1) <- cf */\r | |
672 | res |= 1 << (cnt-1);\r | |
673 | }\r | |
674 | /* set the new carry flag, based on the variable "cf" */\r | |
675 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
676 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
677 | xor of CF and the most significant bit. Blecck.\r | |
678 | Note that we're forming a 2 bit word here to index\r | |
679 | into the table. The expression cf+(res>>14)&0x2\r | |
680 | represents the two bit word b_15 CF.\r | |
681 | */\r | |
682 | /* parenthesized following expression... */\r | |
683 | CONDITIONAL_SET_FLAG(cnt==1&&xor_0x3_tab[cf+((res>>14)&0x2)],\r | |
684 | m, F_OF);\r | |
685 | }\r | |
686 | return res & 0xffff;\r | |
687 | }\r | |
688 | \r | |
689 | uint8 rcr_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
690 | {\r | |
691 | uint8 res, cnt;\r | |
692 | uint8 mask, cf, ocf = 0;\r | |
693 | /* rotate right through carry */\r | |
694 | /*\r | |
695 | s is the rotate distance. It varies from 0 - 8.\r | |
696 | d is the byte object rotated.\r | |
697 | have\r | |
698 | CF B_7 B_6 B_5 B_4 B_3 B_2 B_1 B_0\r | |
699 | The new rotate is done mod 9, and given this,\r | |
700 | for a rotation of n bits (mod 9) the new carry flag is\r | |
701 | then located n bits from the LSB. The low part is\r | |
702 | then shifted up cnt bits, and the high part is or'd\r | |
703 | in. Using CAPS for new values, and lowercase for the\r | |
704 | original values, this can be expressed as:\r | |
705 | IF n > 0\r | |
706 | 1) CF <- b_(n-1)\r | |
707 | 2) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)\r | |
708 | 3) B_(8-n) <- cf\r | |
709 | 4) B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0)\r | |
710 | I think this is correct.\r | |
711 | */\r | |
712 | res = d;\r | |
713 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
714 | if ((cnt = s % 9)) /* not a typo, do nada if cnt==0 */\r | |
715 | {\r | |
716 | /* extract the new CARRY FLAG. */\r | |
717 | /* CF <- b_(n-1) */\r | |
718 | if (cnt == 1)\r | |
719 | {\r | |
720 | cf = d & 0x1;\r | |
721 | /* note hackery here. Access_flag(..) evaluates to either\r | |
722 | 0 if flag not set\r | |
723 | non-zero if flag is set.\r | |
724 | doing access_flag(..) != 0 casts that into either\r | |
725 | 0..1 in any representation of the flags register\r | |
726 | (i.e. packed bit array or unpacked.)\r | |
727 | */\r | |
728 | ocf = ACCESS_FLAG(m,F_CF) != 0;\r | |
729 | }\r | |
730 | else\r | |
731 | cf = (d >> (cnt-1)) & 0x1;\r | |
732 | /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_n */\r | |
733 | /* note that the right hand side done by the mask\r | |
734 | This is effectively done by shifting the\r | |
735 | object to the right. The result must be masked,\r | |
736 | in case the object came in and was treated\r | |
737 | as a negative number. Needed???*/\r | |
738 | mask = (1<<(8-cnt))-1;\r | |
739 | res = (d >> cnt) & mask;\r | |
740 | /* now the high stuff which rotated around\r | |
741 | into the positions B_cnt-2 .. B_0 */\r | |
742 | /* B_(7) .. B_(8-(n-1)) <- b_(n-2) .. b_(0) */\r | |
743 | /* shift it downward, 7-(n-2) = 9-n positions.\r | |
744 | and mask off the result before or'ing in.\r | |
745 | */\r | |
746 | res |= (d << (9-cnt));\r | |
747 | /* if the carry flag was set, or it in. */\r | |
748 | if (ACCESS_FLAG(m,F_CF)) /* carry flag is set */\r | |
749 | {\r | |
750 | /* B_(8-n) <- cf */\r | |
751 | res |= 1 << (8 - cnt);\r | |
752 | }\r | |
753 | /* set the new carry flag, based on the variable "cf" */\r | |
754 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
755 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
756 | xor of CF and the most significant bit. Blecck. */\r | |
757 | /* parenthesized... */\r | |
758 | if (cnt == 1)\r | |
759 | { /* [JCE] Explicit braces to stop gcc -Wall moaning */\r | |
760 | CONDITIONAL_SET_FLAG(xor_0x3_tab[ocf+((d>>6)&0x2)],\r | |
761 | m, F_OF);\r | |
762 | }\r | |
763 | }\r | |
764 | return res;\r | |
765 | }\r | |
766 | \r | |
767 | uint16 rcr_word(PC_ENV *m, uint16 d, uint16 s)\r | |
768 | {\r | |
769 | uint16 res, cnt;\r | |
770 | uint16 mask, cf, ocf = 0;\r | |
771 | /* rotate right through carry */\r | |
772 | /*\r | |
773 | s is the rotate distance. It varies from 0 - 8.\r | |
774 | d is the byte object rotated.\r | |
775 | have\r | |
776 | CF B_15 ... B_0\r | |
777 | The new rotate is done mod 17, and given this,\r | |
778 | for a rotation of n bits (mod 17) the new carry flag is\r | |
779 | then located n bits from the LSB. The low part is\r | |
780 | then shifted up cnt bits, and the high part is or'd\r | |
781 | in. Using CAPS for new values, and lowercase for the\r | |
782 | original values, this can be expressed as:\r | |
783 | IF n > 0\r | |
784 | 1) CF <- b_(n-1)\r | |
785 | 2) B_(16-(n+1)) .. B_(0) <- b_(15) .. b_(n)\r | |
786 | 3) B_(16-n) <- cf\r | |
787 | 4) B_(15) .. B_(16-(n-1)) <- b_(n-2) .. b_(0)\r | |
788 | I think this is correct.\r | |
789 | */\r | |
790 | res = d;\r | |
791 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
792 | if ((cnt = s % 17)) /* not a typo, do nada if cnt==0 */\r | |
793 | {\r | |
794 | /* extract the new CARRY FLAG. */\r | |
795 | /* CF <- b_(n-1) */\r | |
796 | if (cnt==1)\r | |
797 | {\r | |
798 | cf = d & 0x1;\r | |
799 | /* see note above on teh byte version */\r | |
800 | ocf = ACCESS_FLAG(m,F_CF) != 0;\r | |
801 | }\r | |
802 | else\r | |
803 | cf = (d >> (cnt-1)) & 0x1;\r | |
804 | /* B_(16-(n+1)) .. B_(0) <- b_(15) .. b_n */\r | |
805 | /* note that the right hand side done by the mask\r | |
806 | This is effectively done by shifting the\r | |
807 | object to the right. The result must be masked,\r | |
808 | in case the object came in and was treated\r | |
809 | as a negative number. Needed???*/\r | |
810 | mask = (1<<(16-cnt))-1;\r | |
811 | res = (d >> cnt) & mask;\r | |
812 | /* now the high stuff which rotated around\r | |
813 | into the positions B_cnt-2 .. B_0 */\r | |
814 | /* B_(15) .. B_(16-(n-1)) <- b_(n-2) .. b_(0) */\r | |
815 | /* shift it downward, 15-(n-2) = 17-n positions.\r | |
816 | and mask off the result before or'ing in.\r | |
817 | */\r | |
818 | res |= (d << (17-cnt));\r | |
819 | /* if the carry flag was set, or it in. */\r | |
820 | if (ACCESS_FLAG(m,F_CF)) /* carry flag is set */\r | |
821 | {\r | |
822 | /* B_(16-n) <- cf */\r | |
823 | res |= 1 << (16 - cnt);\r | |
824 | }\r | |
825 | /* set the new carry flag, based on the variable "cf" */\r | |
826 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
827 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
828 | xor of CF and the most significant bit. Blecck. */\r | |
829 | if (cnt==1)\r | |
830 | { /* [JCE] Explicit braces to stop gcc -Wall moaning */\r | |
831 | CONDITIONAL_SET_FLAG(xor_0x3_tab[ocf+((d>>14)&0x2)],\r | |
832 | m, F_OF);\r | |
833 | }\r | |
834 | }\r | |
835 | return res;\r | |
836 | }\r | |
837 | \r | |
838 | uint8 rol_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
839 | {\r | |
840 | register uint32 res, cnt, mask;\r | |
841 | /* rotate left */\r | |
842 | /*\r | |
843 | s is the rotate distance. It varies from 0 - 8.\r | |
844 | d is the byte object rotated.\r | |
845 | have\r | |
846 | CF B_7 ... B_0\r | |
847 | The new rotate is done mod 8.\r | |
848 | Much simpler than the "rcl" or "rcr" operations.\r | |
849 | IF n > 0\r | |
850 | 1) B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0)\r | |
851 | 2) B_(n-1) .. B_(0) <- b_(7) .. b_(8-n)\r | |
852 | I think this is correct.\r | |
853 | */\r | |
854 | res =d;\r | |
855 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
856 | if ((cnt = s % 8)) /* not a typo, do nada if cnt==0 */\r | |
857 | {\r | |
858 | /* B_(7) .. B_(n) <- b_(8-(n+1)) .. b_(0) */\r | |
859 | res = (d << cnt);\r | |
860 | /* B_(n-1) .. B_(0) <- b_(7) .. b_(8-n) */\r | |
861 | mask = (1 << cnt) - 1;\r | |
862 | res |= (d >> (8-cnt)) & mask;\r | |
863 | /* set the new carry flag, Note that it is the low order\r | |
864 | bit of the result!!! */\r | |
865 | CONDITIONAL_SET_FLAG(res&0x1, m, F_CF);\r | |
866 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
867 | xor of CF and the most significant bit. Blecck. */\r | |
868 | CONDITIONAL_SET_FLAG(cnt==1 &&\r | |
869 | xor_0x3_tab[(res&0x1)+((res>>6)&0x2)],\r | |
870 | m, F_OF);\r | |
871 | }\r | |
872 | return res&0xff;\r | |
873 | }\r | |
874 | \r | |
875 | uint16 rol_word(PC_ENV *m, uint16 d, uint16 s)\r | |
876 | {\r | |
877 | register uint32 res, cnt, mask;\r | |
878 | /* rotate left */\r | |
879 | /*\r | |
880 | s is the rotate distance. It varies from 0 - 8.\r | |
881 | d is the byte object rotated.\r | |
882 | have\r | |
883 | CF B_15 ... B_0\r | |
884 | The new rotate is done mod 8.\r | |
885 | Much simpler than the "rcl" or "rcr" operations.\r | |
886 | IF n > 0\r | |
887 | 1) B_(15) .. B_(n) <- b_(16-(n+1)) .. b_(0)\r | |
888 | 2) B_(n-1) .. B_(0) <- b_(16) .. b_(16-n)\r | |
889 | I think this is correct.\r | |
890 | */\r | |
891 | res = d;\r | |
892 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
893 | if ((cnt = s % 16)) /* not a typo, do nada if cnt==0 */\r | |
894 | {\r | |
895 | /* B_(16) .. B_(n) <- b_(16-(n+1)) .. b_(0) */\r | |
896 | res = (d << cnt);\r | |
897 | /* B_(n-1) .. B_(0) <- b_(15) .. b_(16-n) */\r | |
898 | mask = (1 << cnt) - 1;\r | |
899 | res |= (d >> (16-cnt)) & mask;\r | |
900 | /* set the new carry flag, Note that it is the low order\r | |
901 | bit of the result!!! */\r | |
902 | CONDITIONAL_SET_FLAG(res&0x1, m, F_CF);\r | |
903 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
904 | xor of CF and the most significant bit. Blecck. */\r | |
905 | CONDITIONAL_SET_FLAG(cnt==1 &&\r | |
906 | xor_0x3_tab[(res&0x1)+((res>>14)&0x2)],\r | |
907 | m, F_OF);\r | |
908 | }\r | |
909 | return res&0xffff;\r | |
910 | }\r | |
911 | \r | |
912 | uint8 ror_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
913 | {\r | |
914 | register uint32 res, cnt, mask;\r | |
915 | /* rotate right */\r | |
916 | /*\r | |
917 | s is the rotate distance. It varies from 0 - 8.\r | |
918 | d is the byte object rotated.\r | |
919 | have\r | |
920 | B_7 ... B_0\r | |
921 | The rotate is done mod 8.\r | |
922 | IF n > 0\r | |
923 | 1) B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n)\r | |
924 | 2) B_(7) .. B_(8-n) <- b_(n-1) .. b_(0)\r | |
925 | */\r | |
926 | res = d;\r | |
927 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
928 | if ((cnt = s % 8)) /* not a typo, do nada if cnt==0 */\r | |
929 | {\r | |
930 | /* B_(7) .. B_(8-n) <- b_(n-1) .. b_(0)*/\r | |
931 | res = (d << (8-cnt));\r | |
932 | /* B_(8-(n+1)) .. B_(0) <- b_(7) .. b_(n) */\r | |
933 | mask = (1 << (8-cnt)) - 1;\r | |
934 | res |= (d >> (cnt)) & mask;\r | |
935 | /* set the new carry flag, Note that it is the low order\r | |
936 | bit of the result!!! */\r | |
937 | CONDITIONAL_SET_FLAG(res&0x80, m, F_CF);\r | |
938 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
939 | xor of the two most significant bits. Blecck. */\r | |
940 | CONDITIONAL_SET_FLAG(cnt==1 &&\r | |
941 | xor_0x3_tab[(res>>6)&0x3],\r | |
942 | m, F_OF);\r | |
943 | }\r | |
944 | return res&0xff;\r | |
945 | }\r | |
946 | \r | |
947 | uint16 ror_word(PC_ENV *m, uint16 d, uint16 s)\r | |
948 | {\r | |
949 | register uint32 res, cnt, mask;\r | |
950 | /* rotate right */\r | |
951 | /*\r | |
952 | s is the rotate distance. It varies from 0 - 8.\r | |
953 | d is the byte object rotated.\r | |
954 | have\r | |
955 | B_15 ... B_0\r | |
956 | The rotate is done mod 16.\r | |
957 | IF n > 0\r | |
958 | 1) B_(16-(n+1)) .. B_(0) <- b_(15) .. b_(n)\r | |
959 | 2) B_(15) .. B_(16-n) <- b_(n-1) .. b_(0)\r | |
960 | I think this is correct.\r | |
961 | */\r | |
962 | res =d ;\r | |
963 | /* [JCE] Extra brackets to stop gcc -Wall moaning */\r | |
964 | if ((cnt = s % 16)) /* not a typo, do nada if cnt==0 */\r | |
965 | {\r | |
966 | /* B_(15) .. B_(16-n) <- b_(n-1) .. b_(0)*/\r | |
967 | res = (d << (16-cnt));\r | |
968 | /* B_(16-(n+1)) .. B_(0) <- b_(15) .. b_(n) */\r | |
969 | mask = (1 << (16-cnt)) - 1;\r | |
970 | res |= (d >> (cnt)) & mask;\r | |
971 | /* set the new carry flag, Note that it is the low order\r | |
972 | bit of the result!!! */\r | |
973 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_CF);\r | |
974 | /* OVERFLOW is set *IFF* cnt==1, then it is the\r | |
975 | xor of CF and the most significant bit. Blecck. */\r | |
976 | CONDITIONAL_SET_FLAG(cnt==1 &&\r | |
977 | xor_0x3_tab[(res>>14)&0x3],\r | |
978 | m, F_OF);\r | |
979 | }\r | |
980 | return res & 0xffff;\r | |
981 | }\r | |
982 | \r | |
983 | uint8 shl_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
984 | {\r | |
985 | uint32 cnt,res,cf;\r | |
986 | if (s < 8)\r | |
987 | {\r | |
988 | cnt = s % 8;\r | |
989 | /* last bit shifted out goes into carry flag */\r | |
990 | if (cnt>0)\r | |
991 | {\r | |
992 | res = d << cnt;\r | |
993 | cf = d & (1<<(8-cnt));\r | |
994 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
995 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
996 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
997 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
998 | }\r | |
999 | else\r | |
1000 | {\r | |
1001 | res = (uint8)d;\r | |
1002 | }\r | |
1003 | if (cnt == 1)\r | |
1004 | {\r | |
1005 | /* Needs simplification. */\r | |
1006 | CONDITIONAL_SET_FLAG(\r | |
1007 | (((res&0x80)==0x80) ^\r | |
1008 | (ACCESS_FLAG(m,F_CF) != 0)) ,\r | |
1009 | /* was (m->R_FLG&F_CF)==F_CF)), */\r | |
1010 | m, F_OF);\r | |
1011 | }\r | |
1012 | else\r | |
1013 | {\r | |
1014 | CLEAR_FLAG(m,F_OF);\r | |
1015 | }\r | |
1016 | }\r | |
1017 | else\r | |
1018 | {\r | |
1019 | res = 0;\r | |
1020 | CLEAR_FLAG(m,F_CF);\r | |
1021 | CLEAR_FLAG(m,F_OF);\r | |
1022 | CLEAR_FLAG(m,F_SF);\r | |
1023 | CLEAR_FLAG(m,F_PF);\r | |
1024 | SET_FLAG(m,F_ZF);\r | |
1025 | }\r | |
1026 | return res&0xff;\r | |
1027 | }\r | |
1028 | \r | |
1029 | uint16 shl_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1030 | {\r | |
1031 | uint32 cnt,res,cf;\r | |
1032 | if (s < 16)\r | |
1033 | {\r | |
1034 | cnt = s % 16;\r | |
1035 | if (cnt > 0)\r | |
1036 | {\r | |
1037 | res = d << cnt;\r | |
1038 | /* last bit shifted out goes into carry flag */\r | |
1039 | cf = d & (1<<(16-cnt));\r | |
1040 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
1041 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
1042 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
1043 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1044 | }\r | |
1045 | else\r | |
1046 | {\r | |
1047 | res = (uint16)d;\r | |
1048 | }\r | |
1049 | if (cnt == 1)\r | |
1050 | {\r | |
1051 | /* Needs simplification. */\r | |
1052 | CONDITIONAL_SET_FLAG(\r | |
1053 | (((res&0x8000)==0x8000) ^\r | |
1054 | (ACCESS_FLAG(m,F_CF) != 0)),\r | |
1055 | /*((m&F_CF)==F_CF)),*/\r | |
1056 | m, F_OF);\r | |
1057 | }\r | |
1058 | else\r | |
1059 | {\r | |
1060 | CLEAR_FLAG(m,F_OF);\r | |
1061 | }\r | |
1062 | }\r | |
1063 | else\r | |
1064 | {\r | |
1065 | res = 0;\r | |
1066 | CLEAR_FLAG(m,F_CF);\r | |
1067 | CLEAR_FLAG(m,F_OF);\r | |
1068 | SET_FLAG(m,F_ZF);\r | |
1069 | CLEAR_FLAG(m,F_SF);\r | |
1070 | CLEAR_FLAG(m,F_PF);\r | |
1071 | }\r | |
1072 | return res&0xffff;\r | |
1073 | }\r | |
1074 | \r | |
1075 | uint8 shr_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
1076 | {\r | |
1077 | uint32 cnt,res,cf,mask;\r | |
1078 | if (s < 8)\r | |
1079 | {\r | |
1080 | cnt = s % 8;\r | |
1081 | if (cnt > 0)\r | |
1082 | {\r | |
1083 | mask = (1<<(8-cnt))-1;\r | |
1084 | cf = d & (1<<(cnt-1));\r | |
1085 | res = (d >> cnt) & mask;\r | |
1086 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
1087 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
1088 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
1089 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1090 | }\r | |
1091 | else\r | |
1092 | {\r | |
1093 | res = (uint8) d;\r | |
1094 | }\r | |
1095 | if (cnt == 1)\r | |
1096 | {\r | |
1097 | CONDITIONAL_SET_FLAG(\r | |
1098 | xor_0x3_tab[(res>>6)&0x3], m, F_OF);\r | |
1099 | }\r | |
1100 | else\r | |
1101 | {\r | |
1102 | CLEAR_FLAG(m,F_OF);\r | |
1103 | }\r | |
1104 | }\r | |
1105 | else\r | |
1106 | {\r | |
1107 | res = 0;\r | |
1108 | CLEAR_FLAG(m,F_CF);\r | |
1109 | CLEAR_FLAG(m,F_OF);\r | |
1110 | SET_FLAG(m,F_ZF);\r | |
1111 | CLEAR_FLAG(m,F_SF);\r | |
1112 | CLEAR_FLAG(m,F_PF);\r | |
1113 | }\r | |
1114 | return res&0xff;\r | |
1115 | }\r | |
1116 | \r | |
1117 | uint16 shr_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1118 | {\r | |
1119 | uint32 cnt,res,cf,mask;\r | |
1120 | res = d;\r | |
1121 | if (s < 16)\r | |
1122 | {\r | |
1123 | cnt = s % 16;\r | |
1124 | if (cnt > 0)\r | |
1125 | {\r | |
1126 | mask = (1<<(16-cnt))-1;\r | |
1127 | cf = d & (1<<(cnt-1));\r | |
1128 | res = (d >> cnt) & mask;\r | |
1129 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
1130 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
1131 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
1132 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1133 | }\r | |
1134 | else\r | |
1135 | {\r | |
1136 | res = d;\r | |
1137 | }\r | |
1138 | if (cnt == 1)\r | |
1139 | {\r | |
1140 | CONDITIONAL_SET_FLAG(\r | |
1141 | xor_0x3_tab[(res>>14)&0x3], m, F_OF);\r | |
1142 | }\r | |
1143 | else\r | |
1144 | {\r | |
1145 | CLEAR_FLAG(m,F_OF);\r | |
1146 | }\r | |
1147 | }\r | |
1148 | else\r | |
1149 | {\r | |
1150 | res = 0;\r | |
1151 | CLEAR_FLAG(m,F_CF);\r | |
1152 | CLEAR_FLAG(m,F_OF);\r | |
1153 | SET_FLAG(m,F_ZF);\r | |
1154 | CLEAR_FLAG(m,F_SF);\r | |
1155 | CLEAR_FLAG(m,F_PF);\r | |
1156 | }\r | |
1157 | return res&0xffff;\r | |
1158 | }\r | |
1159 | \r | |
1160 | /* XXXX ??? flags may be wrong??? */\r | |
1161 | uint8 sar_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
1162 | {\r | |
1163 | uint32 cnt,res,cf,mask,sf;\r | |
1164 | res = d;\r | |
1165 | sf = d & 0x80;\r | |
1166 | cnt = s % 8;\r | |
1167 | if(cnt > 0 && cnt < 8)\r | |
1168 | {\r | |
1169 | mask = (1<<(8-cnt))-1;\r | |
1170 | cf = d & (1<<(cnt-1));\r | |
1171 | res = (d >> cnt) & mask;\r | |
1172 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
1173 | if (sf)\r | |
1174 | {\r | |
1175 | res |= ~mask;\r | |
1176 | }\r | |
1177 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
1178 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1179 | CONDITIONAL_SET_FLAG(res & 0x80, m, F_SF);\r | |
1180 | }\r | |
1181 | else if (cnt >= 8)\r | |
1182 | {\r | |
1183 | if (sf)\r | |
1184 | {\r | |
1185 | res = 0xff;\r | |
1186 | SET_FLAG(m,F_CF);\r | |
1187 | CLEAR_FLAG(m,F_ZF);\r | |
1188 | SET_FLAG(m, F_SF);\r | |
1189 | SET_FLAG(m, F_PF);\r | |
1190 | }\r | |
1191 | else\r | |
1192 | {\r | |
1193 | res = 0;\r | |
1194 | CLEAR_FLAG(m,F_CF);\r | |
1195 | SET_FLAG(m,F_ZF);\r | |
1196 | CLEAR_FLAG(m, F_SF);\r | |
1197 | CLEAR_FLAG(m, F_PF);\r | |
1198 | }\r | |
1199 | }\r | |
1200 | return res&0xff;\r | |
1201 | }\r | |
1202 | \r | |
1203 | uint16 sar_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1204 | {\r | |
1205 | uint32 cnt, res, cf, mask, sf;\r | |
1206 | sf = d & 0x8000;\r | |
1207 | cnt = s % 16;\r | |
1208 | res = d;\r | |
1209 | if (cnt > 0 && cnt < 16)\r | |
1210 | {\r | |
1211 | mask = (1<<(16-cnt))-1;\r | |
1212 | cf = d & (1<<(cnt-1));\r | |
1213 | res = (d >> cnt) & mask;\r | |
1214 | CONDITIONAL_SET_FLAG(cf, m, F_CF);\r | |
1215 | if (sf)\r | |
1216 | {\r | |
1217 | res |= ~mask;\r | |
1218 | }\r | |
1219 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
1220 | CONDITIONAL_SET_FLAG(res & 0x8000, m, F_SF);\r | |
1221 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1222 | }\r | |
1223 | else if (cnt >= 16)\r | |
1224 | {\r | |
1225 | if (sf)\r | |
1226 | {\r | |
1227 | res = 0xffff;\r | |
1228 | SET_FLAG(m,F_CF);\r | |
1229 | CLEAR_FLAG(m,F_ZF);\r | |
1230 | SET_FLAG(m, F_SF);\r | |
1231 | SET_FLAG(m, F_PF);\r | |
1232 | }\r | |
1233 | else\r | |
1234 | {\r | |
1235 | res = 0;\r | |
1236 | CLEAR_FLAG(m,F_CF);\r | |
1237 | SET_FLAG(m,F_ZF);\r | |
1238 | CLEAR_FLAG(m, F_SF);\r | |
1239 | CLEAR_FLAG(m, F_PF);\r | |
1240 | }\r | |
1241 | }\r | |
1242 | return res&0xffff;\r | |
1243 | }\r | |
1244 | \r | |
1245 | uint8 sbb_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
1246 | {\r | |
1247 | register uint32 res; /* all operands in native machine order */\r | |
1248 | register uint32 bc;\r | |
1249 | if (ACCESS_FLAG(m,F_CF) )\r | |
1250 | res = d - s - 1;\r | |
1251 | else\r | |
1252 | res = d - s;\r | |
1253 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
1254 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
1255 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1256 | /* calculate the borrow chain. See note at top */\r | |
1257 | bc= (res&(~d|s))|(~d&s);\r | |
1258 | CONDITIONAL_SET_FLAG(bc&0x80,m, F_CF);\r | |
1259 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>6)&0x3], m, F_OF);\r | |
1260 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
1261 | return res & 0xff;\r | |
1262 | }\r | |
1263 | \r | |
1264 | uint16 sbb_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1265 | {\r | |
1266 | register uint32 res; /* all operands in native machine order */\r | |
1267 | register uint32 bc;\r | |
1268 | if (ACCESS_FLAG(m,F_CF))\r | |
1269 | res = d - s - 1;\r | |
1270 | else\r | |
1271 | res = d - s;\r | |
1272 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
1273 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
1274 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1275 | /* calculate the borrow chain. See note at top */\r | |
1276 | bc= (res&(~d|s))|(~d&s);\r | |
1277 | CONDITIONAL_SET_FLAG(bc&0x8000,m, F_CF);\r | |
1278 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>14)&0x3], m, F_OF);\r | |
1279 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
1280 | return res & 0xffff;\r | |
1281 | }\r | |
1282 | \r | |
1283 | uint8 sub_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
1284 | {\r | |
1285 | register uint32 res; /* all operands in native machine order */\r | |
1286 | register uint32 bc;\r | |
1287 | res = d - s;\r | |
1288 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
1289 | CONDITIONAL_SET_FLAG((res&0xff)==0, m, F_ZF);\r | |
1290 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1291 | /* calculate the borrow chain. See note at top */\r | |
1292 | bc= (res&(~d|s))|(~d&s);\r | |
1293 | CONDITIONAL_SET_FLAG(bc&0x80,m, F_CF);\r | |
1294 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>6)&0x3], m, F_OF);\r | |
1295 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
1296 | return res & 0xff;\r | |
1297 | }\r | |
1298 | \r | |
1299 | uint16 sub_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1300 | {\r | |
1301 | register uint32 res; /* all operands in native machine order */\r | |
1302 | register uint32 bc;\r | |
1303 | res = d - s;\r | |
1304 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
1305 | CONDITIONAL_SET_FLAG((res&0xffff)==0, m, F_ZF);\r | |
1306 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1307 | /* calculate the borrow chain. See note at top */\r | |
1308 | bc= (res&(~d|s))|(~d&s);\r | |
1309 | CONDITIONAL_SET_FLAG(bc&0x8000,m, F_CF);\r | |
1310 | CONDITIONAL_SET_FLAG(xor_0x3_tab[(bc>>14)&0x3], m, F_OF);\r | |
1311 | CONDITIONAL_SET_FLAG(bc&0x8, m, F_AF);\r | |
1312 | return res & 0xffff;\r | |
1313 | }\r | |
1314 | \r | |
1315 | void test_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
1316 | {\r | |
1317 | register uint32 res; /* all operands in native machine order */\r | |
1318 | res = d & s;\r | |
1319 | CLEAR_FLAG(m, F_OF);\r | |
1320 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
1321 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
1322 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1323 | /* AF == dont care*/\r | |
1324 | CLEAR_FLAG(m, F_CF);\r | |
1325 | }\r | |
1326 | \r | |
1327 | void test_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1328 | {\r | |
1329 | register uint32 res; /* all operands in native machine order */\r | |
1330 | res = d & s;\r | |
1331 | CLEAR_FLAG(m, F_OF);\r | |
1332 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
1333 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
1334 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1335 | /* AF == dont care*/\r | |
1336 | CLEAR_FLAG(m, F_CF);\r | |
1337 | }\r | |
1338 | \r | |
1339 | uint8 xor_byte(PC_ENV *m, uint8 d, uint8 s)\r | |
1340 | {\r | |
1341 | register uint8 res; /* all operands in native machine order */\r | |
1342 | res = d ^ s;\r | |
1343 | CLEAR_FLAG(m, F_OF);\r | |
1344 | CONDITIONAL_SET_FLAG(res&0x80, m, F_SF);\r | |
1345 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
1346 | CONDITIONAL_SET_FLAG(parity_tab[res], m, F_PF);\r | |
1347 | CLEAR_FLAG(m, F_CF);\r | |
1348 | return res;\r | |
1349 | }\r | |
1350 | \r | |
1351 | uint16 xor_word(PC_ENV *m, uint16 d, uint16 s)\r | |
1352 | {\r | |
1353 | register uint16 res; /* all operands in native machine order */\r | |
1354 | res = d ^ s;\r | |
1355 | /* set the carry flag to be bit 8 */\r | |
1356 | CLEAR_FLAG(m, F_OF);\r | |
1357 | CONDITIONAL_SET_FLAG(res&0x8000, m, F_SF);\r | |
1358 | CONDITIONAL_SET_FLAG(res==0, m, F_ZF);\r | |
1359 | CONDITIONAL_SET_FLAG(parity_tab[res&0xff], m, F_PF);\r | |
1360 | CLEAR_FLAG(m, F_CF);\r | |
1361 | return res;\r | |
1362 | }\r | |
1363 | \r | |
1364 | void imul_byte(PC_ENV *m, uint8 s)\r | |
1365 | {\r | |
1366 | int16 res = (int8)m->R_AL * (int8)s;\r | |
1367 | m->R_AX = res;\r | |
1368 | /* Undef --- Can't hurt */\r | |
1369 | CONDITIONAL_SET_FLAG(res&0x8000,m,F_SF);\r | |
1370 | CONDITIONAL_SET_FLAG(res==0,m,F_ZF);\r | |
1371 | if (m->R_AH == 0 || m->R_AH == 0xff)\r | |
1372 | {\r | |
1373 | CLEAR_FLAG(m, F_CF);\r | |
1374 | CLEAR_FLAG(m, F_OF);\r | |
1375 | }\r | |
1376 | else\r | |
1377 | {\r | |
1378 | SET_FLAG(m, F_CF);\r | |
1379 | SET_FLAG(m, F_OF);\r | |
1380 | }\r | |
1381 | }\r | |
1382 | \r | |
1383 | void imul_word(PC_ENV *m, uint16 s)\r | |
1384 | {\r | |
1385 | int32 res = (int16)m->R_AX * (int16)s;\r | |
1386 | m->R_AX = res & 0xffff;\r | |
1387 | m->R_DX = (res >> 16) & 0xffff;\r | |
1388 | /* Undef --- Can't hurt */\r | |
1389 | CONDITIONAL_SET_FLAG(res&0x80000000,m,F_SF);\r | |
1390 | CONDITIONAL_SET_FLAG(res==0,m,F_ZF);\r | |
1391 | if (m->R_DX == 0 || m->R_DX == 0xffff)\r | |
1392 | {\r | |
1393 | CLEAR_FLAG(m, F_CF);\r | |
1394 | CLEAR_FLAG(m, F_OF);\r | |
1395 | }\r | |
1396 | else\r | |
1397 | {\r | |
1398 | SET_FLAG(m, F_CF);\r | |
1399 | SET_FLAG(m, F_OF);\r | |
1400 | }\r | |
1401 | }\r | |
1402 | \r | |
1403 | void mul_byte(PC_ENV *m, uint8 s)\r | |
1404 | {\r | |
1405 | uint16 res = m->R_AL * s;\r | |
1406 | m->R_AX = res;\r | |
1407 | /* Undef --- Can't hurt */\r | |
1408 | CLEAR_FLAG(m,F_SF);\r | |
1409 | CONDITIONAL_SET_FLAG(res==0,m,F_ZF);\r | |
1410 | if (m->R_AH == 0)\r | |
1411 | {\r | |
1412 | CLEAR_FLAG(m, F_CF);\r | |
1413 | CLEAR_FLAG(m, F_OF);\r | |
1414 | }\r | |
1415 | else\r | |
1416 | {\r | |
1417 | SET_FLAG(m, F_CF);\r | |
1418 | SET_FLAG(m, F_OF);\r | |
1419 | }\r | |
1420 | }\r | |
1421 | \r | |
1422 | void mul_word(PC_ENV *m, uint16 s)\r | |
1423 | {\r | |
1424 | uint32 res = m->R_AX * s;\r | |
1425 | /* Undef --- Can't hurt */\r | |
1426 | CLEAR_FLAG(m,F_SF);\r | |
1427 | CONDITIONAL_SET_FLAG(res==0,m,F_ZF);\r | |
1428 | m->R_AX = res & 0xffff;\r | |
1429 | m->R_DX = (res >> 16) & 0xffff;\r | |
1430 | if (m->R_DX == 0)\r | |
1431 | {\r | |
1432 | CLEAR_FLAG(m, F_CF);\r | |
1433 | CLEAR_FLAG(m, F_OF);\r | |
1434 | }\r | |
1435 | else\r | |
1436 | {\r | |
1437 | SET_FLAG(m, F_CF);\r | |
1438 | SET_FLAG(m, F_OF);\r | |
1439 | }\r | |
1440 | }\r | |
1441 | \r | |
1442 | void idiv_byte(PC_ENV *m, uint8 s)\r | |
1443 | {\r | |
1444 | int32 dvd,div,mod;\r | |
1445 | dvd = (int16)m->R_AX;\r | |
1446 | if (s == 0)\r | |
1447 | {\r | |
1448 | i86_intr_raise(m,0);\r | |
1449 | return;\r | |
1450 | }\r | |
1451 | div = dvd / (int8)s;\r | |
1452 | mod = dvd % (int8)s;\r | |
1453 | if (abs(div) > 0x7f)\r | |
1454 | {\r | |
1455 | i86_intr_raise(m,0);\r | |
1456 | return;\r | |
1457 | }\r | |
1458 | /* Undef --- Can't hurt */\r | |
1459 | CONDITIONAL_SET_FLAG(div&0x80,m,F_SF);\r | |
1460 | CONDITIONAL_SET_FLAG(div==0,m,F_ZF);\r | |
1461 | m->R_AL = (int8)div;\r | |
1462 | m->R_AH = (int8)mod;\r | |
1463 | }\r | |
1464 | \r | |
1465 | void idiv_word(PC_ENV *m, uint16 s)\r | |
1466 | {\r | |
1467 | int32 dvd,dvs,div,mod;\r | |
1468 | dvd = m->R_DX;\r | |
1469 | dvd = (dvd << 16) | m->R_AX;\r | |
1470 | if (s == 0)\r | |
1471 | {\r | |
1472 | i86_intr_raise(m,0);\r | |
1473 | return;\r | |
1474 | }\r | |
1475 | dvs = (int16)s;\r | |
1476 | div = dvd / dvs;\r | |
1477 | mod = dvd % dvs;\r | |
1478 | if (abs(div) > 0x7fff)\r | |
1479 | {\r | |
1480 | i86_intr_raise(m,0);\r | |
1481 | return;\r | |
1482 | }\r | |
1483 | /* Undef --- Can't hurt */\r | |
1484 | CONDITIONAL_SET_FLAG(div&0x8000,m,F_SF);\r | |
1485 | CONDITIONAL_SET_FLAG(div==0,m,F_ZF);\r | |
1486 | /* debug_printf(m, "\n%d/%d=%d,%d\n",dvd,dvs,div,mod); */\r | |
1487 | m->R_AX = div;\r | |
1488 | m->R_DX = mod;\r | |
1489 | }\r | |
1490 | \r | |
1491 | void div_byte(PC_ENV *m, uint8 s)\r | |
1492 | {\r | |
1493 | uint32 dvd,dvs,div,mod;\r | |
1494 | dvs = s;\r | |
1495 | dvd = m->R_AX;\r | |
1496 | if (s == 0)\r | |
1497 | {\r | |
1498 | i86_intr_raise(m,0);\r | |
1499 | return;\r | |
1500 | }\r | |
1501 | div = dvd / dvs;\r | |
1502 | mod = dvd % dvs;\r | |
1503 | if (abs(div) > 0xff)\r | |
1504 | {\r | |
1505 | i86_intr_raise(m,0);\r | |
1506 | return;\r | |
1507 | }\r | |
1508 | /* Undef --- Can't hurt */\r | |
1509 | CLEAR_FLAG(m,F_SF);\r | |
1510 | CONDITIONAL_SET_FLAG(div==0,m,F_ZF);\r | |
1511 | m->R_AL = (uint8)div;\r | |
1512 | m->R_AH = (uint8)mod;\r | |
1513 | }\r | |
1514 | \r | |
1515 | void div_word(PC_ENV *m, uint16 s)\r | |
1516 | {\r | |
1517 | uint32 dvd,dvs,div,mod;\r | |
1518 | dvd = m->R_DX;\r | |
1519 | dvd = (dvd << 16) | m->R_AX;\r | |
1520 | dvs = s;\r | |
1521 | if (dvs == 0)\r | |
1522 | {\r | |
1523 | i86_intr_raise(m,0);\r | |
1524 | return;\r | |
1525 | }\r | |
1526 | div = dvd / dvs;\r | |
1527 | mod = dvd % dvs;\r | |
1528 | /* printf("dvd=%x dvs=%x -> div=%x mod=%x\n",dvd, dvs,div, mod);*/\r | |
1529 | if (abs(div) > 0xffff)\r | |
1530 | {\r | |
1531 | i86_intr_raise(m,0);\r | |
1532 | return;\r | |
1533 | }\r | |
1534 | /* Undef --- Can't hurt */\r | |
1535 | CLEAR_FLAG(m,F_SF);\r | |
1536 | CONDITIONAL_SET_FLAG(div==0,m,F_ZF);\r | |
1537 | m->R_AX = div;\r | |
1538 | m->R_DX = mod;\r | |
1539 | }\r |