如何在 SystemC 中使用控制信号管理 for 循环中的计数器?

How to manage counters within a for loop with a control signal in SystemC?

我正在用 SystemC 编写一个模块,它基本上必须按如下方式工作:它通过端口 p_in 接收一串字节,并通过端口 h 接收控制信号。如果 h 信号为 true,模块必须将字节保存在矩阵 matrix 中。在我使用的 SC_THREAD 中,我实现了这个:

void MY_MODULE::my_method(){

    if(!rst){
      //put all the output ports to 0
    }

    while(1){

        //The module waits while the signal h is false
        while(!h) wait();

        //The iterations to fill in the matrix begin
        //The iterations must work just if h = true
        for(i=0; i<100; i++){
            for(j=0; j<100; j++){
                wait();
                matrix.nmmatrix[i*matrix.width+j] = p_in;               
            }
        }
    }
}

我遇到的问题是,即使 h 为假,计数器 ij 继续递增,但当 hfalse 再次为 true 时继续。你能指出我做错了什么吗?

更新

我修改了死循环如下:

    while(1){

        //The module waits while the signal h is false
        while(!h) wait();

        //The iterations to fill in the matrix begin
        //The iterations must work just if h = true
        for(i=0; i<100; i++){
          //Wait for a positive event of control signal h
          wait(h.posedge_event());
            for(j=0; j<100; j++){
                wait();
                matrix.nmmatrix[i*matrix.width+j] = p_in;               
            }
        }
    }

现在,当信号 hfalse 时,计数器停止增加。尽管如此,我遇到的问题是,当 h 第一次为 true 时,计数不会开始,但当 h 第二次为 true 时,它就会开始。可能是哪个问题?

为什么不像在外循环中那样将它放在内循环中?

while(!h) wait();

因此,您的 ij 增量可以通过 h false.

暂停

您的代码流程很接近,但请尝试这样做以避免错过第一个时钟的捕获,其中 h 为真:

while(1){
    while(!h) wait();

    for(i=0; i<100; i++){
        for(j=0; j<100; j++){
            matrix.nmmatrix[i*matrix.width+j] = p_in;  // h is true in this clock from the first while(!h)
            wait();                                    // one wait for next clock
            while(!h) wait();                          // wait further if h is not true
        }
    }
}

仅使用 clk.pos() 作为灵敏度就足够了,因为您使用的是 SC_THREAD

通过对@mrbone 建议的代码稍作修改,问题得到解决:

   while(1){
        while(!h) wait(h.posedge_event()); //wait for a positive edge of h

        for(i=0; i<100; i++){
            for(j=0; j<100; j++){
                matrix.nmmatrix[i*matrix.width+j] = p_in;  // h is true in this clock from the first while(!h)
                wait();                                    // one wait for next clock
                while(!h) wait();                          // wait further if h is not true
            }
        }
    }

当仅使用 wait() 时,计数在 htrue 后的一个时钟周期开始。添加此语句时,将在检测到 h 的上升沿时开始计数。