Implement a simple blinker
Reimplemented the icebreaker top in nmigen_dg
This commit is contained in:
parent
0d4b733da6
commit
3dacf1eb90
28
fpga/icebreaker.dg
Normal file
28
fpga/icebreaker.dg
Normal file
@ -0,0 +1,28 @@
|
||||
import "/nmigen_boards.icebreaker/ICEBreakerPlatform"
|
||||
import "/nmigen_dg/*"
|
||||
import "modules/Blinker"
|
||||
import "modules/Pulser"
|
||||
|
||||
Top = subclass Elaboratable where
|
||||
elaborate = platform ~> m where with m = Module! =>
|
||||
freq = int platform.default_clk_frequency
|
||||
|
||||
@ledr = platform.request "led_r"
|
||||
@ledg = platform.request "led_g"
|
||||
|
||||
blinker1 = Blinker$ int (freq // 3)
|
||||
m.submodules += blinker1
|
||||
|
||||
blinker2 = Pulser$ int (freq // 3)
|
||||
m.submodules += blinker2
|
||||
|
||||
comb$ drive @ledr blinker1.out
|
||||
|
||||
hello = Signal 1 reset: 1
|
||||
when blinker2.out $ ->
|
||||
sync$ drive hello ~hello
|
||||
sync$ drive @ledg hello
|
||||
|
||||
if __name__ == "__main__" =>
|
||||
plat = ICEBreakerPlatform!
|
||||
plat.build Top! do_program: True
|
@ -1,28 +0,0 @@
|
||||
from nmigen import *
|
||||
from nmigen.cli import main
|
||||
from nmigen_boards.icebreaker import ICEBreakerPlatform
|
||||
|
||||
class Blinker(Elaboratable):
|
||||
def __init__(self, maxperiod: int):
|
||||
self.maxperiod = maxperiod
|
||||
|
||||
def elaborate(self, platform):
|
||||
led = platform.request("led_r")
|
||||
|
||||
m = Module()
|
||||
|
||||
counter = Signal(range(self.maxperiod + 1))
|
||||
|
||||
with m.If(counter == 0):
|
||||
m.d.sync += led.eq(~led)
|
||||
m.d.sync += counter.eq(self.maxperiod)
|
||||
with m.Else():
|
||||
m.d.sync += counter.eq(counter - 1)
|
||||
|
||||
return m
|
||||
|
||||
if __name__ == "__main__":
|
||||
plat = ICEBreakerPlatform()
|
||||
#main(plat, ports=[plat.led])
|
||||
plat.build(Blinker(10000000), do_program=True)
|
||||
|
2
fpga/modules/__init__.py
Normal file
2
fpga/modules/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .blinker import Blinker
|
||||
from .blinker import Pulser
|
29
fpga/modules/blinker.dg
Normal file
29
fpga/modules/blinker.dg
Normal file
@ -0,0 +1,29 @@
|
||||
import "/nmigen/cli/main"
|
||||
import "/nmigen_dg/*"
|
||||
|
||||
Blinker = subclass Elaboratable where
|
||||
__init__ = ncycles pulse: False ~> None where
|
||||
@ncycles = ncycles
|
||||
@pulse = pulse
|
||||
@out = Signal!
|
||||
|
||||
elaborate = platform ~> m where with m = Module! =>
|
||||
counter = Signal$ range (@ncycles + 1)
|
||||
|
||||
if @pulse =>
|
||||
sync$ drive @out LOW
|
||||
|
||||
when
|
||||
counter == 0 ,->
|
||||
sync$ drive @out (~ @out)
|
||||
sync$ drive counter @ncycles
|
||||
otherwise ,->
|
||||
sync$ drive counter (counter - 1)
|
||||
|
||||
|
||||
Pulser = bind Blinker pulse: True
|
||||
|
||||
|
||||
if __name__ == "__main__" =>
|
||||
blinker = Blinker ncycles: 10000000
|
||||
main blinker ports: [blinker.out]
|
Loading…
Reference in New Issue
Block a user