Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Last active September 6, 2022 22:03
Show Gist options
  • Save rahulbhadani/4f3074872432e3a77c4e9b02b243697e to your computer and use it in GitHub Desktop.
Save rahulbhadani/4f3074872432e3a77c4e9b02b243697e to your computer and use it in GitHub Desktop.
8x1 Multiplexer
#include<systemc>
#include<cstdlib>
#include<cstring>
#include<string>
using namespace std;
using namespace sc_core;
using namespace sc_dt;
SC_MODULE(mux8x1)
{
/* I/O Ports*/
sc_in< sc_lv<8> > in;
sc_in< sc_lv<3> > sel;
sc_out<sc_lv<1> > out;
/*Internal variables*/
sc_lv<8> inbv;
sc_lv<3> selbv;
void select()
{
while(1)
{
wait();
inbv = in.read();
selbv = sel.read();
sc_logic temp_out = (inbv[0] & ~selbv[2] & ~selbv[1] & ~selbv[0]) | (inbv[1] & ~selbv[2] & ~selbv[1] & selbv[0]) | (inbv[2] & ~selbv[2] & selbv[1] & ~selbv[0]) | (inbv[3] & ~selbv[2] & selbv[1] & selbv[0]) | (inbv[4] & selbv[2] & ~selbv[1] & ~selbv[0]) | (inbv[5] & selbv[2] & ~selbv[1] & selbv[0]) | (inbv[6] & selbv[2] & selbv[1] & ~selbv[0]) | (inbv[7] & selbv[2] & selbv[1] & selbv[0]);
sc_lv<1> lv_temp ;
lv_temp[0] = temp_out;
out.write(lv_temp);
}
}
SC_CTOR(mux8x1)
{
SC_THREAD(select);
sensitive << sel << in;
}
~mux8x1(){}
};
int sc_main(int argc, char ** argv)
{
sc_signal <sc_lv<8>> data_in;
sc_signal <sc_lv<3>> select_in;
sc_signal <sc_lv<1>> output;
sc_trace_file *fp;
mux8x1 M("MUX8");
fp = sc_create_vcd_trace_file("Mux_tracer");
fp->set_time_unit(1.0, SC_NS);
M.in(data_in);
M.sel(select_in);
M.out(output);
sc_trace(fp, data_in, "Input");
sc_trace(fp, select_in, "Select");
sc_trace(fp, output, "Output");
sc_start(1.0, SC_NS);
// Testbench
vector<sc_lv<8>> data_set {"11000011", "00100000", "00000111", "01010101", "00000000", "11111111", "10101010"};
vector<sc_lv<3>> select_set = {"000", "111", "010", "011"};
for(int i = 0; i < data_set.size(); ++i)
{
for (int j = 0; j < select_set.size(); ++j)
{
data_in.write(data_set.at(i));
select_in.write(select_set.at(j));
sc_start(1.0, SC_NS);
cout << sc_time_stamp().to_seconds()<<" Data: "<<data_in.read()<< " Select: "<< select_in.read()<<" Output: "<<output.read()<<endl;
}
}
sc_stop();
sc_close_vcd_trace_file(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment