py_interface.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "py_interface.h"
  2. PyObject *pyOnSerial;
  3. PyObject *pyOnConsole;
  4. PyObject *pyOnControl;
  5. static PyObject *method_start(PyObject *self, PyObject *args) {
  6. char *firmware_file;
  7. if(!PyArg_ParseTuple(args, "s", &firmware_file)) {
  8. return NULL;
  9. }
  10. Py_BEGIN_ALLOW_THREADS
  11. start_emu(firmware_file);
  12. Py_END_ALLOW_THREADS
  13. return Py_None;
  14. }
  15. static PyObject *method_cmd(PyObject *self, PyObject *args) {
  16. char *cmd;
  17. if(!PyArg_ParseTuple(args, "s", &cmd)) {
  18. return NULL;
  19. }
  20. cmd_emu(cmd, strlen(cmd));
  21. return Py_None;
  22. }
  23. static PyObject *method_get_regs(PyObject *self, PyObject *args) {
  24. uint8_t reg_type;
  25. if(!PyArg_ParseTuple(args, "B", &reg_type)) {
  26. return NULL;
  27. }
  28. switch(reg_type) {
  29. case GET_REG_P1:
  30. return get_port1_regs();
  31. case GET_REG_BCM:
  32. return get_bcm_regs();
  33. case GET_REG_TIMER_A:
  34. return get_timer_regs();
  35. case GET_REG_USCI:
  36. return get_usci_regs();
  37. case GET_REG_CPU:
  38. return get_cpu_regs();
  39. }
  40. return Py_None;
  41. }
  42. static PyObject *method_set_regs(PyObject *self, PyObject *args) {
  43. uint8_t reg_type, reg_value;
  44. if(!PyArg_ParseTuple(args, "BB", &reg_type, &reg_value)) {
  45. return NULL;
  46. }
  47. set_reg(reg_type, reg_value);
  48. return Py_None;
  49. }
  50. static PyObject *method_stop(PyObject *self, PyObject *args) {
  51. stop_emu();
  52. // Py_XDECREF(pyOnSerial);
  53. // Py_XDECREF(pyOnConsole);
  54. // Py_XDECREF(pyOnControl);
  55. return Py_None;
  56. }
  57. static PyObject *method_reset(PyObject *self, PyObject *args) {
  58. reset_emu();
  59. return Py_None;
  60. }
  61. static PyObject *method_play(PyObject *self, PyObject *args) {
  62. play_emu();
  63. return Py_None;
  64. }
  65. static PyObject *method_pause(PyObject *self, PyObject *args) {
  66. pause_emu();
  67. return Py_None;
  68. }
  69. static PyObject *method_on_serial(PyObject *self, PyObject *args) {
  70. if(!PyArg_ParseTuple(args, "O", &pyOnSerial)) {
  71. PyErr_SetString(PyExc_ValueError, "Invalid argument, must be single object");
  72. pyOnSerial = NULL;
  73. return NULL;
  74. }
  75. if(!PyCallable_Check(pyOnSerial)) {
  76. PyErr_SetString(PyExc_ValueError, "Argument object is not callable");
  77. pyOnSerial = NULL;
  78. return NULL;
  79. }
  80. Py_INCREF(pyOnSerial);
  81. return Py_None;
  82. }
  83. static PyObject *method_on_console(PyObject *self, PyObject *args) {
  84. if(!PyArg_ParseTuple(args, "O", &pyOnConsole)) {
  85. PyErr_SetString(PyExc_ValueError, "Invalid argument, must be single object");
  86. pyOnControl = NULL;
  87. return NULL;
  88. }
  89. if(!PyCallable_Check(pyOnConsole)) {
  90. PyErr_SetString(PyExc_ValueError, "Argument object is not callable");
  91. pyOnConsole = NULL;
  92. return NULL;
  93. }
  94. Py_INCREF(pyOnConsole);
  95. PyObject *tuple = PyTuple_New(1);
  96. PyObject *pyBuf = PyUnicode_FromString("Console established..\n");
  97. PyTuple_SetItem(tuple, 0, pyBuf);
  98. PyObject_Call(pyOnConsole, tuple, NULL);
  99. return Py_None;
  100. }
  101. static PyObject *method_on_control(PyObject *self, PyObject *args) {
  102. if(!PyArg_ParseTuple(args, "O", &pyOnControl)) {
  103. PyErr_SetString(PyExc_ValueError, "Invalid argument, must be single object");
  104. pyOnSerial = NULL;
  105. return NULL;
  106. }
  107. if(!PyCallable_Check(pyOnControl)) {
  108. PyErr_SetString(PyExc_ValueError, "Argument object is not callable");
  109. pyOnSerial = NULL;
  110. return NULL;
  111. }
  112. Py_INCREF(pyOnControl);
  113. return Py_None;
  114. }
  115. static PyObject *method_write_serial(PyObject *self, PyObject *args) {
  116. char *cmd;
  117. if(!PyArg_ParseTuple(args, "s", &cmd)) {
  118. return NULL;
  119. }
  120. Py_BEGIN_ALLOW_THREADS
  121. write_serial(cmd, strlen(cmd));
  122. Py_END_ALLOW_THREADS
  123. return Py_None;
  124. }
  125. static PyObject *method_set_misc(PyObject *self, PyObject *args) {
  126. return get_misc_data();
  127. }
  128. static PyMethodDef RunMethods[] = {
  129. {"init", method_start, METH_VARARGS, "Initialise msp430 emulator"},
  130. {"cmd", method_cmd, METH_VARARGS, "Send command to msp430 emulator"},
  131. {"stop", method_stop, METH_NOARGS, "Stop msp430 emulator"},
  132. {"play", method_play, METH_NOARGS, "Start running msp430 emulator"},
  133. {"pause", method_pause, METH_NOARGS, "Pause running msp430 emulator"},
  134. {"reset", method_reset, METH_NOARGS, "Reset msp430 emulator"},
  135. {"on_serial", method_on_serial, METH_VARARGS, "Set emulator callback for serial"},
  136. {"on_console", method_on_console, METH_VARARGS, "Set emulator callback for console"},
  137. {"on_control", method_on_control, METH_VARARGS, "Set emulator callback for control"},
  138. {"get_regs", method_get_regs, METH_VARARGS, "Get emulator registers"},
  139. {"set_regs", method_set_regs, METH_VARARGS, "Set emulator registers"},
  140. {"get_misc", method_set_misc, METH_NOARGS, "Get emulator miscellaneous information"},
  141. {"write_serial", method_write_serial, METH_VARARGS, "Write to UART serial"},
  142. {NULL, NULL, 0, NULL}
  143. };
  144. static struct PyModuleDef msp430module = {
  145. PyModuleDef_HEAD_INIT,
  146. "_msp430emu",
  147. "Python interface for msp430 emulator",
  148. -1,
  149. RunMethods
  150. };
  151. PyMODINIT_FUNC PyInit__msp430emu(void) {
  152. return PyModule_Create(&msp430module);
  153. }
  154. void print_serial(Emulator *emu, char *buf) {
  155. if(pyOnSerial == NULL) return;
  156. // set thread state
  157. PyGILState_STATE gstate;
  158. gstate = PyGILState_Ensure();
  159. PyObject *tuple = PyTuple_New(1);
  160. PyObject *pyBuf = PyUnicode_FromString(buf);
  161. PyTuple_SetItem(tuple, 0, pyBuf);
  162. PyObject_Call(pyOnSerial, tuple, NULL);
  163. // Py_DECREF(pyBuf);
  164. // Py_DECREF(tuple);
  165. // resolve thread state
  166. PyGILState_Release(gstate);
  167. }
  168. void print_console (Emulator *emu, const char *buf) {
  169. if(pyOnConsole == NULL) return;
  170. // set thread state
  171. PyGILState_STATE gstate;
  172. gstate = PyGILState_Ensure();
  173. PyObject *tuple = PyTuple_New(1);
  174. PyObject *pyBuf = PyUnicode_FromString(buf);
  175. PyTuple_SetItem(tuple, 0, pyBuf);
  176. PyObject_Call(pyOnConsole, tuple, NULL);
  177. // Py_DECREF(pyBuf);
  178. // Py_DECREF(tuple);
  179. // resolve thread state
  180. PyGILState_Release(gstate);
  181. }
  182. void send_control (Emulator *emu, uint8_t opcode, void *data, size_t size) {
  183. if(pyOnControl == NULL) return;
  184. // set thread state
  185. PyGILState_STATE gstate;
  186. gstate = PyGILState_Ensure();
  187. PyObject *tuple = PyTuple_New(2);
  188. PyObject *pyOpcode = PyLong_FromLong(opcode);
  189. PyTuple_SetItem(tuple, 0, pyOpcode);
  190. PyObject *pyData = PyBytes_FromStringAndSize(data, size);
  191. PyTuple_SetItem(tuple, 1, pyData);
  192. PyObject_Call(pyOnControl, tuple, NULL);
  193. // Py_DECREF(pyOpcode);
  194. // Py_DECREF(pyData);
  195. // Py_DECREF(tuple);
  196. // resolve thread state
  197. PyGILState_Release(gstate);
  198. }