HDLBits练习汇总-05-Verilog语言--拓展部分

简介: HDLBits练习汇总-05-Verilog语言--拓展部分

三目运算符


Verilog 有一个三元条件运算符 ( ? : ) 很像 C:

(condition ? if_true : if_false)

这可用于根据一行上的条件(多路复用器)选择两个值之一,而无需在组合 always 块中使用 if-then。

Examples:

(0 ? 3 : 5)     // This is 5 because the condition is false.
(sel ? b : a)   // A 2-to-1 multiplexer between a and b selected by sel.
always @(posedge clk)         // A T-flip-flop.
  q <= toggle ? ~q : q;
always @(*)                   // State transition logic for a one-input FSM
  case (state)
    A: next = w ? B : A;
    B: next = w ? A : B;
  endcase
assign out = ena ? q : 1'bz;  // A tri-state buffer
((sel[1:0] == 2'h0) ? a :     // A 3-to-1 mux
 (sel[1:0] == 2'h1) ? b :
                      c )

练习


给定四个无符号数,找出最小值。无符号数可以与标准比较运算符 (a < b) 进行比较。使用条件运算符创建两路最小电路,然后组合其中的一些来创建 4 路最小电路。可能需要一些用于中间结果的线向量。

Module Declaration

module top_module (
    input [7:0] a, b, c, d,
    output [7:0] min);

答案


module top_module (
    input [7:0] a, b, c, d,
    output [7:0] min);//
    wire [7:0] out_t0;
    wire [7:0] out_t1;
    assign out_t0 = (a<b)?a:b; 
    assign out_t1 = (c<d)?c:d;
    assign min = (out_t0<out_t1) ? out_t0:out_t1;
endmodule

归约运算符


已经熟悉两个值之间的按位运算,例如a & b或a ^ b。有时,您想创建一个对一个向量的所有位进行操作的宽门,例如(a[0] & a[1] & a[2] & a[3] … ),如果向量很长。

的减少运营商可以做AND,OR,以及向量的比特的XOR,产生输出的一个比特:

& a[3:0]     // AND: a[3]&a[2]&a[1]&a[0]. Equivalent to (a[3:0] == 4'hf)
| b[3:0]     // OR:  b[3]|b[2]|b[1]|b[0]. Equivalent to (b[3:0] != 4'h0)
^ c[2:0]     // XOR: c[2]^c[1]^c[0]

这些是只有一个操作数的一元操作符(类似于NOT操作符!和~ )。你也可以将这些门的输出倒转来创建NAND、NOR和XNOR门,例如(~& d[7:0])。

练习


当通过不完善的信道传输数据时,奇偶校验通常用作检测错误的简单方法。创建一个电路来计算 8 位字节的奇偶校验位(这将向字节添加第 9 位)。我们将使用“偶数”奇偶校验,其中奇偶校验位只是所有 8 个数据位的异或。

Module Declaration

module top_module (
    input [7:0] in,
    output parity);

答案


module top_module (
    input [7:0] in,
    output parity); 
    assign parity = ^ in[7:0];
endmodule

归约练习


问题


在 [99:0] 中构建一个具有 100 个输入的组合电路。

有3个输出:

  • out_and:100 输入与门的输出。
  • out_or:100 输入或门的输出。
  • out_xor:100 输入异或门的输出。

Module Declaration

module top_module( 
    input [99:0] in,
    output out_and,
    output out_or,
    output out_xor 
);

答案


module top_module( 
    input [99:0] in,
    output out_and,
    output out_or,
    output out_xor 
);
  assign out_and =  &in[99:0];
    assign out_or =  |in[99:0];
    assign out_xor =  ^in[99:0];
endmodule

反转


问题


给定一个 100 位的输入向量 [99:0],反转其位顺序。

Module Declaration

module top_module( 
    input [99:0] in,
    output [99:0] out
);

答案


module top_module( 
    input [99:0] in,
    output [99:0] out
);
    genvar i ;
    generate
    for (i = 0 ; i <= 99 ; i = i+1)
  begin : loop0
        assign out[i]=in[99-i];
    end
  endgenerate
endmodule

人口计数电路


问题


“人口计数”电路计算输入向量中“1”的数量。为 255 位输入向量构建人口计数电路。

Module Declaration

module top_module( 
    input [254:0] in,
    output [7:0] out );

答案


module top_module( 
    input [254:0] in,
    output [7:0] out );
    int i;
    always @ (*)begin
      out = 8'b0000_0000;   
        for (i=0; i<=254; i++)begin
            if(in[i] == 1'b1)
                out = out + 1'b1;
            else
                out = out + 1'b0;
        end
    end
endmodule

100位加法器


问题


通过实例化 100 个全加器来创建一个 100 位二进制纹波进位加法器。加法器将两个 100 位数字和一个进位相加,以产生 100 位总和并进位。为了鼓励您实际实例化全加器,还要输出纹波进位加法器中每个全加器的进位。cout[99] 是最后一个全加器的最后一个进位,也是你经常看到的进位。

Module Declaration

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

答案


module top_module( 
    input [99:0] a, b,
    input cin,
    output [99:0] cout,
    output [99:0] sum );
  genvar i ;
    generate
    for (i = 0 ; i <= 99 ; i = i+1)
  begin : loop0
        if(i==0)begin
           assign {cout[i],sum[i]} = a[i]+b[i]+cin;
        end
        else begin
           assign {cout[i],sum[i]} = a[i]+b[i]+cout[i-1];
        end
    end
  endgenerate
endmodule

100位BCD加法器


问题


为您提供了一个名为bcd_fadd的 BCD 一位加法器,它将两个 BCD 数字和进位相加,并产生一个总和和进位。

module bcd_fadd {
    input [3:0] a,
    input [3:0] b,
    input     cin,
    output   cout,
    output [3:0] sum );

实例化 100 个bcd_fadd副本以创建 100 位 BCD 纹波进位加法器。您的加法器应将两个 100 位 BCD 数(打包成 400 位向量)和一个进位相加,以产生 100 位总和并执行。

Module Declaration
module top_module( 
    input [399:0] a, b,
    input cin,
    output cout,
    output [399:0] sum );

答案


module top_module( 
    input [399:0] a, b,
    input cin,
    output cout,
    output [399:0] sum );
    wire [99:0] cout_in;
  genvar i ;
    generate
    for (i = 0 ; i <= 99 ; i = i+1)
  begin : loop0
        if(i==0)begin
            bcd_fadd u_bcd_fadd(
                .a   (a[4 * i + 3: 4 * i]   ),
                .b   (b[4 * i + 3: 4 * i]   ),
                .cin (cin ),
                .cout(cout_in[i]),
                .sum (sum[4 * i + 3: 4 * i])
            );
        end
        else begin
           bcd_fadd ui_bcd_fadd(
                .a   (a[4 * i + 3: 4 * i]   ),
                .b   (b[4 * i + 3: 4 * i]   ),
                .cin (cout_in[i-1] ),
                .cout(cout_in[i]),
                .sum (sum[4 * i + 3: 4 * i])
            );
        end
    end
  endgenerate
    assign cout = cout_in[99];
endmodule
目录
相关文章
|
9月前
|
算法 芯片
快速入门数字芯片设计,UCSD ECE111(十二)Testbench和VHDL(一)
快速入门数字芯片设计,UCSD ECE111(十二)Testbench和VHDL
71 0
|
9月前
|
算法 芯片
快速入门数字芯片设计,UCSD ECE111(十二)Testbench和VHDL(三)
快速入门数字芯片设计,UCSD ECE111(十二)Testbench和VHDL(三)
53 0
|
9月前
|
C语言 芯片
快速入门数字芯片设计,UCSD ECE111(十二)Testbench和VHDL(二)
快速入门数字芯片设计,UCSD ECE111(十二)Testbench和VHDL(二)
59 0
|
10月前
【乌拉喵.教程】进一步学习编写TestBench(VHDL语言),quartus与modelsim时序仿真
【乌拉喵.教程】进一步学习编写TestBench(VHDL语言),quartus与modelsim时序仿真
109 0
【乌拉喵.教程】进一步学习编写TestBench(VHDL语言),quartus与modelsim时序仿真
|
存储 程序员 开发工具
第三章 硬件描述语言verilog(一)
第三章 硬件描述语言verilog(一)
288 0
第三章 硬件描述语言verilog(一)
|
芯片
HDLBits练习汇总-01-Verilog语言--基础部分
HDLBits练习汇总-01-Verilog语言--基础部分
126 0
HDLBits练习汇总-01-Verilog语言--基础部分
HDLBits练习汇总-04-Verilog语言--程序部分(二)
HDLBits练习汇总-04-Verilog语言--程序部分
96 0
HDLBits练习汇总-04-Verilog语言--程序部分(二)
HDLBits练习汇总-04-Verilog语言--程序部分(一)
HDLBits练习汇总-04-Verilog语言--程序部分
116 0
HDLBits练习汇总-04-Verilog语言--程序部分(一)
|
编译器
HDLBits练习汇总-03-Verilog语言--模块层次结构(一)
HDLBits练习汇总-03-Verilog语言--模块层次结构
136 0
HDLBits练习汇总-03-Verilog语言--模块层次结构(一)
HDLBits练习汇总-03-Verilog语言--模块层次结构(二)
HDLBits练习汇总-03-Verilog语言--模块层次结构
294 0
HDLBits练习汇总-03-Verilog语言--模块层次结构(二)