HDLBits学习------Problem 1~9

136 阅读1分钟

​本文已参与「新人创作礼」活动,一起开启掘金创作之路。

 参考连接:HDLBits导学

Problem 1 : Zero

        问题:没有输入,输出0

        解决

module top_module(
    output zero
);// Module body starts after semicolon
    assign zero = 0;
endmodule


Problem 2 : Wire

       问题:使用wire让输出等于输入

       解决

module top_module( input in, output out );
    assign out = in;
endmodule


Problem 3 : Wire4

        问题:创建一个模块有3个输入和4个输出,实现这样的连接:

                                                                        ​编辑

        思路:wire 的源一般只能有一个,终点确可以有多个。一个源可以驱动多个信号。

        解决

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

        注意:assign 语句并不是创建 wire ,而是将创建 wire 之间的连接。


 Problem 4 : Notgate

        问题:通过 assign 语句以及 Verilog 的逻辑操作符,实现一个非门模块。

        思路“~”  逐位取反,只有一位也可以逻辑取反

        解决

module top_module( input in, output out );
    assign out = ~in;
endmodule


Problem 5 : Andgate

问题:创建一个模块实现与门

        思路“&” 逐位与

        解决

module top_module( 
    input a, 
    input b, 
    output out );
	
    assign out = a&b;
    
endmodule

        注意“&” 是逐位与,而 “&&” 是逻辑与


Problem 6 : Norgate

****  问题:创建一个模块实现或非门

        思路“|” 逐位或 和 “ ~” 的组合使用

        解决

module top_module( 
    input a, 
    input b, 
    output out );

    assign out = ~(a|b);
    
endmodule

        注意“~” 非逻辑的优先级大于 “|”


Problem 7 : XNorgate

问题:创建一个模块实现同或门

        思路“~” 取反 和 “^” 异或的组合使用

        解决

module top_module( 
    input a, 
    input b, 
    output out );

    assign out = ~(a^b);
    
endmodule

        注意“^” 为逐位异或,Verilog 中不存在逻辑异或符号。


Problem 8 : Declaring wires

问题:实现以下电路。 创建两条中间线(命名为您想要的任何名称)将 AND 和 OR 门连接在一起。

        

​编辑

        解决

`default_nettype none
module top_module(
    input a,
    input b,
    input c,
    input d,
    output out,
    output out_n   ); 
    
    wire a_b = a & b;
    wire c_d = c & d;
    assign out = a_b | c_d;
    assign out_n = ~(a_b | c_d);

endmodule

        注意:可以在定义变量的同时给它赋值;

                信号只能被一个信号驱动,但是可以驱动多个信号


Problem 9 : 7458

        问题:创建一个与 7458 芯片功能相同的模块,它有 10 个输入和 2 个输出

​编辑

思路:7458的一个输出是四个输入中 两两相与后再或得到一个输出,另一个输出是六个输入中三三相与后再或得到的

        解决

module top_module ( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );
	
	wire and2_1 = p2a & p2b;
	wire and2_2 = p2c & p2d;
	wire and3_1 = p1a & p1b & p1c;
	wire and3_2 = p1d & p1e & p1f;
	
	assign p2y = and2_1 | and2_2;
	assign p1y = and3_1 | and3_2;


endmodule