当前位置: 首页 > 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 查看

相关文章:

  • 做么户网站怎么去前置审批搜索引擎优化seo信息
  • 北京市建委网站打不开郑州网站seo外包公司
  • 日照网站建设哪一家好松松软文
  • 和朋友合伙做网站营销策划
  • 中国建筑装饰网唐迪夫优化大师windows
  • 四川建设网官网站seo基本流程
  • CRMEB PHP多门店版v3.2.1系统全开源+Uniapp前端+搭建教程
  • 用vscode破解最新typora1.10.8
  • 前端vue2每三十秒被动接受后端服务器发送过来得数据
  • 服务器数据恢复——异常断电导致服务器故障的数据恢复案例
  • 编程语言的发展逻辑:从人类认知到人工智能协同
  • C预处理详解2
  • WHAT - React Native 的 Expo Router
  • Redis哈希表Rehash全解析:扩容缩容背后的渐进式智慧
  • ref() 与 reactive()
  • 黑马Day01-03集开始
  • 原子操作(CAS)
  • 《TCP/IP 详解 卷1:协议》第13章:TCP连接管理
  • java-SpringBoot框架开发计算器网页端编程练习项目【web版】
  • 马克思主义基本原理知识笔记
  • MediaMarktSaturn EDI 对接指南:欧洲零售卖场的数字化协同范例
  • 虚幻基础:插槽
  • C++面试6——类和结构体的区别和使用场景
  • 零基础学习RabbitMQ(3)--核心概念
  • 打包上传到Linux部署并启动
  • C++ string类的操作