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