Repository navigation
zh getting started more cases
github-actions[bot] edited this page Oct 30, 2026
·
1 revision
目标:验证普通累加和边界。上一页通过只说明“加 3,再保持两拍”这一场景正确。
| 输入序列 | 期望最后的 total | 检查的场景 |
|---|---|---|
| 0 | 0 | 零也是合法输入,不能误当成“没有输入” |
| 3、5 | 8 | 状态能跨输入累加 |
| 255 | 255 | 最大输入值 |
| 255、1 | 0 | 刚好溢出到零 |
| 250、10 | 4 | 溢出后保留低 8 位 |
不要用 dut.total.U() 来计算期望值,那会让 DUT 和自己比较。这里的期望值来自上一页
写明的 8 位累加规则,可以手工算出来。
下面是 examples/getting_started/test_accumulator.py 中本节新增的部分;前面章节的定义继续保留:
@pytest.mark.asyncio
@pytest.mark.parametrize("operands, expected", [
([0], 0),
([3, 5], 8),
([255], 255),
([255, 1], 0),
([250, 10], 4),
])
async def test_accumulator_cases(operands, expected):
dut = TutorialDut()
try:
async with Execution(dut.backend):
for operand in operands:
dut.operand.Set(operand)
dut.enable.Set(1)
await ClockCycles(dut.clock, 1)
dut.enable.Set(0)
assert dut.total.U() == expected
finally:
dut.close()python3 -m pytest -v examples/getting_started/test_accumulator.py::test_accumulator_cases应看到五条 PASSED 和 5 passed。-v 会列出每个用例,便于确认测试表的每一行都执行了。
[3, 5] 是列表,保存两个数;for operand in operands: 按顺序取出它们。
每次循环写一个输入,等一个上升沿,再撤下使能。
@pytest.mark.parametrize("operands, expected", [...]) 可以理解为“用这张表反复调用下面
的测试函数”。表中每一行的两项分别传给 operands 和 expected。每次调用都新建 DUT,
因此不同用例不会继承前一次的累加值。
在表中添加 ([128, 128], 0),预期变为 6 passed。
如果错误地写成 ([128, 128], 256),只有新增的用例失败;8 位寄存器存不下 256。
当前只检查了每组输入后的最终状态,还没有检查每次响应是否都正确、是否都到达。 后续会逐步补上这些检查。先观察代码里反复出现的四行输入动作: 写 operand、拉高 enable、等一拍、撤下 enable。
内容由 CI 自动同步;请编辑主仓库文档。 Automatically synced by CI; edit the source documentation.
- 首页
- 教程
- 操作指南
- 参考
- 原理说明
- Home
-
Tutorials
- Learning path
- 0. Prepare your environment
- 1. Your first PASS and FAIL
- 2. Inputs and clocks
- 3. More cases and boundaries
- 4. Extract a Driver
- 5. Delayed output and a Monitor
- 6. Assemble an Agent
- 7. Models and the test environment
- 8. Pipelines, batches, and cancellation
- 9. Understand failures
- 10. Functional coverage
- Optional: out-of-order responses
- Optional: native clocks and project DUTs
-
How-to guides
- Find your task
- Simulation and events
- Verification components
- Coverage and regressions
- Migration and maintenance
- Runnable examples
- Reference
- Explanation