1 /* 2 * Toshiba TX79-specific instructions translation routines 3 * 4 * Copyright (c) 2018 Fredrik Noring 5 * Copyright (c) 2021 Philippe Mathieu-Daudé 6 * 7 * SPDX-License-Identifier: GPL-2.0-or-later 8 */ 9 10 #include "qemu/osdep.h" 11 #include "tcg/tcg-op.h" 12 #include "tcg/tcg-op-gvec.h" 13 #include "exec/helper-gen.h" 14 #include "translate.h" 15 16 /* Include the auto-generated decoder. */ 17 #include "decode-tx79.c.inc" 18 19 /* 20 * Overview of the TX79-specific instruction set 21 * ============================================= 22 * 23 * The R5900 and the C790 have 128-bit wide GPRs, where the upper 64 bits 24 * are only used by the specific quadword (128-bit) LQ/SQ load/store 25 * instructions and certain multimedia instructions (MMIs). These MMIs 26 * configure the 128-bit data path as two 64-bit, four 32-bit, eight 16-bit 27 * or sixteen 8-bit paths. 28 * 29 * Reference: 30 * 31 * The Toshiba TX System RISC TX79 Core Architecture manual, 32 * https://wiki.qemu.org/File:C790.pdf 33 */ 34 35 bool decode_ext_tx79(DisasContext *ctx, uint32_t insn) 36 { 37 if (TARGET_LONG_BITS == 64 && decode_tx79(ctx, insn)) { 38 return true; 39 } 40 return false; 41 } 42 43 /* 44 * Three-Operand Multiply and Multiply-Add (4 instructions) 45 * -------------------------------------------------------- 46 * MADD [rd,] rs, rt Multiply/Add 47 * MADDU [rd,] rs, rt Multiply/Add Unsigned 48 * MULT [rd,] rs, rt Multiply (3-operand) 49 * MULTU [rd,] rs, rt Multiply Unsigned (3-operand) 50 */ 51 52 /* 53 * Multiply Instructions for Pipeline 1 (10 instructions) 54 * ------------------------------------------------------ 55 * MULT1 [rd,] rs, rt Multiply Pipeline 1 56 * MULTU1 [rd,] rs, rt Multiply Unsigned Pipeline 1 57 * DIV1 rs, rt Divide Pipeline 1 58 * DIVU1 rs, rt Divide Unsigned Pipeline 1 59 * MADD1 [rd,] rs, rt Multiply-Add Pipeline 1 60 * MADDU1 [rd,] rs, rt Multiply-Add Unsigned Pipeline 1 61 * MFHI1 rd Move From HI1 Register 62 * MFLO1 rd Move From LO1 Register 63 * MTHI1 rs Move To HI1 Register 64 * MTLO1 rs Move To LO1 Register 65 */ 66 67 static bool trans_MFHI1(DisasContext *ctx, arg_rtype *a) 68 { 69 gen_store_gpr(cpu_HI[1], a->rd); 70 71 return true; 72 } 73 74 static bool trans_MFLO1(DisasContext *ctx, arg_rtype *a) 75 { 76 gen_store_gpr(cpu_LO[1], a->rd); 77 78 return true; 79 } 80 81 static bool trans_MTHI1(DisasContext *ctx, arg_rtype *a) 82 { 83 gen_load_gpr(cpu_HI[1], a->rs); 84 85 return true; 86 } 87 88 static bool trans_MTLO1(DisasContext *ctx, arg_rtype *a) 89 { 90 gen_load_gpr(cpu_LO[1], a->rs); 91 92 return true; 93 } 94 95 /* 96 * Arithmetic (19 instructions) 97 * ---------------------------- 98 * PADDB rd, rs, rt Parallel Add Byte 99 * PSUBB rd, rs, rt Parallel Subtract Byte 100 * PADDH rd, rs, rt Parallel Add Halfword 101 * PSUBH rd, rs, rt Parallel Subtract Halfword 102 * PADDW rd, rs, rt Parallel Add Word 103 * PSUBW rd, rs, rt Parallel Subtract Word 104 * PADSBH rd, rs, rt Parallel Add/Subtract Halfword 105 * PADDSB rd, rs, rt Parallel Add with Signed Saturation Byte 106 * PSUBSB rd, rs, rt Parallel Subtract with Signed Saturation Byte 107 * PADDSH rd, rs, rt Parallel Add with Signed Saturation Halfword 108 * PSUBSH rd, rs, rt Parallel Subtract with Signed Saturation Halfword 109 * PADDSW rd, rs, rt Parallel Add with Signed Saturation Word 110 * PSUBSW rd, rs, rt Parallel Subtract with Signed Saturation Word 111 * PADDUB rd, rs, rt Parallel Add with Unsigned saturation Byte 112 * PSUBUB rd, rs, rt Parallel Subtract with Unsigned saturation Byte 113 * PADDUH rd, rs, rt Parallel Add with Unsigned saturation Halfword 114 * PSUBUH rd, rs, rt Parallel Subtract with Unsigned saturation Halfword 115 * PADDUW rd, rs, rt Parallel Add with Unsigned saturation Word 116 * PSUBUW rd, rs, rt Parallel Subtract with Unsigned saturation Word 117 */ 118 119 static bool trans_parallel_arith(DisasContext *ctx, arg_rtype *a, 120 void (*gen_logic_i64)(TCGv_i64, TCGv_i64, TCGv_i64)) 121 { 122 TCGv_i64 ax, bx; 123 124 if (a->rd == 0) { 125 /* nop */ 126 return true; 127 } 128 129 ax = tcg_temp_new_i64(); 130 bx = tcg_temp_new_i64(); 131 132 /* Lower half */ 133 gen_load_gpr(ax, a->rs); 134 gen_load_gpr(bx, a->rt); 135 gen_logic_i64(cpu_gpr[a->rd], ax, bx); 136 137 /* Upper half */ 138 gen_load_gpr_hi(ax, a->rs); 139 gen_load_gpr_hi(bx, a->rt); 140 gen_logic_i64(cpu_gpr_hi[a->rd], ax, bx); 141 142 tcg_temp_free(bx); 143 tcg_temp_free(ax); 144 145 return true; 146 } 147 148 /* Parallel Subtract Byte */ 149 static bool trans_PSUBB(DisasContext *ctx, arg_rtype *a) 150 { 151 return trans_parallel_arith(ctx, a, tcg_gen_vec_sub8_i64); 152 } 153 154 /* Parallel Subtract Halfword */ 155 static bool trans_PSUBH(DisasContext *ctx, arg_rtype *a) 156 { 157 return trans_parallel_arith(ctx, a, tcg_gen_vec_sub16_i64); 158 } 159 160 /* Parallel Subtract Word */ 161 static bool trans_PSUBW(DisasContext *ctx, arg_rtype *a) 162 { 163 return trans_parallel_arith(ctx, a, tcg_gen_vec_sub32_i64); 164 } 165 166 /* 167 * Min/Max (4 instructions) 168 * ------------------------ 169 * PMAXH rd, rs, rt Parallel Maximum Halfword 170 * PMINH rd, rs, rt Parallel Minimum Halfword 171 * PMAXW rd, rs, rt Parallel Maximum Word 172 * PMINW rd, rs, rt Parallel Minimum Word 173 */ 174 175 /* 176 * Absolute (2 instructions) 177 * ------------------------- 178 * PABSH rd, rt Parallel Absolute Halfword 179 * PABSW rd, rt Parallel Absolute Word 180 */ 181 182 /* 183 * Logical (4 instructions) 184 * ------------------------ 185 * PAND rd, rs, rt Parallel AND 186 * POR rd, rs, rt Parallel OR 187 * PXOR rd, rs, rt Parallel XOR 188 * PNOR rd, rs, rt Parallel NOR 189 */ 190 191 /* Parallel And */ 192 static bool trans_PAND(DisasContext *ctx, arg_rtype *a) 193 { 194 return trans_parallel_arith(ctx, a, tcg_gen_and_i64); 195 } 196 197 /* Parallel Or */ 198 static bool trans_POR(DisasContext *ctx, arg_rtype *a) 199 { 200 return trans_parallel_arith(ctx, a, tcg_gen_or_i64); 201 } 202 203 /* Parallel Exclusive Or */ 204 static bool trans_PXOR(DisasContext *ctx, arg_rtype *a) 205 { 206 return trans_parallel_arith(ctx, a, tcg_gen_xor_i64); 207 } 208 209 /* Parallel Not Or */ 210 static bool trans_PNOR(DisasContext *ctx, arg_rtype *a) 211 { 212 return trans_parallel_arith(ctx, a, tcg_gen_nor_i64); 213 } 214 215 /* 216 * Shift (9 instructions) 217 * ---------------------- 218 * PSLLH rd, rt, sa Parallel Shift Left Logical Halfword 219 * PSRLH rd, rt, sa Parallel Shift Right Logical Halfword 220 * PSRAH rd, rt, sa Parallel Shift Right Arithmetic Halfword 221 * PSLLW rd, rt, sa Parallel Shift Left Logical Word 222 * PSRLW rd, rt, sa Parallel Shift Right Logical Word 223 * PSRAW rd, rt, sa Parallel Shift Right Arithmetic Word 224 * PSLLVW rd, rt, rs Parallel Shift Left Logical Variable Word 225 * PSRLVW rd, rt, rs Parallel Shift Right Logical Variable Word 226 * PSRAVW rd, rt, rs Parallel Shift Right Arithmetic Variable Word 227 */ 228 229 /* 230 * Compare (6 instructions) 231 * ------------------------ 232 * PCGTB rd, rs, rt Parallel Compare for Greater Than Byte 233 * PCEQB rd, rs, rt Parallel Compare for Equal Byte 234 * PCGTH rd, rs, rt Parallel Compare for Greater Than Halfword 235 * PCEQH rd, rs, rt Parallel Compare for Equal Halfword 236 * PCGTW rd, rs, rt Parallel Compare for Greater Than Word 237 * PCEQW rd, rs, rt Parallel Compare for Equal Word 238 */ 239 240 /* 241 * LZC (1 instruction) 242 * ------------------- 243 * PLZCW rd, rs Parallel Leading Zero or One Count Word 244 */ 245 246 /* 247 * Quadword Load and Store (2 instructions) 248 * ---------------------------------------- 249 * LQ rt, offset(base) Load Quadword 250 * SQ rt, offset(base) Store Quadword 251 */ 252 253 /* 254 * Multiply and Divide (19 instructions) 255 * ------------------------------------- 256 * PMULTW rd, rs, rt Parallel Multiply Word 257 * PMULTUW rd, rs, rt Parallel Multiply Unsigned Word 258 * PDIVW rs, rt Parallel Divide Word 259 * PDIVUW rs, rt Parallel Divide Unsigned Word 260 * PMADDW rd, rs, rt Parallel Multiply-Add Word 261 * PMADDUW rd, rs, rt Parallel Multiply-Add Unsigned Word 262 * PMSUBW rd, rs, rt Parallel Multiply-Subtract Word 263 * PMULTH rd, rs, rt Parallel Multiply Halfword 264 * PMADDH rd, rs, rt Parallel Multiply-Add Halfword 265 * PMSUBH rd, rs, rt Parallel Multiply-Subtract Halfword 266 * PHMADH rd, rs, rt Parallel Horizontal Multiply-Add Halfword 267 * PHMSBH rd, rs, rt Parallel Horizontal Multiply-Subtract Halfword 268 * PDIVBW rs, rt Parallel Divide Broadcast Word 269 * PMFHI rd Parallel Move From HI Register 270 * PMFLO rd Parallel Move From LO Register 271 * PMTHI rs Parallel Move To HI Register 272 * PMTLO rs Parallel Move To LO Register 273 * PMFHL rd Parallel Move From HI/LO Register 274 * PMTHL rs Parallel Move To HI/LO Register 275 */ 276 277 /* 278 * Pack/Extend (11 instructions) 279 * ----------------------------- 280 * PPAC5 rd, rt Parallel Pack to 5 bits 281 * PPACB rd, rs, rt Parallel Pack to Byte 282 * PPACH rd, rs, rt Parallel Pack to Halfword 283 * PPACW rd, rs, rt Parallel Pack to Word 284 * PEXT5 rd, rt Parallel Extend Upper from 5 bits 285 * PEXTUB rd, rs, rt Parallel Extend Upper from Byte 286 * PEXTLB rd, rs, rt Parallel Extend Lower from Byte 287 * PEXTUH rd, rs, rt Parallel Extend Upper from Halfword 288 * PEXTLH rd, rs, rt Parallel Extend Lower from Halfword 289 * PEXTUW rd, rs, rt Parallel Extend Upper from Word 290 * PEXTLW rd, rs, rt Parallel Extend Lower from Word 291 */ 292 293 static void gen_pextw(TCGv_i64 dl, TCGv_i64 dh, TCGv_i64 a, TCGv_i64 b) 294 { 295 tcg_gen_deposit_i64(dl, b, a, 32, 32); 296 tcg_gen_shri_i64(b, b, 32); 297 tcg_gen_deposit_i64(dh, a, b, 0, 32); 298 } 299 300 static bool trans_PEXTLx(DisasContext *ctx, arg_rtype *a, unsigned wlen) 301 { 302 TCGv_i64 ax, bx; 303 304 if (a->rd == 0) { 305 /* nop */ 306 return true; 307 } 308 309 ax = tcg_temp_new_i64(); 310 bx = tcg_temp_new_i64(); 311 312 gen_load_gpr(ax, a->rs); 313 gen_load_gpr(bx, a->rt); 314 315 /* Lower half */ 316 for (int i = 0; i < 64 / (2 * wlen); i++) { 317 tcg_gen_deposit_i64(cpu_gpr[a->rd], 318 cpu_gpr[a->rd], bx, 2 * wlen * i, wlen); 319 tcg_gen_deposit_i64(cpu_gpr[a->rd], 320 cpu_gpr[a->rd], ax, 2 * wlen * i + wlen, wlen); 321 tcg_gen_shri_i64(bx, bx, wlen); 322 tcg_gen_shri_i64(ax, ax, wlen); 323 } 324 /* Upper half */ 325 for (int i = 0; i < 64 / (2 * wlen); i++) { 326 tcg_gen_deposit_i64(cpu_gpr_hi[a->rd], 327 cpu_gpr_hi[a->rd], bx, 2 * wlen * i, wlen); 328 tcg_gen_deposit_i64(cpu_gpr_hi[a->rd], 329 cpu_gpr_hi[a->rd], ax, 2 * wlen * i + wlen, wlen); 330 tcg_gen_shri_i64(bx, bx, wlen); 331 tcg_gen_shri_i64(ax, ax, wlen); 332 } 333 334 tcg_temp_free(bx); 335 tcg_temp_free(ax); 336 337 return true; 338 } 339 340 /* Parallel Extend Lower from Byte */ 341 static bool trans_PEXTLB(DisasContext *ctx, arg_rtype *a) 342 { 343 return trans_PEXTLx(ctx, a, 8); 344 } 345 346 /* Parallel Extend Lower from Halfword */ 347 static bool trans_PEXTLH(DisasContext *ctx, arg_rtype *a) 348 { 349 return trans_PEXTLx(ctx, a, 16); 350 } 351 352 /* Parallel Extend Lower from Word */ 353 static bool trans_PEXTLW(DisasContext *ctx, arg_rtype *a) 354 { 355 TCGv_i64 ax, bx; 356 357 if (a->rd == 0) { 358 /* nop */ 359 return true; 360 } 361 362 ax = tcg_temp_new_i64(); 363 bx = tcg_temp_new_i64(); 364 365 gen_load_gpr(ax, a->rs); 366 gen_load_gpr(bx, a->rt); 367 gen_pextw(cpu_gpr[a->rd], cpu_gpr_hi[a->rd], ax, bx); 368 369 tcg_temp_free(bx); 370 tcg_temp_free(ax); 371 372 return true; 373 } 374 375 /* Parallel Extend Upper from Word */ 376 static bool trans_PEXTUW(DisasContext *ctx, arg_rtype *a) 377 { 378 TCGv_i64 ax, bx; 379 380 if (a->rd == 0) { 381 /* nop */ 382 return true; 383 } 384 385 ax = tcg_temp_new_i64(); 386 bx = tcg_temp_new_i64(); 387 388 gen_load_gpr_hi(ax, a->rs); 389 gen_load_gpr_hi(bx, a->rt); 390 gen_pextw(cpu_gpr[a->rd], cpu_gpr_hi[a->rd], ax, bx); 391 392 tcg_temp_free(bx); 393 tcg_temp_free(ax); 394 395 return true; 396 } 397 398 /* 399 * Others (16 instructions) 400 * ------------------------ 401 * PCPYH rd, rt Parallel Copy Halfword 402 * PCPYLD rd, rs, rt Parallel Copy Lower Doubleword 403 * PCPYUD rd, rs, rt Parallel Copy Upper Doubleword 404 * PREVH rd, rt Parallel Reverse Halfword 405 * PINTH rd, rs, rt Parallel Interleave Halfword 406 * PINTEH rd, rs, rt Parallel Interleave Even Halfword 407 * PEXEH rd, rt Parallel Exchange Even Halfword 408 * PEXCH rd, rt Parallel Exchange Center Halfword 409 * PEXEW rd, rt Parallel Exchange Even Word 410 * PEXCW rd, rt Parallel Exchange Center Word 411 * QFSRV rd, rs, rt Quadword Funnel Shift Right Variable 412 * MFSA rd Move from Shift Amount Register 413 * MTSA rs Move to Shift Amount Register 414 * MTSAB rs, immediate Move Byte Count to Shift Amount Register 415 * MTSAH rs, immediate Move Halfword Count to Shift Amount Register 416 * PROT3W rd, rt Parallel Rotate 3 Words 417 */ 418 419 /* Parallel Copy Halfword */ 420 static bool trans_PCPYH(DisasContext *s, arg_rtype *a) 421 { 422 if (a->rd == 0) { 423 /* nop */ 424 return true; 425 } 426 427 if (a->rt == 0) { 428 tcg_gen_movi_i64(cpu_gpr[a->rd], 0); 429 tcg_gen_movi_i64(cpu_gpr_hi[a->rd], 0); 430 return true; 431 } 432 433 tcg_gen_deposit_i64(cpu_gpr[a->rd], cpu_gpr[a->rt], cpu_gpr[a->rt], 16, 16); 434 tcg_gen_deposit_i64(cpu_gpr[a->rd], cpu_gpr[a->rd], cpu_gpr[a->rd], 32, 32); 435 tcg_gen_deposit_i64(cpu_gpr_hi[a->rd], cpu_gpr_hi[a->rt], cpu_gpr_hi[a->rt], 16, 16); 436 tcg_gen_deposit_i64(cpu_gpr_hi[a->rd], cpu_gpr_hi[a->rd], cpu_gpr_hi[a->rd], 32, 32); 437 438 return true; 439 } 440 441 /* Parallel Copy Lower Doubleword */ 442 static bool trans_PCPYLD(DisasContext *s, arg_rtype *a) 443 { 444 if (a->rd == 0) { 445 /* nop */ 446 return true; 447 } 448 449 if (a->rs == 0) { 450 tcg_gen_movi_i64(cpu_gpr_hi[a->rd], 0); 451 } else { 452 tcg_gen_mov_i64(cpu_gpr_hi[a->rd], cpu_gpr[a->rs]); 453 } 454 455 if (a->rt == 0) { 456 tcg_gen_movi_i64(cpu_gpr[a->rd], 0); 457 } else if (a->rd != a->rt) { 458 tcg_gen_mov_i64(cpu_gpr[a->rd], cpu_gpr[a->rt]); 459 } 460 461 return true; 462 } 463 464 /* Parallel Copy Upper Doubleword */ 465 static bool trans_PCPYUD(DisasContext *s, arg_rtype *a) 466 { 467 if (a->rd == 0) { 468 /* nop */ 469 return true; 470 } 471 472 gen_load_gpr_hi(cpu_gpr[a->rd], a->rs); 473 474 if (a->rt == 0) { 475 tcg_gen_movi_i64(cpu_gpr_hi[a->rd], 0); 476 } else if (a->rd != a->rt) { 477 tcg_gen_mov_i64(cpu_gpr_hi[a->rd], cpu_gpr_hi[a->rt]); 478 } 479 480 return true; 481 } 482