bcm.c 8.7 KB

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