def __init__(self, inwidth): wlog = int(log(inwidth, 2)) self.inputs = Array() for i in range(inwidth): self.inputs.append(Signal(1, name_override="input_{}".format(i))) self.output = Signal(name_override="output") self.selector = Signal(max=inwidth + 1) self.io = set(self.inputs) | set([self.output, self.selector]) sel_r = Signal(max=inwidth + 1) sel25 = Signal(max=1 << inwidth) zero = Constant(0) muxes = [] for i in range(len(self.inputs)): x = Constant(1 << i, inwidth) choose = Signal() choose.eq(self.selector & x) muxes.append(Mux(self.selector & x, self.inputs[i], zero)) mux = self.output.eq(reduce(orop, muxes)) self.comb += mux self.sync += sel_r.eq(self.selector) d = {} x = 1 for i in range(inwidth): d[i] = (sel25.eq(x << i), ) self.sync += If( self.selector != sel_r, sel25.eq(0), ).Else(Case(sel_r, d))
class Blinker(Module): def __init__(self, led, maxperiod1, maxperiod2, select): self.counter = Signal(max=maxperiod1 + 1) self.period1 = Signal(max=maxperiod1 + 1) self.period2 = Signal(max=maxperiod2 + 1) self.selector = Signal(max=select + 1) self.period = Signal(max=maxperiod1 + 1) self.comb += self.period.eq( Mux(self.selector, self.period1, self.period2)) self.comb += self.period1.eq(maxperiod1) self.comb += self.period2.eq(maxperiod2) self.sync += If(self.counter == 0, led.eq(~led), self.counter.eq(self.period)).Else( self.counter.eq(self.counter - 1)) self.led = led
class LoopbackTop(Module): def __init__(self, clk_freq, baud_rate): self.submodules.uart = uart.Core(clk_freq, baud_rate) self.rx_led = Signal() self.tx_led = Signal() self.load_led = Signal() self.take_led = Signal() self.empty_led = Signal() self.comb += [self.tx_led.eq(~self.uart.tx), self.rx_led.eq(~self.uart.rx), self.load_led.eq(self.uart.sout.load), self.take_led.eq(self.uart.sin.take), self.empty_led.eq(self.uart.sin.empty)] self.comb += [self.uart.out_data.eq(self.uart.in_data)] self.sync += [ self.uart.wr.eq(0), self.uart.rd.eq(0), If(~self.uart.sin.empty, self.uart.wr.eq(1), self.uart.rd.eq(1) ) ]