py_interface.c 6.6 KB

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