summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/8257dma.c
blob: f8a322cad1ccf40cb46a132e1363f4a3bee4882a (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**********************************************************************

    8257 DMA interface and emulation

    For datasheet http://www.threedee.com/jcm/library/index.html

    2008/05     Miodrag Milanovic

        - added support for autoload mode
        - fixed bug in calculating count

    2007/11     couriersud

        - architecture copied from 8237 DMA
        - significant changes to implementation

    The DMA works like this:

    1.  The device asserts the DRQn line
    2.  The DMA clears the TC (terminal count) line
    3.  The DMA asserts the CPU's HRQ (halt request) line
    4.  Upon acknowledgement of the halt, the DMA will let the device
        know that it needs to send information by asserting the DACKn
        line
    5.  The DMA will read the byte from the device
    6.  The device clears the DRQn line
    7.  The DMA clears the CPU's HRQ line
    8.  (steps 3-7 are repeated for every byte in the chain)

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

#include "driver.h"
#include "memconv.h"
#include "8257dma.h"

typedef struct _dma8257_t dma8257_t;
struct _dma8257_t
{
	const dma8257_interface *intf;
	emu_timer *timer;
	emu_timer *msbflip_timer;

	UINT16 registers[DMA8257_NUM_CHANNELS*2];

	UINT16 address[DMA8257_NUM_CHANNELS];
	UINT16 count[DMA8257_NUM_CHANNELS];
	UINT8  rwmode[DMA8257_NUM_CHANNELS];

	UINT8 mode;
	UINT8 rr;

	UINT8 msb;
	UINT8 drq;

	/* bits  0- 3 :  Terminal count for channels 0-3 */
	UINT8 status;
};

#define DMA_MODE_AUTOLOAD(mode)		((mode) & 0x80)
#define DMA_MODE_TCSTOP(mode)		((mode) & 0x40)
#define DMA_MODE_EXWRITE(mode)		((mode) & 0x20)
#define DMA_MODE_ROTPRIO(mode)		((mode) & 0x10)
#define DMA_MODE_CH_EN(mode, chan)	((mode) & (1 << (chan)))

static TIMER_CALLBACK( dma8257_timerproc );
static TIMER_CALLBACK( dma8257_msbflip_timerproc );
static void dma8257_update_status(running_machine *machine, dma8257_t *dma8257);


/* ----------------------------------------------------------------------- */

INLINE dma8257_t *get_safe_token(const device_config *device) {
	assert( device != NULL );
	assert( device->token != NULL );
	assert( device->type == DMA8257 );
	return ( dma8257_t * ) device->token;
}

static int dma8257_do_operation(running_machine *machine, dma8257_t *dma8257, int channel)
{
	int done;
	UINT8 data;
	UINT8 mode;

	mode = dma8257->rwmode[channel];
	if (dma8257->count[channel] == 0x0000)
	{
		dma8257->status |=  (0x01 << channel);
		if (dma8257->intf->out_tc[channel])
			dma8257->intf->out_tc[channel](machine, 0, ASSERT_LINE);
	}
	switch(mode) {
	case 1:
		data = dma8257->intf->memory_read(machine, dma8257->address[channel]);
		dma8257->intf->channel_write[channel](machine, 0, data);

		dma8257->address[channel]++;
		dma8257->count[channel]--;
		done = (dma8257->count[channel] == 0xFFFF);
		break;

	case 2:
		data = dma8257->intf->channel_read[channel](machine, 0);
		dma8257->intf->memory_write(machine, dma8257->address[channel], data);

		dma8257->address[channel]++;
		dma8257->count[channel]--;
		done = (dma8257->count[channel] == 0xFFFF);
		break;
	case 0: /* verify */
		dma8257->address[channel]++;
		dma8257->count[channel]--;
		done = (dma8257->count[channel] == 0xFFFF);
		break;
	default:
		fatalerror("dma8257_do_operation: invalid mode!\n");
		break;
	}
	if (done)
	{
		if ((channel==2) && DMA_MODE_AUTOLOAD(dma8257->mode)) {
			/* in case of autoload at the end channel 3 info is */
			/* copied to channel 2 info                         */
			dma8257->registers[4] = dma8257->registers[6];
			dma8257->registers[5] = dma8257->registers[7];
		}
		if (dma8257->intf->out_tc[channel])
			dma8257->intf->out_tc[channel](machine, 0, CLEAR_LINE);
	}
	return done;
}



static TIMER_CALLBACK( dma8257_timerproc )
{
	dma8257_t *dma8257 = ptr;
	int i, channel = 0, rr;
	int done;

	rr = DMA_MODE_ROTPRIO(dma8257->mode) ? dma8257->rr : 0;
	for (i = 0; i < DMA8257_NUM_CHANNELS; i++)
	{
		channel = (i + rr) % DMA8257_NUM_CHANNELS;
		if ((dma8257->status & (1 << channel)) == 0)
			if (dma8257->mode & dma8257->drq & (1 << channel))
				break;
	}
	done = dma8257_do_operation(machine, dma8257, channel);

	dma8257->rr = (channel + 1) & 0x03;

	if (done)
	{
		dma8257->drq    &= ~(0x01 << channel);
		dma8257_update_status(machine, dma8257);
		if (DMA_MODE_TCSTOP(dma8257->mode))
			dma8257->mode &= ~(0x01 << channel);
	}
}



static TIMER_CALLBACK( dma8257_msbflip_timerproc )
{
	dma8257_t *dma8257 = ptr;
	dma8257->msb ^= 1;
}



static void dma8257_update_status(running_machine *machine, dma8257_t *dma8257)
{
	UINT16 pending_transfer;
	attotime next;

	/* no transfer is active right now; is there a transfer pending right now? */
	pending_transfer = dma8257->drq & (dma8257->mode & 0x0F);

	if (pending_transfer)
	{
		next = ATTOTIME_IN_HZ(dma8257->intf->clockhz / 4 );
		timer_adjust_periodic(dma8257->timer,
			attotime_zero,
			0,
			/* 1 byte transferred in 4 clock cycles */
			next);
	}
	else
	{
		/* no transfers active right now */
		timer_reset(dma8257->timer, attotime_never);
	}

	/* set the halt line */
	if (dma8257->intf && dma8257->intf->cpunum >= 0)
	{
		cpunum_set_input_line(machine, dma8257->intf->cpunum, INPUT_LINE_HALT,
			pending_transfer ? ASSERT_LINE : CLEAR_LINE);
	}

}



/* ----------------------------------------------------------------------- */

static void prepare_msb_flip(dma8257_t *dma8257)
{
	timer_adjust_oneshot(dma8257->msbflip_timer, attotime_zero, 0);
}



READ8_DEVICE_HANDLER( dma8257_r )
{
	dma8257_t *dma8257 = get_safe_token(device);
	UINT8 data = 0xFF;

	switch(offset) {
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
		/* DMA address/count register */
		data = ( dma8257->registers[offset] >> (dma8257->msb ? 8 : 0) ) & 0xFF;
		prepare_msb_flip(dma8257);
		break;

	case 8:
		/* DMA status register */
		data = (UINT8) dma8257->status;
		/* read resets status ! */
		dma8257->status &= 0xF0;

		break;

	default:
		logerror("8257: Read from register %d.\n", offset);
		data = 0xFF;
		break;
	}
	return data;
}



WRITE8_DEVICE_HANDLER( dma8257_w )
{
	dma8257_t *dma8257 = get_safe_token(device);

	switch(offset) {
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
		/* DMA address/count register */
		if (dma8257->msb)
			dma8257->registers[offset] |= ((UINT16) data) << 8;
		else
			dma8257->registers[offset] = data;

		if (DMA_MODE_AUTOLOAD(dma8257->mode)) {
			/* in case of autoload when inserting channel 2 info */
			/* it is automaticaly copied to channel 3 info       */
			switch(offset) {
				case 4:
				case 5:
					if (dma8257->msb)
						dma8257->registers[offset+2] |= ((UINT16) data) << 8;
					else
						dma8257->registers[offset+2] = data;
			}
		}

		prepare_msb_flip(dma8257);
		break;

	case 8:
		/* DMA mode register */
		dma8257->mode = data;
		break;

	default:
		logerror("8257: Write to register %d.\n", offset);
		break;
	}
}



static TIMER_CALLBACK( dma8257_drq_write_callback )
{
	int channel = param >> 1;
	int state = param & 0x01;
	dma8257_t *dma8257 = ptr;

	/* normalize state */
	if (state)
	{
		dma8257->drq |= 0x01 << channel;
		dma8257->address[channel] =  dma8257->registers[channel * 2];
		dma8257->count[channel] =  dma8257->registers[channel * 2 + 1] & 0x3FFF;
		dma8257->rwmode[channel] =  dma8257->registers[channel * 2 + 1] >> 14;
		/* clear channel TC */
		dma8257->status &= ~(0x01 << channel);
	}
	else
		dma8257->drq &= ~(0x01 << channel);

	dma8257_update_status(machine, dma8257);
}



WRITE8_DEVICE_HANDLER( dma8257_drq_w )
{
	dma8257_t *dma8257 = get_safe_token(device);
	int param = (offset << 1) | (data ? 1 : 0);
	
	timer_call_after_resynch(dma8257, param, dma8257_drq_write_callback);
}


/* ----------------------------------------------------------------------- */

/* device interface */

static DEVICE_START( dma8257 )
{
	dma8257_t *dma8257 = get_safe_token(device);
	char unique_tag[30];

	/* validate arguments */
	assert(device != NULL);
	assert(device->tag != NULL);
	assert(strlen(device->tag) < 20);

	//dma8257->device_type = device_type;
	dma8257->intf = device->static_config;

	dma8257->status = 0x0f;
	dma8257->timer = timer_alloc(dma8257_timerproc, dma8257);
	dma8257->msbflip_timer = timer_alloc(dma8257_msbflip_timerproc, dma8257);

	state_save_combine_module_and_tag(unique_tag, "dma8257", device->tag);

	state_save_register_item_array(unique_tag, 0, dma8257->address);
	state_save_register_item_array(unique_tag, 0, dma8257->count);
	state_save_register_item_array(unique_tag, 0, dma8257->rwmode);
	state_save_register_item_array(unique_tag, 0, dma8257->registers);

	state_save_register_item(unique_tag, 0, dma8257->mode);
	state_save_register_item(unique_tag, 0, dma8257->rr);
	state_save_register_item(unique_tag, 0, dma8257->msb);
	state_save_register_item(unique_tag, 0, dma8257->drq);
	state_save_register_item(unique_tag, 0, dma8257->status);

}


static DEVICE_RESET( dma8257 )
{
	dma8257_t *dma8257 = get_safe_token(device);

	dma8257->status &= 0xf0;
	dma8257->mode = 0;
	dma8257_update_status(device->machine, dma8257 );
}


static DEVICE_SET_INFO( dma8257 )
{
	switch (state)
	{
		/* no parameters to set */
	}
}


DEVICE_GET_INFO( dma8257 )
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:					info->i = sizeof(dma8257_t);				break;
		case DEVINFO_INT_INLINE_CONFIG_BYTES:			info->i = 0;							break;
		case DEVINFO_INT_CLASS:							info->i = DEVICE_CLASS_PERIPHERAL;		break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_SET_INFO:						info->set_info = DEVICE_SET_INFO_NAME(dma8257); break;
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME(dma8257);break;
		case DEVINFO_FCT_STOP:							/* Nothing */							break;
		case DEVINFO_FCT_RESET:							info->reset = DEVICE_RESET_NAME(dma8257);break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							info->s = "DMA8257";					break;
		case DEVINFO_STR_FAMILY:						info->s = "DMA controllers";			break;
		case DEVINFO_STR_VERSION:						info->s = "1.0";						break;
		case DEVINFO_STR_SOURCE_FILE:					info->s = __FILE__;						break;
		case DEVINFO_STR_CREDITS:						info->s = "Copyright Nicola Salmoria and the MAME Team"; break;
	}
}