Skip to content

Commit 672201b

Browse files
committed
Deploying to main from @ amaranth-lang/amaranth@3857822 🚀
1 parent d0a47a9 commit 672201b

File tree

160 files changed

+35097
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+35097
-0
lines changed

docs/amaranth/v0.5.3/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_build/
2+
_linkcheck/
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from amaranth import *
2+
3+
4+
class LEDBlinker(Elaboratable):
5+
def elaborate(self, platform):
6+
m = Module()
7+
8+
led = platform.request("led")
9+
10+
half_freq = int(platform.default_clk_frequency // 2)
11+
timer = Signal(range(half_freq + 1))
12+
13+
with m.If(timer == half_freq):
14+
m.d.sync += led.o.eq(~led.o)
15+
m.d.sync += timer.eq(0)
16+
with m.Else():
17+
m.d.sync += timer.eq(timer + 1)
18+
19+
return m
20+
# --- BUILD ---
21+
from amaranth_boards.icestick import ICEStickPlatform
22+
23+
24+
ICEStickPlatform().build(LEDBlinker(), do_program=True)
+81
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))
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(* generator = "Amaranth" *)
2+
module top(ovf, clk, rst, en);
3+
reg \$auto$verilog_backend.cc:2255:dump_module$1 = 0;
4+
(* src = "up_counter.py:36" *)
5+
wire \$1 ;
6+
(* src = "up_counter.py:42" *)
7+
wire [16:0] \$3 ;
8+
(* src = "up_counter.py:42" *)
9+
wire [16:0] \$4 ;
10+
(* src = "<site-packages>/amaranth/hdl/ir.py:509" *)
11+
input clk;
12+
wire clk;
13+
(* src = "up_counter.py:29" *)
14+
reg [15:0] count = 16'h0000;
15+
(* src = "up_counter.py:29" *)
16+
reg [15:0] \count$next ;
17+
(* src = "<site-packages>/amaranth/lib/wiring.py:1647" *)
18+
input en;
19+
wire en;
20+
(* src = "<site-packages>/amaranth/lib/wiring.py:1647" *)
21+
output ovf;
22+
wire ovf;
23+
(* src = "<site-packages>/amaranth/hdl/ir.py:509" *)
24+
input rst;
25+
wire rst;
26+
assign \$1 = count == (* src = "up_counter.py:36" *) 5'h19;
27+
assign \$4 = count + (* src = "up_counter.py:42" *) 1'h1;
28+
always @(posedge clk)
29+
count <= \count$next ;
30+
always @* begin
31+
if (\$auto$verilog_backend.cc:2255:dump_module$1 ) begin end
32+
\count$next = count;
33+
(* src = "up_counter.py:38" *)
34+
if (en) begin
35+
(* full_case = 32'd1 *)
36+
(* src = "up_counter.py:39" *)
37+
if (ovf) begin
38+
\count$next = 16'h0000;
39+
end else begin
40+
\count$next = \$4 [15:0];
41+
end
42+
end
43+
(* src = "<site-packages>/amaranth/hdl/xfrm.py:534" *)
44+
if (rst) begin
45+
\count$next = 16'h0000;
46+
end
47+
end
48+
assign \$3 = \$4 ;
49+
assign ovf = \$1 ;
50+
endmodule
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))

docs/amaranth/v0.5.3/_images/simulator/example1.svg

+1
Loading

docs/amaranth/v0.5.3/_images/simulator/example2.svg

+1
Loading

docs/amaranth/v0.5.3/_images/simulator/example3.svg

+1
Loading

docs/amaranth/v0.5.3/_images/start/up_counter.svg

+1
Loading
56.3 KB
Loading

0 commit comments

Comments
 (0)