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

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.xdnf.cn/news/14608.html

相关文章:

  • 攻防世界-MISC-MeowMeowMeow
  • PostgreSQL(知识片):查询/计算Selectivity(可选性)
  • 将两个mp4的文件合并在一起形成新的文件
  • 从0开始学习R语言--Day31--概率图模型
  • 【MV】编排8:基于时间线数据多层分段避免过度拟合特定歌曲
  • 《C++初阶之类和对象》【初始化列表 + 自定义类型转换 + static成员】
  • FunASR搭建语音识别服务和VAD检测
  • 飞算 JavaAI 插件炸场!一小时搭图书管理系统
  • Java并发编程中高效缓存设计的哲学
  • Word2Vec 原理是什么
  • vscode 插件
  • Java底层原理:深入理解JVM内存管理机制
  • C#图书管理系统笔记(残缺版)
  • SQLite3 在嵌入式系统中的应用指南
  • Apache SeaTunnel Spark引擎执行流程源码分析
  • Java SE - 图书管理系统模拟实现
  • 国产麒麟 安装可视化数据库软件DBeaver(图解)
  • 前端开发入门指南:掌握HTML基础
  • 【RK3568 嵌入式linux QT开发笔记】 二维码开源库 libqrencode 交叉静态编译和使用
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DrinkWater(喝水记录组件)
  • DeepSeek中的提示库及其用法示例
  • 用于算法性能预测的 GNN 框架
  • H5新增属性
  • Three.js 中自定义 UV 坐标贴图详解
  • Java数据结构第二十四期:探秘 AVL 树,当二叉搜索树学会 “自我调节”
  • 华为云 Flexus+DeepSeek 征文|增值税发票智能提取小工具:基于大模型的自动化信息解析实践
  • 计算机操作系统(十六)进程同步
  • 安全版V4.5密码加密算法由SM3改为MD5
  • 使用Windows自带的WSL安装Ubuntu Linux系统
  • SQLite FTS4全文搜索实战指南:从入门到优化