|
| 1 | +#ifndef IPS_DAC_MODEL_HPP |
| 2 | +#define IPS_DAC_MODEL_HPP |
| 3 | + |
| 4 | +#include <systemc-ams.h> |
| 5 | +#include "vunit.hpp" |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief Digital to Analog Converter module representation |
| 10 | + * This module generates an analog signal based on the [Vmin, Vmax] voltage |
| 11 | + * range |
| 12 | + * |
| 13 | + * @tparam BITS - the number of output bits of the digital code |
| 14 | + * @tparam VMIN - lowest voltage value |
| 15 | + * @tparam VMAX - highest voltage value |
| 16 | + * @tparam VU - voltage unit based on VUnit |
| 17 | + */ |
| 18 | +template <unsigned int BITS = 8, int VMIN = 0, int VMAX = 5, VUnit VU = VUnit::v> |
| 19 | +SCA_TDF_MODULE(dac) |
| 20 | +{ |
| 21 | +protected: |
| 22 | + // Min voltage value based on the voltage units |
| 23 | + const double V_MIN = static_cast<double>(VMIN) / static_cast<double>(VU); |
| 24 | + // Max voltage value based on the voltage units |
| 25 | + const double V_MAX = static_cast<double>(VMAX) / static_cast<double>(VU); |
| 26 | + // Max digital output code |
| 27 | + const double MAX_DIG = static_cast<double>((1 << BITS) - 1); |
| 28 | +public: |
| 29 | + // Input digital code |
| 30 | + sca_tdf::sca_in<sc_dt::sc_uint<BITS> > in; |
| 31 | + // Output analog voltage |
| 32 | + sca_tdf::sca_out<double> out; |
| 33 | + |
| 34 | + /** |
| 35 | + * @brief Construct a new dac object |
| 36 | + * |
| 37 | + */ |
| 38 | + SCA_CTOR(dac) : in("in"), out("out") |
| 39 | + { |
| 40 | + // Propagation time from input to output |
| 41 | + set_timestep(sca_core::sca_time(0.1, sc_core::SC_US)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief Convert the digital signal into analog signal |
| 46 | + * The N-bit digital code is converted into an analog signal in a voltage |
| 47 | + * range from Vmin to Vmax |
| 48 | + * |
| 49 | + */ |
| 50 | + void processing() |
| 51 | + { |
| 52 | + double dig_in = static_cast<double>(this->in.read().to_uint()); |
| 53 | + double ana_out = V_MIN + (dig_in / MAX_DIG) * (V_MAX - V_MIN); |
| 54 | + |
| 55 | + this->out.write(ana_out); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +#endif // IPS_DAC_MODEL_HPP |
0 commit comments