HDLBits:在线学习 Verilog (九 · Problem 40 - 44)

news/2024/7/19 15:48:48 标签: 编程语言, js, java, 算法, javascript
js_content">

本系列内容来自于知乎专栏,链接如下:https://zhuanlan.zhihu.com/c_1131528588117385216

本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题,并附上解答和一些作者个人的理解,相信无论是想 7 分钟精通 Verilog,还是对 Verilog 和数电知识查漏补缺的同学,都能从中有所收获。

Problem 40 Combinational for-loop: 255-bit population count

设计电路来计算输入矢量中 ’1‘ 的个数,题目要求建立一个255bit输入的矢量来判断输入中 ’1‘ 的个数。

Hint

像这种重复的工作,我们可以采用for循环来计算。

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

    integer i;

    always @ (*)
        begin
            out = 8'b0000_0000;     //为了后面的计数累加,此处先初始化为0.
            for (i=0; i<255; i++)
                begin
                    if(in[i] == 1'b1)
                        out = out + 1'b1;
                    else
                        out = out + 1'b0;
                end
        end

endmodule

Problem 42 Generate for-loop: 100-bit binary adder 2

通过实例化100个全加器来实现一个100bit的二进制加法器。该加法器有两个100bit的输入和cin,输出为sum与cout。为了鼓励大家使用实例化来完成电路设计,我们同时需要输出每个全加器的cout。故cout[99]标志着全加器的最终进位。

Hint

有好多加法器需要实例化,可采用实例化数组或generate语句来实现。

解析:

相当于例化100个1bit的全加器来实现100bit的带进位的加法器,我在这里偷懒了,首先想到两个always语句之间是并行的,然后就可以仅使用for循环来实现电路设计了。

考虑到for循环中只有cin与cout是变化的,每次计算中cout是本次计算的输出,也是下次计算的输入(cout就是下次计算的cin)。故我们先计算出cout[0] 和 sum[0]。

assign cout[0] = a[0] & b[0] | a[0] & cin | b[0] & cin;
assign sum[0]  = a[0] ^ b[0] ^ cin;

然后开始for-loop

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

    assign cout[0] = a[0] & b[0] | a[0] & cin | b[0] & cin;
    //输出cout[0] == 下一次计算cin[1]
    assign sum[0]  = a[0] ^ b[0] ^ cin;

    integer i;

    always @ (*)
        begin
            for (i=1; i<100; i++)
                begin
                    sum[i]  = a[i] ^ b[i] ^ cout[i-1]; //这里cout[0]相当于cin[1]
                end
        end

    always @ (*)
        begin
            for(i=1; i<100; i++)
                begin
                    cout[i] = a[i] & b[i] | a[i] & cout[i-1] | b[i] & cout[i-1];
                    //cout[1] 等于下一次计算的输入cin[2].
                end
        end


endmodule

当然我的代码和题目要求是不符的,如果大家有好的代码可在评论区上传。

Problem 42 Generate for-loop: 100-digit BCD adder

本题已经提供了一个名为bcd_fadd的BCD一位全加器,他会添加两个BCD码和一个cin,并产生一个cout和sum。

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

我们需要实例化100个bcd_fadd来实现100位的BCD进位加法器。该加法器应包含两个100bit的BCD码(包含在400bit的矢量中)和一个cin, 输出产生sum 和 cout。

Hint

实例化数组和generate语句在这里很有用。

解析:

在本题中我们采用generate语句,什么是generate语句?

生成语句可以动态的生成verilog代码,当对矢量中的多个位进行重复操作时,或者当进行多个模块的实例引用的重复操作时,或者根据参数的定义来确定程序中是否应该包含某段Verilog代码的时候,使用生成语句能大大简化程序的编写过程。

使用关键字generate 与 endgenerate来指定范围。generate语句有generate-for、generate-if、generate-case三种语句,本题中我们使用generate-for语句。

generate-for语句:

(1) 必须有genvar关键字定义for语句的变量。

(2)for语句的内容必须加begin和end(即使就一句)。

(3)for语句必须有个名字。

例:

//创建一个2进制转换器

Module gray2bin
#(parameter SIZE = 8)
(
  input [SIZE-1:0] gray,
  output [SIZE-1:0] bin
)

Genvar gi;  //在generate语句中采用genvar声明
    generate 
  for (gi=0; gi<SIZE; gi=gi+1)
      begin : genbit    //for语句必须有名字
        assign bin[i] = ^gray[SIZE-1:gi];
      end
    endgenerate 
endmodule

//但是看Verilog Pro上写generate-for语句中,generate与endgenerate是可有可无的。

Verilog Generate Configurable RTL Designs - Verilog Pro www.verilogpro.com

解析:

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

    wire [399:0] cout_temp;
    genvar i;

    bcd_fadd inst1_bcd_fadd (
        .a(a[3:0]),
        .b(b[3:0]),
        .cin(cin),
        .cout(cout_temp[0]),
        .sum(sum[3:0])
    );
    //与上题同理,还是先计算cout[0],我声明一个wire型的cout_temp来存放每次计算后cout的值。


    generate
        for(i=4; i<400; i=i+4)
            begin: bcd
                bcd_fadd inst_bcd_fadd(
                    .a(a[i+3:i]),
                    .b(b[i+3:i]),
                    .cin(cout_temp[i-4]), //上次计算输出的cout
                    .cout(cout_temp[i]), //本次计算输出的cout,在下次计算中变为cin
                    .sum(sum[i+3:i])
                );
            end
    endgenerate 

    assign cout = cout_temp[396];

endmodule

Problem 43 Wire

实现如下电路:

解析:一个简单的wire型输出,直接assign即可

module top_module (
    input in,
    output out);
 assign out = in;
endmodule

Problem 44 GND

实现如下电路:

解析:接地

module top_module (
    output out);
    assign out = 1'b0;
endmodule

【打个广告】

各位对数字IC和FPGA设计感兴趣的盆友们,我们搭建了一个QQ交流群。经过这一段时间的发展,群聊中已经有将近900个志同道合的同学加入。QQ群里已经整理了许多的资料,其中包含有视频,资料,教程,芯片手册,软件安装包,虚拟机环境。覆盖了从FPGA开发,数字前端,电路设计等等一系列的内容。QQ群号为810689010 ,进群暗号:公众号。欢迎进群交流!

为了能让这个群长期发展下去,用于支付扩建群聊所需的会员费,无奈将这个群设置成付费入群,仅需一元即可,多谢大家支持。

QQ群二维码

期待与您的添加!

‧  END  

长按识别图中二维码关注

欢迎关注微信公众号【数字积木】,更精彩的内容等着你!


http://www.niftyadmin.cn/n/791380.html

相关文章

Android 三大图片缓存原理、特性对比

这是我在 MDCC 上分享的内容(略微改动)&#xff0c;也是源码解析第一期发布时介绍的源码解析后续会慢慢做的事。 从总体设计和原理上对几个图片缓存进行对比&#xff0c;没用到他们的朋友也可以了解他们在某些特性上的实现。 上篇关于选择开源项目的好处及如何选择开源项目可见…

HDLBits:在线学习 Verilog (十 · Problem 45 - 49)

本系列内容来自于知乎专栏&#xff0c;链接如下&#xff1a;https://zhuanlan.zhihu.com/c_1131528588117385216本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题&#xff0c;并附上解答和一些作者个人的理解&#xff0c;相信无论是想 7 分钟精通 Verilog…

Glide 使用 +圆形图+圆角图

Glide3.0新增 1.GIF Animated GIF decoding - Just use the same Glide.with(...).load(...) call and if the image you load is an animated GIF, Glide will load and display a custom animated drawable containing the gif. For more control you can use Glide.with(c…

HDLBits:在线学习 Verilog (十一 · Problem 50 - 54)

本系列内容来自于知乎专栏&#xff0c;链接如下&#xff1a;https://zhuanlan.zhihu.com/c_1131528588117385216本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题&#xff0c;并附上解答和一些作者个人的理解&#xff0c;相信无论是想 7 分钟精通 Verilog…

能否在非UI线程更新界面呢?

简要总结爱哥文章&#xff1a;http://blog.csdn.net/aigestudio/article/details/43449123 结论&#xff1a; 可以&#xff0c;阻碍非UI线程更新UI的是ViewRootIml类的checkThread().所以在ViewRootIml未创建前是可以更新UI的。ViewRootIml是在addView中Create哦。 handleRes…

HDLBits:在线学习 Verilog (十二 · Problem 55 - 59)

本系列内容来自于知乎专栏&#xff0c;链接如下&#xff1a;https://zhuanlan.zhihu.com/c_1131528588117385216本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题&#xff0c;并附上解答和一些作者个人的理解&#xff0c;相信无论是想 7 分钟精通 Verilog…

HDLBits:在线学习 Verilog (十三 · Problem 60-64)

本系列内容来自于知乎专栏&#xff0c;链接如下&#xff1a;https://zhuanlan.zhihu.com/c_1131528588117385216本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题&#xff0c;并附上解答和一些作者个人的理解&#xff0c;相信无论是想 7 分钟精通 Verilog…

HDLBits:在线学习 Verilog (十四 · Problem 65-69)

本系列内容来自于知乎专栏&#xff0c;链接如下&#xff1a;https://zhuanlan.zhihu.com/c_1131528588117385216本系列文章将和读者一起巡礼数字逻辑在线学习网站 HDLBits 的教程与习题&#xff0c;并附上解答和一些作者个人的理解&#xff0c;相信无论是想 7 分钟精通 Verilog…