2013-02-22 08:28:05 -05:00
|
|
|
from migen.fhdl.structure import *
|
2013-02-28 19:09:00 -05:00
|
|
|
from migen.fhdl.specials import Memory
|
2013-02-22 08:28:05 -05:00
|
|
|
from migen.bus import csr
|
|
|
|
from migen.bank import description, csrgen
|
|
|
|
from migen.bank.description import *
|
2013-02-28 19:09:00 -05:00
|
|
|
from migen.genlib.misc import optree
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
class RegParams:
|
|
|
|
def __init__(self, name, base, width, nb):
|
|
|
|
self.name = name
|
|
|
|
self.base = base
|
|
|
|
self.width = width
|
|
|
|
self.nb = nb
|
|
|
|
|
|
|
|
self.size = nb*width
|
|
|
|
self.words = int(2**bits_for(self.width-1)/8)
|
|
|
|
|
|
|
|
def list_regs(objects):
|
|
|
|
r = []
|
|
|
|
for object in objects:
|
|
|
|
if "_reg" in object:
|
|
|
|
r.append(objects[object])
|
|
|
|
return r
|
|
|
|
|
|
|
|
class Term:
|
|
|
|
#
|
|
|
|
# Definition
|
|
|
|
#
|
|
|
|
def __init__(self, width):
|
|
|
|
self.width = width
|
|
|
|
self.interface = None
|
|
|
|
|
|
|
|
self.i = Signal(width)
|
|
|
|
self.t = Signal(width)
|
|
|
|
self.m = Signal(width)
|
|
|
|
self.o = Signal()
|
|
|
|
|
|
|
|
self.reg_p = RegParams("term_reg", 0, width, 2)
|
2013-02-27 18:32:42 -05:00
|
|
|
self.reg = None
|
2013-02-22 08:28:05 -05:00
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def get_registers_comb(self):
|
2013-02-27 18:32:42 -05:00
|
|
|
comb = [self.t.eq(self.reg.field.r[0*self.width:1*self.width])]
|
|
|
|
comb += [self.m.eq(self.reg.field.r[1*self.width:2*self.width])]
|
2013-02-22 08:28:05 -05:00
|
|
|
return comb
|
|
|
|
|
2013-02-27 18:32:42 -05:00
|
|
|
def get_fragment(self):
|
2013-02-22 08:28:05 -05:00
|
|
|
comb = [self.o.eq((self.m & self.i) == self.t)]
|
2013-03-22 07:35:12 -04:00
|
|
|
comb += self.get_registers_comb()
|
2013-02-22 08:28:05 -05:00
|
|
|
return Fragment(comb)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Driver
|
|
|
|
#
|
2013-03-22 07:35:12 -04:00
|
|
|
def set(self, dat, mask=None):
|
2013-02-22 08:28:05 -05:00
|
|
|
if mask is None:
|
|
|
|
mask = (2**self.width)-1
|
2013-03-21 07:23:44 -04:00
|
|
|
self.interface.write_n(self.reg_p.base, mask, self.width)
|
2013-02-22 08:28:05 -05:00
|
|
|
self.interface.write_n(self.reg_p.base + self.reg_p.words, dat, self.width)
|
|
|
|
|
|
|
|
class RangeDetector:
|
|
|
|
#
|
|
|
|
# Definition
|
|
|
|
#
|
|
|
|
def __init__(self, width):
|
|
|
|
self.width = width
|
|
|
|
self.pipe = pipe
|
|
|
|
self.interface = None
|
|
|
|
|
|
|
|
self.reg_p = RegParams("range_reg", 0, width, 2)
|
2013-02-27 18:32:42 -05:00
|
|
|
self.reg = None
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
self.i = Signal(width)
|
|
|
|
self.low = Signal(width)
|
|
|
|
self.high = Signal(width)
|
|
|
|
self.o = Signal()
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def get_registers_comb(self):
|
2013-02-27 18:32:42 -05:00
|
|
|
comb = [self.low.eq(self.reg.field.r[0*self.width:1*self.width])]
|
|
|
|
comb += [self.low.eq(self.reg.field.r[1*self.width:2*self.width])]
|
2013-02-22 08:28:05 -05:00
|
|
|
return comb
|
|
|
|
|
2013-02-27 18:32:42 -05:00
|
|
|
def get_fragment(self):
|
2013-02-22 08:28:05 -05:00
|
|
|
comb = [self.o.eq((self.i >= self.low) & (self.i <= self.high))]
|
2013-03-22 07:35:12 -04:00
|
|
|
comb += self.get_registers_comb()
|
2013-02-22 08:28:05 -05:00
|
|
|
return Fragment(comb)
|
|
|
|
#
|
|
|
|
# Driver
|
|
|
|
#
|
2013-03-22 07:35:12 -04:00
|
|
|
def set_low(self, dat):
|
2013-02-22 08:28:05 -05:00
|
|
|
self.interface.write_n(self.reg_p.base, dat ,self.width)
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def set_high(self, dat):
|
2013-02-22 08:28:05 -05:00
|
|
|
self.interface.write_n(self.reg_p.base + self.reg_p.words, dat ,self.width)
|
|
|
|
|
|
|
|
class EdgeDetector:
|
|
|
|
#
|
|
|
|
# Definition
|
|
|
|
#
|
2013-03-23 07:26:22 -04:00
|
|
|
def __init__(self, width, mode="RFB"):
|
2013-02-22 08:28:05 -05:00
|
|
|
self.width = width
|
|
|
|
self.mode = mode
|
|
|
|
self.interface = None
|
|
|
|
|
2013-02-27 18:32:42 -05:00
|
|
|
self.reg_p = RegParams("edge_reg", 0, width, len(self.mode))
|
|
|
|
self.reg = None
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
self.i = Signal(self.width)
|
|
|
|
self.i_d = Signal(self.width)
|
|
|
|
if "R" in self.mode:
|
|
|
|
self.r_mask = Signal(self.width)
|
|
|
|
self.ro = Signal()
|
|
|
|
if "F" in self.mode:
|
|
|
|
self.f_mask = Signal(self.width)
|
|
|
|
self.fo = Signal()
|
|
|
|
if "B" in self.mode:
|
|
|
|
self.b_mask = Signal(self.width)
|
|
|
|
self.bo = Signal()
|
|
|
|
self.o = Signal()
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def get_registers_comb(self):
|
2013-02-22 08:28:05 -05:00
|
|
|
comb = []
|
|
|
|
i = 0
|
|
|
|
if "R" in self.mode:
|
2013-02-27 18:32:42 -05:00
|
|
|
comb += [self.r_mask.eq(self.reg.field.r[i*self.width:(i+1)*self.width])]
|
2013-02-22 08:28:05 -05:00
|
|
|
i += 1
|
|
|
|
if "F" in self.mode:
|
2013-02-27 18:32:42 -05:00
|
|
|
comb += [self.f_mask.eq(self.reg.field.r[i*self.width:(i+1)*self.width])]
|
2013-02-22 08:28:05 -05:00
|
|
|
i += 1
|
|
|
|
if "B" in self.mode:
|
2013-02-27 18:32:42 -05:00
|
|
|
comb += [self.b_mask.eq(self.reg.field.r[i*self.width:(i+1)*self.width])]
|
2013-02-22 08:28:05 -05:00
|
|
|
i += 1
|
|
|
|
return comb
|
|
|
|
|
2013-02-27 18:32:42 -05:00
|
|
|
def get_fragment(self):
|
2013-02-22 08:28:05 -05:00
|
|
|
comb = []
|
|
|
|
sync = [self.i_d.eq(self.i)]
|
|
|
|
|
|
|
|
# Rising Edge
|
|
|
|
if "R" in self.mode:
|
|
|
|
comb += [self.ro.eq(self.r_mask & self.i & (~self.i_d))]
|
|
|
|
else:
|
|
|
|
comb += [self.ro.eq(0)]
|
|
|
|
|
|
|
|
# Falling Edge
|
|
|
|
if "F" in self.mode:
|
2013-03-21 07:23:44 -04:00
|
|
|
comb += [self.fo.eq(self.f_mask & (~self.i) & self.i_d)]
|
2013-02-22 08:28:05 -05:00
|
|
|
else:
|
|
|
|
comb += [self.fo.eq(0)]
|
|
|
|
|
|
|
|
# Both
|
|
|
|
if "B" in self.mode:
|
2013-03-21 07:23:44 -04:00
|
|
|
comb += [self.bo.eq((self.b_mask & self.i) != self.i_d)]
|
2013-02-22 08:28:05 -05:00
|
|
|
else:
|
|
|
|
comb += [self.bo.eq(0)]
|
|
|
|
|
|
|
|
# Output
|
|
|
|
comb += [self.o.eq(self.ro | self.fo | self.bo)]
|
|
|
|
|
|
|
|
# Registers
|
2013-03-22 07:35:12 -04:00
|
|
|
comb += self.get_registers_comb()
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
return Fragment(comb, sync)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Driver
|
|
|
|
#
|
|
|
|
def get_offset(self, type):
|
|
|
|
if type == "R":
|
|
|
|
r = 0
|
|
|
|
r = r + self.words if "F" in self.mode else r
|
|
|
|
r = r + self.words if "B" in self.mode else r
|
|
|
|
return r
|
|
|
|
elif type == "F":
|
|
|
|
r = 0
|
|
|
|
r = r + self.words if "B" in self.mode else r
|
|
|
|
return r
|
|
|
|
elif type == "B":
|
|
|
|
r = 0
|
|
|
|
return r
|
|
|
|
return 0
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def set_r(self, dat):
|
2013-02-22 08:28:05 -05:00
|
|
|
self.interface.write_n(self.reg_p.base + self.get_offset("R"), dat ,self.width)
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def set_f(self, dat):
|
2013-02-22 08:28:05 -05:00
|
|
|
self.interface.write_n(self.reg_p.base + self.get_offset("F"), dat ,self.width)
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def set_b(self, dat):
|
2013-02-22 08:28:05 -05:00
|
|
|
self.interface.write_n(self.reg_p.base + self.get_offset("B"), dat ,self.width)
|
|
|
|
|
|
|
|
class Sum:
|
|
|
|
#
|
|
|
|
# Definition
|
|
|
|
#
|
|
|
|
def __init__(self, width=4):
|
|
|
|
self.width = width
|
|
|
|
self.interface = None
|
|
|
|
|
|
|
|
self.i = Signal(self.width)
|
|
|
|
self.o = Signal()
|
|
|
|
|
|
|
|
self.reg_p = RegParams("sum_reg", 0, 8, 4)
|
2013-02-27 18:32:42 -05:00
|
|
|
self.reg = None
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
self.prog_stb = Signal()
|
|
|
|
self.prog_adr = Signal(width)
|
|
|
|
self.prog_dat = Signal()
|
|
|
|
|
|
|
|
self._mem = Memory(1, 2**self.width)
|
|
|
|
self._lut_port = self._mem.get_port()
|
|
|
|
self._prog_port = self._mem.get_port(write_capable=True)
|
|
|
|
|
2013-03-22 07:35:12 -04:00
|
|
|
def get_registers_comb(self):
|
2013-02-22 08:28:05 -05:00
|
|
|
comb = [
|
2013-02-27 18:32:42 -05:00
|
|
|
self.prog_adr.eq(self.reg.field.r[0:16]),
|
|
|
|
self.prog_dat.eq(self.reg.field.r[16]),
|
|
|
|
self.prog_stb.eq(self.reg.field.r[17])
|
2013-02-22 08:28:05 -05:00
|
|
|
]
|
|
|
|
return comb
|
|
|
|
|
2013-02-27 18:32:42 -05:00
|
|
|
def get_fragment(self):
|
2013-02-22 08:28:05 -05:00
|
|
|
comb = [
|
|
|
|
self._lut_port.adr.eq(self.i),
|
|
|
|
|
|
|
|
self._prog_port.adr.eq(self.prog_adr),
|
|
|
|
self._prog_port.we.eq(self.prog_stb),
|
2013-02-27 18:32:42 -05:00
|
|
|
self._prog_port.dat_w.eq(self.prog_dat),
|
2013-03-23 07:26:22 -04:00
|
|
|
|
2013-04-02 15:13:21 -04:00
|
|
|
self.o.eq(self._lut_port.dat_r),
|
2013-02-22 08:28:05 -05:00
|
|
|
]
|
2013-03-22 07:35:12 -04:00
|
|
|
comb += self.get_registers_comb()
|
2013-02-28 19:09:00 -05:00
|
|
|
return Fragment(comb, specials={self._mem})
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
#
|
2013-03-22 07:35:12 -04:00
|
|
|
# Driver
|
2013-02-22 08:28:05 -05:00
|
|
|
#
|
2013-03-22 07:35:12 -04:00
|
|
|
def set(self, truth_table):
|
2013-02-22 08:28:05 -05:00
|
|
|
for i in range(len(truth_table)):
|
|
|
|
val = truth_table[i]
|
|
|
|
we = 1<<17
|
|
|
|
dat = val<<16
|
|
|
|
addr = i
|
2013-03-21 07:23:44 -04:00
|
|
|
self.interface.write_n(self.reg_p.base, we + dat + addr, self.reg_p.size)
|
|
|
|
self.interface.write_n(self.reg_p.base, dat + addr, self.reg_p.size)
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
class Trigger:
|
|
|
|
#
|
|
|
|
# Definition
|
|
|
|
#
|
2013-04-02 15:13:21 -04:00
|
|
|
def __init__(self, width, ports, address=0x0000, interface=None):
|
|
|
|
self.width = width
|
2013-02-22 08:28:05 -05:00
|
|
|
self.ports = ports
|
|
|
|
|
|
|
|
self.sum = Sum(len(ports))
|
2013-04-02 15:13:21 -04:00
|
|
|
self.trig = Signal(self.width)
|
2013-02-22 08:28:05 -05:00
|
|
|
self.hit = Signal()
|
|
|
|
|
|
|
|
# insert port number in port reg name
|
|
|
|
for i in range(len(self.ports)):
|
|
|
|
self.ports[i].reg_p.name += "_%d"%i
|
|
|
|
|
|
|
|
# generate ports csr registers fields
|
|
|
|
for port in self.ports:
|
2013-03-26 17:14:25 -04:00
|
|
|
rf = RegisterField(port.reg_p.size, reset=0, access_bus=WRITE_ONLY,
|
|
|
|
access_dev=READ_ONLY, name=port.reg_p.name)
|
2013-02-27 18:32:42 -05:00
|
|
|
setattr(self, port.reg_p.name, rf)
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
# generate sum csr registers fields
|
2013-03-26 17:14:25 -04:00
|
|
|
self.sum_reg = RegisterField(self.sum.reg_p.size, reset=0, access_bus=WRITE_ONLY,
|
|
|
|
access_dev=READ_ONLY, name=self.sum.reg_p.name)
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
# generate registers
|
|
|
|
self.regs = list_regs(self.__dict__)
|
|
|
|
self.bank = csrgen.Bank(self.regs, address=address)
|
|
|
|
|
|
|
|
# update base addr & interface
|
2013-02-27 18:32:42 -05:00
|
|
|
self.set_address(address)
|
|
|
|
self.set_interface(interface)
|
|
|
|
self.set_registers()
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
def set_address(self, address):
|
|
|
|
self.address = address
|
2013-03-21 07:23:44 -04:00
|
|
|
self.bank = csrgen.Bank(self.regs, address=self.address)
|
2013-02-22 08:28:05 -05:00
|
|
|
for port in self.ports:
|
|
|
|
port.reg_p.base = self.bank.get_base(port.reg_p.name)
|
|
|
|
self.sum.reg_p.base = self.bank.get_base(self.sum.reg_p.name)
|
2013-03-21 07:23:44 -04:00
|
|
|
|
2013-02-22 08:28:05 -05:00
|
|
|
def set_interface(self, interface):
|
|
|
|
self.interface = interface
|
|
|
|
for port in self.ports:
|
|
|
|
port.interface = self.interface
|
|
|
|
self.sum.interface = self.interface
|
2013-02-27 18:32:42 -05:00
|
|
|
|
|
|
|
def set_registers(self):
|
|
|
|
self.sum.reg=self.sum_reg
|
|
|
|
for port in self.ports:
|
|
|
|
port.reg=getattr(self, port.reg_p.name)
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
def get_fragment(self):
|
|
|
|
# connect trig to input of each trig element
|
2013-02-27 18:32:42 -05:00
|
|
|
comb = [port.i.eq(self.trig) for port in self.ports]
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
# connect output of trig elements to sum
|
|
|
|
comb += [self.sum.i[j].eq(self.ports[j].o) for j in range(len(self.ports))]
|
|
|
|
|
|
|
|
# connect sum ouput to hit
|
|
|
|
comb += [self.hit.eq(self.sum.o)]
|
|
|
|
|
|
|
|
# add ports & sum to frag
|
2013-02-27 18:32:42 -05:00
|
|
|
frag = self.bank.get_fragment()
|
|
|
|
frag += self.sum.get_fragment()
|
2013-02-22 08:28:05 -05:00
|
|
|
for port in self.ports:
|
2013-02-27 18:32:42 -05:00
|
|
|
frag += port.get_fragment()
|
2013-02-22 08:28:05 -05:00
|
|
|
|
|
|
|
return frag + Fragment(comb)
|