py_functions.c 8.4 KB

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