クロックカウンタを作る
ここでは、クロックカウンタを作成することで、モジュール作成、プロセスの作成等について説明する。
サンプル

- main.cpp
- ClockCounter.h
- ClockCounter.cpp
- Makefile
コンパイル/実行
$ make <== コンパイル $ ./run.x <== シミュレーション実行
実行結果
[[SystemC]] 2.2.0 --- Jun 28 2008 10:19:12 Copyright (c) 1996-2006 by all Contributors ALL RIGHTS RESERVED Note: VCD trace timescale unit is set by user to 1.000000e-09 sec. 0 s : CntOut = 0 10 ns : CntOut = 1 60 ns : CntOut = 6 195 ns : CntOut = 20 232 ns : CntOut = 24 732 ns : CntOut = 74
実行すると10nsごとにCntOutが1加算されている。
クロックは、10nsの周期となっている。
クロックは、10nsの周期となっている。
モデル説明
階層構造
sc_main |===> clockCounter(ClockCounter)
クロックカウンタ・入出力信号
方向 | 型 | 信号名 | 説明 |
input | bool | clk | クロック入力 |
output | unsigned int | CntOut | クロックカウント値出力 |
ソースコード内容
ClcokCounter.h
#ifndef __CLOCK_COUNTER_H
#define __CLOCK_COUNTER_H
#include <systemc.h>
SC_MODULE( ClockCounter )
{
//-- ports ----------------------------------------------------//
sc_in_clk clk;
sc_out<unsigned int> CntOut;
//-- signals --------------------------------------------------//
//-- variables ------------------------------------------------//
//-- process functions ----------------------------------------//
void main_thread();
//-- functions ------------------------------------------------//
// constructor
SC_HAS_PROCESS( ClockCounter );
ClockCounter( sc_module_name );
};
#endif /* __CLOCK_COUNTER_H */
- "#include <systemc.h>"でSystemCライブラリのヘッダをインクルード
- SC_MODULEにてClockCounterモジュールを定義
- クロック信号clkを入力ポートとして定義
- クロックカウント値を出力するためのCntOut信号を出力ポートとして定義
- プロセス間数となるmain_proc関数を定義
- コンストラクタを定義
- プロセスを持つモジュールのためSC_HAS_PROCESSを定義
ClockCounter.cpp
#include "ClockCounter.h"
// constructor
ClockCounter::ClockCounter( sc_module_name name )
: sc_module( name )
, clk( "clk" )
, CntOut( "CntOut" )
{
// process setting
SC_THREAD( main_thread );
//sensitive << clk.pos();
}
void ClockCounter::main_thread()
{
unsigned int cnt = 0;
CntOut.write( 0 );
while (true)
{
//wait();
wait( clk.posedge_event() );
++cnt;
CntOut.write( cnt );
}
}
- コンストラクタ記述内
- SC_MODULEによりsc_moduleを継承するので、sc_moduleの初期化を記述
- main_proc関数をSC_THREADに指定
- プロセス関数main_proc記述内
- クロックカウント値を保持する変数cntを用意、初期値ゼロに設定
- CntOut信号の出力を初期値ゼロに設定
- 無限ループ*while(true))を記述
- wait関数によりクロックの立上りを待つ
- カウント値のインクリメントしたらCntOutへ出力
main.cpp
#include <systemc.h>
#include "ClockCounter.h"
int sc_main( int argc, char* argv[] )
{
ClockCounter *clockCounter;
// signals
sc_clock clk( "clk", 10.0, SC_NS );
sc_signal<unsigned int> CntOut;
// module instance & binding
clockCounter = new ClockCounter( "ClockCounter" );
sc_assert( clockCounter );
clockCounter->clk( clk );
clockCounter->CntOut( CntOut );
/////////////////////////////////////////////////////////////////////////
// vcd dump
sc_trace_file *trace_f;
trace_f = sc_create_vcd_trace_file( "systemc" ); // 出力フェイル名を指定する
sc_assert( trace_f );
trace_f->set_time_unit( 1.0, SC_NS );
sc_trace( trace_f, clk, "clk" );
sc_trace( trace_f, CntOut, "CntOut" );
/////////////////////////////////////////////////////////////////////////
// initialize
sc_start( SC_ZERO_TIME );
cout << sc_time_stamp() << " : " << "CntOut = " << CntOut.read() << endl;
// simulation
sc_start( 10.0, SC_NS );
cout << sc_time_stamp() << " : " << "CntOut = " << CntOut.read() << endl;
sc_start( 50.0, SC_NS );
cout << sc_time_stamp() << " : " << "CntOut = " << CntOut.read() << endl;
sc_start( 135.0, SC_NS );
cout << sc_time_stamp() << " : " << "CntOut = " << CntOut.read() << endl;
sc_start( 37.0, SC_NS );
cout << sc_time_stamp() << " : " << "CntOut = " << CntOut.read() << endl;
sc_start( 500.0, SC_NS );
cout << sc_time_stamp() << " : " << "CntOut = " << CntOut.read() << endl;
/////////////////////////////////////////////////////////////////////////
delete clockCounter;
sc_close_vcd_trace_file( trace_f );
return 0;
}
- systemc.hヘッダファイルをインクルード
- ClockCounter.hヘッダファイルをインクルード
- メイン関数としてsc_mainを使用
- sc_clockによりクロックを作成、周期は10nsに設定
- CntOut信号を受け取るsc_signalを用意
- ClockCounterをインスタンスする
- clockCounterのclk,CntOut信号を接続
- 波形出力設定, ファイルはVCD形式,clk,CntOut信号を出力対象とする
- シミュレーション実行
- sc_start(SC_ZERO_TIME)ですべてのプロセスで最初のwaitまで実行
- sc_start実行後、CntOut信号出力を表示、10nsごとに1カウント去れていることを確認
添付ファイル