当前位置: 首页 > news >正文

FPGA基础 -- Verilog 验证平台之 **cocotb 验证 `阶乘计算模块(factorial)` 的例子**

一个完整的 cocotb 验证 阶乘计算模块(factorial) 的例子,涵盖:

  1. Verilog RTL 设计(组合/时序可选)
  2. cocotb Python 验证平台(含随机激励、断言、日志)
  3. 仿真运行说明(使用 iverilog + gtkwave

1. Verilog RTL:阶乘计算器(同步启动、串行计算)

// factorial.v
module factorial (input         clk,input         rst_n,input         start,input  [3:0]  n,         // 最大支持 15!(4位)output reg    done,output reg [31:0] result
);reg [3:0] i;reg [31:0] acc;reg busy;always @(posedge clk or negedge rst_n) beginif (!rst_n) beginacc    <= 1;i      <= 0;busy   <= 0;done   <= 0;result <= 0;end else beginif (start && !busy) beginacc  <= 1;i    <= n;busy <= 1;done <= 0;end else if (busy) beginif (i > 1) beginacc <= acc * i;i   <= i - 1;end else beginresult <= acc;busy   <= 0;done   <= 1;endend else begindone <= 0;endendend
endmodule

2. cocotb 测试代码:测试随机 0~12 的阶乘

# test_factorial.py
import cocotb
from cocotb.triggers import RisingEdge, Timer
from cocotb.clock import Clock
import random
import math@cocotb.test()
async def test_factorial(dut):"""随机测试多组阶乘计算"""# 创建时钟 10ns 周期cocotb.start_soon(Clock(dut.clk, 10, units="ns").start())# 初始化dut.rst_n.value = 0dut.start.value = 0dut.n.value = 0await RisingEdge(dut.clk)dut.rst_n.value = 1await RisingEdge(dut.clk)for trial in range(10):x = random.randint(0, 12)expected = math.factorial(x)# 启动计算dut.n.value = xdut.start.value = 1await RisingEdge(dut.clk)dut.start.value = 0# 等待 donefor _ in range(50):await RisingEdge(dut.clk)if dut.done.value:breakelse:assert False, f"Timeout: DUT failed to complete for n={x}"got = dut.result.value.integerassert got == expected, f"Failed: {x}! = {got}, expected {expected}"dut._log.info(f"PASS: {x}! = {got}")

3. 仿真运行说明(以 Icarus Verilog + cocotb 为例)

文件结构

project/
├── factorial.v
├── test_factorial.py
├── Makefile

Makefile 示例(适配 Icarus)

TOPLEVEL_LANG = verilog
VERILOG_SOURCES = $(PWD)/factorial.v
TOPLEVEL = factorial
MODULE = test_factorialSIM = icarusinclude $(shell cocotb-config --makefiles)/Makefile.sim

4. 运行与结果

make

输出类似:

[INFO] cocotb.regression                         Test Passed: test_factorial
PASS: 5! = 120
PASS: 0! = 1
PASS: 7! = 5040
...

5. 优点总结(相比纯 Verilog TB)

项目cocotb 测试优势
语言友好使用 Python 写用例、随机测试、断言都非常简单
数学函数丰富可直接调用 math.factorial 做黄金模型
日志与报错assert + dut._log.info 提供直观日志
脚本化测试易于与 GitLab CI / pytest 等整合

如需扩展:

  • 加入 边界值测试(如 n = 12、15)
  • 测试 非法输入行为(如 n > 15)
  • 加入 波形支持vcd 输出可通过 $dumpfile 查看
http://www.lqws.cn/news/501373.html

相关文章:

  • 【AI大模型】Spring AI 基于Redis实现对话持久存储详解
  • 报错:macOS 安装 sentencepiece
  • Sui 随全球加速采用,正式启用雅典 SuiHub 创新中心
  • 【动手学深度学习】4.7. 前向传播、反向传播和计算图
  • 【AI时代速通QT】第三节:Linux环境中安装QT并做测试调试
  • Unity反射机制
  • RAG实战 第四章:RAG 检索增强技术与优化
  • 极速JavaScript:全面性能优化实战指南
  • body和后台接口入参格式不一样,为什么可以正确接收
  • 基于海思3403平台开发4目360°全景拼接相机方案
  • go语言多重复值
  • Linux 设备驱动之网络设备驱动
  • 新中国风通用读书颂词分享PPT模版
  • 对手机屏中断路和短路的单元进行切割或熔接,实现液晶线路激光修复原理
  • libevent(1)之基础概述
  • bmc TrueSight 监控 Oracle 11g 配置
  • Flutter 与 原生(Android/iOS)通信 Platform Channel
  • ASP.NET Core 中 Kestrel 的应用及在前后端分离项目中的角色
  • SnowConvert:自动化数据迁移的技术解析与最佳实践
  • 深入JVM:从零到实战,解锁Java性能与调优的终极武器
  • JDK 17 中 java.lang.System 常用方法及应用场景
  • 对于高考边界的理解以及未来就业层级的学习与思考
  • 鸿蒙HarmonyOS 5开发:AlphabetIndexer组件在通讯录中的高效索引实现(附:代码)
  • Linux环境下MariaDB如何实现负载均衡
  • 华为云Flexus+DeepSeek征文 | 基于CCE容器的AI Agent高可用部署架构与弹性扩容实践
  • C++修炼:异常
  • Excel学习04
  • 代理模式:控制对象访问的守门员[特殊字符],优雅实现功能增强与访问控制!
  • 嵌入式Linux驱动开发基础-1 hello驱动
  • 【大模型问题】ms-swift微调时,显存持续增长原因分析与解决方案