py_functions.c 8.6 KB

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