py_interface.c 5.8 KB

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