前言
这是我第二次刷HDLBits的题,第一次是刚接触FPGA时,为了快速入门Verilog,第一次刷题跟着B站视频刷的,但是现在发现没有很大的用处,个人感觉还是有一点Verilog基础后,再来刷HDLBits会好一点,虽然很多人说这上面的题都很简单,但是还是值得刷一遍,里面几乎涵盖了Verilog的所有常用语法,并且还可以尝试用不同方法解同一道题。
代码以下是我写的每道题的代码和思路
module top_module (
input in,
output out);
assign out = in;
endmodule
// 输出接地
module top_module (
output out);
assign out = 1'd0;
endmodule
// 或非NOR,学会看图
module top_module (
input in1,
input in2,
output out);
assign out = ~(in1|in2);
endmodule
module top_module (
input in1,
input in2,
output out);
assign out = in1 & (~in2);
endmodule
// 异或、同或
module top_module (
input in1,
input in2,
input in3,
output out);
wire out1;
assign out1 = ~(in1^in2);
assign out = out1 ^in3;
endmodule
// XOR 、 NOR :X是异;N是非
module top_module(
input a, b,
output out_and,
output out_or,
output out_xor,
output out_nand,
output out_nor,
output out_xnor,
output out_anotb
);
assign out_and = a & b ;
assign out_or = a | b;
assign out_xor = a ^ b;
assign out_nand = ~(a&b);
assign out_nor = ~(a | b);
assign out_xnor = ~(a^b);
assign out_anotb = a &(~b);
endmodule
// 7421芯片
module top_module (
input p1a, p1b, p1c, p1d,
output p1y,
input p2a, p2b, p2c, p2d,
output p2y );
assign p1y = ~(p1a & p1b & p1c & p1d);
assign p2y = ~(p2a & p2b & p2c & p2d);
endmodule
// 真值表:需要化简
module top_module(
input x3,
input x2,
input x1, // three inputs
output f // one output
);
assign f = (!x3 & x2) | (x1 & x3);
endmodule
module top_module ( input [1:0] A, input [1:0] B, output z );
assign z = (A==B)?1'b1:1'b0;
endmodule
module top_module (input x, input y, output z);
assign z = (x^y) & x;
endmodule
// 看波形图写Verilog
module top_module ( input x, input y, output z );
// 方法一:
// assign z = (!x & !y)|(x&y);
// 方法二:
assign z = (x==y)?1'b1:1'b0;
endmodule
//==================== 12 ======================
// 多模块例化
module top_module (input x, input y, output z);
wire z_a_1;
wire z_a_2;
wire z_b_1;
wire z_b_2;
wire z_a1b1;
wire z_a2b2;
assign z_a1b1 = z_a_1 | z_b_1;
assign z_a2b2 = z_a_2 & z_b_2;
assign z = z_a1b1 ^ z_a2b2;
mt2015_q4a mt2015_q4a_inst_1(
.x(x),
.y(y),
.z(z_a_1));
mt2015_q4a mt2015_q4a_inst_2(
.x(x),
.y(y),
.z(z_a_2));
mt2015_q4b mt2015_q4b_inst_1(
.x(x),
.y(y),
.z(z_b_1));
mt2015_q4b mt2015_q4b_inst_2(
.x(x),
.y(y),
.z(z_b_2));
endmodule
module mt2015_q4a (input x, input y, output z);
assign z = (x^y) & x;
endmodule
module mt2015_q4b ( input x, input y, output z );
// 方法一:
// assign z = (!x & !y)|(x&y);
// 方法二:
assign z = (x==y)?1'b1:1'b0;
endmodule
//==================== 13 ======================
// 手机响铃、震动,下面2道题都是通过,描述写代码
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
//assign ringer = ring ;报错,这里的非不能省
assign ringer = ring & !vibrate_mode;
assign motor = ring & vibrate_mode;
endmodule
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
assign heater = mode & too_cold;
assign aircon = !mode & too_hot;
assign fan = heater | aircon | fan_on;
endmodule
module top_module(
input [2:0] in,
output [1:0] out );
// 不能这样写,这样就会??仿真看看
// reg count;
// integer i;
// always @(*) begin
// count = 1'b0;
// for(i = 0;i
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?