1

I am developing a simple NAND module in SystemC. By specification, it should have a 4 ns delay so I tried to describe it with a process with a "wait" statement and SC_THREAD, as follows:

//file: nand.h
#include "systemc.h"

SC_MODULE(nand2){
    sc_in<bool> A, B; 
    sc_out<bool> F; 

    void do_nand2(){
        bool a, b, f;
        a = A.read();
        b = B.read();
        f = !(a && b);
        wait(4, SC_NS);
        F.write(f);
    }

    SC_CTOR(nand2){
        SC_THREAD(do_nand2);
        sensitive << A << B;
    }
};

To simulate I've created another module the outputs the stimulus for the NAND, as follows:

//file: stim.h
#include "systemc.h"

SC_MODULE(stim){
    sc_out<bool> A, B;
    sc_in<bool> Clk;

    void stimGen(){
        wait();
        A.write(false);
        B.write(false);
        wait();
        A.write(false);
        B.write(true);
        wait();
        A.write(true);
        B.write(true);
        wait();
        A.write(true);
        B.write(false);
    }

    SC_CTOR(stim){
        SC_THREAD(stimGen);
        sensitive << Clk.pos();
    }
};

Having these two modules described, the top module (where sc_main is) looks like this:

//file: top.cpp    
#include "systemc.h"
#include "nand.h"
#include "stim.h"

int sc_main(int argc, char* argv[]){
    sc_signal<bool> ASig, BSig, FSig;
    sc_clock Clk("Clock", 100, SC_NS, 0.5);

    stim Stim("Stimulus");
    Stim.A(ASig); Stim.B(BSig); Stim.Clk(Clk);

    nand2 nand2("nand2");
    nand2.A(ASig); nand2.B(BSig); nand2.F(FSig);

    sc_trace_file *wf = sc_create_vcd_trace_file("sim");
    sc_trace(wf, Stim.Clk, "Clock");
    sc_trace(wf, nand2.A, "A");
    sc_trace(wf, nand2.B, "B");
    sc_trace(wf, nand2.F, "F");

    sc_start(400, SC_NS);

    sc_close_vcd_trace_file(wf);

    return 0;
}

The code was compiled and simulated with no errors, however when visualizing the .vcd file in gtkwave the output (F) gets stuck in 1, only showing the delay in the beginning of the simulation.

To test if there were any errors in the code I removed the "wait" statements and changed SC_THREAD to SC_METHOD in the nand.h file and simulated again, now getting the correct results, but without the delays of course.

What am I doing wrong?

2 Answers2

2

Just solved the problem:

instead of using

wait(4, SC_NS); 

with SC_THREAD I used

next_trigger(4, SC_NS);

with SC_METHOD and it worked just fine.

  • Are you sure this works? See https://stackoverflow.com/questions/68098155/how-to-simulate-output-delay-using-next-trigger-in-systemc –  Jun 23 '21 at 13:52
2

It's best if you use an SC_METHOD for process do_nand2, which is sensitive to the inputs. A thread usually has an infinite loop inside of it and it runs for the entire length of the simulation. A method runs only once from beginning to end when triggered. You use threads mostly for stimulus or concurrent processes and threads may, or may not be sensitive to any events.

Karibe
  • 155
  • 1
  • 10