|
| 1 | +package ConcatTuple where |
| 2 | + |
| 3 | +import Vector |
| 4 | + |
| 5 | +-- Test the ConcatTuple type class |
| 6 | + |
| 7 | +{-# verilog sysConcatTuple #-} |
| 8 | +sysConcatTuple :: Module Empty |
| 9 | +sysConcatTuple = |
| 10 | + module |
| 11 | + counter :: Reg (UInt 6) <- mkReg 0 |
| 12 | + |
| 13 | + rules |
| 14 | + "test0": when counter == 0 ==> action |
| 15 | + $display "=== Testing ConcatTuple ===" |
| 16 | + -- Test empty vector to unit type |
| 17 | + let empty :: Vector 0 (Bool, Int 8) |
| 18 | + empty = nil |
| 19 | + let unit :: () |
| 20 | + unit = concatTuple empty |
| 21 | + $display "concatTuple(empty_vector) = () [unit type]" |
| 22 | + counter := counter + 1 |
| 23 | + |
| 24 | + "test1": when counter == 1 ==> action |
| 25 | + -- Test single element vector |
| 26 | + let v1 :: Vector 1 (Bool, Int 8) |
| 27 | + v1 = (True, 25) :> nil |
| 28 | + let result :: (Bool, Int 8) |
| 29 | + result = concatTuple v1 |
| 30 | + $display "concatTuple([(True, 25)]) = (%b, %0d)" (tpl_1 result) (tpl_2 result) |
| 31 | + counter := counter + 1 |
| 32 | + |
| 33 | + "test2": when counter == 2 ==> action |
| 34 | + -- Test vector of 3 single elements to 3-tuple |
| 35 | + let v3 :: Vector 3 (Int 8) |
| 36 | + v3 = 10 :> 20 :> 30 :> nil |
| 37 | + let t3 :: (Int 8, Int 8, Int 8) |
| 38 | + t3 = concatTuple v3 |
| 39 | + $display "concatTuple([10, 20, 30]) = (%0d, %0d, %0d)" (tpl_1 t3) (tpl_2 t3) (tpl_3 t3) |
| 40 | + counter := counter + 1 |
| 41 | + |
| 42 | + "test3": when counter == 3 ==> action |
| 43 | + -- Test vector of 4 single elements to 4-tuple |
| 44 | + let v4 :: Vector 4 (UInt 4) |
| 45 | + v4 = 1 :> 2 :> 3 :> 4 :> nil |
| 46 | + let t4 :: (UInt 4, UInt 4, UInt 4, UInt 4) |
| 47 | + t4 = concatTuple v4 |
| 48 | + $display "concatTuple([1, 2, 3, 4]) = (%0d, %0d, %0d, %0d)" (tpl_1 t4) (tpl_2 t4) (tpl_3 t4) (tpl_4 t4) |
| 49 | + counter := counter + 1 |
| 50 | + |
| 51 | + "test4": when counter == 4 ==> action |
| 52 | + -- Test vector of 2 pairs to 4-tuple |
| 53 | + let v2 :: Vector 2 (Bool, Int 8) |
| 54 | + v2 = (True, 5) :> (False, negate 12) :> nil |
| 55 | + let t4 :: (Bool, Int 8, Bool, Int 8) |
| 56 | + t4 = concatTuple v2 |
| 57 | + $display "concatTuple([(True, 5), (False, -12)]) = (%b, %0d, %b, %0d)" (tpl_1 t4) (tpl_2 t4) (tpl_3 t4) (tpl_4 t4) |
| 58 | + counter := counter + 1 |
| 59 | + |
| 60 | + "test5": when counter == 5 ==> action |
| 61 | + -- Test vector of 3 pairs to 6-tuple |
| 62 | + let v3 :: Vector 3 (UInt 4, Bit 3) |
| 63 | + v3 = (1, 0b001) :> (2, 0b010) :> (3, 0b011) :> nil |
| 64 | + let t6 :: (UInt 4, Bit 3, UInt 4, Bit 3, UInt 4, Bit 3) |
| 65 | + t6 = concatTuple v3 |
| 66 | + $display "concatTuple([(1, 0b001), (2, 0b010), (3, 0b011)]) = (%0d, %b, %0d, %b, %0d, %b)" (tpl_1 t6) (tpl_2 t6) (tpl_3 t6) (tpl_4 t6) (tpl_5 t6) (tpl_6 t6) |
| 67 | + counter := counter + 1 |
| 68 | + |
| 69 | + "test6": when counter == 6 ==> action |
| 70 | + -- Test vector of 2 triples to 6-tuple |
| 71 | + let v2 :: Vector 2 (Bool, Int 8, UInt 4) |
| 72 | + v2 = (True, 10, 5) :> (False, negate 20, 8) :> nil |
| 73 | + let t6 :: (Bool, Int 8, UInt 4, Bool, Int 8, UInt 4) |
| 74 | + t6 = concatTuple v2 |
| 75 | + $display "concatTuple([(True, 10, 5), (False, -20, 8)]) = (%b, %0d, %0d, %b, %0d, %0d)" (tpl_1 t6) (tpl_2 t6) (tpl_3 t6) (tpl_4 t6) (tpl_5 t6) (tpl_6 t6) |
| 76 | + $display "" |
| 77 | + $display "=== Testing unconcatTuple ===" |
| 78 | + counter := counter + 1 |
| 79 | + |
| 80 | + "test7": when counter == 7 ==> action |
| 81 | + -- Test 4-tuple to vector of 4 singles |
| 82 | + let t4 :: (Int 8, Int 8, Int 8, Int 8) |
| 83 | + t4 = (7, 8, 9, 10) |
| 84 | + let v4 :: Vector 4 (Int 8) |
| 85 | + v4 = unconcatTuple t4 |
| 86 | + $display "unconcatTuple((7, 8, 9, 10)) = [%0d, %0d, %0d, %0d]" (v4 !! 0) (v4 !! 1) (v4 !! 2) (v4 !! 3) |
| 87 | + counter := counter + 1 |
| 88 | + |
| 89 | + "test8": when counter == 8 ==> action |
| 90 | + -- Test 4-tuple to vector of 2 pairs |
| 91 | + let t4 :: (Bool, Int 8, Bool, Int 8) |
| 92 | + t4 = (False, 100, True, negate 50) |
| 93 | + let v2 :: Vector 2 (Bool, Int 8) |
| 94 | + v2 = unconcatTuple t4 |
| 95 | + $display "unconcatTuple((False, 100, True, -50)) = [(%b, %0d), (%b, %0d)]" (tpl_1 (v2 !! 0)) (tpl_2 (v2 !! 0)) (tpl_1 (v2 !! 1)) (tpl_2 (v2 !! 1)) |
| 96 | + counter := counter + 1 |
| 97 | + |
| 98 | + "test9": when counter == 9 ==> action |
| 99 | + -- Test 6-tuple to vector of 3 pairs |
| 100 | + let t6 :: (UInt 4, Bit 2, UInt 4, Bit 2, UInt 4, Bit 2) |
| 101 | + t6 = (5, 0b00, 10, 0b01, 15, 0b10) |
| 102 | + let v3 :: Vector 3 (UInt 4, Bit 2) |
| 103 | + v3 = unconcatTuple t6 |
| 104 | + $display "unconcatTuple((5, 0b00, 10, 0b01, 15, 0b10)) = [(%0d, %b), (%0d, %b), (%0d, %b)]" (tpl_1 (v3 !! 0)) (tpl_2 (v3 !! 0)) (tpl_1 (v3 !! 1)) (tpl_2 (v3 !! 1)) (tpl_1 (v3 !! 2)) (tpl_2 (v3 !! 2)) |
| 105 | + counter := counter + 1 |
| 106 | + |
| 107 | + "test10": when counter == 10 ==> action |
| 108 | + -- Test 6-tuple to vector of 2 triples |
| 109 | + let t6 :: (Bool, Int 8, UInt 4, Bool, Int 8, UInt 4) |
| 110 | + t6 = (True, 15, 3, False, negate 25, 7) |
| 111 | + let v2 :: Vector 2 (Bool, Int 8, UInt 4) |
| 112 | + v2 = unconcatTuple t6 |
| 113 | + $display "unconcatTuple((True, 15, 3, False, -25, 7)) = [(%b, %0d, %0d), (%b, %0d, %0d)]" (tpl_1 (v2 !! 0)) (tpl_2 (v2 !! 0)) (tpl_3 (v2 !! 0)) (tpl_1 (v2 !! 1)) (tpl_2 (v2 !! 1)) (tpl_3 (v2 !! 1)) |
| 114 | + $display "" |
| 115 | + $display "=== Round-trip test ===" |
| 116 | + counter := counter + 1 |
| 117 | + |
| 118 | + "test11": when counter == 11 ==> action |
| 119 | + -- Test concatTuple and unconcatTuple round-trip with singles |
| 120 | + let v_orig :: Vector 5 (Int 8) |
| 121 | + v_orig = 1 :> 2 :> 3 :> 4 :> 5 :> nil |
| 122 | + let t5 :: (Int 8, Int 8, Int 8, Int 8, Int 8) |
| 123 | + t5 = concatTuple v_orig |
| 124 | + let v_restored :: Vector 5 (Int 8) |
| 125 | + v_restored = unconcatTuple t5 |
| 126 | + $display "concatTuple/unconcatTuple round-trip (singles): [%0d, %0d, %0d, %0d, %0d]" (v_restored !! 0) (v_restored !! 1) (v_restored !! 2) (v_restored !! 3) (v_restored !! 4) |
| 127 | + counter := counter + 1 |
| 128 | + |
| 129 | + "test12": when counter == 12 ==> action |
| 130 | + -- Test concatTuple and unconcatTuple round-trip with pairs |
| 131 | + let v_orig :: Vector 3 (Bool, Int 8) |
| 132 | + v_orig = (True, 11) :> (False, 22) :> (True, 33) :> nil |
| 133 | + let t6 :: (Bool, Int 8, Bool, Int 8, Bool, Int 8) |
| 134 | + t6 = concatTuple v_orig |
| 135 | + let v_restored :: Vector 3 (Bool, Int 8) |
| 136 | + v_restored = unconcatTuple t6 |
| 137 | + $display "concatTuple/unconcatTuple round-trip (pairs): [(%b, %0d), (%b, %0d), (%b, %0d)]" (tpl_1 (v_restored !! 0)) (tpl_2 (v_restored !! 0)) (tpl_1 (v_restored !! 1)) (tpl_2 (v_restored !! 1)) (tpl_1 (v_restored !! 2)) (tpl_2 (v_restored !! 2)) |
| 138 | + $display "" |
| 139 | + $display "All ConcatTuple tests passed" |
| 140 | + $finish 0 |
0 commit comments