summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/imagedev/cartslot.c
blob: 632da01ce87af62ba9f718ec639f0f654b615179 (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
/**********************************************************************

    cartslot.c

    Cartridge device

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

#include <ctype.h>
#include "emu.h"
#include "cartslot.h"


// device type definition
const device_type CARTSLOT = &device_creator<cartslot_image_device>;

//-------------------------------------------------
//  cartslot_image_device - constructor
//-------------------------------------------------

cartslot_image_device::cartslot_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
    : device_t(mconfig, CARTSLOT, "Cartslot", tag, owner, clock),
	  device_image_interface(mconfig, *this),
	  m_extensions("bin"),
	  m_interface(NULL),
	  m_must_be_loaded(0),
	  m_device_start(NULL),
	  m_device_load(NULL),
	  m_device_unload(NULL),
	  m_device_partialhash(NULL),
	  m_device_displayinfo(NULL)
{

}

//-------------------------------------------------
//  cartslot_image_device - destructor
//-------------------------------------------------

cartslot_image_device::~cartslot_image_device()
{
}

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

void cartslot_image_device::device_config_complete()
{
	// set brief and instance name
	update_names();
}

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

/*-------------------------------------------------
    load_cartridge
-------------------------------------------------*/

int cartslot_image_device::load_cartridge(const rom_entry *romrgn, const rom_entry *roment, bool load)
{
	const char *region;
	const char *type;
	UINT32 flags;
	offs_t offset, size, read_length, pos = 0, len;
	UINT8 *ptr;
	UINT8 clear_val;
	int datawidth, littleendian, i, j;
	device_t *cpu;

	astring regiontag;
	device().siblingtag(regiontag, ROMREGION_GETTAG(romrgn));
	region = regiontag.cstr();
	offset = ROM_GETOFFSET(roment);
	size = ROM_GETLENGTH(roment);
	flags = ROM_GETFLAGS(roment);
	ptr = ((UINT8 *) device().machine().region(region)->base()) + offset;

	if (load)
	{
		if (software_entry() == NULL)
		{
			/* must this be full size */
			if (flags & ROM_FULLSIZE)
			{
				if (length() != size)
					return IMAGE_INIT_FAIL;
			}

			/* read the ROM */
			pos = read_length = fread(ptr, size);

			/* reset the ROM to the initial point. */
			/* eventually, we could add a flag to allow the ROM to continue instead of restarting whenever a new cart region is present */
			fseek(0, SEEK_SET);
		}
		else
		{
			/* must this be full size */
			if (flags & ROM_FULLSIZE)
			{
				if (get_software_region_length("rom") != size)
					return IMAGE_INIT_FAIL;
			}

			/* read the ROM */
			pos = read_length = get_software_region_length("rom");
			memcpy(ptr, get_software_region("rom"), read_length);
		}

		/* do we need to mirror the ROM? */
		if (flags & ROM_MIRROR)
		{
			while(pos < size)
			{
				len = MIN(read_length, size - pos);
				memcpy(ptr + pos, ptr, len);
				pos += len;
			}
		}

		/* postprocess this region */
		type = regiontag.cstr();
		littleendian = ROMREGION_ISLITTLEENDIAN(romrgn);
		datawidth = ROMREGION_GETWIDTH(romrgn) / 8;

		/* if the region is inverted, do that now */
		device_memory_interface *memory;
		cpu = device().machine().device(type);
		if (cpu!=NULL && cpu->interface(memory))
		{
			datawidth = cpu->memory().space_config(AS_PROGRAM)->m_databus_width / 8;
			littleendian = (cpu->memory().space_config()->m_endianness == ENDIANNESS_LITTLE);
		}

		/* swap the endianness if we need to */
#ifdef LSB_FIRST
		if (datawidth > 1 && !littleendian)
#else
		if (datawidth > 1 && littleendian)
#endif
		{
			for (i = 0; i < size; i += datawidth)
			{
				UINT8 temp[8];
				memcpy(temp, &ptr[i], datawidth);
				for (j = datawidth - 1; j >= 0; j--)
					ptr[i + j] = temp[datawidth - 1 - j];
			}
		}
	}

	/* clear out anything that remains */
	if (!(flags & ROM_NOCLEAR))
	{
		clear_val = (flags & ROM_FILL_FF) ? 0xFF : 0x00;
		memset(ptr + pos, clear_val, size - pos);
	}
	return IMAGE_INIT_PASS;
}



/*-------------------------------------------------
    process_cartridge
-------------------------------------------------*/

int cartslot_image_device::process_cartridge(bool load)
{
	const rom_entry *romrgn, *roment;
	int result = 0;

	device_iterator deviter(device().mconfig().root_device());
	for (device_t *device = deviter.first(); device != NULL; device = deviter.next())
		for (romrgn = rom_first_region(*device); romrgn != NULL; romrgn = rom_next_region(romrgn))
		{
			roment = romrgn + 1;
			while(!ROMENTRY_ISREGIONEND(roment))
			{
				if (ROMENTRY_GETTYPE(roment) == ROMENTRYTYPE_CARTRIDGE)
				{
					astring regiontag;
					this->device().siblingtag(regiontag, roment->_hashdata);

					if (strcmp(regiontag.cstr(),this->device().tag())==0)
					{
						result |= load_cartridge(romrgn, roment, load);

						/* if loading failed in any cart region, stop loading */
						if (result)
							return result;
					}
				}
				roment++;
			}
		}

	return IMAGE_INIT_PASS;
}

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

void cartslot_image_device::device_start()
{

	/* if this cartridge has a custom DEVICE_START, use it */
	if (m_device_start != NULL)
	{
		(*m_device_start)(this);
	}
}


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

bool cartslot_image_device::call_load()
{
	/* if this cartridge has a custom DEVICE_IMAGE_LOAD, use it */
	if (m_device_load != NULL)
		return (*m_device_load)(*this);

	/* otherwise try the normal route */
	return process_cartridge(true);
}


/*-------------------------------------------------
    call_unload
-------------------------------------------------*/
void cartslot_image_device::call_unload()
{
	/* if this cartridge has a custom DEVICE_IMAGE_UNLOAD, use it */
	if (m_device_unload != NULL)
	{
		(*m_device_unload)(*this);
		return;
	}
	process_cartridge(false);
}