def test_nested_ternary(): from kratos import mux mod = Generator("mod") a = mod.var("a", 1) b = mod.var("b", 1) c = a + mux(a, a, b) assert str(c) == "a + (a ? a: b)"
def __init__(self, count_width): super().__init__("ValidIO") # the upper count as an IO from output config self.max_cycle = self.input("max_cycle", count_width) # start signal self.start = self.input("start", 1) # clks and reset # CGRA uses posedge reset self.clk = self.clock("clk") self.rst = self.reset("reset") self.stall = self.input("stall", 1) self.start_state = self.enum("start_state", { "io_valid_idle": 0, "io_valid_count": 1 }) self.has_start = self.var("has_start", self.start_state) self.count = self.var("count", count_width) # valid output self.valid = self.var("valid", 1) # only if the state is in counting and count is larger or equal to max_cycle self.wire(self.valid, (self.has_start == self.start_state.io_valid_count) & (self.count < self.max_cycle)) # normal output self.f2io_1 = self.input("f2io_1", 1) # mode self.mode = self.input("mode", 1) # output self.out = self.output("out", 1) self.wire(self.out, mux(self.mode == 1, self.valid, self.f2io_1)) # counter enable logic self.cen = self.var("cen", 1) self.wire(self.cen, (self.has_start == self.start_state.io_valid_count) & (self.count < self.max_cycle) & (~self.stall)) # add code blocks self.add_always(self.start_signal) self.add_always(self.count_cycle)