main jcart.c
 1#include <stdlib.h>
 2#include "genesis.h"
 3
 4static io_port *get_ports(m68k_context *m68k)
 5{
 6	genesis_context *gen = m68k->system;
 7	if (!gen->extra) {
 8		io_port *ports = calloc(2, sizeof(io_port));
 9		ports[0].device_type = IO_GAMEPAD3;
10		ports[0].device.pad.gamepad_num = 3;
11		ports[1].device_type = IO_GAMEPAD3;
12		ports[1].device.pad.gamepad_num = 4;
13		io_control_write(ports, 0x40, 0);
14		io_control_write(ports + 1, 0x40, 0);
15		gen->extra = ports;
16	}
17		
18	return gen->extra;
19}
20
21void *jcart_write_w(uint32_t address, void *context, uint16_t value)
22{
23	m68k_context *m68k= context;
24	io_port *ports = get_ports(m68k);
25	value = value << 6 & 0x40;
26	io_data_write(ports, value, m68k->current_cycle);
27	io_data_write(ports + 1, value, m68k->current_cycle);
28	return context;
29}
30
31void *jcart_write_b(uint32_t address, void *context, uint8_t value)
32{
33	if (address & 1) {
34		return jcart_write_w(address, context, value);
35	}
36	return context;
37}
38
39uint16_t jcart_read_w(uint32_t address, void *context)
40{
41	m68k_context *m68k= context;
42	io_port *ports = get_ports(m68k);
43	//according to Eke, bit 14 is forced low, at least on the Micro Machines 2 cart
44	//TODO: Test behavior of actual cart
45	uint16_t value = io_data_read(ports, m68k->current_cycle) << 8;
46	value |= io_data_read(ports + 1, m68k->current_cycle);
47	return value;
48}
49
50uint8_t jcart_read_b(uint32_t address, void *context)
51{
52	m68k_context *m68k= context;
53	io_port *ports = get_ports(m68k);
54	return io_data_read(ports + (address & 1), m68k->current_cycle);
55}
56
57void jcart_adjust_cycles(genesis_context *context, uint32_t deduction)
58{
59	io_port *ports = get_ports(context->m68k);
60	io_adjust_cycles(ports, context->m68k->current_cycle, deduction);
61	io_adjust_cycles(ports + 1, context->m68k->current_cycle, deduction);
62}
63
64void jcart_gamepad_down(genesis_context *context, uint8_t gamepad_num, uint8_t button)
65{
66	io_port *ports = get_ports(context->m68k);
67	if (gamepad_num == ports[1].device.pad.gamepad_num) {
68		ports++;
69	} else if (gamepad_num != ports[0].device.pad.gamepad_num) {
70		ports = NULL;
71	}
72	if (ports) {
73		io_port_gamepad_down(ports, button);
74	}
75}
76
77void jcart_gamepad_up(genesis_context *context, uint8_t gamepad_num, uint8_t button)
78{
79	io_port *ports = get_ports(context->m68k);
80	if (gamepad_num == ports[1].device.pad.gamepad_num) {
81		ports++;
82	} else if (gamepad_num != ports[0].device.pad.gamepad_num) {
83		ports = NULL;
84	}
85	if (ports) {
86		io_port_gamepad_up(ports, button);
87	}
88}