1 // license:BSD-3-Clause
2 // copyright-holders:smf
3 #include "emu.h"
4 #include "xvd701.h"
5 
jvc_xvd701_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)6 jvc_xvd701_device::jvc_xvd701_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
7 	: device_t(mconfig, JVC_XVD701, tag, owner, clock),
8 	device_serial_interface(mconfig, *this),
9 	device_rs232_port_interface(mconfig, *this),
10 	m_response_index(0),
11 	m_timer_response(nullptr)
12 {
13 }
14 
device_add_mconfig(machine_config & config)15 void jvc_xvd701_device::device_add_mconfig(machine_config &config)
16 {
17 }
18 
INPUT_PORTS_START(xvd701)19 static INPUT_PORTS_START(xvd701)
20 INPUT_PORTS_END
21 
22 ioport_constructor jvc_xvd701_device::device_input_ports() const
23 {
24 	return INPUT_PORTS_NAME(xvd701);
25 }
26 
device_start()27 void jvc_xvd701_device::device_start()
28 {
29 	int startbits = 1;
30 	int databits = 8;
31 	parity_t parity = PARITY_ODD;
32 	stop_bits_t stopbits = STOP_BITS_1;
33 
34 	set_data_frame(startbits, databits, parity, stopbits);
35 
36 	int txbaud = 9600;
37 	set_tra_rate(txbaud);
38 
39 	int rxbaud = 9600;
40 	set_rcv_rate(rxbaud);
41 
42 	output_rxd(1);
43 
44 	// TODO: make this configurable
45 	output_dcd(0);
46 	output_dsr(0);
47 	output_ri(0);
48 	output_cts(0);
49 
50 	m_timer_response = timer_alloc(TIMER_RESPONSE);
51 }
52 
device_reset()53 void jvc_xvd701_device::device_reset()
54 {
55 	memset(m_command, 0, sizeof(m_command));
56 
57 	m_response_index = sizeof(m_response);
58 }
59 
device_timer(emu_timer & timer,device_timer_id id,int param,void * ptr)60 void jvc_xvd701_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
61 {
62 	switch (id)
63 	{
64 	case TIMER_RESPONSE:
65 		send_response();
66 		break;
67 
68 	default:
69 		break;
70 	}
71 }
72 
tra_callback()73 void jvc_xvd701_device::tra_callback()
74 {
75 	output_rxd(transmit_register_get_data_bit());
76 }
77 
tra_complete()78 void jvc_xvd701_device::tra_complete()
79 {
80 	m_timer_response->adjust(attotime::from_msec(100));
81 }
82 
sum(unsigned char * buffer,int length)83 unsigned char jvc_xvd701_device::sum(unsigned char *buffer, int length)
84 {
85 	int sum = 0;
86 
87 	for (int i = 0; i < length; i++)
88 		sum += buffer[i];
89 
90 	return sum & 0x7f;
91 }
92 
send_response()93 void jvc_xvd701_device::send_response()
94 {
95 	if (m_response_index < sizeof(m_response) && is_transmit_register_empty())
96 	{
97 //      printf("sending %02x\n", m_response[m_response_index]);
98 		transmit_register_setup(m_response[m_response_index++]);
99 	}
100 }
101 
rcv_complete()102 void jvc_xvd701_device::rcv_complete()
103 {
104 	receive_register_extract();
105 
106 	for (int i = 0; i < sizeof(m_command) - 1; i++)
107 		m_command[i] = m_command[i + 1];
108 
109 	m_command[sizeof(m_command) - 1] = get_received_char();
110 
111 	if (m_command[0] == 0xff &&
112 		m_command[1] == 0xff &&
113 		m_command[2] == 0x21 &&
114 		sum(m_command, sizeof(m_command)) == 0)
115 	{
116 		// printf("xvd701");
117 
118 		//for (int i = 0; i < sizeof(m_command); i++)
119 		//  printf(" %02x", m_command[i]);
120 
121 		//printf("\n");
122 
123 		// FF FF 21 3E 40 70 00 00 00 00 73 DEVICE ON
124 		// FF FF 21 3E 40 60 00 00 00 00 03 DEVICE OFF
125 		// FF FF 21 0C 44 60 00 00 00 00 31 STOP
126 		// FF FF 21 0C 43 75 00 00 00 00 1D PLAY
127 		// FF FF 21 0C 43 6D 00 00 00 00 25 PAUSE
128 		// FF FF 21 0C 50 20 00 00 00 00 63 SEEK TO SPECIFIC CHAPTER
129 		// FF FF 21 0C 50 73 00 00 00 00 12 FF (SEEK TO NEXT CHAPTER)
130 		// FF FF 21 0C 50 61 00 00 00 00 24 PREV (SEEK TO PREVIOUS CHAPTER)
131 
132 		m_response[0] = 0xff;
133 		m_response[1] = 0xfe;
134 		m_response[2] = 0x7f;
135 		m_response[3] = 0x7e;
136 		m_response[4] = 0x7d;
137 		m_response[5] = 0x7c;
138 		m_response[6] = 0x7b;
139 		m_response[7] = 0x7a;
140 		m_response[8] = 0x79;
141 		m_response[9] = 0x78;
142 		m_response[10] = 0x77;
143 		m_response_index = 0;
144 
145 		m_timer_response->adjust(attotime::from_msec(100));
146 	}
147 }
148 
149 DEFINE_DEVICE_TYPE(JVC_XVD701, jvc_xvd701_device, "xvd701", "JVC XV-D701")
150