Skip to content

Commit d5b5b50

Browse files
committed
Deploy preview for PR 10 🛫
1 parent cd5a698 commit d5b5b50

172 files changed

Lines changed: 51319 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from amaranth import *
2+
from amaranth.lib import wiring
3+
from amaranth.lib.wiring import In, Out
4+
5+
6+
class UpCounter(wiring.Component):
7+
"""
8+
A 16-bit up counter with a fixed limit.
9+
10+
Parameters
11+
----------
12+
limit : int
13+
The value at which the counter overflows.
14+
15+
Attributes
16+
----------
17+
en : Signal, in
18+
The counter is incremented if ``en`` is asserted, and retains
19+
its value otherwise.
20+
ovf : Signal, out
21+
``ovf`` is asserted when the counter reaches its limit.
22+
"""
23+
24+
en: In(1)
25+
ovf: Out(1)
26+
27+
def __init__(self, limit):
28+
self.limit = limit
29+
self.count = Signal(16)
30+
31+
super().__init__()
32+
33+
def elaborate(self, platform):
34+
m = Module()
35+
36+
m.d.comb += self.ovf.eq(self.count == self.limit)
37+
38+
with m.If(self.en):
39+
with m.If(self.ovf):
40+
m.d.sync += self.count.eq(0)
41+
with m.Else():
42+
m.d.sync += self.count.eq(self.count + 1)
43+
44+
return m
45+
# --- TEST ---
46+
from amaranth.sim import Simulator
47+
48+
49+
dut = UpCounter(25)
50+
async def bench(ctx):
51+
# Disabled counter should not overflow.
52+
ctx.set(dut.en, 0)
53+
for _ in range(30):
54+
await ctx.tick()
55+
assert not ctx.get(dut.ovf)
56+
57+
# Once enabled, the counter should overflow in 25 cycles.
58+
ctx.set(dut.en, 1)
59+
for _ in range(24):
60+
await ctx.tick()
61+
assert not ctx.get(dut.ovf)
62+
await ctx.tick()
63+
assert ctx.get(dut.ovf)
64+
65+
# The overflow should clear in one cycle.
66+
await ctx.tick()
67+
assert not ctx.get(dut.ovf)
68+
69+
70+
sim = Simulator(dut)
71+
sim.add_clock(1e-6) # 1 MHz
72+
sim.add_testbench(bench)
73+
with sim.write_vcd("up_counter.vcd"):
74+
sim.run()
75+
# --- CONVERT ---
76+
from amaranth.back import verilog
77+
78+
79+
top = UpCounter(25)
80+
with open("up_counter.v", "w") as f:
81+
f.write(verilog.convert(top))
51.1 KB
Loading
Lines changed: 18 additions & 0 deletions
Loading
43.3 KB
Loading
134 KB
Loading
227 KB
Loading
56.3 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"simulator.rst": "amaranth/simulator.rst"}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CSR
2+
===
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
csr/bus
8+
csr/reg
9+
csr/action
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
CSR fields
2+
----------
3+
4+
.. py:module:: amaranth_soc.csr.action
5+
6+
The :mod:`amaranth_soc.csr.action` module provides built-in :class:`~amaranth_soc.csr.reg.FieldAction` implementations intended for common use cases, which are split in three categories: :ref:`basic fields <csr-action-basic>` for numerical values, :ref:`flag fields <csr-action-flag>` for arrays of bits, and :ref:`reserved fields <csr-action-reserved>` to serve as placeholders for compatibility.
7+
8+
.. _csr-action-basic:
9+
10+
Basic fields
11+
============
12+
13+
Such fields are either exclusively writable by a CSR bus initiator (e.g. :class:`W`, :class:`RW`) or the peripheral itself (e.g. :class:`R`). This effectively removes the possibility of a write conflict between a CSR bus initiator and the peripheral.
14+
15+
.. autoclass:: R()
16+
.. autoclass:: W()
17+
.. autoclass:: RW()
18+
19+
.. _csr-action-flag:
20+
21+
Flag fields
22+
===========
23+
24+
Flag fields may be concurrently written by a CSR bus initiator and the peripheral. Each bit of a flag field may be set or cleared independently of others.
25+
26+
Suggested use cases
27+
+++++++++++++++++++
28+
29+
- :class:`RW1C` flags may be used when a peripheral needs to notify the CPU of a given condition, such as an error or a pending interrupt. To acknowledge the notification, the CPU would then write 1 to the flag bit.
30+
31+
- :class:`RW1S` flags may be used for self-clearing bits, such as the enable bit of a one-shot timer. When the timer reaches its maximum value, it would automatically disable itself by clearing its enable bit.
32+
33+
- A pair of :class:`RW1C` and :class:`RW1S` flags may be used to target the same range of bits (e.g. that drives an array of GPIO pins). This allows a CSR bus initiator to set and clear bits in one write transaction (which is guaranteed to be atomic). If a single :class:`RW` field was used instead, a read-modify-write transaction would be needed, and would require locking to insure its atomicity in a multi-tasked environment.
34+
35+
.. autoclass:: RW1C()
36+
.. autoclass:: RW1S()
37+
38+
.. _csr-action-reserved:
39+
40+
Reserved fields
41+
===============
42+
43+
Reserved fields may be defined to provide placeholders for past, future or undocumented functions of a peripheral.
44+
45+
Suggested use cases
46+
+++++++++++++++++++
47+
48+
Reserved for future use (as value)
49+
..................................
50+
51+
A :class:`ResRAWL` field can be used as a placeholder to ensure forward compatibility of software binaries with future SoC revisions, where it may be replaced with a :ref:`basic field <csr-action-basic>`.
52+
53+
The value returned by reads (and written back) must have defined semantics (e.g. a no-op) that can be relied upon in future SoC revisions. When writing to this field, software drivers targeting the current SoC revision must set up an atomic read-modify-write transaction.
54+
55+
Reserved for future use (as flag)
56+
.................................
57+
58+
If a field is expected to be implemented as a :ref:`flag <csr-action-flag>` in a future SoC revision, it can be defined as a :class:`ResRAW0` field in the current revision to ensure forward compatibility of software binaries.
59+
60+
Software drivers targeting the current SoC revision should ignore the value returned by reads. Writing a value of 0 must be a no-op if the field is implemented in a future SoC revision.
61+
62+
Defined but deprecated
63+
......................
64+
65+
If a field was deprecated in a previous SoC revision, it can be replaced with a :class:`ResR0WA` field to ensure backward compatibility of software binaries.
66+
67+
The value of 0 returned by reads (and written back) must retain the semantics defined in the SoC revision where this field was deprecated.
68+
69+
Defined but unimplemented
70+
.........................
71+
72+
If a field is only implemented in some variants of a peripheral, it can be replaced by a :class:`ResR0W0` field in the others.
73+
74+
.. autoclass:: ResRAW0()
75+
.. autoclass:: ResRAWL()
76+
.. autoclass:: ResR0WA()
77+
.. autoclass:: ResR0W0()

0 commit comments

Comments
 (0)