Verilog编程基础练习

Verilog编程基础练习

一、一位全加器

(一)门级描述

1.verilog代码如下:

module ad_1(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

2.RTL电路图如下:

在这里插入图片描述

(二)行为级描述

1.verilog代码如下:

module ad_12 (
  input A, B, Cin,
  output Sum, Cout

);
  assign {Cout, Sum} = A + B + Cin;
endmodule

2.RTL电路图如下:

在这里插入图片描述

(三)logisim电路图

在这里插入图片描述

二、四位全加器

(一)门级描述

1.verilog代码如下:

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

2.RTL电路图如下:

在这里插入图片描述

(二)行为级描述

1.verilog代码如下:

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

2.RTL电路图如下:

在这里插入图片描述

(三)logisim电路图

在这里插入图片描述

三、Verilog设计一个8位全加器

1.verilog代码如下:

module ad8 (
  input [7:0] A, B, 
  input cin,
  output[7:0]sum, 
  output cout

);
  assign {cout, sum} = A + B + cin;
endmodule

2.RTL电路图如下:

在这里插入图片描述

四、Verilog设计一个3-8译码器

(一)Logsim电路绘制一个3-8译码器的电路图:

在这里插入图片描述

(二)3-8译码器的真值表:

abcY0Y1Y2Y3Y4Y5Y6Y7
00010000000
00101000000
01000100000
01100010000
10000001000
10100000100
11000000010
11100000001

(三)verilog生成RTL电路图

1.verilog代码如下:

module Decoder_3to8( 
	a, 
	b, 
	c, 
	out 
);
input a; //输入端口a 
input b; //输入端口b 
input c; //输入端口c 
output [7:0]out;//输出端口out 
 
reg [7:0]out; 
always@(a,b,c)
begin 
	case({a,b,c}) 
	3'b000:out = 8'b0000_0001; 
	3'b001:out = 8'b0000_0010; 
	3'b010:out = 8'b0000_0100; 
	3'b011:out = 8'b0000_1000; 
	3'b100:out = 8'b0001_0000; 
	3'b101:out = 8'b0010_0000; 
	3'b110:out = 8'b0100_0000; 
	3'b111:out = 8'b1000_0000; 
	endcase 
end
endmodule

2.RTL电路图如下:

在这里插入图片描述

(四)在modelsim中仿真生成波形图

在这里插入图片描述

(五)对应的问题

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

答:Verilog综合生成的电路将内部的基本门电路进行封装,只通过输入输出来观察实行对应的功能,简化电路图,仿真测试生成的结果与真值表相同

2)Verilog代码设计的3-8译码器模块的输出信号 为何要定义为 reg类型而不用默认wire(导线)类型?改成wire型是否可以?

答:reg型表示寄存器类型,用于always模块中被赋值的信号,通常用于存储状态或时序逻辑。wire型表示连续赋值类型,用于assign关键字指定的组合逻辑信号,输入输出都默认为wire类型,通常用于组合逻辑。

五、扩展实验

1.verilog代码如下:

module ALU16 (
    input [15:0] operand_A,
    input [15:0] operand_B,
    input [2:0]  opcode,  // 0: ADD, 1: SUB, 2: AND, 3: OR, 4: XOR, 5: NOT
    output reg [15:0] result,
    output reg zero_flag
);

always @* begin
    case(opcode)
        3'b000: result = operand_A + operand_B;      // 加法
        3'b001: result = operand_A - operand_B;      // 减法
        3'b010: result = operand_A & operand_B;      // 与
        3'b011: result = operand_A | operand_B;      // 或
        3'b100: result = operand_A ^ operand_B;      // 或非
        3'b101: result = ~operand_A;                 // 非
        default: result = 16'b0;                     // Default case (optional)
    endcase
    
    zero_flag = (result == 16'b0);
end

endmodule

2.RTL电路图如下:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值