牛客 verilog入门 VIP

1、输出1

答案:

复制代码
`timescale 1ns/1ns

module top_module(
    output wire one
);

    assign one = 1'b1;
endmodule

2、wire连线

答案:

复制代码
`timescale 1ns/1ns

module wire0(
    input wire in0,
    output wire out1
);

assign out1 = in0;
endmodule

3、多wire连线

复制代码
`timescale 1ns/1ns

module top_module(
    input wire  a,
    input wire  b,

    output wire x,
    output wire y,
    output wire z
);

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

endmodule

4、反相器

答案:

复制代码
`timescale 1ns/1ns

module top_module(
   	input in,
	output out 
);

assign out = ~in;
endmodule

5、与门

答案:

复制代码
`timescale 1ns/1ns
module top_module( 
    input a, 
    input b, 
    input c,
    output d );

    wire temp;

    and gate(temp, a, b);
    and gate(d, temp, c);
    
endmodule

6、NOR门

答案:

复制代码
`timescale 1ns/1ns

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

    or gate(d, a, b);
    not gate(c, d);
endmodule

7、XOR门

答案:

复制代码
`timescale 1ns/1ns

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

8、逻辑运算

答案:

复制代码
`timescale 1ns/1ns

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

endmodule

9、模拟逻辑芯片

答案:

复制代码
`timescale 1ns/1ns

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

    wire out1, out2, out3, out4;
    assign out1 = p2a & p2b;
    assign out2 = p1a & p1c & p1b;
    assign out3 = p2c & p2d;
    assign out4 = p1f & p1e & p1d;

    assign p1y = out2 | out4;
    assign p2y = out1 | out3;
    
endmodule

10、逻辑运算2

答案:

复制代码
`timescale 1ns/1ns

module top_module (
	input a,
	input b,
	input c,
	input d,
	output e,
	output f );
	
	wire out1, out2, out3;
	assign out1 = a & b;
	assign out2 = c ^ d;
	assign out3 = out1 ^ out2;
	assign f = out3 | d;
	assign e = ~out3;

endmodule

11、多位信号

答案:

复制代码
`timescale 1ns/1ns

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

endmodule

12、信号顺序调整

答案:

复制代码
`timescale 1ns/1ns

module top_module(
    input [15:0] in,
    output out
);

    assign out = {in[3:0], in[7:4], in[11:8], in[15:11]};

endmodule

13、位运算与逻辑运算

答案:

复制代码
`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

14、对信号按位操作

答案:

复制代码
`timescale 1ns/1ns

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

15、信号级联合并

答案:

复制代码
`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] out;
    assign out = {a, b, c, d, e, f, {2'b11}};

    assign w = out[31:24];
    assign x = out[23:16];
    assign y = out[15:8];
    assign z = out[7:0];

endmodule

16、信号反转输出

答案:

复制代码
`timescale 1ns/1ns

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[i] = in[15-i];
    end
endgenerate

endmodule

17、三元操作符

答案:

复制代码
`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 = (temp1 > c)? temp1 : c;
    assign max = (temp2 > d)? temp2 : d;

endmodule

18、 多位信号xnor

答案:

复制代码
`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

19、五到一选择器

答案:

复制代码
`timescale 1ns/1ns

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

    always @(*)begin
        out = (sel == 0)? a : (sel == 1)? b : (sel == 2)? c : (sel == 3)? d : (sel == 4)? e : 4'b0;
    end

endmodule

20、 256选1选择器

答案:

复制代码
`timescale 1ns/1ns

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

assign out = in[sel];

endmodule
相关推荐
szxinmai主板定制专家15 小时前
RK3568 + CODESYS+实时系统运动控制器PLC,支持 AI 视觉目标检测,预测性维护,混合多系统部署,多路模拟量采集
arm开发·人工智能·嵌入式硬件·fpga开发
GateWorld18 小时前
LCD显示技术完全指南:原理·制造·驱动·FPGA实现之驱动二
fpga开发·lcd显示·fpga点亮屏幕·minilvds
GateWorld20 小时前
LCD显示技术完全指南:原理·制造·驱动·FPGA实现之驱动一
fpga开发·lcd显示·minilvds·fpga点屏
XMAIPC_Robot20 小时前
深度无人机自动驾驶仪,中小型无人机硬件在环仿真飞行
运维·arm开发·人工智能·fpga开发·无人机·边缘计算
小眼睛FPGA1 天前
【紫光HiYou开源入门轻量级PCIE开发板PG2L25G】实验例程1-基于紫光FPGA 的LED 流水灯
fpga开发
不会武功的火柴1 天前
SystemVerilog语法(8)-有限状态机(FSM)
嵌入式硬件·fpga开发·自动化·ic验证·rtl·uvm方法学
Kent Gu2 天前
Lattice FPGA选型
fpga开发
Terasic友晶科技2 天前
答疑解惑|为DE25-Nano开发板配置Linux kernel时.config文件没有起作用是什么原因?
linux·服务器·fpga开发·linux kernel·de25-nano
8K超高清2 天前
CCBN展会多图回顾
人工智能·算法·fpga开发·接口隔离原则·智能硬件
小眼睛FPGA2 天前
【紫光HiYou开源入门轻量级PCIE开发板PG2L25G】实验例程5-DDR3 读写实验例程
fpga开发