Debian Codegraph @

stable/mips64_exec.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * MIPS64 Step-by-step execution.
6 */
7
8 #if __GNUC__ > 2
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <assert.h>
18
19 #include "cpu.h"
20 #include "vm.h"
21 #include "mips64_exec.h"
22 #include "memory.h"
23 #include "insn_lookup.h"
24 #include "dynamips.h"
25
26 /* Forward declaration of instruction array */
27 static struct mips64_insn_exec_tag mips64_exec_tags[];
28 static insn_lookup_t *ilt = NULL;
29
30 /* ILT */
31 static forced_inline void *mips64_exec_get_insn(int index)
32 {
33 return(&mips64_exec_tags[index]);
34 }
35
36 static int mips64_exec_chk_lo(struct mips64_insn_exec_tag *tag,int value)
37 {
38 return((value & tag->mask) == (tag->value & 0xFFFF));
39 }
40
41 static int mips64_exec_chk_hi(struct mips64_insn_exec_tag *tag,int value)
42 {
43 return((value & (tag->mask >> 16)) == (tag->value >> 16));
44 }
45
46 /* Destroy instruction lookup table */
47 static void destroy_ilt(void)
48 {
49 assert(ilt);
50 ilt_destroy(ilt);
51 ilt = NULL;
52 }
53
54 /* Initialize instruction lookup table */
55 void mips64_exec_create_ilt(void)
56 {
57 int i,count;
58
59 for(i=0,count=0;mips64_exec_tags[i].exec;i++)
60 count++;
61
62 ilt = ilt_create("mips64e",count,
63 (ilt_get_insn_cbk_t)mips64_exec_get_insn,
64 (ilt_check_cbk_t)mips64_exec_chk_lo,
65 (ilt_check_cbk_t)mips64_exec_chk_hi);
66
67 atexit(destroy_ilt);
68 }
69
70 /* Dump statistics */
71 void mips64_dump_stats(cpu_mips_t *cpu)
72 {
73 int i;
74
75 #if NJM_STATS_ENABLE
76 printf("\n");
77
78 for(i=0;mips64_exec_tags[i].exec;i++)
79 printf(" * %-10s : %10llu\n",
80 mips64_exec_tags[i].name,mips64_exec_tags[i].count);
81
82 printf("%llu instructions executed since startup.\n",cpu->insn_exec_count);
83 #else
84 printf("Statistics support is not compiled in.\n");
85 #endif
86 }
87
88 /* Dump an instruction */
89 int mips64_dump_insn(char *buffer,size_t buf_size,size_t insn_name_size,
90 m_uint64_t pc,mips_insn_t instruction)
91 {
92 char insn_name[64],insn_format[32],*name;
93 int base,rs,rd,rt,sa,offset,imm;
94 struct mips64_insn_exec_tag *tag;
95 m_uint64_t new_pc;
96 int index;
97
98 /* Lookup for instruction */
99 index = ilt_lookup(ilt,instruction);
100 tag = mips64_exec_get_insn(index);
101
102 if (!tag) {
103 snprintf(buffer,buf_size,"%8.8x (unknown)",instruction);
104 return(-1);
105 }
106
107 if (!(name = tag->name))
108 name = "[unknown]";
109
110 if (!insn_name_size)
111 insn_name_size = 10;
112
113 snprintf(insn_format,sizeof(insn_format),"%%-%lus",(u_long)insn_name_size);
114 snprintf(insn_name,sizeof(insn_name),insn_format,name);
115
116 switch(tag->instr_type) {
117 case 1: /* instructions without operands */
118 snprintf(buffer,buf_size,"%8.8x %s",instruction,insn_name);
119 break;
120
121 case 2: /* load/store instructions */
122 base = bits(instruction,21,25);
123 rt = bits(instruction,16,20);
124 offset = (m_int16_t)bits(instruction,0,15);
125 snprintf(buffer,buf_size,"%8.8x %s %s,%d(%s)",
126 instruction,insn_name,mips64_gpr_reg_names[rt],
127 offset,mips64_gpr_reg_names[base]);
128 break;
129
130 case 3: /* GPR[rd] = GPR[rs] op GPR[rt] */
131 rs = bits(instruction,21,25);
132 rt = bits(instruction,16,20);
133 rd = bits(instruction,11,15);
134 snprintf(buffer,buf_size,"%8.8x %s %s,%s,%s",
135 instruction,insn_name,mips64_gpr_reg_names[rd],
136 mips64_gpr_reg_names[rs],mips64_gpr_reg_names[rt]);
137 break;
138
139 case 4: /* GPR[rd] = GPR[rt] op GPR[rs] */
140 rs = bits(instruction,21,25);
141 rt = bits(instruction,16,20);
142 rd = bits(instruction,11,15);
143 snprintf(buffer,buf_size,"%8.8x %s %s,%s,%s",
144 instruction,insn_name,mips64_gpr_reg_names[rd],
145 mips64_gpr_reg_names[rt],mips64_gpr_reg_names[rs]);
146 break;
147
148 case 5: /* GPR[rt] = GPR[rs] op immediate (hex) */
149 rs = bits(instruction,21,25);
150 rt = bits(instruction,16,20);
151 imm = bits(instruction,0,15);
152 snprintf(buffer,buf_size,"%8.8x %s %s,%s,0x%x",
153 instruction,insn_name,mips64_gpr_reg_names[rt],
154 mips64_gpr_reg_names[rs],imm);
155 break;
156
157 case 6: /* GPR[rt] = GPR[rs] op immediate (dec) */
158 rs = bits(instruction,21,25);
159 rt = bits(instruction,16,20);
160 imm = bits(instruction,0,15);
161 snprintf(buffer,buf_size,"%8.8x %s %s,%s,%d",
162 instruction,insn_name,mips64_gpr_reg_names[rt],
163 mips64_gpr_reg_names[rs],(m_int16_t)imm);
164 break;
165
166 case 7: /* GPR[rd] = GPR[rt] op sa */
167 rt = bits(instruction,16,20);
168 rd = bits(instruction,11,15);
169 sa = bits(instruction,6,10);
170 snprintf(buffer,buf_size,"%8.8x %s %s,%s,%d",
171 instruction,insn_name,mips64_gpr_reg_names[rd],
172 mips64_gpr_reg_names[rt],sa);
173 break;
174
175 case 8: /* Branch with: GPR[rs] / GPR[rt] / offset */
176 rs = bits(instruction,21,25);
177 rt = bits(instruction,16,20);
178 offset = bits(instruction,0,15);
179 new_pc = (pc + 4) + sign_extend(offset << 2,18);
180 snprintf(buffer,buf_size,"%8.8x %s %s,%s,0x%llx",
181 instruction,insn_name,mips64_gpr_reg_names[rs],
182 mips64_gpr_reg_names[rt],new_pc);
183 break;
184
185 case 9: /* Branch with: GPR[rs] / offset */
186 rs = bits(instruction,21,25);
187 offset = bits(instruction,0,15);
188 new_pc = (pc + 4) + sign_extend(offset << 2,18);
189 snprintf(buffer,buf_size,"%8.8x %s %s,0x%llx",
190 instruction,insn_name,mips64_gpr_reg_names[rs],new_pc);
191 break;
192
193 case 10: /* Branch with: offset */
194 offset = bits(instruction,0,15);
195 new_pc = (pc + 4) + sign_extend(offset << 2,18);
196 snprintf(buffer,buf_size,"%8.8x %s 0x%llx",
197 instruction,insn_name,new_pc);
198 break;
199
200 case 11: /* Jump */
201 offset = bits(instruction,0,25);
202 new_pc = (pc & ~((1 << 28) - 1)) | (offset << 2);
203 snprintf(buffer,buf_size,"%8.8x %s 0x%llx",
204 instruction,insn_name,new_pc);
205 break;
206
207 case 13: /* op GPR[rs] */
208 rs = bits(instruction,21,25);
209 snprintf(buffer,buf_size,"%8.8x %s %s",
210 instruction,insn_name,mips64_gpr_reg_names[rs]);
211 break;
212
213 case 14: /* op GPR[rd] */
214 rd = bits(instruction,11,15);
215 snprintf(buffer,buf_size,"%8.8x %s %s",
216 instruction,insn_name,mips64_gpr_reg_names[rd]);
217 break;
218
219 case 15: /* op GPR[rd], GPR[rs] */
220 rs = bits(instruction,21,25);
221 rd = bits(instruction,11,15);
222 snprintf(buffer,buf_size,"%8.8x %s %s,%s",
223 instruction,insn_name,mips64_gpr_reg_names[rd],
224 mips64_gpr_reg_names[rs]);
225 break;
226
227 case 16: /* op GPR[rt], imm */
228 rt = bits(instruction,16,20);
229 imm = bits(instruction,0,15);
230 snprintf(buffer,buf_size,"%8.8x %s %s,0x%x",
231 instruction,insn_name,mips64_gpr_reg_names[rt],imm);
232 break;
233
234 case 17: /* op GPR[rs], GPR[rt] */
235 rs = bits(instruction,21,25);
236 rt = bits(instruction,16,20);
237 snprintf(buffer,buf_size,"%8.8x %s %s,%s",
238 instruction,insn_name,mips64_gpr_reg_names[rs],
239 mips64_gpr_reg_names[rt]);
240 break;
241
242 case 18: /* op GPR[rt], CP0[rd] */
243 rt = bits(instruction,16,20);
244 rd = bits(instruction,11,15);
245 snprintf(buffer,buf_size,"%8.8x %s %s,%s",
246 instruction,insn_name,mips64_gpr_reg_names[rt],
247 mips64_cp0_reg_names[rd]);
248 break;
249
250 case 19: /* op GPR[rt], $rd */
251 rt = bits(instruction,16,20);
252 rd = bits(instruction,11,15);
253 snprintf(buffer,buf_size,"%8.8x %s %s,$%d",
254 instruction,insn_name,mips64_gpr_reg_names[rt],rd);
255 break;
256
257 case 20: /* op GPR[rs], imm */
258 rs = bits(instruction,21,25);
259 imm = bits(instruction,0,15);
260 snprintf(buffer,buf_size,"%8.8x %s %s,0x%x",
261 instruction,insn_name,mips64_gpr_reg_names[rs],imm);
262 break;
263
264 default:
265 snprintf(buffer,buf_size,"%8.8x %s (TO DEFINE - %d)",
266 instruction,insn_name,tag->instr_type);
267 return(-1);
268 }
269
270 return(0);
271 }
272
273 /* Dump an instruction block */
274 void mips64_dump_insn_block(cpu_mips_t *cpu,m_uint64_t pc,u_int count,
275 size_t insn_name_size)
276 {
277 mips_insn_t *ptr,insn;
278 char buffer[80];
279 int i;
280
281 for(i=0;i<count;i++) {
282 ptr = cpu->mem_op_lookup(cpu,pc);
283 insn = vmtoh32(*ptr);
284
285 mips64_dump_insn(buffer,sizeof(buffer),insn_name_size,pc,insn);
286 printf("0x%llx: %s\n",pc,buffer);
287 pc += sizeof(mips_insn_t);
288 }
289 }
290
291 /* Execute a memory operation */
292 _Unused static forced_inline void mips64_exec_memop(cpu_mips_t *cpu,int memop,
293 m_uint64_t vaddr,u_int dst_reg,
294 int keep_ll_bit)
295 {
296 fastcall mips_memop_fn fn;
297
298 if (!keep_ll_bit) cpu->ll_bit = 0;
299 fn = cpu->mem_op_fn[memop];
300 fn(cpu,vaddr,dst_reg);
301 }
302
303 /* Execute a memory operation (2) */
304 static forced_inline void mips64_exec_memop2(cpu_mips_t *cpu,int memop,
305 m_uint64_t base,int offset,
306 u_int dst_reg,int keep_ll_bit)
307 {
308 m_uint64_t vaddr = cpu->gpr[base] + sign_extend(offset,16);
309 fastcall mips_memop_fn fn;
310
311 if (!keep_ll_bit) cpu->ll_bit = 0;
312 fn = cpu->mem_op_fn[memop];
313 fn(cpu,vaddr,dst_reg);
314 }
315
316 /* Fetch an instruction */
317 static forced_inline int mips64_exec_fetch(cpu_mips_t *cpu,m_uint64_t pc,
318 mips_insn_t *insn)
319 {
320 m_uint64_t exec_page;
321 m_uint32_t offset;
322
323 exec_page = pc & ~(m_uint64_t)MIPS_MIN_PAGE_IMASK;
324
325 if (unlikely(exec_page != cpu->njm_exec_page)) {
326 cpu->njm_exec_page = exec_page;
327 cpu->njm_exec_ptr = cpu->mem_op_lookup(cpu,exec_page);
328 }
329
330 offset = (pc & MIPS_MIN_PAGE_IMASK) >> 2;
331 *insn = vmtoh32(cpu->njm_exec_ptr[offset]);
332 return(0);
333 }
334
335 /* Unknown opcode */
336 static fastcall int mips64_exec_unknown(cpu_mips_t *cpu,mips_insn_t insn)
337 {
338 printf("MIPS64: unknown opcode 0x%8.8x at pc = 0x%llx\n",insn,cpu->pc);
339 mips64_dump_regs(cpu->gen);
340 return(0);
341 }
342
343 /* Execute a single instruction */
344 static forced_inline int
345 mips64_exec_single_instruction(cpu_mips_t *cpu,mips_insn_t instruction)
346 {
347 register fastcall int (*exec)(cpu_mips_t *,mips_insn_t) = NULL;
348 struct mips64_insn_exec_tag *tag;
349 int index;
350
351 #if DEBUG_INSN_PERF_CNT
352 cpu->perf_counter++;
353 #endif
354
355 /* Increment CP0 count register */
356 mips64_exec_inc_cp0_cnt(cpu);
357
358 /* Lookup for instruction */
359 index = ilt_lookup(ilt,instruction);
360 tag = mips64_exec_get_insn(index);
361 exec = tag->exec;
362
363 #if NJM_STATS_ENABLE
364 cpu->insn_exec_count++;
365 mips64_exec_tags[index].count++;
366 #endif
367 #if 0
368 {
369 char buffer[80];
370
371 if (mips64_dump_insn(buffer,sizeof(buffer),0,cpu->pc,instruction)!=-1)
372 cpu_log(cpu->gen,"EXEC","0x%llx: %s\n",cpu->pc,buffer);
373 }
374 #endif
375 return(exec(cpu,instruction));
376 }
377
378 /* Single-step execution */
379 fastcall void mips64_exec_single_step(cpu_mips_t *cpu,mips_insn_t instruction)
380 {
381 int res;
382
383 res = mips64_exec_single_instruction(cpu,instruction);
384
385 /* Normal flow ? */
386 if (likely(!res)) cpu->pc += 4;
387 }
388
389 /* Run MIPS code in step-by-step mode */
390 void *mips64_exec_run_cpu(cpu_gen_t *gen)
391 {
392 cpu_mips_t *cpu = CPU_MIPS64(gen);
393 pthread_t timer_irq_thread;
394 int timer_irq_check = 0;
395 mips_insn_t insn;
396 int res;
397
398 if (pthread_create(&timer_irq_thread,NULL,
399 (void *)mips64_timer_irq_run,cpu))
400 {
401 fprintf(stderr,"VM '%s': unable to create Timer IRQ thread for CPU%u.\n",
402 cpu->vm->name,gen->id);
403 cpu_stop(gen);
404 return NULL;
405 }
406
407 gen->cpu_thread_running = TRUE;
408 cpu_exec_loop_set(gen);
409
410 start_cpu:
411 gen->idle_count = 0;
412
413 for(;;) {
414 if (unlikely(gen->state != CPU_STATE_RUNNING))
415 break;
416
417 /* Handle virtual idle loop */
418 if (unlikely(cpu->pc == cpu->idle_pc)) {
419 if (++gen->idle_count == gen->idle_max) {
420 cpu_idle_loop(gen);
421 gen->idle_count = 0;
422 }
423 }
424
425 /* Handle the virtual CPU clock */
426 if (++timer_irq_check == cpu->timer_irq_check_itv) {
427 timer_irq_check = 0;
428
429 if (cpu->timer_irq_pending && !cpu->irq_disable) {
430 mips64_trigger_timer_irq(cpu);
431 mips64_trigger_irq(cpu);
432 cpu->timer_irq_pending--;
433 }
434 }
435
436 /* Reset "zero register" (for safety) */
437 cpu->gpr[0] = 0;
438
439 /* Check IRQ */
440 if (unlikely(cpu->irq_pending)) {
441 mips64_trigger_irq(cpu);
442 continue;
443 }
444
445 /* Fetch and execute the instruction */
446 mips64_exec_fetch(cpu,cpu->pc,&insn);
447 res = mips64_exec_single_instruction(cpu,insn);
448
449 /* Normal flow ? */
450 if (likely(!res)) cpu->pc += sizeof(mips_insn_t);
451 }
452
453 if (!cpu->pc) {
454 cpu_stop(gen);
455 cpu_log(gen,"SLOW_EXEC","PC=0, halting CPU.\n");
456 }
457
458 /* Check regularly if the CPU has been restarted */
459 while(gen->cpu_thread_running) {
460 gen->seq_state++;
461
462 switch(gen->state) {
463 case CPU_STATE_RUNNING:
464 gen->state = CPU_STATE_RUNNING;
465 goto start_cpu;
466
467 case CPU_STATE_HALTED:
468 gen->cpu_thread_running = FALSE;
469 pthread_join(timer_irq_thread,NULL);
470 break;
471 }
472
473 /* CPU is paused */
474 usleep(200000);
475 }
476
477 return NULL;
478 }
479
480 /* Execute the instruction in delay slot */
481 static forced_inline void mips64_exec_bdslot(cpu_mips_t *cpu)
482 {
483 mips_insn_t insn;
484
485 /* Fetch the instruction in delay slot */
486 mips64_exec_fetch(cpu,cpu->pc+4,&insn);
487
488 /* Execute the instruction */
489 mips64_exec_single_instruction(cpu,insn);
490 }
491
492 /* ADD */
493 static fastcall int mips64_exec_ADD(cpu_mips_t *cpu,mips_insn_t insn)
494 {
495 int rs = bits(insn,21,25);
496 int rt = bits(insn,16,20);
497 int rd = bits(insn,11,15);
498 m_uint64_t res;
499
500 /* TODO: Exception handling */
501 res = (m_uint32_t)cpu->gpr[rs] + (m_uint32_t)cpu->gpr[rt];
502 cpu->gpr[rd] = sign_extend(res,32);
503 return(0);
504 }
505
506 /* ADDI */
507 static fastcall int mips64_exec_ADDI(cpu_mips_t *cpu,mips_insn_t insn)
508 {
509 int rs = bits(insn,21,25);
510 int rt = bits(insn,16,20);
511 int imm = bits(insn,0,15);
512 m_uint32_t res,val = sign_extend(imm,16);
513
514 /* TODO: Exception handling */
515 res = (m_uint32_t)cpu->gpr[rs] + val;
516 cpu->gpr[rt] = sign_extend(res,32);
517 return(0);
518 }
519
520 /* ADDIU */
521 static fastcall int mips64_exec_ADDIU(cpu_mips_t *cpu,mips_insn_t insn)
522 {
523 int rs = bits(insn,21,25);
524 int rt = bits(insn,16,20);
525 int imm = bits(insn,0,15);
526 m_uint32_t res,val = sign_extend(imm,16);
527
528 res = (m_uint32_t)cpu->gpr[rs] + val;
529 cpu->gpr[rt] = sign_extend(res,32);
530 return(0);
531 }
532
533 /* ADDU */
534 static fastcall int mips64_exec_ADDU(cpu_mips_t *cpu,mips_insn_t insn)
535 {
536 int rs = bits(insn,21,25);
537 int rt = bits(insn,16,20);
538 int rd = bits(insn,11,15);
539 m_uint32_t res;
540
541 res = (m_uint32_t)cpu->gpr[rs] + (m_uint32_t)cpu->gpr[rt];
542 cpu->gpr[rd] = sign_extend(res,32);
543 return(0);
544 }
545
546 /* AND */
547 static fastcall int mips64_exec_AND(cpu_mips_t *cpu,mips_insn_t insn)
548 {
549 int rs = bits(insn,21,25);
550 int rt = bits(insn,16,20);
551 int rd = bits(insn,11,15);
552
553 cpu->gpr[rd] = cpu->gpr[rs] & cpu->gpr[rt];
554 return(0);
555 }
556
557 /* ANDI */
558 static fastcall int mips64_exec_ANDI(cpu_mips_t *cpu,mips_insn_t insn)
559 {
560 int rs = bits(insn,21,25);
561 int rt = bits(insn,16,20);
562 int imm = bits(insn,0,15);
563
564 cpu->gpr[rt] = cpu->gpr[rs] & imm;
565 return(0);
566 }
567
568 /* B (Branch, virtual instruction) */
569 static fastcall int mips64_exec_B(cpu_mips_t *cpu,mips_insn_t insn)
570 {
571 int offset = bits(insn,0,15);
572 m_uint64_t new_pc;
573
574 /* compute the new pc */
575 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
576
577 /* exec the instruction in the delay slot */
578 mips64_exec_bdslot(cpu);
579
580 /* set the new pc in cpu structure */
581 cpu->pc = new_pc;
582 return(1);
583 }
584
585 /* BAL (Branch And Link, virtual instruction) */
586 static fastcall int mips64_exec_BAL(cpu_mips_t *cpu,mips_insn_t insn)
587 {
588 int offset = bits(insn,0,15);
589 m_uint64_t new_pc;
590
591 /* compute the new pc */
592 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
593
594 /* set the return address (instruction after the delay slot) */
595 cpu->gpr[MIPS_GPR_RA] = cpu->pc + 8;
596
597 /* exec the instruction in the delay slot */
598 mips64_exec_bdslot(cpu);
599
600 /* set the new pc in cpu structure */
601 cpu->pc = new_pc;
602 return(1);
603 }
604
605 /* BEQ (Branch On Equal) */
606 static fastcall int mips64_exec_BEQ(cpu_mips_t *cpu,mips_insn_t insn)
607 {
608 int rs = bits(insn,21,25);
609 int rt = bits(insn,16,20);
610 int offset = bits(insn,0,15);
611 m_uint64_t new_pc;
612 int res;
613
614 /* compute the new pc */
615 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
616
617 /* take the branch if gpr[rs] == gpr[rt] */
618 res = (cpu->gpr[rs] == cpu->gpr[rt]);
619
620 /* exec the instruction in the delay slot */
621 mips64_exec_bdslot(cpu);
622
623 /* take the branch if the test result is true */
624 if (res)
625 cpu->pc = new_pc;
626 else
627 cpu->pc += 8;
628
629 return(1);
630 }
631
632 /* BEQL (Branch On Equal Likely) */
633 static fastcall int mips64_exec_BEQL(cpu_mips_t *cpu,mips_insn_t insn)
634 {
635 int rs = bits(insn,21,25);
636 int rt = bits(insn,16,20);
637 int offset = bits(insn,0,15);
638 m_uint64_t new_pc;
639 int res;
640
641 /* compute the new pc */
642 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
643
644 /* take the branch if gpr[rs] == gpr[rt] */
645 res = (cpu->gpr[rs] == cpu->gpr[rt]);
646
647 /* take the branch if the test result is true */
648 if (res) {
649 mips64_exec_bdslot(cpu);
650 cpu->pc = new_pc;
651 } else
652 cpu->pc += 8;
653
654 return(1);
655 }
656
657 /* BEQZ (Branch On Equal Zero) - Virtual Instruction */
658 static fastcall int mips64_exec_BEQZ(cpu_mips_t *cpu,mips_insn_t insn)
659 {
660 int rs = bits(insn,21,25);
661 int offset = bits(insn,0,15);
662 m_uint64_t new_pc;
663 int res;
664
665 /* compute the new pc */
666 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
667
668 /* take the branch if gpr[rs] == 0 */
669 res = (cpu->gpr[rs] == 0);
670
671 /* exec the instruction in the delay slot */
672 mips64_exec_bdslot(cpu);
673
674 /* take the branch if the test result is true */
675 if (res)
676 cpu->pc = new_pc;
677 else
678 cpu->pc += 8;
679
680 return(1);
681 }
682
683 /* BNEZ (Branch On Not Equal Zero) - Virtual Instruction */
684 static fastcall int mips64_exec_BNEZ(cpu_mips_t *cpu,mips_insn_t insn)
685 {
686 int rs = bits(insn,21,25);
687 int offset = bits(insn,0,15);
688 m_uint64_t new_pc;
689 int res;
690
691 /* compute the new pc */
692 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
693
694 /* take the branch if gpr[rs] != 0 */
695 res = (cpu->gpr[rs] != 0);
696
697 /* exec the instruction in the delay slot */
698 mips64_exec_bdslot(cpu);
699
700 /* take the branch if the test result is true */
701 if (res)
702 cpu->pc = new_pc;
703 else
704 cpu->pc += 8;
705
706 return(1);
707 }
708
709 /* BGEZ (Branch On Greater or Equal Than Zero) */
710 static fastcall int mips64_exec_BGEZ(cpu_mips_t *cpu,mips_insn_t insn)
711 {
712 int rs = bits(insn,21,25);
713 int offset = bits(insn,0,15);
714 m_uint64_t new_pc;
715 int res;
716
717 /* compute the new pc */
718 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
719
720 /* take the branch if gpr[rs] >= 0 */
721 res = ((m_int64_t)cpu->gpr[rs] >= 0);
722
723 /* exec the instruction in the delay slot */
724 mips64_exec_bdslot(cpu);
725
726 /* take the branch if the test result is true */
727 if (res)
728 cpu->pc = new_pc;
729 else
730 cpu->pc += 8;
731
732 return(1);
733 }
734
735 /* BGEZAL (Branch On Greater or Equal Than Zero And Link) */
736 static fastcall int mips64_exec_BGEZAL(cpu_mips_t *cpu,mips_insn_t insn)
737 {
738 int rs = bits(insn,21,25);
739 int offset = bits(insn,0,15);
740 m_uint64_t new_pc;
741 int res;
742
743 /* compute the new pc */
744 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
745
746 /* set the return address (instruction after the delay slot) */
747 cpu->gpr[MIPS_GPR_RA] = cpu->pc + 8;
748
749 /* take the branch if gpr[rs] >= 0 */
750 res = ((m_int64_t)cpu->gpr[rs] >= 0);
751
752 /* exec the instruction in the delay slot */
753 mips64_exec_bdslot(cpu);
754
755 /* take the branch if the test result is true */
756 if (res)
757 cpu->pc = new_pc;
758 else
759 cpu->pc += 8;
760
761 return(1);
762 }
763
764 /* BGEZALL (Branch On Greater or Equal Than Zero And Link Likely) */
765 static fastcall int mips64_exec_BGEZALL(cpu_mips_t *cpu,mips_insn_t insn)
766 {
767 int rs = bits(insn,21,25);
768 int offset = bits(insn,0,15);
769 m_uint64_t new_pc;
770 int res;
771
772 /* compute the new pc */
773 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
774
775 /* set the return address (instruction after the delay slot) */
776 cpu->gpr[MIPS_GPR_RA] = cpu->pc + 8;
777
778 /* take the branch if gpr[rs] >= 0 */
779 res = ((m_int64_t)cpu->gpr[rs] >= 0);
780
781 /* take the branch if the test result is true */
782 if (res) {
783 mips64_exec_bdslot(cpu);
784 cpu->pc = new_pc;
785 } else
786 cpu->pc += 8;
787
788 return(1);
789 }
790
791 /* BGEZL (Branch On Greater or Equal Than Zero Likely) */
792 static fastcall int mips64_exec_BGEZL(cpu_mips_t *cpu,mips_insn_t insn)
793 {
794 int rs = bits(insn,21,25);
795 int offset = bits(insn,0,15);
796 m_uint64_t new_pc;
797 int res;
798
799 /* compute the new pc */
800 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
801
802 /* take the branch if gpr[rs] >= 0 */
803 res = ((m_int64_t)cpu->gpr[rs] >= 0);
804
805 /* take the branch if the test result is true */
806 if (res) {
807 mips64_exec_bdslot(cpu);
808 cpu->pc = new_pc;
809 } else
810 cpu->pc += 8;
811
812 return(1);
813 }
814
815 /* BGTZ (Branch On Greater Than Zero) */
816 static fastcall int mips64_exec_BGTZ(cpu_mips_t *cpu,mips_insn_t insn)
817 {
818 int rs = bits(insn,21,25);
819 int offset = bits(insn,0,15);
820 m_uint64_t new_pc;
821 int res;
822
823 /* compute the new pc */
824 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
825
826 /* take the branch if gpr[rs] > 0 */
827 res = ((m_int64_t)cpu->gpr[rs] > 0);
828
829 /* exec the instruction in the delay slot */
830 mips64_exec_bdslot(cpu);
831
832 /* take the branch if the test result is true */
833 if (res)
834 cpu->pc = new_pc;
835 else
836 cpu->pc += 8;
837
838 return(1);
839 }
840
841 /* BGTZL (Branch On Greater Than Zero Likely) */
842 static fastcall int mips64_exec_BGTZL(cpu_mips_t *cpu,mips_insn_t insn)
843 {
844 int rs = bits(insn,21,25);
845 int offset = bits(insn,0,15);
846 m_uint64_t new_pc;
847 int res;
848
849 /* compute the new pc */
850 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
851
852 /* take the branch if gpr[rs] > 0 */
853 res = ((m_int64_t)cpu->gpr[rs] > 0);
854
855 /* take the branch if the test result is true */
856 if (res) {
857 mips64_exec_bdslot(cpu);
858 cpu->pc = new_pc;
859 } else
860 cpu->pc += 8;
861
862 return(1);
863 }
864
865 /* BLEZ (Branch On Less or Equal Than Zero) */
866 static fastcall int mips64_exec_BLEZ(cpu_mips_t *cpu,mips_insn_t insn)
867 {
868 int rs = bits(insn,21,25);
869 int offset = bits(insn,0,15);
870 m_uint64_t new_pc;
871 int res;
872
873 /* compute the new pc */
874 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
875
876 /* take the branch if gpr[rs] <= 0 */
877 res = ((m_int64_t)cpu->gpr[rs] <= 0);
878
879 /* exec the instruction in the delay slot */
880 mips64_exec_bdslot(cpu);
881
882 /* take the branch if the test result is true */
883 if (res)
884 cpu->pc = new_pc;
885 else
886 cpu->pc += 8;
887
888 return(1);
889 }
890
891 /* BLEZL (Branch On Less or Equal Than Zero Likely) */
892 static fastcall int mips64_exec_BLEZL(cpu_mips_t *cpu,mips_insn_t insn)
893 {
894 int rs = bits(insn,21,25);
895 int offset = bits(insn,0,15);
896 m_uint64_t new_pc;
897 int res;
898
899 /* compute the new pc */
900 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
901
902 /* take the branch if gpr[rs] <= 0 */
903 res = ((m_int64_t)cpu->gpr[rs] <= 0);
904
905 /* take the branch if the test result is true */
906 if (res) {
907 mips64_exec_bdslot(cpu);
908 cpu->pc = new_pc;
909 } else
910 cpu->pc += 8;
911
912 return(1);
913 }
914
915 /* BLTZ (Branch On Less Than Zero) */
916 static fastcall int mips64_exec_BLTZ(cpu_mips_t *cpu,mips_insn_t insn)
917 {
918 int rs = bits(insn,21,25);
919 int offset = bits(insn,0,15);
920 m_uint64_t new_pc;
921 int res;
922
923 /* compute the new pc */
924 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
925
926 /* take the branch if gpr[rs] < 0 */
927 res = ((m_int64_t)cpu->gpr[rs] < 0);
928
929 /* exec the instruction in the delay slot */
930 mips64_exec_bdslot(cpu);
931
932 /* take the branch if the test result is true */
933 if (res)
934 cpu->pc = new_pc;
935 else
936 cpu->pc += 8;
937
938 return(1);
939 }
940
941 /* BLTZAL (Branch On Less Than Zero And Link) */
942 static fastcall int mips64_exec_BLTZAL(cpu_mips_t *cpu,mips_insn_t insn)
943 {
944 int rs = bits(insn,21,25);
945 int offset = bits(insn,0,15);
946 m_uint64_t new_pc;
947 int res;
948
949 /* compute the new pc */
950 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
951
952 /* set the return address (instruction after the delay slot) */
953 cpu->gpr[MIPS_GPR_RA] = cpu->pc + 8;
954
955 /* take the branch if gpr[rs] < 0 */
956 res = ((m_int64_t)cpu->gpr[rs] < 0);
957
958 /* exec the instruction in the delay slot */
959 mips64_exec_bdslot(cpu);
960
961 /* take the branch if the test result is true */
962 if (res)
963 cpu->pc = new_pc;
964 else
965 cpu->pc += 8;
966
967 return(1);
968 }
969
970 /* BLTZALL (Branch On Less Than Zero And Link Likely) */
971 static fastcall int mips64_exec_BLTZALL(cpu_mips_t *cpu,mips_insn_t insn)
972 {
973 int rs = bits(insn,21,25);
974 int offset = bits(insn,0,15);
975 m_uint64_t new_pc;
976 int res;
977
978 /* compute the new pc */
979 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
980
981 /* set the return address (instruction after the delay slot) */
982 cpu->gpr[MIPS_GPR_RA] = cpu->pc + 8;
983
984 /* take the branch if gpr[rs] < 0 */
985 res = ((m_int64_t)cpu->gpr[rs] < 0);
986
987 /* take the branch if the test result is true */
988 if (res) {
989 mips64_exec_bdslot(cpu);
990 cpu->pc = new_pc;
991 } else
992 cpu->pc += 8;
993
994 return(1);
995 }
996
997 /* BLTZL (Branch On Less Than Zero Likely) */
998 static fastcall int mips64_exec_BLTZL(cpu_mips_t *cpu,mips_insn_t insn)
999 {
1000 int rs = bits(insn,21,25);
1001 int offset = bits(insn,0,15);
1002 m_uint64_t new_pc;
1003 int res;
1004
1005 /* compute the new pc */
1006 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
1007
1008 /* take the branch if gpr[rs] < 0 */
1009 res = ((m_int64_t)cpu->gpr[rs] < 0);
1010
1011 /* take the branch if the test result is true */
1012 if (res) {
1013 mips64_exec_bdslot(cpu);
1014 cpu->pc = new_pc;
1015 } else
1016 cpu->pc += 8;
1017
1018 return(1);
1019 }
1020
1021 /* BNE (Branch On Not Equal) */
1022 static fastcall int mips64_exec_BNE(cpu_mips_t *cpu,mips_insn_t insn)
1023 {
1024 int rs = bits(insn,21,25);
1025 int rt = bits(insn,16,20);
1026 int offset = bits(insn,0,15);
1027 m_uint64_t new_pc;
1028 int res;
1029
1030 /* compute the new pc */
1031 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
1032
1033 /* take the branch if gpr[rs] != gpr[rt] */
1034 res = (cpu->gpr[rs] != cpu->gpr[rt]);
1035
1036 /* exec the instruction in the delay slot */
1037 mips64_exec_bdslot(cpu);
1038
1039 /* take the branch if the test result is true */
1040 if (res)
1041 cpu->pc = new_pc;
1042 else
1043 cpu->pc += 8;
1044
1045 return(1);
1046 }
1047
1048 /* BNEL (Branch On Not Equal Likely) */
1049 static fastcall int mips64_exec_BNEL(cpu_mips_t *cpu,mips_insn_t insn)
1050 {
1051 int rs = bits(insn,21,25);
1052 int rt = bits(insn,16,20);
1053 int offset = bits(insn,0,15);
1054 m_uint64_t new_pc;
1055 int res;
1056
1057 /* compute the new pc */
1058 new_pc = (cpu->pc + 4) + sign_extend(offset << 2,18);
1059
1060 /* take the branch if gpr[rs] != gpr[rt] */
1061 res = (cpu->gpr[rs] != cpu->gpr[rt]);
1062
1063 /* take the branch if the test result is true */
1064 if (res) {
1065 mips64_exec_bdslot(cpu);
1066 cpu->pc = new_pc;
1067 } else
1068 cpu->pc += 8;
1069
1070 return(1);
1071 }
1072
1073 /* BREAK */
1074 static fastcall int mips64_exec_BREAK(cpu_mips_t *cpu,mips_insn_t insn)
1075 {
1076 u_int code = bits(insn,6,25);
1077
1078 mips64_exec_break(cpu,code);
1079 return(1);
1080 }
1081
1082 /* CACHE */
1083 static fastcall int mips64_exec_CACHE(cpu_mips_t *cpu,mips_insn_t insn)
1084 {
1085 int base = bits(insn,21,25);
1086 int op = bits(insn,16,20);
1087 int offset = bits(insn,0,15);
1088
1089 mips64_exec_memop2(cpu,MIPS_MEMOP_CACHE,base,offset,op,FALSE);
1090 return(0);
1091 }
1092
1093 /* CFC0 */
1094 static fastcall int mips64_exec_CFC0(cpu_mips_t *cpu,mips_insn_t insn)
1095 {
1096 int rt = bits(insn,16,20);
1097 int rd = bits(insn,11,15);
1098
1099 mips64_cp0_exec_cfc0(cpu,rt,rd);
1100 return(0);
1101 }
1102
1103 /* CTC0 */
1104 static fastcall int mips64_exec_CTC0(cpu_mips_t *cpu,mips_insn_t insn)
1105 {
1106 int rt = bits(insn,16,20);
1107 int rd = bits(insn,11,15);
1108
1109 mips64_cp0_exec_ctc0(cpu,rt,rd);
1110 return(0);
1111 }
1112
1113 /* DADDIU */
1114 static fastcall int mips64_exec_DADDIU(cpu_mips_t *cpu,mips_insn_t insn)
1115 {
1116 int rs = bits(insn,21,25);
1117 int rt = bits(insn,16,20);
1118 int imm = bits(insn,0,15);
1119 m_uint64_t val = sign_extend(imm,16);
1120
1121 cpu->gpr[rt] = cpu->gpr[rs] + val;
1122 return(0);
1123 }
1124
1125 /* DADDU: rd = rs + rt */
1126 static fastcall int mips64_exec_DADDU(cpu_mips_t *cpu,mips_insn_t insn)
1127 {
1128 int rs = bits(insn,21,25);
1129 int rt = bits(insn,16,20);
1130 int rd = bits(insn,11,15);
1131
1132 cpu->gpr[rd] = cpu->gpr[rs] + cpu->gpr[rt];
1133 return(0);
1134 }
1135
1136 /* DIV */
1137 static fastcall int mips64_exec_DIV(cpu_mips_t *cpu,mips_insn_t insn)
1138 {
1139 int rs = bits(insn,21,25);
1140 int rt = bits(insn,16,20);
1141
1142 cpu->lo = (m_int32_t)cpu->gpr[rs] / (m_int32_t)cpu->gpr[rt];
1143 cpu->hi = (m_int32_t)cpu->gpr[rs] % (m_int32_t)cpu->gpr[rt];
1144
1145 cpu->lo = sign_extend(cpu->lo,32);
1146 cpu->hi = sign_extend(cpu->hi,32);
1147 return(0);
1148 }
1149
1150 /* DIVU */
1151 static fastcall int mips64_exec_DIVU(cpu_mips_t *cpu,mips_insn_t insn)
1152 {
1153 int rs = bits(insn,21,25);
1154 int rt = bits(insn,16,20);
1155
1156 if (cpu->gpr[rt] == 0)
1157 return(0);
1158
1159 cpu->lo = (m_uint32_t)cpu->gpr[rs] / (m_uint32_t)cpu->gpr[rt];
1160 cpu->hi = (m_uint32_t)cpu->gpr[rs] % (m_uint32_t)cpu->gpr[rt];
1161
1162 cpu->lo = sign_extend(cpu->lo,32);
1163 cpu->hi = sign_extend(cpu->hi,32);
1164 return(0);
1165 }
1166
1167 /* DMFC0 */
1168 static fastcall int mips64_exec_DMFC0(cpu_mips_t *cpu,mips_insn_t insn)
1169 {
1170 int rt = bits(insn,16,20);
1171 int rd = bits(insn,11,15);
1172
1173 mips64_cp0_exec_dmfc0(cpu,rt,rd);
1174 return(0);
1175 }
1176
1177 /* DMFC1 */
1178 static fastcall int mips64_exec_DMFC1(cpu_mips_t *cpu,mips_insn_t insn)
1179 {
1180 int rt = bits(insn,16,20);
1181 int rd = bits(insn,11,15);
1182
1183 mips64_exec_dmfc1(cpu,rt,rd);
1184 return(0);
1185 }
1186
1187 /* DMTC0 */
1188 static fastcall int mips64_exec_DMTC0(cpu_mips_t *cpu,mips_insn_t insn)
1189 {
1190 int rt = bits(insn,16,20);
1191 int rd = bits(insn,11,15);
1192
1193 mips64_cp0_exec_dmtc0(cpu,rt,rd);
1194 return(0);
1195 }
1196
1197 /* DMTC1 */
1198 static fastcall int mips64_exec_DMTC1(cpu_mips_t *cpu,mips_insn_t insn)
1199 {
1200 int rt = bits(insn,16,20);
1201 int rd = bits(insn,11,15);
1202
1203 mips64_exec_dmtc1(cpu,rt,rd);
1204 return(0);
1205 }
1206
1207 /* DSLL */
1208 static fastcall int mips64_exec_DSLL(cpu_mips_t *cpu,mips_insn_t insn)
1209 {
1210 int rt = bits(insn,16,20);
1211 int rd = bits(insn,11,15);
1212 int sa = bits(insn,6,10);
1213
1214 cpu->gpr[rd] = cpu->gpr[rt] << sa;
1215 return(0);
1216 }
1217
1218 /* DSLL32 */
1219 static fastcall int mips64_exec_DSLL32(cpu_mips_t *cpu,mips_insn_t insn)
1220 {
1221 int rt = bits(insn,16,20);
1222 int rd = bits(insn,11,15);
1223 int sa = bits(insn,6,10);
1224
1225 cpu->gpr[rd] = cpu->gpr[rt] << (32 + sa);
1226 return(0);
1227 }
1228
1229 /* DSLLV */
1230 static fastcall int mips64_exec_DSLLV(cpu_mips_t *cpu,mips_insn_t insn)
1231 {
1232 int rs = bits(insn,21,25);
1233 int rt = bits(insn,16,20);
1234 int rd = bits(insn,11,15);
1235
1236 cpu->gpr[rd] = cpu->gpr[rt] << (cpu->gpr[rs] & 0x3f);
1237 return(0);
1238 }
1239
1240 /* DSRA */
1241 static fastcall int mips64_exec_DSRA(cpu_mips_t *cpu,mips_insn_t insn)
1242 {
1243 int rt = bits(insn,16,20);
1244 int rd = bits(insn,11,15);
1245 int sa = bits(insn,6,10);
1246
1247 cpu->gpr[rd] = (m_int64_t)cpu->gpr[rt] >> sa;
1248 return(0);
1249 }
1250
1251 /* DSRA32 */
1252 static fastcall int mips64_exec_DSRA32(cpu_mips_t *cpu,mips_insn_t insn)
1253 {
1254 int rt = bits(insn,16,20);
1255 int rd = bits(insn,11,15);
1256 int sa = bits(insn,6,10);
1257
1258 cpu->gpr[rd] = (m_int64_t)cpu->gpr[rt] >> (32 + sa);
1259 return(0);
1260 }
1261
1262 /* DSRAV */
1263 static fastcall int mips64_exec_DSRAV(cpu_mips_t *cpu,mips_insn_t insn)
1264 {
1265 int rs = bits(insn,21,25);
1266 int rt = bits(insn,16,20);
1267 int rd = bits(insn,11,15);
1268
1269 cpu->gpr[rd] = (m_int64_t)cpu->gpr[rt] >> (cpu->gpr[rs] & 0x3f);
1270 return(0);
1271 }
1272
1273 /* DSRL */
1274 static fastcall int mips64_exec_DSRL(cpu_mips_t *cpu,mips_insn_t insn)
1275 {
1276 int rt = bits(insn,16,20);
1277 int rd = bits(insn,11,15);
1278 int sa = bits(insn,6,10);
1279
1280 cpu->gpr[rd] = cpu->gpr[rt] >> sa;
1281 return(0);
1282 }
1283
1284 /* DSRL32 */
1285 static fastcall int mips64_exec_DSRL32(cpu_mips_t *cpu,mips_insn_t insn)
1286 {
1287 int rt = bits(insn,16,20);
1288 int rd = bits(insn,11,15);
1289 int sa = bits(insn,6,10);
1290
1291 cpu->gpr[rd] = cpu->gpr[rt] >> (32 + sa);
1292 return(0);
1293 }
1294
1295 /* DSRLV */
1296 static fastcall int mips64_exec_DSRLV(cpu_mips_t *cpu,mips_insn_t insn)
1297 {
1298 int rs = bits(insn,21,25);
1299 int rt = bits(insn,16,20);
1300 int rd = bits(insn,11,15);
1301
1302 cpu->gpr[rd] = cpu->gpr[rt] >> (cpu->gpr[rs] & 0x3f);
1303 return(0);
1304 }
1305
1306 /* DSUBU */
1307 static fastcall int mips64_exec_DSUBU(cpu_mips_t *cpu,mips_insn_t insn)
1308 {
1309 int rs = bits(insn,21,25);
1310 int rt = bits(insn,16,20);
1311 int rd = bits(insn,11,15);
1312
1313 cpu->gpr[rd] = cpu->gpr[rs] - cpu->gpr[rt];
1314 return(0);
1315 }
1316
1317 /* ERET */
1318 static fastcall int mips64_exec_ERET(cpu_mips_t *cpu,mips_insn_t insn)
1319 {
1320 mips64_exec_eret(cpu);
1321 return(1);
1322 }
1323
1324 /* J */
1325 static fastcall int mips64_exec_J(cpu_mips_t *cpu,mips_insn_t insn)
1326 {
1327 u_int instr_index = bits(insn,0,25);
1328 m_uint64_t new_pc;
1329
1330 /* compute the new pc */
1331 new_pc = cpu->pc & ~((1 << 28) - 1);
1332 new_pc |= instr_index << 2;
1333
1334 /* exec the instruction in the delay slot */
1335 mips64_exec_bdslot(cpu);
1336
1337 /* set the new pc */
1338 cpu->pc = new_pc;
1339 return(1);
1340 }
1341
1342 /* JAL */
1343 static fastcall int mips64_exec_JAL(cpu_mips_t *cpu,mips_insn_t insn)
1344 {
1345 u_int instr_index = bits(insn,0,25);
1346 m_uint64_t new_pc;
1347
1348 /* compute the new pc */
1349 new_pc = cpu->pc & ~((1 << 28) - 1);
1350 new_pc |= instr_index << 2;
1351
1352 /* set the return address (instruction after the delay slot) */
1353 cpu->gpr[MIPS_GPR_RA] = cpu->pc + 8;
1354
1355 /* exec the instruction in the delay slot */
1356 mips64_exec_bdslot(cpu);
1357
1358 /* set the new pc */
1359 cpu->pc = new_pc;
1360 return(1);
1361 }
1362
1363 /* JALR */
1364 static fastcall int mips64_exec_JALR(cpu_mips_t *cpu,mips_insn_t insn)
1365 {
1366 int rs = bits(insn,21,25);
1367 int rd = bits(insn,11,15);
1368 m_uint64_t new_pc;
1369
1370 /* set the return pc (instruction after the delay slot) in GPR[rd] */
1371 cpu->gpr[rd] = cpu->pc + 8;
1372
1373 /* get the new pc */
1374 new_pc = cpu->gpr[rs];
1375
1376 /* exec the instruction in the delay slot */
1377 mips64_exec_bdslot(cpu);
1378
1379 /* set the new pc */
1380 cpu->pc = new_pc;
1381 return(1);
1382 }
1383
1384 /* JR */
1385 static fastcall int mips64_exec_JR(cpu_mips_t *cpu,mips_insn_t insn)
1386 {
1387 int rs = bits(insn,21,25);
1388 m_uint64_t new_pc;
1389
1390 /* get the new pc */
1391 new_pc = cpu->gpr[rs];
1392
1393 /* exec the instruction in the delay slot */
1394 mips64_exec_bdslot(cpu);
1395
1396 /* set the new pc */
1397 cpu->pc = new_pc;
1398 return(1);
1399 }
1400
1401 /* LB (Load Byte) */
1402 static fastcall int mips64_exec_LB(cpu_mips_t *cpu,mips_insn_t insn)
1403 {
1404 int base = bits(insn,21,25);
1405 int rt = bits(insn,16,20);
1406 int offset = bits(insn,0,15);
1407
1408 mips64_exec_memop2(cpu,MIPS_MEMOP_LB,base,offset,rt,TRUE);
1409 return(0);
1410 }
1411
1412 /* LBU (Load Byte Unsigned) */
1413 static fastcall int mips64_exec_LBU(cpu_mips_t *cpu,mips_insn_t insn)
1414 {
1415 int base = bits(insn,21,25);
1416 int rt = bits(insn,16,20);
1417 int offset = bits(insn,0,15);
1418
1419 mips64_exec_memop2(cpu,MIPS_MEMOP_LBU,base,offset,rt,TRUE);
1420 return(0);
1421 }
1422
1423 /* LD (Load Double-Word) */
1424 static fastcall int mips64_exec_LD(cpu_mips_t *cpu,mips_insn_t insn)
1425 {
1426 int base = bits(insn,21,25);
1427 int rt = bits(insn,16,20);
1428 int offset = bits(insn,0,15);
1429
1430 mips64_exec_memop2(cpu,MIPS_MEMOP_LD,base,offset,rt,TRUE);
1431 return(0);
1432 }
1433
1434 /* LDC1 (Load Double-Word to Coprocessor 1) */
1435 static fastcall int mips64_exec_LDC1(cpu_mips_t *cpu,mips_insn_t insn)
1436 {
1437 int base = bits(insn,21,25);
1438 int ft = bits(insn,16,20);
1439 int offset = bits(insn,0,15);
1440
1441 mips64_exec_memop2(cpu,MIPS_MEMOP_LDC1,base,offset,ft,TRUE);
1442 return(0);
1443 }
1444
1445 /* LDL (Load Double-Word Left) */
1446 static fastcall int mips64_exec_LDL(cpu_mips_t *cpu,mips_insn_t insn)
1447 {
1448 int base = bits(insn,21,25);
1449 int rt = bits(insn,16,20);
1450 int offset = bits(insn,0,15);
1451
1452 mips64_exec_memop2(cpu,MIPS_MEMOP_LDL,base,offset,rt,TRUE);
1453 return(0);
1454 }
1455
1456 /* LDR (Load Double-Word Right) */
1457 static fastcall int mips64_exec_LDR(cpu_mips_t *cpu,mips_insn_t insn)
1458 {
1459 int base = bits(insn,21,25);
1460 int rt = bits(insn,16,20);
1461 int offset = bits(insn,0,15);
1462
1463 mips64_exec_memop2(cpu,MIPS_MEMOP_LDR,base,offset,rt,TRUE);
1464 return(0);
1465 }
1466
1467 /* LH (Load Half-Word) */
1468 static fastcall int mips64_exec_LH(cpu_mips_t *cpu,mips_insn_t insn)
1469 {
1470 int base = bits(insn,21,25);
1471 int rt = bits(insn,16,20);
1472 int offset = bits(insn,0,15);
1473
1474 mips64_exec_memop2(cpu,MIPS_MEMOP_LH,base,offset,rt,TRUE);
1475 return(0);
1476 }
1477
1478 /* LHU (Load Half-Word Unsigned) */
1479 static fastcall int mips64_exec_LHU(cpu_mips_t *cpu,mips_insn_t insn)
1480 {
1481 int base = bits(insn,21,25);
1482 int rt = bits(insn,16,20);
1483 int offset = bits(insn,0,15);
1484
1485 mips64_exec_memop2(cpu,MIPS_MEMOP_LHU,base,offset,rt,TRUE);
1486 return(0);
1487 }
1488
1489 /* LI (virtual) */
1490 static fastcall int mips64_exec_LI(cpu_mips_t *cpu,mips_insn_t insn)
1491 {
1492 int rt = bits(insn,16,20);
1493 int imm = bits(insn,0,15);
1494
1495 cpu->gpr[rt] = sign_extend(imm,16);
1496 return(0);
1497 }
1498
1499 /* LL (Load Linked) */
1500 static fastcall int mips64_exec_LL(cpu_mips_t *cpu,mips_insn_t insn)
1501 {
1502 int base = bits(insn,21,25);
1503 int rt = bits(insn,16,20);
1504 int offset = bits(insn,0,15);
1505
1506 mips64_exec_memop2(cpu,MIPS_MEMOP_LL,base,offset,rt,TRUE);
1507 return(0);
1508 }
1509
1510 /* LUI */
1511 static fastcall int mips64_exec_LUI(cpu_mips_t *cpu,mips_insn_t insn)
1512 {
1513 int rt = bits(insn,16,20);
1514 int imm = bits(insn,0,15);
1515
1516 cpu->gpr[rt] = sign_extend(imm,16) << 16;
1517 return(0);
1518 }
1519
1520 /* LW (Load Word) */
1521 static fastcall int mips64_exec_LW(cpu_mips_t *cpu,mips_insn_t insn)
1522 {
1523 int base = bits(insn,21,25);
1524 int rt = bits(insn,16,20);
1525 int offset = bits(insn,0,15);
1526
1527 mips64_exec_memop2(cpu,MIPS_MEMOP_LW,base,offset,rt,TRUE);
1528 return(0);
1529 }
1530
1531 /* LWL (Load Word Left) */
1532 static fastcall int mips64_exec_LWL(cpu_mips_t *cpu,mips_insn_t insn)
1533 {
1534 int base = bits(insn,21,25);
1535 int rt = bits(insn,16,20);
1536 int offset = bits(insn,0,15);
1537
1538 mips64_exec_memop2(cpu,MIPS_MEMOP_LWL,base,offset,rt,TRUE);
1539 return(0);
1540 }
1541
1542 /* LWR (Load Word Right) */
1543 static fastcall int mips64_exec_LWR(cpu_mips_t *cpu,mips_insn_t insn)
1544 {
1545 int base = bits(insn,21,25);
1546 int rt = bits(insn,16,20);
1547 int offset = bits(insn,0,15);
1548
1549 mips64_exec_memop2(cpu,MIPS_MEMOP_LWR,base,offset,rt,TRUE);
1550 return(0);
1551 }
1552
1553 /* LWU (Load Word Unsigned) */
1554 static fastcall int mips64_exec_LWU(cpu_mips_t *cpu,mips_insn_t insn)
1555 {
1556 int base = bits(insn,21,25);
1557 int rt = bits(insn,16,20);
1558 int offset = bits(insn,0,15);
1559
1560 mips64_exec_memop2(cpu,MIPS_MEMOP_LWU,base,offset,rt,TRUE);
1561 return(0);
1562 }
1563
1564 /* MFC0 */
1565 static fastcall int mips64_exec_MFC0(cpu_mips_t *cpu,mips_insn_t insn)
1566 {
1567 int rt = bits(insn,16,20);
1568 int rd = bits(insn,11,15);
1569
1570 mips64_cp0_exec_mfc0(cpu,rt,rd);
1571 return(0);
1572 }
1573
1574 /* MFC1 */
1575 static fastcall int mips64_exec_MFC1(cpu_mips_t *cpu,mips_insn_t insn)
1576 {
1577 int rt = bits(insn,16,20);
1578 int rd = bits(insn,11,15);
1579
1580 mips64_exec_mfc1(cpu,rt,rd);
1581 return(0);
1582 }
1583
1584 /* MFHI */
1585 static fastcall int mips64_exec_MFHI(cpu_mips_t *cpu,mips_insn_t insn)
1586 {
1587 int rd = bits(insn,11,15);
1588
1589 if (rd) cpu->gpr[rd] = cpu->hi;
1590 return(0);
1591 }
1592
1593 /* MFLO */
1594 static fastcall int mips64_exec_MFLO(cpu_mips_t *cpu,mips_insn_t insn)
1595 {
1596 int rd = bits(insn,11,15);
1597
1598 if (rd) cpu->gpr[rd] = cpu->lo;
1599 return(0);
1600 }
1601
1602 /* MOVE (virtual instruction, real: ADDU) */
1603 static fastcall int mips64_exec_MOVE(cpu_mips_t *cpu,mips_insn_t insn)
1604 {
1605 int rs = bits(insn,21,25);
1606 int rd = bits(insn,11,15);
1607
1608 cpu->gpr[rd] = sign_extend(cpu->gpr[rs],32);
1609 return(0);
1610 }
1611
1612 /* MTC0 */
1613 static fastcall int mips64_exec_MTC0(cpu_mips_t *cpu,mips_insn_t insn)
1614 {
1615 int rt = bits(insn,16,20);
1616 int rd = bits(insn,11,15);
1617
1618 mips64_cp0_exec_mtc0(cpu,rt,rd);
1619 return(0);
1620 }
1621
1622 /* MTC1 */
1623 static fastcall int mips64_exec_MTC1(cpu_mips_t *cpu,mips_insn_t insn)
1624 {
1625 int rt = bits(insn,16,20);
1626 int rd = bits(insn,11,15);
1627
1628 mips64_exec_mtc1(cpu,rt,rd);
1629 return(0);
1630 }
1631
1632 /* MTHI */
1633 static fastcall int mips64_exec_MTHI(cpu_mips_t *cpu,mips_insn_t insn)
1634 {
1635 int rs = bits(insn,21,25);
1636
1637 cpu->hi = cpu->gpr[rs];
1638 return(0);
1639 }
1640
1641 /* MTLO */
1642 static fastcall int mips64_exec_MTLO(cpu_mips_t *cpu,mips_insn_t insn)
1643 {
1644 int rs = bits(insn,21,25);
1645
1646 cpu->lo = cpu->gpr[rs];
1647 return(0);
1648 }
1649
1650 /* MUL */
1651 static fastcall int mips64_exec_MUL(cpu_mips_t *cpu,mips_insn_t insn)
1652 {
1653 int rs = bits(insn,21,25);
1654 int rt = bits(insn,16,20);
1655 int rd = bits(insn,11,15);
1656 m_int32_t val;
1657
1658 /* note: after this instruction, HI/LO regs are undefined */
1659 val = (m_int32_t)cpu->gpr[rs] * (m_int32_t)cpu->gpr[rt];
1660 cpu->gpr[rd] = sign_extend(val,32);
1661 return(0);
1662 }
1663
1664 /* MULT */
1665 static fastcall int mips64_exec_MULT(cpu_mips_t *cpu,mips_insn_t insn)
1666 {
1667 int rs = bits(insn,21,25);
1668 int rt = bits(insn,16,20);
1669 m_int64_t val;
1670
1671 val = (m_int64_t)(m_int32_t)cpu->gpr[rs];
1672 val *= (m_int64_t)(m_int32_t)cpu->gpr[rt];
1673
1674 cpu->lo = sign_extend(val,32);
1675 cpu->hi = sign_extend(val >> 32,32);
1676 return(0);
1677 }
1678
1679 /* MULTU */
1680 static fastcall int mips64_exec_MULTU(cpu_mips_t *cpu,mips_insn_t insn)
1681 {
1682 int rs = bits(insn,21,25);
1683 int rt = bits(insn,16,20);
1684 m_uint64_t val;
1685
1686 val = (m_uint64_t)(m_uint32_t)cpu->gpr[rs];
1687 val *= (m_uint64_t)(m_uint32_t)cpu->gpr[rt];
1688 cpu->lo = sign_extend(val,32);
1689 cpu->hi = sign_extend(val >> 32,32);
1690 return(0);
1691 }
1692
1693 /* NOP */
1694 static fastcall int mips64_exec_NOP(cpu_mips_t *cpu,mips_insn_t insn)
1695 {
1696 return(0);
1697 }
1698
1699 /* NOR */
1700 static fastcall int mips64_exec_NOR(cpu_mips_t *cpu,mips_insn_t insn)
1701 {
1702 int rs = bits(insn,21,25);
1703 int rt = bits(insn,16,20);
1704 int rd = bits(insn,11,15);
1705
1706 cpu->gpr[rd] = ~(cpu->gpr[rs] | cpu->gpr[rt]);
1707 return(0);
1708 }
1709
1710 /* OR */
1711 static fastcall int mips64_exec_OR(cpu_mips_t *cpu,mips_insn_t insn)
1712 {
1713 int rs = bits(insn,21,25);
1714 int rt = bits(insn,16,20);
1715 int rd = bits(insn,11,15);
1716
1717 cpu->gpr[rd] = cpu->gpr[rs] | cpu->gpr[rt];
1718 return(0);
1719 }
1720
1721 /* ORI */
1722 static fastcall int mips64_exec_ORI(cpu_mips_t *cpu,mips_insn_t insn)
1723 {
1724 int rs = bits(insn,21,25);
1725 int rt = bits(insn,16,20);
1726 int imm = bits(insn,0,15);
1727
1728 cpu->gpr[rt] = cpu->gpr[rs] | imm;
1729 return(0);
1730 }
1731
1732 /* PREF */
1733 static fastcall int mips64_exec_PREF(cpu_mips_t *cpu,mips_insn_t insn)
1734 {
1735 return(0);
1736 }
1737
1738 /* PREFI */
1739 static fastcall int mips64_exec_PREFI(cpu_mips_t *cpu,mips_insn_t insn)
1740 {
1741 return(0);
1742 }
1743
1744 /* SB (Store Byte) */
1745 static fastcall int mips64_exec_SB(cpu_mips_t *cpu,mips_insn_t insn)
1746 {
1747 int base = bits(insn,21,25);
1748 int rt = bits(insn,16,20);
1749 int offset = bits(insn,0,15);
1750
1751 mips64_exec_memop2(cpu,MIPS_MEMOP_SB,base,offset,rt,FALSE);
1752 return(0);
1753 }
1754
1755 /* SC (Store Conditional) */
1756 static fastcall int mips64_exec_SC(cpu_mips_t *cpu,mips_insn_t insn)
1757 {
1758 int base = bits(insn,21,25);
1759 int rt = bits(insn,16,20);
1760 int offset = bits(insn,0,15);
1761
1762 mips64_exec_memop2(cpu,MIPS_MEMOP_SC,base,offset,rt,TRUE);
1763 return(0);
1764 }
1765
1766 /* SD (Store Double-Word) */
1767 static fastcall int mips64_exec_SD(cpu_mips_t *cpu,mips_insn_t insn)
1768 {
1769 int base = bits(insn,21,25);
1770 int rt = bits(insn,16,20);
1771 int offset = bits(insn,0,15);
1772
1773 mips64_exec_memop2(cpu,MIPS_MEMOP_SD,base,offset,rt,FALSE);
1774 return(0);
1775 }
1776
1777 /* SDL (Store Double-Word Left) */
1778 static fastcall int mips64_exec_SDL(cpu_mips_t *cpu,mips_insn_t insn)
1779 {
1780 int base = bits(insn,21,25);
1781 int rt = bits(insn,16,20);
1782 int offset = bits(insn,0,15);
1783
1784 mips64_exec_memop2(cpu,MIPS_MEMOP_SDL,base,offset,rt,FALSE);
1785 return(0);
1786 }
1787
1788 /* SDR (Store Double-Word Right) */
1789 static fastcall int mips64_exec_SDR(cpu_mips_t *cpu,mips_insn_t insn)
1790 {
1791 int base = bits(insn,21,25);
1792 int rt = bits(insn,16,20);
1793 int offset = bits(insn,0,15);
1794
1795 mips64_exec_memop2(cpu,MIPS_MEMOP_SDR,base,offset,rt,FALSE);
1796 return(0);
1797 }
1798
1799 /* SDC1 (Store Double-Word from Coprocessor 1) */
1800 static fastcall int mips64_exec_SDC1(cpu_mips_t *cpu,mips_insn_t insn)
1801 {
1802 int base = bits(insn,21,25);
1803 int ft = bits(insn,16,20);
1804 int offset = bits(insn,0,15);
1805
1806 mips64_exec_memop2(cpu,MIPS_MEMOP_SDC1,base,offset,ft,FALSE);
1807 return(0);
1808 }
1809
1810 /* SH (Store Half-Word) */
1811 static fastcall int mips64_exec_SH(cpu_mips_t *cpu,mips_insn_t insn)
1812 {
1813 int base = bits(insn,21,25);
1814 int rt = bits(insn,16,20);
1815 int offset = bits(insn,0,15);
1816
1817 mips64_exec_memop2(cpu,MIPS_MEMOP_SH,base,offset,rt,FALSE);
1818 return(0);
1819 }
1820
1821 /* SLL */
1822 static fastcall int mips64_exec_SLL(cpu_mips_t *cpu,mips_insn_t insn)
1823 {
1824 int rt = bits(insn,16,20);
1825 int rd = bits(insn,11,15);
1826 int sa = bits(insn,6,10);
1827 m_uint32_t res;
1828
1829 res = (m_uint32_t)cpu->gpr[rt] << sa;
1830 cpu->gpr[rd] = sign_extend(res,32);
1831 return(0);
1832 }
1833
1834 /* SLLV */
1835 static fastcall int mips64_exec_SLLV(cpu_mips_t *cpu,mips_insn_t insn)
1836 {
1837 int rs = bits(insn,21,25);
1838 int rt = bits(insn,16,20);
1839 int rd = bits(insn,11,15);
1840 m_uint32_t res;
1841
1842 res = (m_uint32_t)cpu->gpr[rt] << (cpu->gpr[rs] & 0x1f);
1843 cpu->gpr[rd] = sign_extend(res,32);
1844 return(0);
1845 }
1846
1847 /* SLT */
1848 static fastcall int mips64_exec_SLT(cpu_mips_t *cpu,mips_insn_t insn)
1849 {
1850 int rs = bits(insn,21,25);
1851 int rt = bits(insn,16,20);
1852 int rd = bits(insn,11,15);
1853
1854 if ((m_int64_t)cpu->gpr[rs] < (m_int64_t)cpu->gpr[rt])
1855 cpu->gpr[rd] = 1;
1856 else
1857 cpu->gpr[rd] = 0;
1858
1859 return(0);
1860 }
1861
1862 /* SLTI */
1863 static fastcall int mips64_exec_SLTI(cpu_mips_t *cpu,mips_insn_t insn)
1864 {
1865 int rs = bits(insn,21,25);
1866 int rt = bits(insn,16,20);
1867 int imm = bits(insn,0,15);
1868 m_int64_t val = sign_extend(imm,16);
1869
1870 if ((m_int64_t)cpu->gpr[rs] < val)
1871 cpu->gpr[rt] = 1;
1872 else
1873 cpu->gpr[rt] = 0;
1874
1875 return(0);
1876 }
1877
1878 /* SLTIU */
1879 static fastcall int mips64_exec_SLTIU(cpu_mips_t *cpu,mips_insn_t insn)
1880 {
1881 int rs = bits(insn,21,25);
1882 int rt = bits(insn,16,20);
1883 int imm = bits(insn,0,15);
1884 m_uint64_t val = sign_extend(imm,16);
1885
1886 if (cpu->gpr[rs] < val)
1887 cpu->gpr[rt] = 1;
1888 else
1889 cpu->gpr[rt] = 0;
1890
1891 return(0);
1892 }
1893
1894 /* SLTU */
1895 static fastcall int mips64_exec_SLTU(cpu_mips_t *cpu,mips_insn_t insn)
1896 {
1897 int rs = bits(insn,21,25);
1898 int rt = bits(insn,16,20);
1899 int rd = bits(insn,11,15);
1900
1901 if (cpu->gpr[rs] < cpu->gpr[rt])
1902 cpu->gpr[rd] = 1;
1903 else
1904 cpu->gpr[rd] = 0;
1905
1906 return(0);
1907 }
1908
1909 /* SRA */
1910 static fastcall int mips64_exec_SRA(cpu_mips_t *cpu,mips_insn_t insn)
1911 {
1912 int rt = bits(insn,16,20);
1913 int rd = bits(insn,11,15);
1914 int sa = bits(insn,6,10);
1915 m_int32_t res;
1916
1917 res = (m_int32_t)cpu->gpr[rt] >> sa;
1918 cpu->gpr[rd] = sign_extend(res,32);
1919 return(0);
1920 }
1921
1922 /* SRAV */
1923 static fastcall int mips64_exec_SRAV(cpu_mips_t *cpu,mips_insn_t insn)
1924 {
1925 int rs = bits(insn,21,25);
1926 int rt = bits(insn,16,20);
1927 int rd = bits(insn,11,15);
1928 m_int32_t res;
1929
1930 res = (m_int32_t)cpu->gpr[rt] >> (cpu->gpr[rs] & 0x1f);
1931 cpu->gpr[rd] = sign_extend(res,32);
1932 return(0);
1933 }
1934
1935 /* SRL */
1936 static fastcall int mips64_exec_SRL(cpu_mips_t *cpu,mips_insn_t insn)
1937 {
1938 int rt = bits(insn,16,20);
1939 int rd = bits(insn,11,15);
1940 int sa = bits(insn,6,10);
1941 m_uint32_t res;
1942
1943 res = (m_uint32_t)cpu->gpr[rt] >> sa;
1944 cpu->gpr[rd] = sign_extend(res,32);
1945 return(0);
1946 }
1947
1948 /* SRLV */
1949 static fastcall int mips64_exec_SRLV(cpu_mips_t *cpu,mips_insn_t insn)
1950 {
1951 int rs = bits(insn,21,25);
1952 int rt = bits(insn,16,20);
1953 int rd = bits(insn,11,15);
1954 m_uint32_t res;
1955
1956 res = (m_uint32_t)cpu->gpr[rt] >> (cpu->gpr[rs] & 0x1f);
1957 cpu->gpr[rd] = sign_extend(res,32);
1958 return(0);
1959 }
1960
1961 /* SUB */
1962 static fastcall int mips64_exec_SUB(cpu_mips_t *cpu,mips_insn_t insn)
1963 {
1964 int rs = bits(insn,21,25);
1965 int rt = bits(insn,16,20);
1966 int rd = bits(insn,11,15);
1967 m_uint32_t res;
1968
1969 /* TODO: Exception handling */
1970 res = (m_uint32_t)cpu->gpr[rs] - (m_uint32_t)cpu->gpr[rt];
1971 cpu->gpr[rd] = sign_extend(res,32);
1972 return(0);
1973 }
1974
1975 /* SUBU */
1976 static fastcall int mips64_exec_SUBU(cpu_mips_t *cpu,mips_insn_t insn)
1977 {
1978 int rs = bits(insn,21,25);
1979 int rt = bits(insn,16,20);
1980 int rd = bits(insn,11,15);
1981 m_uint32_t res;
1982
1983 res = (m_uint32_t)cpu->gpr[rs] - (m_uint32_t)cpu->gpr[rt];
1984 cpu->gpr[rd] = sign_extend(res,32);
1985 return(0);
1986 }
1987
1988 /* SW (Store Word) */
1989 static fastcall int mips64_exec_SW(cpu_mips_t *cpu,mips_insn_t insn)
1990 {
1991 int base = bits(insn,21,25);
1992 int rt = bits(insn,16,20);
1993 int offset = bits(insn,0,15);
1994
1995 mips64_exec_memop2(cpu,MIPS_MEMOP_SW,base,offset,rt,FALSE);
1996 return(0);
1997 }
1998
1999 /* SWL (Store Word Left) */
2000 static fastcall int mips64_exec_SWL(cpu_mips_t *cpu,mips_insn_t insn)
2001 {
2002 int base = bits(insn,21,25);
2003 int rt = bits(insn,16,20);
2004 int offset = bits(insn,0,15);
2005
2006 mips64_exec_memop2(cpu,MIPS_MEMOP_SWL,base,offset,rt,FALSE);
2007 return(0);
2008 }
2009
2010 /* SWR (Store Word Right) */
2011 static fastcall int mips64_exec_SWR(cpu_mips_t *cpu,mips_insn_t insn)
2012 {
2013 int base = bits(insn,21,25);
2014 int rt = bits(insn,16,20);
2015 int offset = bits(insn,0,15);
2016
2017 mips64_exec_memop2(cpu,MIPS_MEMOP_SWR,base,offset,rt,FALSE);
2018 return(0);
2019 }
2020
2021 /* SYNC */
2022 static fastcall int mips64_exec_SYNC(cpu_mips_t *cpu,mips_insn_t insn)
2023 {
2024 return(0);
2025 }
2026
2027 /* SYSCALL */
2028 static fastcall int mips64_exec_SYSCALL(cpu_mips_t *cpu,mips_insn_t insn)
2029 {
2030 mips64_exec_syscall(cpu);
2031 return(1);
2032 }
2033
2034 /* TEQ (Trap if Equal) */
2035 static fastcall int mips64_exec_TEQ(cpu_mips_t *cpu,mips_insn_t insn)
2036 {
2037 int rs = bits(insn,21,25);
2038 int rt = bits(insn,16,20);
2039
2040 if (unlikely(cpu->gpr[rs] == cpu->gpr[rt])) {
2041 mips64_trigger_trap_exception(cpu);
2042 return(1);
2043 }
2044
2045 return(0);
2046 }
2047
2048 /* TEQI (Trap if Equal Immediate) */
2049 static fastcall int mips64_exec_TEQI(cpu_mips_t *cpu,mips_insn_t insn)
2050 {
2051 int rs = bits(insn,21,25);
2052 int imm = bits(insn,0,15);
2053 m_uint64_t val = sign_extend(imm,16);
2054
2055 if (unlikely(cpu->gpr[rs] == val)) {
2056 mips64_trigger_trap_exception(cpu);
2057 return(1);
2058 }
2059
2060 return(0);
2061 }
2062
2063 /* TLBP */
2064 static fastcall int mips64_exec_TLBP(cpu_mips_t *cpu,mips_insn_t insn)
2065 {
2066 mips64_cp0_exec_tlbp(cpu);
2067 return(0);
2068 }
2069
2070 /* TLBR */
2071 static fastcall int mips64_exec_TLBR(cpu_mips_t *cpu,mips_insn_t insn)
2072 {
2073 mips64_cp0_exec_tlbr(cpu);
2074 return(0);
2075 }
2076
2077 /* TLBWI */
2078 static fastcall int mips64_exec_TLBWI(cpu_mips_t *cpu,mips_insn_t insn)
2079 {
2080 mips64_cp0_exec_tlbwi(cpu);
2081 return(0);
2082 }
2083
2084 /* TLBWR */
2085 static fastcall int mips64_exec_TLBWR(cpu_mips_t *cpu,mips_insn_t insn)
2086 {
2087 mips64_cp0_exec_tlbwr(cpu);
2088 return(0);
2089 }
2090
2091 /* XOR */
2092 static fastcall int mips64_exec_XOR(cpu_mips_t *cpu,mips_insn_t insn)
2093 {
2094 int rs = bits(insn,21,25);
2095 int rt = bits(insn,16,20);
2096 int rd = bits(insn,11,15);
2097
2098 cpu->gpr[rd] = cpu->gpr[rs] ^ cpu->gpr[rt];
2099 return(0);
2100 }
2101
2102 /* XORI */
2103 static fastcall int mips64_exec_XORI(cpu_mips_t *cpu,mips_insn_t insn)
2104 {
2105 int rs = bits(insn,21,25);
2106 int rt = bits(insn,16,20);
2107 int imm = bits(insn,0,15);
2108
2109 cpu->gpr[rt] = cpu->gpr[rs] ^ imm;
2110 return(0);
2111 }
2112
2113 /* MIPS instruction array */
2114 static struct mips64_insn_exec_tag mips64_exec_tags[] = {
2115 { "li" , mips64_exec_LI , 0xffe00000 , 0x24000000, 1, 16 },
2116 { "move" , mips64_exec_MOVE , 0xfc1f07ff , 0x00000021, 1, 15 },
2117 { "b" , mips64_exec_B , 0xffff0000 , 0x10000000, 0, 10 },
2118 { "bal" , mips64_exec_BAL , 0xffff0000 , 0x04110000, 0, 10 },
2119 { "beqz" , mips64_exec_BEQZ , 0xfc1f0000 , 0x10000000, 0, 9 },
2120 { "bnez" , mips64_exec_BNEZ , 0xfc1f0000 , 0x14000000, 0, 9 },
2121 { "add" , mips64_exec_ADD , 0xfc0007ff , 0x00000020, 1, 3 },
2122 { "addi" , mips64_exec_ADDI , 0xfc000000 , 0x20000000, 1, 6 },
2123 { "addiu" , mips64_exec_ADDIU , 0xfc000000 , 0x24000000, 1, 6 },
2124 { "addu" , mips64_exec_ADDU , 0xfc0007ff , 0x00000021, 1, 3 },
2125 { "and" , mips64_exec_AND , 0xfc0007ff , 0x00000024, 1, 3 },
2126 { "andi" , mips64_exec_ANDI , 0xfc000000 , 0x30000000, 1, 5 },
2127 { "beq" , mips64_exec_BEQ , 0xfc000000 , 0x10000000, 0, 8 },
2128 { "beql" , mips64_exec_BEQL , 0xfc000000 , 0x50000000, 0, 8 },
2129 { "bgez" , mips64_exec_BGEZ , 0xfc1f0000 , 0x04010000, 0, 9 },
2130 { "bgezal" , mips64_exec_BGEZAL , 0xfc1f0000 , 0x04110000, 0, 9 },
2131 { "bgezall", mips64_exec_BGEZALL , 0xfc1f0000 , 0x04130000, 0, 9 },
2132 { "bgezl" , mips64_exec_BGEZL , 0xfc1f0000 , 0x04030000, 0, 9 },
2133 { "bgtz" , mips64_exec_BGTZ , 0xfc1f0000 , 0x1c000000, 0, 9 },
2134 { "bgtzl" , mips64_exec_BGTZL , 0xfc1f0000 , 0x5c000000, 0, 9 },
2135 { "blez" , mips64_exec_BLEZ , 0xfc1f0000 , 0x18000000, 0, 9 },
2136 { "blezl" , mips64_exec_BLEZL , 0xfc1f0000 , 0x58000000, 0, 9 },
2137 { "bltz" , mips64_exec_BLTZ , 0xfc1f0000 , 0x04000000, 0, 9 },
2138 { "bltzal" , mips64_exec_BLTZAL , 0xfc1f0000 , 0x04100000, 0, 9 },
2139 { "bltzall", mips64_exec_BLTZALL , 0xfc1f0000 , 0x04120000, 0, 9 },
2140 { "bltzl" , mips64_exec_BLTZL , 0xfc1f0000 , 0x04020000, 0, 9 },
2141 { "bne" , mips64_exec_BNE , 0xfc000000 , 0x14000000, 0, 8 },
2142 { "bnel" , mips64_exec_BNEL , 0xfc000000 , 0x54000000, 0, 8 },
2143 { "break" , mips64_exec_BREAK , 0xfc00003f , 0x0000000d, 1, 0 },
2144 { "cache" , mips64_exec_CACHE , 0xfc000000 , 0xbc000000, 1, 2 },
2145 { "cfc0" , mips64_exec_CFC0 , 0xffe007ff , 0x40400000, 1, 18 },
2146 { "ctc0" , mips64_exec_CTC0 , 0xffe007ff , 0x40600000, 1, 18 },
2147 { "daddiu" , mips64_exec_DADDIU , 0xfc000000 , 0x64000000, 1, 5 },
2148 { "daddu" , mips64_exec_DADDU , 0xfc0007ff , 0x0000002d, 1, 3 },
2149 { "div" , mips64_exec_DIV , 0xfc00ffff , 0x0000001a, 1, 17 },
2150 { "divu" , mips64_exec_DIVU , 0xfc00ffff , 0x0000001b, 1, 17 },
2151 { "dmfc0" , mips64_exec_DMFC0 , 0xffe007f8 , 0x40200000, 1, 18 },
2152 { "dmfc1" , mips64_exec_DMFC1 , 0xffe007ff , 0x44200000, 1, 19 },
2153 { "dmtc0" , mips64_exec_DMTC0 , 0xffe007f8 , 0x40a00000, 1, 18 },
2154 { "dmtc1" , mips64_exec_DMTC1 , 0xffe007ff , 0x44a00000, 1, 19 },
2155 { "dsll" , mips64_exec_DSLL , 0xffe0003f , 0x00000038, 1, 7 },
2156 { "dsll32" , mips64_exec_DSLL32 , 0xffe0003f , 0x0000003c, 1, 7 },
2157 { "dsllv" , mips64_exec_DSLLV , 0xfc0007ff , 0x00000014, 1, 4 },
2158 { "dsra" , mips64_exec_DSRA , 0xffe0003f , 0x0000003b, 1, 7 },
2159 { "dsra32" , mips64_exec_DSRA32 , 0xffe0003f , 0x0000003f, 1, 7 },
2160 { "dsrav" , mips64_exec_DSRAV , 0xfc0007ff , 0x00000017, 1, 4 },
2161 { "dsrl" , mips64_exec_DSRL , 0xffe0003f , 0x0000003a, 1, 7 },
2162 { "dsrl32" , mips64_exec_DSRL32 , 0xffe0003f , 0x0000003e, 1, 7 },
2163 { "dsrlv" , mips64_exec_DSRLV , 0xfc0007ff , 0x00000016, 1, 4 },
2164 { "dsubu" , mips64_exec_DSUBU , 0xfc0007ff , 0x0000002f, 1, 3 },
2165 { "eret" , mips64_exec_ERET , 0xffffffff , 0x42000018, 0, 1 },
2166 { "j" , mips64_exec_J , 0xfc000000 , 0x08000000, 0, 11 },
2167 { "jal" , mips64_exec_JAL , 0xfc000000 , 0x0c000000, 0, 11 },
2168 { "jalr" , mips64_exec_JALR , 0xfc1f003f , 0x00000009, 0, 15 },
2169 { "jr" , mips64_exec_JR , 0xfc1ff83f , 0x00000008, 0, 13 },
2170 { "lb" , mips64_exec_LB , 0xfc000000 , 0x80000000, 1, 2 },
2171 { "lbu" , mips64_exec_LBU , 0xfc000000 , 0x90000000, 1, 2 },
2172 { "ld" , mips64_exec_LD , 0xfc000000 , 0xdc000000, 1, 2 },
2173 { "ldc1" , mips64_exec_LDC1 , 0xfc000000 , 0xd4000000, 1, 3 },
2174 { "ldl" , mips64_exec_LDL , 0xfc000000 , 0x68000000, 1, 2 },
2175 { "ldr" , mips64_exec_LDR , 0xfc000000 , 0x6c000000, 1, 2 },
2176 { "lh" , mips64_exec_LH , 0xfc000000 , 0x84000000, 1, 2 },
2177 { "lhu" , mips64_exec_LHU , 0xfc000000 , 0x94000000, 1, 2 },
2178 { "ll" , mips64_exec_LL , 0xfc000000 , 0xc0000000, 1, 2 },
2179 { "lui" , mips64_exec_LUI , 0xffe00000 , 0x3c000000, 1, 16 },
2180 { "lw" , mips64_exec_LW , 0xfc000000 , 0x8c000000, 1, 2 },
2181 { "lwl" , mips64_exec_LWL , 0xfc000000 , 0x88000000, 1, 2 },
2182 { "lwr" , mips64_exec_LWR , 0xfc000000 , 0x98000000, 1, 2 },
2183 { "lwu" , mips64_exec_LWU , 0xfc000000 , 0x9c000000, 1, 2 },
2184 { "mfc0" , mips64_exec_MFC0 , 0xffe007ff , 0x40000000, 1, 18 },
2185 { "mfc0_1" , mips64_exec_CFC0 , 0xffe007ff , 0x40000001, 1, 19 },
2186 { "mfc1" , mips64_exec_MFC1 , 0xffe007ff , 0x44000000, 1, 19 },
2187 { "mfhi" , mips64_exec_MFHI , 0xffff07ff , 0x00000010, 1, 14 },
2188 { "mflo" , mips64_exec_MFLO , 0xffff07ff , 0x00000012, 1, 14 },
2189 { "mtc0" , mips64_exec_MTC0 , 0xffe007ff , 0x40800000, 1, 18 },
2190 { "mtc1" , mips64_exec_MTC1 , 0xffe007ff , 0x44800000, 1, 19 },
2191 { "mthi" , mips64_exec_MTHI , 0xfc1fffff , 0x00000011, 1, 13 },
2192 { "mtlo" , mips64_exec_MTLO , 0xfc1fffff , 0x00000013, 1, 13 },
2193 { "mul" , mips64_exec_MUL , 0xfc0007ff , 0x70000002, 1, 4 },
2194 { "mult" , mips64_exec_MULT , 0xfc00ffff , 0x00000018, 1, 17 },
2195 { "multu" , mips64_exec_MULTU , 0xfc00ffff , 0x00000019, 1, 17 },
2196 { "nop" , mips64_exec_NOP , 0xffffffff , 0x00000000, 1, 1 },
2197 { "nor" , mips64_exec_NOR , 0xfc0007ff , 0x00000027, 1, 3 },
2198 { "or" , mips64_exec_OR , 0xfc0007ff , 0x00000025, 1, 3 },
2199 { "ori" , mips64_exec_ORI , 0xfc000000 , 0x34000000, 1, 5 },
2200 { "pref" , mips64_exec_PREF , 0xfc000000 , 0xcc000000, 1, 0 },
2201 { "prefi" , mips64_exec_PREFI , 0xfc0007ff , 0x4c00000f, 1, 0 },
2202 { "sb" , mips64_exec_SB , 0xfc000000 , 0xa0000000, 1, 2 },
2203 { "sc" , mips64_exec_SC , 0xfc000000 , 0xe0000000, 1, 2 },
2204 { "sd" , mips64_exec_SD , 0xfc000000 , 0xfc000000, 1, 2 },
2205 { "sdc1" , mips64_exec_SDC1 , 0xfc000000 , 0xf4000000, 1, 3 },
2206 { "sdl" , mips64_exec_SDL , 0xfc000000 , 0xb0000000, 1, 2 },
2207 { "sdr" , mips64_exec_SDR , 0xfc000000 , 0xb4000000, 1, 2 },
2208 { "sh" , mips64_exec_SH , 0xfc000000 , 0xa4000000, 1, 2 },
2209 { "sll" , mips64_exec_SLL , 0xffe0003f , 0x00000000, 1, 7 },
2210 { "sllv" , mips64_exec_SLLV , 0xfc0007ff , 0x00000004, 1, 4 },
2211 { "slt" , mips64_exec_SLT , 0xfc0007ff , 0x0000002a, 1, 3 },
2212 { "slti" , mips64_exec_SLTI , 0xfc000000 , 0x28000000, 1, 5 },
2213 { "sltiu" , mips64_exec_SLTIU , 0xfc000000 , 0x2c000000, 1, 5 },
2214 { "sltu" , mips64_exec_SLTU , 0xfc0007ff , 0x0000002b, 1, 3 },
2215 { "sra" , mips64_exec_SRA , 0xffe0003f , 0x00000003, 1, 7 },
2216 { "srav" , mips64_exec_SRAV , 0xfc0007ff , 0x00000007, 1, 4 },
2217 { "srl" , mips64_exec_SRL , 0xffe0003f , 0x00000002, 1, 7 },
2218 { "srlv" , mips64_exec_SRLV , 0xfc0007ff , 0x00000006, 1, 4 },
2219 { "sub" , mips64_exec_SUB , 0xfc0007ff , 0x00000022, 1, 3 },
2220 { "subu" , mips64_exec_SUBU , 0xfc0007ff , 0x00000023, 1, 3 },
2221 { "sw" , mips64_exec_SW , 0xfc000000 , 0xac000000, 1, 2 },
2222 { "swl" , mips64_exec_SWL , 0xfc000000 , 0xa8000000, 1, 2 },
2223 { "swr" , mips64_exec_SWR , 0xfc000000 , 0xb8000000, 1, 2 },
2224 { "sync" , mips64_exec_SYNC , 0xfffff83f , 0x0000000f, 1, 1 },
2225 { "syscall", mips64_exec_SYSCALL , 0xfc00003f , 0x0000000c, 1, 1 },
2226 { "teq" , mips64_exec_TEQ , 0xfc00003f , 0x00000034, 1, 17 },
2227 { "teqi" , mips64_exec_TEQI , 0xfc1f0000 , 0x040c0000, 1, 20 },
2228 { "tlbp" , mips64_exec_TLBP , 0xffffffff , 0x42000008, 1, 1 },
2229 { "tlbr" , mips64_exec_TLBR , 0xffffffff , 0x42000001, 1, 1 },
2230 { "tlbwi" , mips64_exec_TLBWI , 0xffffffff , 0x42000002, 1, 1 },
2231 { "tlbwr" , mips64_exec_TLBWR , 0xffffffff , 0x42000006, 1, 1 },
2232 { "xor" , mips64_exec_XOR , 0xfc0007ff , 0x00000026, 1, 3 },
2233 { "xori" , mips64_exec_XORI , 0xfc000000 , 0x38000000, 1, 5 },
2234 { "unknown", mips64_exec_unknown , 0x00000000 , 0x00000000, 1, 0 },
2235 { NULL , NULL , 0x00000000 , 0x00000000, 1, 0 },
2236 };
2237
2238 #endif