SystemC simulation misses the first iteration

56 Views Asked by At

I'm having difficulties with initiating the simulation. Whenever I start a for loop, I always miss the first iteration of the loop. Here is my code:

#include <systemc.h>
#include <iostream>

using namespace std;

SC_MODULE(cpu){
    sc_in_clk clk { "cpu_clk" };
    sc_in<bool> rw { "cpu_rw" };
    sc_inout<sc_lv<16>> dbus { "cpu_dbus" };
    sc_out<sc_lv<20>> abus { "cpu_abus" };

    void cpu_loop();

    SC_CTOR(cpu){
        SC_CTHREAD(cpu_loop, clk.pos());
    }
};

void cpu::cpu_loop(){
    while(1){
        wait();

        cout << "cpu" << endl;
    }
}

SC_MODULE(sram){
    sc_in_clk clk { "sram_clk" };
    sc_in<bool> rw { "sram_rw" };
    sc_inout<sc_lv<16>> dbus { "sram_dbus" };
    sc_in<sc_lv<20>> abus { "sram_abus" };

    void sram_loop();

    SC_CTOR(sram){
        SC_CTHREAD(sram_loop, clk.pos());
    }
};

void sram::sram_loop(){
    while(1){
        wait();

        cout << "sram" << endl;
    }
}

int sc_main(int argc, char* argv[]) {
    int i;
    sc_signal<sc_lv<16>, SC_MANY_WRITERS> dbus { "main_dbus" };
    sc_signal<sc_lv<20>, SC_MANY_WRITERS> abus { "main_abus" };
    sc_signal<bool> clk { "main_clk" };
    sc_signal<bool> rw { "main_rw" };

    cpu custom_cpu("custom_cpu");
    sram custom_sram ("custom_sram");

    custom_cpu.abus(abus);
    custom_cpu.dbus(dbus);
    custom_cpu.clk(clk);
    custom_cpu.rw(rw);


    custom_sram.abus(abus);
    custom_sram.dbus(dbus);
    custom_sram.clk(clk);
    custom_sram.rw(rw);

    sc_start(1, SC_MS);
    clk = 0;

    for(i = 0; i < 10; i++){
        cout << "============== " << i << "==============" << endl;
        clk = 1;
        sc_start(1, SC_MS);
        clk = 0;
        sc_start(1, SC_MS);
    }

    return 0;
}

And here is the output:

============== 0==============
============== 1==============
cpu
sram
============== 2==============
cpu
sram
============== 3==============
cpu
sram
============== 4==============
cpu
sram
============== 5==============
cpu
sram
============== 6==============
cpu
sram
============== 7==============
cpu
sram
============== 8==============
cpu
sram
============== 9==============
cpu
sram

I've searched for an answer, but it seems that no one else has this problem, so I'm assuming I'm doing something wrong.

1

There are 1 best solutions below

0
toolic On BEST ANSWER

If you do the print out before you call wait in your while loops, you get the expected output. This is consistent with the for loop. For example:

void cpu::cpu_loop(){
    while(1){
        cout << "cpu" << endl;
        wait();
    }
}

This runs on EDA playgrpound.