4x1 Multiplexer using case statement - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

203


48
 
1
// Code your testbench here
2
// or browse Examples
3
module tb_4to1_mux;
4
5
   // Declare internal reg variables to drive design inputs
6
   // Declare wire signals to collect design output
7
   // Declare other internal variables used in testbench
8
   reg [3:0] a;
9
   reg [3:0] b;
10
   reg [3:0] c;
11
   reg [3:0] d;
12
   wire [3:0] out;
13
   reg [1:0] sel;
14
   integer i;
15
   
16
   // Instantiate one of the designs, in this case, we have used the design with case statement
17
   // Connect testbench variables declared above with those in the design
18
   mux_4to1_case  mux0 (   .a (a),
19
                           .b (b),
20
                           .c (c),
21
                           .d (d),
22
                           .sel (sel),
23
                           .out (out));
24
25
   // This initial block is the stimulus
26
   initial begin
27
     $monitor ("[%0t] sel=0x%0h a=0x%0h b=0x%0h c=0x%0h d=0x%0h out=0x%0h", $time, sel, a, b, c, d, out);
28
   
29
      // 1. At time 0, drive random values to a/b/c/d and keep sel = 0
30
      sel <= 0;
31
      a <= $random;
32
      b <= $random;
33
      c <= $random;
34
      d <= $random;
35
      
36
      // 2. Change the value of sel after every 5ns
37
      for (i = 1; i < 4; i=i+1) begin
38
         #5 sel <= i;
39
      end
40
      
41
      // 3. After Step2 is over, wait for 5ns and finish simulation
42
      #5 $finish;
43
   end
44
   initial begin
45
     $dumpvars;
46
     $dumpfile("dump.vcd");
47
   end
48
endmodule
19
 
1
// Code your design here
2
module mux_4to1_case ( input [3:0] a,                 // 4-bit input called a
3
                       input [3:0] b,                 // 4-bit input called b
4
                       input [3:0] c,                 // 4-bit input called c
5
                       input [3:0] d,                 // 4-bit input called d
6
                       input [1:0] sel,               // input sel used to select between a,b,c,d
7
                       output reg [3:0] out);             // 4-bit output based on input sel
8
                  
9
   // This always block gets executed whenever a/b/c/d/sel changes value
10
   // When that happens, based on value in sel, output is assigned to either a/b/c/d
11
   always @ (a or b or c or d or sel) begin
12
      case (sel)
13
         2'b00 : out <= a;
14
         2'b01 : out <= b;
15
         2'b10 : out <= c;
16
         2'b11 : out <= d;
17
      endcase
18
   end
19
endmodule
16178 views and 4 likes     
A short description will be helpful for you to remember your playground's details
 
100:0