Jump to content

How To use SC_THREAD instead of SC_METHOD


Recommended Posts

time() process is a SC_METHOD, How to implement it by time() as SC_THREAD ?

 

#include <systemc.h>

SC_MODULE(timer)
{   
    sc_out<bool> intr0, intr1;
    sc_out<sc_uint<8>> Data_out;
    sc_in<sc_uint<8>> Addr, Data_in;
    sc_in<bool> read_en, write_en, Reset;
    sc_uint<8> timer_cntrl, timer_val, timer_cmp, timer_intr_status;
    sc_event e1, e2, e3;

    int NowTime = 0;
    int mLastUpdate = 0;

    void write()
    {
        
            if (write_en.read() == true)
            {
                Timer_update();
                switch (Addr.read())
                {
                case 0x0:
                    timer_cntrl = Data_in.read();
                    e1.notify();
                    Disable_Timer();
                    break;
                case 0x4:
                    break;
                case 0x8:
                    timer_cmp = Data_in.read();
                    break;
                case 0xC:
                    timer_intr_status = Data_in.read();
                    break;
                }
            }

    }

    void read()
    {
        
            if (read_en.read() == true)
            {
                switch (Addr.read())
                {
                case 0x0:
                    Data_out.write(timer_cntrl);
                    break;
                case 0x4:
                    Timer_update();
                    Data_out.write(timer_val);
                    break;
                case 0x8:
                    Data_out.write(timer_cmp);
                    break;
                case 0xC:
                    Data_out.write(timer_intr_status);
                    break;
                }
            } 
    }

    void Timer_update()
    {
        NowTime = sc_time_stamp().to_default_time_units();  //current time
        if (Reset.read() == 1)
        {
            timer_val = 0;
            timer_cmp = 0;
            timer_intr_status = 0;
            timer_val = 0;
            intr0 = 0;
            intr1 = 0;
        }
        else if (timer_cntrl & 1 << 0)
        {
            timer_val = ((NowTime) - mLastUpdate) / (20) % (256);
        }
    }

    void time()
    {
        static sc_time time;
        static int temp = 0;
        static int flag = 0;

        if ((time != SC_ZERO_TIME) && (timer_cntrl & 1 << 0))
        {
            int temp = time.to_default_time_units() + mLastUpdate;
            int time_delay = temp - NowTime;
            e1.notify(time_delay, SC_NS);
            time = SC_ZERO_TIME;
        }
        else if (timer_cntrl & 1 << 0)
        {
            switch (temp)
            {
            case 0:
                next_trigger(20 * (timer_cmp + flag), SC_NS);
                temp = 1;
                break;
            case 1:
                if (timer_cntrl & 1 << 1)
                {
                    intr0 = 1;
                    timer_intr_status |= 1 << 0;
                    e2.notify(20, SC_NS);
                }
                next_trigger(20 * (255 - timer_cmp), SC_NS);
                temp = 2;
                break;
            case 2:
                if (timer_cntrl & 1 << 2)
                {
                    intr1 = 1;
                    timer_intr_status |= 1 << 1;
                    e3.notify(20, SC_NS);
                }
                next_trigger(0, SC_NS);
                temp = 0;
                flag = 1;
                break;
            }
        }
        else
        {
            time = sc_time_stamp();
        }
    }

    void Disable_Timer()
    {
        static int temp1 = 0;
        static int temp2 = 0;
        static bool flag = 0;
        if (!(timer_cntrl & 1 << 0) && !flag && NowTime)
        {
            temp1 = NowTime;
            flag = 1;
        }
        if ((timer_cntrl & 1 << 0) && flag)
        {
            temp2 = NowTime;
            flag = 0;
            mLastUpdate = temp2 - temp1;
        }
    }

    void interrupt_disable()
    {
        while (true)
        {
            wait();
            if (intr0.read() == 1 || intr1.read() == 1)
            {
                intr0 = 0;
                intr1 = 0;
            }
        }
    }

    SC_CTOR(timer)
    {

        SC_METHOD(write);
        sensitive << write_en;

        SC_METHOD(read);
        sensitive << read_en;

        SC_THREAD(time);
        sensitive << e1;

        SC_THREAD(interrupt_disable);
        sensitive << e2 << e3;

    }
};
 

Link to comment
Share on other sites

First: using the code feature of the forum makes things more readable.

In your constructor you declared time() already as SC_THREAD now you need to surround your code with an endless loop:

void time()
{
    static sc_time time;
    static int temp = 0;
    static int flag = 0;
    while(true) { //<=== the endless loop to not leave the thread
        wait(e1); //<=== waiting for the even to trigger behavior
        if ((time != SC_ZERO_TIME) && (timer_cntrl & 1 << 0))
        {
            int temp = time.to_default_time_units() + mLastUpdate;
            int time_delay = temp - NowTime;
            e1.notify(time_delay, SC_NS);
            time = SC_ZERO_TIME;
        }
        else if (timer_cntrl & 1 << 0)
        {
            switch (temp)
            {
            case 0:
                next_trigger(20 * (timer_cmp + flag), SC_NS);
                temp = 1;
                break;
            case 1:
                if (timer_cntrl & 1 << 1)
                {
                    intr0 = 1;
                    timer_intr_status |= 1 << 0;
                    e2.notify(20, SC_NS);
                }
                next_trigger(20 * (255 - timer_cmp), SC_NS);
                temp = 2;
                break;
            case 2:
                if (timer_cntrl & 1 << 2)
                {
                    intr1 = 1;
                    timer_intr_status |= 1 << 1;
                    e3.notify(20, SC_NS);
                }
                next_trigger(0, SC_NS);
                temp = 0;
                flag = 1;
                break;
            }
        }
        else
        {
            time = sc_time_stamp();
        }
    }
} 

This can be found in any SystemC book or lecture material.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...