前言
这是我第二次刷HDLBits的题,第一次是刚接触FPGA时,为了快速入门Verilog,第一次刷题跟着B站视频刷的,但是现在发现没有很大的用处,个人感觉还是有一点Verilog基础后,再来刷HDLBits会好一点,虽然很多人说这上面的题都很简单,但是还是值得刷一遍,里面几乎涵盖了Verilog的所有常用语法,并且还可以尝试用不同方法解同一道题。
代码以下是我写的每道题的代码和思路
Basic// 线
module top_module (
input in, output out
);
assign out = in;
endmodule
// 3输入、4输出
// assign:描述的是连接关系,不是赋值关系
module top_module(
input a,b,c,
output w,x,y,z );
assign w = a;
assign x = b;
assign y = b;
assign z = c;
endmodule
// 非门
module top_module( input in, output out );
assign out = ~ in;
endmodule
// 与门
module top_module(
input a,
input b,
output out );
assign out = a & b;
endmodule
// 或非门
module top_module(
input a,
input b,
output out );
assign out = ~(a | b);
endmodule
// 同或门=异或非
module top_module(
input a,
input b,
output out );
assign out = ~(a ^ b);
endmodule
// module内部用wire连接
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire wire_ab;
wire wire_cd;
wire wire_abcd;
assign wire_ab = a & b;
assign wire_cd = c & d;
assign wire_abcd = wire_ab | wire_cd;
assign out = wire_abcd;
assign out_n = ~ wire_abcd;
endmodule
// 7458芯片
module top_module (
input p1a, p1b, p1c, p1d, p1e, p1f,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
wire wire_1_abc;
wire wire_1_def;
wire wire_2_ab;
wire wire_2_cd;
wire wire_1;
wire wire_2;
assign wire_2_ab = p2a & p2b;
assign wire_2_cd = p2c & p2d;
assign wire_1 = wire_2_ab | wire_2_cd;
assign wire_1_abc = p1a & p1b & p1c;
assign wire_1_def = p1d & p1e & p1f;
assign wire_2 = wire_1_abc | wire_1_def;
assign p1y = wire_2;
assign p2y = wire_1;
endmodule