1) ASSIGN
//Verilog module for 1:4 DEMUX
module Demux_1x4_assign(
Data_in,
sel,
Data_out_0,
Data_out_1,
Data_out_2,
Data_out_3
);
//list the inputs and their sizes
input [3:0]Data_in;
input [1:0] sel;
//list the outputs and their sizes
output [3:0] Data_out_0,Data_out_1,Data_out_2,Data_out_3;
assign Data_out_0[0] = (Data_in[0]) & (~sel[1] & ~sel[0]) ;
assign Data_out_0[1] = (Data_in[1]) & (~sel[1] & ~sel[0]) ;
assign Data_out_0[2] = (Data_in[2]) & (~sel[1] & ~sel[0]) ;
assign Data_out_0[3] = (Data_in[3]) & (~sel[1] & ~sel[0]) ;
assign Data_out_1[0] = (Data_in[0]) & (~sel[1] & sel[0]) ;
assign Data_out_1[1] = (Data_in[1]) & (~sel[1] & sel[0]) ;
assign Data_out_1[2] = (Data_in[2]) & (~sel[1] & sel[0]) ;
assign Data_out_1[3] = (Data_in[3]) & (~sel[1] & sel[0]) ;
assign Data_out_2[0] = (Data_in[0]) & ( sel[1] & ~sel[0]) ;
assign Data_out_2[1] = (Data_in[1]) & ( sel[1] & ~sel[0]) ;
assign Data_out_2[2] = (Data_in[2]) & ( sel[1] & ~sel[0]) ;
assign Data_out_2[3] = (Data_in[3]) & ( sel[1] & ~sel[0]) ;
assign Data_out_3[0] = (Data_in[0]) & ( sel[1] & sel[0]) ;
assign Data_out_3[1] = (Data_in[1]) & ( sel[1] & sel[0]) ;
assign Data_out_3[2] = (Data_in[2]) & ( sel[1] & sel[0]) ;
assign Data_out_3[3] = (Data_in[3]) & ( sel[1] & sel[0]) ;
endmodule
// 時間單位 100ns, 時間精確度100 ps
`timescale 100ns/10ps
module tb_demux;
// Inputs
reg [3:0]Data_in;
reg [1:0] sel;
// Outputs
wire [3:0]Data_out_0,Data_out_1,Data_out_2,Data_out_3;
// Instantiate the Unit Under Test (UUT)
Demux_1x4_assign UUT (
.Data_in(Data_in),
.sel(sel),
.Data_out_0(Data_out_0),
.Data_out_1(Data_out_1),
.Data_out_2(Data_out_2),
.Data_out_3(Data_out_3)
);
initial begin
//Apply Inputs
Data_in <= $random;
sel = 0;
#100; sel = 1;
Data_in <= $random;
#100; sel = 2;
Data_in <= $random;
#100; sel = 3;
Data_in <= $random;
#100;
Data_in = 0;
#100;
$stop;
end
endmodule
2) Case
//Verilog module for 1:4 DEMUX
module DeMux_1x4(
Data_in,
sel,
Data_out_0,
Data_out_1,
Data_out_2,
Data_out_3
);
//list the inputs and their sizes
input [3:0]Data_in;
input [1:0] sel;
//list the outputs and their sizes
output [3:0] Data_out_0,Data_out_1,Data_out_2,Data_out_3;
//Internal variables
reg [3:0] Data_out_0,Data_out_1,Data_out_2,Data_out_3;
//always block with Data_in and sel in its sensitivity list
always @(Data_in or sel)
begin
case (sel) //case statement with "sel"
//multiple statements can be written inside each case.
//you just have to use 'begin' and 'end' keywords as shown below.
2'b00 : begin
Data_out_0 = Data_in;
Data_out_1 = 0;
Data_out_2 = 0;
Data_out_3 = 0;
end
2'b01 : begin
Data_out_0 = 0;
Data_out_1 = Data_in;
Data_out_2 = 0;
Data_out_3 = 0;
end
2'b10 : begin
Data_out_0 = 0;
Data_out_1 = 0;
Data_out_2 = Data_in;
Data_out_3 = 0;
end
2'b11 : begin
Data_out_0 = 0;
Data_out_1 = 0;
Data_out_2 = 0;
Data_out_3 = Data_in;
end
endcase
end
endmodule
// 時間單位 100ns, 時間精確度100 ps
`timescale 100ns/10ps
module tb_demux;
// Inputs
reg [3:0]Data_in;
reg [1:0] sel;
// Outputs
wire [3:0]Data_out_0,Data_out_1,Data_out_2,Data_out_3;
// Instantiate the Unit Under Test (UUT)
DeMux_1x4 UUT (
.Data_in(Data_in),
.sel(sel),
.Data_out_0(Data_out_0),
.Data_out_1(Data_out_1),
.Data_out_2(Data_out_2),
.Data_out_3(Data_out_3)
);
initial begin
//Apply Inputs
Data_in <= $random;
sel = 0;
#100; sel = 1;
Data_in <= $random;
#100; sel = 2;
Data_in <= $random;
#100; sel = 3;
Data_in <= $random;
#100;
Data_in = 0;
#100;
$stop;
end
endmodule
沒有留言:
張貼留言