Verilog编程基础练习

Verilog编程基础练习

一.使用Logsim绘制一个3-8译码器电路图,并列出3-8译码器的逻辑真值表。

在这里插入图片描述

在这里插入图片描述

二.采用Verilog编程(if-else或者case)设计一个3-8译码器,生成RTL原理电路图。
module decoder3_8

 (

    input   wire    in1 ,  

    input   wire    in2 ,   

    input   wire    in3 ,   

    output reg [7:0]    out     

 );

 always@(*)

    case({in1, in2, in3})

        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;

        default: out = 8'b0000_0001;

    endcase

 endmodule

在这里插入图片描述

三.编写一个仿真测试文件,对3-8译码器进行仿真测试,输出测试波形图和Transcript结果。
 timescale 1ns/1ns

 module tb_decoder3_8();

 //reg define

 reg        in1;

 reg        in2;

 reg        in3;

 //wire     define

 wire   [7:0]   out;

 //初始化输入信号

 initial    begin

    in1 <= 1'b0;

    in2 <= 1'b0;

    in3 <= 1'b0;

 end

 //in1:产生输入随机数,模拟输入端 1 的输入情况

 always #10 in1 <= {$random} % 2;

 //in2:产生输入随机数,模拟输入端 2 的输入情况

 always #10 in2 <= {$random} % 2;

 //in3:产生输入随机数,模拟输入端 3 的输入情况

 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_inst----------------

 decoder3_8 decoder3_8_ins

 (

    .in1(in1), //input in1

    .in2(in2), //input in2

    .in3(in3), //input in3

    .out(out) //output [7:0] out

 );

 endmodule

在这里插入图片描述

在这里插入图片描述

两个问题:

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

差异在于具体实现细节,verilog生成的电路图将中间复杂的电路封装成了一个译码模块,隐

去了具体细节,整体更美观、简洁。仿真测试生成的结果与真值表一致。

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

  • reg类型表示寄存器,可以用于存储数据,可以在always块中对reg进行赋值。
  • wire类型表示逻辑连线,默认不能在always块中进行赋值。

在这个3-8译码器中,我们在always块里通过case语句对out进行了多次赋值,所以out必须定义为reg类型。

如果将output reg [7:0] out改成output [7:0] out,则会报以下错误:

Copy codeIllegal output or inout declaration for net "out"  
Net types for actual and formal differ

出现这个错误的原因是,我们尝试在always块中对wire类型的out信号进行了赋值。Verilog语法规则不允许这种情况,必须使用reg类型才可以。

所以出错的根本原因是:wire类型默认不能在always块中进行赋值,它仅表示逻辑或布线连接。要对一个信号进行赋值,必须定义为reg类型的寄存器。

四.用Verilog的门级描述方式写一个“1位全加器”, 生成RTL电路,与Logsim的“1位全加器”进行对比。

verilog1位全加器代码(门级):

module FullAdder_gate_level (

  input A, B, Cin,

  output Sum, Cout

);

  
  wire X1, X2, X3, X4;

  // XOR gates

  assign X1 = A ^ B;

  assign X2 = X1 ^ Cin;

  // AND gates

  assign X3 = A & B;

  assign X4 = X1 & Cin;

  // OR gates

  assign Sum = X2;

  assign Cout = X3 | X4;
  

endmodule


RTL电路:在这里插入图片描述

对比logsim电路:

img

五.采用Verilog模块调用子模块的方式,用4个上面的“1位全加器”级联方式,构成一个“4位全加器”(即串行全加器),生成RTL电路,与Logsim的“4位全加器”电路进行对比。
module FullAdder_functional_level (

  input A, B, Cin,

  output Sum, Cout

);

  
  // Full Adder logic

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

  
endmodule
  

module FourBitAdder_functional_level (

  input [3:0] A, B, Cin,

  output [3:0] Sum,

  output Cout

);


  wire c1, c2, c3;

  FullAdder_functional_level fa1(A[0], B[0], Cin, Sum[0], c1);

  FullAdder_functional_level fa2(A[1], B[1], c1, Sum[1], c2);

  FullAdder_functional_level fa3(A[2], B[2], c2, Sum[2], c3);

  FullAdder_functional_level fa4(A[3], B[3], c3, Sum[3], Cout);

  
endmodule


RTL电路:在这里插入图片描述

对比logsim电路:在这里插入图片描述

六.采用Verilog设计一个8位全加器模块。

verilog代码:

module eight_bit_adder(
    input [7:0] a,
    input [7:0] b,
    input cin,
    output [7:0] sum,
    output cout
);

wire [7:0] c;

full_adder fa0(a[0], b[0], cin, sum[0], c[0]);
full_adder fa1(a[1], b[1], c[0], sum[1], c[1]);
full_adder fa2(a[2], b[2], c[1], sum[2], c[2]);
full_adder fa3(a[3], b[3], c[2], sum[3], c[3]);
full_adder fa4(a[4], b[4], c[3], sum[4], c[4]);
full_adder fa5(a[5], b[5], c[4], sum[5], c[5]);
full_adder fa6(a[6], b[6], c[5], sum[6], c[6]);
full_adder fa7(a[7], b[7], c[6], sum[7], cout);

endmodule

module full_adder(
    input a,
    input b,
    input cin,
    output sum,
    output cout
);

assign sum = a ^ b ^ cin;
assign cout = (a & b) | (a & cin) | (b & cin);

endmodule


RTL电路图:在这里插入图片描述

七.扩展实验:完成一个16位ALU(算术逻辑单元)的电路设计,采用Verilog设计模式,生成RTL电路。

verilog代码:

module ALU_16bit (

  input [15:0] operand_A,

  input [15:0] operand_B,

  input [2:0]  opcode,

  input       cin,

  output reg [15:0] result,

  output      cout,

  output      zero

);

  

  reg [15:0] temp_result;

  

  always @*

    case(opcode)

      3'b000: temp_result = operand_A + operand_B;

      3'b001: temp_result = operand_A - operand_B;

      3'b010: temp_result = operand_A & operand_B;

      3'b011: temp_result = operand_A | operand_B;

      3'b100: temp_result = operand_A ^ operand_B;

      3'b101: temp_result = operand_A + 1;

      3'b110: temp_result = operand_A - 1;

      3'b111: temp_result = ~operand_A;

      default: temp_result = 16'b0;

    endcase

  

  assign cout = (temp_result[15] == 1) ? 1'b1 : 1'b0; // Fix index to 15

  assign zero = (temp_result == 16'b0) ? 1'b1 : 1'b0;

  always @*

    if (cin)

      result = temp_result + 1;

    else

      result = temp_result;


 
endmodule

RTL电路图:在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值