py_functions.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "py_functions.h"
  2. // This is an ugly solution but heh
  3. Emulator *emuInst = NULL;
  4. void play_emu() {
  5. emuInst->cpu->running = true;
  6. emuInst->debugger->debug_mode = false;
  7. }
  8. void pause_emu() {
  9. if (emuInst->cpu->running) {
  10. emuInst->cpu->running = false;
  11. emuInst->debugger->debug_mode = true;
  12. // display first round of registers
  13. // display_registers(emu);
  14. // disassemble(emu, cpu->pc, 1);
  15. // update_register_display(emu);
  16. }
  17. }
  18. void reset_emu() {
  19. if(emuInst == NULL) return;
  20. emuInst->cpu->pc = 0xC000;
  21. print_console(emuInst, "Resetting program counter to 0xC000\n");
  22. }
  23. void set_reg(uint8_t reg_type, uint8_t value) {
  24. if(emuInst == NULL) return;
  25. switch(reg_type) {
  26. case SET_REG_P1_IN:
  27. *emuInst->cpu->p1->_IN = value;
  28. }
  29. }
  30. PyObject *get_port1_regs() {
  31. if(emuInst == NULL) return Py_None;
  32. char regs[9];
  33. Port_1 *p = emuInst->cpu->p1;
  34. regs[0] = *p->_OUT;
  35. regs[1] = *p->_DIR;
  36. regs[2] = *p->_IFG;
  37. regs[3] = *p->_IES;
  38. regs[4] = *p->_IE;
  39. regs[5] = *p->_SEL;
  40. regs[6] = *p->_SEL2;
  41. regs[7] = *p->_REN;
  42. regs[8] = *p->_IN;
  43. return PyBytes_FromStringAndSize(regs, 9);
  44. }
  45. PyObject *get_bcm_regs() {
  46. if(emuInst == NULL) return Py_None;
  47. char regs[6];
  48. Bcm *bcm = emuInst->cpu->bcm;
  49. regs[0] = *bcm->DCOCTL;
  50. regs[1] = *bcm->BCSCTL1;
  51. regs[2] = *bcm->BCSCTL2;
  52. regs[3] = *bcm->BCSCTL3;
  53. regs[4] = *bcm->IE1;
  54. regs[5] = *bcm->IFG1;
  55. return PyBytes_FromStringAndSize(regs, 6);
  56. }
  57. PyObject *get_timer_regs() {
  58. if(emuInst == NULL) return Py_None;
  59. char regs[18];
  60. Timer_a *timer = emuInst->cpu->timer_a;
  61. regs[0] = *timer->TA0CTL;
  62. regs[1] = *timer->TA0R;
  63. regs[2] = *timer->TA0CCTL0;
  64. regs[3] = *timer->TA0CCR0;
  65. regs[4] = *timer->TA0CCTL1;
  66. regs[5] = *timer->TA0CCR1;
  67. regs[6] = *timer->TA0CCTL2;
  68. regs[7] = *timer->TA0CCR2;
  69. regs[8] = *timer->TA0IV;
  70. regs[9] = *timer->TA1CTL;
  71. regs[10] = *timer->TA1R;
  72. regs[11] = *timer->TA1CCTL0;
  73. regs[12] = *timer->TA1CCR0;
  74. regs[13] = *timer->TA1CCTL1;
  75. regs[14] = *timer->TA1CCR1;
  76. regs[15] = *timer->TA1CCTL2;
  77. regs[16] = *timer->TA1CCR2;
  78. regs[17] = *timer->TA1IV;
  79. return PyBytes_FromStringAndSize(regs, 18);
  80. }
  81. PyObject *get_usci_regs() {
  82. if(emuInst == NULL) return Py_None;
  83. char regs[12];
  84. Usci *usci = emuInst->cpu->usci;
  85. regs[0] = *usci->UCA0CTL0;
  86. regs[1] = *usci->UCA0CTL1;
  87. regs[2] = *usci->UCA0BR0;
  88. regs[3] = *usci->UCA0BR1;
  89. regs[4] = *usci->UCA0MCTL;
  90. regs[5] = *usci->UCA0STAT;
  91. regs[6] = *usci->UCA0RXBUF;
  92. regs[7] = *usci->UCA0TXBUF;
  93. regs[8] = *usci->UCA0ABCTL;
  94. regs[9] = *usci->UCA0IRTCTL;
  95. regs[10] = *usci->UCA0IRRCTL;
  96. regs[11] = *usci->IFG2;
  97. return PyBytes_FromStringAndSize(regs, 12);
  98. }
  99. void cmd_emu(char *line, int len) {
  100. if(emuInst == NULL) return;
  101. if (!emuInst->cpu->running && emuInst->debugger->debug_mode) {
  102. exec_cmd(emuInst, line, len);
  103. // update_register_display(emu);
  104. }
  105. }
  106. void stop_emu() {
  107. if(emuInst == NULL) return;
  108. emuInst->debugger->quit = true;
  109. print_console(emuInst, "Stopping emulator..\n");
  110. }
  111. void start_emu(char *file) {
  112. emuInst = (Emulator *) calloc( 1, sizeof(Emulator) );
  113. Cpu *cpu = NULL; Debugger *deb = NULL;
  114. emuInst->cpu = (Cpu *) calloc(1, sizeof(Cpu));
  115. emuInst->cpu->bcm = (Bcm *) calloc(1, sizeof(Bcm));
  116. emuInst->cpu->timer_a = (Timer_a *) calloc(1, sizeof(Timer_a));
  117. emuInst->cpu->p1 = (Port_1 *) calloc(1, sizeof(Port_1));
  118. emuInst->cpu->usci = (Usci *) calloc(1, sizeof(Usci));
  119. emuInst->debugger = (Debugger *) calloc(1, sizeof(Debugger));
  120. setup_debugger(emuInst);
  121. cpu = emuInst->cpu;
  122. deb = emuInst->debugger;
  123. // deb->server = (Server *) calloc(1, sizeof(Server));
  124. initialize_msp_memspace();
  125. initialize_msp_registers(emuInst);
  126. setup_bcm(emuInst);
  127. setup_timer_a(emuInst);
  128. setup_port_1(emuInst);
  129. setup_usci(emuInst);
  130. print_console(emuInst, " [MSP430 Emulator]\n Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)\n");
  131. load_bootloader(0x0C00);
  132. if(load_firmware(emuInst, file, 0xC000) == 0) {
  133. // display_registers(emuInst);
  134. disassemble(emuInst, cpu->pc, 1);
  135. // update_register_display(emuInst);
  136. while (!deb->quit) {
  137. // Handle debugger when CPU is not running
  138. if (!cpu->running) {
  139. usleep(10000);
  140. continue;
  141. }
  142. // Handle Breakpoints
  143. //handle_breakpoints(emuInst);
  144. // Instruction Decoder
  145. decode(emuInst, fetch(emuInst), EXECUTE);
  146. // Handle Peripherals
  147. handle_bcm(emuInst);
  148. handle_timer_a(emuInst);
  149. handle_port_1(emuInst);
  150. handle_usci(emuInst);
  151. // Average of 4 cycles per instruction
  152. mclk_wait_cycles(emuInst, 4);
  153. }
  154. }
  155. uninitialize_msp_memspace();
  156. free(cpu->timer_a);
  157. free(cpu->bcm);
  158. free(cpu->p1);
  159. free(cpu->usci);
  160. free(cpu);
  161. free(deb->server);
  162. free(deb);
  163. free(emuInst);
  164. return;
  165. }
  166. //void init_packet_queue (Emulator *emu){
  167. // Server *s = emu->debugger->server;
  168. // s->pending_packets_head = NULL;
  169. // s->pending_packets_tail = NULL;
  170. // s->packets_queued = 0;
  171. // s->spin_lock = false;
  172. //}
  173. //void *emulator (void *ctxt) {
  174. // emu = (Emulator *) ctxt;
  175. // Debugger *deb = emu->debugger;
  176. //
  177. // init_packet_queue(emu);
  178. // printf("starting emulator...\n");
  179. //}