[SC]SystemC在CPU/GPU验证中的应用(四)

SystemC在CPU/GPU验证中的应用(四)

       摘要:下面分享50个逐步升级SystemC编程能力的示例及建议的学习路线图。您可以一次一批地完成它们——从前五个基础的例子开始,然后转向channels, TLM, bus models, simple CPU/GPU kernels等等。在每个阶段掌握之后,再进行下一组的学习。


50个代表性的SystemC例子

  1. Hello, SystemC! (module + sc_main)
  2. Simple clock generator
  3. 4-bit up/down counter
  4. Blocking FIFO channel
  5. Non-blocking handshake channel
  6. Combinational AND/OR modules
  7. D-flip‐flop with async reset
  8. 8×1 multiplexer
  9. Simple RAM model (blocking accesses)
  10. Simple ROM model
  11. Dual-port RAM
  12. Bus arbiter (round-robin)
  13. TLM2.0 blocking transport (initiator)
  14. TLM2.0 blocking transport (target)
  15. TLM2.0 non-blocking transport
  16. TLM2.0 analysis port / export
  17. Simple AXI-Lite bus model
  18. AXI-Lite master + slave example
  19. Quantum keeper & time annotation
  20. tlm_utils::simple_initiator_socket
  21. tlm_utils::simple_target_socket
  22. Hierarchical module instantiation
  23. Dynamic process spawn & kill
  24. Event notification & sc_event_queue
  25. Reset synchronization circuit
  26. Clock domain crossing FIFO
  27. Bus monitor / tracer (TLM analysis)
  28. Memory-mapped register file
  29. Interrupt controller model
  30. Pipeline stage model (fetch/decode/execute)
  31. Simple 4-stage CPU datapath
  32. Cache model (direct-mapped)
  33. DMA engine model
  34. GPGPU kernel launcher skeleton
  35. GPU shader core (vector add)
  36. Barrier synchronization (sc_barrier emulation)
  37. Producer-consumer with sc_mutex
  38. sc_semaphore example
  39. SystemC-AMS basic RC filter
  40. Fixed-point arithmetic with sc_fixed
  41. Power‐aware sc_trace (VCD generation)
  42. Cross-trade-off analysis (timing vs. power)
  43. SystemC assertions (SC_ASSERT)
  44. UVM-SystemC basic use case
  45. Co-simulation stub (Verilog DPI)
  46. SystemC Python binding stub
  47. Parameterized module (SC_MODULE_T)
  48. TLM-2.0 generic payload extensions
  49. Simple NoC router model
  50. Full mini‐SOC: CPU + L2 cache + memory + interconnect

Fourth Batch: Examples 21–30

Below are the first five examples with complete code + detailed comments.

21. tlm_utils::simple_target_socket 示例

文件名:tlm_simple_target.cpp

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

using namespace sc_core;
using namespace tlm;
using namespace std;

// Initiator 模块:通过 simple_initiator_socket 发起读写事务
SC_MODULE(Initiator) {
    tlm_utils::simple_initiator_socket<Initiator> socket;

    SC_CTOR(Initiator)
    : socket("socket")
    {
        SC_THREAD(thread_process);
    }

    void thread_process() {
        // 1) 写事务
        unsigned int data = 0xDEADBEEF;
        tlm_generic_payload txn;
        sc_time delay = SC_ZERO_TIME;

        txn.set_command(TLM_WRITE_COMMAND);
        txn.set_address(4);
        txn.set_data_ptr(reinterpret_cast<unsigned char*>(&data));
        txn.set_data_length(4);
        txn.set_streaming_width(4);

        cout << sc_time_stamp() << " Initiator: start WRITE\n";
        socket->b_transport(txn, delay);
        wait(delay);
        cout << sc_time_stamp() << " Initiator: WRITE done\n";

        // 2) 读事务
        data = 0;
        txn.set_command(TLM_READ_COMMAND);
        cout << sc_time_stamp() << " Initiator: start READ\n";
        socket->b_transport(txn, delay);
        wait(delay);
        cout << sc_time_stamp()
             << " Initiator: READ data=0x" << hex << data << dec << "\n";

        sc_stop();
    }
};

// Target 模块:通过 simple_target_socket 接收事务
SC_MODULE(Target) {
    tlm_utils::simple_target_socket<Target> socket;
    unsigned int mem[16];

    SC_CTOR(Target)
    : socket("socket")
    {
        // 注册 b_transport 回调
        socket.register_b_transport(this, &Target::b_transport);
        // 初始化内存
        for (int i = 0; i < 16; ++i) mem[i] = i;
    }

    // Blocking transport 回调
    void b_transport(tlm_generic_payload& trans, sc_time& delay) {
        unsigned int addr = trans.get_address() / 4;
        unsigned char* ptr = trans.get_data_ptr();

        // 模拟访问延迟
        delay += sc_time(10, SC_NS);
        if (trans.is_write()) {
            unsigned int w = *reinterpret_cast<unsigned int*>(ptr);
            cout << sc_time_stamp()
                 << " Target: WRITE mem[" << addr << "]=" << w << "\n";
            mem[addr] = w;
        } else {
            unsigned int r = mem[addr];
            *reinterpret_cast<unsigned int*>(ptr) = r;
            cout << sc_time_stamp()
                 << " Target: READ  mem[" << addr << "]=" << r <<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元直数字电路验证

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值