Getting Started—— Basic篇
1、four wires
module top_module(
input a,b,c,
output w,x,y,z );
assign w=a;
assign x=b;
assign y=b;
assign z=c;
endmodule
assign可以修改为
assign {w,x,y,z} = {a,b,b,c};
2、与门、或门、非门、异或门
module gate(
input a,
input b,
output y
);
assign y = a & b;//与门
assign y = a | b;//或门
assign y = ~a;//非门
assign y = a ^ b;//异或门
endmodule
Getting Started——vector篇
按位操作与逻辑操作
逻辑操作符
逻辑操作符主要有 3 个:&&(逻辑与), ||(逻辑或),!(逻辑非)。
逻辑操作符的计算结果是一个 1bit 的值,0 表示假,1 表示真,x 表示不确定。
如果一个操作数不为 0,它等价于逻辑 1;如果一个操作数等于 0,它等价于逻辑 0。如果它任意一位为 x 或 z,它等价于 x。
如果任意一个操作数包含 x,逻辑操作符运算结果不一定为 x。
按位操作符
按位操作符包括:取反(~),与(&),或(|),异或(^),同或(~^)。
按位操作符对 2 个操作数的每 1bit 数据进行按位操作。
如果 2 个操作数位宽不相等,则用 0 向左扩展补充较短的操作数。
取反操作符只有一个操作数,它对操作数的每 1bit 数据进行取反操作。
举例
module top_module(
input [2:0] a,
input [2:0] b,
output [2:0] out_or_bitwise,
output out_or_logical,
output [5:0] out_not
);
assign out_or_bitwise = a | b;
assign out_or_logical = a || b;
assign out_not[2:0] = ~a; // Part-select on left side is o.
assign out_not[5:3] = ~b; //Assigning to [5:3] does not conflict with [2:0]
endmodule
给定一个8位输入向量[7:0] ,反转其位序。
module top_module (
input [7:0] in,
output [7:0] out
);
assign {out[0],out[1],out[2],out[3],out[4],out[5],out[6],out[7]} = in;
endmodule
连接运算符{ }
允许将向量连接在一起以形成一个更大的向量。
复制操作符允许重复一个向量并将它们连接在一起:
{num{vector}}
module top_module (
input [7:0] in,
output [31:0] out
);
// Concatenate two things together:
// 1: {in[7]} repeated 24 times (24 bits)
// 2: in[7:0] (8 bits)
assign out = { {24{in[7]}}, in };
endmodule
module
要进行内部连接,需要声明一些连接。
在命名连接和模块实例时要小心: 名称必须是唯一的。
提供给您的模块是:
my_dff ( input clk, input d, output q );
module top_module (
input clk,
input d,
output q
);
wire a, b; // Create two wires. I called them a and b.
// Create three instances of my_dff, with three different instance names (d1, d2, and d3).
// Connect ports by position: ( input clk, input d, output q)
my_dff d1 ( clk, d, a );
my_dff d2 ( clk, a, b );
my_dff d3 ( clk, b, q );
endmodule