flag_handler.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. MSP430 Emulator
  3. Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)
  4. "MSP430 Emulator" is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. "MSP430 Emulator" is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "decoder.h"
  16. #include "flag_handler.h"
  17. /**
  18. * @brief Test if the result of the asm instruction is zero
  19. * @param result_addr The address of the operation's result
  20. * @param bw_flag Byte or Word flag
  21. * @return true if zero, false otherwise
  22. */
  23. uint8_t is_zero (uint16_t *result_addr, uint8_t bw_flag)
  24. {
  25. if (bw_flag == WORD) {
  26. if (*result_addr == 0 ) {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. else if (bw_flag == BYTE) {
  32. if (*(int8_t *) result_addr == 0) {
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. return false;
  38. }
  39. /**
  40. * @brief Test if the result of the asm instruction is negative
  41. * @param result_addr The address of the operation's result
  42. * @param bw_flag Byte or Word flag
  43. * @return true if zero, false otherwise
  44. */
  45. uint8_t is_negative(int16_t *result_addr, uint8_t bw_flag)
  46. {
  47. if (bw_flag == WORD) {
  48. if (*result_addr < 0) {
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. else if (bw_flag == BYTE) {
  54. if (*((int8_t *) result_addr) < 0) {
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. return false;
  60. }
  61. /**
  62. * @brief Test if the result of the asm instruction WILL carry
  63. * @param original_dst_value The original value at the destination
  64. * @param source_value The value at the source location
  65. * @param bw_flag Byte or Word flag
  66. * @return true if zero, false otherwise
  67. */
  68. uint8_t is_carried(uint32_t original_dst_value, uint32_t source_value,
  69. uint8_t bw_flag)
  70. {
  71. if (bw_flag == WORD) {
  72. if ((65535 - (uint16_t)source_value) < (uint16_t)original_dst_value ||
  73. ((original_dst_value + source_value) >> 16) != 0) {
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. else if (bw_flag == BYTE) {
  79. if ((255 - (uint8_t)source_value) < (uint8_t)original_dst_value ||
  80. ((original_dst_value + source_value) >> 8) != 0) {
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. return false;
  86. }
  87. /**
  88. * @brief Test if the result of the asm instruction is overflowed
  89. * @param source_value The value at the source operand
  90. * @param destination_value The value at the destination operand
  91. * @param result A pointer to the result of the operation
  92. * @param bw_flag Byte or Word flag
  93. * @return true if zero, false otherwise
  94. */
  95. uint8_t is_overflowed(uint16_t source_value, uint16_t destination_value,
  96. uint16_t *result, uint8_t bw_flag)
  97. {
  98. if (bw_flag == WORD) {
  99. if ( (source_value >> 15) == (destination_value >> 15) &&
  100. (*result >> 15) != (destination_value >> 15) ) {
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. else if (bw_flag == BYTE) {
  106. uint8_t dst_prev_value = (uint8_t) destination_value;
  107. uint8_t src_value = (uint8_t) source_value;
  108. if ( (src_value >> 7) == (dst_prev_value >> 7) &&
  109. (*(uint8_t *)result >> 7) != (dst_prev_value >> 7)) {
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. else {
  115. printf("Error, overflowed function: invalid bw_flag\n");
  116. }
  117. return false;
  118. }