litex/migScope/migIo.py

38 lines
1.2 KiB
Python
Raw Normal View History

2012-09-12 16:19:42 -04:00
from migen.fhdl.structure import *
from migen.bus import csr
from migen.bank import description, csrgen
from migen.bank.description import *
class MigIo:
2012-09-14 06:57:09 -04:00
def __init__(self,address, width, mode = "IO", interface=None):
2012-09-13 07:14:27 -04:00
self.address = address
2012-09-12 16:19:42 -04:00
self.width = width
self.mode = mode
2012-09-14 06:57:09 -04:00
self.interface = interface
self.words = int(2**bits_for(width-1)/8)
2012-09-12 16:19:42 -04:00
if "I" in self.mode:
self.i = Signal(BV(self.width))
self.ireg = description.RegisterField("i", self.width, READ_ONLY, WRITE_ONLY)
self.ireg.field.w.name_override = "inputs"
if "O" in self.mode:
self.o = Signal(BV(self.width))
self.oreg = description.RegisterField("o", self.width)
self.oreg.field.r.name_override = "ouptuts"
2012-09-13 07:14:27 -04:00
self.bank = csrgen.Bank([self.oreg, self.ireg], address=self.address)
2012-09-14 06:57:09 -04:00
def write(self, data):
self.interface.write_n(self.address, data, self.width)
def read(self):
2012-09-14 08:08:20 -04:00
r = self.interface.read_n(self.address + self.words, self.width)
2012-09-14 06:57:09 -04:00
return r
2012-09-12 16:19:42 -04:00
def get_fragment(self):
comb = []
2012-09-13 07:14:27 -04:00
if "I" in self.mode:
comb += [self.ireg.field.w.eq(self.i)]
if "O" in self.mode:
comb += [self.o.eq(self.oreg.field.r)]
2012-09-12 16:19:42 -04:00
return Fragment(comb=comb) + self.bank.get_fragment()