py_functions.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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_misc_data() {
  55. if(emuInst == NULL) return Py_None;
  56. Cpu *cpu = emuInst->cpu;
  57. Bcm *bcm = cpu->bcm;
  58. PyObject *dict = PyDict_New();
  59. PyDict_SetItemString(dict, "period", PyLong_FromUnsignedLongLong(cpu->nsecs));
  60. PyDict_SetItemString(dict, "mclk", PyLong_FromUnsignedLongLong(bcm->mclk_freq));
  61. PyObject *mck_src_str;
  62. switch(bcm->mclk_source) {
  63. case DCOCLK: {mck_src_str = PyUnicode_FromString("DCOCLK"); break;}
  64. case XT2CLK: {mck_src_str = PyUnicode_FromString("XT2CLK"); break;}
  65. case VLOCLK: {mck_src_str = PyUnicode_FromString("VLOCLK"); break;}
  66. default: {mck_src_str = PyUnicode_FromString("?"); break;}
  67. // TACLK, ACLK, SMCLK, MCLK, INCLK, NUM_CLOCKS
  68. }
  69. PyDict_SetItemString(dict, "mclk_src", mck_src_str);
  70. PyDict_SetItemString(dict, "uart_baud", PyLong_FromUnsignedLong(cpu->usci->UART_baud));
  71. return dict;
  72. }
  73. PyObject *get_port1_regs() {
  74. if(emuInst == NULL) return Py_None;
  75. char regs[9];
  76. Port_1 *p = emuInst->cpu->p1;
  77. regs[0] = *p->_OUT;
  78. regs[1] = *p->_DIR;
  79. regs[2] = *p->_IFG;
  80. regs[3] = *p->_IES;
  81. regs[4] = *p->_IE;
  82. regs[5] = *p->_SEL;
  83. regs[6] = *p->_SEL2;
  84. regs[7] = *p->_REN;
  85. regs[8] = *p->_IN;
  86. return PyBytes_FromStringAndSize(regs, 9);
  87. }
  88. PyObject *get_cpu_regs() {
  89. if(emuInst == NULL) return Py_None;
  90. Cpu *cpu = emuInst->cpu;
  91. uint16_t regs[] = {
  92. cpu->pc, cpu->sp, sr_to_value(emuInst), cpu->cg2,
  93. cpu->r4, cpu->r5, cpu->r6, cpu->r7,
  94. cpu->r8, cpu->r9, cpu->r10, cpu->r11,
  95. cpu->r12, cpu->r13, cpu->r14, cpu->r15
  96. };
  97. return PyBytes_FromStringAndSize((char *)regs, sizeof(regs));
  98. }
  99. PyObject *get_bcm_regs() {
  100. if(emuInst == NULL) return Py_None;
  101. char regs[6];
  102. Bcm *bcm = emuInst->cpu->bcm;
  103. regs[0] = *bcm->DCOCTL;
  104. regs[1] = *bcm->BCSCTL1;
  105. regs[2] = *bcm->BCSCTL2;
  106. regs[3] = *bcm->BCSCTL3;
  107. regs[4] = *bcm->IE1;
  108. regs[5] = *bcm->IFG1;
  109. return PyBytes_FromStringAndSize(regs, 6);
  110. }
  111. PyObject *get_timer_regs() {
  112. if(emuInst == NULL) return Py_None;
  113. char regs[18];
  114. Timer_a *timer = emuInst->cpu->timer_a;
  115. regs[0] = *timer->TA0CTL;
  116. regs[1] = *timer->TA0R;
  117. regs[2] = *timer->TA0CCTL0;
  118. regs[3] = *timer->TA0CCR0;
  119. regs[4] = *timer->TA0CCTL1;
  120. regs[5] = *timer->TA0CCR1;
  121. regs[6] = *timer->TA0CCTL2;
  122. regs[7] = *timer->TA0CCR2;
  123. regs[8] = *timer->TA0IV;
  124. regs[9] = *timer->TA1CTL;
  125. regs[10] = *timer->TA1R;
  126. regs[11] = *timer->TA1CCTL0;
  127. regs[12] = *timer->TA1CCR0;
  128. regs[13] = *timer->TA1CCTL1;
  129. regs[14] = *timer->TA1CCR1;
  130. regs[15] = *timer->TA1CCTL2;
  131. regs[16] = *timer->TA1CCR2;
  132. regs[17] = *timer->TA1IV;
  133. return PyBytes_FromStringAndSize(regs, 18);
  134. }
  135. PyObject *get_usci_regs() {
  136. if(emuInst == NULL) return Py_None;
  137. char regs[12];
  138. Usci *usci = emuInst->cpu->usci;
  139. regs[0] = *usci->UCA0CTL0;
  140. regs[1] = *usci->UCA0CTL1;
  141. regs[2] = *usci->UCA0BR0;
  142. regs[3] = *usci->UCA0BR1;
  143. regs[4] = *usci->UCA0MCTL;
  144. regs[5] = *usci->UCA0STAT;
  145. regs[6] = *usci->UCA0RXBUF;
  146. regs[7] = *usci->UCA0TXBUF;
  147. regs[8] = *usci->UCA0ABCTL;
  148. regs[9] = *usci->UCA0IRTCTL;
  149. regs[10] = *usci->UCA0IRRCTL;
  150. regs[11] = *usci->IFG2;
  151. return PyBytes_FromStringAndSize(regs, 12);
  152. }
  153. void cmd_emu(char *line, int len) {
  154. if(emuInst == NULL) return;
  155. if (!emuInst->cpu->running && emuInst->debugger->debug_mode) {
  156. exec_cmd(emuInst, line, len);
  157. // update_register_display(emu);
  158. }
  159. }
  160. void stop_emu() {
  161. if(emuInst == NULL) return;
  162. emuInst->debugger->quit = true;
  163. print_console(emuInst, "Stopping emulator..\n");
  164. }
  165. void write_serial(uint8_t *data, int len) {
  166. if(emuInst == NULL) return;
  167. set_uart_buf(emuInst, data, len);
  168. //// int i = 0;
  169. //// uint8_t *bytes = data;
  170. // *usci->UCA0RXBUF = data[0];
  171. // *usci->IFG2 |= RXIFG;
  172. // puts("Setting interrupt");
  173. // service_interrupt(emuInst, USCIAB0RX_VECTOR);
  174. // printf("len is %d\n", len);
  175. // for(int i=0; i < len; i++) {
  176. // usleep(333);
  177. // printf("waiting.. ");
  178. // while (*usci->IFG2 & RXIFG) {
  179. // usleep(333);
  180. // if(emuInst->debugger->quit) {
  181. // puts("debugger stopped");
  182. // return;
  183. // }
  184. // }
  185. //// uint8_t thing = *(bytes);
  186. // *usci->UCA0RXBUF = data[i];
  187. // *usci->IFG2 |= RXIFG;
  188. // printf("0x%04X in UCA0RXBUF\n", (uint8_t)*usci->UCA0RXBUF);
  189. // printf("waiting.. ");
  190. // while (*usci->IFG2 & RXIFG) {
  191. // usleep(333);
  192. // if(emuInst->debugger->quit) {
  193. // puts("debugger stopped");
  194. // return;
  195. // }
  196. // }
  197. // puts("done\n");
  198. // }
  199. // while (true) {
  200. // usleep(333);
  201. // while (*usci->IFG2 & RXIFG);
  202. // uint8_t thing = *(bytes);
  203. //
  204. //// if (thing == '\n') {
  205. //// thing = '\r';
  206. //// }
  207. // *usci->UCA0RXBUF = thing;
  208. // *usci->IFG2 |= RXIFG;
  209. //
  210. // //printf("\n0x%04X in UCA0RXBUF\n", (uint8_t)*usci->UCA0RXBUF);
  211. // //puts("waiting..");
  212. // while (*usci->IFG2 & RXIFG);
  213. // //puts("done");
  214. // //*usci->IFG2 |= RXIFG;
  215. // if (*usci->UCA0RXBUF == '\r' || *usci->UCA0RXBUF == '\n') break;
  216. // ++bytes;
  217. // }
  218. }
  219. void start_emu(char *file) {
  220. emuInst = (Emulator *) calloc( 1, sizeof(Emulator) );
  221. Cpu *cpu = NULL; Debugger *deb = NULL;
  222. emuInst->cpu = (Cpu *) calloc(1, sizeof(Cpu));
  223. emuInst->cpu->bcm = (Bcm *) calloc(1, sizeof(Bcm));
  224. emuInst->cpu->timer_a = (Timer_a *) calloc(1, sizeof(Timer_a));
  225. emuInst->cpu->p1 = (Port_1 *) calloc(1, sizeof(Port_1));
  226. emuInst->cpu->usci = (Usci *) calloc(1, sizeof(Usci));
  227. emuInst->debugger = (Debugger *) calloc(1, sizeof(Debugger));
  228. setup_debugger(emuInst);
  229. cpu = emuInst->cpu;
  230. deb = emuInst->debugger;
  231. // deb->server = (Server *) calloc(1, sizeof(Server));
  232. initialize_msp_memspace();
  233. initialize_msp_registers(emuInst);
  234. setup_bcm(emuInst);
  235. setup_timer_a(emuInst);
  236. setup_port_1(emuInst);
  237. setup_usci(emuInst);
  238. print_console(emuInst, "[MSP430 Emulator]\n Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)\n");
  239. load_bootloader(0x0C00);
  240. if(load_firmware(emuInst, file, 0xC000) == 0) {
  241. // display_registers(emuInst);
  242. disassemble(emuInst, cpu->pc, 1);
  243. // update_register_display(emuInst);
  244. uint16_t counter = 0;
  245. uint64_t time_delta = 0;
  246. uint64_t time_last = getnano();
  247. while (!deb->quit) {
  248. // Handle debugger when CPU is not running
  249. if (!cpu->running) {
  250. counter = 0;
  251. time_delta = 0;
  252. time_last = getnano();
  253. usleep(10000);
  254. continue;
  255. }
  256. // Handle Breakpoints
  257. //handle_breakpoints(emuInst);
  258. cpu_step(emuInst);
  259. counter++;
  260. if(counter > 500) {
  261. uint64_t time_now = getnano();
  262. if(cpu->bcm->mclk_period == 0) break;
  263. // Average of 4 cycles per instruction
  264. uint64_t cycles_time = (uint64_t)(cpu->bcm->mclk_period * 4.883 * 500);
  265. uint64_t delta = time_now - time_last;
  266. if(time_last > time_now) delta = 0;
  267. uint64_t sleep_time = cycles_time - delta;
  268. if(delta > cycles_time) {
  269. time_delta += (delta - cycles_time);
  270. } else if(time_delta > sleep_time) {
  271. time_delta -= sleep_time;
  272. } else {
  273. sleep_time += time_delta;
  274. time_delta = 0;
  275. usleep(sleep_time/1000);
  276. }
  277. time_last = time_now;
  278. counter = 0;
  279. }
  280. }
  281. }
  282. uninitialize_msp_memspace();
  283. free(cpu->usci->UART_buf_data);
  284. free(cpu->timer_a);
  285. free(cpu->bcm);
  286. free(cpu->p1);
  287. free(cpu->usci);
  288. free(cpu);
  289. free(deb->server);
  290. free(deb);
  291. free(emuInst);
  292. return;
  293. }
  294. //void init_packet_queue (Emulator *emu){
  295. // Server *s = emu->debugger->server;
  296. // s->pending_packets_head = NULL;
  297. // s->pending_packets_tail = NULL;
  298. // s->packets_queued = 0;
  299. // s->spin_lock = false;
  300. //}
  301. //void *emulator (void *ctxt) {
  302. // emu = (Emulator *) ctxt;
  303. // Debugger *deb = emu->debugger;
  304. //
  305. // init_packet_queue(emu);
  306. // printf("starting emulator...\n");
  307. //}