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
|
/***************************************************************************
image.c
Core image functions and definitions.
Copyright Nicola Salmoria and the MAME Team.
Visit http://mamedev.org for licensing and usage restrictions.
***************************************************************************/
#include <ctype.h>
#include "emu.h"
#include "emuopts.h"
#include "image.h"
#include "config.h"
#include "xmlfile.h"
#include "formats/ioprocs.h"
/* ----------------------------------------------------------------------- */
static int image_fseek_thunk(void *file, INT64 offset, int whence)
{
device_image_interface *image = (device_image_interface *) file;
return image->fseek(offset, whence);
}
static size_t image_fread_thunk(void *file, void *buffer, size_t length)
{
device_image_interface *image = (device_image_interface *) file;
return image->fread(buffer, length);
}
static size_t image_fwrite_thunk(void *file, const void *buffer, size_t length)
{
device_image_interface *image = (device_image_interface *) file;
return image->fwrite(buffer, length);
}
static UINT64 image_fsize_thunk(void *file)
{
device_image_interface *image = (device_image_interface *) file;
return image->length();
}
/* ----------------------------------------------------------------------- */
struct io_procs image_ioprocs =
{
NULL,
image_fseek_thunk,
image_fread_thunk,
image_fwrite_thunk,
image_fsize_thunk
};
/***************************************************************************
INITIALIZATION HELPERS
***************************************************************************/
/*-------------------------------------------------
image_dirs_load - loads image device directory
configuration items
-------------------------------------------------*/
static void image_dirs_load(running_machine &machine, int config_type, xml_data_node *parentnode)
{
xml_data_node *node;
const char *dev_instance;
const char *working_directory;
if ((config_type == CONFIG_TYPE_GAME) && (parentnode != NULL))
{
for (node = xml_get_sibling(parentnode->child, "device"); node; node = xml_get_sibling(node->next, "device"))
{
dev_instance = xml_get_attribute_string(node, "instance", NULL);
if ((dev_instance != NULL) && (dev_instance[0] != '\0'))
{
image_interface_iterator iter(machine.root_device());
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
if (!strcmp(dev_instance, image->instance_name())) {
working_directory = xml_get_attribute_string(node, "directory", NULL);
if (working_directory != NULL)
image->set_working_directory(working_directory);
}
}
}
}
}
}
/*-------------------------------------------------
image_dirs_save - saves out image device
directories to the configuration file
-------------------------------------------------*/
static void image_dirs_save(running_machine &machine, int config_type, xml_data_node *parentnode)
{
xml_data_node *node;
const char *dev_instance;
/* only care about game-specific data */
if (config_type == CONFIG_TYPE_GAME)
{
image_interface_iterator iter(machine.root_device());
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
dev_instance = image->instance_name();
node = xml_add_child(parentnode, "device", NULL);
if (node != NULL)
{
xml_set_attribute(node, "instance", dev_instance);
xml_set_attribute(node, "directory", image->working_directory());
}
}
}
}
/*-------------------------------------------------
write_config - emit current option statuses as
INI files
-------------------------------------------------*/
static int write_config(emu_options &options, const char *filename, const game_driver *gamedrv)
{
char buffer[128];
int retval = 1;
if (gamedrv != NULL)
{
sprintf(buffer, "%s.ini", gamedrv->name);
filename = buffer;
}
emu_file file(options.ini_path(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE);
file_error filerr = file.open(filename);
if (filerr == FILERR_NONE)
{
astring inistring;
options.output_ini(inistring);
file.puts(inistring);
retval = 0;
}
return retval;
}
/*-------------------------------------------------
image_options_extract - extract device options
out of core into the options
-------------------------------------------------*/
static void image_options_extract(running_machine &machine)
{
/* only extract the device options if we've added them
no need to assert in case they are missing */
{
int index = 0;
image_interface_iterator iter(machine.root_device());
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
const char *filename = image->filename();
/* and set the option */
astring error;
machine.options().set_value(image->instance_name(), filename ? filename : "", OPTION_PRIORITY_CMDLINE, error);
index++;
}
}
/* write the config, if appropriate */
if (machine.options().write_config())
write_config(machine.options(), NULL, &machine.system());
}
/*-------------------------------------------------
image_unload_all - unload all images and
extract options
-------------------------------------------------*/
void image_unload_all(running_machine &machine)
{
// extract the options
image_options_extract(machine);
image_interface_iterator iter(machine.root_device());
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
// unload this image
image->unload();
}
}
/*-------------------------------------------------
image_device_init - initialize devices for a specific
running_machine
-------------------------------------------------*/
void image_device_init(running_machine &machine)
{
const char *image_name;
/* make sure that any required devices have been allocated */
image_interface_iterator iter(machine.root_device());
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
/* is an image specified for this image */
image_name = machine.options().device_option(*image);
if ((image_name != NULL) && (image_name[0] != '\0'))
{
/* mark init state */
image->set_init_phase();
/* try to load this image */
bool result = image->load(image_name);
/* did the image load fail? */
if (result)
{
/* retrieve image error message */
astring image_err = astring(image->error());
astring image_basename(image_name);
/* unload all images */
image_unload_all(machine);
fatalerror_exitcode(machine, MAMERR_DEVICE, "Device %s load (%s) failed: %s",
image->device().name(),
image_basename.cstr(),
image_err.cstr());
}
}
}
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
/* is an image specified for this image */
image_name = image->filename();
if (!((image_name != NULL) && (image_name[0] != '\0')))
{
/* no image... must this device be loaded? */
if (image->must_be_loaded())
{
fatalerror_exitcode(machine, MAMERR_DEVICE, "Driver requires that device \"%s\" must have an image to load", image->instance_name());
}
}
}
}
/*-------------------------------------------------
image_postdevice_init - initialize devices for a specific
running_machine
-------------------------------------------------*/
void image_postdevice_init(running_machine &machine)
{
/* make sure that any required devices have been allocated */
image_interface_iterator iter(machine.root_device());
for (device_image_interface *image = iter.first(); image != NULL; image = iter.next())
{
int result = image->finish_load();
/* did the image load fail? */
if (result)
{
/* retrieve image error message */
astring image_err = astring(image->error());
/* unload all images */
image_unload_all(machine);
fatalerror_exitcode(machine, MAMERR_DEVICE, "Device %s load failed: %s",
image->device().name(),
image_err.cstr());
}
}
/* add a callback for when we shut down */
machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(image_unload_all), &machine));
}
/***************************************************************************
INITIALIZATION
***************************************************************************/
/*-------------------------------------------------
image_init - start up the image system
-------------------------------------------------*/
void image_init(running_machine &machine)
{
image_device_init(machine);
config_register(machine, "image_directories", config_saveload_delegate(FUNC(image_dirs_load), &machine), config_saveload_delegate(FUNC(image_dirs_save), &machine));
}
/****************************************************************************
Battery functions
These functions provide transparent access to battery-backed RAM on an
image; typically for cartridges.
****************************************************************************/
/*-------------------------------------------------
image_battery_load_by_name - retrieves the battery
backed RAM for an image. A filename may be supplied
to the function.
The function comes in two flavors, depending on
what should happen when no battery is available:
we could fill the memory with a given value, or
pass a default battery (for a pre-initialized
battery from factory)
-------------------------------------------------*/
void image_battery_load_by_name(emu_options &options, const char *filename, void *buffer, int length, int fill)
{
file_error filerr;
int bytes_read = 0;
assert_always(buffer && (length > 0), "Must specify sensical buffer/length");
/* try to open the battery file and read it in, if possible */
emu_file file(options.nvram_directory(), OPEN_FLAG_READ);
filerr = file.open(filename);
if (filerr == FILERR_NONE)
bytes_read = file.read(buffer, length);
/* fill remaining bytes (if necessary) */
memset(((char *) buffer) + bytes_read, fill, length - bytes_read);
}
void image_battery_load_by_name(emu_options &options, const char *filename, void *buffer, int length, void *def_buffer)
{
file_error filerr;
int bytes_read = 0;
assert_always(buffer && (length > 0), "Must specify sensical buffer/length");
/* try to open the battery file and read it in, if possible */
emu_file file(options.nvram_directory(), OPEN_FLAG_READ);
filerr = file.open(filename);
if (filerr == FILERR_NONE)
bytes_read = file.read(buffer, length);
/* if no file was present, copy the default battery */
if (bytes_read == 0 && def_buffer)
memcpy((char *) buffer, (char *) def_buffer, length);
}
/*-------------------------------------------------
image_battery_save_by_name - stores the battery
backed RAM for an image. A filename may be supplied
to the function.
-------------------------------------------------*/
void image_battery_save_by_name(emu_options &options, const char *filename, const void *buffer, int length)
{
assert_always(buffer && (length > 0), "Must specify sensical buffer/length");
/* try to open the battery file and write it out, if possible */
emu_file file(options.nvram_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS);
file_error filerr = file.open(filename);
if (filerr == FILERR_NONE)
file.write(buffer, length);
}
|