西北工业大学-数字电子技术基础-实验报告-实验2
数字电子技术基础第二次实验报告
一、题目代码以及波形分析
1. 设计一款可综合的2选1多路选择器
①编写模块源码
module multiplexer(x1,x2,s,f);
input x1,x2,s;
output f;
assign f=(~s&x1)|(s&x2);
endmodule
②测试模块
`timescale 1ns/1ps
module tb_multiplexer;
reg x1_test;
reg x2_test;
reg s_test;
wire f_test;
initial
s_test=0;
always #80 s_test=~s_test;
initial
begin
x1_test=0;
x2_test=0;
#20
x1_test=1;
x2_test=0;
#20
x1_test=0;
x2_test=1;
#20
x1_test=1;
x2_test=1;
#20
x1_test=0;
x2_test=0;
#20
x1_test=1;
x2_test=0;
#20
x1_test=0;
x2_test=1;
#20
x1_test=1;
x2_test=1;
end
multiplexer UUT_multiplexer(.x1(x1_test),.x2(x2_test),.s(s_test),.f(f_test));
endmodule
③仿真后的波形截图
④对波形的分析
本例目的是令s为控制信号,实现二选一多路选择器。分析波形图可以知道,s为0时,f 输出x1信号;s为1时,f输出x2信号。所以实现了目标功能。
2. 设计一款可综合的2-4译码器
①编写模块源码
module dec2to4(W,En,Y);
input [1:0]W;
input En;
output reg [0:3]Y;
always@(W,En)
case({En,W})
3'b100:Y=4'b1000;
3'b101:Y=4'b0100;
3'b110:Y=4'b0010;
3'b111:Y=4'b0001;
default:Y=4'b0000;
endcase
endmodule
②测试模块
`timescale 1ns/1ps
module tb_dec2to4;
reg [1:0]W_test;
reg En_test;
wire [0:3]Y_test;
initial
En_test=0;
always #80 En_test=~En_test;
initial
begin
W_test=2'b00;
#20
W_test=2'b01;
#20
W_test=2'b11;
#20
W_test=2'b10;
#20
W_test=2'b00;
#20
W_test=2'b01;
#20
W_test=2'b11;
#20
W_test=2'b10;
#20
W_test=2'b00;
end
dec2to4 UUT_dec2to4(.W(W_test),.En(En_test),.Y(Y_test));
endmodule
③仿真后的波形截图
④对波形的分析
本例目的是实现可综合的2-4译码器,其中数组W是输入信号,共有两个值,输入一个两位二进制数据,目的是通过译码器将它转换成独热码,数组Y是输出信号,输出四个二进制数据,构成独热码。En是使能信号,当En为0时,输出的数组Y始终为0,译码器不工作;当En为1时,输出的数组Y为独热码,译码器工作。根据输出的波形图,可以判断译码器实现了目的。
3.设计一款可综合的8-3编码器
①编写模块源码
module enc8to3(W,Y,z);
input [7:0]W;
output reg [2:0]Y;
output reg z;
always@(W)
begin
z=1;
case(W)
8'b10000000:Y=3'b111;
8'b01000000:Y=3'b110;
8'b00100000:Y=3'b101;
8'b00010000:Y=3'b100;
8'b00001000:Y=3'b011;
8'b00000100:Y=3'b010;
8'b00000010:Y=3'b001;
8'b00000001:Y=3'b000;
default z=0;
endcase
end
endmodule
②测试模块
`timescale 1ns/1ps module tb_enc8to3; reg [7:0]W_test;
wire [2:0]Y_test;
wire z_test;
initial
begin
W_test=8'b10000000;
#20
W_test=8'b01000000;
#20
W_test=8'b00100000;
#20
W_test=8'b00010000;
#20
W_test=8'b00001000;
#20
W_test=8'b00000100;
#20
W_test=8'b00000010;
#20
W_test=8'b00000001;
#20
W_test=8'b00000000;
end
enc8to3 UUT_enc8to3(.W(W_test),.Y(Y_test),.z(z_test));
endmodule
③仿真后的波形截图
④对波形的分析
本例目的是实现可综合的8-3编码器,其中数组W是输入信号,共有八个值,输入八位独热码数据,目的是通过编码器将它转换成三位二进制数据,数组Y是输出信号,输出一个三位二进制数据。z是判断信号,当输入的数据是八位独热码时,输出的z为1,判断编码器工作;当输入的数据不是独热码时,输出的z为0,判断编码器不工作。根据输出的波形图,可以判断编码器实现了目的。
4.设计一款可综合的1位二进制比较器
①编写模块源码
module comparer(a,b,f0,f1,f2);
input a,b;
output reg f0,f1,f2;
always@(a,b)
case({a,b})
2'b00: begin
f0=0;
f1=1;
f2=0;
end
2'b10: begin
f0=1;
f1=0;
f2=0;
end
2'b01: begin
f0=0;
f1=0;
f2=1;
end
2'b11: begin
f0=0;
f1=1;
f2=0;
end
default ; endcase
endmodule
②测试模块
`timescale 1ns/1ps module tb_comparer; reg a_test;
reg b_test;
wire f0_test;
wire f1_test;
wire f2_test;
initial
begin
a_test=0;
b_test=0;
#20
a_test=1;
b_test=0;
#20
a_test=1;
b_test=1;
#20
a_test=0;
b_test=1;
#20
a_test=0;
b_test=0;
end
comparer UUT_comparer(.a(a_test),.b(b_test),.f0(f0_test),.f1(f1_test),.f2(f2_test));
endmodule
③仿真后的波形截图
④对波形的分析
本题目的是实现可综合的1位二进制比较器,其中a和b是输入数据,分别输入一个一位二进制数据,比较器将对它们进行比较。f0、f1、f2是输出数据,用来表示比较结果,如果a 大于b,则f0输出为1;如果a等于b,则f1输出为1;如果a小于b,则f2输出为1。根据输出的波形图判断,1位二进制比较器实现了目标。
5. 设计一款可综合的2+2位简单全加器
①编写模块源码
module fulladder(carryin,x0,x1,y0,y1,s0,s1,carryout);
input carryin,x0,x1,y0,y1;
output s0,s1,carryout;
adder stage0 (carryin,x0,y0,s0,c1);
adder stage1 (c1,x1,y1,s1,carryout);
endmodule
module adder(cin,x,y,s,cout);
input cin,x,y;
output s,cout;
assign s=x^y^cin,
cout=(x&y)|(x&cin)|(y&cin);
endmodule
②测试模块
`timescale 1ns/1ps
module tb_fulladder;
reg carryin_test;
reg x0_test;
reg x1_test;
reg y0_test;
reg y1_test;
wire s0_test;
wire s1_test;
wire carryout_test;
initial
carryin_test=0;
always #160 carryin_test=~carryin_test;
initial
begin
x0_test=0;
x1_test=0; y0_test=0; y1_test=0;
#10
x0_test=1; x1_test=0; y0_test=0; y1_test=0;
#10
x0_test=1; x1_test=1; y0_test=0; y1_test=0;
#10
x0_test=0; x1_test=1; y0_test=0; y1_test=0;
#10
x0_test=0; x1_test=0; y0_test=1; y1_test=0;
#10
x0_test=1; x1_test=0; y0_test=1; y1_test=0;
#10
x0_test=1; x1_test=1; y0_test=1; y1_test=0;
#10
x0_test=0; x1_test=1; y0_test=1;
#10
x0_test=0; x1_test=0; y0_test=1; y1_test=1;
#10
x0_test=1; x1_test=0; y0_test=1; y1_test=1;
#10
x0_test=1; x1_test=1; y0_test=1; y1_test=1;
#10
x0_test=0;
y0_test=1; y1_test=1;
#10
x0_test=0; x1_test=0; y0_test=0; y1_test=1;
#10
x0_test=1; x1_test=0; y0_test=0; y1_test=1;
#10
x0_test=1; x1_test=1; y0_test=0; y1_test=1;