forked from jeffpar/pcjs.v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x86func.js
3395 lines (3222 loc) · 98.2 KB
/
x86func.js
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview Implements PCx86 opcode workers
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
if (typeof module !== "undefined") {
var Messages = require("./messages");
var X86 = require("./x86");
}
/**
* fnADCb(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnADCb = function(dst, src)
{
let b = (dst + src + this.getCarry())|0;
this.setArithResult(dst, src, b, X86.RESULT.BYTE | X86.RESULT.ALL);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesArithRM) : this.cycleCounts.nOpCyclesArithMR);
return b & 0xff;
};
/**
* fnADCw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnADCw = function(dst, src)
{
let w = (dst + src + this.getCarry())|0;
this.setArithResult(dst, src, w, this.typeData | X86.RESULT.ALL);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesArithRM) : this.cycleCounts.nOpCyclesArithMR);
return w & this.maskData;
};
/**
* fnADDb(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnADDb = function(dst, src)
{
let b = (dst + src)|0;
this.setArithResult(dst, src, b, X86.RESULT.BYTE | X86.RESULT.ALL);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesArithRM) : this.cycleCounts.nOpCyclesArithMR);
return b & 0xff;
};
/**
* fnADDw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnADDw = function(dst, src)
{
let w = (dst + src)|0;
this.setArithResult(dst, src, w, this.typeData | X86.RESULT.ALL);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesArithRM) : this.cycleCounts.nOpCyclesArithMR);
return w & this.maskData;
};
/**
* fnANDb(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnANDb = function(dst, src)
{
let b = dst & src;
this.setLogicResult(b, X86.RESULT.BYTE);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesArithRM) : this.cycleCounts.nOpCyclesArithMR);
return b;
};
/**
* fnANDw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnANDw = function(dst, src)
{
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesArithRM) : this.cycleCounts.nOpCyclesArithMR);
return this.setLogicResult(dst & src, this.typeData) & this.maskData;
};
/**
* fnARPL(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnARPL = function(dst, src)
{
this.nStepCycles -= (10 + (this.regEA === X86.ADDR_INVALID? 0 : 1));
if ((dst & X86.SEL.RPL) < (src & X86.SEL.RPL)) {
dst = (dst & ~X86.SEL.RPL) | (src & X86.SEL.RPL);
this.setZF();
return dst;
}
this.clearZF();
return dst;
};
/**
* fnBOUND(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBOUND = function(dst, src)
{
if (this.regEA === X86.ADDR_INVALID) {
/*
* Generate UD_FAULT (INT 0x06: Invalid Opcode) if src is not a memory operand.
*/
X86.opInvalid.call(this);
return dst;
}
/*
* Note that BOUND performs signed comparisons, so we must transform all arguments into signed values.
*/
let wIndex = dst;
let wLower = this.getWord(this.regEA);
let wUpper = this.getWord(this.regEA + this.sizeData);
if (this.sizeData == 2) {
wIndex = (dst << 16) >> 16;
wLower = (wLower << 16) >> 16;
wUpper = (wUpper << 16) >> 16;
}
this.nStepCycles -= this.cycleCounts.nOpCyclesBound;
if (wIndex < wLower || wIndex > wUpper) {
/*
* The INT 0x05 handler must be called with CS:IP pointing to the BOUND instruction.
*
* TODO: Determine the cycle cost when a BOUND exception is triggered, over and above nCyclesBound,
* and then call X86.helpFault(X86.EXCEPTION.BR_FAULT, null, nCycles).
*/
X86.helpFault.call(this, X86.EXCEPTION.BR_FAULT);
}
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnBSF(dst, src)
*
* Scan src starting at bit 0. If a set bit is found, the bit index is stored in dst and ZF is cleared;
* otherwise, ZF is set and dst is unchanged.
*
* NOTES: Early versions of the 80386 manuals misstated how ZF was set/cleared. Also, Intel insists that
* dst is undefined whenever ZF is set, but in fact, the 80386 leaves dst unchanged when that happens;
* unfortunately, some early 80486s would always modify dst, so it is unsafe to rely on dst when ZF is set.
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBSF = function(dst, src)
{
let n = 0;
if (!src) {
this.setZF();
} else {
this.clearZF();
let bit = 0x1;
while (bit & this.maskData) {
if (src & bit) {
dst = n;
break;
}
bit <<= 1;
n++; // TODO: Determine if n should be incremented before the bailout for an accurate cycle count
}
}
this.nStepCycles -= 11 + n * 3;
return dst;
};
/**
* fnBSR(dst, src)
*
* Scan src starting from the highest bit. If a set bit is found, the bit index is stored in dst and ZF is
* cleared; otherwise, ZF is set and dst is unchanged.
*
* NOTES: Early versions of the 80386 manuals misstated how ZF was set/cleared. Also, Intel insists that
* dst is undefined whenever ZF is set, but in fact, the 80386 leaves dst unchanged when that happens;
* unfortunately, some early 80486s would always modify dst, so it is unsafe to rely on dst when ZF is set.
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBSR = function(dst, src)
{
let n = 0;
if (!src) {
this.setZF();
} else {
this.clearZF();
let i = (this.sizeData == 2? 15 : 31), bit = 1 << i;
while (bit) {
if (src & bit) {
dst = i;
break;
}
bit >>>= 1;
n++; i--; // TODO: Determine if n should be incremented before the bailout for an accurate cycle count
}
}
this.nStepCycles -= 11 + n * 3;
return dst;
};
/**
* fnBT(dst, src)
*
* In this form of BT, src is an immediate operand (OR dst is register operand); immediate operands
* are supposed to be masked with either 0xf or 0x1f for 16-bit or 32-bit operands, respectively.
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBT = function(dst, src)
{
let bit = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & bit) this.setCF(); else this.clearCF();
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? 3 : 6);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnBTC(dst, src)
*
* In this form of BTC, src is an immediate operand (OR dst is register operand); immediate operands
* are supposed to be masked with either 0xf or 0x1f for 16-bit or 32-bit operands, respectively.
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTC = function(dst, src)
{
let bit = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & bit) this.setCF(); else this.clearCF();
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? 6 : 8);
return dst ^ bit;
};
/**
* fnBTR(dst, src)
*
* In this form of BTR, src is an immediate operand (OR dst is register operand); immediate operands
* are supposed to be masked with either 0xf or 0x1f for 16-bit or 32-bit operands, respectively.
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTR = function(dst, src)
{
let bit = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & bit) this.setCF(); else this.clearCF();
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? 6 : 8);
return dst & ~bit;
};
/**
* fnBTS(dst, src)
*
* In this form of BTS, src is an immediate operand (OR dst is register operand); immediate operands
* are supposed to be masked with either 0xf or 0x1f for 16-bit or 32-bit operands, respectively.
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTS = function(dst, src)
{
let bit = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & bit) this.setCF(); else this.clearCF();
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? 6 : 8);
return dst | bit;
};
/**
* fnBTMem(dst, src)
*
* In this form of BT, src is a register operand, which is NOT truncated if dst is a memory operand;
* however, if dst is also a register operand, then we defer to the simpler function, fnBT().
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTMem = function(dst, src)
{
if (this.regEA === X86.ADDR_INVALID) {
return X86.fnBT.call(this, dst, src);
}
/*
* TODO: Consider a worker function that performs the following block of code for: BT, BTC, BTR, and BTS.
* It's somewhat inconvenient, because it needs to provide two results: an updated src AND an updated dst.
*
* src is usually positive BUT can also be negative (as the IA32 spec says: "The offset operand then selects
* a bit position within the range −231 to 231 − 1 for a register offset and 0 to 31 for an immediate offset.")
*/
let max = this.sizeData << 3;
if (src >= max || src < -max) {
/*
* Now we need to divide src by 16 or 32, according to the OPERAND size, which means shifting it right
* by either 4 or 5 bits. That gives us a short or long INDEX, which we then multiply by the OPERAND size
* to obtain to the corresponding short or long OFFSET that we must add to the original EA offset.
*/
let i = src >> (this.sizeData == 2? 4 : 5);
dst = this.getEAWord(this.segEA, this.offEA + i * this.sizeData);
}
/*
* Now we convert src from a bit index to a bit mask.
*/
src = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & src) this.setCF(); else this.clearCF();
this.nStepCycles -= 6;
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnBTCMem(dst, src)
*
* In this form of BTC, src is a register operand, which is NOT truncated if dst is a memory operand;
* however, if dst is also a register operand, then we defer to the simpler function, fnBTC().
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTCMem = function(dst, src)
{
if (this.regEA === X86.ADDR_INVALID) {
return X86.fnBTC.call(this, dst, src);
}
/*
* src is usually positive BUT can also be negative (as the IA32 spec says: "The offset operand then selects
* a bit position within the range −231 to 231 − 1 for a register offset and 0 to 31 for an immediate offset.")
*/
let max = this.sizeData << 3;
if (src >= max || src < -max) {
/*
* Now we need to divide src by 16 or 32, according to the OPERAND size, which means shifting it right
* by either 4 or 5 bits. That gives us a short or long INDEX, which we then multiply by the OPERAND size
* to obtain to the corresponding short or long OFFSET that we must add to the original EA offset.
*/
let i = src >> (this.sizeData == 2? 4 : 5);
dst = this.getEAWord(this.segEA, this.offEA + i * this.sizeData);
}
/*
* Now we convert src from a bit index to a bit mask.
*/
src = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & src) this.setCF(); else this.clearCF();
this.nStepCycles -= 8;
return dst ^ src;
};
/**
* fnBTRMem(dst, src)
*
* In this form of BTR, src is a register operand, which is NOT truncated if dst is a memory operand;
* however, if dst is also a register operand, then we defer to the simpler function, fnBTR().
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTRMem = function(dst, src)
{
if (this.regEA === X86.ADDR_INVALID) {
return X86.fnBTR.call(this, dst, src);
}
/*
* src is usually positive BUT can also be negative (as the IA32 spec says: "The offset operand then selects
* a bit position within the range −231 to 231 − 1 for a register offset and 0 to 31 for an immediate offset.")
*/
let max = this.sizeData << 3;
if (src >= max || src < -max) {
/*
* Now we need to divide src by 16 or 32, according to the OPERAND size, which means shifting it right
* by either 4 or 5 bits. That gives us a short or long INDEX, which we then multiply by the OPERAND size
* to obtain to the corresponding short or long OFFSET that we must add to the original EA offset.
*/
let i = src >> (this.sizeData == 2? 4 : 5);
dst = this.getEAWord(this.segEA, this.offEA + i * this.sizeData);
}
/*
* Now we convert src from a bit index to a bit mask.
*/
src = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & src) this.setCF(); else this.clearCF();
this.nStepCycles -= 8;
return dst & ~src;
};
/**
* fnBTSMem(dst, src)
*
* In this form of BTS, src is a register operand, which is NOT truncated if dst is a memory operand;
* however, if dst is also a register operand, then we defer to the simpler function, fnBTS().
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnBTSMem = function(dst, src)
{
if (this.regEA === X86.ADDR_INVALID) {
return X86.fnBTS.call(this, dst, src);
}
/*
* src is usually positive BUT can also be negative (as the IA32 spec says: "The offset operand then selects
* a bit position within the range −231 to 231 − 1 for a register offset and 0 to 31 for an immediate offset.")
*/
let max = this.sizeData << 3;
if (src >= max || src < -max) {
/*
* Now we need to divide src by 16 or 32, according to the OPERAND size, which means shifting it right
* by either 4 or 5 bits. That gives us a short or long INDEX, which we then multiply by the OPERAND size
* to obtain to the corresponding short or long OFFSET that we must add to the original EA offset.
*/
let i = src >> (this.sizeData == 2? 4 : 5);
dst = this.getEAWord(this.segEA, this.offEA + i * this.sizeData);
}
/*
* Now we convert src from a bit index to a bit mask.
*/
src = 1 << (src & (this.sizeData == 2? 0xf : 0x1f));
if (dst & src) this.setCF(); else this.clearCF();
this.nStepCycles -= 8;
return dst | src;
};
/**
* fnCALLw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src (null)
* @return {number}
*/
X86.fnCALLw = function(dst, src)
{
this.pushWord(this.getIP());
this.setIP(dst);
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesCallWR : this.cycleCounts.nOpCyclesCallWM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnCALLFdw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src (null)
* @return {number}
*/
X86.fnCALLFdw = function(dst, src)
{
if (this.regEA === X86.ADDR_INVALID) {
return X86.fnGRPUndefined.call(this, dst, src);
}
/*
* Originally, we would snapshot regLSP into opLSP because helpCALLF() could trigger a segment fault,
* but additionally, the stack segment could trigger either a segment fault or a page fault; indeed,
* any operation that performs multiple stack modifications must take this precaution and snapshot regLSP.
*/
this.opLSP = this.regLSP;
X86.helpCALLF.call(this, dst, this.getShort(this.regEA + this.sizeData));
this.nStepCycles -= this.cycleCounts.nOpCyclesCallDM;
this.opFlags |= X86.OPFLAG.NOWRITE;
this.opLSP = X86.ADDR_INVALID;
return dst;
};
/**
* fnCMPb(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number} dst unchanged
*/
X86.fnCMPb = function(dst, src)
{
let b = (dst - src)|0;
this.setArithResult(dst, src, b, X86.RESULT.BYTE | X86.RESULT.ALL, true);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesCompareRM) : this.cycleCounts.nOpCyclesArithRM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnCMPw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number} dst unchanged
*/
X86.fnCMPw = function(dst, src)
{
let w = (dst - src)|0;
this.setArithResult(dst, src, w, this.typeData | X86.RESULT.ALL, true);
this.nStepCycles -= (this.regEAWrite === X86.ADDR_INVALID? (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesArithRR : this.cycleCounts.nOpCyclesCompareRM) : this.cycleCounts.nOpCyclesArithRM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnDECb(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src (null)
* @return {number}
*/
X86.fnDECb = function(dst, src)
{
let b = (dst - 1)|0;
this.setArithResult(dst, 1, b, X86.RESULT.BYTE | X86.RESULT.NOTCF, true);
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesIncR : this.cycleCounts.nOpCyclesIncM);
return b & 0xff;
};
/**
* fnDECw(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src (null)
* @return {number}
*/
X86.fnDECw = function(dst, src)
{
let w = (dst - 1)|0;
this.setArithResult(dst, 1, w, this.typeData | X86.RESULT.NOTCF, true);
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesIncR : this.cycleCounts.nOpCyclesIncM);
return w & this.maskData;
};
/**
* fnDIVb(dst, src)
*
* @this {CPUx86}
* @param {number} dst (the divisor)
* @param {number} src (null; AX is the implied src)
* @return {number} (we return dst unchanged, since it's actually AX that's modified)
*/
X86.fnDIVb = function(dst, src)
{
/*
* Detect zero divisor
*/
if (!dst) {
X86.helpDIVOverflow.call(this);
return dst;
}
/*
* Detect too-small divisor (quotient overflow)
*/
let result = ((src = this.regEAX & 0xffff) / dst);
if (result > 0xff) {
X86.helpDIVOverflow.call(this);
return dst;
}
this.regMDLo = (result & 0xff) | (((src % dst) & 0xff) << 8);
this.fMDSet = true;
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesDivBR : this.cycleCounts.nOpCyclesDivBM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnDIVw(dst, src)
*
* @this {CPUx86}
* @param {number} dst (the divisor)
* @param {number} src (null; DX:AX or EDX:EAX is the implied src)
* @return {number} (we return dst unchanged, since it's actually DX:AX that's modified)
*/
X86.fnDIVw = function(dst, src)
{
if (this.sizeData == 2) {
/*
* Detect zero divisor
*/
if (!dst) {
X86.helpDIVOverflow.call(this);
return dst;
}
/*
* Detect too-small divisor (quotient overflow)
*
* WARNING: We CANNOT simply do "src = (this.regEDX << 16) | this.regEAX", because if bit 15 of DX
* is set, JavaScript will create a negative 32-bit number. So we instead use non-bitwise operators
* to force JavaScript to create a floating-point value that won't suffer from 32-bit-math side-effects.
*/
src = (this.regEDX & 0xffff) * 0x10000 + (this.regEAX & 0xffff);
let result = (src / dst);
if (result >= 0x10000) {
X86.helpDIVOverflow.call(this);
return dst;
}
this.regMDLo = (result & 0xffff);
this.regMDHi = (src % dst) & 0xffff;
}
else {
if (!X86.helpDIV32.call(this, this.regEAX, this.regEDX, dst)) {
X86.helpDIVOverflow.call(this);
return dst;
}
this.regMDLo |= 0;
this.regMDHi |= 0;
}
this.fMDSet = true;
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesDivWR : this.cycleCounts.nOpCyclesDivWM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnESC(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number} dst unchanged
*/
X86.fnESC = function(dst, src)
{
if (this.fpuActive) {
this.fpuActive.opFPU(this.bOpcode, this.bModRM, dst, src);
}
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? 2 : 8);
return dst;
};
/**
* fnGRPFault(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnGRPFault = function(dst, src)
{
/*
* This should NEVER be called on 8086/8088 CPUs, and yet we preset some of the handlers in aOpGrpPOPw,
* aOpGrp4b, and aOpGrp4w to call it. initProcessor() DOES patch aOpGrp4b[0x07] and aOpGrp4w[0x07] to
* fnGRPInvalid, but that's it.
*
* However, given the infrequency of this call, it's simpler to continue presetting all the handlers in
* aOpGrpPOPw to their post-8086 default, and deal with the appropriate 8086 behavior here (which for now,
* is to call fnGRPUndefined instead).
*/
if (this.model < X86.MODEL_80186) {
return X86.fnGRPUndefined.call(this, dst, src);
}
X86.helpFault.call(this, X86.EXCEPTION.GP_FAULT, 0);
return dst;
};
/**
* fnGRPInvalid(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnGRPInvalid = function(dst, src)
{
X86.opInvalid.call(this);
return dst;
};
/**
* fnGRPUndefined(dst, src)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnGRPUndefined = function(dst, src)
{
X86.opUndefined.call(this);
return dst;
};
/**
* fnIDIVb(dst, src)
*
* @this {CPUx86}
* @param {number} dst (the divisor)
* @param {number} src (null; AX is the implied src)
* @return {number} (we return dst unchanged, since it's actually AX that's modified)
*/
X86.fnIDIVb = function(dst, src)
{
/*
* Detect zero divisor
*/
if (!dst) {
X86.helpDIVOverflow.call(this);
return dst;
}
/*
* Detect too-small divisor (quotient overflow)
*/
let div = ((dst << 24) >> 24);
let result = ((src = (this.regEAX << 16) >> 16) / div)|0;
/*
* Note the following difference, from "AP-186: Introduction to the 80186 Microprocessor, March 1983":
*
* "The 8086 will cause a divide error whenever the absolute value of the quotient is greater then 7FFFH
* (for word operations) or if the absolute value of the quotient is greater than 7FH (for byte operations).
* The 80186 has expanded the range of negative numbers allowed as a quotient by 1 to include 8000H and 80H.
* These numbers represent the most negative numbers representable using 2's complement arithmetic (equaling
* -32768 and -128 in decimal, respectively)."
*/
if (result != ((result << 24) >> 24) || this.model == X86.MODEL_8086 && result == -128) {
X86.helpDIVOverflow.call(this);
return dst;
}
this.regMDLo = (result & 0xff) | (((src % div) & 0xff) << 8);
this.fMDSet = true;
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesIDivBR : this.cycleCounts.nOpCyclesIDivBM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnIDIVw(dst, src)
*
* @this {CPUx86}
* @param {number} dst (the divisor)
* @param {number} src (null; DX:AX or EDX:EAX is the implied src)
* @return {number} (we return dst unchanged, since it's actually DX:AX that's modified)
*/
X86.fnIDIVw = function(dst, src)
{
if (this.sizeData == 2) {
/*
* Detect zero divisor
*/
if (!dst) {
X86.helpDIVOverflow.call(this);
return dst;
}
/*
* Detect too-small divisor (quotient overflow)
*/
let div = ((dst << 16) >> 16);
let result = ((src = (this.regEDX << 16) | (this.regEAX & 0xffff)) / div)|0;
/*
* Note the following difference, from "AP-186: Introduction to the 80186 Microprocessor, March 1983":
*
* "The 8086 will cause a divide error whenever the absolute value of the quotient is greater then 7FFFH
* (for word operations) or if the absolute value of the quotient is greater than 7FH (for byte operations).
* The 80186 has expanded the range of negative numbers allowed as a quotient by 1 to include 8000H and 80H.
* These numbers represent the most negative numbers representable using 2's complement arithmetic (equaling
* -32768 and -128 in decimal, respectively)."
*/
if (result != ((result << 16) >> 16) || this.model == X86.MODEL_8086 && result == -32768) {
X86.helpDIVOverflow.call(this);
return dst;
}
this.regMDLo = (result & 0xffff);
this.regMDHi = (src % div) & 0xffff;
}
else {
if (!X86.helpIDIV32.call(this, this.regEAX, this.regEDX, dst)) {
X86.helpDIVOverflow.call(this);
return dst;
}
this.regMDLo |= 0;
this.regMDHi |= 0;
}
this.fMDSet = true;
this.nStepCycles -= (this.regEA === X86.ADDR_INVALID? this.cycleCounts.nOpCyclesIDivWR : this.cycleCounts.nOpCyclesIDivWM);
this.opFlags |= X86.OPFLAG.NOWRITE;
return dst;
};
/**
* fnIMUL8(dst, src)
*
* 80286_and_80287_Programmers_Reference_Manual_1987.pdf, p.B-44 (p.254) notes that:
*
* "The low 16 bits of the product of a 16-bit signed multiply are the same as those of an
* unsigned multiply. The three operand IMUL instruction can be used for unsigned operands as well."
*
* However, we still sign-extend the operands before multiplying, making it easier to range-check the result.
*
* (80186/80188 and up)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src
* @return {number}
*/
X86.fnIMUL8 = function(dst, src)
{
/*
* NOTE: getIPDisp() already sign-extends the dst parameter, so fnIMULrw() needlessly sign-extends it again;
* a small price to pay for a common function.
*/
let result = X86.fnIMULrw.call(this, this.getIPDisp(), src);
/*
* NOTE: The above function already accounted for the 80386 cycle count, so we are simply accounting for the
* increased time on an 80286; the 80186/80188 have even larger values, but we'll worry about that another day.
*/
if (this.model < X86.MODEL_80386) this.nStepCycles -= 12;
return result;
};
/**
* fnIMULn(dst, src)
*
* 80286_and_80287_Programmers_Reference_Manual_1987.pdf, p.B-44 (p.254) notes that:
*
* "The low 16 bits of the product of a 16-bit signed multiply are the same as those of an
* unsigned multiply. The three operand IMUL instruction can be used for unsigned operands as well."
*
* However, we still sign-extend the operands before multiplying, making it easier to range-check the result.
*
* (80186/80188 and up)
*
* @this {CPUx86}
* @param {number} dst (not used)
* @param {number} src
* @return {number}
*/
X86.fnIMULn = function(dst, src)
{
let result;
dst = this.getIPWord();
if (this.sizeData == 2) {
result = X86.fnIMULrw.call(this, dst, src);
} else {
result = X86.fnIMULrd.call(this, dst, src);
}
/*
* NOTE: The above functions already accounted for 80386 cycle counts, so we are simply accounting for the
* increased time on an 80286; the 80186/80188 have even larger values, but we'll worry about that another day.
*/
if (this.model < X86.MODEL_80386) this.nStepCycles -= 12;
return result;
};
/**
* fnIMUL32(dst, src)
*
* This sets regMDHi:regMDLo to the 64-bit result of dst * src, both of which are treated as signed.
*
* @this {CPUx86}
* @param {number} dst (any 32-bit number, treated as signed)
* @param {number} src (any 32-bit number, treated as signed)
*/
X86.fnIMUL32 = function(dst, src)
{
let fNeg = false;
if (src < 0) {
src = -src|0;
fNeg = !fNeg;
}
if (dst < 0) {
dst = -dst|0;
fNeg = !fNeg;
}
X86.fnMUL32.call(this, dst, src);
if (fNeg) {
this.regMDLo = (~this.regMDLo + 1)|0;
this.regMDHi = (~this.regMDHi + (this.regMDLo? 0 : 1))|0;
}
};
/**
* fnIMULb(dst, src)
*
* This 16-bit multiplication must indicate when the upper 8 bits are simply a sign-extension of the
* lower 8 bits (carry clear) and when the upper 8 bits contain significant bits (carry set). The latter
* will occur whenever a positive result is > 127 (0x007f) and whenever a negative result is < -128
* (0xff80).
*
* Example 1: 16 * 4 = 64 (0x0040): carry is clear
* Example 2: 16 * 8 = 128 (0x0080): carry is set (the sign bit no longer fits in the lower 8 bits)
* Example 3: 16 * -8 (0xf8) = -128 (0xff80): carry is clear (the sign bit *still* fits in the lower 8 bits)
* Example 4: 16 * -16 (0xf0) = -256 (0xff00): carry is set (the sign bit no longer fits in the lower 8 bits)
*
* @this {CPUx86}
* @param {number} dst
* @param {number} src (null; AL is the implied src)
* @return {number} (we return dst unchanged, since it's actually AX that's modified)
*/
X86.fnIMULb = function(dst, src)
{
let result = (((this.regEAX << 24) >> 24) * ((dst << 24) >> 24))|0;
this.regMDLo = result & 0xffff;
if (result > 127 || result < -128) {
this.setCF(); this.setOF();
} else {
this.clearCF(); this.clearOF();
}
if (this.model <= X86.MODEL_8088) {