From ff9b8bb838ecdfbfc1dc81038fcf3b2a87636982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Zrounba?= <6691770+clement-z@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:06:01 +0200 Subject: Initial release --- src/devices/bitstream_source.cpp | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/devices/bitstream_source.cpp (limited to 'src/devices/bitstream_source.cpp') diff --git a/src/devices/bitstream_source.cpp b/src/devices/bitstream_source.cpp new file mode 100644 index 0000000..f2e38a6 --- /dev/null +++ b/src/devices/bitstream_source.cpp @@ -0,0 +1,76 @@ +#include + +#include +#include + +using std::cout; +using std::endl; + +unsigned int BitstreamSource::m_bits_per_value = 8; +//double BitstreamSource::m_output_jitter = 0.0; + +void BitstreamSource::runner() +{ + // Initialize random number generator + //mt19937 gen(m_seed); + //uniform_int_distribution<> dis(1, m_max_value); // uniform in [1, max] + //int operation_mode = 0; // 0: modulated source , 1: independent pulses + + auto max_val = pow(2.0, m_bits_per_value) - 1; + + cout << name() << ":" << endl; + cout << "signal on: " << m_signal_on << endl; + cout << "signal off: " << m_signal_off << endl; + cout << "bits: "; + cout << endl; + + for(const auto &val : m_values) { + auto val_saturated = val; + if (m_current_value > max_val) + val_saturated = max_val; + + for (unsigned int iBit = 0; iBit < m_bits_per_value; ++iBit) { + bool bit = val_saturated & (1 << iBit); + cout << bit; + } + } + cout << " --> " << (dynamic_cast *>(p_out.get_interface()))->name(); + cout << endl; + cout << endl; + + // Loop over all values + for (unsigned int iVal = 0; iVal < m_values.size(); ++iVal) { + m_current_value = m_values[iVal]; + + if (m_current_value > max_val) + m_current_value = max_val; + + for (unsigned int iBit = 0; iBit < m_bits_per_value; ++iBit) { + // Wait at least one clk cycle, and for enable to be high + do { + // Wait next clk edge + wait(); + cout << "CLK tick (" << this->name() << ")" << endl; + } while (!p_enable->read()); + + // Deduce bit "value" from binary representation + // iBit = 0 represents LSB + bool bit = m_current_value & (1 << iBit); + + // Initialize corresponding signal + auto s = (bit ? OpticalSignal(m_signal_on) : + OpticalSignal(m_signal_off)); + + s.getNewId(); // New Id is necessary to notify signal change + + // Send signal to output with no delay + m_out_writer.delayedWrite(s, SC_ZERO_TIME); + } + } + + // Wait for end of simulation + while (true) { + // Do nothing + wait(); + } +} -- cgit v1.2.3