1 // license:BSD-3-Clause
2 // copyright-holders:Roberto Lavarone
3 /**********************************************************************
4 
5     SMC KR2376 Keyboard Encoder emulation
6 
7 **********************************************************************/
8 
9 #include "emu.h"
10 #include "kr2376.h"
11 
12 
13 DEFINE_DEVICE_TYPE(KR2376_ST,  kr2376_st_device,  "kr2376_st",  "SMC KR2376-ST Keyboard Encoder")
14 //DEFINE_DEVICE_TYPE(KR2376_12,  kr2376_12_device,  "kr2376_12",  "SMC KR2376-12 Keyboard Encoder")
15 
kr2376_device(const machine_config & mconfig,device_type type,const char * tag,device_t * owner,uint32_t clock)16 kr2376_device::kr2376_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) :
17 	device_t(mconfig, type, tag, owner, clock),
18 	m_read_x(*this),
19 	m_read_shift(*this),
20 	m_read_control(*this),
21 	m_write_strobe(*this)
22 {
23 }
24 
kr2376_st_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)25 kr2376_st_device::kr2376_st_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
26 	: kr2376_device(mconfig, KR2376_ST, tag, owner, clock)
27 {
28 }
29 
key_codes(int mode,int x,int y)30 uint8_t kr2376_st_device::key_codes(int mode, int x, int y)
31 {
32 	static const uint8_t KEY_CODES[3][8][11] =
33 	{
34 		// normal
35 		{
36 			//  Y0    Y1    Y2    Y3    Y4    Y5    Y6    Y7    Y8    Y9   Y10
37 			// NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL   DC1    P     O        X0
38 			{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x11, 0x50, 0x30 }, // X0
39 			// DLE   K     L     N     M     NAK   SYN   ETB   CAN   EM    SUB       X1
40 			{ 0x10, 0x4b, 0x4c, 0x4e, 0x4d, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a }, // X1
41 			// -     FS    GS    RS    US    <     >     ,     SP    .     _         X2
42 			{ 0x2d, 0x1c, 0x1d, 0x1e, 0x1f, 0x3c, 0x3e, 0x2c, 0x20, 0x2e, 0x5f }, // X2
43 			// 0     :     p     _     @     BS    [     ]     CR    LF    DEL       X3
44 			{ 0x30, 0x3a, 0x70, 0x5f, 0x40, 0x08, 0x5B, 0x5d, 0x0d, 0x0a, 0x7f }, // X3
45 			{ 0x3b, 0x2f, 0x2e, 0x2c, 0x6d, 0x6e, 0x62, 0x76, 0x63, 0x78, 0x7a }, // X4
46 			{ 0x6c, 0x6b, 0x6a, 0x68, 0x67, 0x66, 0x64, 0x73, 0x61, 0x0c, 0x1b }, // X5
47 			{ 0x6f, 0x69, 0x75, 0x79, 0x74, 0x72, 0x65, 0x77, 0x71, 0x09, 0x0b }, // X6
48 			{ 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x5e, 0x5c }  // X7
49 		},
50 
51 		// shift
52 		{
53 			//  Y0    Y1    Y2    Y3    Y4    Y5    Y6    Y7    Y8    Y9   Y10
54 			// NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL   DC1    @     _        X0
55 			{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x11, 0x40, 0x5f }, // X0
56 			// DLE   [     \     ^     ]     NAK   SYN   ETB   CAN   EM    SUB       X1
57 			{ 0x10, 0x5b, 0x5c, 0x5e, 0x5d, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a }, // X1
58 			// =     FS    GS    RS    US    <     >     ,     SP    .     _         X2
59 			{ 0x3d, 0x1c, 0x1d, 0x1e, 0x1f, 0x3c, 0x3e, 0x2c, 0x20, 0x2e, 0x5f }, // X2
60 			// NUL   *     P     DEL   `     BS    {     }     CR    LF    DEL       X3
61 			{ 0x00, 0x2a, 0x50, 0x7f, 0x60, 0x08, 0x7b, 0x7d, 0x0d, 0x0a, 0x7f }, // X3
62 			{ 0x2b, 0x3f, 0x3e, 0x3c, 0x4d, 0x4e, 0x42, 0x56, 0x43, 0x58, 0x5a }, // X4
63 			{ 0x4c, 0x4b, 0x4a, 0x48, 0x47, 0x46, 0x44, 0x53, 0x41, 0x0c, 0x1b }, // X5
64 			{ 0x4f, 0x49, 0x55, 0x59, 0x54, 0x52, 0x45, 0x57, 0x51, 0x09, 0x0b }, // X6
65 			{ 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x7e, 0x7c }  // X7
66 		},
67 
68 		// control
69 		{
70 			//  Y0    Y1    Y2    Y3    Y4    Y5    Y6    Y7    Y8    Y9   Y10
71 			// NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL   DC1   DLE   SI        X0
72 			{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x11, 0x10, 0x0f }, // X0
73 			// DLE   VT    FF    SO    CR    NAK   SYN   ETB   CAN   EM    SUB       X1
74 			{ 0x10, 0x0b, 0x0c, 0x0e, 0x0d, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a }, // X1
75 			// NUL   FS    GS    RS    US    NUL   NUL   NUL   SP    NUL   US        X2
76 			{ 0x00, 0x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x1f }, // X2
77 			// NUL   NUL   DLE   US    NUL   BS    ESC   GS    CR    LF    DEL       X3
78 			{ 0x00, 0x00, 0x10, 0x1f, 0x00, 0x08, 0x1B, 0x1d, 0x0d, 0x0a, 0x7f }, // X3
79 			{ 0x00, 0x00, 0x00, 0x00, 0x1d, 0x0e, 0x02, 0x16, 0x03, 0x18, 0x1a }, // X4
80 			{ 0x0c, 0x0b, 0x0a, 0x08, 0x07, 0x06, 0x04, 0x13, 0x01, 0x0c, 0x1b }, // X5
81 			{ 0x1f, 0x09, 0x15, 0x19, 0x14, 0x12, 0x05, 0x17, 0x11, 0x09, 0x0b }, // X6
82 			{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x1c }  // X7
83 		}
84 	};
85 	return KEY_CODES[mode][x][y];
86 }
87 
88 // TODO: determine ROM contents of KR2376-12
89 //kr2376_12_device::kr2376_12_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
90 //  : kr2376_device(mconfig, KR2376_12, tag, owner, clock)
91 //{}
92 //uint8_t kr2376_12_device::key_codes(int mode, int x, int y)
93 //{
94 //  static const uint8_t KEY_CODES[3][8][11] =
95 //  {
96 //      // normal
97 //      {
98 //          //  Y0    Y1    Y2    Y3    Y4    Y5    Y6    Y7    Y8    Y9   Y10
99 //          { 0x33, 0x32, 0x31, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00 }, // X0
100 //          { 0x36, 0x35, 0x34, 0x7d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // X1
101 //          { 0x39, 0x38, 0x37, 0x5b, 0x02, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00 }, // X2
102 //          { 0x08, 0x0a, 0x09, 0x0b, 0x00, 0x7c, 0x00, 0x00, 0x0d, 0x20, 0x00 }, // X3
103 //          { 0x2d, 0x2f, 0x2e, 0x2c, 0x6d, 0x6e, 0x62, 0x76, 0x63, 0x78, 0x7a }, // X4
104 //          { 0x3a, 0x3b, 0x6c, 0x6b, 0x6a, 0x68, 0x67, 0x66, 0x64, 0x73, 0x61 }, // X5
105 //          { 0x60, 0x70, 0x6f, 0x69, 0x75, 0x79, 0x74, 0x72, 0x65, 0x77, 0x71 }, // X6
106 //          { 0x2d, 0x30, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 }  // X7
107 //      },
108 //
109 //      // shift
110 //      {
111 //          //  Y0    Y1    Y2    Y3    Y4    Y5    Y6    Y7    Y8    Y9   Y10
112 //          { 0x33, 0x32, 0x31, 0x00, 0x00, 0x5e, 0x00, 0x00, 0x00, 0x00, 0x00 }, // X0
113 //          { 0x36, 0x35, 0x34, 0x5d, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // X1
114 //          { 0x39, 0x38, 0x37, 0x7b, 0x04, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00 }, // X2
115 //          { 0x08, 0x0a, 0x09, 0x0b, 0x00, 0x5c, 0x00, 0x00, 0x0d, 0x20, 0x00 }, // X3
116 //          { 0x2d, 0x3f, 0x3e, 0x3c, 0x4d, 0x4e, 0x42, 0x56, 0x43, 0x58, 0x5a }, // X4
117 //          { 0x2a, 0x2b, 0x4c, 0x4b, 0x4a, 0x48, 0x47, 0x46, 0x44, 0x53, 0x41 }, // X5
118 //          { 0x40, 0x50, 0x4f, 0x49, 0x55, 0x59, 0x54, 0x52, 0x45, 0x57, 0x51 }, // X6
119 //          { 0x3d, 0x30, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21 }  // X7
120 //      },
121 //
122 //      // control
123 //      {
124 //          //  Y0    Y1    Y2    Y3    Y4    Y5    Y6    Y7    Y8    Y9   Y10
125 //          { 0x33, 0x32, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // X0
126 //          { 0x36, 0x35, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // X1
127 //          { 0x39, 0x38, 0x37, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00 }, // X2
128 //          { 0x08, 0x0a, 0x09, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x20, 0x00 }, // X3
129 //          { 0x1f, 0x2f, 0x2e, 0x2c, 0x0d, 0x0e, 0x02, 0x16, 0x03, 0x18, 0x1a }, // X4
130 //          { 0x3a, 0x3b, 0x0c, 0x0b, 0x0a, 0x08, 0x07, 0x06, 0x04, 0x13, 0x01 }, // X5
131 //          { 0x00, 0x10, 0x0f, 0x09, 0x15, 0x19, 0x14, 0x12, 0x05, 0x17, 0x11 }, // X6
132 //          { 0x2d, 0x30, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 }  // X7
133 //      }
134 //  };
135 //  return KEY_CODES[mode][x][y];
136 //}
137 
138 //-------------------------------------------------
139 //  device_start - device-specific startup
140 //-------------------------------------------------
141 
device_start()142 void kr2376_device::device_start()
143 {
144 	/* resolve callbacks */
145 	m_read_x.resolve_all_safe(0x7ff);
146 	m_read_shift.resolve_safe(0);
147 	m_read_control.resolve_safe(0);
148 	m_write_strobe.resolve_safe();
149 
150 	/* set initial values */
151 	m_ring11 = 0;
152 	m_ring8 = 0;
153 	m_strobe = 0;
154 	m_strobe_old = 0;
155 	m_parity = 0;
156 	m_data = 0;
157 	memset(m_pins, 0x00, sizeof(m_pins));
158 	change_output_lines();
159 
160 	/* create the timers */
161 	m_scan_timer = timer_alloc(TIMER_SCAN_TICK);
162 	m_scan_timer->adjust(attotime::zero, 0, attotime::from_hz(clock()));
163 
164 	/* register for state saving */
165 	save_item(NAME(m_pins));
166 	save_item(NAME(m_ring11));
167 	save_item(NAME(m_ring8));
168 	save_item(NAME(m_strobe));
169 	save_item(NAME(m_strobe_old));
170 	save_item(NAME(m_parity));
171 	save_item(NAME(m_data));
172 }
173 
174 /*-------------------------------------------------
175     set_input_pin - set an input pin
176 -------------------------------------------------*/
set_input_pin(input_pin_t pin,int data)177 void kr2376_device::set_input_pin( input_pin_t pin, int data )
178 {
179 	data = data ? 1 : 0;
180 	switch ( pin )
181 	{
182 	case KR2376_PII:
183 	case KR2376_DSII:
184 		m_pins[pin] = data;
185 		break;
186 	}
187 }
188 
189 
190 /*-------------------------------------------------
191     get_output_pin - get the status of an output pin
192 -------------------------------------------------*/
get_output_pin(output_pin_t pin)193 int kr2376_device::get_output_pin( output_pin_t pin )
194 {
195 	return m_pins[pin];
196 }
197 
198 
change_output_lines()199 void kr2376_device::change_output_lines()
200 {
201 	if (m_strobe != m_strobe_old)
202 	{
203 		m_strobe_old = m_strobe;
204 
205 		if (m_strobe) // strobe 0 --> 1 transition
206 		{
207 			/* update parity */
208 			m_pins[KR2376_PO] = m_parity ^ m_pins[KR2376_PII];
209 		}
210 		m_pins[KR2376_SO] = m_strobe ^ m_pins[KR2376_DSII];
211 		m_write_strobe(m_strobe ^ m_pins[KR2376_DSII]);
212 	}
213 }
214 
clock_scan_counters()215 void kr2376_device::clock_scan_counters()
216 {
217 	/* ring counters inhibited while strobe active */
218 	if (!m_strobe)
219 	{
220 		m_ring11++;
221 		if (m_ring11 == 11)
222 		{
223 			m_ring11 = 0;
224 			m_ring8++;
225 			if (m_ring8 == 8)
226 				m_ring8 = 0;
227 		}
228 	}
229 }
230 
detect_keypress()231 void kr2376_device::detect_keypress()
232 {
233 	if (m_read_x[m_ring8]() == (1 << m_ring11))
234 	{
235 		m_strobe = 1;
236 		/*  strobe 0->1 transition, encode char and update parity */
237 		if (!m_strobe_old)
238 		{
239 			int i;
240 			int parbit;
241 			int shift = m_read_shift();
242 			int control = m_read_control();
243 			int table = 0;
244 
245 			if (shift)
246 				table = 1;
247 			else if (control)
248 				table = 2;
249 
250 			m_data = key_codes(table, m_ring8, m_ring11);
251 
252 			/* Compute ODD parity */
253 			m_parity = m_data;
254 			parbit = 0;
255 			for (i=0; i<8; i++)
256 				parbit ^= (m_parity >> i) & 1;
257 			m_parity = parbit;
258 		}
259 	}
260 	else
261 	{
262 		m_strobe = 0;
263 	}
264 }
265 
device_timer(emu_timer & timer,device_timer_id id,int param,void * ptr)266 void kr2376_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
267 {
268 	switch (id)
269 		{
270 			case TIMER_SCAN_TICK:
271 				change_output_lines();
272 				clock_scan_counters();
273 				detect_keypress();
274 			break;
275 		}
276 }
277 
278 /* Keyboard Data */
279 
data_r()280 uint8_t kr2376_device::data_r()
281 {
282 	if (m_pins[KR2376_DSII])
283 		return m_data ^ 0xff;
284 	else
285 		return m_data;
286 }
287