summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author couriersud <couriersud@users.noreply.github.com>2022-05-08 18:42:14 +0200
committer GitHub <noreply@github.com>2022-05-09 02:42:14 +1000
commit2b75146d7fac14aa4116d83c9ef232960a1626f4 (patch)
treef510f153e26e3e83044563f0c6687f206922ce67
parent6259cd5fd30ed958d42f138c2ec441bb26dd4e66 (diff)
machine/netlist.cpp: Use templates to compile with NETLIST_CREATE_CSV=1. (#9707)
-rw-r--r--src/devices/machine/netlist.cpp145
-rw-r--r--src/devices/machine/netlist.h61
2 files changed, 110 insertions, 96 deletions
diff --git a/src/devices/machine/netlist.cpp b/src/devices/machine/netlist.cpp
index ad7bb45fbc2..e0023ebb8d4 100644
--- a/src/devices/machine/netlist.cpp
+++ b/src/devices/machine/netlist.cpp
@@ -77,13 +77,70 @@ static netlist::netlist_time_ext nltime_from_attotime(attotime t)
return nlmtime;
}
-#if 0
-static attotime attotime_from_nltime(netlist::netlist_time_ext t)
+void netlist_log_csv<1>::open(running_machine &machine, const std::string &name)
{
- return attotime(t.as_raw() / netlist::netlist_time_ext::resolution(),
- (t.as_raw() % netlist::netlist_time_ext::resolution()) * (ATTOSECONDS_PER_SECOND / netlist::netlist_time_ext::resolution()));
+ m_csv_file = fopen(name.c_str(), "wb");
+ m_machine = &machine;
+}
+
+void netlist_log_csv<1>::close()
+{
+ if (m_csv_file != nullptr)
+ {
+ log_flush();
+ fclose(m_csv_file);
+ }
+}
+
+void netlist_log_csv<1>::log_add(char const* param, double value, bool isfloat)
+{
+ // skip if no file
+ if (m_csv_file == nullptr)
+ return;
+
+ // make a new entry
+ buffer_entry entry = { (*m_machine).scheduler().time(), isfloat, value, param };
+
+ // flush out half of the old entries if we hit the buffer limit
+ if (m_buffer.size() >= MAX_BUFFER_ENTRIES)
+ log_flush(MAX_BUFFER_ENTRIES / 2);
+
+ // fast common case: if we go at the end, just push_back
+ if (m_buffer.size() == 0 || entry.time >= m_buffer.back().time)
+ {
+ m_buffer.push_back(entry);
+ return;
+ }
+
+ // find our place in the queue
+ for (auto cur = m_buffer.rbegin(); cur != m_buffer.rend(); cur++)
+ if (entry.time >= cur->time)
+ {
+ m_buffer.insert(cur.base(), entry);
+ return;
+ }
+
+ // if we're too early, drop this entry rather than risk putting an out-of-order
+ // entry after the last one we flushed
+}
+
+void netlist_log_csv<1>::log_flush(int count)
+{
+ if (m_csv_file == nullptr)
+ return;
+ if (count > m_buffer.size())
+ count = m_buffer.size();
+ while (count--)
+ {
+ auto &entry = m_buffer.front();
+ if (entry.isfloat)
+ fprintf(m_csv_file, "%s,%s,%f\n", entry.time.as_string(), entry.string, entry.value);
+ else
+ fprintf(m_csv_file, "%s,%s,%d\n", entry.time.as_string(), entry.string, int(entry.value));
+ m_buffer.pop_front();
+ }
}
-#endif
+
class netlist_mame_device::netlist_mame_t : public netlist::netlist_state_t
{
@@ -296,9 +353,7 @@ void netlist_mame_analog_input_device::write(const double val)
void netlist_mame_analog_input_device::device_timer(emu_timer &timer, device_timer_id id, int param)
{
update_to_current_time();
-#if NETLIST_CREATE_CSV
- nl_owner().log_add(m_param_name, m_value_for_device_timer, true);
-#endif
+ nl_owner().log_csv().log_add(m_param_name, m_value_for_device_timer, true);
m_param->set(m_value_for_device_timer);
}
@@ -325,18 +380,14 @@ void netlist_mame_logic_input_device::write(const uint32_t val)
void netlist_mame_int_input_device::device_timer(emu_timer &timer, device_timer_id id, int param)
{
update_to_current_time();
-#if NETLIST_CREATE_CSV
- nl_owner().log_add(m_param_name, param, false);
-#endif
+ nl_owner().log_csv().log_add(m_param_name, param, false);
m_param->set(param);
}
void netlist_mame_logic_input_device::device_timer(emu_timer &timer, device_timer_id id, int param)
{
update_to_current_time();
-#if NETLIST_CREATE_CSV
- nl_owner().log_add(m_param_name, param, false);
-#endif
+ nl_owner().log_csv().log_add(m_param_name, param, false);
m_param->set(param);
}
@@ -374,56 +425,6 @@ void netlist_mame_sub_interface::set_mult_offset(const double mult, const double
m_offset = offset;
}
-#if NETLIST_CREATE_CSV
-void netlist_mame_device::log_add(char const* param, double value, bool isfloat)
-{
- // skip if no file
- if (m_csv_file == nullptr)
- return;
-
- // make a new entry
- buffer_entry entry = { machine().scheduler().time(), isfloat, value, param };
-
- // flush out half of the old entries if we hit the buffer limit
- if (m_buffer.size() >= MAX_BUFFER_ENTRIES)
- log_flush(MAX_BUFFER_ENTRIES / 2);
-
- // fast common case: if we go at the end, just push_back
- if (m_buffer.size() == 0 || entry.time >= m_buffer.back().time)
- {
- m_buffer.push_back(entry);
- return;
- }
-
- // find our place in the queue
- for (auto cur = m_buffer.rbegin(); cur != m_buffer.rend(); cur++)
- if (entry.time >= cur->time)
- {
- m_buffer.insert(cur.base(), entry);
- return;
- }
-
- // if we're too early, drop this entry rather than risk putting an out-of-order
- // entry after the last one we flushed
-}
-
-void netlist_mame_device::log_flush(int count)
-{
- if (m_csv_file == nullptr)
- return;
- if (count > m_buffer.size())
- count = m_buffer.size();
- while (count--)
- {
- auto &entry = m_buffer.front();
- if (entry.isfloat)
- fprintf(m_csv_file, "%s,%s,%f\n", entry.time.as_string(), entry.string, entry.value);
- else
- fprintf(m_csv_file, "%s,%s,%d\n", entry.time.as_string(), entry.string, int(entry.value));
- m_buffer.pop_front();
- }
-}
-#endif
netlist_mame_analog_input_device::netlist_mame_analog_input_device(const machine_config &mconfig, const char *tag, device_t *owner, const char *param_name)
: device_t(mconfig, NETLIST_ANALOG_INPUT, tag, owner, 0)
@@ -814,10 +815,6 @@ void netlist_mame_stream_output_device::device_start()
void netlist_mame_stream_output_device::device_reset()
{
LOGDEVCALLS("reset %s\n", name());
-#if 0
- m_cur = 0.0;
- m_last_buffer_time = netlist::netlist_time_ext::zero();
-#endif
}
void netlist_mame_stream_output_device::sound_update_fill(write_stream_view &target)
@@ -1051,15 +1048,13 @@ void netlist_mame_device::device_start_common()
m_device_reset_called = false;
-#if NETLIST_CREATE_CSV
std::string name = machine().system().name;
name += tag();
for (int index = 0; index < name.size(); index++)
if (name[index] == ':')
name[index] = '_';
name += ".csv";
- m_csv_file = fopen(name.c_str(), "wb");
-#endif
+ log_csv().open(machine(), name);
LOGDEVCALLS("device_start exit\n");
}
@@ -1097,13 +1092,7 @@ void netlist_mame_device::device_stop()
LOGDEVCALLS("device_stop\n");
if (m_netlist)
netlist().exec().stop();
-#if NETLIST_CREATE_CSV
- if (m_csv_file != nullptr)
- {
- log_flush();
- fclose(m_csv_file);
- }
-#endif
+ log_csv().close();
}
void netlist_mame_device::device_post_load()
diff --git a/src/devices/machine/netlist.h b/src/devices/machine/netlist.h
index 4745ea1f2e0..f14095deaab 100644
--- a/src/devices/machine/netlist.h
+++ b/src/devices/machine/netlist.h
@@ -57,6 +57,47 @@ namespace netlist {
void _name(const int data, const attotime &time)
+// ----------------------------------------------------------------------------------------
+// netlist_log_csv
+// ----------------------------------------------------------------------------------------
+
+template <int USE>
+struct netlist_log_csv
+{
+private:
+ static constexpr int MAX_BUFFER_ENTRIES = 1000;
+public:
+ void open(running_machine &machine, const std::string &name) { }
+ void close() { }
+ void log_add(char const* param, double value, bool isfloat) { }
+ void log_flush(int count = MAX_BUFFER_ENTRIES) { }
+
+private:
+};
+
+template <>
+struct netlist_log_csv<1>
+{
+private:
+ static constexpr int MAX_BUFFER_ENTRIES = 1000;
+public:
+ void open(running_machine &machine, const std::string &name);
+ void close();
+
+ void log_add(char const* param, double value, bool isfloat);
+ void log_flush(int count = MAX_BUFFER_ENTRIES);
+private:
+ struct buffer_entry
+ {
+ attotime time;
+ bool isfloat;
+ double value;
+ char const *string;
+ };
+ std::deque<buffer_entry> m_buffer;
+ FILE* m_csv_file = nullptr;
+ running_machine * m_machine;
+};
// ----------------------------------------------------------------------------------------
// netlist_mame_device
@@ -82,6 +123,7 @@ public:
static void register_memregion_source(netlist::nlparse_t &parser, device_t &dev, const char *name);
+ auto &log_csv() { return m_log_csv; }
protected:
netlist_mame_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock);
@@ -113,24 +155,7 @@ private:
func_type m_setup_func;
bool m_device_reset_called;
-#if NETLIST_CREATE_CSV
- static constexpr int MAX_BUFFER_ENTRIES = 1000;
-
-public:
- void log_add(char const* param, double value, bool isfloat);
- void log_flush(int count = MAX_BUFFER_ENTRIES);
-
-private:
- struct buffer_entry
- {
- attotime time;
- bool isfloat;
- double value;
- char const *string;
- };
- std::deque<buffer_entry> m_buffer;
- FILE* m_csv_file = nullptr;
-#endif
+ netlist_log_csv<NETLIST_CREATE_CSV> m_log_csv;
};
class netlist_mame_cpu_device : public netlist_mame_device,