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

    RAM device

    Provides a configurable amount of RAM to drivers

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

#include <stdio.h>
#include <ctype.h>

#include "emu.h"
#include "emuopts.h"
#include "ram.h"


/***************************************************************************
    CONSTANTS
***************************************************************************/

#define RAM_STRING_BUFLEN	16
#define MAX_RAM_OPTIONS		16


/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

typedef struct _ram_state ram_state;
struct _ram_state
{
	UINT32 size; /* total amount of ram configured */
	UINT8 *ram;  /* pointer to the start of ram */
};


/*****************************************************************************
    INLINE FUNCTIONS
*****************************************************************************/

INLINE ram_state *get_safe_token(device_t *device)
{
	assert(device != NULL);
	assert(device->type() == RAM);

	return (ram_state *)downcast<legacy_device_base *>(device)->token();
}


/*****************************************************************************
    HELPER FUNCTIONS
*****************************************************************************/

/*-------------------------------------------------
    ram_parse_string - convert a ram string to an
    integer value
-------------------------------------------------*/

UINT32 ram_parse_string(const char *s)
{
	UINT32 ram;
	char suffix = '\0';

	s += sscanf(s, "%u%c", &ram, &suffix);

	switch(tolower(suffix))
	{
	case 'k':
		/* kilobytes */
		ram *= 1024;
		break;

	case 'm':
		/* megabytes */
		ram *= 1024*1024;
		break;

	case '\0':
		/* no suffix */
		break;

	default:
		/* parse failure */
		ram = 0;
		break;
	}

	return ram;
}

/*****************************************************************************
    DEVICE INTERFACE
*****************************************************************************/

static DEVICE_START( ram )
{
	ram_state *ram = get_safe_token(device);
	ram_config *config = (ram_config *)downcast<const legacy_device_config_base &>(device->baseconfig()).inline_config();

	/* the device named 'ram' can get ram options from command line */
	if (strcmp(device->tag(), RAM_TAG) == 0)
	{
		const char *ramsize_string = device->machine->options().ram_size();

		if ((ramsize_string != NULL) && (ramsize_string[0] != '\0'))
			ram->size = ram_parse_string(ramsize_string);
	}

	/* if we didn't get a size yet, use the default */
	if (ram->size == 0)
		ram->size = ram_parse_string(config->default_size);

	/* allocate space for the ram */
	ram->ram = auto_alloc_array(device->machine, UINT8, ram->size);

	/* reset ram to the default value */
	memset(ram->ram, config->default_value, ram->size);

	/* register for state saving */
	device->save_item(NAME(ram->size));
	device->save_pointer(NAME(ram->ram), ram->size);
}

static DEVICE_VALIDITY_CHECK( ram )
{
	ram_config *config = (ram_config *)downcast<const legacy_device_config_base *>(device)->inline_config();
	const char *ramsize_string = NULL;
	int is_valid = FALSE;
	UINT32 specified_ram = 0;
	int error = FALSE;
	const char *gamename_option = NULL;

	/* verify default ram value */
	if (config!=NULL && ram_parse_string(config->default_size) == 0)
	{
		mame_printf_error("%s: '%s' has an invalid default RAM option: %s\n", driver->source_file, driver->name, config->default_size);
		error = TRUE;
	}

	/* command line options are only parsed for the device named RAM_TAG */
	if (device->tag()!=NULL && strcmp(device->tag(), RAM_TAG) == 0)
	{
		/* verify command line ram option */
		ramsize_string = options.ram_size();
		gamename_option = options.system_name();

		if ((ramsize_string != NULL) && (ramsize_string[0] != '\0'))
		{
			specified_ram = ram_parse_string(ramsize_string);

			if (specified_ram == 0)
			{
				mame_printf_error("%s: '%s' cannot recognize the RAM option %s\n", driver->source_file, driver->name, ramsize_string);
				error = TRUE;
			}
			if (gamename_option != NULL && *gamename_option != 0 && strcmp(gamename_option, driver->name) == 0)
			{
				/* compare command line option to default value */
				if (ram_parse_string(config->default_size) == specified_ram)
					is_valid = TRUE;

				/* verify extra ram options */
				if (config->extra_options != NULL)
				{
					int j;
					int size = strlen(config->extra_options);
					char * const s = mame_strdup(config->extra_options);
					char * const e = s + size;
					char *p = s;
					for (j=0;j<size;j++) {
						if (p[j]==',') p[j]=0;
					}

					/* try to parse each option */
					while(p <= e)
					{
						UINT32 option_ram_size = ram_parse_string(p);

						if (option_ram_size == 0)
						{
							mame_printf_error("%s: '%s' has an invalid RAM option: %s\n", driver->source_file, driver->name, p);
							error = TRUE;
						}

						if (option_ram_size == specified_ram)
							is_valid = TRUE;

						p += strlen(p);
						if (p == e)
							break;
						p += 1;
					}

					osd_free(s);
				}

			} else {
				/* if not for this driver then return ok */
				is_valid = TRUE;
			}
		}
		else
		{
			/* not specifying the ramsize on the command line is valid as well */
			is_valid = TRUE;
		}
	}
	else
		is_valid = TRUE;

	if (!is_valid)
	{
		mame_printf_error("%s: '%s' cannot recognize the RAM option %s", driver->source_file, driver->name, ramsize_string);
		mame_printf_error(" (valid options are %s", config->default_size);

		if (config->extra_options != NULL)
			mame_printf_error(",%s).\n", config->extra_options);
		else
			mame_printf_error(").\n");

		error = TRUE;
	}

	return error;

}


DEVICE_GET_INFO( ram )
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:					info->i = sizeof(ram_state);			break;
		case DEVINFO_INT_INLINE_CONFIG_BYTES:			info->i = sizeof(ram_config);				break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_START:							info->start = DEVICE_START_NAME(ram);	break;
		case DEVINFO_FCT_STOP:							/* Nothing */								break;
		case DEVINFO_FCT_RESET:							/* Nothing */								break;
		case DEVINFO_FCT_VALIDITY_CHECK:				info->p = (void*)DEVICE_VALIDITY_CHECK_NAME(ram); break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:							strcpy(info->s, "RAM");				break;
		case DEVINFO_STR_FAMILY:						strcpy(info->s, "RAM");						break;
		case DEVINFO_STR_VERSION:						strcpy(info->s, "1.00");					break;
		case DEVINFO_STR_SOURCE_FILE:					strcpy(info->s, __FILE__);					break;
		case DEVINFO_STR_CREDITS:						strcpy(info->s, "Copyright MESS Team");		break;
	}
}

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

UINT32 ram_get_size(device_t *device)
{
	ram_state *ram = get_safe_token(device);
	return ram->size;
}

UINT8 *ram_get_ptr(device_t *device)
{
	ram_state *ram = get_safe_token(device);
	return ram->ram;
}

DEFINE_LEGACY_DEVICE(RAM, ram);