• 9.12数字逻辑


    rst

    1. `timescale 1ns/1ns
    2. module main_mod(
    3. input clk,
    4. input rst_n,
    5. input [7:0]a,
    6. input [7:0]b,
    7. input [7:0]c,
    8. output [7:0]d
    9. );
    10. wire [7:0]m,n;
    11. sub_mod mod_ab(
    12. .clk(clk),
    13. .rst_n(rst_n),
    14. .data_a(a),
    15. .data_b(b),
    16. .data_c(m)
    17. );
    18. sub_mod mod_bc(
    19. .clk(clk),
    20. .rst_n(rst_n),
    21. .data_a(b),
    22. .data_b(c),
    23. .data_c(n)
    24. );
    25. sub_mod mod_mn(
    26. .clk(clk),
    27. .rst_n(rst),
    28. .data_a(m),
    29. .data_b(n),
    30. .data_c(d)
    31. );
    32. endmodule
    33. module sub_mod(
    34. input clk,
    35. input rst_n,
    36. input[7:0]data_a,
    37. input[7:0]data_b,
    38. output reg[7:0]data_c
    39. );
    40. always@(posedge clk or negedge rst_n)begin
    41. if(!rst_n)begin
    42. data_c<=0;
    43. end
    44. else if(data_a>data_b)begin
    45. data_c<=data_b;
    46. end
    47. else begin
    48. data_c<=data_a;
    49. end
    50. end
    51. endmodule

    四位数值比较器

    a与b都只有01

    如果a要大于b,那么a为1,b为0,y2=a&!b

    如果b要大于a,那么b为1,a为0,y0=!a&b

    如果相等,那么y2,y0都不成立,y1=!(y2|y0)

    写一个每位的比较器模块,然后在主模块中不断循环调用 

    1. `timescale 1ns/1ns
    2. module comparator_4(
    3. input [3:0] A ,
    4. input [3:0] B ,
    5. output wire Y2 , //A>B
    6. output wire Y1 , //A=B
    7. output wire Y0 //A<B
    8. );
    9. wire y2temp[0:3];
    10. wire y0temp[0:3];
    11. wire y1temp[0:3];
    12. genvar i;
    13. generate
    14. for(i=0;i<=3;i=i+1)
    15. begin:loop
    16. compare_1 comparei(
    17. .a(A[i]),
    18. .b(B[i]),
    19. .y1(y1temp[i]),
    20. .y2(y2temp[i]),
    21. .y0(y0temp[i])
    22. );
    23. end
    24. endgenerate
    25. assign Y2=y2temp[3]|(y1temp[3]&y2temp[2])|(y1temp[3]&y1temp[2]&y2temp[1])|(y1temp[3]&y1temp[2]&y1temp[1]&y2temp[0]);
    26. assign Y0=y0temp[3]|(y1temp[3]&y0temp[2])|(y1temp[3]&y1temp[2]&y0temp[1])|(y1temp[3]&y1temp[2]&y1temp[1]&y0temp[0]);
    27. assign Y1=y1temp[3]&y1temp[2]&y1temp[1]&y1temp[0];
    28. endmodule
    29. module compare_1(
    30. input a,
    31. input b,
    32. output y1,
    33. output y2,
    34. output y0
    35. );
    36. assign y0=(!a)&b;
    37. assign y2=(!b)&a;
    38. assign y1=!(y0|y2);
    39. endmodule

    声明自模块,带;,带类名

    与,或,非,异或

    a^b保证a与b不同,再与上a,表示a为1,b与a不同,b为0,则a>b

  • 相关阅读:
    opencv入门
    12 list的使用
    基于stm32单片机的台历日历计时器万年历Proteus仿真
    verilog中$monitor 的用法
    通过Handle(子类)::DownCast(父类)实现Geom2d_TrimmedCurve曲线段找源曲线段
    【wine】WINEDEBUG 分析mame模拟器不能加载roms下面的游戏 可以调整参数,快速启动其中一个游戏kof98
    Css定位
    Penpad获Gate Labs以及Scroll联创Sandy的投资
    《三体2:黑暗森林》读后感
    【python】基础语法
  • 原文地址:https://blog.csdn.net/m0_73553411/article/details/132824897