summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/imagedev/midiin.c
blob: e32a580bc30ac16b3afc161f477e4188fb0623c0 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*********************************************************************

    midiin.c

    MIDI In image device and serial transmitter

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

#include "emu.h"
#include "midiin.h"

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

const device_type MIDIIN = &device_creator<midiin_device>;

/*-------------------------------------------------
    ctor
-------------------------------------------------*/

midiin_device::midiin_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, MIDIIN, "MIDI In image device", tag, owner, clock, "midiin", __FILE__),
		device_image_interface(mconfig, *this),
			device_serial_interface(mconfig, *this)
{
}

/*-------------------------------------------------
    device_start
-------------------------------------------------*/

void midiin_device::device_start()
{
	m_input_func.resolve(m_input_callback, *this);
	m_timer = timer_alloc(0);
	m_midi = NULL;
	m_timer->enable(false);
}

void midiin_device::device_reset()
{
	m_tx_busy = false;
	m_xmit_read = m_xmit_write = 0;

	// we don't Rx, we Tx at 31250 8-N-1
	set_data_frame(8, 1, PARITY_NONE, false);
	set_rcv_rate(0);
	set_tra_rate(31250);
}

/*-------------------------------------------------
    device_config_complete
-------------------------------------------------*/

void midiin_device::device_config_complete(void)
{
	const midiin_config *intf = reinterpret_cast<const midiin_config *>(static_config());
	if(intf != NULL)
	{
		*static_cast<midiin_config *>(this) = *intf;
	}
	else
	{
		memset(&m_input_callback, 0, sizeof(m_input_callback));
	}
	update_names();
}

/*-------------------------------------------------
    device_timer
-------------------------------------------------*/

void midiin_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	if (id) {
		device_serial_interface::device_timer(timer, id, param, ptr);
		return;
	}

	UINT8 buf[8192*4];
	int bytesRead;

	if (m_midi == NULL) {
		return;
	}

	while (osd_poll_midi_channel(m_midi))
	{
		bytesRead = osd_read_midi_channel(m_midi, buf);

		if (bytesRead > 0)
		{
			for (int i = 0; i < bytesRead; i++)
			{
				xmit_char(buf[i]);
			}
		}
	}
}

/*-------------------------------------------------
    call_load
-------------------------------------------------*/

bool midiin_device::call_load(void)
{
	m_midi = osd_open_midi_input(filename());

	if (m_midi == NULL)
	{
		return IMAGE_INIT_FAIL;
	}

	m_timer->adjust(attotime::from_hz(1500), 0, attotime::from_hz(1500));
	m_timer->enable(true);
	return IMAGE_INIT_PASS;
}

/*-------------------------------------------------
    call_unload
-------------------------------------------------*/

void midiin_device::call_unload(void)
{
	if (m_midi)
	{
		osd_close_midi_channel(m_midi);
	}
		m_timer->enable(false);
		m_midi = NULL;
}

void midiin_device::tra_complete()
{
	// is there more waiting to send?
	if (m_xmit_read != m_xmit_write)
	{
//      printf("tx1 %02x\n", m_xmitring[m_xmit_read]);
		transmit_register_setup(m_xmitring[m_xmit_read++]);
		if (m_xmit_read >= XMIT_RING_SIZE)
		{
			m_xmit_read = 0;
		}
	}
	else
	{
		m_tx_busy = false;
	}
}

void midiin_device::tra_callback()
{
	int bit = transmit_register_get_data_bit();
	m_input_func(bit);
}

void midiin_device::xmit_char(UINT8 data)
{
//  printf("MIDI in: xmit %02x\n", data);

	// if tx is busy it'll pick this up automatically when it completes
	if (!m_tx_busy)
	{
		m_tx_busy = true;
//      printf("tx0 %02x\n", data);
		transmit_register_setup(data);
	}
	else
	{
		// tx is busy, it'll pick this up next time
		m_xmitring[m_xmit_write++] = data;
		if (m_xmit_write >= XMIT_RING_SIZE)
		{
			m_xmit_write = 0;
		}
	}
}

void midiin_device::input_callback(UINT8 state)
{
}