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
|
// license:BSD-3-Clause
// copyright-holders:Couriersud
///
/// \file param.h
///
#ifndef NL_CORE_PARAM_H_
#define NL_CORE_PARAM_H_
#include "../nltypes.h"
#include "base_objects.h"
#include "core_device.h"
#include "setup.h"
#include "../plib/palloc.h"
#include "../plib/pfunction.h"
#include "../plib/pstream.h"
#include "../plib/pstring.h"
#include "../plib/putil.h" // psource_t
#include <memory>
namespace netlist
{
/// @brief Base class for all device parameters
///
/// All device parameters classes derive from this object.
class param_t : public detail::device_object_t
{
public:
enum param_type_t
{
STRING,
DOUBLE,
INTEGER,
LOGIC,
POINTER // Special-case which is always initialized at MAME startup
// time
};
// device-less, it's the responsibility of the owner to register!
param_t(const pstring &name);
param_t(core_device_t &device, const pstring &name);
PCOPYASSIGNMOVE(param_t, delete)
virtual ~param_t() noexcept;
param_type_t param_type() const noexcept(false);
virtual pstring value_string() const = 0;
protected:
pstring get_initial(const core_device_t *dev, bool *found) const;
template <typename C>
void set_and_update_param(C &p, const C v) noexcept
{
if (p != v)
{
p = v;
device().update_param();
}
}
};
// -----------------------------------------------------------------------------
// numeric parameter template
// -----------------------------------------------------------------------------
template <typename T>
class param_num_t final : public param_t
{
public:
using value_type = T;
param_num_t(core_device_t &device, const pstring &name, T val) noexcept(
false);
constexpr const T &operator()() const noexcept { return m_param; }
constexpr operator const T &() const noexcept { return m_param; }
void set(const T ¶m) noexcept
{
set_and_update_param(m_param, param);
}
pstring value_string() const override
{
return plib::pfmt("{}").e(gsl::narrow<nl_fptype>(m_param));
}
private:
T m_param;
};
template <typename T>
class param_enum_t final : public param_t
{
public:
using value_type = T;
param_enum_t(core_device_t &device, const pstring &name,
T val) noexcept(false);
constexpr T operator()() const noexcept { return m_param; }
constexpr operator T() const noexcept { return m_param; }
void set(const T ¶m) noexcept
{
set_and_update_param(m_param, param);
}
pstring value_string() const override
{
// returns the numerical value
return plib::pfmt("{}")(static_cast<int>(m_param));
}
private:
T m_param;
};
// -----------------------------------------------------------------------------
// pointer parameter
// -----------------------------------------------------------------------------
// FIXME: not a core component -> legacy
class param_ptr_t final : public param_t
{
public:
param_ptr_t(core_device_t &device, const pstring &name,
std::uint8_t *val);
std::uint8_t *operator()() const noexcept { return m_param; }
void set(std::uint8_t *param) noexcept
{
set_and_update_param(m_param, param);
}
pstring value_string() const override
{
// returns something which errors
return {"PTRERROR"};
}
private:
std::uint8_t *m_param;
};
// -----------------------------------------------------------------------------
// string parameter
// -----------------------------------------------------------------------------
class param_str_t : public param_t
{
public:
param_str_t(core_device_t &device, const pstring &name,
const pstring &val);
// FIXME: The device less constructor is only used by macro parameters
// Every macro device gets a nld_wrapper object as the owner.
// Use this as the owner and get rid of this constructor.
param_str_t(netlist_state_t &state, const pstring &name,
const pstring &val);
pstring operator()() const noexcept { return str(); }
void set(const pstring ¶m)
{
if (*m_param != param)
{
*m_param = param;
changed();
device().update_param();
}
}
pstring value_string() const override { return *m_param; }
protected:
virtual void changed() noexcept;
pstring str() const noexcept { return *m_param; }
private:
host_arena::unique_ptr<pstring> m_param;
};
// -----------------------------------------------------------------------------
// model parameter
// -----------------------------------------------------------------------------
class param_model_t : public param_str_t
{
public:
template <typename T>
class value_base_t
{
public:
template <typename P, typename Y = T,
typename DUMMY = std::enable_if_t<
plib::is_arithmetic<Y>::value>>
value_base_t(P ¶m, const pstring &name)
: m_value(gsl::narrow<T>(param.value(name)))
{
}
template <typename P, typename Y = T,
std::enable_if_t<!plib::is_arithmetic<Y>::value, int> = 0>
value_base_t(P ¶m, const pstring &name)
: m_value(static_cast<T>(param.value_str(name)))
{
}
T operator()() const noexcept { return m_value; }
operator T() const noexcept { return m_value; }
private:
const T m_value;
};
using value_t = value_base_t<nl_fptype>;
using value_str_t = value_base_t<pstring>;
param_model_t(core_device_t &device, const pstring &name,
const pstring &val)
: param_str_t(device, name, val)
{
}
pstring value_str(const pstring &entity);
nl_fptype value(const pstring &entity);
pstring type();
// hide this
void set(const pstring ¶m) = delete;
protected:
void changed() noexcept override;
private:
};
// -----------------------------------------------------------------------------
// data parameter
// -----------------------------------------------------------------------------
class param_data_t : public param_str_t
{
public:
param_data_t(core_device_t &device, const pstring &name)
: param_str_t(device, name, "")
{
}
plib::istream_uptr stream();
protected:
void changed() noexcept override {}
};
// -----------------------------------------------------------------------------
// rom parameter
// -----------------------------------------------------------------------------
template <typename ST, std::size_t AW, std::size_t DW>
class param_rom_t final : public param_data_t
{
public:
param_rom_t(core_device_t &device, const pstring &name);
const ST &operator[](std::size_t n) const noexcept { return m_data[n]; }
protected:
void changed() noexcept override
{
plib::istream_read(*stream(), m_data.data(), 1 << AW);
}
private:
std::array<ST, 1 << AW> m_data;
};
template <typename T>
param_num_t<T>::param_num_t(core_device_t &device, const pstring &name,
const T val)
: param_t(device, name)
, m_param(val)
{
bool found = false;
pstring p = this->get_initial(&device, &found);
if (found)
{
plib::pfunction<nl_fptype> func;
func.compile_infix(p, {});
auto valx = func.evaluate();
if (plib::is_integral<T>::value)
if (plib::abs(valx - plib::trunc(valx)) > nlconst::magic(1e-6))
throw nl_exception(MF_INVALID_NUMBER_CONVERSION_1_2(
device.name() + "." + name, p));
m_param = plib::narrow_cast<T>(valx);
}
device.state().save(*this, m_param, this->name(), "m_param");
}
template <typename T>
param_enum_t<T>::param_enum_t(core_device_t &device, const pstring &name,
const T val)
: param_t(device, name)
, m_param(val)
{
bool found = false;
pstring p = this->get_initial(&device, &found);
if (found)
{
T temp(val);
bool ok = temp.set_from_string(p);
if (!ok)
{
device.state().log().fatal(
MF_INVALID_ENUM_CONVERSION_1_2(name, p));
throw nl_exception(MF_INVALID_ENUM_CONVERSION_1_2(name, p));
}
m_param = temp;
}
device.state().save(*this, m_param, this->name(), "m_param");
}
template <typename ST, std::size_t AW, std::size_t DW>
param_rom_t<ST, AW, DW>::param_rom_t(core_device_t &device,
const pstring & name)
: param_data_t(device, name)
{
auto f = this->stream();
if (!f.empty())
{
plib::istream_read(*f, m_data.data(), 1 << AW);
// FIXME: check for failbit if not in validation.
}
else
device.state().log().warning(MW_ROM_NOT_FOUND(str()));
}
// -----------------------------------------------------------------------------
// Externals
// -----------------------------------------------------------------------------
extern template class param_num_t<std::uint8_t>;
extern template class param_num_t<std::uint16_t>;
extern template class param_num_t<std::uint32_t>;
extern template class param_num_t<std::uint64_t>;
extern template class param_num_t<std::int8_t>;
extern template class param_num_t<std::int16_t>;
extern template class param_num_t<std::int32_t>;
extern template class param_num_t<std::int64_t>;
extern template class param_num_t<float>;
extern template class param_num_t<double>;
extern template class param_num_t<long double>;
extern template class param_num_t<bool>;
// FIXME: Should not be used as parameters. Fix later.
using param_logic_t = param_num_t<bool>;
using param_int_t = param_num_t<int>;
using param_fp_t = param_num_t<nl_fptype>;
extern template class param_model_t::value_base_t<float>;
extern template class param_model_t::value_base_t<double>;
extern template class param_model_t::value_base_t<long double>;
} // namespace netlist
#endif // NL_CORE_PARAM_H_
|