summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/imagedev/serial.c
blob: 761f79449fd99d7bf9914945ea5b0cdce9233fda (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/****************************************************************************

    serial.c

    Code for handling serial port image devices

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

#include "emu.h"
#include "serial.h"


// device type definition
const device_type SERIAL = &device_creator<serial_image_device>;

//-------------------------------------------------
//  serial_image_device - constructor
//-------------------------------------------------

serial_image_device::serial_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, SERIAL, "Serial", tag, owner, clock),
		device_serial_interface(mconfig, *this),
		device_image_interface(mconfig, *this)
{

}

//-------------------------------------------------
//  serial_image_device - destructor
//-------------------------------------------------

serial_image_device::~serial_image_device()
{
}

//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void serial_image_device::device_config_complete()
{
	// inherit a copy of the static data
	const serial_image_interface *intf = reinterpret_cast<const serial_image_interface *>(static_config());
	if (intf != NULL)
		*static_cast<serial_image_interface *>(this) = *intf;

	// or initialize to defaults if none provided
	else
	{
		m_baud_rate = 2400;
		m_data_bits = 8;
		m_stop_bits = 2;
		m_parity = SERIAL_PARITY_NONE;
		m_transmit_on_start = FALSE;
		m_tag_connected = NULL;
	}

	// set brief and instance name
	update_names();
}

static TIMER_CALLBACK(serial_device_baud_rate_callback)
{
	reinterpret_cast<serial_image_device *>(ptr)->timer_callback();
}

/*-------------------------------------------------
    input_callback - called when other side
    has updated state
-------------------------------------------------*/
void serial_image_device::input_callback(UINT8 state)
{
	m_input_state = state;
}

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

void serial_image_device::device_start()
{
	set_data_frame(m_data_bits, m_stop_bits,m_parity);

	m_timer = machine().scheduler().timer_alloc(FUNC(serial_device_baud_rate_callback), this);

	/* signal to other end it is clear to send! */
	/* data is initially high state */
	/* set /rts */
	m_connection_state |= SERIAL_STATE_RTS;
	/* signal to other end data is ready to be accepted */
	/* set /dtr */
	m_connection_state |= SERIAL_STATE_DTR;

	set_out_data_bit(1);
	serial_connection_out();
	transmit_register_reset();
	receive_register_reset();

	device_serial_interface *intf = NULL;
	if (m_tag_connected) {
		device_t *dev = machine().device(m_tag_connected);
		if (dev!=NULL && dev->interface(intf)) {
			intf->connect(this);
		}
	}
}

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

/* reset position in stream */
void serial_image_device::data_stream_reset(serial_data_stream *stream)
{
	/* reset byte offset */
	stream->ByteCount= 0;
	/* reset bit count */
	stream->BitCount = 0;
}

/* free stream */
void serial_image_device::data_stream_free(serial_data_stream *stream)
{
	if (stream->pData!=NULL)
	{
		free(stream->pData);
		stream->pData = NULL;
	}
	stream->DataLength = 0;
}

/* initialise stream */
void serial_image_device::data_stream_init(serial_data_stream *stream, unsigned char *pData, unsigned long DataLength)
{
	stream->pData = pData;
	stream->DataLength = DataLength;
	data_stream_reset(stream);
}

/* get a bit from input stream */

int serial_image_device::data_stream_get_data_bit_from_data_byte(serial_data_stream *stream)
{
	int data_bit;
	int data_byte;

	if (stream->ByteCount<stream->DataLength)
	{
		/* get data from buffer */
		data_byte= stream->pData[stream->ByteCount];
	}
	else
	{
		/* over end of buffer, so return 0 */
		data_byte= 0;
	}

	/* get bit from data */
	data_bit = (data_byte>>(7-stream->BitCount)) & 0x01;

	/* update bit count */
	stream->BitCount++;
	/* ripple overflow onto byte count */
	stream->ByteCount+=stream->BitCount>>3;
	/* lock bit count into range */
	stream->BitCount &=0x07;

	/* do not let it read over end of data */
	if (stream->ByteCount>=stream->DataLength)
	{
		stream->ByteCount = stream->DataLength-1;
	}

	return data_bit;
}


void serial_image_device::set_transmit_state(int state)
{
	int previous_state = m_transmit_state;

	m_transmit_state = state;

	if ((state^previous_state)!=0)
	{
		if (state)
		{
			/* start timer */
			m_timer->adjust(attotime::zero, 0, attotime::from_hz(m_baud_rate));
		}
		else
		{
			/* remove timer */
			m_timer->reset();
		}
	}

}

void serial_image_device::sent_char()
{
	int bit;
	UINT8 data_byte;

	/* generate byte to transmit */
	data_byte = 0;
	for (int i=0; i< m_data_bits; i++)
	{
		data_byte = data_byte<<1;
		bit = data_stream_get_data_bit_from_data_byte(&m_transmit);
		data_byte = data_byte|bit;
	}
	/* setup register */
	transmit_register_setup(data_byte);

	logerror("serial device transmitted char: %02x\n",data_byte);
}

/*-------------------------------------------------
    timer_callback
-------------------------------------------------*/
void serial_image_device::timer_callback()
{
	/* receive data into receive register */
	receive_register_update_bit(get_in_data_bit());

	if (is_receive_register_full())
	{
		//logerror("SERIAL DEVICE\n");
		receive_register_extract();

		logerror("serial device receive char: %02x\n",get_received_char());
	}

	/* is transmit empty? */
	if (is_transmit_register_empty())
	{
		/* char has been sent, execute callback */
		sent_char();
	}

	/* other side says it is clear to send? */
	if (m_connection_state & SERIAL_STATE_CTS)
	{
		/* send bit */
		transmit_register_send_bit();
	}
}

int serial_image_device::load_internal(unsigned char **ptr, int *pDataSize)
{
	int datasize;
	unsigned char *data;

	/* get file size */
	datasize = length();

	if (datasize!=0)
	{
		/* malloc memory for this data */
		data = (unsigned char *)malloc(datasize);

		if (data!=NULL)
		{
			/* read whole file */
			fread(data, datasize);

			*ptr = data;
			*pDataSize = datasize;

			logerror("File loaded!\r\n");

			/* ok! */
			return 1;
		}
	}
	return 0;
}

/*-------------------------------------------------
    call_load
-------------------------------------------------*/
bool serial_image_device::call_load()
{
	int data_length;
	unsigned char *data;
	/* load file and setup transmit data */
	if (load_internal(&data, &data_length))
	{
		data_stream_init(&m_transmit, data, data_length);
		set_transmit_state(m_transmit_on_start ? 1 :0);
		return IMAGE_INIT_PASS;
	}

	return IMAGE_INIT_FAIL;
}


/*-------------------------------------------------
    call_unload
-------------------------------------------------*/
void serial_image_device::call_unload()
{
	/* stop transmit */
	set_transmit_state(0);

	/* free streams */
	data_stream_free(&m_transmit);
	data_stream_free(&m_receive);
}