py_interface.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 PyMethodDef RunMethods[] = {
  115. {"init", method_start, METH_VARARGS, "Initialise msp430 emulator"},
  116. {"cmd", method_cmd, METH_VARARGS, "Send command to msp430 emulator"},
  117. {"stop", method_stop, METH_NOARGS, "Stop msp430 emulator"},
  118. {"play", method_play, METH_NOARGS, "Start running msp430 emulator"},
  119. {"pause", method_pause, METH_NOARGS, "Pause running msp430 emulator"},
  120. {"reset", method_reset, METH_NOARGS, "Reset msp430 emulator"},
  121. {"on_serial", method_on_serial, METH_VARARGS, "Set emulator callback for serial"},
  122. {"on_console", method_on_console, METH_VARARGS, "Set emulator callback for console"},
  123. {"on_control", method_on_control, METH_VARARGS, "Set emulator callback for control"},
  124. {"get_regs", method_get_regs, METH_VARARGS, "Get emulator registers"},
  125. {"set_regs", method_set_regs, METH_VARARGS, "Set emulator registers"},
  126. {NULL, NULL, 0, NULL}
  127. };
  128. static struct PyModuleDef msp430module = {
  129. PyModuleDef_HEAD_INIT,
  130. "_msp430emu",
  131. "Python interface for msp430 emulator",
  132. -1,
  133. RunMethods
  134. };
  135. PyMODINIT_FUNC PyInit__msp430emu(void) {
  136. return PyModule_Create(&msp430module);
  137. }
  138. void print_serial(Emulator *emu, char *buf) {
  139. if(pyOnSerial == NULL) return;
  140. // set thread state
  141. PyGILState_STATE gstate;
  142. gstate = PyGILState_Ensure();
  143. PyObject *tuple = PyTuple_New(1);
  144. PyObject *pyBuf = PyUnicode_FromString(buf);
  145. PyTuple_SetItem(tuple, 0, pyBuf);
  146. PyObject_Call(pyOnSerial, tuple, NULL);
  147. // Py_DECREF(pyBuf);
  148. // Py_DECREF(tuple);
  149. // resolve thread state
  150. PyGILState_Release(gstate);
  151. }
  152. void print_console (Emulator *emu, const char *buf) {
  153. if(pyOnConsole == NULL) {
  154. puts(buf);
  155. return;
  156. }
  157. // set thread state
  158. PyGILState_STATE gstate;
  159. gstate = PyGILState_Ensure();
  160. PyObject *tuple = PyTuple_New(1);
  161. PyObject *pyBuf = PyUnicode_FromString(buf);
  162. PyTuple_SetItem(tuple, 0, pyBuf);
  163. PyObject_Call(pyOnConsole, tuple, NULL);
  164. // Py_DECREF(pyBuf);
  165. // Py_DECREF(tuple);
  166. // resolve thread state
  167. PyGILState_Release(gstate);
  168. }
  169. void send_control (Emulator *emu, uint8_t opcode, void *data, size_t size) {
  170. if(pyOnControl == NULL) return;
  171. // set thread state
  172. PyGILState_STATE gstate;
  173. gstate = PyGILState_Ensure();
  174. PyObject *tuple = PyTuple_New(2);
  175. PyObject *pyOpcode = PyLong_FromLong(opcode);
  176. PyTuple_SetItem(tuple, 0, pyOpcode);
  177. PyObject *pyData = PyBytes_FromStringAndSize(data, size);
  178. PyTuple_SetItem(tuple, 1, pyData);
  179. PyObject_Call(pyOnControl, tuple, NULL);
  180. // Py_DECREF(pyOpcode);
  181. // Py_DECREF(pyData);
  182. // Py_DECREF(tuple);
  183. // resolve thread state
  184. PyGILState_Release(gstate);
  185. }