iT邦幫忙

2021 iThome 鐵人賽

DAY 2
1
Software Development

猴子都寫得出來的 RISC-V CPU Emulator系列 第 3

SystemC: 開始囉,再等等!

CPU 是以 clock cycle 為單位,每一小段時間做一點事
等等!我們一開始就沒提到過時間啊!

說的沒錯,就是那個開始和等等!

start

SystemC 進入 sc_main 執行之後,會執行指定的程式,但並不會開始模擬。
這時候,需要創世神打個響指!

sc_core::sc_start();

wait

執行的過程中,要讓系統知道這件事需要做多久。
所以要用 wait 在這邊等一下,才會讓時間前進!

wait(delay);

thread

在 SystemC 的 module 並不會知道我們要它做什麼,
這時候需要先用 SC_HAS_PROCESS 指定 SC_CURRENT_USER_MODULE,
再用 SC_THREAD 註冊要執行的 function 之後,才能讓 SystemC 系統知道這邊有事要做。

HELLO(sc_module_name name) : sc_module(name){
        SC_HAS_PROCESS(HELLO);
        SC_THREAD(hello_thread);
}

實際程式碼

這次將 hello 改成每一個 delay 過後都會輸出一個字元

//main.cpp
#include <vector>

#include "systemc.h"

class HELLO : public sc_module{
public:
        HELLO(sc_module_name name) : sc_module(name){
                SC_HAS_PROCESS(HELLO);
                SC_THREAD(hello_thread);
        }

private:
        std::vector<char> dataMemory{'H', 'e', 'l', 'l', 'o', ',', ' ', 'S', 'y', 's', 't', 'e', 'm', 'C', '!'};
        void hello_thread(void)
        {
                for(int i = 0; i<dataMemory.size()*2; i++)
                {
                        step();
                        wait(delay);
                }
        }

        void step()
        {
                std::cout << "time " << sc_core::sc_time_stamp() << ":" << dataMemory[pc % dataMemory.size()] << std::endl;
                pc++;
        }
        
        sc_core::sc_time  delay = sc_core::sc_time(1, sc_core::SC_NS);
        uint32_t pc = 0;
};

int sc_main(int argc,char** argv){
        HELLO hello("hello");
        sc_core::sc_start();

        return 0;
}

沿用上一篇的 Makefile,執行結果如下

$ make run
./hello

        SystemC 2.3.3-Accellera --- Sep 17 2021 22:09:07
        Copyright (c) 1996-2018 by all Contributors,
        ALL RIGHTS RESERVED
time 0 s:H
time 1 ns:e
time 2 ns:l
time 3 ns:l
time 4 ns:o
time 5 ns:,
time 6 ns: 
time 7 ns:S
time 8 ns:y
time 9 ns:s
time 10 ns:t
time 11 ns:e
time 12 ns:m
time 13 ns:C
time 14 ns:!
time 15 ns:H
time 16 ns:e
time 17 ns:l
time 18 ns:l
time 19 ns:o
time 20 ns:,
time 21 ns: 
time 22 ns:S
time 23 ns:y
time 24 ns:s
time 25 ns:t
time 26 ns:e
time 27 ns:m
time 28 ns:C
time 29 ns:!


上一篇
SystemC: 哈囉,暖個身吧
下一篇
SystemC: 月球轉運站
系列文
猴子都寫得出來的 RISC-V CPU Emulator31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言