py_interface.c 6.3 KB

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