def test_2(self): @vsc.randobj class my_sub_s(object): def __init__(self): self.has_rs1 = vsc.uint8_t(1) self.has_rs2 = vsc.uint8_t(1) self.has_rd = vsc.uint8_t(1) self.avail_regs = vsc.rand_list_t(vsc.uint8_t(0), 10) self.reserved_rd = vsc.rand_list_t(vsc.uint8_t(0), 10) self.reserved_regs = vsc.rand_list_t(vsc.uint8_t(0), 10) self.rd = vsc.rand_uint8_t(0) self.rs1 = vsc.rand_uint8_t(0) self.rs2 = vsc.rand_uint8_t(0) self.format = vsc.uint8_t(2) obj = my_sub_s() with obj.randomize_with() as it: with vsc.if_then(obj.avail_regs.size > 0): with vsc.if_then(obj.has_rs1): obj.rs1.inside(vsc.rangelist(obj.avail_regs)) with vsc.if_then(obj.has_rs2): obj.rs2.inside(vsc.rangelist(obj.avail_regs)) with vsc.if_then(obj.has_rd): obj.rd.inside(vsc.rangelist(obj.avail_regs)) with vsc.foreach(obj.reserved_rd, idx=True) as i: with vsc.if_then(obj.has_rd): obj.rd != obj.reserved_rd[i] with vsc.if_then(obj.format == 2): obj.rs1 != obj.reserved_rd[i] with vsc.foreach(obj.reserved_regs, idx=True) as i: with vsc.if_then(obj.has_rd): obj.rd != obj.reserved_regs[i] with vsc.if_then(obj.format == 2): obj.rs1 != obj.reserved_regs[i]
def unique_c(self): # with vsc.foreach(self.arr, idx=True) as i: # self.arr[i] == i with vsc.foreach(self.arr, idx=True) as i: with vsc.foreach(self.arr, idx=True) as j: with vsc.implies(i != j): self.arr[i] != self.arr[j]
def ab_c(self): with vsc.foreach(self.a_list, idx=True) as i: if self.c: self.a_list[i] in vsc.rangelist(5,6,7,8) else: self.a_list[i] in vsc.rangelist(10,11,12,13) with vsc.foreach(self.temp_list, idx=True) as i: with vsc.if_then(self.a_list[i].inside(vsc.rangelist(6,7))): self.temp_list[i] == 0 with vsc.else_then: self.temp_list[i] == 1
def ab_c(self): with vsc.foreach(self.a_list, idx=True) as i: if self.c: self.a_list[i] in vsc.rangelist(5,6,7,8) else: self.a_list[i] in vsc.rangelist(10,11,12,13) with vsc.foreach(self.temp_list, idx=True) as i: if self.a_list[i] in [6,7]: self.temp_list[i] == 0 else: self.temp_list[i] == 1
def enum_inter_class(self): with vsc.foreach(self.c1, idx=True) as i: with vsc.foreach(self.c1[i].a, idx=True) as j: with vsc.if_then( self.c1[i].a[0].enum_test == level_e.level_0): self.c2[i].x[0].value == 0 with vsc.else_if( self.c1[i].a[0].enum_test == level_e.level_1): self.c2[i].x[0].value == 1 with vsc.else_then: self.c2[i].x[0].value == 2
def ab_c(self): with vsc.if_then(self.a == 1): with vsc.if_then(self.b == 2): self.c == 2 with vsc.foreach(self.arr, idx=True) as i: with vsc.if_then(self.a == 1): self.arr[i] == 1
def test_array_fixedsz_sum(self): @vsc.randobj class item_c(object): def __init__(self): self.arr = vsc.rand_list_t(vsc.bit_t(8), sz=16) @vsc.constraint def elem_ne_c(self): with vsc.foreach(self.arr) as v: v != 0 it = item_c() for i in range(1): with it.randomize_with(): it.arr.sum > 0 it.arr.sum < 40 with vsc.foreach(it.arr) as v: v != 0 for j in range(len(it.arr)): print("arr[" + str(j) + "] " + str(it.arr[j])) print("sum: " + str(it.arr.sum)) self.assertGreater(it.arr.sum, 0) self.assertLess(it.arr.sum, 40)
def avail_regs_c(self): # TODO self.avail_regs.size == 10 # self.num_of_avail_regs vsc.unique(self.avail_regs) with vsc.foreach(self.avail_regs, idx=True) as i: self.avail_regs[i].not_inside(cfg.reserved_regs) self.avail_regs[i] != riscv_reg_t.ZERO
def gpr_c(self): with vsc.foreach(self.gpr, idx=True) as i: self.gpr[i].not_inside( vsc.rangelist(self.sp, self.tp, self.scratch_reg, self.pmp_reg, riscv_reg_t.ZERO, riscv_reg_t.RA, riscv_reg_t.GP)) vsc.unique(self.gpr)
def aligned_amo_c(self): with vsc.foreach(self.offset, idx = True) as i: with vsc.if_then(self.XLEN == 32): self.offset[i] % 4 == 0 with vsc.else_then(): self.offset[i] % 8 == 0
def dist_a(self): with vsc.foreach(self.a, idx=True) as i: vsc.dist(self.a[i], [ vsc.weight(1, 10), vsc.weight(2, 20), vsc.weight(4, 40), vsc.weight(8, 80)])
def addr_c(self): vsc.solve_order(self.data_page_id, self.max_load_store_offset) vsc.solve_order(self.max_load_store_offset, self.base) self.data_page_id < self.max_data_page_id with vsc.foreach(self.data_page, idx = True) as i: with vsc.if_then(i == self.data_page_id): self.max_load_store_offset == self.data_page[i].size_in_bytes self.base in vsc.rangelist(vsc.rng(0, self.max_load_store_offset - 1))
def default_c(self): self.sub_program_instr_cnt.size == self.num_of_sub_program self.debug_sub_program_instr_cnt.size == self.num_debug_sub_program self.main_program_instr_cnt in vsc.rangelist( vsc.rng(10, self.instr_cnt)) with vsc.foreach(self.sub_program_instr_cnt, idx=True) as i: self.sub_program_instr_cnt[i].inside( vsc.rangelist(vsc.rng(10, self.instr_cnt)))
def legal_loop_regs_c(self): self.num_of_nested_loop.inside(vsc.rangelist(1, 2)) self.loop_limit_reg.size.inside(vsc.rangelist((1, 32))) self.loop_cnt_reg.size.inside(vsc.rangelist((1, 8))) vsc.solve_order(self.num_of_nested_loop, self.loop_cnt_reg) vsc.solve_order(self.num_of_nested_loop, self.loop_limit_reg) with vsc.foreach(self.loop_cnt_reg, idx=True) as i: self.loop_cnt_reg[i] != riscv_reg_t.ZERO with vsc.foreach(cfg.reserved_regs, idx=True) as j: self.loop_cnt_reg[i] != cfg.reserved_regs[j] with vsc.foreach(self.loop_limit_reg, idx=True) as i: with vsc.foreach(cfg.reserved_regs, idx=True) as j: self.loop_limit_reg[i] != cfg.reserved_regs[j] vsc.unique(self.loop_cnt_reg) vsc.unique(self.loop_limit_reg) self.loop_cnt_reg.size == self.num_of_nested_loop self.loop_limit_reg.size == self.num_of_nested_loop
def rs1_c(self): # TODO constraint size with num_of_rs1_reg vsc.solve_order(self.num_of_rs1_reg, self.rs1_reg) self.rs1_reg.size == 1 # self.num_of_rs1_reg self.offset.size == 1 # self.num_of_rs1_reg with vsc.foreach(self.rs1_reg, idx = True) as i: self.rs1_reg[i].not_inside(vsc.rangelist(cfg.reserved_regs, self.reserved_rd, riscv_reg_t.ZERO)) vsc.unique(self.rs1_reg)
def reg_c(self): vsc.solve_order(self.num_of_regs, self.reg) self.reg.size == self.num_of_regs self.offset.size == self.num_of_regs with vsc.foreach(self.reg, idx=True) as i: self.reg[i].not_inside( vsc.rangelist(self.reserved_regs, reg_t.A)) vsc.unique(self.reg)
def program_stack_level_c(self): # The stack level is assigned in ascending order to avoid call loop self.stack_level.size == self.program_cnt self.stack_level[0] == 0 with vsc.foreach(self.stack_level, idx=True) as i: with vsc.if_then(i > 0): self.stack_level[i] in vsc.rangelist( vsc.rng(1, self.program_cnt - 1)) self.stack_level[i] >= self.stack_level[i - 1] self.stack_level[i] <= self.stack_level[i - 1] + 1 self.stack_level[i] <= self.max_stack_level
def randomize_gpr(self, instr): with instr.randomize_with() as it: if self.avail_regs.size > 0: if instr.has_rs1: instr.rs1.inside(vsc.rangelist(self.avail_regs)) if instr.has_rs2: instr.rs2.inside(vsc.rangelist(self.avail_regs)) if instr.has_rd: instr.rd.inside(vsc.rangelist(self.avail_regs)) with vsc.foreach(self.reserved_rd, idx = True) as i: if instr.has_rd == 1: instr.rd != self.reserved_rd[i] if instr.format == riscv_instr_format_t.CB_FORMAT: instr.rs1 != self.reserved_rd[i] with vsc.foreach(cfg.reserved_regs, idx = True) as i: with vsc.if_then(instr.has_rd == 1): instr.rd != cfg.reserved_regs[i] with vsc.if_then(instr.format == riscv_instr_format_t.CB_FORMAT): instr.rs1 != cfg.reserved_regs[i] # TODO: Add constraint for CSR, floating point register return instr
def randomize_avail_regs(self): if self.avail_regs.size > 0: try: with vsc.randomize_with(self.avail_regs): vsc.unique(self.avail_regs) self.avail_regs[0].inside( vsc.rangelist(vsc.rng(riscv_reg_t.S0, riscv_reg_t.A5))) with vsc.foreach(self.avail_regs, idx=True) as i: self.avail_regs[i].not_inside( vsc.rangelist(cfg.reserved_regs, self.reserved_rd)) except Exception: logging.critical("Cannot randomize avail_regs") sys.exit(1)
async def run_xfer(self): channel = vsc.rand_uint32_t() xfer_spec = XferSpec() # Randomly select a channel not currently in use with vsc.randomize_with(channel): channel < self.n_channels with vsc.foreach(self.active_channels, idx=True) as i: with vsc.implies(i == channel): self.active_channels[i] == False print("channel: " + str(channel)) self.active_xfers += 1 self.active_channels[channel] = True xfer_spec.randomize() # TODO: program channel ch = self.dma_regs.channel(int(channel)) sz = await ch.read_sz() sz.chk_sz = xfer_spec.chksz sz.tot_sz = xfer_spec.totsz await ch.write_sz(sz) csr = await ch.read_csr() csr.ine_done = 1 csr.inc_src = xfer_spec.inc_src csr.inc_dst = xfer_spec.inc_dst print("INC_SRC: " + str(csr.inc_src) + " INC_DST: " + str(csr.inc_dst)) csr.en = 1 await ch.write_csr(csr) await ch.write_src_addr(0x00000000 | int(channel) << 16) await ch.write_dst_addr(0x80000000 | int(channel) << 16) # TODO: start xfer # Wait for completion await self.done_ev[int(channel)].wait() self.done_ev[int(channel)].clear() # TODO: program and carry out transfer # await cocotb.triggers.Timer(1, "us") self.active_xfers -= 1 print("Channel " + str(channel) + " done " + str(self.active_xfers)) self.active_channels[channel] = False self.n_complete += 1 pass
def test_inline_foreach_enum(self): class my_e(IntEnum): A = auto() B = auto() C = auto() @vsc.randobj class my_c(object): def __init__(self): self.ef = vsc.rand_enum_t(my_e) self.arr = vsc.rand_list_t(vsc.enum_t(my_e), 10) it = my_c() with it.randomize_with(): with vsc.foreach(it.arr, idx=True) as i: it.arr[i].inside(vsc.rangelist(my_e.B))
def test_inline_foreach(self): @vsc.randobj class my_s(object): def __init__(self): super().__init__() self.arr = vsc.rand_list_t(vsc.uint8_t(), sz=10) item = my_s() with item.randomize_with() as it: with vsc.foreach(it.arr, idx=True) as i: with vsc.if_then(i == 0): it.arr[i] == 1 with vsc.else_then: it.arr[i] == it.arr[i-1]+1 for i in range(10): self.assertEqual(item.arr[i], i+1)
def select_c(self): with vsc.foreach(self.selectedList, idx=True) as i: self.selectedList[i].inside(self.available) with vsc.if_then(i < 4): self.selectedList[i] + 1 == self.selectedList[i + 1]
def l_c(self): self.l.size in vsc.rangelist(vsc.rng(2, 10)) with vsc.foreach(self.l, it=False, idx=True) as idx: with vsc.if_then(idx > 0): self.l[idx] == self.l[idx - 1] + 1
def test_c(self): with vsc.foreach(self.my_l, it=True, idx=True) as (i, it): self.test.not_inside(vsc.rangelist([it.a, it.b]))
def sum_c(self): self.l.sum == 5 with vsc.foreach(self.l) as it: it != 0
def ab_c(self): with vsc.foreach(self.vals) as it: it < 64 vsc.unique(self.vals)
def ab_c(self): self.a in vsc.rangelist(1, 2, 3) with vsc.foreach(self.temp, idx=True) as i: self.a != self.temp[i]
def cx(self): with vsc.foreach(self.x, idx=True) as i: self.x[i] >= -32768 self.x[i] <= 32768 self.x[i] == i - 27
def parent_c(self): with vsc.foreach(self.c1, idx=True) as i: self.c1[i].a[0].value == self.c2[i].x[0].value