0

I've seen some examples on internet about mux 4:1 on verilog. I've tried to do something but the output is not the expeted. This is the source :

module mux41 (a, b, c, d,select,z);

input a,b,c,d;
input [1:0]select;
output reg z;

always@(select )
begin
case (select)
    2'b00: assign z=a;
    2'b01: assign z=b;
    2'b10: assign z=c;
    2'b11: assign z=d;
endcase
end
endmodule

and this is the testbench :

module mux41_tb;

reg at,bt,ct,dt;
reg [1:0] selectt;
wire zt;

mux41   test(.a(at),.b(bt),.c(ct),.d(dt),.select(selectt),
            .z(zt));
            
initial
begin
     $monitor ("a=%d",at,"b=%b",bt,"c=%b",ct,
                    "d=%b","select=%b",selectt,"z=%z",zt);
                    
    selectt =2'b00;
    #5
    selectt =2'b01;
    #5
    selectt =2'b10;
    #5
    selectt =2'b11;
    #5;
    
end

endmodule

but the output is the following :

enter image description here

My question is what I should chance in both codes (source and testbench).

blackgreen
  • 34,072
  • 23
  • 111
  • 129
NIN
  • 197
  • 13
  • Are you using physical switches on your board to select the mux? If so, you might be subject to glitches caused by bouncing of the switch. If this is the case, you need to add either an analog or a digital debouncer. – Russell Aug 24 '16 at 16:57
  • Hi russell.. Not in all.. I am just trying to simulate a simple mux IC (mux 4:1) in verilog. – NIN Aug 24 '16 at 17:09
  • FYI, you should not use `assign` within an `always` block – Greg Aug 24 '16 at 23:39

1 Answers1

1

You're getting

a=xb=xc=x

Because you never gave a, b, or c any values. You assign z to a for example, but a has no value since at has no value, so you're just getting x.

As for

d = 0111...

It's because of your monitor line

$monitor ("a=%d",at,"b=%b",bt,"c=%b",ct,
                "d=%b","select=%b",selectt,"z=%z",zt);

You forgot dt

$monitor ("a=%d",at,"b=%b",bt,"c=%b",ct,
                "d=%b",dt,"select=%b",selectt,"z=%z",zt);
Jason
  • 808
  • 6
  • 25
  • thank you Downvote. I've improved the testbench and now this is the new output : (Look the update of my post) – NIN Aug 24 '16 at 18:26
  • Awesome, now give `at`,`bt`,`ct`,`dt`,and `selectt` values – Jason Aug 24 '16 at 18:57
  • Downvote But I suppose I 'm giving values to { selectt } with : selectt = 2'b00; selectt 2'b01, and so on. – NIN Aug 24 '16 at 21:13
  • yeahhh downvote. thank you so much man. Wow , At last I get to see what were my problems. Look the update please. – NIN Aug 24 '16 at 21:27
  • Sorry for this question. Can I contact to you ? – NIN Aug 24 '16 at 22:26
  • If you have another question, post on stackoverflow and comment here again. I'll take a look :) – Jason Aug 24 '16 at 23:50