25 suicide("undefined instruction %8ux @ %8ux", instr, P->R[15] - 4);
33 if((instr & (1<<4)) && (instr & (1<<7)))
37 amount = P->R[(instr >> 8) & 15];
39 amount = (instr >> 7) & 31;
40 val = P->R[instr & 15];
41 switch((instr >> 5) & 3) {
47 return ((long) val) >> amount;
49 return (val >> amount) | (val << (32 - amount));
66 offset = doshift(instr);
68 offset = instr & ((1<<12) - 1);
71 Rn = P->R + ((instr >> 16) & 15);
72 Rd = P->R + ((instr >> 12) & 15);
73 if((instr & (fW | fP)) == fW)
84 targ = vaddr(addr, 4, &seg);
85 switch(instr & (fB | fL)) {
87 *(u32int*) targ = *Rd;
93 *Rd = *(u32int*) targ;
99 if(Rd == P->R + 15 && !(instr & fL)) {
103 *(u32int*) targ += 8;
108 if((instr & fW) || !(instr & fP))
115 u32int *Rm, *Rn, *Rd, *targ, tmp;
118 Rm = P->R + (instr & 15);
119 Rd = P->R + ((instr >> 12) & 15);
120 Rn = P->R + ((instr >> 16) & 15);
121 if(Rm == P->R + 15 || Rd == P->R + 15 || Rn == P->R + 15)
123 targ = (u32int *) vaddr(*Rn, 4, &seg);
126 tmp = *(u8int*) targ;
127 *(u8int*) targ = *Rm;
139 add(u32int a, u32int b, u8int type, u8int *carry, u8int *overflow)
145 res2 = (u64int)a - b + *carry - 1;
147 if(((a ^ b) & (1<<31)) && !((b ^ res1) & (1<<31))) *overflow = 1;
148 if(res2 & 0x100000000LL) *carry = 0;
151 res2 = (u64int)a + b + *carry;
153 if(!((a ^ b) & (1<<31)) && ((b ^ res1) & (1<<31))) *overflow = 1;
154 if(res2 & 0x100000000LL) *carry = 1;
163 u32int Rn, *Rd, operand, shift, result, op;
164 u8int carry, overflow;
166 Rn = P->R[(instr >> 16) & 15];
167 Rd = P->R + ((instr >> 12) & 15);
168 if(((instr >> 16) & 15) == 15) {
170 if(!(instr & fI) && (instr & (1<<4)))
173 if(Rd == P->R + 15 && (instr & fS))
176 operand = instr & 0xFF;
177 shift = ((instr >> 8) & 15) << 1;
178 operand = (operand >> shift) | (operand << (32 - shift));
180 operand = doshift(instr);
181 op = (instr >> 21) & 15;
183 if(op >= 8 && op <= 11 && !(instr & fS))
184 sysfatal("no PSR transfers plz");
185 if(op >= 5 && op < 8) {
189 if(op != 4 && op != 5 && op != 11)
194 case 0: case 8: result = Rn & operand; break;
195 case 1: case 9: result = Rn ^ operand; break;
196 case 2: case 6: case 10: result = add(Rn, operand, 1, &carry, &overflow); break;
197 case 3: case 7: result = add(operand, Rn, 1, &carry, &overflow); break;
198 case 4: case 5: case 11: result = add(operand, Rn, 0, &carry, &overflow); break;
199 case 12: result = Rn | operand; break;
200 case 13: result = operand; break;
201 case 14: result = Rn & ~operand; break;
202 case 15: result = ~operand; break;
203 default: result = 0; /* never happens */
211 if(carry && op > 1 && op < 12)
216 if(op < 8 || op >= 12)
225 offset = instr & ((1<<24) - 1);
227 offset |= ~((1 << 24) - 1);
231 P->R[15] += offset + 4;
235 halfword(u32int instr)
237 u32int offset, target, *Rn, *Rd;
240 if(instr & (1<<22)) {
241 offset = (instr & 15) | ((instr >> 4) & 0xF0);
243 if((instr & 15) == 15)
245 offset = P->R[instr & 15];
249 if(!(instr & fP) && (instr & fW))
251 Rn = P->R + ((instr >> 16) & 15);
252 Rd = P->R + ((instr >> 12) & 15);
253 if(Rn == P->R + 15 || Rd == P->R + 15)
254 sysfatal("R15 in halfword");
258 switch(instr & (fSg | fH | fL)) {
259 case fSg: *(u8int*) vaddr(target, 1, &seg) = *Rd; break;
260 case fSg | fL: *Rd = (long) *(char*) vaddr(target, 1, &seg); break;
261 case fH: case fSg | fH: *(u16int*) vaddr(target, 2, &seg) = *Rd; break;
262 case fH | fL: *Rd = *(u16int*) vaddr(target, 2, &seg); break;
263 case fH | fL | fSg: *Rd = (long) *(short*) vaddr(target, 2, &seg); break;
268 if(!(instr & fP) || (instr & fW))
281 Rn = P->R + ((instr >> 16) & 15);
282 if(Rn == P->R + 15 || instr & (1<<15))
283 sysfatal("R15 block");
286 for(i = 0; i < 16; i++) {
287 if(!(instr & (1<<i)))
292 P->R[i] = *(u32int*) vaddr(targ, 4, &seg);
294 *(u32int*) vaddr(targ, 4, &seg) = P->R[i];
300 for(i = 15; i >= 0; i--) {
301 if(!(instr & (1<<i)))
306 P->R[i] = *(u32int*) vaddr(targ, 4, &seg);
308 *(u32int*) vaddr(targ, 4, &seg) = P->R[i];
319 multiply(u32int instr)
321 u32int *Rd, *Rn, *Rs, *Rm, res;
323 Rm = P->R + (instr & 15);
324 Rs = P->R + ((instr >> 8) & 15);
325 Rn = P->R + ((instr >> 12) & 15);
326 Rd = P->R + ((instr >> 16) & 15);
327 if(Rd == Rm || Rm == P->R + 15 || Rs == P->R + 15 || Rn == P->R + 15 || Rd == P->R + 15)
333 if(instr & (1<<20)) {
334 P->CPSR &= ~(flN | flZ);
343 multiplylong(u32int instr)
345 u32int *RdH, *RdL, *Rs, *Rm;
348 Rm = P->R + (instr & 15);
349 Rs = P->R + ((instr >> 8) & 15);
350 RdL = P->R + ((instr >> 12) & 15);
351 RdH = P->R + ((instr >> 16) & 15);
352 if(RdL == RdH || RdH == Rm || RdL == Rm || Rm == P->R + 15 || Rs == P->R + 15 || RdL == P->R + 15 || RdH == P->R + 15)
354 if(instr & (1<<22)) {
358 res = ((vlong)*(int*)Rs) * *(int*)Rm;
359 if(instr & (1<<21)) {
361 res += ((uvlong)*RdH) << 32;
365 if(instr & (1<<20)) {
375 singleex(u32int instr)
377 u32int *Rn, *Rd, *Rm, *targ;
380 Rd = P->R + ((instr >> 12) & 15);
381 Rn = P->R + ((instr >> 16) & 15);
382 if(Rd == P->R + 15 || Rn == P->R + 15)
385 targ = vaddr(*Rn, 4, &seg);
390 Rm = P->R + (instr & 15);
393 targ = vaddr(*Rn, 4, &seg);
394 if(canlock(&seg->lock)) {
411 instr = *(u32int*) vaddr(P->R[15], 4, &seg);
414 print("%d ", P->pid);
419 if(findsym(P->R[15], CTEXT, &s) >= 0)
420 print("%s ", s.name);
421 if(fileline(buf, 512, P->R[15]) >= 0)
424 print("%.8ux %.8ux %c%c%c%c\n", P->R[15], instr,
425 (P->CPSR & flZ) ? 'Z' : ' ',
426 (P->CPSR & flC) ? 'C' : ' ',
427 (P->CPSR & flN) ? 'N' : ' ',
428 (P->CPSR & flV) ? 'V' : ' '
432 switch(instr >> 28) {
433 case 0x0: if(!(P->CPSR & flZ)) return; break;
434 case 0x1: if(P->CPSR & flZ) return; break;
435 case 0x2: if(!(P->CPSR & flC)) return; break;
436 case 0x3: if(P->CPSR & flC) return; break;
437 case 0x4: if(!(P->CPSR & flN)) return; break;
438 case 0x5: if(P->CPSR & flN) return; break;
439 case 0x6: if(!(P->CPSR & flV)) return; break;
440 case 0x7: if(P->CPSR & flV) return; break;
441 case 0x8: if(!(P->CPSR & flC) || (P->CPSR & flZ)) return; break;
442 case 0x9: if((P->CPSR & flC) && !(P->CPSR & flZ)) return; break;
443 case 0xA: if(!(P->CPSR & flN) != !(P->CPSR & flV)) return; break;
444 case 0xB: if(!(P->CPSR & flN) == !(P->CPSR & flV)) return; break;
445 case 0xC: if((P->CPSR & flZ) || !(P->CPSR & flN) != !(P->CPSR & flV)) return; break;
446 case 0xD: if(!(P->CPSR & flZ) && !(P->CPSR & flN) == !(P->CPSR & flV)) return; break;
448 default: sysfatal("condition code %x not implemented", instr >> 28);
450 if((instr & 0x0FB00FF0) == 0x01000090)
452 else if((instr & 0x0FE000F0) == 0x01800090)
454 else if((instr & 0x0FC000F0) == 0x90)
456 else if((instr & 0x0F8000F0) == 0x800090)
458 else if((instr & ((1<<26) | (1<<27))) == (1 << 26))
460 else if((instr & 0x0E000090) == 0x90 && (instr & 0x60))
462 else if((instr & ((1<<26) | (1<<27))) == 0)
464 else if((instr & (7<<25)) == (5 << 25))
466 else if((instr & (15<<24)) == (15 << 24))
468 else if((instr & (7<<25)) == (4 << 25))
470 else if((instr & 0x0E000F00) == 0x0C000100)
472 else if((instr & 0x0E000F10) == 0x0E000100)
474 else if((instr & 0x0E000F10) == 0x0E000110)
475 fparegtransfer(instr);
476 else if(vfp && ((instr & 0x0F000A10) == 0x0E000A00))
478 else if(vfp && ((instr & 0x0F000F10) == 0x0E000A10))
479 vfpregtransfer(instr);
480 else if(vfp && ((instr & 0x0F000A00) == 0x0D000A00))
481 vfprmtransfer(instr);