这两题没有给出具体的函数代码,我觉得fix bugs 的解题思路是,首先清楚常规函数的代码原理,然后根据原理修改代码,其中修改一些代码不规范的错误,
4.1.2 NAND
源代码:
module top_module (input a, input b, input c, output out);//
andgate inst1 ( a, b, c, out );
endmodule
修改后的代码:
module top_module (input a, input b, input c, output out);//
wire out_and;
andgate inst1 ( .out(out_and), .a(a), .b(b), .c(c), .d(1'b1), .e(1'b1) );
assign out = !out_and;
endmodule
- 函数参数位置是否正确?
- 参数引用是否正确?
- 判断每个参数的长度是否一致?
4.1.3 MUX
需要清楚4选一是怎么实现的?
所以mux0 和 mux1需要用来判断sel为0,1,2,3时对应的四选一分别为a,b,c,d。
mux0为sel[1]为0时,从sel为0,1选择a,b
mux1为sel[1]为1时,从sel为2,3选择c,d
最后mux3通过第二位进位是否为1来判断选择mux0还是mux1的结果
源代码:
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
修改后的代码:
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] mux00, mux11;
mux2 mux0 ( sel[0]&!sel[1], a, b, mux00 );
mux2 mux1 ( sel[1]&sel[0], c, d, mux11 ); //confirm the order
mux2 mux3 ( sel[1], mux00, mux11, out );
endmodule