summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/ram.cpp
blob: 2a074e7cee31cf3a3c7e1cd3ce95f849c9472d46 (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
// license:BSD-3-Clause
// copyright-holders: Dirk Best
/*************************************************************************

    RAM device

    Provides a configurable amount of RAM to drivers

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

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

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

#include <algorithm>
#include <functional>
#include <set>


namespace {

//-------------------------------------------------
//  parse_string - convert a ram string to an
//  integer value
//-------------------------------------------------

u32 parse_string(const char *s)
{
	static constexpr struct
	{
		const char *suffix;
		unsigned multiple;
	} s_suffixes[] =
	{
		{ "",       1 },
		{ "k",      1024 },
		{ "kb",     1024 },
		{ "kib",    1024 },
		{ "m",      1024 * 1024 },
		{ "mb",     1024 * 1024 },
		{ "mib",    1024 * 1024 }
	};

	// parse the string
	unsigned ram = 0;
	char suffix[8] = { 0, };
	sscanf(s, "%u%7s", &ram, suffix);

	// perform the lookup
	auto const iter(std::find_if(
			std::begin(s_suffixes),
			std::end(s_suffixes),
			[&suffix](const auto &potential_suffix) { return !core_stricmp(suffix, potential_suffix.suffix); }));

	// identify the multiplier (or 0 if not recognized, signalling a parse failure)
	unsigned const multiplier((iter != std::end(s_suffixes)) ? iter->multiple : 0);

	// return the result
	return ram * multiplier;
}


//-------------------------------------------------
//  calculate_extra_options
//-------------------------------------------------

ram_device::extra_option_vector calculate_extra_options(const char *extra_options_string, std::string *bad_option)
{
	ram_device::extra_option_vector result;
	std::string const options(extra_options_string);

	bool done(false);
	for (std::string::size_type start = 0, end = options.find_first_of(','); !done; start = end + 1, end = options.find_first_of(',', start))
	{
		// ignore spaces
		while ((end > start) && (options.length() > start) && ((' ' == options[start]) || ('\t' == options[start])))
			++start;

		// parse the option
		std::string ram_option_string(options.substr(start, (end == -1) ? -1 : end - start));
		u32 const ram_option = parse_string(ram_option_string.c_str());
		if (ram_option == 0)
		{
			if (bad_option)
				*bad_option = std::move(ram_option_string);
			return result;
		}

		// and add it to the results
		result.emplace_back(std::move(ram_option_string), ram_option);
		done = std::string::npos == end;
	}
	return result;
}

};


/*****************************************************************************
    LIVE DEVICE
*****************************************************************************/

// device type definition
DEFINE_DEVICE_TYPE(RAM, ram_device, "ram", "RAM")


//-------------------------------------------------
//  ram_device - constructor
//-------------------------------------------------

ram_device::ram_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, RAM, tag, owner, clock)
	, m_size(0)
	, m_default_size(0)
	, m_default_value(0xCD)
	, m_extra_options_string(nullptr)
{
}


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

void ram_device::device_start()
{
	// the device named 'ram' can get ram options from command line
	u32 const defsize(default_size());
	m_size = 0;
	if (strcmp(tag(), ":" RAM_TAG) == 0)
	{
		char const *const ramsize_string(machine().options().ram_size());
		if (ramsize_string && *ramsize_string)
		{
			m_size = parse_string(ramsize_string);
			if (!is_valid_size(m_size))
			{
				extra_option_vector::const_iterator found(std::find_if(m_extra_options.begin(), m_extra_options.end(), [defsize] (extra_option const &opt) { return opt.second == defsize; }));
				std::ostringstream output;
				util::stream_format(output, "Cannot recognize the RAM option %s (valid options are ", ramsize_string);
				if (!m_extra_options_string)
					util::stream_format(output, "%s).\n", m_default_size);
				else if (m_extra_options.end() != found)
					util::stream_format(output, "%s).\n", m_extra_options_string);
				else
					util::stream_format(output, "%s,%s).\n", m_default_size, m_extra_options_string);

				osd_printf_error("%s", output.str().c_str());

				osd_printf_warning("Setting value to default %s\n", m_default_size);

				m_size = 0;
			}
		}
	}

	// if we didn't get a size yet, use the default
	if (m_size == 0)
		m_size = defsize;

	// allocate space for the ram
	m_pointer = std::make_unique<u8 []>(m_size);
	std::fill_n(m_pointer.get(), m_size, m_default_value);

	// register for state saving
	save_item(NAME(m_size));
	save_pointer(NAME(m_pointer), m_size);
}


//-------------------------------------------------
//  device_validity_check - device-specific validity
//  checks
//-------------------------------------------------

void ram_device::device_validity_check(validity_checker &valid) const
{
	// verify default ram value
	if (default_size() == 0)
		osd_printf_error("Invalid default RAM option: %s\n", m_default_size);

	// calculate any extra options
	if (m_extra_options_string)
	{
		std::string bad_option;
		extra_option_vector const extras(calculate_extra_options(m_extra_options_string, &bad_option));

		// report any errors
		if (!bad_option.empty())
			osd_printf_error("Invalid RAM option: %s\n", bad_option.c_str());

		// report duplicates
		using extra_option_ref_set = std::set<std::reference_wrapper<extra_option const>, bool (*)(extra_option const &, extra_option const &)>;
		extra_option_ref_set sorted([] (extra_option const &a, extra_option const &b) { return a.second < b.second; });
		for (extra_option const &opt : extras)
		{
			auto const ins(sorted.emplace(opt));
			if (!ins.second)
				osd_printf_error("Duplicate RAM options: %s == %s (%u)\n", ins.first->get().first.c_str(), opt.first.c_str(), opt.second);
		}
	}
}


//-------------------------------------------------
//  is_valid_size
//-------------------------------------------------

bool ram_device::is_valid_size(u32 size) const
{
	return size == default_size()
		|| std::find_if(extra_options().begin(), extra_options().end(), [size] (extra_option const &opt) { return opt.second == size; }) != extra_options().end();
}


//-------------------------------------------------
//  default_size
//-------------------------------------------------

u32 ram_device::default_size() const
{
	return parse_string(m_default_size);
}


//-------------------------------------------------
//  extra_options
//-------------------------------------------------

const ram_device::extra_option_vector &ram_device::extra_options() const
{
	if (m_extra_options_string && m_extra_options.empty())
		m_extra_options = calculate_extra_options(m_extra_options_string, nullptr);
	return m_extra_options;
}