annotate jcart.c @ 1610:c206a422d466

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