Verilog入门20题(牛客)

LV1 输出1

在这里插入图片描述
线网型变量赋值用assign,然后注意一下输出信号名称为one即可。

`timescale 1ns/1ns 
module top_module( 
    output  one
); 
assign one = 1; 
endmodule

LV2 wire连线

在这里插入图片描述

注意一下输入信号和输出信号用题目给的名字即可,输入和输出的连接等价于将输入赋值给输出。

`timescale 1ns/1ns
module wire0(
    input in0,
    output out1
);
    assign out1 = in0;
endmodule

LV3 多wire连接

在这里插入图片描述

`timescale 1ns/1ns
module top_module(
    input a,b,
    output x,y,z
);
    assign z = b;
    assign x = b;
    assign y = b;
endmodule

LV4 反相器

在这里插入图片描述
输入取反赋值给输出即可。

`timescale 1ns/1ns
module top_module(
   	input in,
	output out 
);
	assign out =~in;
endmodule

LV5 与门

在这里插入图片描述
三个输入相与之后赋值给输出。

`timescale 1ns/1ns
module top_module( 
    input a, 
    input b, 
    input c,
    output d );
    assign d = a&b&c;
    
endmodule

LV6 NOR门

在这里插入图片描述
由题易得:

`timescale 1ns/1ns

module top_module( 
    input a, 
    input b, 
    output c,
    output d);
    assign c =~( a | b);
    assign d = a |b ;
endmodule

LV7 XOR门

在这里插入图片描述
根据异或的逻辑表达式易得:

`timescale 1ns/1ns

module top_module( 
    input a, 
    input b, 
    output c );
    assign c = (a&(~b)) | (b &(~a));

endmodule

LV8 逻辑运算

在这里插入图片描述
写出逻辑表达式后易得:

`timescale 1ns/1ns

module top_module (
	input a,
	input b,
	input c,
	input d,
	output e,
	output f );
	assign f = (~(a&b)&(c|d)) | ((a&b)&(~(c|d)));
	assign e = ~f;

endmodule

LV9 模拟逻辑芯片

在这里插入图片描述
写出输出关于输入的逻辑表达式后易得:

`timescale 1ns/1ns

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

LV10 逻辑运算2

在这里插入图片描述
写出输出关于输入的逻辑表达式后易得:

`timescale 1ns/1ns

module top_module (
	input a,
	input b,
	input c,
	input d,
	output e,
	output f );
assign 	e = ~((~(a&b)&((~c&d)|(~d&c)))|((a&b)&(~((~c&d)|(~d&c)))));
assign  f = (~e) |d;
endmodule

LV11 多位信号

在这里插入图片描述
由题易得:

`timescale 1ns/1ns

module top_module(
    input [2:0] in,
    output a,b,c
);
    assign a = in[2];
    assign b = in[1];
    assign c = in[0];
endmodule

LV12 信号顺序调整

在这里插入图片描述
题目给出了明显的提示,把输入信号in看成是由4个4位二进制数a,b,c,d按a,b,c,d顺序拼成的,即有assign {a,b,c,d} = in;
然后把这顺序换成d,c,b,a拼出out。

`timescale 1ns/1ns

module top_module(
    input [15:0] in,
    output [15:0] out
);
    wire [3:0] a,b,c,d;//中间变量
    assign {a,b,c,d} = in;
    assign out = {d,c,b,a}; 
endmodule

LV13 位运算与逻辑运算

在这里插入图片描述
由题易得:

`timescale 1ns/1ns

module top_module(
	input [2:0] a, 
	input [2:0] b, 
	output [2:0] c,
	output d
);
	assign c = a|b;
	assign d = a||b;

endmodule

LV14 对信号按位操作

在这里插入图片描述
由题意用归约运算符。

`timescale 1ns/1ns

module top_module( 
    input [4:0] in,
    output out_and,
    output out_or,
    output out_xor
);
    assign  out_and = ∈
    assign out_or  = |in;
    assign out_xor = ^in;
endmodule

LV15 信号级联合并

在这里插入图片描述
由题意直接拼。

`timescale 1ns/1ns

module top_module(
    input [4:0] a, b, c, d, e, f,
    output [7:0] w, x, y, z );
    wire [31:0] temp;//中间变量
    assign temp ={a,b,c,d,e,f,2'b11};
    assign {w,x,y,z} = temp;
endmodule

LV16 信号反转输出

在这里插入图片描述
反转满足关系,in[0] = in[15],in[1] = in[14]…即下标之和为15,故对任意介于0到15之间(包括0和15)的i,满足in[15-i] = in[i],所以有代码:

`timescale 1ns/1ns

module top_module(
    input [15:0] in,
	output [15:0] out
);
    reg [15:0] out1;
    integer i;
    always @(in)
    begin
        for(i=0;i<=15;i=i+1)
        begin
            out1[15-i] = in[i];
        end
    end
    assign out = out1;
endmodule

这里引入中间变量out1是因为线网型输出变量out不能直接在always语句块内被赋值。所以先将值赋给中间变量out1,后通过out1赋值给out。

LV17 三元操作符

在这里插入图片描述
三次比较即可。

`timescale 1ns/1ns

module top_module(
    input [7:0] a, b, c, d,
    output [7:0] max);//
    wire [7:0] temp1,temp2;
    assign temp1 = (a>b)?a:b;
    assign temp2 = (c>d)?c:d;
    assign max = (temp1>temp2)?temp1:temp2;
endmodule

LV18 多位信号xnor

在这里插入图片描述
本题考察拼接操作操作,把a,b,c拼起来是{a,b,c},把a,b,c,a,b,c,拼起来是{a,b,c,a,b,c}可简写为{2{a,b,c}}。基于这个原理,以及同或即异或的取反原理,得到代码如下:

`timescale 1ns/1ns

module top_module(
    input a, b, c, d, e,
	output [24:0] out
);
   wire [24:0] temp1,temp2;
   assign temp1 = {{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}};
   assign temp2 = {5{a,b,c,d,e}};
   assign out = ~(temp1 ^ temp2);
    
endmodule

LV19 五到一选择器

在这里插入图片描述

运用case语句,由题意得:

`timescale 1ns/1ns

module top_module( 
    input [3:0] a, b, c, d, e, 
    input [2:0] sel,
    output reg [3:0] out );
    always @(a,b,c,d,e,sel)
    begin
        case(sel)
            0:out=a;
            1:out=b;
            2:out=c;
            3:out=d;
            4:out=e;
            default:out=0;
        endcase
    end
endmodule

LV20 256选1选择器

在这里插入图片描述
由题意得:

`timescale 1ns/1ns

module top_module (
	input [255:0] in,
	input [7:0] sel,
	output  out
);
	assign out = in[sel];
endmodule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值