use absolute path
[openpower-isa.git] / media / pypowersim_wrapper / pypowersim_wrapper_common.h
1 #include <Python.h>
2 #include <stdint.h>
3 #include <stdio.h>
4
5 static const char* PLUGIN_NAME = "pypowersim";
6 static int python_initialized = 0;
7 static PyObject *plugin_name = NULL;
8 static PyObject *plugin_module = NULL;
9
10 typedef struct pypowersim_state {
11 PyObject *binary;
12 PyObject *bigendian;
13 PyObject *prog;
14 PyObject *qemu_cosim;
15 PyObject *initial_regs;
16 PyObject *initial_sprs;
17 PyObject *svstate;
18 PyObject *mmu;
19 PyObject *initial_cr;
20 PyObject *initial_mem;
21 PyObject *initial_fprs;
22 PyObject *initial_pc;
23 PyObject *args;
24 PyObject *simulator;
25 PyObject *result_obj;
26 } pypowersim_state_t;
27
28 static pypowersim_state_t *pypowersim_prepare(void) {
29 // Allocate memory for state
30 pypowersim_state_t *state = malloc(sizeof(pypowersim_state_t));
31 if (!state) {
32 printf("Error creating pypowersim_state object\n");
33 exit(1);
34 }
35 memset(state, 0, sizeof(pypowersim_state_t));
36
37 // Add pypowersim directory to Python path
38 if (!python_initialized) {
39 // Initialize Python C API
40 Py_Initialize();
41
42 PyObject* sysPath = PySys_GetObject((char*)"path");
43 PyObject* curDir = PyUnicode_FromString("/home/markos/src/openpower-isa/src/openpower/decoder/isa/");
44 PyList_Append(sysPath, curDir);
45 Py_DECREF(curDir);
46
47 // Set plugin name and module
48 plugin_name = PyUnicode_FromString(PLUGIN_NAME);
49 plugin_module = PyImport_Import(plugin_name);
50 Py_DECREF(plugin_name);
51 if (!plugin_module) {
52 PyErr_Print();
53 printf("Error importing module\n");
54 exit(1);
55 }
56 python_initialized = 1;
57 }
58
59 // Set simulator object
60 state->simulator = PyObject_GetAttrString(plugin_module, "run_a_simulation");
61 if (!state->simulator) {
62 PyErr_Print();
63 printf("Error retrieving 'run_a_simulation'\n");
64 exit(1);
65 }
66
67 // Little Endian for now
68 state->bigendian = Py_False;
69 state->prog = Py_None;
70 state->qemu_cosim = Py_False;
71 // Set and clear 128 GPRs
72 state->initial_regs = PyList_New(128);
73 for (int i=0; i < 128; i++) {
74 PyList_SetItem(state->initial_regs, i, PyLong_FromLong(0));
75 }
76 // Create SPRs to all bits set
77 state->initial_sprs= PyDict_New();
78 PyDict_SetItemString(state->initial_sprs, "LR", PyLong_FromLong(0xffffff));
79 // Set empty SVSTATE
80 state->svstate = PyLong_FromLong(0);
81 // Set no MMU
82 state->mmu = Py_None;
83 // Set no initial CR
84 state->initial_cr = PyLong_FromLong(0);
85 // Set empty initial Memory
86 state->initial_mem = PyDict_New();
87 // Set and Clear 128 FPR
88 state->initial_fprs = PyList_New(128);
89 for (int i=0; i < 128; i++) {
90 PyList_SetItem(state->initial_fprs, i, PyLong_FromLong(0));
91 }
92 // Set initial Program Counter
93 state->initial_pc= PyLong_FromLong(0x0);
94
95 return state;
96 }
97
98 static void pypowersim_prepareargs(pypowersim_state_t *state) {
99 // Set the tuple with the state objects
100 state->args = PyTuple_Pack(12, state->binary, state->bigendian, state->prog, state->qemu_cosim,
101 state->initial_regs, state->initial_sprs, state->svstate, state->mmu,
102 state->initial_cr, state->initial_mem, state->initial_fprs, state->initial_pc );
103 if (!state->args) {
104 PyErr_Print();
105 Py_DECREF(state->simulator);
106 printf("Error building args tuple\n");
107 exit(1);
108 }
109 }
110
111 static void pypowersim_finalize(pypowersim_state_t *state) {
112 if (state->simulator) Py_DECREF(state->simulator);
113 if (state->binary) Py_DECREF(state->binary);
114 if (state->bigendian) Py_DECREF(state->bigendian);
115 if (state->prog) Py_DECREF(state->prog);
116 if (state->qemu_cosim) Py_DECREF(state->qemu_cosim);
117 if (state->initial_regs) Py_DECREF(state->initial_regs);
118 if (state->initial_sprs) Py_DECREF(state->initial_sprs);
119 if (state->svstate) Py_DECREF(state->svstate);
120 if (state->mmu) Py_DECREF(state->mmu);
121 if (state->initial_cr) Py_DECREF(state->initial_cr);
122 if (state->initial_mem) Py_DECREF(state->initial_mem);
123 if (state->initial_fprs) Py_DECREF(state->initial_fprs);
124 if (state->initial_pc) Py_DECREF(state->initial_pc);
125 if (state->args) Py_DECREF(state->args);
126 if (state->result_obj) Py_DECREF(state->result_obj);
127 memset(state, 0, sizeof(pypowersim_state_t));
128 if (state) free(state);
129
130 // Finalize Python C API
131 // Py_Finalize();
132 }
133