2 * this doesn't attempt to implement ARM floating-point properties
3 * that aren't visible in the Inferno environment.
4 * all arithmetic is done in double precision.
5 * the FP trap status isn't updated.
8 #include "../port/lib.h"
18 #define ARM7500 /* emulate old pre-VFP opcodes */
20 /* undef this if correct kernel r13 isn't in Ureg;
21 * check calculation in fpiarm below
24 #define REG(ur, x) (*(long*)(((char*)(ur))+roff[(x)]))
26 #define FR(ufp, x) (*(Internal*)(ufp)->regs[(x)&7])
28 #define FR(ufp, x) (*(Internal*)(ufp)->regs[(x)&(Nfpregs - 1)])
31 typedef struct FP2 FP2;
32 typedef struct FP1 FP1;
36 void (*f)(Internal, Internal, Internal*);
41 void (*f)(Internal*, Internal*);
57 #define OFR(X) ((ulong)&((Ureg*)0)->X)
60 OFR(r0), OFR(r1), OFR(r2), OFR(r3),
61 OFR(r4), OFR(r5), OFR(r6), OFR(r7),
62 OFR(r8), OFR(r9), OFR(r10), OFR(r11),
63 OFR(r12), OFR(r13), OFR(r14), OFR(pc),
66 static Internal fpconst[8] = { /* indexed by op&7 (ARM 7500 FPA) */
68 {0, 0x1, 0x00000000, 0x00000000}, /* 0.0 */
69 {0, 0x3FF, 0x00000000, 0x08000000}, /* 1.0 */
70 {0, 0x400, 0x00000000, 0x08000000}, /* 2.0 */
71 {0, 0x400, 0x00000000, 0x0C000000}, /* 3.0 */
72 {0, 0x401, 0x00000000, 0x08000000}, /* 4.0 */
73 {0, 0x401, 0x00000000, 0x0A000000}, /* 5.0 */
74 {0, 0x3FE, 0x00000000, 0x08000000}, /* 0.5 */
75 {0, 0x402, 0x00000000, 0x0A000000}, /* 10.0 */
79 * arm binary operations
83 fadd(Internal m, Internal n, Internal *d)
85 (m.s == n.s? fpiadd: fpisub)(&m, &n, d);
89 fsub(Internal m, Internal n, Internal *d)
92 (m.s == n.s? fpiadd: fpisub)(&m, &n, d);
96 fsubr(Internal m, Internal n, Internal *d)
99 (n.s == m.s? fpiadd: fpisub)(&n, &m, d);
103 fmul(Internal m, Internal n, Internal *d)
109 fdiv(Internal m, Internal n, Internal *d)
115 fdivr(Internal m, Internal n, Internal *d)
121 * arm unary operations
125 fmov(Internal *m, Internal *d)
131 fmovn(Internal *m, Internal *d)
138 fabsf(Internal *m, Internal *d)
145 frnd(Internal *m, Internal *d)
149 (m->s? fsub: fadd)(fpconst[6], *m, d);
153 e = (d->e - ExpBias) + 1;
156 else if(e > FractBits){
158 d->l &= ~((1<<(2*FractBits - e))-1);
162 d->h &= ~((1<<(FractBits-e))-1);
167 * ARM 7500 FPA opcodes
170 static FP1 optab1[16] = { /* Fd := OP Fm */
175 [4] {"SQTF", /*fsqt*/0},
176 /* LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN all `deprecated' */
177 /* URD and NRM aren't implemented */
180 static FP2 optab2[16] = { /* Fd := Fn OP Fm */
184 [3] {"RSUBF", fsubr},
186 [5] {"RDIVF", fdivr},
187 /* POW, RPW deprecated */
188 [8] {"REMF", /*frem*/0},
189 [9] {"FMF", fmul}, /* fast multiply */
190 [10] {"FDV", fdiv}, /* fast divide */
191 [11] {"FRD", fdivr}, /* fast reverse divide */
196 fcmp(Internal *n, Internal *m)
201 if(IsWeird(m) || IsWeird(n)){
202 /* BUG: should trap if not masked */
209 i = fpicmp(&rn, &rm);
219 fld(void (*f)(Internal*, void*), int d, ulong ea, int n, FPsave *ufp)
224 (*f)(&FR(ufp, d), mem);
226 print("MOV%c #%lux, F%d\n", n==8? 'D': 'F', ea, d);
230 fst(void (*f)(void*, Internal*), ulong ea, int s, int n, FPsave *ufp)
238 print("MOV%c F%d,#%lux\n", n==8? 'D': 'F', s, ea);
243 condok(int cc, int c)
248 case 1: /* Z clear */
252 case 3: /* C clear */
256 case 5: /* N clear */
260 case 7: /* V clear */
262 case 8: /* C set and Z clear */
263 return cc&C && (cc&Z) == 0;
264 case 9: /* C clear or Z set */
265 return (cc&C) == 0 || cc&Z;
266 case 10: /* N set and V set, or N clear and V clear */
267 return (~cc&(N|V))==0 || (cc&(N|V)) == 0;
268 case 11: /* N set and V clear, or N clear and V set */
269 return (cc&(N|V))==N || (cc&(N|V))==V;
270 case 12: /* Z clear, and either N set and V set or N clear and V clear */
271 return (cc&Z) == 0 && ((~cc&(N|V))==0 || (cc&(N|V))==0);
272 case 13: /* Z set, or N set and V clear or N clear and V set */
273 return (cc&Z) || (cc&(N|V))==N || (cc&(N|V))==V;
274 case 14: /* always */
276 case 15: /* never (reserved) */
279 return 0; /* not reached */
283 unimp(ulong pc, ulong op)
287 snprint(buf, sizeof(buf), "sys: fp: pc=%lux unimp fp 0x%.8lux", pc, op);
289 print("FPE: %s\n", buf);
295 fpemu(ulong pc, ulong op, Ureg *ur, FPsave *ufp)
300 Internal tmp, *fm, *fn;
302 /* note: would update fault status here if we noted numeric exceptions */
307 if(((op>>25)&7) == 6){
309 unimp(pc, op); /* packed or extended */
312 if((op & (1<<23)) == 0)
322 fld(fpid2i, rd, ea, 8, ufp);
324 fld(fpis2i, rd, ea, 4, ufp);
327 fst(fpii2d, ea, rd, 8, ufp);
329 fst(fpii2s, ea, rd, 4, ufp);
331 if((op & (1<<24)) == 0)
339 * CPRT/transfer, 10.3
347 if(rd == 15 && op & (1<<20)){
362 case 4: /* CMF: Fn :: Fm */
363 case 6: /* CMFE: Fn :: Fm (with exception) */
364 ur->psr &= ~(N|C|Z|V);
365 ur->psr |= fcmp(fn, fm);
367 case 5: /* CNF: Fn :: -Fm */
368 case 7: /* CNFE: Fn :: -Fm (with exception) */
371 ur->psr &= ~(N|C|Z|V);
372 ur->psr |= fcmp(fn, &tmp);
376 print("CMPF %c%d,F%ld =%#lux\n",
377 tag, rn, op&7, ur->psr>>28);
382 * other transfer, 10.3
384 switch((op>>20)&0xF){
389 fpiw2i(&FR(ufp, rn), ®(ur, rd));
391 print("MOVW[FD] R%d, F%d\n", rd, rn);
398 fpii2w(®(ur, rd), &tmp);
400 print("MOV[FD]W F%d, R%d =%ld\n", rn, rd, REG(ur, rd));
402 case 2: /* FPSR := Rd */
403 ufp->status = REG(ur, rd);
405 print("MOVW R%d, FPSR\n", rd);
407 case 3: /* Rd := FPSR */
408 REG(ur, rd) = ufp->status;
410 print("MOVW FPSR, R%d\n", rd);
412 case 4: /* FPCR := Rd */
413 ufp->control = REG(ur, rd);
415 print("MOVW R%d, FPCR\n", rd);
417 case 5: /* Rd := FPCR */
418 REG(ur, rd) = ufp->control;
420 print("MOVW FPCR, R%d\n", rd);
430 if(op & (1<<3)){ /* constant */
441 if(op & (1<<15)){ /* monadic */
447 print("%s %c%ld,F%d\n", fp->name, tag, op&7, rd);
448 (*fp->f)(fm, &FR(ufp, rd));
456 print("%s %c%ld,F%d,F%d\n", fp->name, tag, op&7, rn, rd);
457 (*fp->f)(*fm, FR(ufp, rn), &FR(ufp, rd));
462 * returns the number of FP instructions emulated
472 panic("fpiarm not in a process");
475 * because all the emulated fp state is in the proc structure,
476 * it need not be saved/restored
481 error("illegal instruction: emulated fpu opcode in VFP mode");
483 assert(sizeof(Internal) <= sizeof(ufp->regs[0]));
486 ufp->status = (0x01<<28)|(1<<12); /* sw emulation, alt. C flag */
487 for(n = 0; n < 8; n++)
488 FR(ufp, n) = fpconst[0];
491 validaddr(ur->pc, 4, 0);
492 op = *(ulong*)(ur->pc);
494 print("%#lux: %#8.8lux ", ur->pc, op);
499 if(condok(ur->psr, op>>28))
500 fpemu(ur->pc, op, ur, ufp);
501 ur->pc += 4; /* pretend cpu executed the instr */