1 // license:BSD-3-Clause
2 // copyright-holders:Fabio Priuli
3 /**********************************************************************
4 
5     Sega Master System "Graphic Board" emulation
6 
7 I/O 3f write | this method
8         0x20 | 0x7f
9         0x00 | 0x3f
10         0x30 | 0xff
11 
12 Typical sequence:
13 - 3f write 0x20
14 - read dc
15 - 3f write 0x00
16 - read dc
17 - 3f write 0x20
18 - read dc
19 - 3f write 0x30
20 Suspect from kind of counter that is reset by a 0x30 write to I/O port 0x3f.
21 Once reset reads from i/O port dc expect to see 0xE0.
22 And then any write with differing bits goes through several internal I/O ports
23 with the first port being the one with the buttons
24 
25 In the reset/start state the lower four/five bits are 0.
26 Then a nibble is read containing the buttons (active low)
27 Then 2 nibbles are read to form a byte (first high nibble, then low nibble) indicating
28 whether the pen is on the graphic board, a value of FD, FE, or FF used for this. For
29 any other value the following 2 bytes are not read.
30 Then 2 nibbles are read to form a byte containing the absolute X coordinate.
31 Then 2 nibbles are read to form a byte containing the absolute Y coordiante.
32 
33 **********************************************************************/
34 
35 #include "emu.h"
36 #include "graphic.h"
37 
38 
39 
40 //**************************************************************************
41 //  DEVICE DEFINITIONS
42 //**************************************************************************
43 
44 DEFINE_DEVICE_TYPE(SMS_GRAPHIC, sms_graphic_device, "sms_graphic", "Sega SMS Graphic Board")
45 
46 
INPUT_PORTS_START(sms_graphic)47 static INPUT_PORTS_START( sms_graphic )
48 	PORT_START("BUTTONS")
49 	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON1 ) // MENU
50 	PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON2 ) // DO
51 	PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON3 ) // PEN
52 	PORT_BIT( 0xf8, IP_ACTIVE_LOW, IPT_UNUSED )
53 
54 	PORT_START("X")
55 	PORT_BIT( 0xff, 0x00, IPT_LIGHTGUN_X) PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(15)
56 
57 	PORT_START("Y")
58 	PORT_BIT( 0xff, 0x00, IPT_LIGHTGUN_Y) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(15)
59 INPUT_PORTS_END
60 
61 
62 //-------------------------------------------------
63 //  input_ports - device-specific input ports
64 //-------------------------------------------------
65 
66 ioport_constructor sms_graphic_device::device_input_ports() const
67 {
68 	return INPUT_PORTS_NAME( sms_graphic );
69 }
70 
71 //**************************************************************************
72 //  LIVE DEVICE
73 //**************************************************************************
74 
75 //-------------------------------------------------
76 //  sms_graphic_device - constructor
77 //-------------------------------------------------
78 
sms_graphic_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)79 sms_graphic_device::sms_graphic_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
80 	: device_t(mconfig, SMS_GRAPHIC, tag, owner, clock)
81 	, device_sms_control_port_interface(mconfig, *this)
82 	, m_buttons(*this, "BUTTONS")
83 	, m_x(*this, "X")
84 	, m_y(*this, "Y")
85 	, m_index(0)
86 	, m_previous_write(0xff)
87 	, m_pressure(0xfd)
88 {
89 }
90 
91 
92 //-------------------------------------------------
93 //  device_start - device-specific startup
94 //-------------------------------------------------
95 
device_start()96 void sms_graphic_device::device_start()
97 {
98 	save_item(NAME(m_index));
99 	save_item(NAME(m_previous_write));
100 }
101 
102 
103 //-------------------------------------------------
104 //  sms_peripheral_r - joypad read
105 //-------------------------------------------------
106 
peripheral_r()107 uint8_t sms_graphic_device::peripheral_r()
108 {
109 	switch (m_index)
110 	{
111 		case 0:     // Initial state / "I am a board"
112 			// If any regular button is pressed raise/lower TL ?
113 //          if ((m_buttons->read() & 0x07) != 0x07)
114 //              return 0xf0;
115 			return 0xd0;
116 
117 		case 1:    // Read buttons (active low)
118 			return m_buttons->read();
119 
120 		case 2:    // Some thing only FD, FE, and FF cause the other values to be read
121 			return m_pressure >> 4;
122 
123 		case 3:
124 			return m_pressure & 0x0f;
125 
126 		case 4:    // High nibble X?
127 			return m_x->read() >> 4;
128 
129 		case 5:    // Low nibble X?
130 			return m_x->read() & 0x0f;
131 
132 		case 6:   // High nibble Y?
133 			return m_y->read() >> 4;
134 
135 		case 7:   // Low Nibble Y?
136 			return m_y->read() & 0x0f;
137 	}
138 
139 	return 0xff;
140 }
141 
peripheral_w(uint8_t data)142 void sms_graphic_device::peripheral_w(uint8_t data)
143 {
144 	// Check for toggle on TH/TL
145 	if ((data ^ m_previous_write) & 0xc0)
146 	{
147 		m_index++;
148 	}
149 
150 	// If TR is high, restart
151 	if (data & 0x80)
152 	{
153 		m_index = 0;
154 	}
155 
156 	m_previous_write = data;
157 }
158