// tb_comb.sv // Exhaustive self-checking testbench for the combinational blocks. // Run: iverilog -g2012 -o sim tb_comb.sv mux4.sv decoder3to8.sv prio_enc8.sv \ // prio_enc4.sv adder.sv compare.sv latch_fixed.sv && vvp sim module tb_comb; int errors = 0; // ---- mux4 ---- logic [1:0] sel; logic [7:0] ma, mb, mc, md, my; mux4 #(.WIDTH(8)) u_mux (.sel(sel), .a(ma), .b(mb), .c(mc), .d(md), .y(my)); // ---- decoder3to8 ---- logic den; logic [2:0] dsel; logic [7:0] dy; decoder3to8 u_dec (.en(den), .sel(dsel), .y(dy)); // ---- priority encoders ---- logic [7:0] req8; logic [2:0] idx8; logic v8; prio_enc8 u_p8 (.req(req8), .idx(idx8), .valid(v8)); logic [3:0] req4; logic [1:0] idx4; logic v4; prio_enc4 u_p4 (.req(req4), .idx(idx4), .valid(v4)); // ---- adder and comparator ---- logic [7:0] aa, ab, as; logic acin, acout; adder #(.WIDTH(8)) u_add (.a(aa), .b(ab), .cin(acin), .sum(as), .cout(acout)); logic eq, lt_u, lt_s; compare #(.WIDTH(8)) u_cmp (.a(aa), .b(ab), .eq(eq), .lt_u(lt_u), .lt_s(lt_s)); // ---- latch_fixed ---- logic [1:0] mode; logic [7:0] ly; logic lzero; latch_fixed u_lf (.mode(mode), .a(aa), .b(ab), .y(ly), .zero(lzero)); // Reference model for the priority encoder: scan from the top bit down. function automatic int highest_set(input logic [7:0] v); for (int i = 7; i >= 0; i--) if (v[i]) return i; return -1; endfunction initial begin // mux4: every select value with fixed, distinct data. ma = 8'h11; mb = 8'h22; mc = 8'h33; md = 8'h44; for (int s = 0; s < 4; s++) begin sel = 2'(s); #1; if (my !== (8'h11 * (s + 1))) begin $display("FAIL mux4 sel=%0d y=%h", s, my); errors++; end end // decoder: all 16 input combinations. for (int e = 0; e < 2; e++) for (int s = 0; s < 8; s++) begin den = 1'(e); dsel = 3'(s); #1; if (dy !== (e ? (8'b1 << s) : 8'b0)) begin $display("FAIL decoder en=%0d sel=%0d y=%b", e, s, dy); errors++; end end // priority encoders: all input values. for (int r = 0; r < 256; r++) begin req8 = 8'(r); req4 = 4'(r); #1; if (v8 !== (r != 0) || (r != 0 && idx8 !== 3'(highest_set(8'(r))))) begin $display("FAIL prio_enc8 req=%b idx=%0d valid=%b", req8, idx8, v8); errors++; end if (v4 !== (r[3:0] != 0) || (r[3:0] != 0 && idx4 !== 2'(highest_set({4'b0, 4'(r)})))) begin $display("FAIL prio_enc4 req=%b idx=%0d valid=%b", req4, idx4, v4); errors++; end end // adder, comparator and latch_fixed: all operand pairs. for (int x = 0; x < 256; x++) for (int y = 0; y < 256; y++) for (int c = 0; c < 2; c++) begin aa = 8'(x); ab = 8'(y); acin = 1'(c); mode = 2'(c); #1; if ({acout, as} !== 9'(x + y + c)) begin $display("FAIL adder %0d+%0d+%0d", x, y, c); errors++; end if (eq !== (x == y) || lt_u !== (x < y) || lt_s !== ($signed(8'(x)) < $signed(8'(y)))) begin $display("FAIL compare %0d %0d", x, y); errors++; end if (c == 0 && (ly !== 8'(x) || lzero !== (x == 0))) begin $display("FAIL latch_fixed mode 0"); errors++; end if (c == 1 && (ly !== 8'(y) || lzero !== 1'b0)) begin $display("FAIL latch_fixed mode 1"); errors++; end end mode = 2'd3; #1; if (ly !== 8'd0 || lzero !== 1'b0) begin $display("FAIL latch_fixed mode 3"); errors++; end if (errors == 0) $display("PASS"); else $display("FAILED with %0d errors", errors); $finish; end endmodule