参考连接:HDLBits导学
Problem 158 Mux
问题:这个 8 位宽的 2 选 1 选择器不起作用。修复错误
原代码:
module top_module (
input sel,
input [7:0] a,
input [7:0] b,
output out );
assign out = (~sel & a) | (sel & b);
endmodule
解决:
module top_module (
input sel,
input [7:0] a,
input [7:0] b,
output [7:0]out );
assign out = sel ? a : b;
endmodule
Problem 159 NAND
问题:这个三输入与非门不起作用。修复错误
您必须使用提供的 5 输入与门
module andgate ( output out, input a, input b, input c, input d, input e );
源代码:
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 tmp;
assign out = ~tmp;
andgate inst1 ( tmp,a, b, c,1'b1,1'b1 );
endmodule
Problem 160 MUX/Bugs mux4
问题:这个 4 选1选择器不起作用。修复错误
为您提供了一个无错误的 2 对选1 选择器
module mux2 (
input sel,
input [7:0] a,
input [7:0] b,
output [7:0] out
);
原代码:
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] mux0, mux1;
mux2 mux2_inst1 ( sel[0], a, b, mux0 );
mux2 mux2_inst2 ( sel[0], c, d, mux1 );
mux2 mux2_inst3 ( sel[1], mux0, mux1, out );
endmodule
注意:这个2选1的模块,选择信号为0,选择的是第一个输入信号,反之选择第二个输入信号(自己试一下也就知道了,题目没有给出)
Problem 161 Add/sub/Bugs addsubz
问题:下面这个带有零标志的加减法器不起作用。修复错误
原代码:
// 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
解决:
// 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
(有一说一,不知道为什么~out就不能判断0了)
Problem 162 Case statement/Bugs case
问题:这个组合电路应该识别 0 到 9 的 8 位键盘扫描码。它应该指示是否识别了 10 种情况之一(有效),如果是,则检测到哪个键。修复错误
原代码:
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
解决:
module top_module (
input [7:0] code,
output reg [3:0] out,
output reg valid=1 );//
always @(*) begin
valid = 1'b1;
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: begin valid = 0; out=0; end
endcase
end
endmodule
一开始还有点怀疑会不会给我错误的键盘扫描码(然后我还去百度了,,,)