构建一个没有输入和一个输出常数1的输出的电路 

`timescale 1ns/1ns

module top_module(one);
    output wire one;
    assign one = 1;
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 定义了一个名为top_module的模块,该模块具有一个输出端口one,并将该端口的值赋值为1。

  1. output wire one;:这定义了一个名为one的输出端口。output关键字表示这是一个输出端口,而wire表示这是一个线性的端口类型。这意味着one是一个用于输出信号的线性端口。
  2. assign one = 1;:这一行使用assign关键字将输出端口one赋值为1。这意味着无论何时引用one端口,它都将输出1。

创建一个具有一个输入和一个输出的模块,其行为类似于电路上的连线。

  1. input in0:定义一个名为in0的输入端口。输入端口可以接收一个单独的信号,通常是一个位(1位)信号。
  2. output out1:定义一个名为out1的输出端口。
  3. assign out1 = in0;:连线部分,将in0值分配给out1。这意味着out1端口的值将始终与in0端口的值相同。

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

`timescale 1ns/1ns

module top_module(
    input wire a,b,
    output wire x,y,z
);
assign z=a;
assign x=b;
assign y=b;
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  1. input wire a, b:这定义了两个输入端口,分别命名为ab。它们都被声明为线性(wire)端口类型,表示它们用于输入信号,并且信号可以被赋值和连线。
  2. output wire x, y, z:这定义了三个输出端口,分别命名为xyz。它们也被声明为线性端口类型,表示它们用于输出信号。
  3. assign z = a;:这一行使用assign关键字将输入端口a的值赋值给输出端口z
  4. assign x = b;:这一行使用assign关键字将输入端口b的值赋值给输出端口x
  5. assign y = b;:这一行使用assign关键字将输入端口b的值赋值给输出端口y

输出输入信号的值的相反的值

`timescale 1ns/1ns 

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

endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  1. input in:这定义了一个名为in的输入端口。它表示该模块将接收一个单一的位(1位)输入信号,该信号可以是0或1。
  2. output out:这定义了一个名为out的输出端口。这个输出端口将被用来输出与输入端口in相反的信号。
  3. assign out = ~in;:这一行使用assign关键字来实现逻辑赋值。它将输出端口out赋值为输入端口in的逻辑反值(取反)。也就是说,如果输入信号in为0,那么输出信号out将为1,反之亦然。

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

`timescale 1ns/1ns 
module top_module( input a, input b, input c, output d ); 
assign d = a&b&c; 
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  1. input a, b, c:这定义了三个输入端口,分别命名为abc。它们表示该模块接收三个输入信号,每个信号可以是0或1。
  2. output d:这定义了一个名为d的输出端口。这个输出端口将用于输出逻辑与(AND)操作的结果。
  3. assign d = a & b & c;:这一行使用assign关键字将输出端口d赋值为输入端口abc的逻辑与操作的结果。这意味着只有当abc都为1时,d端口才会输出1;否则,d端口将输出0
`timescale 1ns/1ns 
module top_module( input a, input b, input c, output d ); 
assign d = a&b&c; 
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  1. input a, b, c:这定义了三个输入端口,分别命名为abc。它们表示该模块接收三个输入信号,每个信号可以是0或1.
  2. output d:这定义了一个名为d的输出端口。这个输出端口将用于输出逻辑与(AND)操作的结果.
  3. assign d = a & b & c:这一行使用assign关键字将输出端口d赋值为输入端口abc的逻辑与操作的结果。这意味着只有当abc都为1时,d端口才会输出1;否则,d端口将输出0.

创建实现 OR和NOR 的模块,NOR 门是输出反相的 OR 门。

c 是 nor输出,d是or输出

`timescale 1ns/1ns 
module top_module( input a, input b, output c, output d); 
assign c=~d,d=a|b; 
endmodule
  • 1.
  • 2.
  • 3.
  • 4.

assign c = ~d,d = a | b:这一行包含了两个赋值语句。

首先,c端口被赋值为~d,表示c的值等于d的逻辑反值(取反)。

其次,d端口被赋值为a|b,表示d的值等于输入端口ab的逻辑或操作的结果。

  • ~d 表示将 d 的值取反,即如果 d 为 1,则 c 为 0,反之亦然。
  • a | b 表示将输入信号 ab 进行逻辑或操作,即如果 ab 中有至少一个为 1,那么 d 为 1;如果两者都为 0,那么 d 为 0。

创建一个实现 XOR 门的模块

`timescale 1ns/1ns
module top_module( 
    input a, 
    input b, 
    output c );
    assign c = (~a & b) + (a & ~b);
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

写出如图的rtl逻辑,限制使用最多四次assign

【Verilog】Verilog 入门语法_位或

`timescale 1ns/1ns
module top_module (
	input a,
	input b,
	input c,
	input d,
	output e,
	output f );
	assign e=~(a&b)^(c|d);
	assign f=~e;
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

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

`timescale 1ns/1ns
module top_module(
    input [2:0]in,
    output a,b,c
);
assign {a,b,c}=in;
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  1. module top_module(input [2:0] in, output a, b, c);:这是模块的定义开始,模块名为top_module,并在括号中列出了模块的输入和输出端口。 (1)input [2:0] in:这定义了一个名为in的输入端口,它是一个3位宽的信号。[2:0] 表示这个信号可以包含3个比特,即可以表示0到7之间的整数值。因此,in可以取8种不同的二进制组合,从000到111     (2)output a, b, c:这定义了三个输出端口,分别命名为abc。这些输出端口将用于输出in端口的不同比特值。
  2. assign {a, b, c} = in;:这一行使用assign关键字,将输入端口in的值拆分成三个部分,并分别赋值给输出端口abc。使用{}括号表示一个向量的赋值,其中a接收in的最低位,b接收in的中间位,c接收in的最高位。

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

`timescale 1ns/1ns
module top_module(
    input    wire    [15:0] in ,
    output   wire    [15:0] out
);

wire [3:0] a,b,c,d;
assign {a,b,c,d} = in;
assign out = {d,c,b,a};
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

  1. wire [3:0] a, b, c, d;:这定义了四个中间的线性信号端口,分别命名为abcd。每个中间端口都是4位宽的线性信号。
  2. assign {a, b, c, d} = in;:这一行使用assign关键字,将输入端口in的值拆分成四个部分,并分别赋值给中间端口abcd。使用{}括号表示一个向量的赋值,其中a接收in的低4位,b接收in的接下来的4位,以此类推。
  3. assign out = {d, c, b, a};:这一行将中间端口dcba的值重新组合成一个16位宽的向量,并赋值给输出端口out。这个操作实际上是对输入信号in的反转,即输出信号out的最低位是输入信号in的最高位,

 

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

`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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

或:|  按位或;||    逻辑或;

将一个五输入的信号分别进行的每一位进行: 全部按位与;全部按位或;全部按位异或

`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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  1. input [4:0] in:这是一个5位宽的输入端口,用于接收一个5位宽的数据。[4:0] 表示索引范围从4到0,因此in 是一个包含5个位的向量。

然后,使用assign语句为这些输出端口分配值:

  • assign out_and = &in[4:0];:这将 out_and 设置为输入向量 in 中所有位的按位与操作的结果。换句话说,out_and 将等于 in[4] & in[3] & in[2] & in[1] & in[0]
  • assign out_or = |in[4:0];:这将 out_or 设置为输入向量 in 中所有位的按位或操作的结果。换句话说,out_or 将等于 in[4] | in[3] | in[2] | in[1] | in[0]
  • assign out_xor = ^in[4:0];:这将 out_xor 设置为输入向量 in 中所有位的按位异或操作的结果。换句话说,out_xor 将等于 in[4] ^ in[3] ^ in[2] ^ in[1] ^ in[0]

将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

`timescale 1ns/1ns

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

{a,b,c,d,e,f,2'b11}是拼接操作。

输入一个16位的信号in,将其从低位到高位输出(即反转顺序输出)为out

`timescale 1ns/1ns

module top_module(
    input [15:0] in,
	output [15:0] out
);
assign out={in[0],in[1],in[2],in[3],in[4],in[5],in[6],in[7],in[8],in[9],in[10],in[11],in[12],in[13],in[14],in[15]};

endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

给定四个无符号数字,找到最大值。不使用if进行判断,尽量少使用语句的情况下完成。

`timescale 1ns/1ns
module top_module(
    input [7:0] a, b, c, d,
    output [7:0] max);
    wire [7:0] t1;
    wire [7:0] t2;
    assign t1=(a>b)?a:b;
    assign t2=(c>d)?c:d;
    assign max=(t1>t2)?t1:t2;
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

三元操作符,同C语言。

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

`timescale 1ns/1ns
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;
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

使用assign语句为这两个wire赋值:

  1. assign out1 = {{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}};
  • {{5{a}},{5{b}},{5{c}},{5{d}},{5{e}}} 是一个拼接操作,它将每个输入端口 abcde 分别复制5次,然后将它们连接成一个25位宽的向量。因此,out1 包含了输入端口的值,每个值都重复了5次。
  1. assign out2 = {5{a,b,c,d,e}};
  • {5{a,b,c,d,e}} 也是一个拼接操作,它将输入端口 abcde 的值组合成一个5位宽的向量,然后将这个向量重复5次,形成一个25位宽的向量。因此,out2 包含了输入端口的值,每个值都重复了5次。

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

`timescale 1ns/1ns

module top_module( 
    input [3:0] a, b, c, d, e, 
    input [2:0] sel,
    output reg [3:0] out );
    //此题考察case 选择语句
    always@(*)begin
        case(sel)
            0: out = a;
            1: out = b;
            2: out = c;
            3: out = d;
            4: out = e;
            default: out = 'd0;
        endcase
    end
endmodule
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

在模块的主体中,使用 always 块来创建一个组合逻辑,该逻辑会根据 sel 的值选择要输出的输入端口的值。具体地说,它使用 case 语句来根据 sel 的不同值进行选择: