aboutsummaryrefslogtreecommitdiff
path: root/src/devices/bitstream_source.cpp
blob: f2e38a6c4d86cb807c8a29b8de376345f6195eb5 (plain)
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
#include <bitstream_source.h>

#include <random>
#include <cstdlib>

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<sc_signal<OpticalSignal> *>(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();
    }
}