changeset 2422:1978bd770023

Expose gamepad state in debugger
author Michael Pavone <pavone@retrodev.com>
date Sat, 20 Jan 2024 21:15:56 -0800
parents bcfa5e272f5e
children b733a10488c6
files debug.c io.c io.h
diffstat 3 files changed, 43 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/debug.c	Sat Jan 20 01:30:19 2024 -0800
+++ b/debug.c	Sat Jan 20 21:15:56 2024 -0800
@@ -2039,6 +2039,46 @@
 	return root;
 }
 
+static debug_val debug_iopad_get(debug_array *array, uint32_t index)
+{
+	io_port *port = find_gamepad(array->base, index + 1);
+	uint32_t ret = 0;
+	if (port) {
+		ret |= port->input[1];
+		ret |= port->input[0] << 2 & 0xC0;
+		ret |= port->input[2] << 8 & 0xF00;
+	}
+	return debug_int(ret);
+}
+
+static void debug_iopad_set(debug_array *array, uint32_t index, debug_val val)
+{
+	uint32_t ival;
+	if (!debug_cast_int(val, &ival)) {
+		fprintf(stderr, "pad state can only be set to integers\n");
+		return;
+	}
+	io_port *port = find_gamepad(array->base, index + 1);
+	if (port) {
+		port->input[1] &= ~0x3F;
+		port->input[1] |= ival & 0x3F;
+		port->input[0] &= ~0x33;
+		port->input[0] |= ival & 0x3;
+		port->input[0] |= ival >> 2 & 0x30;
+		port->input[2] &= ~0xF;
+		port->input[2] |= ival >> 8 & 0xF;
+	}
+}
+
+debug_root *find_io_root(sega_io *io)
+{
+	debug_root *root = find_root(io);
+	
+	new_readonly_variable(root, "pads", new_fixed_array(io, debug_iopad_get, debug_iopad_set, MAX_JOYSTICKS));
+	
+	return root;
+}
+
 void ambiguous_iter(char *key, tern_val val, uint8_t valtype, void *data)
 {
 	char *prefix = data;
@@ -4797,6 +4837,7 @@
 				root->other_roots = tern_insert_ptr(root->other_roots, "vdp", find_vdp_root(gen->vdp));
 				root->other_roots = tern_insert_ptr(root->other_roots, "ym", find_ym2612_root(gen->ym));
 				root->other_roots = tern_insert_ptr(root->other_roots, "psg", find_psg_root(gen->psg));
+				root->other_roots = tern_insert_ptr(root->other_roots, "io", find_io_root(&gen->io));
 				add_commands(root, genesis_commands, NUM_GENESIS);
 				var = calloc(1, sizeof(debug_var));
 				var->get = debug_frame_get;
--- a/io.c	Sat Jan 20 01:30:19 2024 -0800
+++ b/io.c	Sat Jan 20 21:15:56 2024 -0800
@@ -89,7 +89,7 @@
 	[BUTTON_MODE] = {.states = {GAMEPAD_EXTRA, GAMEPAD_NONE}, .value = 0x8},
 };
 
-static io_port *find_gamepad(sega_io *io, uint8_t gamepad_num)
+io_port *find_gamepad(sega_io *io, uint8_t gamepad_num)
 {
 	for (int i = 0; i < 3; i++)
 	{
--- a/io.h	Sat Jan 20 01:30:19 2024 -0800
+++ b/io.h	Sat Jan 20 21:15:56 2024 -0800
@@ -167,6 +167,7 @@
 void io_keyboard_down(sega_io *io, uint8_t scancode);
 void io_keyboard_up(sega_io *io, uint8_t scancode);
 uint8_t io_has_keyboard(sega_io *io);
+io_port *find_gamepad(sega_io *io, uint8_t gamepad_num);
 
 extern const char * device_type_names[];