SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `define ASSERT(expr) begin if (!(expr)) begin $display("FAIL"); $finish; end end
  2.  
  3. module priority_encoder_v1(input [3:0] in, output [1:0] out, output v);
  4.   assign v = |in;
  5.   assign out = (in[3] == 1) ? 3 :
  6.                (in[2] == 1) ? 2 :
  7.                (in[1] == 1) ? 1 :
  8.                               0;
  9. endmodule
  10.  
  11. module priority_encoder_v2(input [3:0] in, output reg [1:0] out, output reg v);
  12.   always @* begin
  13.     v = |in;
  14.     out = (in[3] == 1) ? 3 :
  15.           (in[2] == 1) ? 2 :
  16.           (in[1] == 1) ? 1 :
  17.                          0;
  18.   end
  19. endmodule
  20.  
  21. module testbench #(parameter SIZE=4);
  22.   reg [SIZE:0] in; // 1 bit bigger
  23.   wire [1:0] out;
  24.   wire v;
  25.  
  26.   priority_encoder_v1 pe(in[SIZE-1:0], out, v);
  27.  
  28.   initial begin
  29.     for (in = 0; in < (1<<SIZE); in = in + 1) begin
  30.       #1 $display("in=%d, out=%d, v=%d", in, out, v);
  31.       if (in == 0)
  32.         `ASSERT(v === 0)
  33.       else begin
  34.         `ASSERT(v === 1)
  35.         if (in >= 4'b1000)
  36.           `ASSERT(out === 3)
  37.         else if (in >= 4'b0100)
  38.           `ASSERT(out === 2)
  39.         else if (in >= 4'b0010)
  40.           `ASSERT(out === 1)
  41.         else if (in >= 4'b0001)
  42.           `ASSERT(out === 0)
  43.       end
  44.     end
  45.   end
  46. endmodule
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top