registers.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "registers.h"
  16. //##########+++ MSP430 Register initialization +++##########
  17. void initialize_msp_registers(Emulator *emu)
  18. {
  19. Cpu *cpu = emu->cpu;
  20. Debugger *debugger = emu->debugger;
  21. /* Initialize PC to boot loader code on cold boot (COLD)*/
  22. //cpu->pc = 0x0C00;
  23. /* Initialize Program Counter to *0xFFFE at boot or reset (WARM)*/
  24. cpu->pc = 0xC000;
  25. /* Stack pointer typically begins at the top of RAM after reset */
  26. cpu->sp = 0x400;
  27. /* Initialize the status register */
  28. memset(&cpu->sr, 0, sizeof(Status_reg));
  29. cpu->running = false;
  30. cpu->cg2 = 0;
  31. cpu->r4 = cpu->r5 = cpu->r6 = cpu->r7 = cpu->r8 =
  32. cpu->r9 = cpu->r10 = cpu->r11 = cpu->r12 = cpu->r13 =
  33. cpu->r14 = cpu->r15 = 0;
  34. }
  35. void update_register_display (Emulator *emu)
  36. {
  37. Cpu *cpu = emu->cpu;
  38. char thing[50] = "....";
  39. if (emu->cpu->running) {
  40. send_control(emu, UPDATE_ALL_REGS_PACKET, (void *)thing, strlen(thing));
  41. return;
  42. }
  43. sprintf(thing, "%04X", cpu->pc);
  44. send_control(emu, UPDATE_REG_R0_PACKET, (void *)thing, strlen(thing));
  45. sprintf(thing, "%04X", cpu->sp);
  46. send_control(emu, UPDATE_REG_R1_PACKET, (void *)thing, strlen(thing));
  47. sprintf(thing, "%04X", sr_to_value(emu));
  48. send_control(emu, UPDATE_REG_R2_PACKET, (void *)thing, strlen(thing));
  49. sprintf(thing, "%04X",(uint16_t) cpu->cg2);
  50. send_control(emu, UPDATE_REG_R3_PACKET, (void *)thing, strlen(thing));
  51. sprintf(thing, "%04X",(uint16_t) cpu->r4);
  52. send_control(emu, UPDATE_REG_R4_PACKET, (void *)thing, strlen(thing));
  53. sprintf(thing, "%04X",(uint16_t) cpu->r5);
  54. send_control(emu, UPDATE_REG_R5_PACKET, (void *)thing, strlen(thing));
  55. sprintf(thing, "%04X",(uint16_t) cpu->r6);
  56. send_control(emu, UPDATE_REG_R6_PACKET, (void *)thing, strlen(thing));
  57. sprintf(thing, "%04X",(uint16_t) cpu->r7);
  58. send_control(emu, UPDATE_REG_R7_PACKET, (void *)thing, strlen(thing));
  59. sprintf(thing, "%04X",(uint16_t) cpu->r8);
  60. send_control(emu, UPDATE_REG_R8_PACKET, (void *)thing, strlen(thing));
  61. sprintf(thing, "%04X",(uint16_t) cpu->r9);
  62. send_control(emu, UPDATE_REG_R9_PACKET, (void *)thing, strlen(thing));
  63. sprintf(thing, "%04X", (uint16_t)cpu->r10);
  64. send_control(emu, UPDATE_REG_R10_PACKET, (void *)thing, strlen(thing));
  65. sprintf(thing, "%04X", (uint16_t)cpu->r11);
  66. send_control(emu, UPDATE_REG_R11_PACKET, (void *)thing, strlen(thing));
  67. sprintf(thing, "%04X", (uint16_t)cpu->r12);
  68. send_control(emu, UPDATE_REG_R12_PACKET, (void *)thing, strlen(thing));
  69. sprintf(thing, "%04X", (uint16_t)cpu->r13);
  70. send_control(emu, UPDATE_REG_R13_PACKET, (void *)thing, strlen(thing));
  71. sprintf(thing, "%04X", (uint16_t)cpu->r14);
  72. send_control(emu, UPDATE_REG_R14_PACKET, (void *)thing, strlen(thing));
  73. sprintf(thing, "%04X", (uint16_t)cpu->r15);
  74. send_control(emu, UPDATE_REG_R15_PACKET, (void *)thing, strlen(thing));
  75. }
  76. //##########+++ Set SR struct Value +++##########
  77. void set_sr_value (Emulator *emu, uint16_t value)
  78. {
  79. Cpu *cpu = emu->cpu;
  80. // reset SR to set it properly...
  81. memset(&cpu->sr, 0, sizeof(Status_reg));
  82. //memcpy(&cpu->sr, &value, 16);
  83. if (value & 0x8000) cpu->sr.reserved |= 0x8000;
  84. if (value & 0x4000) cpu->sr.reserved |= 0x4000;
  85. if (value & 0x2000) cpu->sr.reserved |= 0x2000;
  86. if (value & 0x1000) cpu->sr.reserved |= 0x1000;
  87. if (value & 0x0800) cpu->sr.reserved |= 0x0800;
  88. if (value & 0x0400) cpu->sr.reserved |= 0x0400;
  89. if (value & 0x0200) cpu->sr.reserved |= 0x0200;
  90. cpu->sr.overflow = (value & 0x0100) ? 1 : 0;
  91. cpu->sr.SCG1 = (value & 0x0080) ? 1 : 0;
  92. cpu->sr.SCG0 = (value & 0x0040) ? 1 : 0;
  93. cpu->sr.OSCOFF = (value & 0x0020) ? 1 : 0;
  94. cpu->sr.CPUOFF = (value & 0x0010) ? 1 : 0;
  95. cpu->sr.GIE = (value & 0x0008) ? 1 : 0;
  96. cpu->sr.negative = (value & 0x0004) ? 1 : 0;
  97. cpu->sr.zero = (value & 0x0002) ? 1 : 0;
  98. cpu->sr.carry = (value & 0x0001) ? 1 : 0;
  99. }
  100. //##########+++ Return value from SR struct +++##########
  101. uint16_t sr_to_value(Emulator *emu)
  102. {
  103. Cpu *cpu = emu->cpu;
  104. uint16_t r2 = 0;
  105. // reserved bits not working quite right yet
  106. if (cpu->sr.reserved & 0b1000000) {
  107. r2 |= 0x8000;
  108. }
  109. if (cpu->sr.reserved & 0b0100000) {
  110. r2 |= 0x4000;
  111. }
  112. if (cpu->sr.reserved & 0b0010000) {
  113. r2 |= 0x2000;
  114. }
  115. if (cpu->sr.reserved & 0b0001000) {
  116. r2 |= 0x1000;
  117. }
  118. if (cpu->sr.reserved & 0b0000100) {
  119. r2 |= 0x0800;
  120. }
  121. if (cpu->sr.reserved & 0b0000010) {
  122. r2 |= 0x0400;
  123. }
  124. if (cpu->sr.reserved & 0b0000001) {
  125. r2 |= 0x0200;
  126. }
  127. if (cpu->sr.overflow) {
  128. r2 |= 0x0100;
  129. }
  130. if (cpu->sr.SCG1) {
  131. r2 |= 0x0080;
  132. }
  133. if (cpu->sr.SCG0) {
  134. r2 |= 0x0040;
  135. }
  136. if (cpu->sr.OSCOFF) {
  137. r2 |= 0x0020;
  138. }
  139. if (cpu->sr.CPUOFF) {
  140. r2 |= 0x0010;
  141. }
  142. if (cpu->sr.GIE) {
  143. r2 |= 0x0008;
  144. }
  145. if (cpu->sr.negative) {
  146. r2 |= 0x0004;
  147. }
  148. if (cpu->sr.zero) {
  149. r2 |= 0x0002;
  150. }
  151. if (cpu->sr.carry) {
  152. r2 |= 0x0001;
  153. }
  154. return r2;
  155. }