Verilog编程基础练习

本文介绍了使用Verilog语言设计3-8译码器和全加器的详细步骤,包括电路图绘制、真值表、Verilog代码实现、RTL电路图以及在Modelsim中的仿真。着重讲解了行为级和门级描述的区别,以及全加器的扩展到16位的ALU示例。
摘要由CSDN通过智能技术生成

Verilog编程基础练习

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

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

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

(三)Verilog设计3-8译码器并生成RTL电路图

  1. Verilog代码如下:
module YiMa( 
	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
  1. RTL电路图如下:

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

image-20231218150115384

(五)问题

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

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

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

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

二、Verilog设计全加器

(一)门级描述

1.一位全加器
  • Verilog代码如下:
module ad(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电路图如下:

  • Logsim电路图如下:

image-20231218152541913

2.四位全加器

  • Verilog代码如下:
module ad(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电路图如下:

image-20231218155153941

  • Logsim电路图如下:

    image-20231218155228754

三、Verilog行为级描述设计全加器

(一)一位全加器

  1. Verilog代码如下 :
module ad_1 (
  input A, B, Cin,
  output Sum, Cout

);
  assign {Cout, Sum} = A + B + Cin;
endmodule
  1. RTL电路图如下:

image-20231218153018988

(二)四位全加器

  1. Verilog代码如下:

    module ad1 (
    input A, B, Cin,
    output Sum, Cout
    );
    assign {Cout, Sum} = A + B + 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
    

    2.RTL电路图如下:
    image-20231218155421365

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

  • Verilog代码如下:
module ad8 (
input [7:0] A, B,
input cin,
output[7:0]sum,
output cout
);
assign {cout, sum} = A + B + cin;
endmodule
  • RTL电路图如下:

image-20231218160151615

五、扩展实验

  • 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
    // Set zero_flag
    zero_flag = (result == 16'b0);
    end
    endmodule
    
  • RTL电路图如下:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值