两手准备秋招,希望有个好结果,只是作为学习笔记
4to1-MUX
分别用case语句和if语句写两个module对比,从中可以对if语句和case语句未写完产生锁存器加深理解:
正确代码:
module mux(sel,a,out);
input wire [1:0] sel;
input wire [3:0] a;
output reg out;
always @(*) begin
if (sel==0)
begin
out=a[0];
end
else if (sel==1)
begin
out=a[1];
end
else if (sel==2)
begin
out=a[2];
end
else if (sel==3)
begin
out=a[3];
end
end
endmodule
module mux1(sel,a,out);
input wire [1:0] sel;
input wire [3:0] a;
output reg out;
always @(*) begin
case (sel[1:0])
2'd0: out=a[0];
2'd1: out=a[1];
2'd2: out=a[2];
default:out=a[3];
endcase
end
endmodule
test代码:
`timescale 1ns/1ns
module test();
reg [1:0] sel;
reg [3:0] a;
wire out;
initial begin
sel=2'd1;
a=4'd2;
#5;
sel=2'd2;
a=4'd1;
#5;
sel=2'd4;
a=4'd3;
#5;