py_functions.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "py_functions.h"
  2. #ifdef _MSC_VER
  3. #include <Windows.h>
  4. #else
  5. #include <time.h>
  6. #endif
  7. uint64_t getnano() {
  8. #ifdef _MSC_VER
  9. static LARGE_INTEGER frequency;
  10. if (frequency.QuadPart == 0) QueryPerformanceFrequency(&frequency);
  11. LARGE_INTEGER now;
  12. QueryPerformanceCounter(&now);
  13. double x = (double)now.QuadPart / (double)frequency.QuadPart;
  14. return (uint64_t)(x * 1000000000.0);
  15. #else
  16. struct timespec now;
  17. clock_gettime(CLOCK_MONOTONIC, &now);
  18. return now.tv_sec * 1000000000 + now.tv_nsec;
  19. #endif
  20. }
  21. // This is an ugly solution but heh
  22. Emulator *emuInst = NULL;
  23. void play_emu() {
  24. emuInst->cpu->running = true;
  25. emuInst->debugger->debug_mode = false;
  26. }
  27. void pause_emu() {
  28. if (emuInst->cpu->running) {
  29. emuInst->cpu->running = false;
  30. emuInst->debugger->debug_mode = true;
  31. // display first round of registers
  32. // display_registers(emu);
  33. // disassemble(emu, cpu->pc, 1);
  34. // update_register_display(emu);
  35. }
  36. }
  37. void reset_emu() {
  38. if(emuInst == NULL) return;
  39. cpu_reset(emuInst);
  40. print_console(emuInst, "CPU Reset\n");
  41. }
  42. void set_reg(uint8_t reg_type, uint8_t value) {
  43. if(emuInst == NULL) return;
  44. Port_1 *p = emuInst->cpu->p1;
  45. switch(reg_type) {
  46. case SET_REG_P1_EN:
  47. p->EXT_EN = value;
  48. break;
  49. case SET_REG_P1_DIR:
  50. p->EXT_DIR = value;
  51. break;
  52. }
  53. }
  54. PyObject *get_port1_regs() {
  55. if(emuInst == NULL) return Py_None;
  56. char regs[9];
  57. Port_1 *p = emuInst->cpu->p1;
  58. regs[0] = *p->_OUT;
  59. regs[1] = *p->_DIR;
  60. regs[2] = *p->_IFG;
  61. regs[3] = *p->_IES;
  62. regs[4] = *p->_IE;
  63. regs[5] = *p->_SEL;
  64. regs[6] = *p->_SEL2;
  65. regs[7] = *p->_REN;
  66. regs[8] = *p->_IN;
  67. return PyBytes_FromStringAndSize(regs, 9);
  68. }
  69. PyObject *get_cpu_regs() {
  70. if(emuInst == NULL) return Py_None;
  71. Cpu *cpu = emuInst->cpu;
  72. uint16_t regs[] = {
  73. cpu->pc, cpu->sp, sr_to_value(emuInst), cpu->cg2,
  74. cpu->r4, cpu->r5, cpu->r6, cpu->r7,
  75. cpu->r8, cpu->r9, cpu->r10, cpu->r11,
  76. cpu->r12, cpu->r13, cpu->r14, cpu->r15
  77. };
  78. return PyBytes_FromStringAndSize((char *)regs, sizeof(regs));
  79. }
  80. PyObject *get_bcm_regs() {
  81. if(emuInst == NULL) return Py_None;
  82. char regs[6];
  83. Bcm *bcm = emuInst->cpu->bcm;
  84. regs[0] = *bcm->DCOCTL;
  85. regs[1] = *bcm->BCSCTL1;
  86. regs[2] = *bcm->BCSCTL2;
  87. regs[3] = *bcm->BCSCTL3;
  88. regs[4] = *bcm->IE1;
  89. regs[5] = *bcm->IFG1;
  90. return PyBytes_FromStringAndSize(regs, 6);
  91. }
  92. PyObject *get_timer_regs() {
  93. if(emuInst == NULL) return Py_None;
  94. char regs[18];
  95. Timer_a *timer = emuInst->cpu->timer_a;
  96. regs[0] = *timer->TA0CTL;
  97. regs[1] = *timer->TA0R;
  98. regs[2] = *timer->TA0CCTL0;
  99. regs[3] = *timer->TA0CCR0;
  100. regs[4] = *timer->TA0CCTL1;
  101. regs[5] = *timer->TA0CCR1;
  102. regs[6] = *timer->TA0CCTL2;
  103. regs[7] = *timer->TA0CCR2;
  104. regs[8] = *timer->TA0IV;
  105. regs[9] = *timer->TA1CTL;
  106. regs[10] = *timer->TA1R;
  107. regs[11] = *timer->TA1CCTL0;
  108. regs[12] = *timer->TA1CCR0;
  109. regs[13] = *timer->TA1CCTL1;
  110. regs[14] = *timer->TA1CCR1;
  111. regs[15] = *timer->TA1CCTL2;
  112. regs[16] = *timer->TA1CCR2;
  113. regs[17] = *timer->TA1IV;
  114. return PyBytes_FromStringAndSize(regs, 18);
  115. }
  116. PyObject *get_usci_regs() {
  117. if(emuInst == NULL) return Py_None;
  118. char regs[12];
  119. Usci *usci = emuInst->cpu->usci;
  120. regs[0] = *usci->UCA0CTL0;
  121. regs[1] = *usci->UCA0CTL1;
  122. regs[2] = *usci->UCA0BR0;
  123. regs[3] = *usci->UCA0BR1;
  124. regs[4] = *usci->UCA0MCTL;
  125. regs[5] = *usci->UCA0STAT;
  126. regs[6] = *usci->UCA0RXBUF;
  127. regs[7] = *usci->UCA0TXBUF;
  128. regs[8] = *usci->UCA0ABCTL;
  129. regs[9] = *usci->UCA0IRTCTL;
  130. regs[10] = *usci->UCA0IRRCTL;
  131. regs[11] = *usci->IFG2;
  132. return PyBytes_FromStringAndSize(regs, 12);
  133. }
  134. void cmd_emu(char *line, int len) {
  135. if(emuInst == NULL) return;
  136. if (!emuInst->cpu->running && emuInst->debugger->debug_mode) {
  137. exec_cmd(emuInst, line, len);
  138. // update_register_display(emu);
  139. }
  140. }
  141. void stop_emu() {
  142. if(emuInst == NULL) return;
  143. emuInst->debugger->quit = true;
  144. print_console(emuInst, "Stopping emulator..\n");
  145. }
  146. void write_serial(uint8_t *data, int len) {
  147. if(emuInst == NULL) return;
  148. Usci *usci = emuInst->cpu->usci;
  149. // int i = 0;
  150. // uint8_t *bytes = data;
  151. printf("len is %d\n", len);
  152. for(int i=0; i < len; i++) {
  153. usleep(333);
  154. printf("waiting.. ");
  155. while (*usci->IFG2 & RXIFG) {
  156. usleep(333);
  157. if(emuInst->debugger->quit) {
  158. puts("debugger stopped");
  159. return;
  160. }
  161. }
  162. // uint8_t thing = *(bytes);
  163. *usci->UCA0RXBUF = data[i];
  164. *usci->IFG2 |= RXIFG;
  165. printf("0x%04X in UCA0RXBUF\n", (uint8_t)*usci->UCA0RXBUF);
  166. printf("waiting.. ");
  167. while (*usci->IFG2 & RXIFG) {
  168. usleep(333);
  169. if(emuInst->debugger->quit) {
  170. puts("debugger stopped");
  171. return;
  172. }
  173. }
  174. puts("done\n");
  175. }
  176. // while (true) {
  177. // usleep(333);
  178. // while (*usci->IFG2 & RXIFG);
  179. // uint8_t thing = *(bytes);
  180. //
  181. //// if (thing == '\n') {
  182. //// thing = '\r';
  183. //// }
  184. // *usci->UCA0RXBUF = thing;
  185. // *usci->IFG2 |= RXIFG;
  186. //
  187. // //printf("\n0x%04X in UCA0RXBUF\n", (uint8_t)*usci->UCA0RXBUF);
  188. // //puts("waiting..");
  189. // while (*usci->IFG2 & RXIFG);
  190. // //puts("done");
  191. // //*usci->IFG2 |= RXIFG;
  192. // if (*usci->UCA0RXBUF == '\r' || *usci->UCA0RXBUF == '\n') break;
  193. // ++bytes;
  194. // }
  195. // return NULL;
  196. // }
  197. }
  198. void start_emu(char *file) {
  199. emuInst = (Emulator *) calloc( 1, sizeof(Emulator) );
  200. Cpu *cpu = NULL; Debugger *deb = NULL;
  201. emuInst->cpu = (Cpu *) calloc(1, sizeof(Cpu));
  202. emuInst->cpu->bcm = (Bcm *) calloc(1, sizeof(Bcm));
  203. emuInst->cpu->timer_a = (Timer_a *) calloc(1, sizeof(Timer_a));
  204. emuInst->cpu->p1 = (Port_1 *) calloc(1, sizeof(Port_1));
  205. emuInst->cpu->usci = (Usci *) calloc(1, sizeof(Usci));
  206. emuInst->debugger = (Debugger *) calloc(1, sizeof(Debugger));
  207. setup_debugger(emuInst);
  208. cpu = emuInst->cpu;
  209. deb = emuInst->debugger;
  210. // deb->server = (Server *) calloc(1, sizeof(Server));
  211. initialize_msp_memspace();
  212. initialize_msp_registers(emuInst);
  213. setup_bcm(emuInst);
  214. setup_timer_a(emuInst);
  215. setup_port_1(emuInst);
  216. setup_usci(emuInst);
  217. print_console(emuInst, "[MSP430 Emulator]\n Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)\n");
  218. load_bootloader(0x0C00);
  219. if(load_firmware(emuInst, file, 0xC000) == 0) {
  220. // display_registers(emuInst);
  221. disassemble(emuInst, cpu->pc, 1);
  222. // update_register_display(emuInst);
  223. uint16_t counter = 0;
  224. uint64_t time_delta = 0;
  225. uint64_t time_last = getnano();
  226. while (!deb->quit) {
  227. // Handle debugger when CPU is not running
  228. if (!cpu->running) {
  229. counter = 0;
  230. time_delta = 0;
  231. time_last = getnano();
  232. usleep(10000);
  233. continue;
  234. }
  235. // Handle Breakpoints
  236. //handle_breakpoints(emuInst);
  237. cpu_step(emuInst);
  238. counter++;
  239. if(counter > 500) {
  240. uint64_t time_now = getnano();
  241. // Average of 4 cycles per instruction (actually around 4.88)
  242. uint64_t cycles_time = (uint64_t)(mclk_clock_nstime(emuInst) * 4.883 * 500);
  243. uint64_t delta = time_now - time_last;
  244. if(time_last > time_now) delta = 0;
  245. uint64_t sleep_time = cycles_time - delta;
  246. if(delta > cycles_time) {
  247. time_delta += (delta - cycles_time);
  248. } else if(time_delta > sleep_time) {
  249. time_delta -= sleep_time;
  250. } else {
  251. sleep_time += time_delta;
  252. time_delta = 0;
  253. usleep(sleep_time/1000);
  254. }
  255. time_last = time_now;
  256. counter = 0;
  257. }
  258. }
  259. }
  260. uninitialize_msp_memspace();
  261. free(cpu->timer_a);
  262. free(cpu->bcm);
  263. free(cpu->p1);
  264. free(cpu->usci);
  265. free(cpu);
  266. free(deb->server);
  267. free(deb);
  268. free(emuInst);
  269. return;
  270. }
  271. //void init_packet_queue (Emulator *emu){
  272. // Server *s = emu->debugger->server;
  273. // s->pending_packets_head = NULL;
  274. // s->pending_packets_tail = NULL;
  275. // s->packets_queued = 0;
  276. // s->spin_lock = false;
  277. //}
  278. //void *emulator (void *ctxt) {
  279. // emu = (Emulator *) ctxt;
  280. // Debugger *deb = emu->debugger;
  281. //
  282. // init_packet_queue(emu);
  283. // printf("starting emulator...\n");
  284. //}