| 1 | /* |
| 2 | * Copyright (c) 2012-2014 Bastian Koppelmann C-Lab/University Paderborn |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "qemu/osdep.h" |
| 18 | #include "cpu.h" |
| 19 | #include "qemu/host-utils.h" |
| 20 | #include "exec/helper-proto.h" |
| 21 | #include "accel/tcg/cpu-ldst.h" |
| 22 | #include "accel/tcg/cpu-loop.h" |
| 23 | #include "qemu/plugin.h" |
| 24 | #include <zlib.h> /* for crc32 */ |
| 25 | |
| 26 | |
| 27 | /* Exception helpers */ |
| 28 | |
| 29 | static G_NORETURN |
| 30 | void raise_exception_sync_internal(CPUTriCoreState *env, uint32_t class, int tin, |
| 31 | uintptr_t pc, uint32_t fcd_pc) |
| 32 | { |
| 33 | CPUState *cs = env_cpu(env); |
| 34 | uint64_t last_pc; |
| 35 | |
| 36 | /* in case we come from a helper-call we need to restore the PC */ |
| 37 | cpu_restore_state(cs, pc); |
| 38 | last_pc = env->PC; |
| 39 | |
| 40 | /* Tin is loaded into d[15] */ |
| 41 | env->gpr_d[15] = tin; |
| 42 | |
| 43 | if (class == TRAPC_CTX_MNG && tin == TIN3_FCU) { |
| 44 | /* upper context cannot be saved, if the context list is empty */ |
| 45 | } else { |
| 46 | helper_svucx(env); |
| 47 | } |
| 48 | |
| 49 | /* The return address in a[11] is updated */ |
| 50 | if (class == TRAPC_CTX_MNG && tin == TIN3_FCD) { |
| 51 | env->SYSCON |= MASK_SYSCON_FCD_SF; |
| 52 | /* when we run out of CSAs after saving a context a FCD trap is taken |
| 53 | and the return address is the start of the trap handler which used |
| 54 | the last CSA */ |
| 55 | env->gpr_a[11] = fcd_pc; |
| 56 | } else if (class == TRAPC_SYSCALL) { |
| 57 | env->gpr_a[11] = env->PC + 4; |
| 58 | } else { |
| 59 | env->gpr_a[11] = env->PC; |
| 60 | } |
| 61 | /* The stack pointer in A[10] is set to the Interrupt Stack Pointer (ISP) |
| 62 | when the processor was not previously using the interrupt stack |
| 63 | (in case of PSW.IS = 0). The stack pointer bit is set for using the |
| 64 | interrupt stack: PSW.IS = 1. */ |
| 65 | if ((env->PSW & MASK_PSW_IS) == 0) { |
| 66 | env->gpr_a[10] = env->ISP; |
| 67 | } |
| 68 | env->PSW |= MASK_PSW_IS; |
| 69 | /* The I/O mode is set to Supervisor mode, which means all permissions |
| 70 | are enabled: PSW.IO = 10 B .*/ |
| 71 | env->PSW |= (2 << 10); |
| 72 | |
| 73 | /*The current Protection Register Set is set to 0: PSW.PRS = 00 B .*/ |
| 74 | env->PSW &= ~MASK_PSW_PRS; |
| 75 | |
| 76 | /* The Call Depth Counter (CDC) is cleared, and the call depth limit is |
| 77 | set for 64: PSW.CDC = 0000000 B .*/ |
| 78 | env->PSW &= ~MASK_PSW_CDC; |
| 79 | |
| 80 | /* Call Depth Counter is enabled, PSW.CDE = 1. */ |
| 81 | env->PSW |= MASK_PSW_CDE; |
| 82 | |
| 83 | /* Write permission to global registers A[0], A[1], A[8], A[9] is |
| 84 | disabled: PSW.GW = 0. */ |
| 85 | env->PSW &= ~MASK_PSW_GW; |
| 86 | |
| 87 | /*The interrupt system is globally disabled: ICR.IE = 0. The ‘old’ |
| 88 | ICR.IE and ICR.CCPN are saved */ |
| 89 | |
| 90 | /* PCXI.PIE = ICR.IE */ |
| 91 | pcxi_set_pie(env, icr_get_ie(env)); |
| 92 | |
| 93 | /* PCXI.PCPN = ICR.CCPN */ |
| 94 | pcxi_set_pcpn(env, icr_get_ccpn(env)); |
| 95 | /* Update PC using the trap vector table */ |
| 96 | env->PC = env->BTV | (class << 5); |
| 97 | |
| 98 | qemu_plugin_vcpu_exception_cb(cs, last_pc); |
| 99 | cpu_loop_exit(cs); |
| 100 | } |
| 101 | |
| 102 | void helper_raise_exception_sync(CPUTriCoreState *env, uint32_t class, |
| 103 | uint32_t tin) |
| 104 | { |
| 105 | raise_exception_sync_internal(env, class, tin, 0, 0); |
| 106 | } |
| 107 | |
| 108 | static void raise_exception_sync_helper(CPUTriCoreState *env, uint32_t class, |
| 109 | uint32_t tin, uintptr_t pc) |
| 110 | { |
| 111 | raise_exception_sync_internal(env, class, tin, pc, 0); |
| 112 | } |
| 113 | |
| 114 | /* Addressing mode helper */ |
| 115 | |
| 116 | static uint16_t reverse16(uint16_t val) |
| 117 | { |
| 118 | uint8_t high = (uint8_t)(val >> 8); |
| 119 | uint8_t low = (uint8_t)(val & 0xff); |
| 120 | |
| 121 | uint16_t rh, rl; |
| 122 | |
| 123 | rl = (uint16_t)((high * 0x0202020202ULL & 0x010884422010ULL) % 1023); |
| 124 | rh = (uint16_t)((low * 0x0202020202ULL & 0x010884422010ULL) % 1023); |
| 125 | |
| 126 | return (rh << 8) | rl; |
| 127 | } |
| 128 | |
| 129 | uint32_t helper_br_update(uint32_t reg) |
| 130 | { |
| 131 | uint32_t index = reg & 0xffff; |
| 132 | uint32_t incr = reg >> 16; |
| 133 | uint32_t new_index = reverse16(reverse16(index) + reverse16(incr)); |
| 134 | return reg - index + new_index; |
| 135 | } |
| 136 | |
| 137 | uint32_t helper_circ_update(uint32_t reg, uint32_t off) |
| 138 | { |
| 139 | uint32_t index = reg & 0xffff; |
| 140 | uint32_t length = reg >> 16; |
| 141 | int32_t new_index = index + off; |
| 142 | if (new_index < 0) { |
| 143 | new_index += length; |
| 144 | } else { |
| 145 | new_index %= length; |
| 146 | } |
| 147 | return reg - index + new_index; |
| 148 | } |
| 149 | |
| 150 | static uint32_t ssov32(CPUTriCoreState *env, int64_t arg) |
| 151 | { |
| 152 | uint32_t ret; |
| 153 | int64_t max_pos = INT32_MAX; |
| 154 | int64_t max_neg = INT32_MIN; |
| 155 | if (arg > max_pos) { |
| 156 | env->PSW_USB_V = (1 << 31); |
| 157 | env->PSW_USB_SV = (1 << 31); |
| 158 | ret = (uint32_t)max_pos; |
| 159 | } else { |
| 160 | if (arg < max_neg) { |
| 161 | env->PSW_USB_V = (1 << 31); |
| 162 | env->PSW_USB_SV = (1 << 31); |
| 163 | ret = (uint32_t)max_neg; |
| 164 | } else { |
| 165 | env->PSW_USB_V = 0; |
| 166 | ret = (uint32_t)arg; |
| 167 | } |
| 168 | } |
| 169 | env->PSW_USB_AV = arg ^ arg * 2u; |
| 170 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static uint32_t suov32_pos(CPUTriCoreState *env, uint64_t arg) |
| 175 | { |
| 176 | uint32_t ret; |
| 177 | uint64_t max_pos = UINT32_MAX; |
| 178 | if (arg > max_pos) { |
| 179 | env->PSW_USB_V = (1 << 31); |
| 180 | env->PSW_USB_SV = (1 << 31); |
| 181 | ret = (uint32_t)max_pos; |
| 182 | } else { |
| 183 | env->PSW_USB_V = 0; |
| 184 | ret = (uint32_t)arg; |
| 185 | } |
| 186 | env->PSW_USB_AV = arg ^ arg * 2u; |
| 187 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | static uint32_t suov32_neg(CPUTriCoreState *env, int64_t arg) |
| 192 | { |
| 193 | uint32_t ret; |
| 194 | |
| 195 | if (arg < 0) { |
| 196 | env->PSW_USB_V = (1 << 31); |
| 197 | env->PSW_USB_SV = (1 << 31); |
| 198 | ret = 0; |
| 199 | } else { |
| 200 | env->PSW_USB_V = 0; |
| 201 | ret = (uint32_t)arg; |
| 202 | } |
| 203 | env->PSW_USB_AV = arg ^ arg * 2u; |
| 204 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static uint32_t ssov16(CPUTriCoreState *env, int32_t hw0, int32_t hw1) |
| 209 | { |
| 210 | int32_t max_pos = INT16_MAX; |
| 211 | int32_t max_neg = INT16_MIN; |
| 212 | int32_t av0, av1; |
| 213 | |
| 214 | env->PSW_USB_V = 0; |
| 215 | av0 = hw0 ^ hw0 * 2u; |
| 216 | if (hw0 > max_pos) { |
| 217 | env->PSW_USB_V = (1 << 31); |
| 218 | hw0 = max_pos; |
| 219 | } else if (hw0 < max_neg) { |
| 220 | env->PSW_USB_V = (1 << 31); |
| 221 | hw0 = max_neg; |
| 222 | } |
| 223 | |
| 224 | av1 = hw1 ^ hw1 * 2u; |
| 225 | if (hw1 > max_pos) { |
| 226 | env->PSW_USB_V = (1 << 31); |
| 227 | hw1 = max_pos; |
| 228 | } else if (hw1 < max_neg) { |
| 229 | env->PSW_USB_V = (1 << 31); |
| 230 | hw1 = max_neg; |
| 231 | } |
| 232 | |
| 233 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 234 | env->PSW_USB_AV = (av0 | av1) << 16; |
| 235 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 236 | return (hw0 & 0xffff) | (hw1 << 16); |
| 237 | } |
| 238 | |
| 239 | static uint32_t suov16(CPUTriCoreState *env, int32_t hw0, int32_t hw1) |
| 240 | { |
| 241 | int32_t max_pos = UINT16_MAX; |
| 242 | int32_t av0, av1; |
| 243 | |
| 244 | env->PSW_USB_V = 0; |
| 245 | av0 = hw0 ^ hw0 * 2u; |
| 246 | if (hw0 > max_pos) { |
| 247 | env->PSW_USB_V = (1 << 31); |
| 248 | hw0 = max_pos; |
| 249 | } else if (hw0 < 0) { |
| 250 | env->PSW_USB_V = (1 << 31); |
| 251 | hw0 = 0; |
| 252 | } |
| 253 | |
| 254 | av1 = hw1 ^ hw1 * 2u; |
| 255 | if (hw1 > max_pos) { |
| 256 | env->PSW_USB_V = (1 << 31); |
| 257 | hw1 = max_pos; |
| 258 | } else if (hw1 < 0) { |
| 259 | env->PSW_USB_V = (1 << 31); |
| 260 | hw1 = 0; |
| 261 | } |
| 262 | |
| 263 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 264 | env->PSW_USB_AV = (av0 | av1) << 16; |
| 265 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 266 | return (hw0 & 0xffff) | (hw1 << 16); |
| 267 | } |
| 268 | |
| 269 | uint32_t helper_add_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 270 | { |
| 271 | int64_t t1 = sextract64(r1, 0, 32); |
| 272 | int64_t t2 = sextract64(r2, 0, 32); |
| 273 | int64_t result = t1 + t2; |
| 274 | return ssov32(env, result); |
| 275 | } |
| 276 | |
| 277 | uint64_t helper_add64_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2) |
| 278 | { |
| 279 | uint64_t result; |
| 280 | int64_t ovf; |
| 281 | |
| 282 | result = r1 + r2; |
| 283 | ovf = (result ^ r1) & ~(r1 ^ r2); |
| 284 | env->PSW_USB_AV = (result ^ result * 2u) >> 32; |
| 285 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 286 | if (ovf < 0) { |
| 287 | env->PSW_USB_V = (1 << 31); |
| 288 | env->PSW_USB_SV = (1 << 31); |
| 289 | /* ext_ret > MAX_INT */ |
| 290 | if ((int64_t)r1 >= 0) { |
| 291 | result = INT64_MAX; |
| 292 | /* ext_ret < MIN_INT */ |
| 293 | } else { |
| 294 | result = INT64_MIN; |
| 295 | } |
| 296 | } else { |
| 297 | env->PSW_USB_V = 0; |
| 298 | } |
| 299 | return result; |
| 300 | } |
| 301 | |
| 302 | uint32_t helper_add_h_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 303 | { |
| 304 | int32_t ret_hw0, ret_hw1; |
| 305 | |
| 306 | ret_hw0 = sextract32(r1, 0, 16) + sextract32(r2, 0, 16); |
| 307 | ret_hw1 = sextract32(r1, 16, 16) + sextract32(r2, 16, 16); |
| 308 | return ssov16(env, ret_hw0, ret_hw1); |
| 309 | } |
| 310 | |
| 311 | uint32_t helper_addr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 312 | uint32_t r2_h) |
| 313 | { |
| 314 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 315 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 316 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 317 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 318 | int64_t result0, result1; |
| 319 | uint32_t ovf0, ovf1; |
| 320 | uint32_t avf0, avf1; |
| 321 | |
| 322 | ovf0 = ovf1 = 0; |
| 323 | |
| 324 | result0 = r2_low + mul_res0 + 0x8000; |
| 325 | result1 = r2_high + mul_res1 + 0x8000; |
| 326 | |
| 327 | avf0 = result0 * 2u; |
| 328 | avf0 = result0 ^ avf0; |
| 329 | avf1 = result1 * 2u; |
| 330 | avf1 = result1 ^ avf1; |
| 331 | |
| 332 | if (result0 > INT32_MAX) { |
| 333 | ovf0 = (1 << 31); |
| 334 | result0 = INT32_MAX; |
| 335 | } else if (result0 < INT32_MIN) { |
| 336 | ovf0 = (1 << 31); |
| 337 | result0 = INT32_MIN; |
| 338 | } |
| 339 | |
| 340 | if (result1 > INT32_MAX) { |
| 341 | ovf1 = (1 << 31); |
| 342 | result1 = INT32_MAX; |
| 343 | } else if (result1 < INT32_MIN) { |
| 344 | ovf1 = (1 << 31); |
| 345 | result1 = INT32_MIN; |
| 346 | } |
| 347 | |
| 348 | env->PSW_USB_V = ovf0 | ovf1; |
| 349 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 350 | |
| 351 | env->PSW_USB_AV = avf0 | avf1; |
| 352 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 353 | |
| 354 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 355 | } |
| 356 | |
| 357 | uint32_t helper_addsur_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 358 | uint32_t r2_h) |
| 359 | { |
| 360 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 361 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 362 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 363 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 364 | int64_t result0, result1; |
| 365 | uint32_t ovf0, ovf1; |
| 366 | uint32_t avf0, avf1; |
| 367 | |
| 368 | ovf0 = ovf1 = 0; |
| 369 | |
| 370 | result0 = r2_low - mul_res0 + 0x8000; |
| 371 | result1 = r2_high + mul_res1 + 0x8000; |
| 372 | |
| 373 | avf0 = result0 * 2u; |
| 374 | avf0 = result0 ^ avf0; |
| 375 | avf1 = result1 * 2u; |
| 376 | avf1 = result1 ^ avf1; |
| 377 | |
| 378 | if (result0 > INT32_MAX) { |
| 379 | ovf0 = (1 << 31); |
| 380 | result0 = INT32_MAX; |
| 381 | } else if (result0 < INT32_MIN) { |
| 382 | ovf0 = (1 << 31); |
| 383 | result0 = INT32_MIN; |
| 384 | } |
| 385 | |
| 386 | if (result1 > INT32_MAX) { |
| 387 | ovf1 = (1 << 31); |
| 388 | result1 = INT32_MAX; |
| 389 | } else if (result1 < INT32_MIN) { |
| 390 | ovf1 = (1 << 31); |
| 391 | result1 = INT32_MIN; |
| 392 | } |
| 393 | |
| 394 | env->PSW_USB_V = ovf0 | ovf1; |
| 395 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 396 | |
| 397 | env->PSW_USB_AV = avf0 | avf1; |
| 398 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 399 | |
| 400 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | uint32_t helper_add_suov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 405 | { |
| 406 | int64_t t1 = extract64(r1, 0, 32); |
| 407 | int64_t t2 = extract64(r2, 0, 32); |
| 408 | int64_t result = t1 + t2; |
| 409 | return suov32_pos(env, result); |
| 410 | } |
| 411 | |
| 412 | uint32_t helper_add_h_suov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 413 | { |
| 414 | int32_t ret_hw0, ret_hw1; |
| 415 | |
| 416 | ret_hw0 = extract32(r1, 0, 16) + extract32(r2, 0, 16); |
| 417 | ret_hw1 = extract32(r1, 16, 16) + extract32(r2, 16, 16); |
| 418 | return suov16(env, ret_hw0, ret_hw1); |
| 419 | } |
| 420 | |
| 421 | uint32_t helper_sub_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 422 | { |
| 423 | int64_t t1 = sextract64(r1, 0, 32); |
| 424 | int64_t t2 = sextract64(r2, 0, 32); |
| 425 | int64_t result = t1 - t2; |
| 426 | return ssov32(env, result); |
| 427 | } |
| 428 | |
| 429 | uint64_t helper_sub64_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2) |
| 430 | { |
| 431 | uint64_t result; |
| 432 | int64_t ovf; |
| 433 | |
| 434 | result = r1 - r2; |
| 435 | ovf = (result ^ r1) & (r1 ^ r2); |
| 436 | env->PSW_USB_AV = (result ^ result * 2u) >> 32; |
| 437 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 438 | if (ovf < 0) { |
| 439 | env->PSW_USB_V = (1 << 31); |
| 440 | env->PSW_USB_SV = (1 << 31); |
| 441 | /* ext_ret > MAX_INT */ |
| 442 | if ((int64_t)r1 >= 0) { |
| 443 | result = INT64_MAX; |
| 444 | /* ext_ret < MIN_INT */ |
| 445 | } else { |
| 446 | result = INT64_MIN; |
| 447 | } |
| 448 | } else { |
| 449 | env->PSW_USB_V = 0; |
| 450 | } |
| 451 | return result; |
| 452 | } |
| 453 | |
| 454 | uint32_t helper_sub_h_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 455 | { |
| 456 | int32_t ret_hw0, ret_hw1; |
| 457 | |
| 458 | ret_hw0 = sextract32(r1, 0, 16) - sextract32(r2, 0, 16); |
| 459 | ret_hw1 = sextract32(r1, 16, 16) - sextract32(r2, 16, 16); |
| 460 | return ssov16(env, ret_hw0, ret_hw1); |
| 461 | } |
| 462 | |
| 463 | uint32_t helper_subr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 464 | uint32_t r2_h) |
| 465 | { |
| 466 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 467 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 468 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 469 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 470 | int64_t result0, result1; |
| 471 | uint32_t ovf0, ovf1; |
| 472 | uint32_t avf0, avf1; |
| 473 | |
| 474 | ovf0 = ovf1 = 0; |
| 475 | |
| 476 | result0 = r2_low - mul_res0 + 0x8000; |
| 477 | result1 = r2_high - mul_res1 + 0x8000; |
| 478 | |
| 479 | avf0 = result0 * 2u; |
| 480 | avf0 = result0 ^ avf0; |
| 481 | avf1 = result1 * 2u; |
| 482 | avf1 = result1 ^ avf1; |
| 483 | |
| 484 | if (result0 > INT32_MAX) { |
| 485 | ovf0 = (1 << 31); |
| 486 | result0 = INT32_MAX; |
| 487 | } else if (result0 < INT32_MIN) { |
| 488 | ovf0 = (1 << 31); |
| 489 | result0 = INT32_MIN; |
| 490 | } |
| 491 | |
| 492 | if (result1 > INT32_MAX) { |
| 493 | ovf1 = (1 << 31); |
| 494 | result1 = INT32_MAX; |
| 495 | } else if (result1 < INT32_MIN) { |
| 496 | ovf1 = (1 << 31); |
| 497 | result1 = INT32_MIN; |
| 498 | } |
| 499 | |
| 500 | env->PSW_USB_V = ovf0 | ovf1; |
| 501 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 502 | |
| 503 | env->PSW_USB_AV = avf0 | avf1; |
| 504 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 505 | |
| 506 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 507 | } |
| 508 | |
| 509 | uint32_t helper_subadr_h_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 510 | uint32_t r2_h) |
| 511 | { |
| 512 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 513 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 514 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 515 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 516 | int64_t result0, result1; |
| 517 | uint32_t ovf0, ovf1; |
| 518 | uint32_t avf0, avf1; |
| 519 | |
| 520 | ovf0 = ovf1 = 0; |
| 521 | |
| 522 | result0 = r2_low + mul_res0 + 0x8000; |
| 523 | result1 = r2_high - mul_res1 + 0x8000; |
| 524 | |
| 525 | avf0 = result0 * 2u; |
| 526 | avf0 = result0 ^ avf0; |
| 527 | avf1 = result1 * 2u; |
| 528 | avf1 = result1 ^ avf1; |
| 529 | |
| 530 | if (result0 > INT32_MAX) { |
| 531 | ovf0 = (1 << 31); |
| 532 | result0 = INT32_MAX; |
| 533 | } else if (result0 < INT32_MIN) { |
| 534 | ovf0 = (1 << 31); |
| 535 | result0 = INT32_MIN; |
| 536 | } |
| 537 | |
| 538 | if (result1 > INT32_MAX) { |
| 539 | ovf1 = (1 << 31); |
| 540 | result1 = INT32_MAX; |
| 541 | } else if (result1 < INT32_MIN) { |
| 542 | ovf1 = (1 << 31); |
| 543 | result1 = INT32_MIN; |
| 544 | } |
| 545 | |
| 546 | env->PSW_USB_V = ovf0 | ovf1; |
| 547 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 548 | |
| 549 | env->PSW_USB_AV = avf0 | avf1; |
| 550 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 551 | |
| 552 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 553 | } |
| 554 | |
| 555 | uint32_t helper_sub_suov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 556 | { |
| 557 | int64_t t1 = extract64(r1, 0, 32); |
| 558 | int64_t t2 = extract64(r2, 0, 32); |
| 559 | int64_t result = t1 - t2; |
| 560 | return suov32_neg(env, result); |
| 561 | } |
| 562 | |
| 563 | uint32_t helper_sub_h_suov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 564 | { |
| 565 | int32_t ret_hw0, ret_hw1; |
| 566 | |
| 567 | ret_hw0 = extract32(r1, 0, 16) - extract32(r2, 0, 16); |
| 568 | ret_hw1 = extract32(r1, 16, 16) - extract32(r2, 16, 16); |
| 569 | return suov16(env, ret_hw0, ret_hw1); |
| 570 | } |
| 571 | |
| 572 | uint32_t helper_mul_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 573 | { |
| 574 | int64_t t1 = sextract64(r1, 0, 32); |
| 575 | int64_t t2 = sextract64(r2, 0, 32); |
| 576 | int64_t result = t1 * t2; |
| 577 | return ssov32(env, result); |
| 578 | } |
| 579 | |
| 580 | uint32_t helper_mul_suov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 581 | { |
| 582 | int64_t t1 = extract64(r1, 0, 32); |
| 583 | int64_t t2 = extract64(r2, 0, 32); |
| 584 | int64_t result = t1 * t2; |
| 585 | |
| 586 | return suov32_pos(env, result); |
| 587 | } |
| 588 | |
| 589 | uint32_t helper_sha_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 590 | { |
| 591 | int64_t t1 = sextract64(r1, 0, 32); |
| 592 | int32_t t2 = sextract64(r2, 0, 6); |
| 593 | int64_t result; |
| 594 | if (t2 == 0) { |
| 595 | result = t1; |
| 596 | } else if (t2 > 0) { |
| 597 | result = t1 << t2; |
| 598 | } else { |
| 599 | result = t1 >> -t2; |
| 600 | } |
| 601 | return ssov32(env, result); |
| 602 | } |
| 603 | |
| 604 | uint32_t helper_abs_ssov(CPUTriCoreState *env, uint32_t r1) |
| 605 | { |
| 606 | uint32_t result; |
| 607 | result = ((int32_t)r1 >= 0) ? r1 : (0 - r1); |
| 608 | return ssov32(env, result); |
| 609 | } |
| 610 | |
| 611 | uint32_t helper_abs_h_ssov(CPUTriCoreState *env, uint32_t r1) |
| 612 | { |
| 613 | int32_t ret_h0, ret_h1; |
| 614 | |
| 615 | ret_h0 = sextract32(r1, 0, 16); |
| 616 | ret_h0 = (ret_h0 >= 0) ? ret_h0 : (0 - ret_h0); |
| 617 | |
| 618 | ret_h1 = sextract32(r1, 16, 16); |
| 619 | ret_h1 = (ret_h1 >= 0) ? ret_h1 : (0 - ret_h1); |
| 620 | |
| 621 | return ssov16(env, ret_h0, ret_h1); |
| 622 | } |
| 623 | |
| 624 | uint32_t helper_absdif_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 625 | { |
| 626 | int64_t t1 = sextract64(r1, 0, 32); |
| 627 | int64_t t2 = sextract64(r2, 0, 32); |
| 628 | int64_t result; |
| 629 | |
| 630 | if (t1 > t2) { |
| 631 | result = t1 - t2; |
| 632 | } else { |
| 633 | result = t2 - t1; |
| 634 | } |
| 635 | return ssov32(env, result); |
| 636 | } |
| 637 | |
| 638 | uint32_t helper_absdif_h_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 639 | { |
| 640 | int32_t t1, t2; |
| 641 | int32_t ret_h0, ret_h1; |
| 642 | |
| 643 | t1 = sextract32(r1, 0, 16); |
| 644 | t2 = sextract32(r2, 0, 16); |
| 645 | if (t1 > t2) { |
| 646 | ret_h0 = t1 - t2; |
| 647 | } else { |
| 648 | ret_h0 = t2 - t1; |
| 649 | } |
| 650 | |
| 651 | t1 = sextract32(r1, 16, 16); |
| 652 | t2 = sextract32(r2, 16, 16); |
| 653 | if (t1 > t2) { |
| 654 | ret_h1 = t1 - t2; |
| 655 | } else { |
| 656 | ret_h1 = t2 - t1; |
| 657 | } |
| 658 | |
| 659 | return ssov16(env, ret_h0, ret_h1); |
| 660 | } |
| 661 | |
| 662 | uint32_t helper_madd32_ssov(CPUTriCoreState *env, uint32_t r1, |
| 663 | uint32_t r2, uint32_t r3) |
| 664 | { |
| 665 | int64_t t1 = sextract64(r1, 0, 32); |
| 666 | int64_t t2 = sextract64(r2, 0, 32); |
| 667 | int64_t t3 = sextract64(r3, 0, 32); |
| 668 | int64_t result; |
| 669 | |
| 670 | result = t2 + (t1 * t3); |
| 671 | return ssov32(env, result); |
| 672 | } |
| 673 | |
| 674 | uint32_t helper_madd32_suov(CPUTriCoreState *env, uint32_t r1, |
| 675 | uint32_t r2, uint32_t r3) |
| 676 | { |
| 677 | uint64_t t1 = extract64(r1, 0, 32); |
| 678 | uint64_t t2 = extract64(r2, 0, 32); |
| 679 | uint64_t t3 = extract64(r3, 0, 32); |
| 680 | int64_t result; |
| 681 | |
| 682 | result = t2 + (t1 * t3); |
| 683 | return suov32_pos(env, result); |
| 684 | } |
| 685 | |
| 686 | uint64_t helper_madd64_ssov(CPUTriCoreState *env, uint32_t r1, |
| 687 | uint64_t r2, uint32_t r3) |
| 688 | { |
| 689 | uint64_t ret, ovf; |
| 690 | int64_t t1 = sextract64(r1, 0, 32); |
| 691 | int64_t t3 = sextract64(r3, 0, 32); |
| 692 | int64_t mul; |
| 693 | |
| 694 | mul = t1 * t3; |
| 695 | ret = mul + r2; |
| 696 | ovf = (ret ^ mul) & ~(mul ^ r2); |
| 697 | |
| 698 | t1 = ret >> 32; |
| 699 | env->PSW_USB_AV = t1 ^ t1 * 2u; |
| 700 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 701 | |
| 702 | if ((int64_t)ovf < 0) { |
| 703 | env->PSW_USB_V = (1 << 31); |
| 704 | env->PSW_USB_SV = (1 << 31); |
| 705 | /* ext_ret > MAX_INT */ |
| 706 | if (mul >= 0) { |
| 707 | ret = INT64_MAX; |
| 708 | /* ext_ret < MIN_INT */ |
| 709 | } else { |
| 710 | ret = INT64_MIN; |
| 711 | } |
| 712 | } else { |
| 713 | env->PSW_USB_V = 0; |
| 714 | } |
| 715 | |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | uint32_t |
| 720 | helper_madd32_q_add_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2) |
| 721 | { |
| 722 | int64_t result; |
| 723 | |
| 724 | result = (r1 + r2); |
| 725 | |
| 726 | env->PSW_USB_AV = (result ^ result * 2u); |
| 727 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 728 | |
| 729 | /* we do the saturation by hand, since we produce an overflow on the host |
| 730 | if the mul before was (0x80000000 * 0x80000000) << 1). If this is the |
| 731 | case, we flip the saturated value. */ |
| 732 | if (r2 == 0x8000000000000000LL) { |
| 733 | if (result > 0x7fffffffLL) { |
| 734 | env->PSW_USB_V = (1 << 31); |
| 735 | env->PSW_USB_SV = (1 << 31); |
| 736 | result = INT32_MIN; |
| 737 | } else if (result < -0x80000000LL) { |
| 738 | env->PSW_USB_V = (1 << 31); |
| 739 | env->PSW_USB_SV = (1 << 31); |
| 740 | result = INT32_MAX; |
| 741 | } else { |
| 742 | env->PSW_USB_V = 0; |
| 743 | } |
| 744 | } else { |
| 745 | if (result > 0x7fffffffLL) { |
| 746 | env->PSW_USB_V = (1 << 31); |
| 747 | env->PSW_USB_SV = (1 << 31); |
| 748 | result = INT32_MAX; |
| 749 | } else if (result < -0x80000000LL) { |
| 750 | env->PSW_USB_V = (1 << 31); |
| 751 | env->PSW_USB_SV = (1 << 31); |
| 752 | result = INT32_MIN; |
| 753 | } else { |
| 754 | env->PSW_USB_V = 0; |
| 755 | } |
| 756 | } |
| 757 | return (uint32_t)result; |
| 758 | } |
| 759 | |
| 760 | uint64_t helper_madd64_q_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2, |
| 761 | uint32_t r3, uint32_t n) |
| 762 | { |
| 763 | int64_t t1 = (int64_t)r1; |
| 764 | int64_t t2 = sextract64(r2, 0, 32); |
| 765 | int64_t t3 = sextract64(r3, 0, 32); |
| 766 | int64_t result, mul; |
| 767 | int64_t ovf; |
| 768 | |
| 769 | mul = (t2 * t3) << n; |
| 770 | result = mul + t1; |
| 771 | |
| 772 | env->PSW_USB_AV = (result ^ result * 2u) >> 32; |
| 773 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 774 | |
| 775 | ovf = (result ^ mul) & ~(mul ^ t1); |
| 776 | /* we do the saturation by hand, since we produce an overflow on the host |
| 777 | if the mul was (0x80000000 * 0x80000000) << 1). If this is the |
| 778 | case, we flip the saturated value. */ |
| 779 | if ((r2 == 0x80000000) && (r3 == 0x80000000) && (n == 1)) { |
| 780 | if (ovf >= 0) { |
| 781 | env->PSW_USB_V = (1 << 31); |
| 782 | env->PSW_USB_SV = (1 << 31); |
| 783 | /* ext_ret > MAX_INT */ |
| 784 | if (mul < 0) { |
| 785 | result = INT64_MAX; |
| 786 | /* ext_ret < MIN_INT */ |
| 787 | } else { |
| 788 | result = INT64_MIN; |
| 789 | } |
| 790 | } else { |
| 791 | env->PSW_USB_V = 0; |
| 792 | } |
| 793 | } else { |
| 794 | if (ovf < 0) { |
| 795 | env->PSW_USB_V = (1 << 31); |
| 796 | env->PSW_USB_SV = (1 << 31); |
| 797 | /* ext_ret > MAX_INT */ |
| 798 | if (mul >= 0) { |
| 799 | result = INT64_MAX; |
| 800 | /* ext_ret < MIN_INT */ |
| 801 | } else { |
| 802 | result = INT64_MIN; |
| 803 | } |
| 804 | } else { |
| 805 | env->PSW_USB_V = 0; |
| 806 | } |
| 807 | } |
| 808 | return (uint64_t)result; |
| 809 | } |
| 810 | |
| 811 | uint32_t helper_maddr_q_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2, |
| 812 | uint32_t r3, uint32_t n) |
| 813 | { |
| 814 | int64_t t1 = sextract64(r1, 0, 32); |
| 815 | int64_t t2 = sextract64(r2, 0, 32); |
| 816 | int64_t t3 = sextract64(r3, 0, 32); |
| 817 | int64_t mul, ret; |
| 818 | |
| 819 | if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) { |
| 820 | mul = 0x7fffffff; |
| 821 | } else { |
| 822 | mul = (t2 * t3) << n; |
| 823 | } |
| 824 | |
| 825 | ret = t1 + mul + 0x8000; |
| 826 | |
| 827 | env->PSW_USB_AV = ret ^ ret * 2u; |
| 828 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 829 | |
| 830 | if (ret > 0x7fffffffll) { |
| 831 | env->PSW_USB_V = (1 << 31); |
| 832 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 833 | ret = INT32_MAX; |
| 834 | } else if (ret < -0x80000000ll) { |
| 835 | env->PSW_USB_V = (1 << 31); |
| 836 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 837 | ret = INT32_MIN; |
| 838 | } else { |
| 839 | env->PSW_USB_V = 0; |
| 840 | } |
| 841 | return ret & 0xffff0000ll; |
| 842 | } |
| 843 | |
| 844 | uint64_t helper_madd64_suov(CPUTriCoreState *env, uint32_t r1, |
| 845 | uint64_t r2, uint32_t r3) |
| 846 | { |
| 847 | uint64_t ret, mul; |
| 848 | uint64_t t1 = extract64(r1, 0, 32); |
| 849 | uint64_t t3 = extract64(r3, 0, 32); |
| 850 | |
| 851 | mul = t1 * t3; |
| 852 | ret = mul + r2; |
| 853 | |
| 854 | t1 = ret >> 32; |
| 855 | env->PSW_USB_AV = t1 ^ t1 * 2u; |
| 856 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 857 | |
| 858 | if (ret < r2) { |
| 859 | env->PSW_USB_V = (1 << 31); |
| 860 | env->PSW_USB_SV = (1 << 31); |
| 861 | /* saturate */ |
| 862 | ret = UINT64_MAX; |
| 863 | } else { |
| 864 | env->PSW_USB_V = 0; |
| 865 | } |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | uint32_t helper_msub32_ssov(CPUTriCoreState *env, uint32_t r1, |
| 870 | uint32_t r2, uint32_t r3) |
| 871 | { |
| 872 | int64_t t1 = sextract64(r1, 0, 32); |
| 873 | int64_t t2 = sextract64(r2, 0, 32); |
| 874 | int64_t t3 = sextract64(r3, 0, 32); |
| 875 | int64_t result; |
| 876 | |
| 877 | result = t2 - (t1 * t3); |
| 878 | return ssov32(env, result); |
| 879 | } |
| 880 | |
| 881 | uint32_t helper_msub32_suov(CPUTriCoreState *env, uint32_t r1, |
| 882 | uint32_t r2, uint32_t r3) |
| 883 | { |
| 884 | uint64_t t1 = extract64(r1, 0, 32); |
| 885 | uint64_t t2 = extract64(r2, 0, 32); |
| 886 | uint64_t t3 = extract64(r3, 0, 32); |
| 887 | uint64_t result; |
| 888 | uint64_t mul; |
| 889 | |
| 890 | mul = (t1 * t3); |
| 891 | result = t2 - mul; |
| 892 | |
| 893 | env->PSW_USB_AV = result ^ result * 2u; |
| 894 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 895 | /* we calculate ovf by hand here, because the multiplication can overflow on |
| 896 | the host, which would give false results if we compare to less than |
| 897 | zero */ |
| 898 | if (mul > t2) { |
| 899 | env->PSW_USB_V = (1 << 31); |
| 900 | env->PSW_USB_SV = (1 << 31); |
| 901 | result = 0; |
| 902 | } else { |
| 903 | env->PSW_USB_V = 0; |
| 904 | } |
| 905 | return result; |
| 906 | } |
| 907 | |
| 908 | uint64_t helper_msub64_ssov(CPUTriCoreState *env, uint32_t r1, |
| 909 | uint64_t r2, uint32_t r3) |
| 910 | { |
| 911 | uint64_t ret, ovf; |
| 912 | int64_t t1 = sextract64(r1, 0, 32); |
| 913 | int64_t t3 = sextract64(r3, 0, 32); |
| 914 | int64_t mul; |
| 915 | |
| 916 | mul = t1 * t3; |
| 917 | ret = r2 - mul; |
| 918 | ovf = (ret ^ r2) & (mul ^ r2); |
| 919 | |
| 920 | t1 = ret >> 32; |
| 921 | env->PSW_USB_AV = t1 ^ t1 * 2u; |
| 922 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 923 | |
| 924 | if ((int64_t)ovf < 0) { |
| 925 | env->PSW_USB_V = (1 << 31); |
| 926 | env->PSW_USB_SV = (1 << 31); |
| 927 | /* ext_ret > MAX_INT */ |
| 928 | if (mul < 0) { |
| 929 | ret = INT64_MAX; |
| 930 | /* ext_ret < MIN_INT */ |
| 931 | } else { |
| 932 | ret = INT64_MIN; |
| 933 | } |
| 934 | } else { |
| 935 | env->PSW_USB_V = 0; |
| 936 | } |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | uint64_t helper_msub64_suov(CPUTriCoreState *env, uint32_t r1, |
| 941 | uint64_t r2, uint32_t r3) |
| 942 | { |
| 943 | uint64_t ret, mul; |
| 944 | uint64_t t1 = extract64(r1, 0, 32); |
| 945 | uint64_t t3 = extract64(r3, 0, 32); |
| 946 | |
| 947 | mul = t1 * t3; |
| 948 | ret = r2 - mul; |
| 949 | |
| 950 | t1 = ret >> 32; |
| 951 | env->PSW_USB_AV = t1 ^ t1 * 2u; |
| 952 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 953 | |
| 954 | if (ret > r2) { |
| 955 | env->PSW_USB_V = (1 << 31); |
| 956 | env->PSW_USB_SV = (1 << 31); |
| 957 | /* saturate */ |
| 958 | ret = 0; |
| 959 | } else { |
| 960 | env->PSW_USB_V = 0; |
| 961 | } |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | uint32_t |
| 966 | helper_msub32_q_sub_ssov(CPUTriCoreState *env, uint64_t r1, uint64_t r2) |
| 967 | { |
| 968 | int64_t result; |
| 969 | int64_t t1 = (int64_t)r1; |
| 970 | int64_t t2 = (int64_t)r2; |
| 971 | |
| 972 | result = t1 - t2; |
| 973 | |
| 974 | env->PSW_USB_AV = (result ^ result * 2u); |
| 975 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 976 | |
| 977 | /* we do the saturation by hand, since we produce an overflow on the host |
| 978 | if the mul before was (0x80000000 * 0x80000000) << 1). If this is the |
| 979 | case, we flip the saturated value. */ |
| 980 | if (r2 == 0x8000000000000000LL) { |
| 981 | if (result > 0x7fffffffLL) { |
| 982 | env->PSW_USB_V = (1 << 31); |
| 983 | env->PSW_USB_SV = (1 << 31); |
| 984 | result = INT32_MIN; |
| 985 | } else if (result < -0x80000000LL) { |
| 986 | env->PSW_USB_V = (1 << 31); |
| 987 | env->PSW_USB_SV = (1 << 31); |
| 988 | result = INT32_MAX; |
| 989 | } else { |
| 990 | env->PSW_USB_V = 0; |
| 991 | } |
| 992 | } else { |
| 993 | if (result > 0x7fffffffLL) { |
| 994 | env->PSW_USB_V = (1 << 31); |
| 995 | env->PSW_USB_SV = (1 << 31); |
| 996 | result = INT32_MAX; |
| 997 | } else if (result < -0x80000000LL) { |
| 998 | env->PSW_USB_V = (1 << 31); |
| 999 | env->PSW_USB_SV = (1 << 31); |
| 1000 | result = INT32_MIN; |
| 1001 | } else { |
| 1002 | env->PSW_USB_V = 0; |
| 1003 | } |
| 1004 | } |
| 1005 | return (uint32_t)result; |
| 1006 | } |
| 1007 | |
| 1008 | uint64_t helper_msub64_q_ssov(CPUTriCoreState *env, uint64_t r1, uint32_t r2, |
| 1009 | uint32_t r3, uint32_t n) |
| 1010 | { |
| 1011 | int64_t t1 = (int64_t)r1; |
| 1012 | int64_t t2 = sextract64(r2, 0, 32); |
| 1013 | int64_t t3 = sextract64(r3, 0, 32); |
| 1014 | int64_t result, mul; |
| 1015 | int64_t ovf; |
| 1016 | |
| 1017 | mul = (t2 * t3) << n; |
| 1018 | result = t1 - mul; |
| 1019 | |
| 1020 | env->PSW_USB_AV = (result ^ result * 2u) >> 32; |
| 1021 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1022 | |
| 1023 | ovf = (result ^ t1) & (t1 ^ mul); |
| 1024 | /* we do the saturation by hand, since we produce an overflow on the host |
| 1025 | if the mul before was (0x80000000 * 0x80000000) << 1). If this is the |
| 1026 | case, we flip the saturated value. */ |
| 1027 | if (mul == 0x8000000000000000LL) { |
| 1028 | if (ovf >= 0) { |
| 1029 | env->PSW_USB_V = (1 << 31); |
| 1030 | env->PSW_USB_SV = (1 << 31); |
| 1031 | /* ext_ret > MAX_INT */ |
| 1032 | if (mul >= 0) { |
| 1033 | result = INT64_MAX; |
| 1034 | /* ext_ret < MIN_INT */ |
| 1035 | } else { |
| 1036 | result = INT64_MIN; |
| 1037 | } |
| 1038 | } else { |
| 1039 | env->PSW_USB_V = 0; |
| 1040 | } |
| 1041 | } else { |
| 1042 | if (ovf < 0) { |
| 1043 | env->PSW_USB_V = (1 << 31); |
| 1044 | env->PSW_USB_SV = (1 << 31); |
| 1045 | /* ext_ret > MAX_INT */ |
| 1046 | if (mul < 0) { |
| 1047 | result = INT64_MAX; |
| 1048 | /* ext_ret < MIN_INT */ |
| 1049 | } else { |
| 1050 | result = INT64_MIN; |
| 1051 | } |
| 1052 | } else { |
| 1053 | env->PSW_USB_V = 0; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | return (uint64_t)result; |
| 1058 | } |
| 1059 | |
| 1060 | uint32_t helper_msubr_q_ssov(CPUTriCoreState *env, uint32_t r1, uint32_t r2, |
| 1061 | uint32_t r3, uint32_t n) |
| 1062 | { |
| 1063 | int64_t t1 = sextract64(r1, 0, 32); |
| 1064 | int64_t t2 = sextract64(r2, 0, 32); |
| 1065 | int64_t t3 = sextract64(r3, 0, 32); |
| 1066 | int64_t mul, ret; |
| 1067 | |
| 1068 | if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) { |
| 1069 | mul = 0x7fffffff; |
| 1070 | } else { |
| 1071 | mul = (t2 * t3) << n; |
| 1072 | } |
| 1073 | |
| 1074 | ret = t1 - mul + 0x8000; |
| 1075 | |
| 1076 | env->PSW_USB_AV = ret ^ ret * 2u; |
| 1077 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1078 | |
| 1079 | if (ret > 0x7fffffffll) { |
| 1080 | env->PSW_USB_V = (1 << 31); |
| 1081 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1082 | ret = INT32_MAX; |
| 1083 | } else if (ret < -0x80000000ll) { |
| 1084 | env->PSW_USB_V = (1 << 31); |
| 1085 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1086 | ret = INT32_MIN; |
| 1087 | } else { |
| 1088 | env->PSW_USB_V = 0; |
| 1089 | } |
| 1090 | return ret & 0xffff0000ll; |
| 1091 | } |
| 1092 | |
| 1093 | uint32_t helper_abs_b(CPUTriCoreState *env, uint32_t arg) |
| 1094 | { |
| 1095 | int32_t b, i; |
| 1096 | int32_t ovf = 0; |
| 1097 | int32_t avf = 0; |
| 1098 | int32_t ret = 0; |
| 1099 | |
| 1100 | for (i = 0; i < 4; i++) { |
| 1101 | b = sextract32(arg, i * 8, 8); |
| 1102 | b = (b >= 0) ? b : (0 - b); |
| 1103 | ovf |= (b > 0x7F) || (b < -0x80); |
| 1104 | avf |= b ^ b * 2u; |
| 1105 | ret |= (b & 0xff) << (i * 8); |
| 1106 | } |
| 1107 | |
| 1108 | env->PSW_USB_V = ovf << 31; |
| 1109 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1110 | env->PSW_USB_AV = avf << 24; |
| 1111 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1112 | |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | uint32_t helper_abs_h(CPUTriCoreState *env, uint32_t arg) |
| 1117 | { |
| 1118 | int32_t h, i; |
| 1119 | int32_t ovf = 0; |
| 1120 | int32_t avf = 0; |
| 1121 | int32_t ret = 0; |
| 1122 | |
| 1123 | for (i = 0; i < 2; i++) { |
| 1124 | h = sextract32(arg, i * 16, 16); |
| 1125 | h = (h >= 0) ? h : (0 - h); |
| 1126 | ovf |= (h > 0x7FFF) || (h < -0x8000); |
| 1127 | avf |= h ^ h * 2u; |
| 1128 | ret |= (h & 0xffff) << (i * 16); |
| 1129 | } |
| 1130 | |
| 1131 | env->PSW_USB_V = ovf << 31; |
| 1132 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1133 | env->PSW_USB_AV = avf << 16; |
| 1134 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1135 | |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | uint32_t helper_absdif_b(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1140 | { |
| 1141 | int32_t b, i; |
| 1142 | int32_t extr_r2; |
| 1143 | int32_t ovf = 0; |
| 1144 | int32_t avf = 0; |
| 1145 | int32_t ret = 0; |
| 1146 | |
| 1147 | for (i = 0; i < 4; i++) { |
| 1148 | extr_r2 = sextract32(r2, i * 8, 8); |
| 1149 | b = sextract32(r1, i * 8, 8); |
| 1150 | b = (b > extr_r2) ? (b - extr_r2) : (extr_r2 - b); |
| 1151 | ovf |= (b > 0x7F) || (b < -0x80); |
| 1152 | avf |= b ^ b * 2u; |
| 1153 | ret |= (b & 0xff) << (i * 8); |
| 1154 | } |
| 1155 | |
| 1156 | env->PSW_USB_V = ovf << 31; |
| 1157 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1158 | env->PSW_USB_AV = avf << 24; |
| 1159 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
| 1163 | uint32_t helper_absdif_h(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1164 | { |
| 1165 | int32_t h, i; |
| 1166 | int32_t extr_r2; |
| 1167 | int32_t ovf = 0; |
| 1168 | int32_t avf = 0; |
| 1169 | int32_t ret = 0; |
| 1170 | |
| 1171 | for (i = 0; i < 2; i++) { |
| 1172 | extr_r2 = sextract32(r2, i * 16, 16); |
| 1173 | h = sextract32(r1, i * 16, 16); |
| 1174 | h = (h > extr_r2) ? (h - extr_r2) : (extr_r2 - h); |
| 1175 | ovf |= (h > 0x7FFF) || (h < -0x8000); |
| 1176 | avf |= h ^ h * 2u; |
| 1177 | ret |= (h & 0xffff) << (i * 16); |
| 1178 | } |
| 1179 | |
| 1180 | env->PSW_USB_V = ovf << 31; |
| 1181 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1182 | env->PSW_USB_AV = avf << 16; |
| 1183 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1184 | |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
| 1188 | uint32_t helper_addr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 1189 | uint32_t r2_h) |
| 1190 | { |
| 1191 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 1192 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 1193 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 1194 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 1195 | int64_t result0, result1; |
| 1196 | uint32_t ovf0, ovf1; |
| 1197 | uint32_t avf0, avf1; |
| 1198 | |
| 1199 | ovf0 = ovf1 = 0; |
| 1200 | |
| 1201 | result0 = r2_low + mul_res0 + 0x8000; |
| 1202 | result1 = r2_high + mul_res1 + 0x8000; |
| 1203 | |
| 1204 | if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) { |
| 1205 | ovf0 = (1 << 31); |
| 1206 | } |
| 1207 | |
| 1208 | if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) { |
| 1209 | ovf1 = (1 << 31); |
| 1210 | } |
| 1211 | |
| 1212 | env->PSW_USB_V = ovf0 | ovf1; |
| 1213 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1214 | |
| 1215 | avf0 = result0 * 2u; |
| 1216 | avf0 = result0 ^ avf0; |
| 1217 | avf1 = result1 * 2u; |
| 1218 | avf1 = result1 ^ avf1; |
| 1219 | |
| 1220 | env->PSW_USB_AV = avf0 | avf1; |
| 1221 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1222 | |
| 1223 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 1224 | } |
| 1225 | |
| 1226 | uint32_t helper_addsur_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 1227 | uint32_t r2_h) |
| 1228 | { |
| 1229 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 1230 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 1231 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 1232 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 1233 | int64_t result0, result1; |
| 1234 | uint32_t ovf0, ovf1; |
| 1235 | uint32_t avf0, avf1; |
| 1236 | |
| 1237 | ovf0 = ovf1 = 0; |
| 1238 | |
| 1239 | result0 = r2_low - mul_res0 + 0x8000; |
| 1240 | result1 = r2_high + mul_res1 + 0x8000; |
| 1241 | |
| 1242 | if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) { |
| 1243 | ovf0 = (1 << 31); |
| 1244 | } |
| 1245 | |
| 1246 | if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) { |
| 1247 | ovf1 = (1 << 31); |
| 1248 | } |
| 1249 | |
| 1250 | env->PSW_USB_V = ovf0 | ovf1; |
| 1251 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1252 | |
| 1253 | avf0 = result0 * 2u; |
| 1254 | avf0 = result0 ^ avf0; |
| 1255 | avf1 = result1 * 2u; |
| 1256 | avf1 = result1 ^ avf1; |
| 1257 | |
| 1258 | env->PSW_USB_AV = avf0 | avf1; |
| 1259 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1260 | |
| 1261 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 1262 | } |
| 1263 | |
| 1264 | uint32_t helper_maddr_q(CPUTriCoreState *env, uint32_t r1, uint32_t r2, |
| 1265 | uint32_t r3, uint32_t n) |
| 1266 | { |
| 1267 | int64_t t1 = sextract64(r1, 0, 32); |
| 1268 | int64_t t2 = sextract64(r2, 0, 32); |
| 1269 | int64_t t3 = sextract64(r3, 0, 32); |
| 1270 | int64_t mul, ret; |
| 1271 | |
| 1272 | if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) { |
| 1273 | mul = 0x7fffffff; |
| 1274 | } else { |
| 1275 | mul = (t2 * t3) << n; |
| 1276 | } |
| 1277 | |
| 1278 | ret = t1 + mul + 0x8000; |
| 1279 | |
| 1280 | if ((ret > 0x7fffffffll) || (ret < -0x80000000ll)) { |
| 1281 | env->PSW_USB_V = (1 << 31); |
| 1282 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1283 | } else { |
| 1284 | env->PSW_USB_V = 0; |
| 1285 | } |
| 1286 | env->PSW_USB_AV = ret ^ ret * 2u; |
| 1287 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1288 | |
| 1289 | return ret & 0xffff0000ll; |
| 1290 | } |
| 1291 | |
| 1292 | uint32_t helper_add_b(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1293 | { |
| 1294 | int32_t b, i; |
| 1295 | int32_t extr_r1, extr_r2; |
| 1296 | int32_t ovf = 0; |
| 1297 | int32_t avf = 0; |
| 1298 | uint32_t ret = 0; |
| 1299 | |
| 1300 | for (i = 0; i < 4; i++) { |
| 1301 | extr_r1 = sextract32(r1, i * 8, 8); |
| 1302 | extr_r2 = sextract32(r2, i * 8, 8); |
| 1303 | |
| 1304 | b = extr_r1 + extr_r2; |
| 1305 | ovf |= ((b > 0x7f) || (b < -0x80)); |
| 1306 | avf |= b ^ b * 2u; |
| 1307 | ret |= ((b & 0xff) << (i*8)); |
| 1308 | } |
| 1309 | |
| 1310 | env->PSW_USB_V = (ovf << 31); |
| 1311 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1312 | env->PSW_USB_AV = avf << 24; |
| 1313 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1314 | |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
| 1318 | uint32_t helper_add_h(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1319 | { |
| 1320 | int32_t h, i; |
| 1321 | int32_t extr_r1, extr_r2; |
| 1322 | int32_t ovf = 0; |
| 1323 | int32_t avf = 0; |
| 1324 | int32_t ret = 0; |
| 1325 | |
| 1326 | for (i = 0; i < 2; i++) { |
| 1327 | extr_r1 = sextract32(r1, i * 16, 16); |
| 1328 | extr_r2 = sextract32(r2, i * 16, 16); |
| 1329 | h = extr_r1 + extr_r2; |
| 1330 | ovf |= ((h > 0x7fff) || (h < -0x8000)); |
| 1331 | avf |= h ^ h * 2u; |
| 1332 | ret |= (h & 0xffff) << (i * 16); |
| 1333 | } |
| 1334 | |
| 1335 | env->PSW_USB_V = (ovf << 31); |
| 1336 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1337 | env->PSW_USB_AV = (avf << 16); |
| 1338 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1339 | |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | uint32_t helper_subr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 1344 | uint32_t r2_h) |
| 1345 | { |
| 1346 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 1347 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 1348 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 1349 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 1350 | int64_t result0, result1; |
| 1351 | uint32_t ovf0, ovf1; |
| 1352 | uint32_t avf0, avf1; |
| 1353 | |
| 1354 | ovf0 = ovf1 = 0; |
| 1355 | |
| 1356 | result0 = r2_low - mul_res0 + 0x8000; |
| 1357 | result1 = r2_high - mul_res1 + 0x8000; |
| 1358 | |
| 1359 | if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) { |
| 1360 | ovf0 = (1 << 31); |
| 1361 | } |
| 1362 | |
| 1363 | if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) { |
| 1364 | ovf1 = (1 << 31); |
| 1365 | } |
| 1366 | |
| 1367 | env->PSW_USB_V = ovf0 | ovf1; |
| 1368 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1369 | |
| 1370 | avf0 = result0 * 2u; |
| 1371 | avf0 = result0 ^ avf0; |
| 1372 | avf1 = result1 * 2u; |
| 1373 | avf1 = result1 ^ avf1; |
| 1374 | |
| 1375 | env->PSW_USB_AV = avf0 | avf1; |
| 1376 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1377 | |
| 1378 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 1379 | } |
| 1380 | |
| 1381 | uint32_t helper_subadr_h(CPUTriCoreState *env, uint64_t r1, uint32_t r2_l, |
| 1382 | uint32_t r2_h) |
| 1383 | { |
| 1384 | int64_t mul_res0 = sextract64(r1, 0, 32); |
| 1385 | int64_t mul_res1 = sextract64(r1, 32, 32); |
| 1386 | int64_t r2_low = sextract64(r2_l, 0, 32); |
| 1387 | int64_t r2_high = sextract64(r2_h, 0, 32); |
| 1388 | int64_t result0, result1; |
| 1389 | uint32_t ovf0, ovf1; |
| 1390 | uint32_t avf0, avf1; |
| 1391 | |
| 1392 | ovf0 = ovf1 = 0; |
| 1393 | |
| 1394 | result0 = r2_low + mul_res0 + 0x8000; |
| 1395 | result1 = r2_high - mul_res1 + 0x8000; |
| 1396 | |
| 1397 | if ((result0 > INT32_MAX) || (result0 < INT32_MIN)) { |
| 1398 | ovf0 = (1 << 31); |
| 1399 | } |
| 1400 | |
| 1401 | if ((result1 > INT32_MAX) || (result1 < INT32_MIN)) { |
| 1402 | ovf1 = (1 << 31); |
| 1403 | } |
| 1404 | |
| 1405 | env->PSW_USB_V = ovf0 | ovf1; |
| 1406 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1407 | |
| 1408 | avf0 = result0 * 2u; |
| 1409 | avf0 = result0 ^ avf0; |
| 1410 | avf1 = result1 * 2u; |
| 1411 | avf1 = result1 ^ avf1; |
| 1412 | |
| 1413 | env->PSW_USB_AV = avf0 | avf1; |
| 1414 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1415 | |
| 1416 | return (result1 & 0xffff0000ULL) | ((result0 >> 16) & 0xffffULL); |
| 1417 | } |
| 1418 | |
| 1419 | uint32_t helper_msubr_q(CPUTriCoreState *env, uint32_t r1, uint32_t r2, |
| 1420 | uint32_t r3, uint32_t n) |
| 1421 | { |
| 1422 | int64_t t1 = sextract64(r1, 0, 32); |
| 1423 | int64_t t2 = sextract64(r2, 0, 32); |
| 1424 | int64_t t3 = sextract64(r3, 0, 32); |
| 1425 | int64_t mul, ret; |
| 1426 | |
| 1427 | if ((t2 == -0x8000ll) && (t3 == -0x8000ll) && (n == 1)) { |
| 1428 | mul = 0x7fffffff; |
| 1429 | } else { |
| 1430 | mul = (t2 * t3) << n; |
| 1431 | } |
| 1432 | |
| 1433 | ret = t1 - mul + 0x8000; |
| 1434 | |
| 1435 | if ((ret > 0x7fffffffll) || (ret < -0x80000000ll)) { |
| 1436 | env->PSW_USB_V = (1 << 31); |
| 1437 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1438 | } else { |
| 1439 | env->PSW_USB_V = 0; |
| 1440 | } |
| 1441 | env->PSW_USB_AV = ret ^ ret * 2u; |
| 1442 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1443 | |
| 1444 | return ret & 0xffff0000ll; |
| 1445 | } |
| 1446 | |
| 1447 | uint32_t helper_sub_b(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1448 | { |
| 1449 | int32_t b, i; |
| 1450 | int32_t extr_r1, extr_r2; |
| 1451 | int32_t ovf = 0; |
| 1452 | int32_t avf = 0; |
| 1453 | uint32_t ret = 0; |
| 1454 | |
| 1455 | for (i = 0; i < 4; i++) { |
| 1456 | extr_r1 = sextract32(r1, i * 8, 8); |
| 1457 | extr_r2 = sextract32(r2, i * 8, 8); |
| 1458 | |
| 1459 | b = extr_r1 - extr_r2; |
| 1460 | ovf |= ((b > 0x7f) || (b < -0x80)); |
| 1461 | avf |= b ^ b * 2u; |
| 1462 | ret |= ((b & 0xff) << (i*8)); |
| 1463 | } |
| 1464 | |
| 1465 | env->PSW_USB_V = (ovf << 31); |
| 1466 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1467 | env->PSW_USB_AV = avf << 24; |
| 1468 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1469 | |
| 1470 | return ret; |
| 1471 | } |
| 1472 | |
| 1473 | uint32_t helper_sub_h(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1474 | { |
| 1475 | int32_t h, i; |
| 1476 | int32_t extr_r1, extr_r2; |
| 1477 | int32_t ovf = 0; |
| 1478 | int32_t avf = 0; |
| 1479 | int32_t ret = 0; |
| 1480 | |
| 1481 | for (i = 0; i < 2; i++) { |
| 1482 | extr_r1 = sextract32(r1, i * 16, 16); |
| 1483 | extr_r2 = sextract32(r2, i * 16, 16); |
| 1484 | h = extr_r1 - extr_r2; |
| 1485 | ovf |= ((h > 0x7fff) || (h < -0x8000)); |
| 1486 | avf |= h ^ h * 2u; |
| 1487 | ret |= (h & 0xffff) << (i * 16); |
| 1488 | } |
| 1489 | |
| 1490 | env->PSW_USB_V = (ovf << 31); |
| 1491 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1492 | env->PSW_USB_AV = avf << 16; |
| 1493 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1494 | |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
| 1498 | uint32_t helper_eq_b(uint32_t r1, uint32_t r2) |
| 1499 | { |
| 1500 | uint32_t ret, msk; |
| 1501 | int32_t i; |
| 1502 | |
| 1503 | ret = 0; |
| 1504 | msk = 0xff; |
| 1505 | for (i = 0; i < 4; i++) { |
| 1506 | if ((r1 & msk) == (r2 & msk)) { |
| 1507 | ret |= msk; |
| 1508 | } |
| 1509 | msk = msk << 8; |
| 1510 | } |
| 1511 | |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | uint32_t helper_eq_h(uint32_t r1, uint32_t r2) |
| 1516 | { |
| 1517 | int32_t ret = 0; |
| 1518 | |
| 1519 | if ((r1 & 0xffff) == (r2 & 0xffff)) { |
| 1520 | ret = 0xffff; |
| 1521 | } |
| 1522 | |
| 1523 | if ((r1 & 0xffff0000) == (r2 & 0xffff0000)) { |
| 1524 | ret |= 0xffff0000; |
| 1525 | } |
| 1526 | |
| 1527 | return ret; |
| 1528 | } |
| 1529 | |
| 1530 | uint32_t helper_eqany_b(uint32_t r1, uint32_t r2) |
| 1531 | { |
| 1532 | int32_t i; |
| 1533 | uint32_t ret = 0; |
| 1534 | |
| 1535 | for (i = 0; i < 4; i++) { |
| 1536 | ret |= (sextract32(r1, i * 8, 8) == sextract32(r2, i * 8, 8)); |
| 1537 | } |
| 1538 | |
| 1539 | return ret; |
| 1540 | } |
| 1541 | |
| 1542 | uint32_t helper_eqany_h(uint32_t r1, uint32_t r2) |
| 1543 | { |
| 1544 | uint32_t ret; |
| 1545 | |
| 1546 | ret = (sextract32(r1, 0, 16) == sextract32(r2, 0, 16)); |
| 1547 | ret |= (sextract32(r1, 16, 16) == sextract32(r2, 16, 16)); |
| 1548 | |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
| 1552 | uint32_t helper_lt_b(uint32_t r1, uint32_t r2) |
| 1553 | { |
| 1554 | int32_t i; |
| 1555 | uint32_t ret = 0; |
| 1556 | |
| 1557 | for (i = 0; i < 4; i++) { |
| 1558 | if (sextract32(r1, i * 8, 8) < sextract32(r2, i * 8, 8)) { |
| 1559 | ret |= (0xff << (i * 8)); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | return ret; |
| 1564 | } |
| 1565 | |
| 1566 | uint32_t helper_lt_bu(uint32_t r1, uint32_t r2) |
| 1567 | { |
| 1568 | int32_t i; |
| 1569 | uint32_t ret = 0; |
| 1570 | |
| 1571 | for (i = 0; i < 4; i++) { |
| 1572 | if (extract32(r1, i * 8, 8) < extract32(r2, i * 8, 8)) { |
| 1573 | ret |= (0xff << (i * 8)); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
| 1580 | uint32_t helper_lt_h(uint32_t r1, uint32_t r2) |
| 1581 | { |
| 1582 | uint32_t ret = 0; |
| 1583 | |
| 1584 | if (sextract32(r1, 0, 16) < sextract32(r2, 0, 16)) { |
| 1585 | ret |= 0xffff; |
| 1586 | } |
| 1587 | |
| 1588 | if (sextract32(r1, 16, 16) < sextract32(r2, 16, 16)) { |
| 1589 | ret |= 0xffff0000; |
| 1590 | } |
| 1591 | |
| 1592 | return ret; |
| 1593 | } |
| 1594 | |
| 1595 | uint32_t helper_lt_hu(uint32_t r1, uint32_t r2) |
| 1596 | { |
| 1597 | uint32_t ret = 0; |
| 1598 | |
| 1599 | if (extract32(r1, 0, 16) < extract32(r2, 0, 16)) { |
| 1600 | ret |= 0xffff; |
| 1601 | } |
| 1602 | |
| 1603 | if (extract32(r1, 16, 16) < extract32(r2, 16, 16)) { |
| 1604 | ret |= 0xffff0000; |
| 1605 | } |
| 1606 | |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
| 1610 | #define EXTREMA_H_B(name, op) \ |
| 1611 | uint32_t helper_##name ##_b(uint32_t r1, uint32_t r2) \ |
| 1612 | { \ |
| 1613 | int32_t i, extr_r1, extr_r2; \ |
| 1614 | uint32_t ret = 0; \ |
| 1615 | \ |
| 1616 | for (i = 0; i < 4; i++) { \ |
| 1617 | extr_r1 = sextract32(r1, i * 8, 8); \ |
| 1618 | extr_r2 = sextract32(r2, i * 8, 8); \ |
| 1619 | extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \ |
| 1620 | ret |= (extr_r1 & 0xff) << (i * 8); \ |
| 1621 | } \ |
| 1622 | return ret; \ |
| 1623 | } \ |
| 1624 | \ |
| 1625 | uint32_t helper_##name ##_bu(uint32_t r1, uint32_t r2) \ |
| 1626 | { \ |
| 1627 | int32_t i; \ |
| 1628 | uint32_t extr_r1, extr_r2; \ |
| 1629 | uint32_t ret = 0; \ |
| 1630 | \ |
| 1631 | for (i = 0; i < 4; i++) { \ |
| 1632 | extr_r1 = extract32(r1, i * 8, 8); \ |
| 1633 | extr_r2 = extract32(r2, i * 8, 8); \ |
| 1634 | extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \ |
| 1635 | ret |= (extr_r1 & 0xff) << (i * 8); \ |
| 1636 | } \ |
| 1637 | return ret; \ |
| 1638 | } \ |
| 1639 | \ |
| 1640 | uint32_t helper_##name ##_h(uint32_t r1, uint32_t r2) \ |
| 1641 | { \ |
| 1642 | int32_t extr_r1, extr_r2; \ |
| 1643 | uint32_t ret = 0; \ |
| 1644 | \ |
| 1645 | extr_r1 = sextract32(r1, 0, 16); \ |
| 1646 | extr_r2 = sextract32(r2, 0, 16); \ |
| 1647 | ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \ |
| 1648 | ret = ret & 0xffff; \ |
| 1649 | \ |
| 1650 | extr_r1 = sextract32(r1, 16, 16); \ |
| 1651 | extr_r2 = sextract32(r2, 16, 16); \ |
| 1652 | extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \ |
| 1653 | ret |= extr_r1 << 16; \ |
| 1654 | \ |
| 1655 | return ret; \ |
| 1656 | } \ |
| 1657 | \ |
| 1658 | uint32_t helper_##name ##_hu(uint32_t r1, uint32_t r2) \ |
| 1659 | { \ |
| 1660 | uint32_t extr_r1, extr_r2; \ |
| 1661 | uint32_t ret = 0; \ |
| 1662 | \ |
| 1663 | extr_r1 = extract32(r1, 0, 16); \ |
| 1664 | extr_r2 = extract32(r2, 0, 16); \ |
| 1665 | ret = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \ |
| 1666 | ret = ret & 0xffff; \ |
| 1667 | \ |
| 1668 | extr_r1 = extract32(r1, 16, 16); \ |
| 1669 | extr_r2 = extract32(r2, 16, 16); \ |
| 1670 | extr_r1 = (extr_r1 op extr_r2) ? extr_r1 : extr_r2; \ |
| 1671 | ret |= extr_r1 << (16); \ |
| 1672 | \ |
| 1673 | return ret; \ |
| 1674 | } \ |
| 1675 | \ |
| 1676 | uint64_t helper_ix##name(uint64_t r1, uint32_t r2) \ |
| 1677 | { \ |
| 1678 | int64_t r2l, r2h, r1hl; \ |
| 1679 | uint64_t ret = 0; \ |
| 1680 | \ |
| 1681 | ret = ((r1 + 2) & 0xffff); \ |
| 1682 | r2l = sextract64(r2, 0, 16); \ |
| 1683 | r2h = sextract64(r2, 16, 16); \ |
| 1684 | r1hl = sextract64(r1, 32, 16); \ |
| 1685 | \ |
| 1686 | if ((r2l op ## = r2h) && (r2l op r1hl)) { \ |
| 1687 | ret |= (r2l & 0xffff) << 32; \ |
| 1688 | ret |= extract64(r1, 0, 16) << 16; \ |
| 1689 | } else if ((r2h op r2l) && (r2h op r1hl)) { \ |
| 1690 | ret |= extract64(r2, 16, 16) << 32; \ |
| 1691 | ret |= extract64(r1 + 1, 0, 16) << 16; \ |
| 1692 | } else { \ |
| 1693 | ret |= r1 & 0xffffffff0000ull; \ |
| 1694 | } \ |
| 1695 | return ret; \ |
| 1696 | } \ |
| 1697 | \ |
| 1698 | uint64_t helper_ix##name ##_u(uint64_t r1, uint32_t r2) \ |
| 1699 | { \ |
| 1700 | int64_t r2l, r2h, r1hl; \ |
| 1701 | uint64_t ret = 0; \ |
| 1702 | \ |
| 1703 | ret = ((r1 + 2) & 0xffff); \ |
| 1704 | r2l = extract64(r2, 0, 16); \ |
| 1705 | r2h = extract64(r2, 16, 16); \ |
| 1706 | r1hl = extract64(r1, 32, 16); \ |
| 1707 | \ |
| 1708 | if ((r2l op ## = r2h) && (r2l op r1hl)) { \ |
| 1709 | ret |= (r2l & 0xffff) << 32; \ |
| 1710 | ret |= extract64(r1, 0, 16) << 16; \ |
| 1711 | } else if ((r2h op r2l) && (r2h op r1hl)) { \ |
| 1712 | ret |= extract64(r2, 16, 16) << 32; \ |
| 1713 | ret |= extract64(r1 + 1, 0, 16) << 16; \ |
| 1714 | } else { \ |
| 1715 | ret |= r1 & 0xffffffff0000ull; \ |
| 1716 | } \ |
| 1717 | return ret; \ |
| 1718 | } |
| 1719 | |
| 1720 | EXTREMA_H_B(max, >) |
| 1721 | EXTREMA_H_B(min, <) |
| 1722 | |
| 1723 | #undef EXTREMA_H_B |
| 1724 | |
| 1725 | uint32_t helper_clo_h(uint32_t r1) |
| 1726 | { |
| 1727 | uint32_t ret_hw0 = extract32(r1, 0, 16); |
| 1728 | uint32_t ret_hw1 = extract32(r1, 16, 16); |
| 1729 | |
| 1730 | ret_hw0 = clo32(ret_hw0 << 16); |
| 1731 | ret_hw1 = clo32(ret_hw1 << 16); |
| 1732 | |
| 1733 | if (ret_hw0 > 16) { |
| 1734 | ret_hw0 = 16; |
| 1735 | } |
| 1736 | if (ret_hw1 > 16) { |
| 1737 | ret_hw1 = 16; |
| 1738 | } |
| 1739 | |
| 1740 | return ret_hw0 | (ret_hw1 << 16); |
| 1741 | } |
| 1742 | |
| 1743 | uint32_t helper_clz_h(uint32_t r1) |
| 1744 | { |
| 1745 | uint32_t ret_hw0 = extract32(r1, 0, 16); |
| 1746 | uint32_t ret_hw1 = extract32(r1, 16, 16); |
| 1747 | |
| 1748 | ret_hw0 = clz32(ret_hw0 << 16); |
| 1749 | ret_hw1 = clz32(ret_hw1 << 16); |
| 1750 | |
| 1751 | if (ret_hw0 > 16) { |
| 1752 | ret_hw0 = 16; |
| 1753 | } |
| 1754 | if (ret_hw1 > 16) { |
| 1755 | ret_hw1 = 16; |
| 1756 | } |
| 1757 | |
| 1758 | return ret_hw0 | (ret_hw1 << 16); |
| 1759 | } |
| 1760 | |
| 1761 | uint32_t helper_cls_h(uint32_t r1) |
| 1762 | { |
| 1763 | uint32_t ret_hw0 = extract32(r1, 0, 16); |
| 1764 | uint32_t ret_hw1 = extract32(r1, 16, 16); |
| 1765 | |
| 1766 | ret_hw0 = clrsb32(ret_hw0 << 16); |
| 1767 | ret_hw1 = clrsb32(ret_hw1 << 16); |
| 1768 | |
| 1769 | if (ret_hw0 > 15) { |
| 1770 | ret_hw0 = 15; |
| 1771 | } |
| 1772 | if (ret_hw1 > 15) { |
| 1773 | ret_hw1 = 15; |
| 1774 | } |
| 1775 | |
| 1776 | return ret_hw0 | (ret_hw1 << 16); |
| 1777 | } |
| 1778 | |
| 1779 | uint32_t helper_sh(uint32_t r1, uint32_t r2) |
| 1780 | { |
| 1781 | int32_t shift_count = sextract32(r2, 0, 6); |
| 1782 | |
| 1783 | if (shift_count == -32) { |
| 1784 | return 0; |
| 1785 | } else if (shift_count < 0) { |
| 1786 | return r1 >> -shift_count; |
| 1787 | } else { |
| 1788 | return r1 << shift_count; |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | uint32_t helper_sh_h(uint32_t r1, uint32_t r2) |
| 1793 | { |
| 1794 | int32_t ret_hw0, ret_hw1; |
| 1795 | int32_t shift_count; |
| 1796 | |
| 1797 | shift_count = sextract32(r2, 0, 5); |
| 1798 | |
| 1799 | if (shift_count == -16) { |
| 1800 | return 0; |
| 1801 | } else if (shift_count < 0) { |
| 1802 | ret_hw0 = extract32(r1, 0, 16) >> -shift_count; |
| 1803 | ret_hw1 = extract32(r1, 16, 16) >> -shift_count; |
| 1804 | return (ret_hw0 & 0xffff) | (ret_hw1 << 16); |
| 1805 | } else { |
| 1806 | ret_hw0 = extract32(r1, 0, 16) << shift_count; |
| 1807 | ret_hw1 = extract32(r1, 16, 16) << shift_count; |
| 1808 | return (ret_hw0 & 0xffff) | (ret_hw1 << 16); |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | uint32_t helper_sha(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 1813 | { |
| 1814 | int32_t shift_count; |
| 1815 | int64_t result, t1; |
| 1816 | uint32_t ret; |
| 1817 | |
| 1818 | shift_count = sextract32(r2, 0, 6); |
| 1819 | t1 = sextract32(r1, 0, 32); |
| 1820 | |
| 1821 | if (shift_count == 0) { |
| 1822 | env->PSW_USB_C = env->PSW_USB_V = 0; |
| 1823 | ret = r1; |
| 1824 | } else if (shift_count == -32) { |
| 1825 | env->PSW_USB_C = r1; |
| 1826 | env->PSW_USB_V = 0; |
| 1827 | ret = t1 >> 31; |
| 1828 | } else if (shift_count > 0) { |
| 1829 | result = t1 << shift_count; |
| 1830 | /* calc carry */ |
| 1831 | env->PSW_USB_C = ((result & 0xffffffff00000000ULL) != 0); |
| 1832 | /* calc v */ |
| 1833 | env->PSW_USB_V = (((result > 0x7fffffffLL) || |
| 1834 | (result < -0x80000000LL)) << 31); |
| 1835 | /* calc sv */ |
| 1836 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 1837 | ret = (uint32_t)result; |
| 1838 | } else { |
| 1839 | env->PSW_USB_V = 0; |
| 1840 | env->PSW_USB_C = (r1 & ((1 << -shift_count) - 1)); |
| 1841 | ret = t1 >> -shift_count; |
| 1842 | } |
| 1843 | |
| 1844 | env->PSW_USB_AV = ret ^ ret * 2u; |
| 1845 | env->PSW_USB_SAV |= env->PSW_USB_AV; |
| 1846 | |
| 1847 | return ret; |
| 1848 | } |
| 1849 | |
| 1850 | uint32_t helper_sha_h(uint32_t r1, uint32_t r2) |
| 1851 | { |
| 1852 | int32_t shift_count; |
| 1853 | int32_t ret_hw0, ret_hw1; |
| 1854 | |
| 1855 | shift_count = sextract32(r2, 0, 5); |
| 1856 | |
| 1857 | if (shift_count == 0) { |
| 1858 | return r1; |
| 1859 | } else if (shift_count < 0) { |
| 1860 | ret_hw0 = sextract32(r1, 0, 16) >> -shift_count; |
| 1861 | ret_hw1 = sextract32(r1, 16, 16) >> -shift_count; |
| 1862 | return (ret_hw0 & 0xffff) | (ret_hw1 << 16); |
| 1863 | } else { |
| 1864 | ret_hw0 = sextract32(r1, 0, 16) << shift_count; |
| 1865 | ret_hw1 = sextract32(r1, 16, 16) << shift_count; |
| 1866 | return (ret_hw0 & 0xffff) | (ret_hw1 << 16); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | uint32_t helper_bmerge(uint32_t r1, uint32_t r2) |
| 1871 | { |
| 1872 | uint32_t i, ret; |
| 1873 | |
| 1874 | ret = 0; |
| 1875 | for (i = 0; i < 16; i++) { |
| 1876 | ret |= (r1 & 1) << (2 * i + 1); |
| 1877 | ret |= (r2 & 1) << (2 * i); |
| 1878 | r1 = r1 >> 1; |
| 1879 | r2 = r2 >> 1; |
| 1880 | } |
| 1881 | return ret; |
| 1882 | } |
| 1883 | |
| 1884 | uint64_t helper_bsplit(uint32_t r1) |
| 1885 | { |
| 1886 | int32_t i; |
| 1887 | uint64_t ret; |
| 1888 | |
| 1889 | ret = 0; |
| 1890 | for (i = 0; i < 32; i = i + 2) { |
| 1891 | /* even */ |
| 1892 | ret |= (r1 & 1) << (i/2); |
| 1893 | r1 = r1 >> 1; |
| 1894 | /* odd */ |
| 1895 | ret |= (uint64_t)(r1 & 1) << (i/2 + 32); |
| 1896 | r1 = r1 >> 1; |
| 1897 | } |
| 1898 | return ret; |
| 1899 | } |
| 1900 | |
| 1901 | uint32_t helper_parity(uint32_t r1) |
| 1902 | { |
| 1903 | uint32_t ret; |
| 1904 | uint32_t nOnes, i; |
| 1905 | |
| 1906 | ret = 0; |
| 1907 | nOnes = 0; |
| 1908 | for (i = 0; i < 8; i++) { |
| 1909 | ret ^= (r1 & 1); |
| 1910 | r1 = r1 >> 1; |
| 1911 | } |
| 1912 | /* second byte */ |
| 1913 | nOnes = 0; |
| 1914 | for (i = 0; i < 8; i++) { |
| 1915 | nOnes ^= (r1 & 1); |
| 1916 | r1 = r1 >> 1; |
| 1917 | } |
| 1918 | ret |= nOnes << 8; |
| 1919 | /* third byte */ |
| 1920 | nOnes = 0; |
| 1921 | for (i = 0; i < 8; i++) { |
| 1922 | nOnes ^= (r1 & 1); |
| 1923 | r1 = r1 >> 1; |
| 1924 | } |
| 1925 | ret |= nOnes << 16; |
| 1926 | /* fourth byte */ |
| 1927 | nOnes = 0; |
| 1928 | for (i = 0; i < 8; i++) { |
| 1929 | nOnes ^= (r1 & 1); |
| 1930 | r1 = r1 >> 1; |
| 1931 | } |
| 1932 | ret |= nOnes << 24; |
| 1933 | |
| 1934 | return ret; |
| 1935 | } |
| 1936 | |
| 1937 | uint32_t helper_pack(uint32_t carry, uint32_t r1_low, uint32_t r1_high, |
| 1938 | uint32_t r2) |
| 1939 | { |
| 1940 | uint32_t ret; |
| 1941 | int32_t fp_exp, fp_frac, temp_exp, fp_exp_frac; |
| 1942 | int32_t int_exp = r1_high; |
| 1943 | int32_t int_mant = r1_low; |
| 1944 | uint32_t flag_rnd = (int_mant & (1 << 7)) && ( |
| 1945 | (int_mant & (1 << 8)) || |
| 1946 | (int_mant & 0x7f) || |
| 1947 | (carry != 0)); |
| 1948 | if (((int_mant & (1<<31)) == 0) && (int_exp == 255)) { |
| 1949 | fp_exp = 255; |
| 1950 | fp_frac = extract32(int_mant, 8, 23); |
| 1951 | } else if ((int_mant & (1<<31)) && (int_exp >= 127)) { |
| 1952 | fp_exp = 255; |
| 1953 | fp_frac = 0; |
| 1954 | } else if ((int_mant & (1<<31)) && (int_exp <= -128)) { |
| 1955 | fp_exp = 0; |
| 1956 | fp_frac = 0; |
| 1957 | } else if (int_mant == 0) { |
| 1958 | fp_exp = 0; |
| 1959 | fp_frac = 0; |
| 1960 | } else { |
| 1961 | if (((int_mant & (1 << 31)) == 0)) { |
| 1962 | temp_exp = 0; |
| 1963 | } else { |
| 1964 | temp_exp = int_exp + 128; |
| 1965 | } |
| 1966 | fp_exp_frac = (((temp_exp & 0xff) << 23) | |
| 1967 | extract32(int_mant, 8, 23)) |
| 1968 | + flag_rnd; |
| 1969 | fp_exp = extract32(fp_exp_frac, 23, 8); |
| 1970 | fp_frac = extract32(fp_exp_frac, 0, 23); |
| 1971 | } |
| 1972 | ret = r2 & (1 << 31); |
| 1973 | ret = ret + (fp_exp << 23); |
| 1974 | ret = ret + (fp_frac & 0x7fffff); |
| 1975 | |
| 1976 | return ret; |
| 1977 | } |
| 1978 | |
| 1979 | uint64_t helper_unpack(uint32_t arg1) |
| 1980 | { |
| 1981 | int32_t fp_exp = extract32(arg1, 23, 8); |
| 1982 | int32_t fp_frac = extract32(arg1, 0, 23); |
| 1983 | uint64_t ret; |
| 1984 | int32_t int_exp, int_mant; |
| 1985 | |
| 1986 | if (fp_exp == 255) { |
| 1987 | int_exp = 255; |
| 1988 | int_mant = (fp_frac << 7); |
| 1989 | } else if ((fp_exp == 0) && (fp_frac == 0)) { |
| 1990 | int_exp = -127; |
| 1991 | int_mant = 0; |
| 1992 | } else if ((fp_exp == 0) && (fp_frac != 0)) { |
| 1993 | int_exp = -126; |
| 1994 | int_mant = (fp_frac << 7); |
| 1995 | } else { |
| 1996 | int_exp = fp_exp - 127; |
| 1997 | int_mant = (fp_frac << 7); |
| 1998 | int_mant |= (1 << 30); |
| 1999 | } |
| 2000 | ret = int_exp; |
| 2001 | ret = ret << 32; |
| 2002 | ret |= int_mant; |
| 2003 | |
| 2004 | return ret; |
| 2005 | } |
| 2006 | |
| 2007 | uint64_t helper_dvinit_b_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 2008 | { |
| 2009 | uint64_t ret; |
| 2010 | int32_t abs_sig_dividend, abs_divisor; |
| 2011 | |
| 2012 | ret = sextract32(r1, 0, 32); |
| 2013 | ret = ret << 24; |
| 2014 | if (!((r1 & 0x80000000) == (r2 & 0x80000000))) { |
| 2015 | ret |= 0xffffff; |
| 2016 | } |
| 2017 | |
| 2018 | abs_sig_dividend = abs((int32_t)r1) >> 8; |
| 2019 | abs_divisor = abs((int32_t)r2); |
| 2020 | /* calc overflow |
| 2021 | ofv if (a/b >= 255) <=> (a/255 >= b) */ |
| 2022 | env->PSW_USB_V = (abs_sig_dividend >= abs_divisor) << 31; |
| 2023 | env->PSW_USB_V = env->PSW_USB_V << 31; |
| 2024 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 2025 | env->PSW_USB_AV = 0; |
| 2026 | |
| 2027 | return ret; |
| 2028 | } |
| 2029 | |
| 2030 | uint64_t helper_dvinit_b_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 2031 | { |
| 2032 | uint64_t ret = sextract32(r1, 0, 32); |
| 2033 | |
| 2034 | ret = ret << 24; |
| 2035 | if (!((r1 & 0x80000000) == (r2 & 0x80000000))) { |
| 2036 | ret |= 0xffffff; |
| 2037 | } |
| 2038 | /* calc overflow */ |
| 2039 | env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffffff80))); |
| 2040 | env->PSW_USB_V = env->PSW_USB_V << 31; |
| 2041 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 2042 | env->PSW_USB_AV = 0; |
| 2043 | |
| 2044 | return ret; |
| 2045 | } |
| 2046 | |
| 2047 | uint64_t helper_dvinit_h_13(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 2048 | { |
| 2049 | uint64_t ret; |
| 2050 | int32_t abs_sig_dividend, abs_divisor; |
| 2051 | |
| 2052 | ret = sextract32(r1, 0, 32); |
| 2053 | ret = ret << 16; |
| 2054 | if (!((r1 & 0x80000000) == (r2 & 0x80000000))) { |
| 2055 | ret |= 0xffff; |
| 2056 | } |
| 2057 | |
| 2058 | abs_sig_dividend = abs((int32_t)r1) >> 16; |
| 2059 | abs_divisor = abs((int32_t)r2); |
| 2060 | /* calc overflow |
| 2061 | ofv if (a/b >= 0xffff) <=> (a/0xffff >= b) */ |
| 2062 | env->PSW_USB_V = (abs_sig_dividend >= abs_divisor) << 31; |
| 2063 | env->PSW_USB_V = env->PSW_USB_V << 31; |
| 2064 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 2065 | env->PSW_USB_AV = 0; |
| 2066 | |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
| 2070 | uint64_t helper_dvinit_h_131(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 2071 | { |
| 2072 | uint64_t ret = sextract32(r1, 0, 32); |
| 2073 | |
| 2074 | ret = ret << 16; |
| 2075 | if (!((r1 & 0x80000000) == (r2 & 0x80000000))) { |
| 2076 | ret |= 0xffff; |
| 2077 | } |
| 2078 | /* calc overflow */ |
| 2079 | env->PSW_USB_V = ((r2 == 0) || ((r2 == 0xffffffff) && (r1 == 0xffff8000))); |
| 2080 | env->PSW_USB_V = env->PSW_USB_V << 31; |
| 2081 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 2082 | env->PSW_USB_AV = 0; |
| 2083 | |
| 2084 | return ret; |
| 2085 | } |
| 2086 | |
| 2087 | uint64_t helper_dvadj(uint64_t r1, uint32_t r2) |
| 2088 | { |
| 2089 | int32_t x_sign = (r1 >> 63); |
| 2090 | int32_t q_sign = x_sign ^ (r2 >> 31); |
| 2091 | int32_t eq_pos = x_sign & ((r1 >> 32) == r2); |
| 2092 | int32_t eq_neg = x_sign & ((r1 >> 32) == -r2); |
| 2093 | uint32_t quotient; |
| 2094 | uint64_t remainder; |
| 2095 | |
| 2096 | if ((q_sign & ~eq_neg) | eq_pos) { |
| 2097 | quotient = (r1 + 1) & 0xffffffff; |
| 2098 | } else { |
| 2099 | quotient = r1 & 0xffffffff; |
| 2100 | } |
| 2101 | |
| 2102 | if (eq_pos | eq_neg) { |
| 2103 | remainder = 0; |
| 2104 | } else { |
| 2105 | remainder = (r1 & 0xffffffff00000000ull); |
| 2106 | } |
| 2107 | return remainder | quotient; |
| 2108 | } |
| 2109 | |
| 2110 | uint64_t helper_dvstep(uint64_t r1, uint32_t r2) |
| 2111 | { |
| 2112 | int32_t dividend_sign = extract64(r1, 63, 1); |
| 2113 | int32_t divisor_sign = extract32(r2, 31, 1); |
| 2114 | int32_t quotient_sign = (dividend_sign != divisor_sign); |
| 2115 | int32_t addend, dividend_quotient, remainder; |
| 2116 | int32_t i, temp; |
| 2117 | |
| 2118 | if (quotient_sign) { |
| 2119 | addend = r2; |
| 2120 | } else { |
| 2121 | addend = -r2; |
| 2122 | } |
| 2123 | dividend_quotient = (int32_t)r1; |
| 2124 | remainder = (int32_t)(r1 >> 32); |
| 2125 | |
| 2126 | for (i = 0; i < 8; i++) { |
| 2127 | remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1); |
| 2128 | dividend_quotient <<= 1; |
| 2129 | temp = remainder + addend; |
| 2130 | if ((temp < 0) == dividend_sign) { |
| 2131 | remainder = temp; |
| 2132 | } |
| 2133 | if (((temp < 0) == dividend_sign)) { |
| 2134 | dividend_quotient = dividend_quotient | !quotient_sign; |
| 2135 | } else { |
| 2136 | dividend_quotient = dividend_quotient | quotient_sign; |
| 2137 | } |
| 2138 | } |
| 2139 | return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient; |
| 2140 | } |
| 2141 | |
| 2142 | uint64_t helper_dvstep_u(uint64_t r1, uint32_t r2) |
| 2143 | { |
| 2144 | int32_t dividend_quotient = extract64(r1, 0, 32); |
| 2145 | int64_t remainder = extract64(r1, 32, 32); |
| 2146 | int32_t i; |
| 2147 | int64_t temp; |
| 2148 | for (i = 0; i < 8; i++) { |
| 2149 | remainder = (remainder << 1) | extract32(dividend_quotient, 31, 1); |
| 2150 | dividend_quotient <<= 1; |
| 2151 | temp = (remainder & 0xffffffff) - r2; |
| 2152 | if (temp >= 0) { |
| 2153 | remainder = temp; |
| 2154 | } |
| 2155 | dividend_quotient = dividend_quotient | !(temp < 0); |
| 2156 | } |
| 2157 | return ((uint64_t)remainder << 32) | (uint32_t)dividend_quotient; |
| 2158 | } |
| 2159 | |
| 2160 | uint64_t helper_divide(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 2161 | { |
| 2162 | int32_t quotient, remainder; |
| 2163 | int32_t dividend = (int32_t)r1; |
| 2164 | int32_t divisor = (int32_t)r2; |
| 2165 | |
| 2166 | if (divisor == 0) { |
| 2167 | if (dividend >= 0) { |
| 2168 | quotient = 0x7fffffff; |
| 2169 | remainder = 0; |
| 2170 | } else { |
| 2171 | quotient = 0x80000000; |
| 2172 | remainder = 0; |
| 2173 | } |
| 2174 | env->PSW_USB_V = (1 << 31); |
| 2175 | } else if ((divisor == 0xffffffff) && (dividend == 0x80000000)) { |
| 2176 | quotient = 0x7fffffff; |
| 2177 | remainder = 0; |
| 2178 | env->PSW_USB_V = (1 << 31); |
| 2179 | } else { |
| 2180 | remainder = dividend % divisor; |
| 2181 | quotient = (dividend - remainder)/divisor; |
| 2182 | env->PSW_USB_V = 0; |
| 2183 | } |
| 2184 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 2185 | env->PSW_USB_AV = 0; |
| 2186 | return ((uint64_t)remainder << 32) | (uint32_t)quotient; |
| 2187 | } |
| 2188 | |
| 2189 | uint64_t helper_divide_u(CPUTriCoreState *env, uint32_t r1, uint32_t r2) |
| 2190 | { |
| 2191 | uint32_t quotient, remainder; |
| 2192 | uint32_t dividend = r1; |
| 2193 | uint32_t divisor = r2; |
| 2194 | |
| 2195 | if (divisor == 0) { |
| 2196 | quotient = 0xffffffff; |
| 2197 | remainder = 0; |
| 2198 | env->PSW_USB_V = (1 << 31); |
| 2199 | } else { |
| 2200 | remainder = dividend % divisor; |
| 2201 | quotient = (dividend - remainder)/divisor; |
| 2202 | env->PSW_USB_V = 0; |
| 2203 | } |
| 2204 | env->PSW_USB_SV |= env->PSW_USB_V; |
| 2205 | env->PSW_USB_AV = 0; |
| 2206 | return ((uint64_t)remainder << 32) | quotient; |
| 2207 | } |
| 2208 | |
| 2209 | uint64_t helper_mul_h(uint32_t arg00, uint32_t arg01, |
| 2210 | uint32_t arg10, uint32_t arg11, uint32_t n) |
| 2211 | { |
| 2212 | uint32_t result0, result1; |
| 2213 | |
| 2214 | int32_t sc1 = ((arg00 & 0xffff) == 0x8000) && |
| 2215 | ((arg10 & 0xffff) == 0x8000) && (n == 1); |
| 2216 | int32_t sc0 = ((arg01 & 0xffff) == 0x8000) && |
| 2217 | ((arg11 & 0xffff) == 0x8000) && (n == 1); |
| 2218 | if (sc1) { |
| 2219 | result1 = 0x7fffffff; |
| 2220 | } else { |
| 2221 | result1 = (((uint32_t)(arg00 * arg10)) << n); |
| 2222 | } |
| 2223 | if (sc0) { |
| 2224 | result0 = 0x7fffffff; |
| 2225 | } else { |
| 2226 | result0 = (((uint32_t)(arg01 * arg11)) << n); |
| 2227 | } |
| 2228 | return (((uint64_t)result1 << 32)) | result0; |
| 2229 | } |
| 2230 | |
| 2231 | uint64_t helper_mulm_h(uint32_t arg00, uint32_t arg01, |
| 2232 | uint32_t arg10, uint32_t arg11, uint32_t n) |
| 2233 | { |
| 2234 | uint64_t ret; |
| 2235 | int64_t result0, result1; |
| 2236 | |
| 2237 | int32_t sc1 = ((arg00 & 0xffff) == 0x8000) && |
| 2238 | ((arg10 & 0xffff) == 0x8000) && (n == 1); |
| 2239 | int32_t sc0 = ((arg01 & 0xffff) == 0x8000) && |
| 2240 | ((arg11 & 0xffff) == 0x8000) && (n == 1); |
| 2241 | |
| 2242 | if (sc1) { |
| 2243 | result1 = 0x7fffffff; |
| 2244 | } else { |
| 2245 | result1 = (((int32_t)arg00 * (int32_t)arg10) << n); |
| 2246 | } |
| 2247 | if (sc0) { |
| 2248 | result0 = 0x7fffffff; |
| 2249 | } else { |
| 2250 | result0 = (((int32_t)arg01 * (int32_t)arg11) << n); |
| 2251 | } |
| 2252 | ret = (result1 + result0); |
| 2253 | ret = ret << 16; |
| 2254 | return ret; |
| 2255 | } |
| 2256 | uint32_t helper_mulr_h(uint32_t arg00, uint32_t arg01, |
| 2257 | uint32_t arg10, uint32_t arg11, uint32_t n) |
| 2258 | { |
| 2259 | uint32_t result0, result1; |
| 2260 | |
| 2261 | int32_t sc1 = ((arg00 & 0xffff) == 0x8000) && |
| 2262 | ((arg10 & 0xffff) == 0x8000) && (n == 1); |
| 2263 | int32_t sc0 = ((arg01 & 0xffff) == 0x8000) && |
| 2264 | ((arg11 & 0xffff) == 0x8000) && (n == 1); |
| 2265 | |
| 2266 | if (sc1) { |
| 2267 | result1 = 0x7fffffff; |
| 2268 | } else { |
| 2269 | result1 = ((arg00 * arg10) << n) + 0x8000; |
| 2270 | } |
| 2271 | if (sc0) { |
| 2272 | result0 = 0x7fffffff; |
| 2273 | } else { |
| 2274 | result0 = ((arg01 * arg11) << n) + 0x8000; |
| 2275 | } |
| 2276 | return (result1 & 0xffff0000) | (result0 >> 16); |
| 2277 | } |
| 2278 | |
| 2279 | uint32_t helper_crc32b(uint32_t arg0, uint32_t arg1) |
| 2280 | { |
| 2281 | uint8_t buf[1] = { arg0 & 0xff }; |
| 2282 | |
| 2283 | return crc32(arg1, buf, 1); |
| 2284 | } |
| 2285 | |
| 2286 | |
| 2287 | uint32_t helper_crc32_be(uint32_t arg0, uint32_t arg1) |
| 2288 | { |
| 2289 | uint8_t buf[4]; |
| 2290 | stl_be_p(buf, arg0); |
| 2291 | |
| 2292 | return crc32(arg1, buf, 4); |
| 2293 | } |
| 2294 | |
| 2295 | uint32_t helper_crc32_le(uint32_t arg0, uint32_t arg1) |
| 2296 | { |
| 2297 | uint8_t buf[4]; |
| 2298 | stl_le_p(buf, arg0); |
| 2299 | |
| 2300 | return crc32(arg1, buf, 4); |
| 2301 | } |
| 2302 | |
| 2303 | static uint32_t crc_div(uint32_t crc_in, uint32_t data, uint32_t gen, |
| 2304 | uint32_t n, uint32_t m) |
| 2305 | { |
| 2306 | uint32_t i; |
| 2307 | |
| 2308 | data = data << n; |
| 2309 | for (i = 0; i < m; i++) { |
| 2310 | if (crc_in & (1u << (n - 1))) { |
| 2311 | crc_in <<= 1; |
| 2312 | if (data & (1u << (m - 1))) { |
| 2313 | crc_in++; |
| 2314 | } |
| 2315 | crc_in ^= gen; |
| 2316 | } else { |
| 2317 | crc_in <<= 1; |
| 2318 | if (data & (1u << (m - 1))) { |
| 2319 | crc_in++; |
| 2320 | } |
| 2321 | } |
| 2322 | data <<= 1; |
| 2323 | } |
| 2324 | |
| 2325 | return crc_in; |
| 2326 | } |
| 2327 | |
| 2328 | uint32_t helper_crcn(uint32_t arg0, uint32_t arg1, uint32_t arg2) |
| 2329 | { |
| 2330 | uint32_t crc_out, crc_in; |
| 2331 | uint32_t n = extract32(arg0, 12, 4) + 1; |
| 2332 | uint32_t gen = extract32(arg0, 16, n); |
| 2333 | uint32_t inv = extract32(arg0, 9, 1); |
| 2334 | uint32_t le = extract32(arg0, 8, 1); |
| 2335 | uint32_t m = extract32(arg0, 0, 3) + 1; |
| 2336 | uint32_t data = extract32(arg1, 0, m); |
| 2337 | uint32_t seed = extract32(arg2, 0, n); |
| 2338 | |
| 2339 | if (le == 1) { |
| 2340 | if (m == 0) { |
| 2341 | data = 0; |
| 2342 | } else { |
| 2343 | data = revbit32(data) >> (32 - m); |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | if (inv == 1) { |
| 2348 | seed = ~seed; |
| 2349 | } |
| 2350 | |
| 2351 | if (m > n) { |
| 2352 | crc_in = (data >> (m - n)) ^ seed; |
| 2353 | } else { |
| 2354 | crc_in = (data << (n - m)) ^ seed; |
| 2355 | } |
| 2356 | |
| 2357 | crc_out = crc_div(crc_in, data, gen, n, m); |
| 2358 | |
| 2359 | if (inv) { |
| 2360 | crc_out = ~crc_out; |
| 2361 | } |
| 2362 | |
| 2363 | return extract32(crc_out, 0, n); |
| 2364 | } |
| 2365 | |
| 2366 | uint32_t helper_shuffle(uint32_t arg0, uint32_t arg1) |
| 2367 | { |
| 2368 | uint32_t resb; |
| 2369 | uint32_t byte_select; |
| 2370 | uint32_t res = 0; |
| 2371 | |
| 2372 | byte_select = arg1 & 0x3; |
| 2373 | resb = extract32(arg0, byte_select * 8, 8); |
| 2374 | res |= resb << 0; |
| 2375 | |
| 2376 | byte_select = (arg1 >> 2) & 0x3; |
| 2377 | resb = extract32(arg0, byte_select * 8, 8); |
| 2378 | res |= resb << 8; |
| 2379 | |
| 2380 | byte_select = (arg1 >> 4) & 0x3; |
| 2381 | resb = extract32(arg0, byte_select * 8, 8); |
| 2382 | res |= resb << 16; |
| 2383 | |
| 2384 | byte_select = (arg1 >> 6) & 0x3; |
| 2385 | resb = extract32(arg0, byte_select * 8, 8); |
| 2386 | res |= resb << 24; |
| 2387 | |
| 2388 | if (arg1 & 0x100) { |
| 2389 | /* Assign the correct nibble position. */ |
| 2390 | res = ((res & 0xf0f0f0f0) >> 4) |
| 2391 | | ((res & 0x0f0f0f0f) << 4); |
| 2392 | /* Assign the correct bit position. */ |
| 2393 | res = ((res & 0x88888888) >> 3) |
| 2394 | | ((res & 0x44444444) >> 1) |
| 2395 | | ((res & 0x22222222) << 1) |
| 2396 | | ((res & 0x11111111) << 3); |
| 2397 | } |
| 2398 | |
| 2399 | return res; |
| 2400 | } |
| 2401 | |
| 2402 | /* context save area (CSA) related helpers */ |
| 2403 | |
| 2404 | static int cdc_increment(uint32_t *psw) |
| 2405 | { |
| 2406 | if ((*psw & MASK_PSW_CDC) == 0x7f) { |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | (*psw)++; |
| 2411 | /* check for overflow */ |
| 2412 | int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7)); |
| 2413 | int mask = (1u << (7 - lo)) - 1; |
| 2414 | int count = *psw & mask; |
| 2415 | if (count == 0) { |
| 2416 | (*psw)--; |
| 2417 | return 1; |
| 2418 | } |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
| 2422 | static int cdc_decrement(uint32_t *psw) |
| 2423 | { |
| 2424 | if ((*psw & MASK_PSW_CDC) == 0x7f) { |
| 2425 | return 0; |
| 2426 | } |
| 2427 | /* check for underflow */ |
| 2428 | int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7)); |
| 2429 | int mask = (1u << (7 - lo)) - 1; |
| 2430 | int count = *psw & mask; |
| 2431 | if (count == 0) { |
| 2432 | return 1; |
| 2433 | } |
| 2434 | (*psw)--; |
| 2435 | return 0; |
| 2436 | } |
| 2437 | |
| 2438 | static bool cdc_zero(uint32_t *psw) |
| 2439 | { |
| 2440 | int cdc = *psw & MASK_PSW_CDC; |
| 2441 | /* Returns TRUE if PSW.CDC.COUNT == 0 or if PSW.CDC == |
| 2442 | 7'b1111111, otherwise returns FALSE. */ |
| 2443 | if (cdc == 0x7f) { |
| 2444 | return true; |
| 2445 | } |
| 2446 | /* find CDC.COUNT */ |
| 2447 | int lo = clo32((*psw & MASK_PSW_CDC) << (32 - 7)); |
| 2448 | int mask = (1u << (7 - lo)) - 1; |
| 2449 | int count = *psw & mask; |
| 2450 | return count == 0; |
| 2451 | } |
| 2452 | |
| 2453 | static void save_context_upper(CPUTriCoreState *env, uint32_t ea) |
| 2454 | { |
| 2455 | cpu_stl_le_data(env, ea, env->PCXI); |
| 2456 | cpu_stl_le_data(env, ea + 4, psw_read(env)); |
| 2457 | cpu_stl_le_data(env, ea + 8, env->gpr_a[10]); |
| 2458 | cpu_stl_le_data(env, ea + 12, env->gpr_a[11]); |
| 2459 | cpu_stl_le_data(env, ea + 16, env->gpr_d[8]); |
| 2460 | cpu_stl_le_data(env, ea + 20, env->gpr_d[9]); |
| 2461 | cpu_stl_le_data(env, ea + 24, env->gpr_d[10]); |
| 2462 | cpu_stl_le_data(env, ea + 28, env->gpr_d[11]); |
| 2463 | cpu_stl_le_data(env, ea + 32, env->gpr_a[12]); |
| 2464 | cpu_stl_le_data(env, ea + 36, env->gpr_a[13]); |
| 2465 | cpu_stl_le_data(env, ea + 40, env->gpr_a[14]); |
| 2466 | cpu_stl_le_data(env, ea + 44, env->gpr_a[15]); |
| 2467 | cpu_stl_le_data(env, ea + 48, env->gpr_d[12]); |
| 2468 | cpu_stl_le_data(env, ea + 52, env->gpr_d[13]); |
| 2469 | cpu_stl_le_data(env, ea + 56, env->gpr_d[14]); |
| 2470 | cpu_stl_le_data(env, ea + 60, env->gpr_d[15]); |
| 2471 | } |
| 2472 | |
| 2473 | static void save_context_lower(CPUTriCoreState *env, uint32_t ea) |
| 2474 | { |
| 2475 | cpu_stl_le_data(env, ea, env->PCXI); |
| 2476 | cpu_stl_le_data(env, ea + 4, env->gpr_a[11]); |
| 2477 | cpu_stl_le_data(env, ea + 8, env->gpr_a[2]); |
| 2478 | cpu_stl_le_data(env, ea + 12, env->gpr_a[3]); |
| 2479 | cpu_stl_le_data(env, ea + 16, env->gpr_d[0]); |
| 2480 | cpu_stl_le_data(env, ea + 20, env->gpr_d[1]); |
| 2481 | cpu_stl_le_data(env, ea + 24, env->gpr_d[2]); |
| 2482 | cpu_stl_le_data(env, ea + 28, env->gpr_d[3]); |
| 2483 | cpu_stl_le_data(env, ea + 32, env->gpr_a[4]); |
| 2484 | cpu_stl_le_data(env, ea + 36, env->gpr_a[5]); |
| 2485 | cpu_stl_le_data(env, ea + 40, env->gpr_a[6]); |
| 2486 | cpu_stl_le_data(env, ea + 44, env->gpr_a[7]); |
| 2487 | cpu_stl_le_data(env, ea + 48, env->gpr_d[4]); |
| 2488 | cpu_stl_le_data(env, ea + 52, env->gpr_d[5]); |
| 2489 | cpu_stl_le_data(env, ea + 56, env->gpr_d[6]); |
| 2490 | cpu_stl_le_data(env, ea + 60, env->gpr_d[7]); |
| 2491 | } |
| 2492 | |
| 2493 | static void restore_context_upper(CPUTriCoreState *env, uint32_t ea, |
| 2494 | uint32_t *new_PCXI, uint32_t *new_PSW) |
| 2495 | { |
| 2496 | *new_PCXI = cpu_ldl_le_data(env, ea); |
| 2497 | *new_PSW = cpu_ldl_le_data(env, ea + 4); |
| 2498 | env->gpr_a[10] = cpu_ldl_le_data(env, ea + 8); |
| 2499 | env->gpr_a[11] = cpu_ldl_le_data(env, ea + 12); |
| 2500 | env->gpr_d[8] = cpu_ldl_le_data(env, ea + 16); |
| 2501 | env->gpr_d[9] = cpu_ldl_le_data(env, ea + 20); |
| 2502 | env->gpr_d[10] = cpu_ldl_le_data(env, ea + 24); |
| 2503 | env->gpr_d[11] = cpu_ldl_le_data(env, ea + 28); |
| 2504 | env->gpr_a[12] = cpu_ldl_le_data(env, ea + 32); |
| 2505 | env->gpr_a[13] = cpu_ldl_le_data(env, ea + 36); |
| 2506 | env->gpr_a[14] = cpu_ldl_le_data(env, ea + 40); |
| 2507 | env->gpr_a[15] = cpu_ldl_le_data(env, ea + 44); |
| 2508 | env->gpr_d[12] = cpu_ldl_le_data(env, ea + 48); |
| 2509 | env->gpr_d[13] = cpu_ldl_le_data(env, ea + 52); |
| 2510 | env->gpr_d[14] = cpu_ldl_le_data(env, ea + 56); |
| 2511 | env->gpr_d[15] = cpu_ldl_le_data(env, ea + 60); |
| 2512 | } |
| 2513 | |
| 2514 | static void restore_context_lower(CPUTriCoreState *env, uint32_t ea, |
| 2515 | uint32_t *ra, uint32_t *pcxi) |
| 2516 | { |
| 2517 | *pcxi = cpu_ldl_le_data(env, ea); |
| 2518 | *ra = cpu_ldl_le_data(env, ea + 4); |
| 2519 | env->gpr_a[2] = cpu_ldl_le_data(env, ea + 8); |
| 2520 | env->gpr_a[3] = cpu_ldl_le_data(env, ea + 12); |
| 2521 | env->gpr_d[0] = cpu_ldl_le_data(env, ea + 16); |
| 2522 | env->gpr_d[1] = cpu_ldl_le_data(env, ea + 20); |
| 2523 | env->gpr_d[2] = cpu_ldl_le_data(env, ea + 24); |
| 2524 | env->gpr_d[3] = cpu_ldl_le_data(env, ea + 28); |
| 2525 | env->gpr_a[4] = cpu_ldl_le_data(env, ea + 32); |
| 2526 | env->gpr_a[5] = cpu_ldl_le_data(env, ea + 36); |
| 2527 | env->gpr_a[6] = cpu_ldl_le_data(env, ea + 40); |
| 2528 | env->gpr_a[7] = cpu_ldl_le_data(env, ea + 44); |
| 2529 | env->gpr_d[4] = cpu_ldl_le_data(env, ea + 48); |
| 2530 | env->gpr_d[5] = cpu_ldl_le_data(env, ea + 52); |
| 2531 | env->gpr_d[6] = cpu_ldl_le_data(env, ea + 56); |
| 2532 | env->gpr_d[7] = cpu_ldl_le_data(env, ea + 60); |
| 2533 | } |
| 2534 | |
| 2535 | void helper_call(CPUTriCoreState *env, uint32_t next_pc) |
| 2536 | { |
| 2537 | uint32_t tmp_FCX; |
| 2538 | uint32_t ea; |
| 2539 | uint32_t new_FCX; |
| 2540 | uint32_t psw; |
| 2541 | |
| 2542 | psw = psw_read(env); |
| 2543 | /* if (FCX == 0) trap(FCU); */ |
| 2544 | if (env->FCX == 0) { |
| 2545 | /* FCU trap */ |
| 2546 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCU, GETPC()); |
| 2547 | } |
| 2548 | /* if (PSW.CDE) then if (cdc_increment()) then trap(CDO); */ |
| 2549 | if (psw & MASK_PSW_CDE) { |
| 2550 | if (cdc_increment(&psw)) { |
| 2551 | /* CDO trap */ |
| 2552 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CDO, GETPC()); |
| 2553 | } |
| 2554 | } |
| 2555 | /* PSW.CDE = 1;*/ |
| 2556 | psw |= MASK_PSW_CDE; |
| 2557 | /* |
| 2558 | * we need to save PSW.CDE and not PSW.CDC into the CSAs. psw already |
| 2559 | * contains the CDC from cdc_increment(), so we cannot call psw_write() |
| 2560 | * here. |
| 2561 | */ |
| 2562 | env->PSW |= MASK_PSW_CDE; |
| 2563 | |
| 2564 | /* tmp_FCX = FCX; */ |
| 2565 | tmp_FCX = env->FCX; |
| 2566 | /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */ |
| 2567 | ea = ((env->FCX & MASK_FCX_FCXS) << 12) + |
| 2568 | ((env->FCX & MASK_FCX_FCXO) << 6); |
| 2569 | /* new_FCX = M(EA, word); */ |
| 2570 | new_FCX = cpu_ldl_le_data(env, ea); |
| 2571 | /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], |
| 2572 | A[12], A[13], A[14], A[15], D[12], D[13], D[14], |
| 2573 | D[15]}; */ |
| 2574 | save_context_upper(env, ea); |
| 2575 | |
| 2576 | /* PCXI.PCPN = ICR.CCPN; */ |
| 2577 | pcxi_set_pcpn(env, icr_get_ccpn(env)); |
| 2578 | /* PCXI.PIE = ICR.IE; */ |
| 2579 | pcxi_set_pie(env, icr_get_ie(env)); |
| 2580 | /* PCXI.UL = 1; */ |
| 2581 | pcxi_set_ul(env, 1); |
| 2582 | |
| 2583 | /* PCXI[19: 0] = FCX[19: 0]; */ |
| 2584 | env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff); |
| 2585 | /* FCX[19: 0] = new_FCX[19: 0]; */ |
| 2586 | env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff); |
| 2587 | /* A[11] = next_pc[31: 0]; */ |
| 2588 | env->gpr_a[11] = next_pc; |
| 2589 | |
| 2590 | /* if (tmp_FCX == LCX) trap(FCD);*/ |
| 2591 | if (tmp_FCX == env->LCX) { |
| 2592 | /* FCD trap */ |
| 2593 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCD, GETPC()); |
| 2594 | } |
| 2595 | psw_write(env, psw); |
| 2596 | } |
| 2597 | |
| 2598 | void helper_ret(CPUTriCoreState *env) |
| 2599 | { |
| 2600 | uint32_t ea; |
| 2601 | uint32_t new_PCXI; |
| 2602 | uint32_t new_PSW, psw; |
| 2603 | |
| 2604 | psw = psw_read(env); |
| 2605 | /* if (PSW.CDE) then if (cdc_decrement()) then trap(CDU);*/ |
| 2606 | if (psw & MASK_PSW_CDE) { |
| 2607 | if (cdc_decrement(&psw)) { |
| 2608 | /* CDU trap */ |
| 2609 | psw_write(env, psw); |
| 2610 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CDU, GETPC()); |
| 2611 | } |
| 2612 | } |
| 2613 | /* if (PCXI[19: 0] == 0) then trap(CSU); */ |
| 2614 | if ((env->PCXI & 0xfffff) == 0) { |
| 2615 | /* CSU trap */ |
| 2616 | psw_write(env, psw); |
| 2617 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CSU, GETPC()); |
| 2618 | } |
| 2619 | /* if (PCXI.UL == 0) then trap(CTYP); */ |
| 2620 | if (pcxi_get_ul(env) == 0) { |
| 2621 | /* CTYP trap */ |
| 2622 | cdc_increment(&psw); /* restore to the start of helper */ |
| 2623 | psw_write(env, psw); |
| 2624 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CTYP, GETPC()); |
| 2625 | } |
| 2626 | /* PC = {A11 [31: 1], 1’b0}; */ |
| 2627 | env->PC = env->gpr_a[11] & 0xfffffffe; |
| 2628 | |
| 2629 | /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */ |
| 2630 | ea = (pcxi_get_pcxs(env) << 28) | |
| 2631 | (pcxi_get_pcxo(env) << 6); |
| 2632 | /* {new_PCXI, new_PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12], |
| 2633 | A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */ |
| 2634 | restore_context_upper(env, ea, &new_PCXI, &new_PSW); |
| 2635 | /* M(EA, word) = FCX; */ |
| 2636 | cpu_stl_le_data(env, ea, env->FCX); |
| 2637 | /* FCX[19: 0] = PCXI[19: 0]; */ |
| 2638 | env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff); |
| 2639 | /* PCXI = new_PCXI; */ |
| 2640 | env->PCXI = new_PCXI; |
| 2641 | |
| 2642 | if (tricore_has_feature(env, TRICORE_FEATURE_131)) { |
| 2643 | /* PSW = {new_PSW[31:26], PSW[25:24], new_PSW[23:0]}; */ |
| 2644 | psw_write(env, (new_PSW & ~(0x3000000)) + (psw & (0x3000000))); |
| 2645 | } else { /* TRICORE_FEATURE_13 only */ |
| 2646 | /* PSW = new_PSW */ |
| 2647 | psw_write(env, new_PSW); |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | void helper_bisr(CPUTriCoreState *env, uint32_t const9) |
| 2652 | { |
| 2653 | uint32_t tmp_FCX; |
| 2654 | uint32_t ea; |
| 2655 | uint32_t new_FCX; |
| 2656 | |
| 2657 | if (env->FCX == 0) { |
| 2658 | /* FCU trap */ |
| 2659 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCU, GETPC()); |
| 2660 | } |
| 2661 | |
| 2662 | tmp_FCX = env->FCX; |
| 2663 | ea = ((env->FCX & 0xf0000) << 12) + ((env->FCX & 0xffff) << 6); |
| 2664 | |
| 2665 | /* new_FCX = M(EA, word); */ |
| 2666 | new_FCX = cpu_ldl_le_data(env, ea); |
| 2667 | /* M(EA, 16 * word) = {PCXI, A[11], A[2], A[3], D[0], D[1], D[2], D[3], A[4] |
| 2668 | , A[5], A[6], A[7], D[4], D[5], D[6], D[7]}; */ |
| 2669 | save_context_lower(env, ea); |
| 2670 | |
| 2671 | |
| 2672 | /* PCXI.PCPN = ICR.CCPN */ |
| 2673 | pcxi_set_pcpn(env, icr_get_ccpn(env)); |
| 2674 | /* PCXI.PIE = ICR.IE */ |
| 2675 | pcxi_set_pie(env, icr_get_ie(env)); |
| 2676 | /* PCXI.UL = 0 */ |
| 2677 | pcxi_set_ul(env, 0); |
| 2678 | |
| 2679 | /* PCXI[19: 0] = FCX[19: 0] */ |
| 2680 | env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff); |
| 2681 | /* FXC[19: 0] = new_FCX[19: 0] */ |
| 2682 | env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff); |
| 2683 | |
| 2684 | /* ICR.IE = 1 */ |
| 2685 | icr_set_ie(env, 1); |
| 2686 | |
| 2687 | icr_set_ccpn(env, const9); |
| 2688 | |
| 2689 | if (tmp_FCX == env->LCX) { |
| 2690 | /* FCD trap */ |
| 2691 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCD, GETPC()); |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | void helper_rfe(CPUTriCoreState *env) |
| 2696 | { |
| 2697 | uint32_t ea; |
| 2698 | uint32_t new_PCXI; |
| 2699 | uint32_t new_PSW; |
| 2700 | /* if (PCXI[19: 0] == 0) then trap(CSU); */ |
| 2701 | if ((env->PCXI & 0xfffff) == 0) { |
| 2702 | /* raise csu trap */ |
| 2703 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CSU, GETPC()); |
| 2704 | } |
| 2705 | /* if (PCXI.UL == 0) then trap(CTYP); */ |
| 2706 | if (pcxi_get_ul(env) == 0) { |
| 2707 | /* raise CTYP trap */ |
| 2708 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CTYP, GETPC()); |
| 2709 | } |
| 2710 | /* if (!cdc_zero() AND PSW.CDE) then trap(NEST); */ |
| 2711 | if (!cdc_zero(&(env->PSW)) && (env->PSW & MASK_PSW_CDE)) { |
| 2712 | /* raise NEST trap */ |
| 2713 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_NEST, GETPC()); |
| 2714 | } |
| 2715 | env->PC = env->gpr_a[11] & ~0x1; |
| 2716 | /* ICR.IE = PCXI.PIE; */ |
| 2717 | icr_set_ie(env, pcxi_get_pie(env)); |
| 2718 | |
| 2719 | /* ICR.CCPN = PCXI.PCPN; */ |
| 2720 | icr_set_ccpn(env, pcxi_get_pcpn(env)); |
| 2721 | |
| 2722 | /*EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0};*/ |
| 2723 | ea = (pcxi_get_pcxs(env) << 28) | |
| 2724 | (pcxi_get_pcxo(env) << 6); |
| 2725 | |
| 2726 | /*{new_PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], A[12], |
| 2727 | A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */ |
| 2728 | restore_context_upper(env, ea, &new_PCXI, &new_PSW); |
| 2729 | /* M(EA, word) = FCX;*/ |
| 2730 | cpu_stl_le_data(env, ea, env->FCX); |
| 2731 | /* FCX[19: 0] = PCXI[19: 0]; */ |
| 2732 | env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff); |
| 2733 | /* PCXI = new_PCXI; */ |
| 2734 | env->PCXI = new_PCXI; |
| 2735 | /* write psw */ |
| 2736 | psw_write(env, new_PSW); |
| 2737 | } |
| 2738 | |
| 2739 | void helper_rfm(CPUTriCoreState *env) |
| 2740 | { |
| 2741 | env->PC = (env->gpr_a[11] & ~0x1); |
| 2742 | /* ICR.IE = PCXI.PIE; */ |
| 2743 | icr_set_ie(env, pcxi_get_pie(env)); |
| 2744 | /* ICR.CCPN = PCXI.PCPN; */ |
| 2745 | icr_set_ccpn(env, pcxi_get_pcpn(env)); |
| 2746 | |
| 2747 | /* {PCXI, PSW, A[10], A[11]} = M(DCX, 4 * word); */ |
| 2748 | env->PCXI = cpu_ldl_le_data(env, env->DCX); |
| 2749 | psw_write(env, cpu_ldl_le_data(env, env->DCX + 4)); |
| 2750 | env->gpr_a[10] = cpu_ldl_le_data(env, env->DCX + 8); |
| 2751 | env->gpr_a[11] = cpu_ldl_le_data(env, env->DCX + 12); |
| 2752 | |
| 2753 | if (tricore_has_feature(env, TRICORE_FEATURE_131)) { |
| 2754 | env->DBGTCR = 0; |
| 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | void helper_ldlcx(CPUTriCoreState *env, uint32_t ea) |
| 2759 | { |
| 2760 | uint32_t dummy; |
| 2761 | /* insn doesn't load PCXI and RA */ |
| 2762 | restore_context_lower(env, ea, &dummy, &dummy); |
| 2763 | } |
| 2764 | |
| 2765 | void helper_lducx(CPUTriCoreState *env, uint32_t ea) |
| 2766 | { |
| 2767 | uint32_t dummy; |
| 2768 | /* insn doesn't load PCXI and PSW */ |
| 2769 | restore_context_upper(env, ea, &dummy, &dummy); |
| 2770 | } |
| 2771 | |
| 2772 | void helper_stlcx(CPUTriCoreState *env, uint32_t ea) |
| 2773 | { |
| 2774 | save_context_lower(env, ea); |
| 2775 | } |
| 2776 | |
| 2777 | void helper_stucx(CPUTriCoreState *env, uint32_t ea) |
| 2778 | { |
| 2779 | save_context_upper(env, ea); |
| 2780 | } |
| 2781 | |
| 2782 | void helper_svlcx(CPUTriCoreState *env) |
| 2783 | { |
| 2784 | uint32_t tmp_FCX; |
| 2785 | uint32_t ea; |
| 2786 | uint32_t new_FCX; |
| 2787 | |
| 2788 | if (env->FCX == 0) { |
| 2789 | /* FCU trap */ |
| 2790 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCU, GETPC()); |
| 2791 | } |
| 2792 | /* tmp_FCX = FCX; */ |
| 2793 | tmp_FCX = env->FCX; |
| 2794 | /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */ |
| 2795 | ea = ((env->FCX & MASK_FCX_FCXS) << 12) + |
| 2796 | ((env->FCX & MASK_FCX_FCXO) << 6); |
| 2797 | /* new_FCX = M(EA, word); */ |
| 2798 | new_FCX = cpu_ldl_le_data(env, ea); |
| 2799 | /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], |
| 2800 | A[12], A[13], A[14], A[15], D[12], D[13], D[14], |
| 2801 | D[15]}; */ |
| 2802 | save_context_lower(env, ea); |
| 2803 | |
| 2804 | /* PCXI.PCPN = ICR.CCPN; */ |
| 2805 | pcxi_set_pcpn(env, icr_get_ccpn(env)); |
| 2806 | |
| 2807 | /* PCXI.PIE = ICR.IE; */ |
| 2808 | pcxi_set_pie(env, icr_get_ie(env)); |
| 2809 | |
| 2810 | /* PCXI.UL = 0; */ |
| 2811 | pcxi_set_ul(env, 0); |
| 2812 | |
| 2813 | /* PCXI[19: 0] = FCX[19: 0]; */ |
| 2814 | env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff); |
| 2815 | /* FCX[19: 0] = new_FCX[19: 0]; */ |
| 2816 | env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff); |
| 2817 | |
| 2818 | /* if (tmp_FCX == LCX) trap(FCD);*/ |
| 2819 | if (tmp_FCX == env->LCX) { |
| 2820 | /* FCD trap */ |
| 2821 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCD, GETPC()); |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | void helper_svucx(CPUTriCoreState *env) |
| 2826 | { |
| 2827 | uint32_t tmp_FCX; |
| 2828 | uint32_t ea; |
| 2829 | uint32_t new_FCX; |
| 2830 | |
| 2831 | if (env->FCX == 0) { |
| 2832 | /* FCU trap */ |
| 2833 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCU, GETPC()); |
| 2834 | } |
| 2835 | /* tmp_FCX = FCX; */ |
| 2836 | tmp_FCX = env->FCX; |
| 2837 | /* EA = {FCX.FCXS, 6'b0, FCX.FCXO, 6'b0}; */ |
| 2838 | ea = ((env->FCX & MASK_FCX_FCXS) << 12) + |
| 2839 | ((env->FCX & MASK_FCX_FCXO) << 6); |
| 2840 | /* new_FCX = M(EA, word); */ |
| 2841 | new_FCX = cpu_ldl_le_data(env, ea); |
| 2842 | /* M(EA, 16 * word) = {PCXI, PSW, A[10], A[11], D[8], D[9], D[10], D[11], |
| 2843 | A[12], A[13], A[14], A[15], D[12], D[13], D[14], |
| 2844 | D[15]}; */ |
| 2845 | save_context_upper(env, ea); |
| 2846 | |
| 2847 | /* PCXI.PCPN = ICR.CCPN; */ |
| 2848 | pcxi_set_pcpn(env, icr_get_ccpn(env)); |
| 2849 | |
| 2850 | /* PCXI.PIE = ICR.IE; */ |
| 2851 | pcxi_set_pie(env, icr_get_ie(env)); |
| 2852 | |
| 2853 | /* PCXI.UL = 1; */ |
| 2854 | pcxi_set_ul(env, 1); |
| 2855 | |
| 2856 | /* PCXI[19: 0] = FCX[19: 0]; */ |
| 2857 | env->PCXI = (env->PCXI & 0xfff00000) + (env->FCX & 0xfffff); |
| 2858 | /* FCX[19: 0] = new_FCX[19: 0]; */ |
| 2859 | env->FCX = (env->FCX & 0xfff00000) + (new_FCX & 0xfffff); |
| 2860 | |
| 2861 | /* if (tmp_FCX == LCX) trap(FCD);*/ |
| 2862 | if (tmp_FCX == env->LCX) { |
| 2863 | /* FCD trap */ |
| 2864 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_FCD, GETPC()); |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | void helper_rslcx(CPUTriCoreState *env) |
| 2869 | { |
| 2870 | uint32_t ea; |
| 2871 | uint32_t new_PCXI; |
| 2872 | /* if (PCXI[19: 0] == 0) then trap(CSU); */ |
| 2873 | if ((env->PCXI & 0xfffff) == 0) { |
| 2874 | /* CSU trap */ |
| 2875 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CSU, GETPC()); |
| 2876 | } |
| 2877 | /* if (PCXI.UL == 1) then trap(CTYP); */ |
| 2878 | if (pcxi_get_ul(env) == 1) { |
| 2879 | /* CTYP trap */ |
| 2880 | raise_exception_sync_helper(env, TRAPC_CTX_MNG, TIN3_CTYP, GETPC()); |
| 2881 | } |
| 2882 | /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */ |
| 2883 | /* EA = {PCXI.PCXS, 6'b0, PCXI.PCXO, 6'b0}; */ |
| 2884 | ea = (pcxi_get_pcxs(env) << 28) | |
| 2885 | (pcxi_get_pcxo(env) << 6); |
| 2886 | |
| 2887 | /* {new_PCXI, A[11], A[10], A[11], D[8], D[9], D[10], D[11], A[12], |
| 2888 | A[13], A[14], A[15], D[12], D[13], D[14], D[15]} = M(EA, 16 * word); */ |
| 2889 | restore_context_lower(env, ea, &env->gpr_a[11], &new_PCXI); |
| 2890 | /* M(EA, word) = FCX; */ |
| 2891 | cpu_stl_le_data(env, ea, env->FCX); |
| 2892 | /* M(EA, word) = FCX; */ |
| 2893 | cpu_stl_le_data(env, ea, env->FCX); |
| 2894 | /* FCX[19: 0] = PCXI[19: 0]; */ |
| 2895 | env->FCX = (env->FCX & 0xfff00000) + (env->PCXI & 0x000fffff); |
| 2896 | /* PCXI = new_PCXI; */ |
| 2897 | env->PCXI = new_PCXI; |
| 2898 | } |
| 2899 | |
| 2900 | void helper_psw_write(CPUTriCoreState *env, uint32_t arg) |
| 2901 | { |
| 2902 | psw_write(env, arg); |
| 2903 | } |
| 2904 | |
| 2905 | uint32_t helper_psw_read(CPUTriCoreState *env) |
| 2906 | { |
| 2907 | return psw_read(env); |
| 2908 | } |