summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/devcb.c
blob: bcbf0e4ea34a94e42e65424576da27ed8e2d6782 (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
/***************************************************************************

    devcb.c

    Device callback interface helpers.

    Copyright Nicola Salmoria and the MAME Team.
    Visit http://mamedev.org for licensing and usage restrictions.

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

#include "devcb.h"
#include "driver.h"



/***************************************************************************
    STATIC-TO-LIVE CONVERSION
***************************************************************************/

/*-------------------------------------------------
    devcb_resolve_read_line - convert a static 
    read line definition to a live definition
-------------------------------------------------*/

static READ_LINE_DEVICE_HANDLER( trampoline_read_port_to_read_line )
{
	return (input_port_read_direct((const input_port_config *)device) & 1) ? ASSERT_LINE : CLEAR_LINE;
}

static READ_LINE_DEVICE_HANDLER( trampoline_read8_to_read_line )
{
	const devcb_resolved_read_line *resolved = (const devcb_resolved_read_line *)device;
	return ((*resolved->real.readdevice)(resolved->realtarget, 0) & 1) ? ASSERT_LINE : CLEAR_LINE;
}

void devcb_resolve_read_line(devcb_resolved_read_line *resolved, const devcb_read_line *config, const device_config *device)
{
	/* reset the resolved structure */
	memset(resolved, 0, sizeof(*resolved));

	/* input port handlers */
	if (config->type == DEVCB_TYPE_INPUT)
	{
		resolved->target = input_port_by_tag(device->machine->portconfig, config->tag);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_read_line: unable to find input port '%s' (requested by %s '%s')", config->tag, device_get_name(device), device->tag);
		resolved->read = trampoline_read_port_to_read_line;
	}
	
	/* address space handlers */
	else if (config->type >= DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM) && config->type < DEVCB_TYPE_MEMORY(ADDRESS_SPACES) && config->readspace != NULL)
	{
		FPTR space = (FPTR)config->type - (FPTR)DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM);
		const device_config *cpu = cputag_get_cpu(device->machine, config->tag);

		if (cpu == NULL)
			fatalerror("devcb_resolve_read_line: unable to find CPU '%s' (requested by %s '%s')", config->tag, device_get_name(device), device->tag);
		resolved->target = resolved;
		resolved->read = trampoline_read8_to_read_line;
		resolved->realtarget = memory_find_address_space(cpu, space);
		if (resolved->realtarget == NULL)
			fatalerror("devcb_resolve_read_line: unable to find CPU '%s' %s space (requested by %s '%s')", config->tag, address_space_names[space], device_get_name(device), device->tag);
		resolved->real.readspace = config->readspace;
	}
	
	/* device handlers */
	else if (config->type != NULL && (config->readline != NULL || config->readdevice != NULL))
	{
		resolved->target = (config->type == DEVCB_TYPE_SELF) ? device : devtag_get_device(device->machine, config->type, config->tag);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_read_line: unable to find %s '%s' (requested by %s '%s')", devtype_get_name(config->type), config->tag, device_get_name(device), device->tag);
	
		/* read_line to read_line is direct */
		if (config->readline != NULL)
			resolved->read = config->readline;
		
		/* read_line to handler goes through a trampoline */
		else
		{
			resolved->realtarget = resolved->target;
			resolved->real.readdevice = config->readdevice;
			resolved->target = resolved;
			resolved->read = trampoline_read8_to_read_line;
		}
	}
}


/*-------------------------------------------------
    devcb_resolve_write_line - convert a static 
    write line definition to a live definition
-------------------------------------------------*/

static WRITE_LINE_DEVICE_HANDLER( trampoline_write8_to_write_line )
{
	const devcb_resolved_write_line *resolved = (const devcb_resolved_write_line *)device;
	(*resolved->real.writedevice)(resolved->realtarget, 0, state);
}

void devcb_resolve_write_line(devcb_resolved_write_line *resolved, const devcb_write_line *config, const device_config *device)
{
	/* reset the resolved structure */
	memset(resolved, 0, sizeof(*resolved));

	/* address space handlers */
	if (config->type >= DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM) && config->type < DEVCB_TYPE_MEMORY(ADDRESS_SPACES) && config->writespace != NULL)
	{
		FPTR space = (FPTR)config->type - (FPTR)DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM);
		const device_config *cpu = cputag_get_cpu(device->machine, config->tag);

		if (cpu == NULL)
			fatalerror("devcb_resolve_write_line: unable to find CPU '%s' (requested by %s '%s')", config->tag, device_get_name(device), device->tag);
		resolved->target = resolved;
		resolved->write = trampoline_write8_to_write_line;
		resolved->realtarget = memory_find_address_space(cpu, space);
		if (resolved->realtarget == NULL)
			fatalerror("devcb_resolve_write_line: unable to find CPU '%s' %s space (requested by %s '%s')", config->tag, address_space_names[space], device_get_name(device), device->tag);
		resolved->real.writespace = config->writespace;
	}
	
	/* device handlers */
	else if (config->type != NULL && (config->writeline != NULL || config->writedevice != NULL))
	{
		resolved->target = (config->type == DEVCB_TYPE_SELF) ? device : devtag_get_device(device->machine, config->type, config->tag);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_write_line: unable to find %s '%s' (requested by %s '%s')", devtype_get_name(config->type), config->tag, device_get_name(device), device->tag);
	
		/* write_line to write_line is direct */
		if (config->writeline != NULL)
			resolved->write = config->writeline;
		
		/* write_line to handler goes through a trampoline */
		else
		{
			resolved->realtarget = resolved->target;
			resolved->real.writedevice = config->writedevice;
			resolved->target = resolved;
			resolved->write = trampoline_write8_to_write_line;
		}
	}
}


/*-------------------------------------------------
    devcb_resolve_read8 - convert a static 
    8-bit read definition to a live definition
-------------------------------------------------*/

static READ8_DEVICE_HANDLER( trampoline_read_port_to_read8 )
{
	return input_port_read_direct((const input_port_config *)device);
}

static READ8_DEVICE_HANDLER( trampoline_read_line_to_read8 )
{
	const devcb_resolved_read8 *resolved = (const devcb_resolved_read8 *)device;
	return (*resolved->real.readline)(resolved->realtarget);
}

void devcb_resolve_read8(devcb_resolved_read8 *resolved, const devcb_read8 *config, const device_config *device)
{
	/* reset the resolved structure */
	memset(resolved, 0, sizeof(*resolved));

	/* input port handlers */
	if (config->type == DEVCB_TYPE_INPUT)
	{
		resolved->target = input_port_by_tag(device->machine->portconfig, config->tag);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_read8: unable to find input port '%s' (requested by %s '%s')", config->tag, device_get_name(device), device->tag);
		resolved->read = trampoline_read_port_to_read8;
	}
	
	/* address space handlers */
	else if (config->type >= DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM) && config->type < DEVCB_TYPE_MEMORY(ADDRESS_SPACES) && config->readspace != NULL)
	{
		FPTR space = (FPTR)config->type - (FPTR)DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM);
		const device_config *cpu = cputag_get_cpu(device->machine, config->tag);

		if (cpu == NULL)
			fatalerror("devcb_resolve_read8: unable to find CPU '%s' (requested by %s '%s')", config->tag, device_get_name(device), device->tag);
		resolved->target = memory_find_address_space(cpu, space);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_read8: unable to find CPU '%s' %s space (requested by %s '%s')", config->tag, address_space_names[space], device_get_name(device), device->tag);
		resolved->read = (read8_device_func)config->readspace;
	}
	
	/* device handlers */
	else if (config->type != NULL && (config->readline != NULL || config->readdevice != NULL))
	{
		resolved->target = (config->type == DEVCB_TYPE_SELF) ? device : devtag_get_device(device->machine, config->type, config->tag);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_read8: unable to find %s '%s' (requested by %s '%s')", devtype_get_name(config->type), config->tag, device_get_name(device), device->tag);
	
		/* read8 to read8 is direct */
		if (config->readdevice != NULL)
			resolved->read = config->readdevice;
		
		/* read8 to read_line goes through a trampoline */
		else
		{
			resolved->realtarget = resolved->target;
			resolved->real.readline = config->readline;
			resolved->target = resolved;
			resolved->read = trampoline_read_line_to_read8;
		}
	}
}


/*-------------------------------------------------
    devcb_resolve_write8 - convert a static 
    8-bit write definition to a live definition
-------------------------------------------------*/

static WRITE8_DEVICE_HANDLER( trampoline_write_line_to_write8 )
{
	const devcb_resolved_write8 *resolved = (const devcb_resolved_write8 *)device;
	(*resolved->real.writeline)(resolved->realtarget, (data & 1) ? ASSERT_LINE : CLEAR_LINE);
}

void devcb_resolve_write8(devcb_resolved_write8 *resolved, const devcb_write8 *config, const device_config *device)
{
	/* reset the resolved structure */
	memset(resolved, 0, sizeof(*resolved));

	/* address space handlers */
	if (config->type >= DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM) && config->type < DEVCB_TYPE_MEMORY(ADDRESS_SPACES) && config->writespace != NULL)
	{
		FPTR space = (FPTR)config->type - (FPTR)DEVCB_TYPE_MEMORY(ADDRESS_SPACE_PROGRAM);
		const device_config *cpu = cputag_get_cpu(device->machine, config->tag);

		if (cpu == NULL)
			fatalerror("devcb_resolve_write8: unable to find CPU '%s' (requested by %s '%s')", config->tag, device_get_name(device), device->tag);
		resolved->target = memory_find_address_space(cpu, space);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_write8: unable to find CPU '%s' %s space (requested by %s '%s')", config->tag, address_space_names[space], device_get_name(device), device->tag);
		resolved->write = (write8_device_func)config->writespace;
	}
	
	/* device handlers */
	else if (config->type != NULL && (config->writeline != NULL || config->writedevice != NULL))
	{
		resolved->target = (config->type == DEVCB_TYPE_SELF) ? device : devtag_get_device(device->machine, config->type, config->tag);
		if (resolved->target == NULL)
			fatalerror("devcb_resolve_write8: unable to find %s '%s' (requested by %s '%s')", devtype_get_name(config->type), config->tag, device_get_name(device), device->tag);
	
		/* write8 to write8 is direct */
		if (config->writedevice != NULL)
			resolved->write = config->writedevice;
		
		/* write8 to write_line goes through a trampoline */
		else
		{
			resolved->realtarget = resolved->target;
			resolved->real.writeline = config->writeline;
			resolved->target = resolved;
			resolved->write = trampoline_write_line_to_write8;
		}
	}
}