Verilog 编程基础练习

目录

一、3-8译码器

1、用 Logsim 绘制3-8译码器电路图

2、3-8译码器逻辑真值表

3、用 Verilog 编程(if-else)设计3-8译码器,生成RTL原理电路图并进行仿真

(1)、Verilog 实现三八译码器代码

(2)、生成RTL电路如下图

(3)、仿真

3、实验问题分析

二、全加器电路

1、1位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

  2、 4位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

3、用Verilog的行为级方式完成1位全加器和4位全加器

(1)、1位全加器

(2)、4位全加起

3、 8位全加器


一、3-8译码器

1、用 Logsim 绘制3-8译码器电路图

2、3-8译码器逻辑真值表

3-8译码器真值表
abcY0Y1Y2Y3Y4Y5Y6Y7
00000000001
00100000010
01000000100
01100001000
10000010000
10100100000
11001000000
11110000000

3、用 Verilog 编程(if-else)设计3-8译码器,生成RTL原理电路图并进行仿真

(1)、Verilog 实现三八译码器代码
module decoder3_8
(
input wire in1 , //输入信号 in1
input wire in2 , //输入信号 in2
input wire in3 , //输入信号 in3
output reg [7:0] out //输出信号 out
);
always@(*)
if({in1, in2, in3} == 3'b000)
out = 8'b0000_0001;
else if({in1, in2, in3} == 3'b001)
out = 8'b0000_0010;
else if({in1, in2, in3} == 3'b010)
out = 8'b0000_0100;
else if({in1, in2, in3} == 3'b011)
out = 8'b0000_1000;
else if({in1, in2, in3} == 3'b100)
out = 8'b0001_0000;
else if({in1, in2, in3} == 3'b101)
out = 8'b0010_0000;
else if({in1, in2, in3} == 3'b110)
out = 8'b0100_0000;
else if({in1, in2, in3} == 3'b111)
out = 8'b1000_0000;
else
out = 8'b0000_0001;
 endmodule
`timescale 1ns/1ns
module tb_decoder3_8();
reg in1;
reg in2;
reg in3;
wire [7:0] out;
initial begin
in1 <= 1'b0;
in2 <= 1'b0;
in3 <= 1'b0;
 end
always #10 in1 <= {$random} % 2; 
always #10 in2 <= {$random} % 2; 
always #10 in3 <= {$random} % 2; 
initial begin
$timeformat(-9, 0, "ns", 6);
$monitor("@time %t:in1=%b in2=%b in3=%b out=%b",$time,in1,in2,in3,out);
end
decoder3_8 decoder3_8_ins
(
.in1(in1), //input in1
.in2(in2), //input in2
.in3(in3), //input in3
.out(out) //output [7:0] out
);
endmodule
(2)、生成RTL电路如下图

(3)、仿真

仿真测试文件:

timescale 1ns/1ns
module three_to_eight_tb();
reg  a;
reg  b;
reg  c;
wire   [7:0]   out;
initial    begin
    a <= 1'b1;
    b <= 1'b0;
    c <= 1'b1;
end
always #10 a <= {$random} % 2;
always #10 b <= {$random} % 2;
always #10 c <= {$random} % 2;
initial begin
 $timeformat(-9, 0, "ns", 6);
    $monitor("@time %t:a=%b b=%b c=%b out=%b",$time,a,b,c,out);
end
three_to_eight three_to_eight_ins
(

    .a(a), 
    .b(b),
    .c(c),
    .out(out)

 );
endmodule
 

仿真结果:

3、实验问题分析

(1)、Verilog综合生成的3-8译码器电路原理图与原始设计电路存在什么差异?仿真测试生成的结果是否与真值表一致?

       Verilog综合生成的3-8译码器电路原理图与原始设计电路在结构和连接上有所不同,原始设计电路时基于门电路的物理链接,而Verilog综合生成的电路是基于编程逻辑单元的配置,这种差异体现在实现的方式上,但两种方式在功能上是相同的。

(2)、Verilog代码设计的3-8译码器模块的输出信号为何要定义为reg类型而不用默认wire(导线)类型?改成wire型是否可以?(即是否可以把 output reg [7:0] out 改为 output [7:0] out)修改后会出现哪些错误?为什么会出现错误?

        reg类型用于定义寄存器,即具有存储功能的元件,而wire类型用于定义导线,及用于连接模块间的信号传输,3-8译码器模块中,输出信号需要根据输入信号的变化而变化,因此需要被定义为reg类型。   

        如果将3-8译码器模块的输出信号由reg类型改为wire类型,那么这个信号将不再有存储功能,也就是说,模块内部不能再对这个信号进行赋值、计算或驱动,浙江导致模块无法正常工作,因为输出信号需要根据输入信号的变化而变化。

        如果将output reg [7:0] out 改为output [7:0] out,那么在模块内部的赋值操作将无法执行,例如,例如 out = 3'b101;这样的语句将无法编译通过,因为 wire类型的信号不能在模块内部被赋值。

二、全加器电路

1、1位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

代码:

module full_adder(A,B,cin,sum,cout);
	input A,B,cin;
	output sum,cout;
	wire t1,t2,t3,t4;
	and U1(t1,A,B);
	and U2(t2,A,cin);
	and U3(t3,B,cin);
	or U4(cout,t1,t2,t3);
	xor U5(t4,A,B);
	xor U6(sum,t4,cin);
endmodule

RTL电路

  2、 4位全加器

(1)、Logsim逻辑电路图

(2)、Verilog代码及生成的RTL电路

代码:

module eight(A,B,cin,sum,cout);
	input A,B,cin;
	output sum,cout;
	wire t1,t2,t3,t4;
	and U1(t1,A,B);
	and U2(t2,A,cin);
	and U3(t3,B,cin);
	or U4(cout,t1,t2,t3);
	xor U5(t4,A,B);
	xor U6(sum,t4,cin);
endmodule

module ad4
(
	input [3:0]A,B,
	input cin,
	output [3:0]sum,
	output cout
);
	wire[4:0]c;
	assign c[0] = cin;
	ad1 ad10(A[0],B[0],c[0],sum[0],c[1]);
	ad1 ad11(A[1],B[1],c[1],sum[1],c[2]);
	ad1 ad12(A[2],B[2],c[2],sum[2],c[3]);
	ad1 ad13(A[3],B[3],c[3],sum[3],c[4]);
	assign cout = c[4];
endmodule

RTL电路:

3、用Verilog的行为级方式完成1位全加器和4位全加器

(1)、1位全加器

代码:

module fulladder(
    input a,  
    input b,  
    input cin, 
    output sum,  
    output cout  
);
assign sum = a ^ b ^ cin;  
assign cout = (a & b) | (b & cin) | (a & cin); 
endmodule

RTL电路

(2)、4位全加起

代码:

module addr4
(
	input	wire	[3:0]	ina			,
	input	wire	[3:0]	inb			,
	input	wire			cin			,
	output	wire	[3:0]	sum			,
	output	wire			cout		 
);

assign {cout,sum}=ina+inb+cin;

endmodule                                                                                                 

RTL电路:

3、 8位全加器

代码:

module eight(
	input clk,
	input rst_n,
		
	input [7:0] a,
	input [7:0] b,
	input cin,
	input enable,
	
	output reg [7:0] sum,
	output reg cout
);

always@(posedge clk or negedge rst_n)begin
	if(!rst_n)begin
		sum <= 8'b0;
		cout <= 1'b0;
	end
	else if(enable)begin
		{cout,sum} <= a+b+cin;
	end
	else begin
		sum <= sum;
		cout <= cout;
	end
end
endmodule

RTL电路:


 

  • 21
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值