summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/bus/sms_ctrl/graphic.c
blob: 20a1f68dcdabeb447fc7b3c227a5b74d9d912617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**********************************************************************

    Sega Master System "Graphic Board" emulation

    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

I/O 3f write | this method
        0x20 | 0x7f
        0x00 | 0x3f
        0x30 | 0xff

Typical sequence:
- 3f write 0x20
- read dc
- 3f write 0x00
- read dc
- 3f write 0x20
- read dc
- 3f write 0x30
Suspect from kind of counter that is reset by a 0x30 write to I/O port 0x3f.
Once reset reads from i/O port dc expect to see 0xE0.
And then any write with differing bits goes through several internal I/O ports
with the first port being the one with the buttons

In the reset/start state the lower four/five bits are 0.
Then a nibble is read containing the buttons (active low)
Then 2 nibbles are read to form a byte (first high nibble, then low nibble) indicating
whether the pen is on the graphic board, a value of FD, FE, or FF used for this. For
any other value the following 2 bytes are not read.
Then 2 nibbles are read to form a byte containing the absolute X coordinate.
THen 2 nibbles are read to form a byte containing the absolute Y coordiante.

**********************************************************************/

#include "graphic.h"



//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

const device_type SMS_GRAPHIC = &device_creator<sms_graphic_device>;


static INPUT_PORTS_START( sms_graphic )
	PORT_START("BUTTONS")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON1 ) // MENU
	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON2 ) // DO
	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON3 ) // PEN
	PORT_BIT( 0xf8, IP_ACTIVE_LOW, IPT_UNUSED )

	PORT_START("X")
	PORT_BIT( 0xff, 0x00, IPT_LIGHTGUN_X) PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(15)

	PORT_START("Y")
	PORT_BIT( 0xff, 0x00, IPT_LIGHTGUN_Y) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(15)
INPUT_PORTS_END


//-------------------------------------------------
//  input_ports - device-specific input ports
//-------------------------------------------------

ioport_constructor sms_graphic_device::device_input_ports() const
{
	return INPUT_PORTS_NAME( sms_graphic );
}

//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  sms_graphic_device - constructor
//-------------------------------------------------

sms_graphic_device::sms_graphic_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, SMS_GRAPHIC, "Graphic Board", tag, owner, clock, "sms_joypad", __FILE__)
	, device_sms_control_port_interface(mconfig, *this)
	, m_buttons(*this, "BUTTONS")
	, m_x(*this, "X")
	, m_y(*this, "Y")
	, m_index(0)
	, m_previous_write(0xff)
	, m_pressure(0xfd)
{
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void sms_graphic_device::device_start()
{
	save_item(NAME(m_index));
	save_item(NAME(m_previous_write));
}


//-------------------------------------------------
//  sms_peripheral_r - joypad read
//-------------------------------------------------

UINT8 sms_graphic_device::peripheral_r()
{
	switch (m_index)
	{
		case 0:     // Initial state / "I am a board"
			// If any regular button is pressed raise/lower TL ?
//          if ((m_buttons->read() & 0x07) != 0x07)
//              return 0xf0;
			return 0xd0;

		case 1:    // Read buttons (active low)
			return m_buttons->read();

		case 2:    // Some thing only FD, FE, and FF cause the other values to be read
			return m_pressure >> 4;

		case 3:
			return m_pressure & 0x0f;

		case 4:    // High nibble X?
			return m_x->read() >> 4;

		case 5:    // Low nibble X?
			return m_x->read() & 0x0f;

		case 6:   // High nibble Y?
			return m_y->read() >> 4;

		case 7:   // Low Nibble Y?
			return m_y->read() & 0x0f;
	}

	return 0xff;
}

void sms_graphic_device::peripheral_w(UINT8 data)
{
	// Check for toggle on TH/TL
	if ((data ^ m_previous_write) & 0xc0)
	{
		m_index++;
	}

	// If TR is high, restart
	if (data & 0x80)
	{
		m_index = 0;
	}

	m_previous_write = data;
}