|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +chipflow-lib is a Python library for working with the ChipFlow platform, enabling users to build ASIC (Application Specific Integrated Circuit) designs using the Amaranth HDL framework. The library provides a CLI tool (`chipflow`) that handles design elaboration, simulation, and submission to the ChipFlow cloud builder. |
| 8 | + |
| 9 | +## Build and Test Commands |
| 10 | + |
| 11 | +### Installation |
| 12 | +- Install dependencies: `pdm install` |
| 13 | +- Python 3.11+ required |
| 14 | +- Uses PDM for dependency management |
| 15 | + |
| 16 | +### Testing |
| 17 | +- Run all tests: `pdm test` |
| 18 | +- Run with coverage: `pdm test-cov` |
| 19 | +- Run with HTML coverage report: `pdm test-cov-html` |
| 20 | +- Run single test: `pdm run pytest tests/test_file.py::test_function_name` |
| 21 | +- Run test for specific module with coverage: `pdm run python -m pytest --cov=chipflow_lib.MODULE tests/test_file.py -v` |
| 22 | + |
| 23 | +### Linting |
| 24 | +- Run all linting checks: `pdm lint` |
| 25 | + - Includes: license header check, ruff linting, and pyright type checking |
| 26 | +- Run ruff only: `pdm run ruff check` |
| 27 | +- Run pyright only: `pdm run pyright chipflow_lib` |
| 28 | + |
| 29 | +### Documentation |
| 30 | +- Build docs: `pdm docs` |
| 31 | +- Test documentation: `pdm test-docs` |
| 32 | + |
| 33 | +### Running the CLI |
| 34 | +- Run chipflow CLI: `pdm chipflow <command>` |
| 35 | + |
| 36 | +## High-Level Architecture |
| 37 | + |
| 38 | +### Core Components |
| 39 | + |
| 40 | +1. **CLI System** (`cli.py`): |
| 41 | + - Entry point for the `chipflow` command |
| 42 | + - Dynamically loads "steps" (silicon, sim, software) from configuration |
| 43 | + - Steps can be extended via `chipflow.toml` `[chipflow.steps]` section |
| 44 | + - Parses `chipflow.toml` configuration using Pydantic models |
| 45 | + |
| 46 | +2. **Configuration System**: |
| 47 | + - `chipflow.toml`: User project configuration file (must exist in `CHIPFLOW_ROOT`) |
| 48 | + - `config_models.py`: Pydantic models defining configuration schema |
| 49 | + - `config.py`: Configuration file parsing logic |
| 50 | + - Key configuration sections: `[chipflow]`, `[chipflow.silicon]`, `[chipflow.simulation]`, `[chipflow.software]`, `[chipflow.test]` |
| 51 | + |
| 52 | +3. **Platform Abstraction** (`platforms/`): |
| 53 | + - `SiliconPlatform`: Targets ASIC fabrication (supports SKY130, GF180, GF130BCD, IHP_SG13G2, HELVELLYN2) |
| 54 | + - `SimPlatform`: Targets simulation (builds C++ CXXRTL simulator) |
| 55 | + - `SoftwarePlatform`: RISC-V software build support |
| 56 | + - Each platform has process-specific port types (e.g., `Sky130Port` with drive mode configuration) |
| 57 | + |
| 58 | +4. **Steps System** (`steps/`): |
| 59 | + - Extensible command architecture |
| 60 | + - `silicon.py`: Handles ASIC preparation and cloud submission |
| 61 | + - `prepare`: Elaborates Amaranth design to RTLIL |
| 62 | + - `submit`: Submits design to ChipFlow cloud builder (requires `CHIPFLOW_API_KEY`) |
| 63 | + - `sim.py`: Simulation workflow |
| 64 | + - `build`: Builds CXXRTL simulator |
| 65 | + - `run`: Runs simulation with software |
| 66 | + - `check`: Validates simulation against reference events |
| 67 | + - `software.py`: RISC-V software compilation |
| 68 | + |
| 69 | +5. **Pin Locking System** (`_pin_lock.py`): |
| 70 | + - `chipflow pin lock`: Allocates physical pins for design components |
| 71 | + - Generates `pins.lock` file with persistent pin assignments |
| 72 | + - Attempts to reuse previous allocations when possible |
| 73 | + - Package definitions in `_packages.py` define available pins per package |
| 74 | + |
| 75 | +6. **IO Annotations** (`platforms/_utils.py`, `platforms/_signatures.py`): |
| 76 | + - IO signatures define standard interfaces (JTAG, SPI, I2C, UART, GPIO, QSPI) |
| 77 | + - `IOModel` configures electrical characteristics (drive mode, trip point, inversion) |
| 78 | + - Annotations attach metadata to Amaranth components for automatic pin allocation |
| 79 | + |
| 80 | +### Key Design Patterns |
| 81 | + |
| 82 | +1. **Component Discovery via Configuration**: |
| 83 | + - User defines top-level components in `[chipflow.top]` section as `name = "module:ClassName"` |
| 84 | + - `_get_cls_by_reference()` dynamically imports and instantiates classes |
| 85 | + - `top_components()` returns dict of instantiated components |
| 86 | + |
| 87 | +2. **Port Wiring**: |
| 88 | + - `_wire_up_ports()` in `steps/__init__.py` automatically connects platform ports to component interfaces |
| 89 | + - Uses pin lock data to map logical interface names to physical ports |
| 90 | + - Handles signal inversion, direction, and enable signals |
| 91 | + |
| 92 | +3. **Build Process**: |
| 93 | + - Amaranth elaboration → RTLIL format → Yosys integration → Platform-specific output |
| 94 | + - For silicon: RTLIL sent to cloud builder with pin configuration |
| 95 | + - For simulation: RTLIL → CXXRTL C++ → compiled simulator executable |
| 96 | + |
| 97 | +4. **Error Handling**: |
| 98 | + - Custom `ChipFlowError` exception for user-facing errors |
| 99 | + - Causes are preserved and printed with `traceback.print_exception(e.__cause__)` |
| 100 | + - CLI wraps unexpected exceptions in `UnexpectedError` with debug context |
| 101 | + |
| 102 | +## Code Style |
| 103 | + |
| 104 | +- Follow PEP-8 style |
| 105 | +- Use `snake_case` for Python |
| 106 | +- Type hints required (checked by pyright in standard mode) |
| 107 | +- Ruff linting enforces: E4, E7, E9, F, W291, W293 (ignores F403, F405 for wildcard imports) |
| 108 | +- All files must have SPDX license header: `# SPDX-License-Identifier: BSD-2-Clause` |
| 109 | +- No trailing whitespace |
| 110 | +- No whitespace on blank lines |
| 111 | + |
| 112 | +## Testing Notes |
| 113 | + |
| 114 | +- Tests located in `tests/` directory |
| 115 | +- Fixtures in `tests/fixtures/` |
| 116 | +- Use public APIs when testing unless specifically instructed otherwise |
| 117 | +- CLI commands count as public API |
| 118 | +- Test coverage enforced via pytest-cov |
| 119 | + |
| 120 | +## Common Workflows |
| 121 | + |
| 122 | +### Submitting a Design to ChipFlow Cloud |
| 123 | +1. Create `chipflow.toml` with `[chipflow.silicon]` section defining process and package |
| 124 | +2. Run `chipflow pin lock` to allocate pins |
| 125 | +3. Run `chipflow silicon prepare` to elaborate design |
| 126 | +4. Set `CHIPFLOW_API_KEY` environment variable |
| 127 | +5. Run `chipflow silicon submit --wait` to submit and monitor build |
| 128 | + |
| 129 | +### Running Simulation |
| 130 | +1. Run `chipflow sim build` to build simulator |
| 131 | +2. Run `chipflow sim run` to run simulation (builds software automatically) |
| 132 | +3. Run `chipflow sim check` to validate against reference events (requires `[chipflow.test]` configuration) |
| 133 | + |
| 134 | +## Environment Variables |
| 135 | + |
| 136 | +- `CHIPFLOW_ROOT`: Project root directory (auto-detected if not set) |
| 137 | +- `CHIPFLOW_API_KEY`: API key for cloud builder authentication |
| 138 | +- `CHIPFLOW_API_KEY_SECRET`: Deprecated, use `CHIPFLOW_API_KEY` instead |
| 139 | +- `CHIPFLOW_API_ORIGIN`: Cloud builder URL (default: https://build.chipflow.org) |
| 140 | +- `CHIPFLOW_BACKEND_VERSION`: Developer override for backend version |
| 141 | +- `CHIPFLOW_SUBMISSION_NAME`: Override submission name (default: git commit hash) |
0 commit comments