helper.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Misc Sparc helpers
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "cpu.h"
  20. #include "host-utils.h"
  21. #include "helper.h"
  22. #include "sysemu.h"
  23. void helper_raise_exception(CPUSPARCState *env, int tt)
  24. {
  25. env->exception_index = tt;
  26. cpu_loop_exit(env);
  27. }
  28. void helper_debug(CPUSPARCState *env)
  29. {
  30. env->exception_index = EXCP_DEBUG;
  31. cpu_loop_exit(env);
  32. }
  33. #ifdef TARGET_SPARC64
  34. target_ulong helper_popc(target_ulong val)
  35. {
  36. return ctpop64(val);
  37. }
  38. void helper_tick_set_count(void *opaque, uint64_t count)
  39. {
  40. #if !defined(CONFIG_USER_ONLY)
  41. cpu_tick_set_count(opaque, count);
  42. #endif
  43. }
  44. uint64_t helper_tick_get_count(void *opaque)
  45. {
  46. #if !defined(CONFIG_USER_ONLY)
  47. return cpu_tick_get_count(opaque);
  48. #else
  49. return 0;
  50. #endif
  51. }
  52. void helper_tick_set_limit(void *opaque, uint64_t limit)
  53. {
  54. #if !defined(CONFIG_USER_ONLY)
  55. cpu_tick_set_limit(opaque, limit);
  56. #endif
  57. }
  58. #endif
  59. static target_ulong helper_udiv_common(CPUSPARCState *env, target_ulong a,
  60. target_ulong b, int cc)
  61. {
  62. int overflow = 0;
  63. uint64_t x0;
  64. uint32_t x1;
  65. x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
  66. x1 = (b & 0xffffffff);
  67. if (x1 == 0) {
  68. helper_raise_exception(env, TT_DIV_ZERO);
  69. }
  70. x0 = x0 / x1;
  71. if (x0 > 0xffffffff) {
  72. x0 = 0xffffffff;
  73. overflow = 1;
  74. }
  75. if (cc) {
  76. env->cc_dst = x0;
  77. env->cc_src2 = overflow;
  78. env->cc_op = CC_OP_DIV;
  79. }
  80. return x0;
  81. }
  82. target_ulong helper_udiv(CPUSPARCState *env, target_ulong a, target_ulong b)
  83. {
  84. return helper_udiv_common(env, a, b, 0);
  85. }
  86. target_ulong helper_udiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b)
  87. {
  88. return helper_udiv_common(env, a, b, 1);
  89. }
  90. static target_ulong helper_sdiv_common(CPUSPARCState *env, target_ulong a,
  91. target_ulong b, int cc)
  92. {
  93. int overflow = 0;
  94. int64_t x0;
  95. int32_t x1;
  96. x0 = (a & 0xffffffff) | ((int64_t) (env->y) << 32);
  97. x1 = (b & 0xffffffff);
  98. if (x1 == 0) {
  99. helper_raise_exception(env, TT_DIV_ZERO);
  100. }
  101. x0 = x0 / x1;
  102. if ((int32_t) x0 != x0) {
  103. x0 = x0 < 0 ? 0x80000000 : 0x7fffffff;
  104. overflow = 1;
  105. }
  106. if (cc) {
  107. env->cc_dst = x0;
  108. env->cc_src2 = overflow;
  109. env->cc_op = CC_OP_DIV;
  110. }
  111. return x0;
  112. }
  113. target_ulong helper_sdiv(CPUSPARCState *env, target_ulong a, target_ulong b)
  114. {
  115. return helper_sdiv_common(env, a, b, 0);
  116. }
  117. target_ulong helper_sdiv_cc(CPUSPARCState *env, target_ulong a, target_ulong b)
  118. {
  119. return helper_sdiv_common(env, a, b, 1);
  120. }