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
|
/*
* this doesn't attempt to implement Power architecture floating-point properties
* that aren't visible in the Inferno environment.
* all arithmetic is done in double precision.
* the FP trap status isn't updated.
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "ureg.h"
#include "fpi.h"
#define REG(x) (*(long*)(((char*)em->ur)+roff[(x)]))
#define FR(x) (*(Internal*)em->fr[(x)&0x1F])
#define REGSP 1 /* stack pointer */
/* BUG: check fetch (not worthwhile in Inferno) */
#define getulong(a) (*(ulong*)(a))
enum {
CRLT = 1<<31,
CRGT = 1<<30,
CREQ = 1<<29,
CRSO = 1<<28,
CRFU = CRSO,
CRFX = 1<<27,
CRFEX = 1<<26,
CRVX = 1<<25,
CROX = 1<<24,
};
#define getCR(x,w) (((w)>>(28-(x*4)))&0xF)
#define mkCR(x,v) (((v)&0xF)<<(28-(x*4)))
#define simm(xx, ii) xx = (short)(ii&0xFFFF);
#define getairr(i) rd = (i>>21)&0x1f; ra = (i>>16)&0x1f; simm(imm,i)
#define getarrr(i) rd = (i>>21)&0x1f; ra = (i>>16)&0x1f; rb = (i>>11)&0x1f;
#define getop(i) ((i>>26)&0x3F)
#define getxo(i) ((i>>1)&0x3FF)
#define FPS_FX (1<<31) /* exception summary (sticky) */
#define FPS_EX (1<<30) /* enabled exception summary */
#define FPS_VX (1<<29) /* invalid operation exception summary */
#define FPS_OX (1<<28) /* overflow exception OX (sticky) */
#define FPS_UX (1<<27) /* underflow exception UX (sticky) */
#define FPS_ZX (1<<26) /* zero divide exception ZX (sticky) */
#define FPS_XX (1<<25) /* inexact exception XX (sticky) */
#define FPS_VXSNAN (1<<24) /* invalid operation exception for SNaN (sticky) */
#define FPS_VXISI (1<<23) /* invalid operation exception for ∞-∞ (sticky) */
#define FPS_VXIDI (1<<22) /* invalid operation exception for ∞/∞ (sticky) */
#define FPS_VXZDZ (1<<21) /* invalid operation exception for 0/0 (sticky) */
#define FPS_VXIMZ (1<<20) /* invalid operation exception for ∞*0 (sticky) */
#define FPS_VXVC (1<<19) /* invalid operation exception for invalid compare (sticky) */
#define FPS_FR (1<<18) /* fraction rounded */
#define FPS_FI (1<<17) /* fraction inexact */
#define FPS_FPRF (1<<16) /* floating point result class */
#define FPS_FPCC (0xF<<12) /* <, >, =, unordered */
#define FPS_VXCVI (1<<8) /* enable exception for invalid integer convert (sticky) */
#define FPS_VE (1<<7) /* invalid operation exception enable */
#define FPS_OE (1<<6) /* enable overflow exceptions */
#define FPS_UE (1<<5) /* enable underflow */
#define FPS_ZE (1<<4) /* enable zero divide */
#define FPS_XE (1<<3) /* enable inexact exceptions */
#define FPS_RN (3<<0) /* rounding mode */
typedef struct Emreg Emreg;
struct Emreg {
Ureg* ur;
ulong (*fr)[3];
FPenv* ufp;
ulong ir;
char* name;
};
int fpemudebug = 0;
#undef OFR
#define OFR(X) ((ulong)&((Ureg*)0)->X)
static int roff[] = {
OFR(r0), OFR(r1), OFR(r2), OFR(r3),
OFR(r4), OFR(r5), OFR(r6), OFR(r7),
OFR(r8), OFR(r9), OFR(r10), OFR(r11),
OFR(r12), OFR(r13), OFR(r14), OFR(r15),
OFR(r16), OFR(r17), OFR(r18), OFR(r19),
OFR(r20), OFR(r21), OFR(r22), OFR(r23),
OFR(r24), OFR(r25), OFR(r26), OFR(r27),
OFR(r28), OFR(r29), OFR(r30), OFR(r31),
};
/*
* initial FP register values assumed by qc's code
*/
static Internal fpreginit[] = {
/* s, e, l, h */
{0, 0x400, 0x00000000, 0x08000000}, /* F31=2.0 */
{0, 0x3FF, 0x00000000, 0x08000000}, /* F30=1.0 */
{0, 0x3FE, 0x00000000, 0x08000000}, /* F29=0.5 */
{0, 0x1, 0x00000000, 0x00000000}, /* F28=0.0 */
{0, 0x433, 0x00000000, 0x08000040}, /* F27=FREGCVI */
};
static void
fadd(Emreg *em, Internal *d, int ra, int rb)
{
Internal a, b;
a = FR(ra);
b = FR(rb);
(a.s == b.s? fpiadd: fpisub)(&b, &a, d);
}
static void
fsub(Emreg *em, Internal *d, int ra, int rb)
{
Internal a, b;
a = FR(ra);
b = FR(rb);
b.s ^= 1;
(b.s == a.s? fpiadd: fpisub)(&b, &a, d);
}
static void
fmul(Emreg *em, Internal *d, int ra, int rb)
{
Internal a, b;
a = FR(ra);
b = FR(rb);
fpimul(&b, &a, d);
}
static void
fdiv(Emreg *em, Internal *d, int ra, int rb)
{
Internal a, b;
a = FR(ra);
b = FR(rb);
fpidiv(&b, &a, d);
}
static void
fmsub(Emreg *em, Internal *d, int ra, int rc, int rb)
{
Internal a, c, b, t;
a = FR(ra);
c = FR(rc);
b = FR(rb);
fpimul(&a, &c, &t);
b.s ^= 1;
(b.s == t.s? fpiadd: fpisub)(&b, &t, d);
}
static void
fmadd(Emreg *em, Internal *d, int ra, int rc, int rb)
{
Internal a, c, b, t;
a = FR(ra);
c = FR(rc);
b = FR(rb);
fpimul(&a, &c, &t);
(t.s == b.s? fpiadd: fpisub)(&b, &t, d);
}
static ulong setfpscr(Emreg*);
static void setfpcc(Emreg*, int);
static void
unimp(Emreg *em, ulong op)
{
char buf[60];
snprint(buf, sizeof(buf), "sys: fp: pc=%lux unimp fp 0x%.8lux", em->ur->pc, op);
if(fpemudebug)
print("FPE: %s\n", buf);
error(buf);
/* no return */
}
/*
* floating load/store
*/
static void
fpeairr(Emreg *em, ulong ir, void **eap, int *rdp)
{
ulong ea;
long imm;
int ra, rd, upd;
getairr(ir);
ea = imm;
upd = (ir&(1L<<26))!=0;
if(ra) {
ea += REG(ra);
if(upd){
if(ra == REGSP)
panic("fpemu: r1 update"); /* can't do it because we're running on the same stack */
REG(ra) = ea;
}
} else {
if(upd)
unimp(em, ir);
}
*rdp = rd;
*eap = (void*)ea;
if(fpemudebug)
print("%8.8lux %s\tf%d,%ld(r%d) ea=%lux upd=%d\n", em->ur->pc, em->name, rd, imm, ra, ea, upd);
}
static void
fpearrr(Emreg *em, ulong ir, int upd, void **eap, int *rdp)
{
ulong ea;
int ra, rb, rd;
getarrr(ir);
ea = REG(rb);
if(ra){
ea += REG(ra);
if(upd){
if(ra == REGSP)
panic("fpemu: r1 update");
REG(ra) = ea;
}
if(fpemudebug)
print("%8.8lux %s\tf%d,(r%d+r%d) ea=%lux upd=%d\n", em->ur->pc, em->name, rd, ra, rb, ea, upd);
} else {
if(upd)
unimp(em, ir);
if(fpemudebug)
print("%8.8lux %s\tf%d,(r%d) ea=%lux\n", em->ur->pc, em->name, rd, rb, ea);
}
*eap = (void*)ea;
*rdp = rd;
}
static void
lfs(Emreg *em, ulong ir)
{
void *ea;
int rd;
em->name = "lfs";
fpeairr(em, ir, &ea, &rd);
fpis2i(&FR(rd), (void*)ea);
}
static void
lfsx(Emreg *em, ulong ir)
{
void *ea;
int rd;
em->name = "lfsx";
fpearrr(em, ir, ((ir>>1)&0x3FF)==567, &ea, &rd);
fpis2i(&FR(rd), (void*)ea);
}
static void
lfd(Emreg *em, ulong ir)
{
void *ea;
int rd;
em->name = "lfd";
fpeairr(em, ir, &ea, &rd);
fpid2i(&FR(rd), (void*)ea);
}
static void
lfdx(Emreg *em, ulong ir)
{
void *ea;
int rd;
em->name = "lfdx";
fpearrr(em, ir, ((ir>>1)&0x3FF)==631, &ea, &rd);
fpid2i(&FR(rd), (void*)ea);
}
static void
stfs(Emreg *em, ulong ir)
{
void *ea;
int rd;
Internal tmp;
em->name = "stfs";
fpeairr(em, ir, &ea, &rd);
tmp = FR(rd);
fpii2s(ea, &tmp);
}
static void
stfsx(Emreg *em, ulong ir)
{
void *ea;
int rd;
Internal tmp;
em->name = "stfsx";
fpearrr(em, ir, getxo(ir)==695, &ea, &rd);
tmp = FR(rd);
fpii2s(ea, &tmp);
}
static void
stfd(Emreg *em, ulong ir)
{
void *ea;
int rd;
Internal tmp;
em->name = "stfd";
fpeairr(em, ir, &ea, &rd);
tmp = FR(rd);
fpii2d(ea, &tmp);
}
static void
stfdx(Emreg *em, ulong ir)
{
void *ea;
int rd;
Internal tmp;
em->name = "stfdx";
fpearrr(em, ir, ((ir>>1)&0x3FF)==759, &ea, &rd);
tmp = FR(rd);
fpii2d(ea, &tmp);
}
static void
mcrfs(Emreg *em, ulong ir)
{
int rd, ra, rb;
static ulong fpscr0[] ={
FPS_FX|FPS_OX,
FPS_UX|FPS_ZX|FPS_XX|FPS_VXSNAN,
FPS_VXISI|FPS_VXIDI|FPS_VXZDZ|FPS_VXIMZ,
FPS_VXVC,
0,
FPS_VXCVI,
};
getarrr(ir);
if(rb || ra&3 || rd&3)
unimp(em, ir);
ra >>= 2;
rd >>= 2;
em->ur->cr = (em->ur->cr & ~mkCR(rd, 0xF)) | mkCR(rd, getCR(ra, em->ufp->fpscr));
em->ufp->fpscr &= ~fpscr0[ra];
if(fpemudebug)
print("%8.8lux mcrfs\tcrf%d,crf%d\n", em->ur->pc, rd, ra);
}
static void
mffs(Emreg *em, ulong ir)
{
int rd, ra, rb;
Double dw;
getarrr(ir);
if(ra || rb)
unimp(em, ir);
dw.h = 0;
dw.l = ((uvlong)0xFFF8000L<<16)|em->ufp->fpscr;
fpid2i(&FR(rd), &dw);
/* it's anyone's guess how CR1 should be set when ir&1 */
em->ur->cr &= ~mkCR(1, 0xE); /* leave SO, reset others */
if(fpemudebug)
print("%8.8lux mffs%s\tfr%d\n", em->ur->pc, ir&1?".":"", rd);
}
static void
mtfsb1(Emreg *em, ulong ir)
{
int rd, ra, rb;
getarrr(ir);
if(ra || rb)
unimp(em, ir);
em->ufp->fpscr |= (1L << (31-rd));
/* BUG: should set summary bits */
if(ir & 1)
em->ur->cr &= ~mkCR(1, 0xE); /* BUG: manual unclear: leave SO, reset others? */
if(fpemudebug)
print("%8.8lux mtfsb1%s\tfr%d\n", em->ur->pc, ir&1?".":"", rd);
}
static void
mtfsb0(Emreg *em, ulong ir)
{
int rd, ra, rb;
getarrr(ir);
if(ra || rb)
unimp(em, ir);
em->ufp->fpscr &= ~(1L << (31-rd));
if(ir & 1)
em->ur->cr &= ~mkCR(1, 0xE); /* BUG: manual unclear: leave SO, reset others? */
if(fpemudebug)
print("%8.8lux mtfsb0%s\tfr%d\n", em->ur->pc, ir&1?".":"", rd);
}
static void
mtfsf(Emreg *em, ulong ir)
{
int fm, rb, i;
ulong v;
Internal b;
Double db;
if(ir & ((1L << 25)|(1L << 16)))
unimp(em, ir);
rb = (ir >> 11) & 0x1F;
fm = (ir >> 17) & 0xFF;
b = FR(rb);
fpii2d(&db, &b); /* reconstruct hi/lo format to recover low word */
v = db.l;
for(i=0; i<8; i++)
if(fm & (1 << (7-i)))
em->ufp->fpscr = (em->ufp->fpscr & ~mkCR(i, 0xF)) | mkCR(i, getCR(i, v));
/* BUG: should set FEX and VX `according to the usual rule' */
if(ir & 1)
em->ur->cr &= ~mkCR(1, 0xE); /* BUG: manual unclear: leave SO, reset others? */
if(fpemudebug)
print("%8.8lux mtfsf%s\t#%.2x,fr%d\n", em->ur->pc, ir&1?".":"", fm, rb);
}
static void
mtfsfi(Emreg *em, ulong ir)
{
int imm, rd;
if(ir & ((0x7F << 16)|(1L << 11)))
unimp(em, ir);
rd = (ir >> 23) & 0xF;
imm = (ir >> 12) & 0xF;
em->ufp->fpscr = (em->ufp->fpscr & ~mkCR(rd, 0xF)) | mkCR(rd, imm);
/* BUG: should set FEX and VX `according to the usual rule' */
if(ir & 1)
em->ur->cr &= ~mkCR(1, 0xE); /* BUG: manual unclear: leave SO, reset others? */
if(fpemudebug)
print("%8.8lux mtfsfi%s\tcrf%d,#%x\n", em->ur->pc, ir&1?".":"", rd, imm);
}
static void
fcmp(Emreg *em, ulong ir)
{
int fc, rd, ra, rb, sig, i;
getarrr(ir);
if(rd & 3)
unimp(em, ir);
rd >>= 2;
sig = 0;
switch(getxo(ir)) {
default:
unimp(em, ir);
case 32:
if(fpemudebug)
print("%8.8lux fcmpo\tcr%d,f%d,f%d\n", em->ur->pc, rd, ra, rb);
sig = 1;
break;
case 0:
if(fpemudebug)
print("%8.8lux fcmpu\tcr%d,f%d,f%d\n", em->ur->pc, rd, ra, rb);
break;
}
if(IsWeird(&FR(ra)) || IsWeird(&FR(rb))) {
if(sig){
; /* BUG: should trap if not masked ... */
}
fc = CRFU;
} else {
i = fpicmp(&FR(ra), &FR(rb));
if(i > 0)
fc = CRGT;
else if(i == 0)
fc = CREQ;
else
fc = CRLT;
}
fc >>= 28;
em->ur->cr = (em->ur->cr & ~mkCR(rd,~0)) | mkCR(rd, fc);
em->ufp->fpscr = (em->ufp->fpscr & ~0xF800) | (fc<<11);
/* BUG: update FX, VXSNAN, VXVC */
}
static void
fariths(Emreg *em, ulong ir)
{
int rd, ra, rb, rc, fmt;
char *cc, *n;
ulong fpscr;
Internal *d;
fmt = 0;
rc = (ir>>6)&0x1F;
getarrr(ir);
d = &FR(rd);
switch(getxo(ir)&0x1F) { /* partial XO decode */
case 22: /* fsqrts */
case 24: /* fres */
default:
unimp(em, ir);
return;
case 18:
if(IsZero(&FR(rb))) {
em->ufp->fpscr |= FPS_ZX | FPS_FX;
error("sys: fp: zero divide");
}
fdiv(em, d, ra, rb);
n = "fdivs";
break;
case 20:
fsub(em, d, ra, rb);
n = "fsubs";
break;
case 21:
fadd(em, d, ra, rb);
n = "fadds";
break;
case 25:
fmul(em, d, ra, rc);
rb = rc;
n = "fmuls";
break;
case 28:
fmsub(em, d, ra, rc, rb);
fmt = 2;
n = "fmsubs";
break;
case 29:
fmadd(em, d, ra, rc, rb);
fmt = 2;
n = "fmadds";
break;
case 30:
fmsub(em, d, ra, rc, rb);
d->s ^= 1;
fmt = 2;
n = "fnmsubs";
break;
case 31:
fmadd(em, d, ra, rc, rb);
d->s ^= 1;
fmt = 2;
n = "fnmadds";
break;
}
if(fmt==1 && ra)
unimp(em, ir);
fpscr = setfpscr(em);
setfpcc(em, rd);
cc = "";
if(ir & 1) {
cc = ".";
em->ur->cr = (em->ur->cr & ~mkCR(1, ~0)) | mkCR(1, (fpscr>>28));
}
if(fpemudebug) {
switch(fmt) {
case 0:
print("%8.8lux %s%s\tfr%d,fr%d,fr%d\n", em->ur->pc, n, cc, rd, ra, rb);
break;
case 1:
print("%8.8lux %s%s\tfr%d,fr%d\n", em->ur->pc, n, cc, rd, rb);
break;
case 2:
print("%8.8lux %s%s\tfr%d,fr%d,fr%d,fr%d\n", em->ur->pc, n, cc, rd, ra, rc, rb);
break;
}
}
}
static void
farith(Emreg *em, ulong ir)
{
Word w;
Double dv;
int rd, ra, rb, rc, fmt;
char *cc, *n;
ulong fpscr;
int nocc;
Internal *d;
fmt = 0;
nocc = 0;
rc = (ir>>6)&0x1F;
getarrr(ir);
d = &FR(rd);
switch(getxo(ir)&0x1F) { /* partial XO decode */
case 22: /* frsqrt */
case 23: /* fsel */
case 26: /* fsqrte */
default:
unimp(em, ir);
return;
case 12: /* frsp */
*d = FR(rb); /* BUG: doesn't round to single precision */
fmt = 1;
n = "frsp";
break;
case 14: /* fctiw */ /* BUG: ignores rounding mode */
case 15: /* fctiwz */
fpii2w(&w, &FR(rb));
dv.h = 0;
dv.l = w;
fpid2i(d, &dv);
fmt = 1;
nocc = 1;
n = "fctiw";
break;
case 18:
if(IsZero(&FR(rb))) {
em->ufp->fpscr |= FPS_ZX | FPS_FX;
error("sys: fp: zero divide");
}
fdiv(em, d, ra, rb);
n = "fdiv";
break;
case 20:
fsub(em, d, ra, rb);
n = "fsub";
break;
case 21:
fadd(em, d, ra, rb);
n = "fadd";
break;
case 25:
fmul(em, d, ra, rc);
rb = rc;
n = "fmul";
break;
case 28:
fmsub(em, d, ra, rc, rb);
fmt = 2;
n = "fmsub";
break;
case 29:
fmadd(em, d, ra, rc, rb);
fmt = 2;
n = "fmadd";
break;
case 30:
fmsub(em, d, ra, rc, rb);
d->s ^= 1;
fmt = 2;
n = "fnmsub";
break;
case 31:
fmadd(em, d, ra, rc, rb);
d->s ^= 1;
fmt = 2;
n = "fnmadd";
break;
}
if(fmt==1 && ra)
unimp(em, ir);
fpscr = setfpscr(em);
if(nocc == 0)
setfpcc(em, rd);
cc = "";
if(ir & 1) {
cc = ".";
em->ur->cr = (em->ur->cr & ~mkCR(1, ~0)) | mkCR(1, (fpscr>>28));
}
if(fpemudebug) {
switch(fmt) {
case 0:
print("%8.8lux %s%s\tfr%d,fr%d,fr%d\n", em->ur->pc, n, cc, rd, ra, rb);
break;
case 1:
print("%8.8lux %s%s\tfr%d,fr%d\n", em->ur->pc, n, cc, rd, rb);
break;
case 2:
print("%8.8lux %s%s\tfr%d,fr%d,fr%d,fr%d\n", em->ur->pc, n, cc, rd, ra, rc, rb);
break;
}
}
}
static void
farith2(Emreg *em, ulong ir)
{
int rd, ra, rb;
char *cc, *n;
ulong fpscr;
Internal *d, *b;
getarrr(ir);
if(ra)
unimp(em, ir);
d = &FR(rd);
b = &FR(rb);
switch(getxo(ir)) { /* full XO decode */
default:
unimp(em, ir);
case 40:
*d = *b;
d->s ^= 1;
n = "fneg";
break;
case 72:
*d = *b;
n = "fmr";
break;
case 136:
*d = *b;
d->s = 1;
n = "fnabs";
break;
case 264:
*d = *b;
d->s = 0;
n = "fabs";
break;
}
fpscr = setfpscr(em);
setfpcc(em, rd);
cc = "";
if(ir & 1) {
cc = ".";
em->ur->cr = (em->ur->cr & ~mkCR(1, ~0)) | mkCR(1, (fpscr>>28));
}
if(fpemudebug)
print("%8.8lux %s%s\tfr%d,fr%d\n", em->ur->pc, n, cc, rd, rb);
}
static ulong
setfpscr(Emreg *em)
{
ulong fps, fpscr;
fps = 0; /* BUG: getfsr() */
fpscr = em->ufp->fpscr;
if(fps & FPAOVFL)
fpscr |= FPS_OX;
if(fps & FPAINEX)
fpscr |= FPS_XX;
if(fps & FPAUNFL)
fpscr |= FPS_UX;
if(fps & FPAZDIV)
fpscr |= FPS_ZX;
if(fpscr != em->ufp->fpscr) {
fpscr |= FPS_FX;
em->ufp->fpscr = fpscr;
}
return fpscr;
}
static void
setfpcc(Emreg *em, int r)
{
int c;
Internal *d;
d = &FR(r);
c = 0;
if(IsZero(d))
c |= 2;
else if(d->s == 1)
c |= 4;
else
c |= 8;
if(IsNaN(d))
c |= 1;
em->ufp->fpscr = (em->ufp->fpscr & ~0xF800) | (0<<15) | (c<<11); /* unsure about class bit */
}
static uchar op63flag[32] = {
[12] 1, [14] 1, [15] 1, [18] 1, [20] 1, [21] 1, [22] 1,
[23] 1, [25] 1, [26] 1, [28] 1, [29] 1, [30] 1, [31] 1,
};
/*
* returns the number of FP instructions emulated
*/
int
fpipower(Ureg *ur)
{
ulong op;
int xo;
Emreg emreg, *em;
FPenv *ufp;
int n;
ufp = &up->env->fpu; /* because all the state is in Osenv, it need not be saved/restored */
em = &emreg;
em->ur = ur;
em->fr = ufp->emreg;
em->ufp = ufp;
em->name = nil;
if(em->ufp->fpistate != FPACTIVE) {
em->ufp->fpistate = FPACTIVE;
em->ufp->fpscr = 0; /* TO DO */
for(n = 0; n < nelem(fpreginit); n++)
FR(31-n) = fpreginit[n];
}
for(n=0;;n++){
op = getulong(ur->pc);
em->ir = op;
if(fpemudebug > 1)
print("%8.8lux %8.8lux: ", ur->pc, op);
switch(op>>26){
default:
return n;
case 48: /* lfs */
case 49: /* lfsu */
lfs(em, op);
break;
case 50: /* lfd */
case 51: /* lfdu */
lfd(em, op);
break;
case 52: /* stfs */
case 53: /* stfsu */
stfs(em, op);
break;
case 54: /* stfd */
case 55: /* stfdu */
stfd(em, op);
break;
case 31: /* indexed load/store */
xo = getxo(op);
if((xo & 0x300) != 0x200)
return n;
switch(xo){
default:
return n;
case 535: /* lfsx */
case 567: /* lfsux */
lfsx(em, op);
break;
case 599: /* lfdx */
case 631: /* lfdux */
lfdx(em, op);
break;
case 663: /* stfsx */
case 695: /* stfsux */
stfsx(em, op);
break;
case 727: /* stfdx */
case 759: /* stfdux */
stfdx(em, op);
break;
}
break;
case 63: /* double precision */
xo = getxo(op);
if(op63flag[xo & 0x1F]){
farith(em, op);
break;
}
switch(xo){
default:
return n;
case 0: /* fcmpu */
case 32: /* fcmpo */
fcmp(em, op);
break;
case 40: /* fneg */
case 72: /* fmr */
case 136: /* fnabs */
case 264: /* fabs */
farith2(em, op);
break;
case 38:
mtfsb1(em, op);
break;
case 64:
mcrfs(em, op);
break;
case 70:
mtfsb0(em, op);
break;
case 134:
mtfsfi(em, op);
break;
case 583:
mffs(em, op);
break;
case 711:
mtfsf(em, op);
break;
}
break;
case 59: /* single precision */
fariths(em, op);
break;
}
ur->pc += 4;
if(anyhigher())
sched();
}
return n;
}
/*
50: lfd frD,d(rA)
51: lfdu frD,d(rA)
31,631: lfdux frD,rA,rB
31,599: lfdx frD,rA,rB
48: lfs frD,d(rA)
49: lfsu frD,d(rA)
31,567: lfsux frD,rA,rB
31,535: lfsx frD,rA,rB
54: stfd frS,d(rA)
55: stfdu frS,d(rA)
31,759: stfdux frS,rA,rB
31,727: stfdx frS,rA,rB
52: stfs frS,d(rA)
53: stfsu frS,d(rA)
31,695: stfsux frS,rA,rB
31,663: stfsx frS,rA,rB
63,64: mcrfs crfD,crfS
63,583: mffs[.] frD
63,70: mtfsb0[.] crbD
63,38: mtfsb1[.] crbD
63,711: mtfsf[.] FM,frB
63,134: mtfsfi[.] crfD,IMM
*/
/*
float to int:
FMOVD g+0(SB),F1
FCTIWZ F1,F4
FMOVD F4,.rathole+0(SB)
MOVW .rathole+4(SB),R7
MOVW R7,l+0(SB)
*/
/*
int to float:
MOVW $1127219200,R9
MOVW l+0(SB),R7
MOVW R9,.rathole+0(SB)
XOR $-2147483648,R7,R6
MOVW R6,.rathole+4(SB)
FMOVD .rathole+0(SB),F0
FSUB F27,F0
unsigned to float:
MOVW ul+0(SB),R5
MOVW R9,.rathole+0(SB)
XOR $-2147483648,R5,R4
MOVW R4,.rathole+4(SB)
FMOVD .rathole+0(SB),F3
FSUB F27,F3
FCMPU F3,F28
BGE ,3(PC)
FMOVD $4.29496729600000000e+09,F2
FADD F2,F3
FMOVD F3,g+0(SB)
*/
|