Skip to content

Commit

Permalink
Support set_output_callback().
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarev committed Jul 17, 2024
1 parent 897454f commit 27af8c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions z80/_z80module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ class machine : public B {
return old_callback;
}

void on_output(fast_u16 addr, fast_u8 value) {
if(!on_input_callback)
return;

PyObject *args = Py_BuildValue("(i, i)", addr, value);
decref_guard arg_guard(args);

PyObject *result = PyObject_CallObject(on_output_callback, args);
decref_guard result_guard(result);
}

PyObject *set_output_callback(PyObject *callback) {
PyObject *old_callback = on_output_callback;
on_output_callback = callback;
return old_callback;
}

fast_u8 on_get_b() const { return state.b; }
void on_set_b(fast_u8 n) { state.b = n; }

Expand Down Expand Up @@ -208,6 +225,7 @@ class machine : public B {

private:
PyObject *on_input_callback = nullptr;
PyObject *on_output_callback = nullptr;
};

static const unsigned max_instr_size = 4;
Expand Down
19 changes: 19 additions & 0 deletions z80/machine.inc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ static PyObject *set_input_callback(PyObject *self, PyObject *args) {
Py_RETURN_NONE;
}

static PyObject *set_output_callback(PyObject *self, PyObject *args) {
PyObject *new_callback;
if(!PyArg_ParseTuple(args, "O:set_callback", &new_callback))
return nullptr;

if(!PyCallable_Check(new_callback)) {
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return nullptr;
}

auto &machine = cast_machine(self);
PyObject *old_callback = machine.set_output_callback(new_callback);
Py_XINCREF(new_callback);
Py_XDECREF(old_callback);
Py_RETURN_NONE;
}

static PyObject *run(PyObject *self, PyObject *args) {
auto &machine = cast_machine(self);
z80::events_mask::type events = machine.on_run();
Expand Down Expand Up @@ -266,6 +283,8 @@ static PyMethodDef methods[] = {
"processing on reading, writing or executing them."},
{"set_input_callback", set_input_callback, METH_VARARGS,
"Set a callback function handling reading from ports."},
{"set_output_callback", set_output_callback, METH_VARARGS,
"Set a callback function handling writing to ports."},
{"run", run, METH_NOARGS,
"Run emulator until one or several events are signaled."},
#if defined(Z80_MACHINE)
Expand Down

0 comments on commit 27af8c1

Please sign in to comment.