【HDLBits刷题笔记】15 Finding bugs in code

打印 上一主题 下一主题

主题 545|帖子 545|积分 1635

Bugs mux2

原本代码的逻辑是反的,这不是坑人吗。
  1. module top_module (
  2.     input sel,
  3.     input [7:0] a,
  4.     input [7:0] b,
  5.     output [7:0]out  );
  6.     assign out = ({8{sel}} & a) | ({8{~sel}} & b);
  7. endmodule
复制代码
Bugs nand3

五输入的与门现在要实现三输入的与非门,多余的门可以输入1并将输出取反。
  1. module top_module (input a, input b, input c, output out);//
  2.     wire out_n;
  3.     andgate inst1 ( out_n,a, b, c, 1'b1,1'b1 );
  4.     assign out = ~out_n;
  5. endmodule
复制代码
Bugs mux4

bug1:mux0和mux1的位宽没设置,默认是1;
bug2:选通引脚有问题,应该先通过mux[0]判断是ac还是bd,再通过mux[1]进行判断。
  1. module top_module (
  2.     input [1:0] sel,
  3.     input [7:0] a,
  4.     input [7:0] b,
  5.     input [7:0] c,
  6.     input [7:0] d,
  7.     output [7:0] out  ); //
  8.     wire [7:0]mux0, mux1;
  9.     mux2 u_mux0 ( sel[0],    a,    b, mux0 );
  10.     mux2 u_mux1 ( sel[0],    c,    d, mux1 );
  11.     mux2 u_mux2 ( sel[1], mux0, mux1,  out );
  12. endmodule
复制代码
Bugs addsubz

verilog中~是按位取反,!是逻辑取反。
同时需要补充out不为0的情况,否则输出会默认保持,综合出latch。
  1. // synthesis verilog_input_version verilog_2001
  2. module top_module (
  3.     input do_sub,
  4.     input [7:0] a,
  5.     input [7:0] b,
  6.     output reg [7:0] out,
  7.     output reg result_is_zero
  8. );//
  9.     always @(*) begin
  10.         case (do_sub)
  11.           0: out = a+b;
  12.           1: out = a-b;
  13.         endcase
  14.         if (!out)
  15.             result_is_zero = 1;
  16.         else
  17.             result_is_zero = 0;
  18.     end
  19. endmodule
复制代码
Bugs case

这道题比较考验眼力,一个是d要改成h,还有一个是6位改成8位。晕。
还有就是先给两个输出赋默认值,就不会综合出latch了,而且代码也更加简洁。
  1. module top_module (
  2.     input [7:0] code,
  3.     output reg [3:0] out,
  4.     output reg valid );//
  5.      always @(*)
  6.      begin
  7.          out = 0;
  8.          valid = 1'b1;
  9.          case (code)
  10.              8'h45: out = 0;
  11.              8'h16: out = 1;
  12.              8'h1e: out = 2;
  13.              8'h26: out = 3;
  14.              8'h25: out = 4;
  15.              8'h2e: out = 5;
  16.              8'h36: out = 6;
  17.              8'h3d: out = 7;
  18.              8'h3e: out = 8;
  19.              8'h46: out = 9;
  20.              default: valid = 0;
  21.          endcase
  22.      end
  23. endmodule
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万有斥力

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表