上一篇笔记指路👉

这一节主要是找bug,相对比较轻松。(终于不用自己写代码了( •̀ ω •́ )y)
题目:This 8-bit wide 2-to-1 multiplexer doesn't work. Fix the bug(s).
白话:这个8位宽的二选一数据选择器无法实现功能,修改bug。
原代码以及波形图:
module top_module (
input sel,
input [7:0] a,
input [7:0] b,
output out );
assign out = (~sel & a) | (sel & b);
endmodule

from hdlbits
bug分析:
这里的input a,b都是8位的,而out只有1位,所以会导致a,b的高位被丢掉,应把out也改为8位;
从波形图分析可知,当sel=1的时候,out=a;当sel=0时,out=b。
答案不唯一:
module top_module (
input sel,
input [7:0] a,
input [7:0] b,
output [7:0] out );
assign out = (sel)?a:b;
endmodule
题目:
This three-input NAND gate doesn't work. Fix the bug(s).
You must use the provided 5-input AND gate:
module andgate ( output out, input a, input b, input c, input d, input e );
白话:修改三输入与非门的bug,并给一个5输入与门的子模块。
原代码:
module top_module (input a, input b, input c, output out);//
andgate inst1 ( a, b, c, out );
endmodule bug分析:
五输入与门子模块没有调用完。五输入与门的表达式是:a&b&c&d&e,如果只例化a,b,c,那么d,e就悬空,这可能就会导致错误的结果;
题目要求的是三输入与非门,而子模块只有与门,所以还要自己添加一个非门。
答案不唯一:
module top_module (input a, input b, input c, output out);//
wire out1;
andgate inst1 (.a(a),
.b(b),
.c(c),
.out(out1),
.d(1'd1),
.e(1'd1)
);
assign out=~out1;
endmodule
题目:
This 4-to-1 multiplexer doesn't work. Fix the bug(s).
You are provided with a bug-free 2-to-1 multiplexer:
module mux2 ( input sel, input [7:0] a, input [7:0] b, output [7:0] out );
白话:修改四选一数据选择器的bug。已提供两个无bug的二选一数据选择器子模块mux2。
原代码:
module top_module (
input [1:0] sel,
input [7:0] a,
input [7:0] b,
input [7:0] c,
input [7:0] d,
output [7:0] out ); //
wire mux0, mux1;
mux2 mux0 ( sel[0], a, b, mux0 );
mux2 mux1 ( sel[1], c, d, mux1 );
mux2 mux2 ( sel[1], mux0, mux1, out );
endmodule
bug分析:
wire变量mux0, mux1与例化的名字重复,且只有1位。应该改为别的名字,且位数应该变为8位。例如:wire [7:0] out_mux0, out_mux1;
用二选一数据选择器构成四选一数据选择器,那么需要3个二选一数据选择器。低位2个,控制信号为sel[0],高位1个,控制信号为sel[1]。如下图所示:

图片来自https://blog.csdn.net/qq_46545864/article/details/110913683
答案不唯一:
module top_module (
input [1:0] sel,
input [7:0] a,
input [7:0] b,
input [7:0] c,
input [7:0] d,
output [7:0] out ); //
wire [7:0] out_mux0, out_mux1;
mux2 mux0 ( sel[0], a, b, out_mux0 );
mux2 mux1 ( sel[0], c, d, out_mux1 );
mux2 mux2 ( sel[1], out_mux0, out_mux1, out );
endmodule
题目:The following adder-subtractor with zero flag doesn't work. Fix the bug(s).
白话:修复带有0标志位的加法器—减法器(adder-subtractor)。
原代码及仿真图:
// synthesis verilog_input_version verilog_2001
module top_module (
input do_sub,
input [7:0] a,
input [7:0] b,
output reg [7:0] out,
output reg result_is_zero
);//
always @(*) begin
case (do_sub)
0: out = a+b;
1: out = a-b;
endcase
if (~out)
result_is_zero = 1;
end
endmodule

from hdlbits
bug分析:从仿真图可以很明显的看出,case里面的加法减法是没有问题的,问题出在标志位。当out=0的时候,标志位才变1,很容易修改。
答案不唯一:
// synthesis verilog_input_version verilog_2001
module top_module (
input do_sub,
input [7:0] a,
input [7:0] b,
output reg [7:0] out,
output reg result_is_zero
);//
always @(*) begin
case (do_sub)
0: out = a+b;
1: out = a-b;
endcase
if (out==0)
result_is_zero = 1;
else
result_is_zero =0;
end
endmodule
题目:
This combinational circuit is supposed to recognize 8-bit keyboard scancodes for keys 0 through 9. It should indicate whether one of the 10 cases were recognized (valid), and if so, which key was detected. Fix the bug(s).
白话:这个电路是用于识别键盘输入的0~9,每个数字是8位。case里面的每一个状态对应一个数字,如果识别到了数字,valid=1。修复bug。
原代码:
module top_module (
input [7:0] code,
output reg [3:0] out,
output reg valid=1 );//
always @(*)
case (code)
8'h45: out = 0;
8'h16: out = 1;
8'h1e: out = 2;
8'd26: out = 3;
8'h25: out = 4;
8'h2e: out = 5;
8'h36: out = 6;
8'h3d: out = 7;
8'h3e: out = 8;
6'h46: out = 9;
default: valid = 0;
endcase
endmodule
bug分析:
case里面数的进制以及位宽不统一。out=3的一行是8'd26,out=9的一行是6'h46。应当和其他数据类型统一,改为8'h26和6'h46。
default后面不应该写valid,应该写out的default值,以表明除了case里面情况以外,out的值为多少。
valid的判断。当out等于0~9的时候,valid=1,否则为0。但是在case里面,out的default值就是0,所以说如果单用out来判断valid,肯定是不够的。所以这里还要加上code的值来辅助判断。
答案不唯一:
module top_module (
input [7:0] code,
output reg [3:0] out,
output reg valid=1 );//
always @(*) begin
case (code)
8'h45: out = 0;
8'h16: out = 1;
8'h1e: out = 2;
8'h26: out = 3;
8'h25: out = 4;
8'h2e: out = 5;
8'h36: out = 6;
8'h3d: out = 7;
8'h3e: out = 8;
8'h46: out = 9;
default: out = 0;
endcase
if (out==0 && code!=8'h45) begin
valid=0;
end else begin
valid=1;
end
end
endmodule
这一节主要是更改一些bug,有语法错误,也有逻辑错误。语法错误相对比较好改,只要在之前写的时候注意了语法就行。但是逻辑错误就比较难改,需要弄清楚原代码的逻辑,然后去和要实现的功能去进行对比,才能知道哪里出了问题。