• 「Verilog学习笔记」移位运算与乘法


    专栏前言

    本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

     

    分析 

    1、在硬件中进行乘除法运算是比较消耗资源的一种方法,想要在不影响延迟并尽量减少资源消耗,必须从硬件的特点上进行设计。根据寄存器的原理,由于是二进制,所以进位和退位为x2或者/2,同样除7可以使用进位3然后减去本身的做法,这样就将乘除法运算转化为位运算,这是一种比较简单的整数运算处理。

    2、 需要给出一个计数器的状态机,注意d输入不是随时有效的,只有在cnt计数为0的那个时钟沿,d输入有效,因此需要设计一个寄存器din,在cnt为0时候锁存d的值

    1. `timescale 1ns/1ns
    2. module multi_sel(
    3. input [7:0]d ,
    4. input clk,
    5. input rst,
    6. output reg input_grant,
    7. output reg [10:0]out
    8. );
    9. //*************code***********//
    10. reg [1:0] cnt ;
    11. reg [7:0] din ;
    12. always @ (posedge clk or negedge rst) begin
    13. if (~rst) begin
    14. cnt <= 0 ;
    15. out <= 0 ;
    16. input_grant <= 0 ;
    17. din <= 0 ;
    18. end
    19. else begin
    20. cnt <= (cnt + 1) % 4 ; // cnt <= cnt + 1 由于是2位的寄存器 溢出后自动清0 两种写法皆可
    21. case (cnt)
    22. 0 : begin
    23. din <= d ;
    24. input_grant <= 1 ;
    25. out <= d ;
    26. end
    27. 1 : begin
    28. input_grant <= 0 ;
    29. out <= (din << 2) - din ;
    30. end
    31. 2 : begin
    32. input_grant <= 0 ;
    33. out <= (din << 3) - din ;
    34. end
    35. 3 : begin
    36. input_grant <= 0 ;
    37. out <= din << 3 ;
    38. end
    39. endcase
    40. end
    41. end
    42. //*************code***********//
    43. endmodule

    Testbench

    1. `timescale 1ns/1ns
    2. module testbench();
    3. reg clk = 0 ;
    4. reg rst = 0 ;
    5. reg [7:0] d ;
    6. wire [10:0] out ;
    7. wire input_grant ;
    8. always #5 clk = ~clk ;
    9. // Create clock with period=10
    10. // A testbench
    11. initial begin
    12. # 10 ;
    13. rst = 1 ;
    14. # 200 ;
    15. end
    16. initial begin
    17. d = 143 ;
    18. # 40 d = 7 ;
    19. # 50 d = 6 ;
    20. # 10 d = 128 ;
    21. # 10 d = 129 ;
    22. # 60 $finish ;
    23. end
    24. multi_sel u1(
    25. .clk(clk),
    26. .d(d),
    27. .rst(rst),
    28. .out(out),
    29. .input_grant(input_grant)
    30. );
    31. //end
    32. initial begin
    33. $dumpfile("out.vcd");
    34. // This will dump all signal, which may not be useful
    35. //$dumpvars;
    36. // dumping only this module
    37. //$dumpvars(1, testbench);
    38. // dumping only these variable
    39. // the first number (level) is actually useless
    40. $dumpvars(0, testbench);
    41. end
    42. endmodule
  • 相关阅读:
    解决跨域问题
    【Windows】安装win10虚拟机
    lt基站学习总结
    【动态规划刷题 16】最长等差数列 (有难度) && 等差数列划分 II - 子序列
    Python进行数据可视化,探索和发现数据中的模式和趋势。
    让充电器秒供多个快充口,乐得瑞推出1拖2功率分配快充线方案
    BCC源码内容概览(5)
    免费版Photoshop2024智能人像磨皮插件
    18-1、k8s 对外服务之ingress
    Ubuntu上通过源码方式安装Redis
  • 原文地址:https://blog.csdn.net/m0_54689021/article/details/134228210