systemC的时序逻辑建模
systemc的时序逻辑方法分为两种:
1) 静态时序逻辑:
使用关键字sensitive,sensitive_pos , sensitive_neg :使得触发为值触发,正跳边沿触发,负跳变沿触发
这种触发方式为静态触发方式。
2) 动态时序逻辑:
在挂起的线程函数的wait语句中使用。
条件为port.posedge_event() port.negedge_event() port.value_changed_event() ,
并且可以结合逻辑操作符和时间表示复杂的逻辑关系。
ps: 现在已经更改语句了,详情看我的链接
http://blog.51cto.com/13824643/2137344
一般在不复杂的时序逻辑中,都使用静态时序逻辑,并且同组合逻辑相组合。
下面是一个关于检测序列的sc程序,只有当检测到3个连续的1时,流水检测才输出为1:
#include "base.h" #ifndef STREAMCHECK #define STREAMCHECK const int size = 3 ; SC_MODULE(streamcheck){ sc_in<bool >stream_in , clk ; sc_out<bool> check ; sc_signal<sc_uint<size> > reg ; void move(); void getcheck(); SC_CTOR(streamcheck){ reg = 0 ; SC_METHOD(move); sensitive_pos<<clk; // sequential logic , triggered by the clk !!! sc_METHOD(check); sensitive<<reg ; // combinational logic , determine by the reg . // Hold one thing ! the sc_signal is equal to the reg type in the verilog hdl . } }; #endif
#include "clkcounter.h" void clkcounter::reset(){ counter = 0 ; } void clkcounter::count(){ if( en ){ sc_uint<5> temp = count.read() ; if ( temp > 32 ) carry = 1 ; else carry = 0 ; count = temp+1 } } void startcount(){ en = 1 ; } void stopcount(){ en = 0 ; }
在这种同步的时序下,可以添加只由端口跳变触发的进程函数来达到异步的目的。
同时注意一点:在进程或线程函数的条件分支没有完全覆盖时会产生不必要的锁存器,这时需要通过对其取默认值的方式进行解决。
另外博主再加一个脉冲计数器的模型:
#include "base.h" #ifndef CLKCOUNTER #define CLKCOUNTER SC_MODULE(clk_counter){ sc_in<bool> clk , start , stop , reset ; sc_out<bool> carry ; sc_out<sc_uint<5> > counter ; sc_signal<bool> en ; void reset() ; void count() ; void startcount(); void stopcount(); SC_CTOR(clk_counter){ SC_METHOD(reset); // asynchronous reset port ! sensitive<<reset; SC_METHOD(count); // triggered by the clk , but need to check the en signal sensitive<<clk; SC_METHOD(startcount); // when the start port meet posedge , count process start ! sensitive_pos<<start; SC_METHOD(stopcount); // when the stop port meet negedge , count process end ! sensitive_neg<<stop; } }; #endif #include "clkcounter.h" void clkcounter::reset(){ counter = 0 ; } void clkcounter::count(){ if( en ){ sc_uint<5> temp = count.read() ; if ( temp > 32 ) carry = 1 ; else carry = 0 ; count = temp+1 } } void startcount(){ en = 1 ; } void stopcount(){ en = 0 ; }