USTC Verilog oj题解1-9

1.
module top_module(
        output one
    );
    assign one = 1 ;
endmodule

2.

module top_module(
  output out
);
    assign one = 0 ;
endmodule

3.

module top_module(
    input wire in, output wire out
);
#wire in,out;(可以在这里定义类型,上面删去wire就可以了)
    assign out = in;
endmodule

4.

module top_module( 
    input a,b,c,
    output w,x,y,z
    );

        wire w,x,y,z;

        assign w = a;
        assign x = b;
        assign y = b;
        assign z = c;

endmodule
#assign都是同时运行哦

5.

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

#顺便一说       !”表示逻辑取反,~”表示按位取反
当位宽不为1时:
“~”会把所有位取反:a[4:0] ={1,1,0,0,1} , ~a ={0,0,1,1,0};
“!”会将变量当作一个数值看待,不管化为几进制的数,非0为1:a[3:0] ={1,0,0,1} ,a=9,!a=0。a[3:0] ={0,0,0,0} ,a=0,!a=1

6.

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

    and a1(out,a,b);
endmodule
#
与门 and
或门 or
非门 not
与非门 nand
或非门 nor
异或门 xor
同或门 xnor
缓冲器 bu

这里举例了一些verilog自带的模块。
您可以把他们理解为类似于c中的函数的东西。
格式位
<语句><时延><模块名>(输出,输入)
(这里只有一个输出,多个输入,毕竟例子里的是简单的单输出逻辑门)
7.

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

    nor a1(out,a,b);
endmodule
8.

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

    assign out = ~(a ^ b);
endmodule
#这里想说一下运算优先级
大中小括号可以在计算里表优先级
至于多个运算符放在一起的情况,优先级参考本图

9.

module top_module(
    input a,
    input b,
    input c,
    input d,
    output out,
    output out_n   
); 

    wire and1,and2,out,out_n;

    assign and1 = a & b;
    assign and2 = c & d;
    assign out = and1 | and2;
    assign out_n = ~(and1 | and2);
endmodule



#ic#
全部评论

相关推荐

要AC不要WA:投一天,喜提两笔试
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务