SystemC -- 计数器IP模块
1、RTCReal‑Time Counter实时计数模块内部维护秒 / 分 / 小时并提供一个简单寄存器接口读取时间每秒自动递增基于posedge(clk)上升沿触发支持 24 小时制提供read_time()接口也可轻松改成寄存器映射可复位如果clk 1 Hz → 每1 个 clk 上升沿 1 秒如果clk 1000 Hz1 kHz → 要1000 个上升沿 1 秒如果clk 50 MHz → 要50,000,000 个边沿 1 秒秒计数 累计时钟边沿数 ÷ clk 频率RTC Alarm闹钟/定时唤醒#ifndef RTC_H #define RTC_H #include systemc.h class RTC : public sc_module { public: // 时钟与复位 sc_inbool clk; sc_inbool rst_n; // 构造函数 SC_HAS_PROCESS(RTC); RTC(sc_module_name name); // 对外读取接口简单方法非时序 void read_time(uint8_t hour, uint8_t min, uint8_t sec); private: // 时间寄存器 uint8_t sec; uint8_t min; uint8_t hour; // 内部计数进程 void rtc_counter(); }; #endif#include rtc.h RTC::RTC(sc_module_name name) : sc_module(name), sec(0), min(0), hour(0) { SC_METHOD(rtc_counter); sensitive clk.pos(); dont_initialize(); } void RTC::rtc_counter() { if (!rst_n.read()) { sec 0; min 0; hour 0; return; } // 每秒加 11 clk cycle 1 second 模型 sec; if (sec 60) { sec sec % 60; min sec / 60; if (min 60) { hour min / 60; } } } void RTC::read_time(uint8_t h, uint8_t m, uint8_t s) { h hour; m min; s sec; }2、WDGWatchdog Timer看门狗模块有一个递减计数器软件要定期“喂狗”kick()否则计数器减到 0 →复位触发支持使能enable设定超时周期看门狗复位输出wdg_reset#ifndef WDG_H #define WDG_H #include systemc.h class WDG : public sc_module { public: // 时钟与复位 sc_inbool clk; sc_inbool rst_n; // 看门狗复位输出脉冲 sc_outbool wdg_reset; //true进行复位 SC_HAS_PROCESS(WDG); WDG(sc_module_name name, uint32_t timeout_val 100); // 软件接口喂狗 void kick(); // 配置接口 void set_timeout(uint32_t val); private: uint32_t counter; uint32_t timeout; bool enabled; void wdg_process(); }; #endif#include wdg.h WDG::WDG(sc_module_name name, uint32_t timeout_val) : sc_module(name), counter(timeout_val), timeout(timeout_val), enabled(false) { SC_METHOD(wdg_process); sensitive clk.pos(); dont_initialize(); } void WDG::wdg_process() { if (!rst_n.read()) { counter timeout; enabled false; wdg_reset.write(false); return; } if (!enabled) { wdg_reset.write(false); return; } if (counter 0) { counter--; wdg_reset.write(false); } if (counter 0) { // 看门狗超时产生复位脉冲 wdg_reset.write(true); enabled false; // 可配置自动停或自动重载 } } void WDG::kick() { if (enabled) { counter timeout; // 重装计数值喂狗 } } void WDG::set_timeout(uint32_t val) { timeout val; if (enabled) counter val; }✅ 一句话总结这个 WDG 模块在每个时钟沿递减计数软件定期调用kick()喂狗若未及时喂狗计数器归零后输出复位信号是典型的看gate 硬件行为建模。3、Timer模块定时 → 中断向下计数或向上这里用向下更直观可配置load_value计数到 0 → 产生定时中断脉冲支持使能enable重装载auto-reload / one-shot软件写load可重启计时#ifndef TIMER_H #define TIMER_H #include systemc.h class Timer : public sc_module { public: // 时钟与复位 sc_inbool clk; sc_inbool rst_n; // 定时中断输出脉冲 sc_outbool timer_irq; SC_HAS_PROCESS(Timer); Timer(sc_module_name name); // 软件接口 void set_load(uint32_t val); // 设置定时值 void start(); // 启动计时 void stop(); // 停止计时 void reload(bool en); // 使能/关闭自动重载 private: uint32_t counter; uint32在32_t load_value; bool enabled; bool auto_reload; void timer_process(); }; #endif#include timer.h Timer::Timer(sc_module_name name) : sc_module(name), counter(0), load_value(0), enabled(false), auto_reload(true) { SC_METHOD(timer_process); sensitive clk.pos(); dont_initialize(); } void Timer::timer_process() { if (!rst_n.read()) { counter load_value; timer_irq.write(false); return; } timer_irq.write(false); // 默认非中断 if (!enabled) return; if (counter 0) { counter--; } if (counter 0) { timer_irq.write(true); // 定时中断脉冲1 周期 if (auto_reload) counter load_value; else enabled false; // one-shot 模式 } } void Timer::set_load(uint32_t val) { load_value val; if (enabled) counter val; } void Timer::start() { enabled true; counter load_value; } void Timer::stop() { enabled false; } void Timer::reload(bool en) { auto_reload en; }Timer ≠ RTCRTC 是“真实时间”时/分/秒Timer 是“定长计数 → 中断”用于周期性任务、超时检测irq 是单周期脉冲更符合硬件中断握手习惯auto_reloadtrue→ 周期性定时器false→ one-shot单次✅ 一句话总结这个 Timer 模块在每个时钟沿递减计数到 0 产生一个高有效中断脉冲可配置成周期性或单次定时是典型的 SoC 内部定时器建模方式。