其他分享
首页 > 其他分享> > SystemC事务级建模01之最简单的模块间数据传输

SystemC事务级建模01之最简单的模块间数据传输

作者:互联网

现在来看一个非常简单的情形:CPU传输数据给GPU。我们使用SystemC来对该情形进行事务级的建模。

CPU是事务的发起者,GPU是事务的接收者。它们通过 Socket连接(注意,这里的Socket不是网络中的那个Socket)。事务发起者的Socket类型是simple_initiator_socket,事务接收者的Socket类型是simple_target_socket

先给CPU建模,

#include <systemc.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/simple_target_socket.h>

class Cpu : public sc_module {
public:
    SC_HAS_PROCESS(Cpu);
    Cpu(sc_module_name name) : sc_module(name) {

    }

public:
    tlm_utils::simple_initiator_socket<Cpu> initiator_socket_gpu;
};

再给GPU建模,

#include <systemc.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/simple_target_socket.h>

class Gpu : public sc_module {
public:
    SC_HAS_PROCESS(Gpu);
    Gpu(sc_module_name name) : sc_module(name) {

    }

public:
    tlm_utils::simple_target_socket<Gpu> target_socket_cpu;
};

然后,我们需要把CPUGPUSocket连接起来,

int sc_main(int argc, char *argv[]) {
    Cpu cpu{"cpu"};
    Gpu gpu{"gpu"};
    cpu.initiator_socket_gpu(gpu.target_socket_cpu);
    return 0;
}

接着,我们尝试从CPU发送一个数据到GPU

CPU中加入一个send方法,并将其声明为方法进程,负责往GPU发送一个消息:Hello, I am CPU

class Cpu : public sc_module {
public:
    SC_HAS_PROCESS(Cpu);
    Cpu(sc_module_name name) : sc_module(name) {
        SC_METHOD(send);
    }

    void send() {
        auto payload = new tlm::tlm_generic_payload{};
        auto data = std::string{"Hello, I am CPU"};
        payload->set_data_ptr((unsigned char *) data.c_str());
        auto delay = SC_ZERO_TIME;
        initiator_socket_gpu->b_transport(*payload, delay);
    }

public:
    tlm_utils::simple_initiator_socket<Cpu> initiator_socket_gpu;
};

GPU要接收CPU发送过来的字符串,需要,

  1. target_socket上注册一个接收的方法,这里叫receive
  2. payload中取出CPU发送过来的消息
class Gpu : public sc_module {
public:
    SC_HAS_PROCESS(Gpu);
    Gpu(sc_module_name name) : sc_module(name) {
        target_socket_cpu.register_b_transport(this, &Gpu::recieve);
    }

    void recieve(tlm::tlm_generic_payload &payload, sc_time &delay) {
         std::cout << payload.get_data_ptr() << std::endl;
    }

public:
    tlm_utils::simple_target_socket<Gpu> target_socket_cpu;
};

编译,运行,

C:\Users\adam\CLionProjects\untitled\cmake-build-debug\untitled.exe

        SystemC 2.3.3-Accellera --- Mar 10 2021 20:59:57
        Copyright (c) 1996-2018 by all Contributors,
        ALL RIGHTS RESERVED
Hello, I am CPU

Process finished with exit code 0

搞定!

标签:01,name,建模,module,public,socket,sc,CPU,SystemC
来源: https://www.cnblogs.com/cngpus/p/16229669.html