nmigen-learning/fpga/icebreaker.dg

83 lines
2.4 KiB
Plaintext
Raw Normal View History

import "/nmigen_boards.icebreaker/ICEBreakerPlatform"
import "/nmigen_dg/*"
import "/sys"
import "modules/blinker/Blinker"
2020-08-09 21:55:44 +02:00
import "modules/pulser/Pulser"
import "modules/segment7/Segment7x2"
2020-08-07 00:22:39 +02:00
import "resources/pmod"
Top = subclass Elaboratable where
elaborate = platform ~> m where with m = Module! =>
freq = int platform.default_clk_frequency
2020-08-13 20:23:04 +02:00
@ledr = platform.request "led_r" 0
@ledg = platform.request "led_g" 0
2020-08-07 00:22:39 +02:00
@seg7 = platform.request "seven_seg"
blinker = (Submodule.blinker$ Blinker$ freq // 3).out
pulser = (Submodule.pulser $ Pulser$ freq // 2).out
seg7 = Submodule.seg7$ Segment7x2 decimal: False
2020-08-17 00:34:12 +02:00
Comb$ @ledr ::= blinker
2020-08-09 21:57:26 +02:00
ff = Signal 1 reset: 1
When pulser $ ->
2020-08-17 00:34:12 +02:00
Sync$ ff ::= ~ff
2020-08-09 21:57:26 +02:00
2020-08-17 00:34:12 +02:00
Comb$ @ledg ::= ff
2020-08-09 21:57:26 +02:00
counter = Signal 4
2020-08-09 21:57:26 +02:00
When pulser $ ->
2020-08-17 00:34:12 +02:00
Sync$ counter ::= counter + 1
# TODO: learn SPI FLASH
mem = Memory
width: 8
depth: 16 # ew
init: ([0xde, 0xad, 0xbe, 0xef]*4)
rdport = Submodule.rdport$ mem.read_port!
2020-08-17 00:34:12 +02:00
Comb$ rdport.addr ::= counter
2020-08-17 00:34:12 +02:00
Comb$ seg7.number ::= rdport.data
#Comb$ seg7.number ::= counter
Comb$ @seg7 ::= Cat seg7.segs seg7.select
@leds = Cat
2020-08-13 20:23:04 +02:00
platform.request "led_g" 4
platform.request "led_g" 2
platform.request "led_g" 3
platform.request "led_g" 1
@ledm = platform.request "led_r" 1
@btn_r = platform.request "button" 1
@btn_m = platform.request "button" 2
@btn_l = platform.request "button" 3
state = Signal 4 reset: 0b1000
2020-08-17 00:34:12 +02:00
Comb$ @leds ::= state
Comb$ @ledm ::= (@btn_r | @btn_l)
2020-08-13 20:23:04 +02:00
When (on_rising_edge @btn_l) $ ->
2020-08-17 00:34:12 +02:00
Sync$ state ::= state.rotate_left 1
2020-08-13 20:23:04 +02:00
When (on_rising_edge @btn_r) $ ->
2020-08-17 00:34:12 +02:00
Sync$ state ::= state.rotate_right 1
2020-08-13 20:23:04 +02:00
When (on_rising_edge @btn_m) $ ->
2020-08-17 00:34:12 +02:00
Sync$ state ::= ~state
2020-08-13 20:23:04 +02:00
on_rising_edge = signal -> result where
prev = Signal (signal.shape!)
2020-08-17 00:34:12 +02:00
Sync$ prev ::= signal
result = ~ signal.implies prev
2020-08-13 20:23:04 +02:00
if __name__ == "__main__" =>
plat = ICEBreakerPlatform!
plat.add_resources$ pmod.seven_seg 0 pmod: 0 # pmod 1a
plat.add_resources$ pmod.dip_switch8 0 pmod: 1 # pmod 1b
plat.add_resources$ plat.break_off_pmod
plat.build Top! do_program: ("--flash" in sys.argv)