Skip to content

Commit f729171

Browse files
committed
Adding ADC model and its testbench
1 parent b0f4f6c commit f729171

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

modules/adc/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Include common Makefile
2+
include ../Makefile
3+
4+
SRCDIR+=../../utils/src
5+
INCDIR+=-I$(SYSTEMC_AMS_HOME)/include -I../utils/include
6+
LIBDIR+=-L$(SYSTEMC_AMS_HOME)/lib-linux64
7+
LIBS+=-lsystemc-ams
8+
9+
# Defining preprocessor directive for debug
10+
ifdef IPS_DEBUG_EN
11+
CFLAGS += -DIPS_DEBUG_EN
12+
LFLAGS += -DIPS_DEBUG_EN
13+
endif # IPS_DEBUG_EN
14+
15+
# Defining preprocessor directive for dumping enable
16+
ifdef IPS_DUMP_EN
17+
CFLAGS += -DIPS_DUMP_EN
18+
LFLAGS += -DIPS_DUMP_EN
19+
endif # IPS_DUMP_EN
20+
21+
# Run the compiled file
22+
run:
23+
@./$(TARGET)
24+
25+
# Show waveform
26+
waveform:
27+
@gtkwave ips_adc.vcd

modules/adc/include/adc.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef IPS_ADC_MODEL_HPP
2+
#define IPS_ADC_MODEL_HPP
3+
4+
#include <systemc-ams.h>
5+
#include "vunit.hpp"
6+
7+
8+
template <unsigned int BITS = 8, int VMIN = 0, int VMAX = 5, VUnit VU = VUnit::v>
9+
SCA_TDF_MODULE(dac)
10+
{
11+
protected:
12+
const double V_MAX = static_cast<double>(VMAX) / static_cast<double>(VU);
13+
const double V_MIN = static_cast<double>(VMIN) / static_cast<double>(VU);
14+
const double MAX_DIG = static_cast<double>((1 << BITS) - 1);
15+
public:
16+
sca_tdf::sca_in<double> in;
17+
sca_tdf::sca_out<sc_dt::sc_uint<BITS> > out;
18+
19+
SCA_CTOR(dac) : in("in"), out("out") {
20+
set_timestep(sca_core::sca_time(0.1, sc_core::SC_US));
21+
}
22+
23+
void processing()
24+
{
25+
double normalized_ana_in = (in.read() - V_MIN) / (V_MAX - V_MIN);
26+
unsigned int dig_code = static_cast<unsigned int>(normalized_ana_in * MAX_DIG);
27+
28+
this->out.write(static_cast<sc_dt::sc_uint<BITS> >(dig_code));
29+
}
30+
};
31+
32+
#endif // IPS_ADC_MODEL_HPP

modules/adc/include/test_adc.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef IPS_TEST_ADC_HPP
2+
#define IPS_TEST_ADC_HPP
3+
4+
#include <cstdlib>
5+
6+
7+
template <unsigned int N>
8+
SCA_TDF_MODULE(test_adc) {
9+
public:
10+
sca_tdf::sca_out<double> o_ana;
11+
const int MAX_CODE = (1 << N);
12+
13+
SCA_CTOR(test_adc) {
14+
set_timestep(sca_core::sca_time(0.1, sc_core::SC_US));
15+
}
16+
17+
void processing()
18+
{
19+
this->o_ana.write(static_cast<double>(rand() % MAX_CODE) / MAX_CODE);
20+
}
21+
};
22+
23+
#endif // IPS_TEST_ADC_HPP

modules/adc/src/tb_adc.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <systemc-ams.h>
2+
#include "adc.hpp"
3+
#include "test_adc.hpp"
4+
5+
#define N 8
6+
7+
8+
int sc_main(int, char*[])
9+
{
10+
sca_tdf::sca_signal<double> s_ana;
11+
sca_tdf::sca_signal<sc_dt::sc_uint<N> > s_dig_out;
12+
13+
const int MAX_ITER = (1 << N) - 1;
14+
15+
dac<N> ips_adc("ips_adc");
16+
ips_adc.in(s_ana);
17+
ips_adc.out(s_dig_out);
18+
19+
test_adc<N> ips_test_adc("ips_test_adc");
20+
ips_test_adc.o_ana(s_ana);
21+
22+
sca_util::sca_trace_file* tf = sca_util::sca_create_vcd_trace_file("ips_adc");
23+
sca_util::sca_trace(tf, s_ana, "in");
24+
sca_util::sca_trace(tf, s_dig_out, "out");
25+
26+
sc_start(MAX_ITER * 0.1, SC_US);
27+
28+
sca_util::sca_close_vcd_trace_file(tf);
29+
30+
return 0;
31+
};

0 commit comments

Comments
 (0)