SystemCの勉強がてらSC_MODULEを使ったサンプルコードを作成しました。
簡単な回路で、Producerがデータを送信してConsumerでデータを受け取って標準出力させます。
#include <systemc.h>
SC_MODULE(Producer) { // Producerモジュールを作成
sc_out<int> dout;
void generate_data() {
for (int i = 0; i < 10; i++) {
dout.write(i);
wait(1, SC_NS);
}
}
SC_CTOR(Producer) {
SC_THREAD(generate_data);
}
};
SC_MODULE(Consumer) { // Consumerモジュールを作成
sc_in<int> din;
void consume_data() {
for (int i = 0; i < 10; i++) {
cout << "Consumer received data: ";
cout << din.read() << " at ";
cout << sc_time_stamp() << endl;
wait(1, SC_NS);
}
}
SC_CTOR(Consumer) {
SC_THREAD(consume_data);
}
};
SC_MODULE(top) { // Topモジュールを作成
Producer producer;
Consumer consumer;
sc_signal<int> data_channel;
SC_CTOR(top) : producer("Producer"), consumer("Consumer") {
producer.dout(data_channel);
consumer.din(data_channel);
}
};
int sc_main(int argc, char* argv[]) {
top i_top("i_top");
sc_start();
return 0;
}
実行結果
Consumer received data: 0 at 0 s
Consumer received data: 0 at 1 ns
Consumer received data: 1 at 2 ns
Consumer received data: 2 at 3 ns
Consumer received data: 3 at 4 ns
Consumer received data: 4 at 5 ns
Consumer received data: 5 at 6 ns
Consumer received data: 6 at 7 ns
Consumer received data: 7 at 8 ns
Consumer received data: 8 at 9 ns
コメント