牛客网Verilog刷题解答:LEVEL【1】--- 入门特别版20题

目录

VL1 输出

VL2 wire连线

VL3 多wire连接

VL4 反相器

VL5 与门

VL6 NOR 门

VL7 XOR 门

VL8 逻辑运算

VL9 模拟逻辑芯片

VL10 逻辑运算2

VL11 多位信号

VL12 信号顺序调整

VL13 位运算与逻辑运算

VL14 对信号按位操作

VL15 信号级联合并

VL16:信号反转输出

VL17 三元操作符

VL18 多位信号xnor

VL19 五到一选择器

VL20 256选1选择器


VL1 输出


描述:构建一个没有输入和一个输出常数1的输出的电路
输入描述:无
输出描述:输出信号为one

Plan1:
module top_module(
    output one
);
assign one = 1'b1;
endmodule

VL2 wire连线


描述:创建一个具有一个输入和一个输出的模块,其行为类似于电路上的连线
输入描述:输入信号in0
输出描述:输出信号out1

Plan1:
module wire0(
    input in0,
    output out1
);
assign out1 = in0;
endmodule

VL3 多wire连接


描述:创建一个具有 2个输入和 3个输出的模块,使用线连接的方式:
a -> z
b -> x
b -> y
输入描述:input wire  a  b 
输出描述:output wire x y z 

Plan1:
module top_module(
    input wire a,b,
    output wire x,y,z
);
assign z = a;
assign x = b;
assign y = b;
endmodule

VL4 反相器


描述:输出输入信号的值的相反的值
输入描述:in
输出描述:out

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

VL5 与门


描述:创建实现 AND 门的模块,输入有三个wire,将三个信号(a b c)进行与操作,请思考在实际电路需要几个与门?请写出对应的RTL
输入描述:a b c 
输出描述:d

Plan1:
module top_module( 
    input a, 
    input b, 
    input c,
    output d );

assign d = a & b & c;

endmodule

VL6 NOR 门


描述;创建实现 OR和NOR 的模块,NOR 门是输出反相的 OR 门。c 是 nor输出,d是or输出
输入描述:a b
输出描述:c d

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

assign d = a|b;
assign c = !d;
endmodule

VL7 XOR 门


描述:创建一个实现 XOR 门的模块
输入描述:a b 
输出描述:c

Plan1:
module top_module( 
    input a, 
    input b, 
    output c );
    
assign c = a^b;
endmodule

VL8 逻辑运算


描述:写出如图的rtl逻辑,限制使用最多四次assign
输入描述:a b c d 
输出描述:e f 

module top_module (
    input a,
    input b,
    input c,
    input d,
    output e,
    output f );
    
assign x = a & b;
assign y = c | d;
assign f = x ^ y;
assign e = !f ;
endmodule

VL9 模拟逻辑芯片


描述:下图为某芯片的逻辑,请通过RTL实现它的功能
输入描述:p1a, p1b, p1c, p1d, p1e, p1f,p2a, p2b, p2c, p2d
输出描述:p1y,p2y

Plan1:
module top_module ( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );

assign a = p2a & p2b;
assign b = p2c & p2d;
assign p2y = a | b;
assign a1 = p1a & p1c & p1b;
assign b1 = p1f & p1e & p1d;
assign p1y = a1 | b1;
    
endmodule

VL10 逻辑运算2


描述:根据下述逻辑,给出对应的module设计
输入描述:a b c d
输出描述:e f

Plan1:
module top_module (
    input a,
    input b,
    input c,
    input d,
    output e,
    output f );
assign  x = a & b;
assign  y = c ^ d;
assign  z = x ^ y;
assign  e = !z;
assign  f = z | d;

endmodule

VL11 多位信号


描述:构建一个具有一个3位输入in的信号[2:0],将其分成三个独立的输出a b c(从2到0)
输入描述:in
输出描述:a b c

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

VL12 信号顺序调整


描述:一个16位信号in包含四个四位数[3:0]a[3:0]b[3:0]c[3:0]d,将它们顺序倒置为dcba输出,输出out
输入描述:in
输出描述:out

Plan1:
module top_module(
    input [15:0] in,
    output [15:0] out
);
assign out[15:12] = in[3:0];
assign out[11:8] = in[7:4];
assign out[7:4] = in[11:8];
assign out[3:0] = in[15:12]; 
endmodule

VL13 位运算与逻辑运算


描述:现有一个模块,输入信号为[2:0]a和[2:0]b,请输出信号的按位或[2:0]c和或信号d
输入描述:[2:0]a [2:0]b
输出描述:[2:0]c d

Plan1:
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

VL14 对信号按位操作


描述:将一个五输入的信号分别进行的每一位进行: 全部按位与;全部按位或;全部按位异或
输入描述:[4:0]in
输出描述:out_and,  out_or,  out_xor

Plan1:
module top_module( 
    input [4:0] in,
    output out_and,
    output out_or,
    output out_xor
);
assign out_and = in[4] & in[3] & in[2] & in[1] & in[0];
assign out_or  = in[4] | in[3] | in[2] | in[1] | in[0];
assign out_xor = in[4] ^ in[3] ^ in[2] ^ in[1] ^ in[0];
endmodule

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

Plan3:
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

VL15 信号级联合并


描述:将6个输入信号串联转为四个信号输出,输入信号为[4:0] a[4:0] b[4:0]c [4:0]d [4:0]e [4:0]f,末尾增加一个宽度为两位的3,形成32位长度后,按照从前到后的顺序输出[7:0]w [7:0]x [7:0]y [7:0]z
输入描述:[4:0] a[4:0] b[4:0]c [4:0]d [4:0]e [4:0]f
输出描述:[7:0]w [7:0]x [7:0]y [7:0]z

Plan1:
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 = temp[31:24];
    assign x = temp[23:16];
    assign y = temp[15:8];
    assign z = temp[7:0];
endmodule

Plan2:
module top_module(
    input [4:0] a, b, c, d, e, f,
    output [7:0] w, x, y, z );
assign {w,x,y,z} = {a,b,c,d,e,f,2'b11};
endmodule

Plan3:
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

VL16:信号反转输出


描述:输入一个16位的信号in,将其从低位到高位输出(即反转顺序输出)为out
输入描述:[15:0] in
输出描述:[15:0] out

Plan1:
module top_module(
    input [15:0] in,
    output [15:0] out
);
genvar i;
generate
    for(i = 0; i < 16; i = i+1) begin
    assign out[15-i] = in[i]; 
    end
endgenerate
endmodule

VL17 三元操作符


描述:给定四个无符号数字,找到最大值。不使用if进行判断,尽量少使用语句的情况下完成。
输入描述:[7:0]a b c d
输出描述:[7:0] max
/wire中间变量必须指定尾款,否则默认为1bit位宽

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

Plan2:
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

VL18 多位信号xnor


描述:给定五个1bit信号(a、b、c、d 和 e),生成两种25位的数据: 一种是将信号复制五次后连接起来aaaaabbbbb...,一种是将信号连接起来复制五次成为abcdeabcde... 。比较两个25位信号,如果两个信号的同样位置的位相等,则输出1。
输入描述:a, b, c, d, e,
输出描述:[24:0] out

Plan1:
module top_module(
    input a, b, c, d, e,
    output [24:0] out
);
wire [4:0]  tmp1;
wire [24:0] tmp2,tmp3;
assign tmp1 = {a,b,c,d,e};
assign tmp2 = {tmp1,tmp1,tmp1,tmp1,tmp1};
assign tmp3 = {a,a,a,a,a,b,b,b,b,b,c,c,c,c,c,d,d,d,d,d,e,e,e,e,e};
assign out = ~(tmp2^tmp3);
endmodule

Plan2:
module top_module(
    input a, b, c, d, e,
    output [24:0] out
);
    wire [24:0] out1,out2;
    assign out1 = {{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}};
    assign out2 = {5{a, b, c, d, e}};
//    assign out = ~(out1 ^ out2);

    genvar i;                                               
    generate for(i=0; i<25; i=i+1 ) //generate for模块复制
    begin : LOOP    
        assign out[i] = (out1[i] == out2[i]);
    end                      
    endgenerate   

endmodule

VL19 五到一选择器


描述:输入5个4bit信号,根据sel的值选出对应的信号,对应关系为:0~a  1~b 2~c 3~d 4~e 其他~置零
输入描述:[3:0] a b c d e [2:0]sel 
输出描述:[3:0] out

Plan1:
module top_module( 
    input [3:0] a, b, c, d, e, 
    input [2:0] sel,
    output reg [3:0] out );
    always @(*)begin
        case(sel)
            2'b000:  out = a;
            2'b001:  out = b;
            2'b010:  out = c;
            2'b011:  out = d;
            2'b100:  out = e;
            default: out = 0;
        endcase
    end
endmodule

Plan2:
module top_module( 
    input [3:0] a, b, c, d, e, 
    input [2:0] sel,
    output reg [3:0] out );

    always@(*)
    case(sel)
        0:out = a;
        1:out = b;
        2:out = c;
        3:out = d;
        4:out = e;
        default:out = 0;
    endcase

endmodule

VL20 256选1选择器


描述:输入一个256位信号,根据sel信号输出对应位数值,当 sel = 0 时选择 in[0],sel = 1 时选择 in[1],以此类推
输入描述:[255:0] in [7:0]sel
输出描述:out

Plan1:
module top_module (
    input [255:0] in,
    input [7:0] sel,
    output  out
);

assign out = in[sel];
endmodule

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值