registers.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. cpu->interrupt = NULL_VECTOR;
  35. }
  36. void update_register_display (Emulator *emu)
  37. {
  38. Cpu *cpu = emu->cpu;
  39. char thing[50] = "....";
  40. if (emu->cpu->running) {
  41. send_control(emu, UPDATE_ALL_REGS_PACKET, (void *)thing, strlen(thing));
  42. return;
  43. }
  44. sprintf(thing, "%04X", cpu->pc);
  45. send_control(emu, UPDATE_REG_R0_PACKET, (void *)thing, strlen(thing));
  46. sprintf(thing, "%04X", cpu->sp);
  47. send_control(emu, UPDATE_REG_R1_PACKET, (void *)thing, strlen(thing));
  48. sprintf(thing, "%04X", sr_to_value(emu));
  49. send_control(emu, UPDATE_REG_R2_PACKET, (void *)thing, strlen(thing));
  50. sprintf(thing, "%04X",(uint16_t) cpu->cg2);
  51. send_control(emu, UPDATE_REG_R3_PACKET, (void *)thing, strlen(thing));
  52. sprintf(thing, "%04X",(uint16_t) cpu->r4);
  53. send_control(emu, UPDATE_REG_R4_PACKET, (void *)thing, strlen(thing));
  54. sprintf(thing, "%04X",(uint16_t) cpu->r5);
  55. send_control(emu, UPDATE_REG_R5_PACKET, (void *)thing, strlen(thing));
  56. sprintf(thing, "%04X",(uint16_t) cpu->r6);
  57. send_control(emu, UPDATE_REG_R6_PACKET, (void *)thing, strlen(thing));
  58. sprintf(thing, "%04X",(uint16_t) cpu->r7);
  59. send_control(emu, UPDATE_REG_R7_PACKET, (void *)thing, strlen(thing));
  60. sprintf(thing, "%04X",(uint16_t) cpu->r8);
  61. send_control(emu, UPDATE_REG_R8_PACKET, (void *)thing, strlen(thing));
  62. sprintf(thing, "%04X",(uint16_t) cpu->r9);
  63. send_control(emu, UPDATE_REG_R9_PACKET, (void *)thing, strlen(thing));
  64. sprintf(thing, "%04X", (uint16_t)cpu->r10);
  65. send_control(emu, UPDATE_REG_R10_PACKET, (void *)thing, strlen(thing));
  66. sprintf(thing, "%04X", (uint16_t)cpu->r11);
  67. send_control(emu, UPDATE_REG_R11_PACKET, (void *)thing, strlen(thing));
  68. sprintf(thing, "%04X", (uint16_t)cpu->r12);
  69. send_control(emu, UPDATE_REG_R12_PACKET, (void *)thing, strlen(thing));
  70. sprintf(thing, "%04X", (uint16_t)cpu->r13);
  71. send_control(emu, UPDATE_REG_R13_PACKET, (void *)thing, strlen(thing));
  72. sprintf(thing, "%04X", (uint16_t)cpu->r14);
  73. send_control(emu, UPDATE_REG_R14_PACKET, (void *)thing, strlen(thing));
  74. sprintf(thing, "%04X", (uint16_t)cpu->r15);
  75. send_control(emu, UPDATE_REG_R15_PACKET, (void *)thing, strlen(thing));
  76. }
  77. //##########+++ Set SR struct Value +++##########
  78. void set_sr_value (Emulator *emu, uint16_t value)
  79. {
  80. Cpu *cpu = emu->cpu;
  81. // reset SR to set it properly...
  82. memset(&cpu->sr, 0, sizeof(Status_reg));
  83. //memcpy(&cpu->sr, &value, 16);
  84. if (value & 0x8000) cpu->sr.reserved |= 0x8000;
  85. if (value & 0x4000) cpu->sr.reserved |= 0x4000;
  86. if (value & 0x2000) cpu->sr.reserved |= 0x2000;
  87. if (value & 0x1000) cpu->sr.reserved |= 0x1000;
  88. if (value & 0x0800) cpu->sr.reserved |= 0x0800;
  89. if (value & 0x0400) cpu->sr.reserved |= 0x0400;
  90. if (value & 0x0200) cpu->sr.reserved |= 0x0200;
  91. cpu->sr.overflow = (value & 0x0100) ? 1 : 0;
  92. cpu->sr.SCG1 = (value & 0x0080) ? 1 : 0;
  93. cpu->sr.SCG0 = (value & 0x0040) ? 1 : 0;
  94. cpu->sr.OSCOFF = (value & 0x0020) ? 1 : 0;
  95. cpu->sr.CPUOFF = (value & 0x0010) ? 1 : 0;
  96. cpu->sr.GIE = (value & 0x0008) ? 1 : 0;
  97. cpu->sr.negative = (value & 0x0004) ? 1 : 0;
  98. cpu->sr.zero = (value & 0x0002) ? 1 : 0;
  99. cpu->sr.carry = (value & 0x0001) ? 1 : 0;
  100. }
  101. //##########+++ Return value from SR struct +++##########
  102. uint16_t sr_to_value(Emulator *emu)
  103. {
  104. Cpu *cpu = emu->cpu;
  105. uint16_t r2 = 0;
  106. // reserved bits not working quite right yet
  107. if (cpu->sr.reserved & 0b1000000) {
  108. r2 |= 0x8000;
  109. }
  110. if (cpu->sr.reserved & 0b0100000) {
  111. r2 |= 0x4000;
  112. }
  113. if (cpu->sr.reserved & 0b0010000) {
  114. r2 |= 0x2000;
  115. }
  116. if (cpu->sr.reserved & 0b0001000) {
  117. r2 |= 0x1000;
  118. }
  119. if (cpu->sr.reserved & 0b0000100) {
  120. r2 |= 0x0800;
  121. }
  122. if (cpu->sr.reserved & 0b0000010) {
  123. r2 |= 0x0400;
  124. }
  125. if (cpu->sr.reserved & 0b0000001) {
  126. r2 |= 0x0200;
  127. }
  128. if (cpu->sr.overflow) {
  129. r2 |= 0x0100;
  130. }
  131. if (cpu->sr.SCG1) {
  132. r2 |= 0x0080;
  133. }
  134. if (cpu->sr.SCG0) {
  135. r2 |= 0x0040;
  136. }
  137. if (cpu->sr.OSCOFF) {
  138. r2 |= 0x0020;
  139. }
  140. if (cpu->sr.CPUOFF) {
  141. r2 |= 0x0010;
  142. }
  143. if (cpu->sr.GIE) {
  144. r2 |= 0x0008;
  145. }
  146. if (cpu->sr.negative) {
  147. r2 |= 0x0004;
  148. }
  149. if (cpu->sr.zero) {
  150. r2 |= 0x0002;
  151. }
  152. if (cpu->sr.carry) {
  153. r2 |= 0x0001;
  154. }
  155. return r2;
  156. }
  157. void cpu_step(Emulator *emu) {
  158. Cpu *cpu = emu->cpu;
  159. if(!cpu->sr.CPUOFF) {
  160. // Instruction Decoder
  161. decode(emu, fetch(emu), EXECUTE);
  162. handle_bcm(emu);
  163. }
  164. // Handle Peripherals
  165. handle_timer_a(emu);
  166. handle_port_1(emu);
  167. handle_usci(emu);
  168. handle_interrupts(emu);
  169. }
  170. void cpu_reset(Emulator *emu) {
  171. Cpu *cpu = emu->cpu;
  172. cpu->pc = 0xC000;
  173. cpu->sp = 0x400;
  174. memset(&cpu->sr, 0, sizeof(Status_reg));
  175. cpu->cg2 = cpu->r4 = cpu->r5 = cpu->r6 = cpu->r7 =
  176. cpu->r8 = cpu->r9 = cpu->r10 = cpu->r11 = cpu->r12 =
  177. cpu->r13 = cpu->r14 = cpu->r15 = 0;
  178. cpu->interrupt = NULL_VECTOR;
  179. }