Skip to content

Commit 28f67f4

Browse files
robtaylorclaude
andcommitted
Improve custom simulation model documentation
Replace incomplete code example with practical guidance: - Point to existing model implementations in chipflow_lib/common/sim/ - Reference CXXRTL documentation and runtime source - Show simplified model registration example - Add note that comprehensive CXXRTL runtime docs are planned This provides actionable guidance while acknowledging that detailed CXXRTL runtime documentation should be separate work. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5b7e86c commit 28f67f4

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

docs/simulation-guide.rst

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -370,64 +370,56 @@ Customizing Simulation
370370
Adding Custom Models
371371
~~~~~~~~~~~~~~~~~~~~
372372

373-
To add a custom peripheral model:
373+
ChipFlow's built-in simulation models cover common peripherals (UART, SPI, I2C, GPIO, QSPI Flash). For custom peripherals, you'll need to write C++ models that interact with the CXXRTL-generated design.
374374

375-
1. **Write the C++ Model**
375+
**Learning Resources:**
376376

377-
Create ``design/sim/my_model.h``:
377+
1. **Study existing models**: The best way to learn is to examine ChipFlow's built-in implementations:
378378

379-
.. code-block:: cpp
379+
- ``chipflow_lib/common/sim/models.h`` - Model interfaces and helper functions
380+
- ``chipflow_lib/common/sim/models.cc`` - Complete implementations for:
380381

381-
#pragma once
382-
#include <cxxrtl/cxxrtl.h>
382+
- ``uart`` - UART transceiver with baud rate control
383+
- ``spiflash`` - QSPI flash memory with command processing
384+
- ``spi`` - Generic SPI peripheral
385+
- ``i2c`` - I2C bus controller with start/stop detection
383386

384-
namespace my_design {
387+
2. **CXXRTL Runtime API**: Models interact with the generated design using CXXRTL's API:
385388

386-
template<size_t WIDTH>
387-
class my_peripheral_model {
388-
cxxrtl::value<WIDTH>& output;
389-
cxxrtl::value<WIDTH>& input;
389+
- `CXXRTL Documentation <https://yosyshq.readthedocs.io/projects/yosys/en/latest/cmd/write_cxxrtl.html>`_ - Command reference
390+
- CXXRTL runtime source: ``yosys/backends/cxxrtl/runtime/`` (in Yosys repository)
391+
- Key types: ``cxxrtl::value<WIDTH>`` for signal access, ``.get()`` to read, ``.set()`` to write
390392

391-
public:
392-
my_peripheral_model(const char* name,
393-
cxxrtl::value<WIDTH>& o,
394-
cxxrtl::value<WIDTH>& i)
395-
: output(o), input(i) {}
393+
**Model Registration:**
396394

397-
void step(unsigned timestamp) {
398-
// Model behavior - use .get() for reading, .set() for writing
399-
input.set(output.get());
400-
}
401-
};
395+
Once you've written a model (e.g., ``design/sim/my_model.h``), register it with ChipFlow:
402396

403-
} // namespace my_design
404-
405-
2. **Create a SimModel**
406-
407-
In your custom SimStep:
397+
.. code-block:: python
408398
409-
.. code-block:: python
399+
from chipflow_lib.platform import SimPlatform, SimModel, BasicCxxBuilder
400+
from pathlib import Path
410401
411-
from chipflow_lib.platform import SimPlatform, SimModel, BasicCxxBuilder
402+
MY_BUILDER = BasicCxxBuilder(
403+
models=[
404+
SimModel('my_peripheral', 'my_design', MyPeripheralSignature),
405+
],
406+
hpp_files=[Path('design/sim/my_model.h')],
407+
)
412408
413-
MY_BUILDER = BasicCxxBuilder(
414-
models=[
415-
SimModel('my_peripheral', 'my_design', MyPeripheralSignature),
416-
],
417-
hpp_files=[Path('design/sim/my_model.h')],
418-
)
409+
class MySimStep(SimStep):
410+
def __init__(self, config):
411+
super().__init__(config)
412+
self.platform._builders.append(MY_BUILDER)
419413
420-
class MySimPlatform(SimPlatform):
421-
def __init__(self, config):
422-
super().__init__(config)
423-
self._builders.append(MY_BUILDER)
414+
Then reference your custom step in ``chipflow.toml``:
424415

425-
3. **Reference in chipflow.toml**
416+
.. code-block:: toml
426417
427-
.. code-block:: toml
418+
[chipflow.steps]
419+
sim = "my_design.steps.sim:MySimStep"
428420
429-
[chipflow.steps]
430-
sim = "my_design.steps.sim:MySimStep"
421+
.. note::
422+
Comprehensive CXXRTL runtime documentation is planned for a future release. For now, refer to existing model implementations and the Yosys CXXRTL source code.
431423

432424
Performance Tips
433425
----------------

0 commit comments

Comments
 (0)