This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathfpux86.js
3376 lines (3147 loc) · 94.7 KB
/
fpux86.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 FPU logic
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2019 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.
*
* FPU instruction description excerpts from the PC Magazine "Programmer's Technical Reference:
* The Processor and Coprocessor," Copyright 1992 by Ziff-Davis Press (ISBN 1-56276-016-5).
*/
"use strict";
if (typeof module !== "undefined") {
var Str = require("../../shared/lib/strlib");
var Web = require("../../shared/lib/weblib");
var Component = require("../../shared/lib/component");
var State = require("../../shared/lib/state");
var PCx86 = require("./defines");
var X86 = require("./x86");
}
/*
* Operand Type Reference
*
* ST(0), ST stack top; the register currently at the top of the stack
* ST(i) register in the stack i (0<=i<=7) stack elements from the top
* SR (short-real) short real (32 bits) number in memory; exponent bias is 127 (0x7f)
* LR (long-real) long real (64 bits) number in memory; exponent bias is 1023 (0x3ff)
* TR (temp-real) temporary real (80 bits) number in memory; exponent bias is 16383 (0x3fff)
* PD (packed-decimal) packed decimal integer (18 digits, 10 bytes) in memory
* WI (word-integer) word binary integer (16 bits) in memory
* SI (short-integer) short binary integer (32 bits) in memory
* LI (long-integer) long binary integer (64 bits) in memory
* NN (nn-bytes) memory area nn bytes long
*
* FPU Coprocessor Trivia
*
* Microsoft C 4.00 libraries executed software interrupts in the range 0x34-0x3B immediately after
* FPU operations, to assist with floating-point emulation when no coprocessor was present, since
* processors prior to the 80286 had no mechanism for generating a fault when an unsupported FPU
* instruction was executed.
*
* In short, INT 0x34 through INT 0x3B was used after ESC opcodes 0xD8 through 0xDF, INT 0x3C was
* used for FPU instructions containing a segment override, and INT 0x3D was used for FWAIT.
*
* A sample piece of code is available in x86ops.js, because it also highlights the Microsoft C 4.00
* library's dependency on the 8086/8088 behavior of "PUSH SP" (see the opPUSHSP_8086() function).
*/
/**
* @class FPUx86
* @unrestricted (allows the class to define properties, both dot and named, outside of the constructor)
*/
class FPUx86 extends Component {
/**
* FPUx86(parmsFPU)
*
* The FPUx86 class uses the following (parmsFPU) properties:
*
* model: a number (eg, 8087) that should match one of the X86.FPU.MODEL values (default is 8087)
* stepping: a string (eg, "B1") that should match one of the X86.FPU.STEPPING values (default is "")
*
* @this {FPUx86}
* @param {Object} parmsFPU
*/
constructor(parmsFPU)
{
super("FPU", parmsFPU);
this.model = this.parms['model'] || X86.FPU.MODEL_8087;
/*
* We take the 'stepping' value, convert it to a hex value, and then add that to the model to provide
* a single value that's unique for any given CPU stepping. If no stepping is provided, then stepping
* is equal to model.
*/
let stepping = this.parms['stepping'];
this.stepping = this.model + (stepping? Str.parseInt(stepping, 16) : 0);
/*
* Perform a one-time allocation of all floating-point registers.
* NOTE: The FPU's internal registers are supposed to be 80-bit, but JavaScript gives us only 64-bit floats.
*/
this.regStack = new Float64Array(8);
this.intStack = new Int32Array(this.regStack.buffer);
/*
* Used for "short-real" (SR) 32-bit floating-point operations.
*/
this.regTmpSR = new Float32Array(1);
this.intTmpSR = new Int32Array(this.regTmpSR.buffer);
/*
* Used for "long-real" (LR) 64-bit floating-point operations. We also use intTmpLR as temporary storage
* for all "word-integer" (WI or INT16), "short-integer" (SI or INT32) and "long-integer" (LI or INT64) values,
* since it's just large enough to accommodate all three integer sizes.
*/
this.regTmpLR = new Float64Array(1);
this.intTmpLR = new Int32Array(this.regTmpLR.buffer);
/*
* Used for conversion to/from the 80-bit "temp-real" (TR) format; used as three 32-bit integers,
* where [0] contains TR bits 0-31, [1] contains TR bits 32-63, and [2] contains TR bits 64-79; the
* upper 16 bits of [2] are not used and should remain zero.
*/
this.intTmpTR = new Array(3);
/*
* Initialize other (non-floating-point) coprocessor registers that resetFPU() doesn't touch,
* such as the "exception" registers: regCodeSel, regCodeOff, regDataSel, regDataOff, and regOpcode.
*
* Note that regCodeSel and regDataSel are NEVER set in real-mode and are ALWAYS set in protected-mode,
* so we set them to -1 in their "unset" state; if those values ever show up in an exception block,
* something may have gone amiss (it's not impossible though, because if an exception occurs before any
* memory operands have been used, regDataSel may still be "unset").
*
* NOTE: iStack is the low 3 bits of the bModRM byte, for instructions that have an explicit stack operand.
*/
this.regCodeSel = this.regDataSel = -1;
this.regCodeOff = this.regDataOff = this.regOpcode = this.iStack = 0;
/*
* Initialize special floating-point constants, as if they were internal read-only registers;
* all other simple (non-special) constants are "statically" initialized below, as class constants.
*/
this.regIndefinite = new Float64Array(1);
this.intIndefinite = new Int32Array(this.regIndefinite.buffer);
this.intIndefinite[0] = 0x00000000; this.intIndefinite[1] = 0xFFF8000;
/*
* Initialize all other coprocessor registers (control word, tag word, status word, etc) by resetting them.
*/
this.resetFPU();
/**
* setEAFromSR()
*
* Stores the (32-bit) "short-real" value in the internal regTmpSR register to the address in regEA.
*
* @this {FPUx86}
*/
this.setEAFromSR = FPUx86.prototype.setEAFromSI;
/**
* setEAFromLR()
*
* Stores the (64-bit) "long-real" value in the internal regTmpLR register to the address in regEA.
*
* @this {FPUx86}
*/
this.setEAFromLR = FPUx86.prototype.setEAFromLI;
}
/**
* initBus(cmp, bus, cpu, dbg)
*
* @this {FPUx86}
* @param {Computer} cmp
* @param {BusX86} bus
* @param {CPUx86} cpu
* @param {DebuggerX86} dbg
*/
initBus(cmp, bus, cpu, dbg)
{
this.cpu = cpu;
this.chipset = cmp.getMachineComponent("ChipSet");
this.setReady();
}
/**
* clearBusy()
*
* The ChipSet calls us whenever an I/O operation that clears the coprocessor's "busy" state is performed.
*
* @this {FPUx86}
*/
clearBusy()
{
/*
* We're never "busy" as far as other components are concerned, because we perform all FPU operations
* synchronously, so there's nothing to do here.
*/
}
/**
* powerUp(data, fRepower)
*
* @this {FPUx86}
* @param {Object|null} data
* @param {boolean} [fRepower]
* @return {boolean} true if successful, false if failure
*/
powerUp(data, fRepower)
{
if (!fRepower) {
if (!data || !this.restore) {
this.resetFPU();
} else {
if (!this.restore(data)) return false;
}
}
return true;
}
/**
* powerDown(fSave, fShutdown)
*
* @this {FPUx86}
* @param {boolean} [fSave]
* @param {boolean} [fShutdown]
* @return {Object|boolean} component state if fSave; otherwise, true if successful, false if failure
*/
powerDown(fSave, fShutdown)
{
return fSave? this.save() : true;
}
/**
* save()
*
* This implements save support for the FPUx86 component.
*
* @this {FPUx86}
* @return {Object}
*/
save()
{
let state = new State(this);
let a = [], i = 0;
a[i++] = this.regControl;
a[i++] = this.getStatus();
a[i++] = this.getTags();
/*
* Note that, unlike the FSAVE() and FRSTOR() operations, we save the registers in regStack in their physical
* order (0-7) rather than their logical order (ST0-ST7). Moreover, FSAVE() and FRSTOR() use the "temp-real" (TR)
* format, whereas we use the current native format -- which, sadly, is only a 64-bit "long-real" (LR) format.
*/
for (let iReg = 0; iReg < this.regStack.length; iReg++) {
a[i++] = this.regStack[iReg];
}
state.set(0, a);
return state.data();
}
/**
* restore(data)
*
* This implements restore support for the FPUx86 component.
*
* @this {FPUx86}
* @param {Object} data
* @return {boolean} true if successful, false if failure
*/
restore(data)
{
let a = data[0], i = 0;
this.setControl(a[i++]);
this.setStatus(a[i++]);
this.setTags(a[i++]);
for (let iReg = 0; iReg < this.regStack.length; iReg++) {
this.regStack[iReg] = a[i++];
}
return true;
}
/**
* resetFPU()
*
* Aside from calling this internally (eg, during initialization and FINIT operations), the ChipSet may also call
* us whenever an I/O operation that resets the coprocessor is performed. Only 80487 coprocessors and higher will
* also clear the "exception" registers, but the 80487 is currently beyond my planned level of support.
*
* TODO: Add support for X86.FPU.CONTROL.PC (Precision Control) and X86.FPU.CONTROL.IC (Infinity Control)
*
* @this {FPUx86}
*/
resetFPU()
{
this.regUsed = 0; // bits 0-7 are set as regs 0-7 are used
this.regControl = X86.FPU.CONTROL.INIT;
this.regStatus = 0; // contains all status register bits EXCEPT for ST
this.iST = 0; // the ST bits for regStatus are actually stored here
if (DEBUG) {
/*
* All the registers were tagged "unused" above, which is all that would normally happen, but debugging is
* a little easier if we zero all the registers as well.
*/
for (let iReg = 0; iReg < this.regStack.length; iReg++) {
this.regStack[iReg] = 0.0;
}
}
if (this.chipset) this.chipset.clearFPUInterrupt();
}
/**
* isModel(model)
*
* If the current model is equal to the specified model, then it's assumed the current operation
* is supported, and we return true.
*
* @this {FPUx86}
* @param {number} model
* @return {boolean}
*/
isModel(model)
{
return this.model == model;
}
/**
* isAtLeastModel(model)
*
* If the current model is greater than or equal to the specified model, then it's assumed that the
* current operation is supported, and we return true.
*
* @this {FPUx86}
* @param {number} model
* @return {boolean}
*/
isAtLeastModel(model)
{
return this.model >= model;
}
/**
* opStop(fError)
*
* Place this inside any opcode handler to stop the CPU from running the current instruction; eg:
*
* if (this.opStop()) return;
*
* You can still use the Debugger to single-step over the instruction; opStop() will return false in that case.
*
* @this {FPUx86}
* @param {boolean} [fError]
* @return {boolean} (true if there was an error or the CPU was running, false if not)
*/
opStop(fError)
{
if (DEBUG) {
let cpu = this.cpu;
if (fError || cpu.isRunning()) {
cpu.setIP(cpu.opLIP - cpu.segCS.base);
cpu.stopCPU();
return true;
}
}
return false;
}
/**
* opNone()
*
* Used for any coprocessor opcode that has no known operation for the given model.
*
* @this {FPUx86}
*/
opNone()
{
if (DEBUG) this.println(this.idComponent + ".opNone(" + Str.toHexByte(this.cpu.bOpcode) + "," + Str.toHexByte(this.cpu.bModRM) + ")");
this.opStop(true);
}
/**
* opObsolete()
*
* Used for any coprocessor opcodes that are redundant and potentially obsolete.
*
* @this {FPUx86}
*/
opObsolete()
{
if (DEBUG) this.println(this.idComponent + ".opObsolete(" + Str.toHexByte(this.cpu.bOpcode) + "," + Str.toHexByte(this.cpu.bModRM) + ")");
this.opStop(true);
}
/**
* opUnimplemented()
*
* Used for any coprocessor opcode that DOES have a known operation, we just haven't implemented it yet.
*
* @this {FPUx86}
*/
opUnimplemented()
{
if (DEBUG) this.println(this.idComponent + ".opUnimplemented(" + Str.toHexByte(this.cpu.bOpcode) + "," + Str.toHexByte(this.cpu.bModRM) + ")");
this.opStop(true);
}
/**
* checkException()
*
* @this {FPUx86}
* @return {boolean} (true if unmasked exception exists, false if not)
*/
checkException()
{
this.regStatus &= ~X86.FPU.STATUS.ES;
/*
* NOTE: The "Stack Fault" (SF) status bit wasn't introduced until the 80387, so it triggers the pre-existing
* "Invalid Operation" (IE) exception; there is no corresponding "Stack Fault" (SE) exception, and the matching
* control bit is still reserved. Consequently, X86.FPU.CONTROL.EXC is a *subset* of X86.FPU.STATUS.EXC (0x3F
* instead of 0x7F).
*
* However, we shouldn't have to do anything special when SF is set, because any setException() call that sets
* SF should ALSO set IE.
*/
if (this.regStatus & (~this.regControl & X86.FPU.CONTROL.EXC)) {
this.regStatus |= X86.FPU.STATUS.ES; // set ES whenever one or more unmasked EXC bits are set
}
if ((this.regStatus & X86.FPU.STATUS.ES) && !(this.regControl & X86.FPU.CONTROL.IEM)) {
this.chipset.setFPUInterrupt();
return true;
}
this.chipset.clearFPUInterrupt();
return false;
}
/**
* setException(n)
*
* Sets one or more of the FPU.STATUS.ECX bits; ie:
*
* IE (0x0001 bit 0: Invalid Operation)
* DE (0x0002 bit 1: Denormalized Operand)
* ZE (0x0004 bit 2: Zero Divide)
* OE (0x0008 bit 3: Overflow)
* UE (0x0010 bit 4: Underflow)
* PE (0x0020 bit 5: Precision)
* SF (0x0040 bit 6: Stack Fault; 80387 and later)
*
* Also, as noted in checkException(), any time you set the SF bit, you should also set the IE bit, because
* Stack Fault is a subset of Invalid Operation. TODO: We should include a test for that in the assertion below.
*
* @this {FPUx86}
* @param {number} n (one or more of the above error status bits)
* @return {boolean} (true if unmasked exception exists, false if not)
*/
setException(n)
{
if (DEBUG) this.println(this.idComponent + ".setException(" + Str.toHexWord(n) + ")");
if (!this.isAtLeastModel(X86.FPU.MODEL_80387)) {
n &= ~X86.FPU.STATUS.SF; // the SF bit didn't exist on pre-80387 coprocessors
}
this.assert(!(n & ~X86.FPU.STATUS.EXC)); // make sure the caller isn't setting any non-EXC bits
this.regStatus |= n;
return this.checkException();
}
/**
* getControl()
*
* @this {FPUx86}
* @return {number}
*/
getControl()
{
return this.regControl;
}
/**
* setControl(n)
*
* NOTE: Be sure to use this function for all "wholesale" regControl updates, because it ensures that
* unused bits cannot be set -- including bit 6, which could otherwise inadvertently mask the SF error
* condition on 80387 and newer coprocessors.
*
* @this {FPUx86}
* @param {number} n
*/
setControl(n)
{
this.regControl = n & ~X86.FPU.CONTROL.UNUSED;
}
/**
* clearStatus(n)
*
* @this {FPUx86}
* @param {number} n
*/
clearStatus(n)
{
this.regStatus &= ~n;
this.checkException();
}
/**
* getStatus()
*
* @this {FPUx86}
* @return {number} regStatus merged with iST
*/
getStatus()
{
/*
* As long as we never store any ST bits in regStatus, they should always be zero, so in
* order to return the complete regStatus, all we need to do is shift and "or" the bits from iST.
*/
return this.regStatus | (this.iST << X86.FPU.STATUS.ST_SHIFT);
}
/**
* setStatus(n)
*
* NOTE: Be sure to use this function for all "wholesale" regStatus updates, because it ensures that
* the ST bits get propagated to the internal iST register. Setting individual EXC bits should be done
* through the fault() interface, and clearing individual EXC or BUSY bits should be done through
* clearStatus(). Both functions, including this function, call checkException() after updating regStatus.
*
* @this {FPUx86}
* @param {number} n
*/
setStatus(n)
{
this.regStatus = n & ~X86.FPU.STATUS.ST;
this.iST = (n & X86.FPU.STATUS.ST) >> X86.FPU.STATUS.ST_SHIFT;
this.checkException();
}
/**
* checkOperand(v)
*
* @this {FPUx86}
* @param {number|null} v
* @return {boolean} (true if no exception, false otherwise)
*/
checkOperand(v)
{
return isNaN(v)? !this.setException(X86.FPU.STATUS.IE) : true;
}
/**
* checkResult(v)
*
* @this {FPUx86}
* @param {number} v
* @return {boolean} (true if no exception, false otherwise)
*/
checkResult(v)
{
return !isFinite(v)? !this.setException(v === Infinity? X86.FPU.STATUS.OE : X86.FPU.STATUS.UE) : true;
}
/**
* doAdd(operand1, operand2)
*
* @this {FPUx86}
* @param {number|null} operand1
* @param {number|null} operand2
* @return {number|null}
*/
doAdd(operand1, operand2)
{
let result = null;
if (operand1 != null && operand2 != null) {
result = operand1 + operand2;
if (!this.checkResult(result)) result = null;
}
return result;
}
/**
* doSubtract(operand1, operand2)
*
* @this {FPUx86}
* @param {number|null} operand1
* @param {number|null} operand2
* @return {number|null}
*/
doSubtract(operand1, operand2)
{
let result = null;
if (operand1 != null && operand2 != null) {
result = operand1 - operand2;
if (!this.checkResult(result)) result = null;
}
return result;
}
/**
* doMultiply(operand1, operand2)
*
* @this {FPUx86}
* @param {number|null} operand1
* @param {number|null} operand2
* @return {number|null}
*/
doMultiply(operand1, operand2)
{
let result = null;
if (operand1 != null && operand2 != null) {
result = operand1 * operand2;
if (!this.checkResult(result)) result = null;
}
return result;
}
/**
* doDivide(dividend, divisor)
*
* TODO: IE exceptions: infinity / infinity, 0 / 0, 0 / pseudo-zero, or divisor is denormal or unnormal.
*
* @this {FPUx86}
* @param {number|null} dividend
* @param {number|null} divisor
* @return {number|null}
*/
doDivide(dividend, divisor)
{
let quotient = null;
if (dividend != null && divisor != null) {
if (divisor || !this.setException(X86.FPU.STATUS.DE)) {
quotient = dividend / divisor;
if (!this.checkResult(quotient)) quotient = null;
}
}
return quotient;
}
/**
* doCompare(operand1, operand2)
*
* @this {FPUx86}
* @param {number|null} operand1
* @param {number|null} operand2
* @return {boolean}
*/
doCompare(operand1, operand2)
{
if (operand1 != null && operand2 != null) {
let cc = 0; // default value used when result > 0
if (!isNaN(operand1) && !isNaN(operand2)) {
let result = operand1 - operand2;
if (result < 0) {
cc = X86.FPU.STATUS.C0;
} else if (result === 0) {
cc = X86.FPU.STATUS.C3;
}
} else {
cc = X86.FPU.STATUS.C0 | X86.FPU.STATUS.C2 | X86.FPU.STATUS.C3;
}
this.regStatus = (this.regStatus & ~X86.FPU.STATUS.CC) | cc;
return true;
}
return false;
}
/**
* doSquareRoot(operand)
*
* @this {FPUx86}
* @param {number|null} operand
* @return {number|null}
*/
doSquareRoot(operand)
{
let result = null;
/*
* Happily, -0 is ALSO >= 0. Also happily, Math.sqrt(-0) returns -0.
*/
if (operand >= 0 || !this.setException(X86.FPU.STATUS.IE)) {
result = Math.sqrt(operand);
if (!this.checkResult(result)) result = null;
}
return result;
}
/**
* roundValue(operand, max)
*
* NOTE: The max parameter is EXCLUSIVE, not inclusive (ie, the maximum positive value is < max).
*
* Also, callers that expect intTmpLR[] to be loaded with the result *must* also specify a max parameter;
* callers performing internal rounding and using just the return value may omit max to skip loading intTmpLR[].
*
* @this {FPUx86}
* @param {number|null} operand
* @param {number} [max] (ie, 0x8000, 0x80000000, or 0x8000000000000000)
* @return {number|null} (rounded result, or null if there was an unmasked exception)
*/
roundValue(operand, max)
{
if (operand == null) return null;
let rc = (this.regControl & X86.FPU.CONTROL.RC.MASK), result;
if (rc == X86.FPU.CONTROL.RC.NEAR) {
result = Math.round(operand);
if (result - operand === 0.5 && (result % 2)) result--;
}
else if (rc == X86.FPU.CONTROL.RC.DOWN || rc == X86.FPU.CONTROL.RC.CHOP && operand > 0) {
result = Math.floor(operand);
}
else { // X86.FPU.CONTROL.RC.UP or X86.FPU.CONTROL.RC.CHOP && operand <= 0
result = Math.ceil(operand);
}
if (max) {
if (result >= max) {
if (this.setException(X86.FPU.STATUS.IE)) return null;
result = -max; // apparently, the masked response is to return the most negative integer (not max - 1)
}
else if (result < -max) {
if (this.setException(X86.FPU.STATUS.IE)) return null;
result = -max;
}
this.intTmpLR[0] = result|0;
if (max > FPUx86.MAX_INT32) {
this.intTmpLR[1] = (result / 0x100000000)|0;
if (!this.intTmpLR[1] && result < 0) this.intTmpLR[1] = -1;
}
}
return result;
}
/**
* truncateValue(v)
*
* @this {FPUx86}
* @param {number} v
* @return {number}
*/
truncateValue(v)
{
return v > 0? Math.floor(v) : Math.ceil(v);
}
/**
* getTag(iReg)
*
* @this {FPUx86}
* @param {number} iReg (register index)
* @return {number} tag value for register
*/
getTag(iReg)
{
let bitUsed = (1 << iReg);
let tag = X86.FPU.TAGS.EMPTY;
if (this.regUsed & bitUsed) {
let f = this.regStack[iReg];
tag = X86.FPU.TAGS.VALID;
if (f === 0.0) {
tag = X86.FPU.TAGS.ZERO;
}
else if (!isFinite(f)) {
tag = X86.FPU.TAGS.SPECIAL;
}
}
return tag;
}
/**
* getTags()
*
* @this {FPUx86}
* @return {number} tag values for all registers
*/
getTags()
{
let tags = 0;
for (let iReg = this.regStack.length - 1; iReg >= 0; iReg--) {
tags <<= 2;
tags |= this.getTag(iReg);
}
return tags;
}
/**
* setTag(iReg, tag)
*
* @this {FPUx86}
* @param {number} iReg (register index)
* @param {number} tag value for register (EMPTY is the only supported value)
*/
setTag(iReg, tag)
{
this.assert(!(iReg & ~0x7) && tag == X86.FPU.TAGS.EMPTY);
this.regUsed &= ~(1 << iReg);
}
/**
* setTags(n)
*
* All we need to update here are which physical registers are marked "empty"; the rest of the tags
* are generated on the fly based on actual values in the registers.
*
* @this {FPUx86}
* @param {number} n (16-bit tag word, containing 8 2-bit tags)
*/
setTags(n)
{
this.regUsed = 0;
for (let bitUsed = 0x1; bitUsed <= 0x80; bitUsed <<= 1) {
let tag = n & X86.FPU.TAGS.MASK;
if (tag != X86.FPU.TAGS.EMPTY) {
this.regUsed |= bitUsed;
}
n >>= 2;
}
}
/**
* getWI(i)
*
* Gets a "word-integer" (WI aka INT16) from ST(i)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {boolean} true if intTmpLR was loaded, false if not
*/
getWI(i)
{
return this.roundValue(this.getST(i), FPUx86.MAX_INT16) != null;
}
/**
* getSI(i)
*
* Gets a "short-integer" (SI aka INT32) from ST(i)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {boolean} true if intTmpLR was loaded, false if not
*/
getSI(i)
{
return this.roundValue(this.getST(i), FPUx86.MAX_INT32) != null;
}
/**
* getLI(i)
*
* Gets a "long-integer" (LI aka INT64) from ST(i)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {boolean} true if intTmpLR was loaded, false if not
*/
getLI(i)
{
return this.roundValue(this.getST(i), FPUx86.MAX_INT64) != null;
}
/**
* getSR(i)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {boolean} true if regTmpSR was loaded, false if not
*/
getSR(i)
{
let iReg = (this.iST + i) & 7;
if (this.regUsed & (1 << iReg)) {
this.regTmpSR[0] = this.regStack[iReg];
return true;
} else if (!this.setException(X86.FPU.STATUS.IE)) {
this.regTmpSR[0] = this.regIndefinite[0];
return true;
}
return false;
}
/**
* getLR(i)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {boolean} true if regTmpLR was loaded, false if not
*/
getLR(i)
{
let iReg = (this.iST + i) & 7;
if (this.regUsed & (1 << iReg)) {
this.regTmpLR[0] = this.regStack[iReg];
return true;
} else if (!this.setException(X86.FPU.STATUS.IE)) {
this.regTmpLR[0] = this.regIndefinite[0];
return true;
}
return false;
}
/**
* getST(i)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {number|null} v
*/
getST(i)
{
let v = null;
let iReg = (this.iST + i) & 7;
if (this.regUsed & (1 << iReg)) {
v = this.regStack[iReg];
} else if (!this.setException(X86.FPU.STATUS.IE)) {
v = this.regIndefinite[0];
}
return v;
}
/**
* getSTSign(i)
*
* Returns zero if sign bit clear, and non-zero (negative) if sign bit set. This is safer
* than comparing getST() to zero, because JavaScript comparisons involving NaNs are meaningless.
*
* For internal use only; ignores whether the register is empty, and performs no exception checks.
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @return {number}
*/
getSTSign(i)
{
let iInt = ((this.iST + i) & 7) << 1;
return this.intStack[iInt + 1] & (0x80000000|0);
}
/**
* setST(i, v)
*
* @this {FPUx86}
* @param {number} i (eg, 0 for top-of-stack)
* @param {number|null} v
* @return {boolean}
*/
setST(i, v)
{
if (v != null && this.checkOperand(v)) {
let iReg = (this.iST + i) & 7;
this.regStack[iReg] = v;
this.regUsed |= (1 << iReg);
return true;
}
return false;
}
/**
* getTR(i, fSafe)
*
* @this {FPUx86}
* @param {number} i (stack index, 0-7)
* @param {boolean} [fSafe] (true to ignore all exception criteria; used by FSAVE)
* @return {Array.<number>|null} ("temp-real" aka TR, as an array of three 32-bit integers)
*/
getTR(i, fSafe)
{
let a = null;
let iReg = (this.iST + i) & 7;
if (fSafe || this.regUsed & (1 << iReg) || !this.setException(X86.FPU.STATUS.IE)) {
let iInt = iReg << 1;
a = this.getTRFromLR(this.intStack[iInt], this.intStack[iInt + 1]);
}
return a;
}
/**
* setTR(i, a)
*
* Sets ST(i) to the TR ("long-real") in a[].
*
* @this {FPUx86}
* @param {number} i (stack index, 0-7)
* @param {Array.<number>|null} a
*/
setTR(i, a)
{
if (a) this.setST(i, this.getLRFromTR(a));
}
/**
* getWIFromEA()
*
* Returns the (16-bit) "word-integer" value located at regEA.
*
* @this {FPUx86}
* @return {number} v