• HDLBits: 在线学习 SystemVerilog(四)-Problem 15-18


    HDLBits: 在线学习 SystemVerilog(四)-Problem 15-18

    34fadbc9f26c9b86f9dd1cd9f4dc601f.jpeg

    HDLBits 是一组小型电路设计习题集,使用 Verilog/SystemVerilog 硬件描述语言 (HDL) 练习数字硬件设计~

    网址如下:

    https://hdlbits.01xz.net/

    关于HDLBits的Verilog实现可以查看下面专栏:

    https://www.zhihu.com/column/c_1131528588117385216

    缩略词索引:

    • SV:SystemVerilog

    Problem 15-Vector3

    题目说明

    模块 32 位输入向量如下所示,按照上下对应关系,输出为下方的 4 个 8-bits 向量。

    47314ffe54833d9c4ebe69ab83ab4dc4.png图片来自 HDLBits

    这个题目的核心就是上面的图片,将上面的输入向量映射到下面向量。

    模块端口声明

    1. module top_module (
    2.     input [4:0] a, b, c, d, e, f,
    3.     output [7:0] w, x, y, z );

    题目解析

    这个题目重点是向量拼接,拼接操作符的基本语法使用 { } 将较小的向量括起来,每个 { } 内的向量使用逗号作为间隔。

    1. {3'b111, 3'b000} => 6'b111000
    2. {1'b1, 1'b0, 3'b101} => 5'b10101
    3. {4'ha, 4'd10} => 8'b10101010     // 4'ha and 4'd10 are both 4'b1010 in binary

    拼接运算符中的向量务必需要标注位宽,不然综合器怎么能知道你的结果需要多宽的位宽。因此 { 1,2,3 } 这样的操作是非法的,并会产生一个 Error:unsized constants are not allowed in concatenations.

    1. module top_module (
    2.     input logic [4:0] a, b, c, d, e, f,
    3.     output logic [7:0] w, x, y, z 
    4.    );//
    5.     assign w = {a,b[4:2]} ;
    6.     assign x = {b[1:0],c,d[4]} ;
    7.     assign y = {d[3:0],e[4:1]} ;
    8.     assign z = {e[0],f,2'b11} ;
    9. endmodule
    182ede63cf86ab3d6520c1b47640847a.png

    点击Submit,等待一会就能看到下图结果:

    f0920b36f5e56ee6c615a222985774f2.png

    注意图中的Ref是参考波形,Yours是你的代码生成的波形,网站会对比这两个波形,一旦这两者不匹配,仿真结果会变红(后面会展示)。

    这一题就结束了。

    Problem 16-Vectorr

    题目说明

    给定一个 8bit 输入向量,将其反向输出。

    模块端口声明

    1. module top_module( 
    2.     input [7:0] in,
    3.     output [7:0] out
    4. );

    题目解析

    这道题难度不大但是不要想着使用assign out[7:0] = in[0:7];解决问题,因为在Verilog中这个语句不起作用,因为Verilog使用向量时的位序应与定义时保持一致。

    简单解决就是将输入按照一个一个bit分开,然后重新组合即可。

    但是如果向量是1024位呢?大家可以思考,后续还有类似问题,再使用其他方式解决,下面有参考示例。

    简单解答

    1. module top_module( 
    2.     input  logic [7:0] in,
    3.     output logic [7:0] out
    4. );
    5.     assign out = {in[0],in[1],in[2],in[3],in[4],in[5],in[6],in[7]} ;
    6.     
    7. endmodule

    其他方式解决

    1. module top_module( 
    2.     input  logic [7:0] in,
    3.     output logic [7:0] out
    4. );
    5.     parameter count = 'd8; 
    6.     var integer i;
    7.     always_comb begin
    8.         for(i = 0; i < count; i = i + 1)begin
    9.             out[i] = in[count - i - 1];
    10.         end
    11.     end
    12.     
    13. endmodule
    65b93b632988cae21a561141429da72c.png adca83be98acb5eb8c6ad6ade286ad99.png

    点击Submit,等待一会就能看到下图结果:

    23db9b524af3c1895bbfd81f8ef87956.png

    注意图中的Ref是参考波形,Yours是你的代码生成的波形,网站会对比这两个波形,一旦这两者不匹配,仿真结果会变红(后面会展示)。

    这一题就结束了。

    Problem 17-Vector4

    题目说明

    将一个 8bit 有符号数扩展为 32bit 数。

    模块端口声明

    1. module top_module (
    2.     input [7:0] in,
    3.     output [31:0] out );

    题目解析

    本题考查的是向量的复制和拼接语法:{ 重复次数 { 向量 } }。

    重复次数必须是一个常量,而且请特别注意重复操作符有两对 { }.外层的 {} 不能少。

    如:

    1. {5{1'b1}}           // 5'b11111 (or 5'd31 or 5'h1f)
    2. {2{a,b,c}}          // The same as {a,b,c,a,b,c}
    3. {3'd5, {2{3'd6}}}   // 9'b101_110_110. It's a concatenation of 101 with
    4.                     // the second vector, which is two copies of 3'b110.

    还需要注意有符号和无符号的复制:

    重复操作符的应用场景之一是在有符号数的扩展。有符号数的扩展是将符号位填充待扩展的比特。比如要将 4bit 的 4'b0101 有符号数扩展为 8bit ,0 是符号位,那么扩展之后为 8'b0000 0101.

    1. module top_module (
    2.     input  logic [7:0] in,
    3.     output logic [31:0] out 
    4.   );//
    5.     
    6.     assign out = {{24{in[7]}},in};
    7. endmodule
    27522af403a2a3b923859392e6288b61.png

    点击Submit,等待一会就能看到下图结果:

    512a3b0a60824e244808ef324265223f.png

    注意图中是没有波形的~

    这一题就结束了。

    Problem 18-Vector5

    题目说明

    给定五个 1 位信号(a、b、c、d 和 e),计算 25 位输出向量中的所有 25 个成对的逻辑比较。如果被比较的两位相等,则输出应为 1。

    b546dd08a88dfaf3a2ab14c4f3811d9d.png图片来自 HDLBits

    问题的核心就是上面的图片,相关操作如下:

    1. out[24] = ~a ^ a;   // a == a, so out[24] is always 1.
    2. out[23] = ~a ^ b;
    3. out[22] = ~a ^ c;
    4. ...
    5. out[ 1] = ~e ^ d;
    6. out[ 0] = ~e ^ e;

    模块端口声明

    1. module top_module (
    2.     input a, b, c, d, e,
    3.     output [24:0] out );

    题目解析

    这个题目还是上一题的延续,属于将几个知识点串联起来,向量复制扩展,XNOR操作,前面知识掌握了这个题目就不难了。

    1. module top_module (
    2.     input a, b, c, d, e,
    3.     output [24:0] out );//
    4.  
    5.     // The output is XNOR of two vectors created by 
    6.     // concatenating and replicating the five inputs.
    7.     // assign out = ~{ ... } ^ { ... };
    8.     assign out = ~{{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}} ^ {5{a,b,c,d,e}};
    9.     
    10.     
    11. endmodule
    041ff66f09448aa9a1dc844a9992114c.png

    点击Submit,等待一会就能看到下图结果:

    f62ea21e3dcbaf35d7564c3832d30356.png

    注意图中无波形~

    这一题就结束了。

    总结

    今天的几道题就结束了,整体难度不大,后面的题目难度会越来越大~

    最后我这边做题的代码也是个人理解使用,有错误欢迎大家批评指正,祝大家学习愉快~

    代码链接:

    https://github.com/suisuisi/SystemVerilog/tree/main/SystemVerilogHDLBits

    往期推荐

    HDLBits: 在线学习 SystemVerilog(三)-Problem 10-14

    HDLBits: 在线学习 SystemVerilog(二)-Problem 7-9

    SystemVerilog-(按)位运算符

    HDLBits: 在线学习 SystemVerilog(一)-Problem 2-6

    HDLBits: 在线学习 SystemVerilog(零)-在线“巡礼” HDLBits

  • 相关阅读:
    Gromacs分析处理-模拟前后蛋白结构差异对比图的制作
    NetCore实战:基于html生成pdf文件案例讲解
    【云原生】K8S二进制搭建一
    跟着蚂蚁高级专家学架构:Netty+RabbitMQ+Docker+虚拟机并发编程
    漏洞复现----37、Apache Unomi 远程代码执行漏洞 (CVE-2020-13942)
    分享一下微信优惠券怎么制作
    齐岳|七甲川染料CY7/CY7.5标记磁性二硫化钨WS2/二氧化锰MnO2/二氧化钛TIO2纳米粒
    SpringBoot集成Kafka+Kafka优化问题
    Go HTTP 调用(上)
    Java多线程(三)
  • 原文地址:https://blog.csdn.net/Pieces_thinking/article/details/126635268