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:!


<<:  【从零开始的Swift开发心路历程-Day6】简易调色盘Part2

>>:  Day 3:AWS是什麽?30天从动漫/影视作品看AWS服务应用 -《Vivy -Fluorite Eye's Song》Part 3

Day11 - 物理模拟篇 - 弹跳球世界II - 成为Canvas Ninja ~ 理解2D渲染的精髓

继上一篇我们讲到向量类的建立,接着我们在这一篇文机会提到反射行为的模拟~ 反射这种行为,在反射面为铅...

[DAY18]跟 Vue.js 认识的30天 - Vue 混入(`mixin`)

混入(mixin)似乎也是个初学 Vue 比较少被用到的功能,但还是照顺序的了解一下。 基础运用 在...

Day 16: 物件导向设计、函数式设计 (待改进中... )

「什麽是物件导向? 对软件架构师来说: 物件导向是透过使用多型(Polymorphism) 来获得...

ProxmoxVE PVE VM 安装 ChromeOS

ProxmoxVE PVE VM 安装 ChromeOS ChromeOS 版本 Download ...

[DAY-08] 增进诚实敢言 把一切摊在阳光下

人们如果主动隐瞒某些事 反而会花两倍时间想着那着些事 秘密的问题在於 只要你说出来 他就不再是秘密...