Verilog编程基础练习

一、Quartus、ModelSim下载及其破解

这里推荐在B站上搜索相关视频,视频下方就附带有百度网盘资源。

二、3-8译码器的解释以及仿真实验

(一)3-8Decoder的定义及其运算法则

1.定义:3-8译码器(3-to-8 decoder)是一种数字电路,它接受3个输入信号并将其转换成8个输出信号。这种译码器通常用于将一个3位的二进制输入映射到8个可能的输出线中的一个。其基本原理是通过对输入信号的各种组合进行编码,使得每个可能的输入组合都对应一个唯一的输出。

2. 逻辑运算法则:

对于3位二进制输入 ABC,对应的输出线为 Y0 到 Y7。具体的编码规则如下:

  • 当 ABC = 000 时,Y0 = 1,其余输出线为0。
  • 当 ABC = 001 时,Y1 = 1,其余输出线为0。
  • 当 ABC = 010 时,Y2 = 1,其余输出线为0。
  • 当 ABC = 011 时,Y3 = 1,其余输出线为0。
  • 当 ABC = 100 时,Y4 = 1,其余输出线为0。
  • 当 ABC = 101 时,Y5 = 1,其余输出线为0。
  • 当 ABC = 110 时,Y6 = 1,其余输出线为0。
  • 当 ABC = 111 时,Y7 = 1,其余输出线为0。

(二)3-8译码器(3-8Decoder)Logsim电路仿真以及Quartus模拟

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

2. 3-8译码器真值表:

3.利用Verilog代码生成RTL电路图:

module Decoder_3to8 (
  input [2:0] input_bits,
  output [7:0] output_bits
);

  assign output_bits[0] = (input_bits == 3'b000) ? 1'b1 : 1'b0;
  assign output_bits[1] = (input_bits == 3'b001) ? 1'b1 : 1'b0;
  assign output_bits[2] = (input_bits == 3'b010) ? 1'b1 : 1'b0;
  assign output_bits[3] = (input_bits == 3'b011) ? 1'b1 : 1'b0;
  assign output_bits[4] = (input_bits == 3'b100) ? 1'b1 : 1'b0;
  assign output_bits[5] = (input_bits == 3'b101) ? 1'b1 : 1'b0;
  assign output_bits[6] = (input_bits == 3'b110) ? 1'b1 : 1'b0;
  assign output_bits[7] = (input_bits == 3'b111) ? 1'b1 : 1'b0;

endmodule

4.测试代码:

// Copyright (C) 1991-2013 Altera Corporation
// Your use of Altera Corporation's design tools, logic functions 
// and other software and tools, and its AMPP partner logic 
// functions, and any output files from any of the foregoing 
// (including device programming or simulation files), and any 
// associated documentation or information are expressly subject 
// to the terms and conditions of the Altera Program License 
// Subscription Agreement, Altera MegaCore Function License 
// Agreement, or other applicable license agreement, including, 
// without limitation, that your use is for the sole purpose of 
// programming logic devices manufactured by Altera and sold by 
// Altera or its authorized distributors.  Please refer to the 
// applicable agreement for further details.

// *****************************************************************************
// This file contains a Verilog test bench template that is freely editable to  
// suit user's needs .Comments are provided in each section to help the user    
// fill out necessary details.                                                  
// *****************************************************************************
// Generated on "12/15/2023 16:30:15"
                                                                                
// Verilog Test Bench template for design : Decoder_3to8
// 
// Simulation tool : ModelSim (Verilog)
// 

`timescale 1 ps/ 1 ps
module Decoder_3to8_vlg_tst();
// constants                                           
// general purpose registers
reg eachvec;
// test vector input registers
reg [2:0] input_bits;
// wires                                               
wire [7:0]  output_bits;

// assign statements (if any)                          
Decoder_3to8 i1 (
// port map - connection between master ports and signals/registers   
	.input_bits(input_bits),
	.output_bits(output_bits)
);
initial                                                
begin                                                  
$display("Running testbench");        

	 // Test case 1
    input_bits = 3'b000;
    #10;
    if (output_bits !== 8'b00000001) $fatal("Test case 1 failed");
    // Test case 2
    input_bits = 3'b001;
    #10;
    if (output_bits !== 8'b00000010) $fatal("Test case 2 failed");
	 // Test case 3
    input_bits = 3'b010;
    #10;
    if (output_bits !== 8'b00000100) $fatal("Test case 3 failed");
	 // Test case 4
    input_bits = 3'b011;
    #10;
    if (output_bits !== 8'b00001000) $fatal("Test case 4 failed");
	 // Test case 5
    input_bits = 3'b100;
    #10;
    if (output_bits !== 8'b00010000) $fatal("Test case 5 failed");
	 // Test case 6
    input_bits = 3'b101;
    #10;
    if (output_bits !== 8'b00100000) $fatal("Test case 6 failed");
	 // Test case 7
    input_bits = 3'b110;
    #10;
    if (output_bits !== 8'b01000000) $fatal("Test case 7 failed");
	 // Test case 8
	 input_bits = 3'b111;
    #10;
    if (output_bits !== 8'b10000000) $fatal("Test case 8 failed");

end                                                    
always
             
begin                                                                                                        
@eachvec;                                                                                         
end                                                    
endmodule

5.在Modelsim中仿真分析生成波形图:

解决以下问题:

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

答:

①存在的差异为输入端与输出端,其余基本无差异。

②仿真测试的结果经过一一检验也呈现出以下规则:

  • 当 ABC = 000 时,Y0 = 1,其余输出线为0。
  • 当 ABC = 001 时,Y1 = 1,其余输出线为0。
  • 当 ABC = 010 时,Y2 = 1,其余输出线为0。
  • 当 ABC = 011 时,Y3 = 1,其余输出线为0。
  • 当 ABC = 100 时,Y4 = 1,其余输出线为0。
  • 当 ABC = 101 时,Y5 = 1,其余输出线为0。
  • 当 ABC = 110 时,Y6 = 1,其余输出线为0。
  • 当 ABC = 111 时,Y7 = 1,其余输出线为0。

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

答:

①reg和wire两种数据类型的不同用法:reg 用于表示寄存器类型的变量,用于always模块内被赋值的信号,通常用于存储状态或时序逻辑。另一方面,wire 用于表示连续赋值的变量,用于always模块内未被赋值的信号,通常用于组合逻辑。因为3-8译码器模块的输出信号out是在always块中被赋值的,因此必须定义为reg类型。

②改成wire型是否可以:若如上更改会出现“Multiple drivers”错误,因为wire类型的信号可以有多个驱动器,而reg类型的信号只能有一个驱动器。因此,如果将out定义为wire类型,会出现多个驱动器的情况,从而导致错误。

三、关于全加器的解释以及仿真实验

(一)全加器的定义及其运算法则

1.定义:

全加器(Full Adder)是一种数字电路组件,用于将两个二进制数的每一位和一个进位位相加。全加器与半加器不同,它可以处理三个输入:两个待加的二进制位和来自前一位的进位。全加器的输出包括一个和位(Sum)和一个进位位(Cout)。

2.运算法则:

一个1位全加器的运算法则如下:

1.输入:

  • A(加数的一位)
  • B(被加数的一位)
  • Cin(前一位的进位)

2.输出:

  • Sum(和位):A、B和Cin的异或结果,表示该位的二进制和。
  • Cout(进位输出):A、B和Cin的任意两个或三个的与运算结果,表示该位的进位。

(二)一、四全加器Logsim电路仿真以及Quartus模拟

1.一位全加器

(1)一位全加器logsim电路图:

(2)利用Verilog代码生成RTL电路图:

module FullAdder1Bit (

  input A, B, Cin,

  output Sum, Cout

);

  
  wire X1, X2, X3, X4;

  // 异或门

  assign X1 = A ^ B;

  assign X2 = X1 ^ Cin;

  // 与门

  assign X3 = A & B;

  assign X4 = X1 & Cin;

  // 或门

  assign Cout = X3 | X4;

  assign Sum = X2;


endmodule

2.四位全加器

(1)四位全加器logsim电路图:

(2)利用Verilog代码生成RTL电路图:

module FourBitAdder(
  input [3:0] A,
  input [3:0] B,
  input Cin,
  output [3:0] Sum,
  output Cout
);

  wire [3:0] carry_out_intermediate;
  wire [3:0] sum_intermediate;

  FullAdder1Bit fa0 (.A(A[0]), .B(B[0]), .Cin(Cin), .Sum(sum_intermediate[0]), .Cout(carry_out_intermediate[0]));
  FullAdder1Bit fa1 (.A(A[1]), .B(B[1]), .Cin(carry_out_intermediate[0]), .Sum(sum_intermediate[1]), .Cout(carry_out_intermediate[1]));
  FullAdder1Bit fa2 (.A(A[2]), .B(B[2]), .Cin(carry_out_intermediate[1]), .Sum(sum_intermediate[2]), .Cout(carry_out_intermediate[2]));
  FullAdder1Bit fa3 (.A(A[3]), .B(B[3]), .Cin(carry_out_intermediate[2]), .Sum(sum_intermediate[3]), .Cout(carry_out_intermediate[3]));

  assign Sum = sum_intermediate;
  assign Cout = carry_out_intermediate[3];

endmodule

module FullAdder1Bit(
  input A,
  input B,
  input Cin,
  output Sum,
  output Cout
);

  assign Sum = A ^ B ^ Cin;
  assign Cout = (A & B) | (B & Cin) | (A & Cin);

endmodule

(三)Verilog的1、4位全加器行为级方式重新描述

1.一位全加器:

(一)一位全加器logsim电路图:

(二)利用Verilog代码生成RTL电路图:

module FullAdder_functional_level (

  input A, B, Cin,

  output Sum, Cout

);

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

endmodule

2.四位全加器:

(一)四位全加器logsim电路图:

(二)利用Verilog代码生成RTL电路图:

module FourBitAdder_functional_level(
  input [3:0] A,
  input [3:0] B,
  input Cin,
  output [3:0] Sum,
  output Cout
);

  wire [3:0] carry_out_intermediate;
  wire [3:0] sum_intermediate;

  FullAdder1Bit fa0 (.A(A[0]), .B(B[0]), .Cin(Cin), .Sum(sum_intermediate[0]), .Cout(carry_out_intermediate[0]));
  FullAdder1Bit fa1 (.A(A[1]), .B(B[1]), .Cin(carry_out_intermediate[0]), .Sum(sum_intermediate[1]), .Cout(carry_out_intermediate[1]));
  FullAdder1Bit fa2 (.A(A[2]), .B(B[2]), .Cin(carry_out_intermediate[1]), .Sum(sum_intermediate[2]), .Cout(carry_out_intermediate[2]));
  FullAdder1Bit fa3 (.A(A[3]), .B(B[3]), .Cin(carry_out_intermediate[2]), .Sum(sum_intermediate[3]), .Cout(carry_out_intermediate[3]));

  assign Sum = sum_intermediate;
  assign Cout = carry_out_intermediate[3];

endmodule

module FullAdder1Bit(
  input A,
  input B,
  input Cin,
  output Sum,
  output Cout
);

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

endmodule

(四)Verilog设计8位全加器模块

1.八位全加器介绍:

八位全加器是一种数字电路,用于执行二进制数的加法运算。它由八个一位全加器(Full Adder)组成,每个一位全加器用于处理相应位置上的二进制位相加,同时考虑前一位的进位。在这种结构中,八位全加器可以用于相加两个八位的二进制数。

2.Verilog的八位全加器代码:

module EightBitAdder (
    input wire [7:0] A, // 8位输入A
    input wire [7:0] B, // 8位输入B
    output wire [7:0] Sum, // 8位和输出
    output wire Cout    // 进位输出
);

    // 内部信号
    wire [7:0] Carry;

    // 生成八个一位全加器实例
    OneBitAdder FA0(A[0], B[0], 1'b0, Sum[0], Carry[0]);
    OneBitAdder FA1(A[1], B[1], Carry[0], Sum[1], Carry[1]);
    OneBitAdder FA2(A[2], B[2], Carry[1], Sum[2], Carry[2]);
    OneBitAdder FA3(A[3], B[3], Carry[2], Sum[3], Carry[3]);
    OneBitAdder FA4(A[4], B[4], Carry[3], Sum[4], Carry[4]);
    OneBitAdder FA5(A[5], B[5], Carry[4], Sum[5], Carry[5]);
    OneBitAdder FA6(A[6], B[6], Carry[5], Sum[6], Carry[6]);
    OneBitAdder FA7(A[7], B[7], Carry[6], Sum[7], Cout);

endmodule

module OneBitAdder (
    input wire a,
    input wire b,
    input wire Cin,
    output wire Sum,
    output wire Cout
);

    // 位运算:a、b和进位相加,结果保存在Sum中
    assign Sum = a ^ b ^ Cin;

    // 计算进位
    assign Cout = (a & b) | (b & Cin) | (a & Cin);

endmodule

四、拓展实验

题目:学习教材上的 并行加法器原理(先行进位加法器),参考附件资料,完成一个16位ALU(算术逻辑单元)的电路设计,采用Verilog设计模式,生成RTL电路。

Verilog代码:

module SimpleALU_16bit (
    input wire [15:0] A,
    input wire [15:0] B,
    input wire [3:0]  opcode,
    output reg [15:0] result,
    output reg zero,
    output reg overflow,
    output reg carry
);

always @(A or B or opcode) begin
    case(opcode)
        4'b0000: // Addition
            begin
                result = A + B;
                zero = (result == 16'b0);
                overflow = (A[15] & B[15] & ~result[15]) | (~A[15] & ~B[15] & result[15]);
                carry = (A[15] & B[15]) | (~result[15] & (A[15] | B[15]));
            end
        4'b0001: // Subtraction
            begin
                result = A - B;
                zero = (result == 16'b0);
                overflow = (A[15] & ~B[15] & ~result[15]) | (~A[15] & B[15] & result[15]);
                carry = (A[15] | ~B[15]) & (~result[15] | (A[15] & ~B[15]));
            end
        4'b0010: // AND
            begin
                result = A & B;
                zero = (result == 16'b0);
                overflow = 1'b0;
                carry = 1'b0;
            end
        4'b0011: // OR
            begin
                result = A | B;
                zero = (result == 16'b0);
                overflow = 1'b0;
                carry = 1'b0;
            end
        4'b0100: // XOR
            begin
                result = A ^ B;
                zero = (result == 16'b0);
                overflow = 1'b0;
                carry = 1'b0;
            end
        // Add more operations as needed
        // ...

        default:
            begin
                result = 16'b0;
                zero = 1'b0;
                overflow = 1'b0;
                carry = 1'b0;
            end
    endcase
end

endmodule

仿真电路图:

测试代码:

// Copyright (C) 1991-2013 Altera Corporation
// Your use of Altera Corporation's design tools, logic functions 
// and other software and tools, and its AMPP partner logic 
// functions, and any output files from any of the foregoing 
// (including device programming or simulation files), and any 
// associated documentation or information are expressly subject 
// to the terms and conditions of the Altera Program License 
// Subscription Agreement, Altera MegaCore Function License 
// Agreement, or other applicable license agreement, including, 
// without limitation, that your use is for the sole purpose of 
// programming logic devices manufactured by Altera and sold by 
// Altera or its authorized distributors.  Please refer to the 
// applicable agreement for further details.

// *****************************************************************************
// This file contains a Verilog test bench template that is freely editable to  
// suit user's needs .Comments are provided in each section to help the user    
// fill out necessary details.                                                  
// *****************************************************************************
// Generated on "12/16/2023 22:05:26"
                                                                                
// Verilog Test Bench template for design : SimpleALU_16bit
// 
// Simulation tool : ModelSim (Verilog)
// 

`timescale 1 ps/ 1 ps
module SimpleALU_16bit_vlg_tst();
// constants                                           
// general purpose registers
reg eachvec;
// test vector input registers
reg [15:0] A;
reg [15:0] B;
reg [3:0] opcode;
// wires                                               
wire carry;
wire overflow;
wire [15:0]  result;
wire zero;

// assign statements (if any)                          
SimpleALU_16bit i1 (
// port map - connection between master ports and signals/registers   
	.A(A),
	.B(B),
	.carry(carry),
	.opcode(opcode),
	.overflow(overflow),
	.result(result),
	.zero(zero)
);
initial begin
    $display("Starting 16-bit ALU Testbench");

    // Test Case 1: Addition
    A = 16'h1234;
    B = 16'h5678;
    opcode = 4'b0000; // Addition
    #10; // Wait for some time
    $display("Test Case 1 - Addition: Result = %h, Zero = %b, Carry = %b, Overflow = %b", result, zero, carry, overflow);

    // Test Case 2: Subtraction
    A = 16'hABCD;
    B = 16'h5432;
    opcode = 4'b0001; // Subtraction
    #10; // Wait for some time
    $display("Test Case 2 - Subtraction: Result = %h, Zero = %b, Carry = %b, Overflow = %b", result, zero, carry, overflow);

    // Add more test cases for other operations


  end

endmodule

仿真结果:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值