Testing framework
Cocotb overview
Cocotb allows you to write hardware tests in Python, providing:- Asynchronous test execution with
async/await - Clock generation and synchronization
- Signal manipulation and monitoring
- Assertion-based testing
- VCD waveform generation
Fixed-point helpers
All Tiny TPU modules use Q8.8 fixed-point format (8 integer bits, 8 fractional bits). Tests include helper functions for conversion:Running tests
Run a specific test
To run tests for a specific module:Available test targets
The Makefile includes test targets for all modules:Test execution flow
When you runmake test_<MODULE_NAME>, the following happens:
Compilation
iverilog compiles the module and all dependencies:
-s <MODULE_NAME>specifies the top-level module-s dumpincludes the dump module for VCD generation-g2012enables SystemVerilog 2012 features
Simulation
VVP (the iverilog runtime) executes the simulation with cocotb:
MODULE=test_<MODULE_NAME>specifies which Python test file to run- Cocotb loads as a VPI plugin to iverilog
Result checking
The test checks for failures in the generated XML report:If this command finds “failure”, the test fails.
Writing tests
Basic test structure
Here’s a complete example test for a processing element:test/test_pe.py
Key testing patterns
Clock generation
Clock generation
Always create a clock at the start of your test:This creates a 10ns period clock (100MHz).
Reset sequence
Reset sequence
Always reset your module before testing:
Synchronization
Synchronization
Use triggers to synchronize with the clock:
Assertions
Assertions
Use Python assertions to verify behavior:
Test organization
All test files are located in thetest/ directory:
- Helper functions for fixed-point conversion
- One or more
@cocotb.test()decorated functions - Comprehensive test cases covering various scenarios
Debugging failed tests
If a test fails:- Check the console output - Cocotb prints detailed error messages
- Review results.xml - Contains detailed test results
- View the waveforms - See waveforms guide
- Add debug prints - Use
dut._log.info()in your test
Continuous integration
The test suite can be run automatically in CI/CD pipelines. The tests exit with:- Exit code 0 on success
- Non-zero exit code on failure
Next steps
Waveforms
Learn how to view and analyze test waveforms
Adding modules
Add new modules with proper tests