bcm.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "bcm.h"
  16. void handle_bcm (Emulator *emu)
  17. {
  18. Cpu *cpu = emu->cpu;
  19. Bcm *bcm = cpu->bcm;
  20. uint8_t DCOCTL = *bcm->DCOCTL;
  21. uint8_t BCSCTL1 = *bcm->BCSCTL1;
  22. uint8_t BCSCTL2 = *bcm->BCSCTL2;
  23. uint8_t BCSCTL3 = *bcm->BCSCTL3;
  24. // HANDLE MCLK -------------------
  25. uint8_t SELMx = BCSCTL2 >> 6;
  26. uint8_t DIVMx = (BCSCTL2 >> 4) & 0x03;
  27. if (SELMx == 0b00 || SELMx == 0b01) { // source = DCOCLK
  28. bcm->mclk_source = DCOCLK;
  29. bcm->mclk_freq = (bcm->dco_freq*1.0) / bcm->mclk_div;
  30. }
  31. else if (SELMx == 0b10) { // XT2CLK
  32. bcm->mclk_source = XT2CLK;
  33. }
  34. else if (SELMx == 0b11) { // VLOCLK
  35. bcm->mclk_source = VLOCLK;
  36. }
  37. switch (DIVMx) {
  38. case 0b00: bcm->mclk_div = 1; break;
  39. case 0b01: bcm->mclk_div = 2; break;
  40. case 0b10: bcm->mclk_div = 4; break;
  41. case 0b11: bcm->mclk_div = 8; break;
  42. default: break;
  43. }
  44. // HANDLE SMCLK -------------------
  45. uint8_t SELS = (BCSCTL2 >> 3) & 0x01;
  46. uint8_t DIVSx = (BCSCTL2 >> 1) & 0x03;
  47. // HANDLE ACLK -------------------
  48. uint8_t DIVAx = (BCSCTL1 >> 4) & 0x03;
  49. // HANDLE LOW POWER MODES --------
  50. // Active Mode (CPU is active, all enabled clocks are active)
  51. if (!cpu->sr.SCG1 && !cpu->sr.SCG0 && !cpu->sr.OSCOFF && !cpu->sr.CPUOFF) {
  52. }
  53. // LPM0 (CPU, MCLK are disabled, SMCLK, ACLK are active)
  54. else if (!cpu->sr.SCG1 && !cpu->sr.SCG0 && !cpu->sr.OSCOFF && cpu->sr.CPUOFF){
  55. }
  56. /* LPM1 (CPU, MCLK are disabled. DCO and DC generator are
  57. disabled if the DCO is not used for SMCLK. ACLK is
  58. active.)
  59. */
  60. else if (!cpu->sr.SCG1 && cpu->sr.SCG0 && !cpu->sr.OSCOFF && cpu->sr.CPUOFF){
  61. }
  62. /* LPM2 (CPU, MCLK, SMCLK, DCO are disabled. DC generator remains enabled.
  63. ACLK is active.) */
  64. else if (cpu->sr.SCG1 && !cpu->sr.SCG0 && !cpu->sr.OSCOFF && cpu->sr.CPUOFF){
  65. }
  66. // LPM3 (CPU, MCLK, SMCLK, DCO are disabled. DC generatordisabled.ACLK active.
  67. else if (cpu->sr.SCG1 && cpu->sr.SCG0 && !cpu->sr.OSCOFF && cpu->sr.CPUOFF){
  68. }
  69. // LPM4 (CPU and all clocks are disabled)
  70. else if (cpu->sr.SCG1 && cpu->sr.SCG0 && cpu->sr.OSCOFF && cpu->sr.CPUOFF){
  71. }
  72. // HANDLE DCO --------------------
  73. uint8_t DCOx = DCOCTL >> 5;
  74. uint8_t MODx = DCOCTL & 0x1F;
  75. uint8_t RSELx = BCSCTL1 & 0x0F;
  76. // Default state of BCM after reset ~1.03 MHz
  77. if (DCOx == 0b011 && RSELx == 0b0111) {
  78. bcm->dco_freq = 1030000;
  79. bcm->dco_period = 971;
  80. bcm->dco_pulse_width = 485;
  81. }
  82. // 16 Mhz
  83. else if (DCOx == 0b100 && RSELx == 0b1111) {
  84. bcm->dco_freq = 16000000;
  85. bcm->dco_period = 63;
  86. bcm->dco_pulse_width = 31;
  87. }
  88. // 12 MHz
  89. else if (DCOx == 0b100 && RSELx == 0b1110) {
  90. bcm->dco_freq = 12000000;
  91. bcm->dco_period = 83;
  92. bcm->dco_pulse_width = 42;
  93. }
  94. // 8 Mhz
  95. else if (DCOx == 0b100 && RSELx == 0b1101) {
  96. bcm->dco_freq = 8000000;
  97. bcm->dco_period = 125;
  98. bcm->dco_pulse_width = 62;
  99. }
  100. // 1 MHz
  101. else if (DCOx == 0b110 && RSELx == 0b0110) {
  102. bcm->dco_freq = 1000000;
  103. bcm->dco_period = 1000;
  104. bcm->dco_pulse_width = 500;
  105. }
  106. // HANDLE LFXT1CLK -------------------
  107. uint8_t XTS = (BCSCTL1 >> 6) & 0x01; // LFXT1CLK select (high/low)
  108. }
  109. void setup_bcm (Emulator *emu)
  110. {
  111. Cpu *cpu = emu->cpu;
  112. Bcm *bcm = cpu->bcm;
  113. static const uint16_t DCOCTL_VLOC = 0x56;
  114. static const uint16_t BCSCTL1_VLOC = 0x57;
  115. static const uint16_t BCSCTL2_VLOC = 0x58;
  116. static const uint16_t BCSCTL3_VLOC = 0x53;
  117. static const uint16_t IE1_VLOC = 0x0;
  118. static const uint16_t IFG1_VLOC = 0x2;
  119. *(bcm->DCOCTL = (uint8_t *) get_addr_ptr(DCOCTL_VLOC)) = 0x60;
  120. *(bcm->BCSCTL1 = (uint8_t *) get_addr_ptr(BCSCTL1_VLOC)) = 0x87;
  121. *(bcm->BCSCTL2 = (uint8_t *) get_addr_ptr(BCSCTL2_VLOC)) = 0;
  122. *(bcm->BCSCTL3 = (uint8_t *) get_addr_ptr(BCSCTL3_VLOC)) = 0x5;
  123. *(bcm->IE1 = (uint8_t *) get_addr_ptr(IE1_VLOC)) = 0;
  124. *(bcm->IFG1 = (uint8_t *) get_addr_ptr(IFG1_VLOC)) = 0;
  125. // 1.03 MHz
  126. bcm->dco_freq = 1030000;
  127. bcm->dco_period = 971;
  128. bcm->dco_pulse_width = 970 / 2;
  129. }
  130. uint64_t nanosec_diff(struct timespec *timeA_p, struct timespec *timeB_p)
  131. {
  132. return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) - ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
  133. }
  134. void mclk_wait_cycles (Emulator *emu, uint64_t cycles)
  135. {
  136. Cpu *cpu = emu->cpu;
  137. Bcm *bcm = cpu->bcm;
  138. struct timespec start, end;
  139. uint64_t i, elapsed_nsecs;
  140. for (i = 0;i < cycles;i++)
  141. {
  142. clock_gettime(CLOCK_MONOTONIC, &start);
  143. while (true)
  144. {
  145. clock_gettime(CLOCK_MONOTONIC, &end);
  146. elapsed_nsecs = nanosec_diff(&end, &start);
  147. // Choose timing based on clock source
  148. if (bcm->mclk_source == DCOCLK)
  149. {
  150. double thing = (1.0/(bcm->dco_freq/bcm->mclk_div))*1000000000.0;
  151. if (elapsed_nsecs >= (uint64_t)thing)
  152. break;
  153. }
  154. else
  155. {
  156. puts("Error, clock source");
  157. }
  158. }
  159. }
  160. }
  161. void smclk_wait_cycles (Emulator *emu, uint64_t cycles)
  162. {
  163. Cpu *cpu = emu->cpu;
  164. Bcm *bcm = cpu->bcm;
  165. struct timespec start, end;
  166. uint64_t i, elapsed_nsecs;
  167. for (i = 0;i < cycles;i++) {
  168. clock_gettime(CLOCK_MONOTONIC, &start);
  169. while (true) {
  170. clock_gettime(CLOCK_MONOTONIC, &end);
  171. elapsed_nsecs = nanosec_diff(&end, &start);
  172. // Choose timing based on clock source
  173. if (bcm->mclk_source == DCOCLK) {
  174. //printf("div: %llu\n",
  175. //(long long unsigned)(1/(bcm->dco_freq/bcm->mclk_div)));
  176. double thing = (1.0/(bcm->dco_freq/bcm->mclk_div))*1000000000.0;
  177. if (elapsed_nsecs >= (uint64_t)thing) {
  178. break;
  179. }
  180. }
  181. else {
  182. puts("Error, clock source");
  183. }
  184. }
  185. }
  186. }
  187. /*
  188. /*
  189. // Start Sources DCO, etc
  190. pthread_t pp;
  191. if ( pthread_create(&pp, NULL, DCO_source, (void *)emu ) ) {
  192. printf("Error creating DCO thread\n");
  193. exit(1);
  194. }
  195. void *DCO_source (void *data)
  196. {
  197. Emulator *emu = (Emulator *)data;
  198. Bcm *bcm = emu->cpu->bcm;
  199. printf("In source thread...\n");
  200. struct timespec start, end;
  201. uint64_t elapsed_nsecs;
  202. uint64_t trimmer = 0;
  203. while (true) {
  204. clock_gettime(CLOCK_MONOTONIC, &start);
  205. while (true) {
  206. clock_gettime(CLOCK_MONOTONIC, &end);
  207. elapsed_nsecs = nanosec_diff(&end, &start);
  208. if (elapsed_nsecs >= bcm->dco_period) break;
  209. }
  210. }
  211. /*
  212. while (true) {
  213. clock_gettime(CLOCK_MONOTONIC, &start);
  214. bcm->dco_high = true;
  215. while (true) {
  216. clock_gettime(CLOCK_MONOTONIC, &end);
  217. elapsed_nsecs = nanosec_diff(&end, &start);
  218. if (elapsed_nsecs >= bcm->dco_pulse_width) {
  219. bcm->dco_high = false;
  220. }
  221. if (elapsed_nsecs >= bcm->dco_period) break;
  222. }
  223. }
  224. return NULL;
  225. }
  226. */