在
power monitor part1 中, 我們介紹過 "要如何取得 dynamic Power" 的方式. 底下就用 Systemc 2.0 中 examples/sysc/fir 當例子.
可參考
SystemC 安裝
fir_data.h
#include <power.h>
SC_MODULE(fir_data) {
sc_in<bool> reset;
sc_in<unsigned> state_out;
sc_in<int> sample;
sc_out<int> result;
sc_out<bool> output_data_ready;
sc_int<19> acc;
sc_int<8> shift[16];
sc_int<9> coefs[16];
// power 4 sc_in<int>sample
TopCss<int> sample_ptr;
int cur_sample;
int pre_sample;
SC_CTOR(fir_data)
{
SC_METHOD(entry);
dont_initialize();
sensitive << reset;
sensitive << state_out;
sensitive << sample;
#include "fir_const.h"
//=================================
// sample signal Dynamic model
//=================================
SC_METHOD(sample_DyPW);
dont_initialize();
sensitive << sample;
sensitive << reset;
cur_sample =0;
pre_sample =0;
};
void entry();
void sample_DyPW();
};
fir_data.cpp
#include <systemc.h>
#include "fir_data.h"
void fir_data::entry()
{
int state;
sc_int<8> sample_tmp;
// reset functionality
if(reset.read()==true) {
sample_tmp = 0;
acc = 0;
for (int i=0; i<=15; i++)
shift[i] = 0;
}
// default settings
result.write(0);
output_data_ready.write(false);
state = state_out.read();
#ifdef DEBUG
// cout << "Data debug : " << " " << state << " " << acc << " " << " at time " << sc_time_stamp() << endl;
cout << "Data debug : " << " " << state << " " << acc << " " << " at time " << sc_time_stamp().to_double() << endl;
for(int i=15; i>=0; i--) {
cout << "Data debug : shift(" << i << ") " << shift[i] << endl;
};
#endif
// cycle behavior could be as well a case statement
switch (state) {
case 1 :
sample_tmp = sample.read();
acc = sample_tmp*coefs[0];
acc += shift[14]* coefs[15];
acc += shift[13]*coefs[14];
acc += shift[12]*coefs[13];
acc += shift[11]*coefs[12];
break;
case 2 :
acc += shift[10]*coefs[11];
acc += shift[9]*coefs[10];
acc += shift[8]*coefs[9];
acc += shift[7]*coefs[8];
break;
case 3 :
acc += shift[6]*coefs[7];
acc += shift[5]*coefs[6];
acc += shift[4]*coefs[5];
acc += shift[3]*coefs[4];
break;
case 4 :
acc += shift[2]*coefs[3];
acc += shift[1]*coefs[2];
acc += shift[0]*coefs[1];
for(int i=14; i>=0; i--) {
shift[i+1] = shift[i];
};
shift[0] = sample.read();
result.write((int)acc);
output_data_ready.write(true);
break;
default :
cout << "Information : Reset state" << endl;
}
}
void fir_data::sample_DyPW(){
if(reset.read()){
sample_ptr.SetSignalDef("sample",0,0,IN);
cur_sample=0; pre_sample=0; }
cur_sample = sample.read();
sample_ptr.UpdateSignalDef("sample",pre_sample,cur_sample,IN);
int c = sample_ptr.CalDymPower("sample");
cout <<"sample cur counts::\t"<< c << endl;
pre_sample = cur_sample;
}
Results:
Display : -16 at time 33000
Stimuli : 3 at time 39000
sample cur counts:: 1
Display : -13 at time 43000
Stimuli : 4 at time 49000
sample cur counts:: 3
Display : 6 at time 53000
Stimuli : 5 at time 59000
sample cur counts:: 1
最後把 cur counts 加總, 就可以算得 total counts
請加入 UpdateSignalDef 到 power.h 中
回覆刪除//================================
// update list
//================================
template
void TopCss::UpdateSignalDef(string n, T Pre, T Cur, SIGNALTYPE s){
_List_iterator > it;
for(it=SignalList.begin(); it!=SignalList.end(); it++){
if( n.compare(it->Name)==0 ){
it->PreSignal = Pre;
it->CurSignal = Cur;
}
}
}