amaranth/examples/gpio.py
2018-12-28 13:22:10 +00:00

29 lines
713 B
Python

from types import SimpleNamespace
from nmigen import *
from nmigen.cli import main
class GPIO:
def __init__(self, pins, bus):
self.pins = pins
self.bus = bus
def get_fragment(self, platform):
m = Module()
m.d.comb += self.bus.r_data.eq(self.pins[self.bus.addr])
with m.If(self.bus.we):
m.d.sync += self.pins[self.bus.addr].eq(self.bus.w_data)
return m.lower(platform)
if __name__ == "__main__":
bus = Record([
("addr", 3),
("r_data", 1),
("w_data", 1),
("we", 1),
])
pins = Signal(8)
gpio = GPIO(Array(pins), bus)
main(gpio, ports=[pins, bus.addr, bus.r_data, bus.w_data, bus.we])