階層構造を出力する SystemC

はじめに

デバッグ用にSystemCの階層構造(hierarchy)をprintfする関数を作成しました。

解説

print_hierarhy()を実行することで、インスタンスされているモジュール、ポート、スレッドを出力します。

print_hierarchy()
// 再帰的に階層構造を取得
void _print_hierarchy(sc_object* parent){
    std::cout << parent->name() << "," << parent->kind() << std::endl;
    if (parent->kind() == "sc_module"){
        for (int i = 0; i < parent->get_child_objects().size(); i++) {
            sc_object* children = parent->get_child_objects()[i];
            _print_hierarchy(children);
        }
    }
}
// 階層構造を出力する関数
void print_hierarchy(){
    for (int i = 0; i < sc_get_top_level_objects().size(); i++) {
        sc_object* children = sc_get_top_level_objects()[i]; // sc_mainのインスタンスを出力
        _print_hierarchy(children);
    }
}

使用例

下記のように、TOPにdff1, dff2, subをインスタンスしたSystemCコードに対して実行してみます。

sample.cpp
#include <systemc.h>

SC_MODULE(SUB) {
    SC_CTOR(SUB)
    {
        SC_THREAD(flip);
    }

    void flip() {
        while(true) {
            wait();
        }
    }
};

SC_MODULE(DFF) {
    sc_in<bool> SC_NAMED(clk);
    sc_in<bool> SC_NAMED(d);
    sc_out<bool> SC_NAMED(q);

    SC_CTOR(DFF)
    {
        SC_CTHREAD(flip, clk.pos());
    }

    void flip() {
        while(true) {
            wait();
            q.write(d.read());
        }
    }
};

SC_MODULE(TOP) {
    sc_in<bool> SC_NAMED(clk);
    sc_in<bool> SC_NAMED(d);
    sc_out<bool> SC_NAMED(q);

    sc_signal<bool> SC_NAMED(buf);

    DFF dff1;
    DFF dff2;
    SUB sub1;

    SC_CTOR(TOP):
        dff1("dff1"),
        dff2("dff2"),
        sub1("sub1")
    {
        dff1(clk, d, buf);
        dff2(clk, buf, q);
    }
};

void _print_hierarchy(sc_object* parent){
    std::cout << parent->name() << "," << parent->kind() << std::endl;
    if (parent->kind() == "sc_module"){
        for (int i = 0; i < parent->get_child_objects().size(); i++) {
            sc_object* children = parent->get_child_objects()[i];
            _print_hierarchy(children);
        }
    }
}

void print_hierarchy(){
    for (int i = 0; i < sc_get_top_level_objects().size(); i++) {
        sc_object* children = sc_get_top_level_objects()[i];
        _print_hierarchy(children);
    }
}

int sc_main(int argc, char* argv[]) {
    TOP top("top");

    // 階層構造を出力する
    print_hierarchy();

    return 0;
}

出力

top,sc_module
top.clk,sc_in
top.d,sc_in
top.q,sc_out
top.buf,sc_signal
top.dff1,sc_module
top.dff1.clk,sc_in
top.dff1.d,sc_in
top.dff1.q,sc_out
top.dff1.flip,sc_cthread_process
top.dff2,sc_module
top.dff2.clk,sc_in
top.dff2.d,sc_in
top.dff2.q,sc_out
top.dff2.flip,sc_cthread_process
top.sub1,sc_module
top.sub1.flip,sc_thread_process

まとめ

SystemCで階層構造を再帰的に出力する関数を作成しました。

デバッグするときにご利用ください。

コメント

タイトルとURLをコピーしました