main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. MSP430 Emulator
  3. Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)
  4. "MSP430 Emulator" is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. "MSP430 Emulator" is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "main.h"
  16. #include <Python.h>
  17. // This is an ugly solution but heh
  18. Emulator *emuInst;
  19. static PyObject *method_run(PyObject *self, PyObject *args) {
  20. unsigned int port;
  21. if(!PyArg_ParseTuple(args, "H", &port)) {
  22. return NULL;
  23. }
  24. Py_BEGIN_ALLOW_THREADS
  25. run(port);
  26. Py_END_ALLOW_THREADS
  27. return Py_None;
  28. }
  29. static PyObject *method_stop(PyObject *self, PyObject *args) {
  30. if(emuInst != NULL) {
  31. emuInst->debugger->quit = true;
  32. }
  33. return Py_None;
  34. }
  35. static PyMethodDef RunMethods[] = {
  36. {"run", method_run, METH_VARARGS, "Python function to start msp430 emulator"},
  37. {"stop", method_stop, METH_NOARGS, "Python function to stop msp430 emulator"},
  38. {NULL, NULL, 0, NULL}
  39. };
  40. static struct PyModuleDef msp430module = {
  41. PyModuleDef_HEAD_INIT,
  42. "_msp430emu",
  43. "Python interface for msp430 emulator",
  44. -1,
  45. RunMethods
  46. };
  47. PyMODINIT_FUNC PyInit__msp430emu(void) {
  48. return PyModule_Create(&msp430module);
  49. }
  50. int run(unsigned int port) {
  51. emuInst = (Emulator *) calloc( 1, sizeof(Emulator) );
  52. Cpu *cpu = NULL; Debugger *deb = NULL;
  53. emuInst->cpu = (Cpu *) calloc(1, sizeof(Cpu));
  54. emuInst->cpu->bcm = (Bcm *) calloc(1, sizeof(Bcm));
  55. emuInst->cpu->timer_a = (Timer_a *) calloc(1, sizeof(Timer_a));
  56. emuInst->cpu->p1 = (Port_1 *) calloc(1, sizeof(Port_1));
  57. emuInst->cpu->usci = (Usci *) calloc(1, sizeof(Usci));
  58. emuInst->debugger = (Debugger *) calloc(1, sizeof(Debugger));
  59. setup_debugger(emuInst);
  60. cpu = emuInst->cpu;
  61. deb = emuInst->debugger;
  62. deb->server = (Server *) calloc(1, sizeof(Server));
  63. if (deb->web_interface == true)
  64. {
  65. deb->ws_port = port;
  66. pthread_t *t = &deb->web_server_thread;
  67. if ( pthread_create(t, NULL, web_server, (void *)emuInst ) )
  68. {
  69. fprintf(stderr, "Error creating web server thread\n");
  70. return 1;
  71. }
  72. while (!deb->web_server_ready)
  73. usleep(10000);
  74. print_console(emuInst, " [MSP430 Emulator]\n Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)\n");
  75. // print_console(emuInst, " [!] Upload your firmware (ELF format only); Type 'h' for debugger options.\n\n");
  76. while (!deb->web_firmware_uploaded)
  77. usleep(10000);
  78. }
  79. //register_signal(SIGINT); // Register Callback for CONTROL-c
  80. initialize_msp_memspace();
  81. initialize_msp_registers(emuInst);
  82. setup_bcm(emuInst);
  83. setup_timer_a(emuInst);
  84. setup_port_1(emuInst);
  85. setup_usci(emuInst);
  86. load_bootloader(0x0C00);
  87. if (deb->web_interface)
  88. load_firmware(emuInst, (char*)"tmp.bin", 0xC000);
  89. // display first round of registers
  90. display_registers(emuInst);
  91. disassemble(emuInst, cpu->pc, 1);
  92. update_register_display(emuInst);
  93. // Fetch-Decode-Execute Cycle (run machine)
  94. while (!deb->quit)
  95. {
  96. // Handle debugger when CPU is not running
  97. if (!cpu->running)
  98. {
  99. usleep(10000);
  100. continue;
  101. }
  102. // Handle Breakpoints
  103. handle_breakpoints(emuInst);
  104. // Instruction Decoder
  105. decode(emuInst, fetch(emuInst), EXECUTE);
  106. // Handle Peripherals
  107. handle_bcm(emuInst);
  108. handle_timer_a(emuInst);
  109. handle_port_1(emuInst);
  110. handle_usci(emuInst);
  111. // Average of 4 cycles per instruction
  112. mclk_wait_cycles(emuInst, 4);
  113. }
  114. uninitialize_msp_memspace();
  115. free(cpu->timer_a);
  116. free(cpu->bcm);
  117. free(cpu->p1);
  118. free(cpu->usci);
  119. free(cpu);
  120. free(deb->server);
  121. free(deb);
  122. free(emuInst);
  123. return 0;
  124. }
  125. int main(int argc, char *argv[])
  126. {
  127. if (argv[1] == NULL)
  128. {
  129. puts("Need port argument");
  130. return(1);
  131. }
  132. return run(strtoul(argv[1], NULL, 10));
  133. }