* Initial plan * Add cocotb test infrastructure and testbenches for APB3, APB4, and AXI4-Lite Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> * Add integration tests, examples, and documentation for cocotb testbenches Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> * Address code review feedback: use relative imports and update installation docs Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> * Add implementation summary document Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> * Merge cocotb dependencies into test group Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> * Add optional cocotb simulation workflow with Icarus Verilog Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: arnavsacheti <36746504+arnavsacheti@users.noreply.github.com>
5.0 KiB
5.0 KiB
Cocotb Testbench Implementation Summary
Overview
This implementation adds comprehensive cocotb-based testbenches for validating generated SystemVerilog bus decoder RTL across multiple CPU interface types (APB3, APB4, and AXI4-Lite).
Files Added
Test Infrastructure
- pyproject.toml - Added cocotb-test dependency group
- tests/cocotb/common/utils.py - Utilities for RDL compilation and code generation
- tests/cocotb/common/apb4_master.py - APB4 Bus Functional Model
- tests/cocotb/Makefile.common - Makefile template for cocotb simulations
Testbenches
-
tests/cocotb/testbenches/test_apb4_decoder.py - APB4 interface tests (3 test cases)
- test_simple_read_write: Basic read/write operations
- test_multiple_registers: Multiple register access
- test_byte_strobe: Byte strobe functionality
-
tests/cocotb/testbenches/test_apb3_decoder.py - APB3 interface tests (2 test cases)
- test_simple_read_write: Basic read/write operations
- test_multiple_registers: Multiple register access
-
tests/cocotb/testbenches/test_axi4lite_decoder.py - AXI4-Lite interface tests (3 test cases)
- test_simple_read_write: Basic read/write operations
- test_multiple_registers: Multiple register access
- test_byte_strobe: Byte strobe functionality
-
tests/cocotb/testbenches/test_apb4_runner.py - Pytest wrapper for running APB4 tests
-
tests/cocotb/testbenches/test_integration.py - Integration tests (9 test cases)
- Tests code generation for all three interfaces
- Tests utility functions
- Validates generated code structure
Documentation & Examples
- tests/cocotb/README.md - Comprehensive cocotb test documentation
- tests/cocotb/examples.py - Example script demonstrating code generation
- tests/README.md - Updated with cocotb test instructions
Features
Bus Functional Models (BFMs)
Each CPU interface has both master and slave BFMs:
- APB4Master/APB4SlaveResponder: Full APB4 protocol with PSTRB support
- APB3Master/APB3SlaveResponder: APB3 protocol without PSTRB
- AXI4LiteMaster/AXI4LiteSlaveResponder: Full AXI4-Lite protocol with separate channels
Test Coverage
- Simple read/write operations: Verify basic decoder functionality
- Multiple registers: Test address decoding for multiple targets
- Register arrays: Validate array handling
- Byte strobes: Test partial word writes (APB4, AXI4-Lite)
- Nested address maps: Validate hierarchical structures
Code Generation Tests
The integration tests validate:
- Code generation for all three CPU interfaces
- Custom module/package naming
- Register arrays
- Nested address maps
- Generated code structure and content
How to Run
Integration Tests (No Simulator Required)
pytest tests/cocotb/testbenches/test_integration.py -v
Example Script
python -m tests.cocotb.examples
Full Simulation Tests (Requires Simulator)
# Install simulator first
apt-get install iverilog # or verilator
# Install cocotb
uv sync --group cocotb-test
# Run tests (when simulator available)
pytest tests/cocotb/testbenches/test_*_runner.py -v
Test Results
✅ Integration Tests: 9/9 Passing
- test_apb4_simple_register
- test_apb3_multiple_registers
- test_axi4lite_nested_addrmap
- test_register_array
- test_get_verilog_sources
- test_compile_rdl_and_export_with_custom_names
- test_cpuif_generation[APB3Cpuif-apb3_intf]
- test_cpuif_generation[APB4Cpuif-apb4_intf]
- test_cpuif_generation[AXI4LiteCpuif-axi4lite_intf]
✅ Existing Unit Tests: 56/56 Passing
- No regressions introduced
- 4 pre-existing failures in test_unroll.py remain unchanged
✅ Security Checks
- No vulnerabilities found in new dependencies (cocotb, cocotb-bus)
- CodeQL analysis: 0 alerts
Design Decisions
- Separate integration tests: Created tests that run without a simulator for CI/CD friendliness
- Relative imports: Used proper Python package structure instead of sys.path manipulation
- Multiple CPU interfaces: Comprehensive coverage of all supported interfaces
- BFM architecture: Reusable Bus Functional Models for each protocol
- Example script: Provides easy-to-run demonstrations of code generation
Future Enhancements
- CI Integration: Add optional CI job with simulator for full cocotb tests
- More test scenarios: Coverage tests, error handling, corner cases
- Avalon MM support: Add testbenches for Avalon Memory-Mapped interface
- Waveform verification: Automated protocol compliance checking
- Performance tests: Bus utilization and throughput testing
Dependencies
Required
- peakrdl-busdecoder (existing)
- systemrdl-compiler (existing)
Optional (for simulation)
- cocotb >= 1.8.0
- cocotb-bus >= 0.2.1
- iverilog or verilator (HDL simulator)
Notes
- Integration tests run in CI without requiring a simulator
- Full simulation tests are marked with pytest.skip when simulator not available
- All code follows project conventions (ruff formatting, type hints)
- Documentation includes both uv (project standard) and pip alternatives