1

I want to implement module, which when is called to work changes signal x the way below:

1 clk pos.edge : x = 0 // 1st phase
2 clk pos.edge : x = 0 // 2nd phase
3 clk pos.edge : x = 1 // 3rd phase

And then stops until called again.

I have function foo() in my module, which is called from main and allows thread work_foo() to execute on the posedge of clock.

I tried to do it this way (with wait()), and in some simple test it gives right wave, but it is an inappropriate way: my_module.h:

#include "systemc.h"

SC_MODULE (my_module)
{
    sc_in <bool> clk;
    sc_out <sc_logic> x;

    sc_signal <bool> valid;

    void foo()
    {
        valid = 1;      
        return;
    }

    void work_foo()
    {
        while (true)
        {
            if (valid == 1)
            { // foo() was called
                if (phase == 0)
                {
                x = SC_LOGIC_0;
                wait(); // waiting for the next tick
                x = SC_LOGIC_0;
                wait(); // waiting for the next tick
                x = SC_LOGIC_1;
                wait(); // waiting for the next tick
            }
            else
            {   
                valid=0;
                wait();
            }
        }
    }

    SC_HAS_PROCESS(my_module);  
    my_module(sc_module_name name):
    sc_module(name),
    clk("clk"), x("x"), valid("valid")

    {
        SC_THREAD(work_foo);
        //dont_initialize();        
        sensitive<<clk.pos();
        valid=0;
    }
};

main.cpp:

#include "systemc.h"
#include "my_module.h"

int sc_main (int argc, char* argv[])
{
    sc_clock clock("clock", 10, SC_NS);    
    sc_signal<sc_logic > x;    
    my_module mm("my_mod");     
    mm.clk(clock);
    mm.x(x);        
    mm.foo  ();
    sc_start(200, SC_NS);
    sc_stop();      
    return 0;
}

And now I'm trying to implement it another way. I was advised to use additional sc_signal (or variable) in my module, which indicates the incrementing number of the phase. The problem is what this gets looped at the very start. How can I solve this?

#include "systemc.h"

SC_MODULE (my_module)
{
    sc_in <bool> clk;
    sc_out <sc_logic> x;

    sc_signal <bool> valid;
    sc_signal <uint> phase;

    void foo()
    {
        valid = 1;      
        return;
    }

    void work_foo()
    {
        while (true)
        {
            if (valid == 1)
            {
                if (phase == 0)
                {
                x = SC_LOGIC_0;
                phase=phase+1;
                }
                else if (phase == 1)
                {
                x = SC_LOGIC_0;
                phase=phase+1;
                }
                else if (phase == 2)
                {
                x = SC_LOGIC_1;
                phase=phase+1;
                }
            }
            else
            {   
                phase=0;    
                valid=0;
                wait();
            }
        }
    }

    SC_HAS_PROCESS(my_module);  
    my_module(sc_module_name name):
    sc_module(name),
    clk("clk"), x("x"), valid("valid")

    {
        SC_THREAD(work_foo);
        //dont_initialize();        
        sensitive<<clk.pos();
        valid=0;
        phase=0;
    }
};
ans
  • 378
  • 1
  • 5
  • 18

0 Answers0