[HDLBits] 习题记录(2)

226 阅读1分钟

4、Create a module with 3 inputs and 4 outputs that behaves like wires that makes these connections: a -> w b -> x b -> y c -> z 创建一个有3个输入端口和四个输出端口的模块,并且按照a -> w;b -> x;b -> y;c -> z,来连接输入和输出端口。

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

conclusion: assign语句不是在定义和创建新的连线,而是用来将已经定义的端口或者寄存器连接起来。

2、Create a module that implements a NOT gate. 创建一个实现非门的模块。

module top_module( 
    input in, 
    output out 
);

    assign out=!in;

endmodule

conclusion: !逻辑取反,例:!1为0; !0 为1 ~按位取反,例:11110 按位取反后为00001

3、Create a module that implements an AND gate. 创建一个实现与门的模块

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

    assign out=a&b;
    
endmodule

conclusion: &:位运算符,按位与,例:111&110=110 &&:逻辑运算符,逻辑与,例:111&110=1 在逻辑判断中最好使用逻辑运算符(区别在于 & 两边都运算,而 && 先算 && 左侧,若左侧为 false 那么右侧就不运算了,效率可能会高一点)