提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
Verilog有一个三元条件运算符(?😃
(condition ? if_true : if_false)
这可以用于在一行中根据条件(a mux!)从两个值中选择一个,而不用在组合always块中使用if-then。
Examples:
(0 ? 3 : 5) .
(sel ? b : a)
always @(posedge clk)
q <= toggle ? ~q : q;
always @(*) //
case (state)
A: next = w ? B : A;
B: next = w ? A : B;
endcase
一、More Verilog Features
1.Conditional ternary operator
Practice: Given four unsigned numbers, find the minimum. Unsigned numbers can be compared with standard comparison operators (a < b). Use the conditional operator to make two-way min circuits, then compose a few of them to create a 4-way min circuit. You’ll probably want some wire vectors for the intermediate results.right?..)
翻译:给定四个无符号数,求最小值。无符号数可以与标准的比较运算符进行比较(a <b).用条件运算符构造双向最小电路,再组合几个组成4路最小电路。你可能需要一些wire作为中间结果。
Solution(不唯一,仅供参考):
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//
wire [7:0]min_ab,min_cd;
assign min_ab = a>b?b:a;
assign min_cd = c>d?d:c;
assign min = min_ab>min_cd?min_cd:min_ab;
endmodule
Timing Diagram
2.Reduction operators
Practice:Create a circuit that will compute a parity bit for a 8-bit byte (which will add a 9th bit to the byte). We will use “even” parity, where the parity bit is just the XOR of all 8 data bits.
翻译:创建一个电路,将计算一个8位字节的奇偶校验位(这将增加第9位的字节)。我们将使用“偶”奇偶校验,其中奇偶校验位就是所有8个数据位的异或。
奇偶校验:通过检验位将传输1的个数变成奇数就是奇校验,变成偶数就是偶校验。
Solution(不唯一,仅供参考):
module top_module (
input [7:0] in,
output parity);
assign parity = ^in;
endmodule
小知识
上述代码中用到了“归纳运算符”,当我们需要对一个向量的所有位进行操作时,如 a[0]&a[1]&a[2]&a[3]…,但这对于长的标量来说,编写代码会比较的麻烦,所以可以使用归纳运算符a[0]&a[1]&a[2]&a[3]…&a[100]=%a;理还有 &,|,~^,例如:
&a [3:0] // AND:a[3]&a[2]&a[1]&a[0]
|b [3:0] // OR: b[3]|b[2]|b[1]|b[0]
^c [2:0] // XOR:c[2]^c[1]^c[0]
“!”表示逻辑取反,“~”表示按位取反。举一个例子让你更好的记住它们的区别:
!(1100)=0;
~(1100)=0011
3.Reduction:Even wider gates
Practice:Build a combinational circuit with 100 inputs, in[99:0].
翻译:建立100输入的与门、或门和异或门(和上题一样)。
Solution(不唯一,仅供参考):
module top_module(
input [99:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_and = ∈
assign out_or = |in;
assign out_xor = ^in;
endmodule
Timing Diagram
4.Combinational for-loop:Vector reversal 2
Practice:Given a 100-bit input vector [99:0], reverse its bit ordering.
翻译:给定一个100位的输入向量[99:0],反转它的位顺序。
Solution(不唯一,仅供参考):
module top_module(
input [99:0] in,
output [99:0] out
);
always @(*)begin
for(int i=0;i<100;i++)begin
out[i] = in[99-i];
end
end
endmodule
方法二:
module top_module(
input [99:0] in,
output [99:0] out
);
generate
genvar i;
begin
for (i = 0; i<100; i=i+1) begin:name2
assign out[i] = in[99-i];
end
end
endgenerate
endmodule
5.Combinational for-loop:255-bit population count
Practice:A “population count” circuit counts the number of '1’s in an input vector. Build a population count circuit for a 255-bit input vector.
翻译:计算“1”的数量。
Solution(不唯一,仅供参考):
module top_module(
input [254:0] in,
output [7:0] out );
always @(*)begin
out = 0;//初始化
for(int i=0;i<255;i++)begin
if(in[i])begin
out = out+1;
end
else begin
out = out;
end
end
end
endmodule
Timing Diagram
6.Generate for-loop:100-bit binary adder2
Practice:Create a 100-bit binary ripple-carry adder by instantiating 100 full adders. The adder adds two 100-bit numbers and a carry-in to produce a 100-bit sum and carry out.
翻译:通过实例化 100 个全加器来实现一个 100bit 的二进制加法器。
提示:有许多完全加法器要实例化。这里可以使用实例数组或generate语句。
Solution(不唯一,仅供参考):
使用generate语句(具体使用看总结)
module top_module(
input [99:0] a, b,
input cin,
output [99:0] cout,
output [99:0] sum );
add_1bit add0(
.a(a[0]),
.b(b[0]),
.cin(cin),
.sum(sum[0]),
.cout(cout[0]),
);
generate
genvar i;
for(i=1;i<100;i++)begin :add100
add_1bit add0(
.a(a[i]),
.b(b[i]),
.cin(cout[i-1]),
.sum(sum[i]),
.cout(cout[i]),
);
end
endgenerate
endmodule
module add_1bit(
input a,
input b,
input cin,
output sum,
output cout
);
assign {cout,sum} = a+b+cin;
endmodule
其他大佬的:
module top_module(
input [99:0] a, b,
input cin,
output reg [99:0] cout,
output reg [99:0] sum );
integer i;
always @(*) begin
cout[0] = (a[0] & b[0]) | (a[0] & cin) | (b[0] & cin);
sum[0] = a[0] ^ b[0] ^ cin;
for(i=1;i<100;i=i+1) begin
sum[i] = a[i] ^ b[i] ^ cout[i-1];
cout[i] = (a[i] & b[i]) | (a[i] & cout[i-1]) | (b[i] & cout[i-1]);
end
end
endmodule
module top_module(
input [99:0] a, b,
input cin,
output reg [99:0] cout,
output reg [99:0] sum );
integer i;
always @(*) begin
{cout[0],sum[0]} = a[0] + b[0] + cin;
for(i=1;i<100;i=i+1) begin
{cout[i],sum[i]} = a[i] + b[i] + cout[i-1];
end
end
endmodule
7.Generate for-loop:100-digit BCD adder
Practice:Instantiate 100 copies of bcd_fadd to create a 100-digit BCD ripple-carry adder. Your adder should add two 100-digit BCD numbers (packed into 400-bit vectors) and a carry-in to produce a 100-digit sum and carry out.
翻译:得到一个名为bcd_fadd的BCD一位数加法器,它将两个BCD数字和进位相加,并生成一个和和进位。实例化100个bcd_fadd副本,创建100位BCD进位加法器,该加法器应包含两个 100bit 的 BCD 码(400bit 的矢量)和一个 cin, 输出产生 sum 和 cout。。
module bcd_fadd (
input [3:0] a,
input [3:0] b,
input cin,
output cout,
output [3:0] sum );
Solution(不唯一,仅供参考):
module top_module(
input [399:0] a, b,
input cin,
output cout,
output [399:0] sum );
wire [99:0] cout1;
bcd_fadd bcd_fadd0(
.a(a[3:0]),
.b(b[3:0]),
.cin(cin),
.cout(cout1[0]),
.sum(sum[3:0])
);
generate
genvar i;
for(i=1;i<100;i++)begin:bcd_fadd1
bcd_fadd bcd_fadd99(
.a(a[4*i+3:4*i]),
.b(b[4*i+3:4*i]),
.cin(cout1[i-1]),
.cout(cout1[i]),
.sum(sum[4*i+3:4*i])
);
end
endgenerate
assign cout = cout1[99];
endmodule
总结
1、 generate语句的用法:
生成语句可以动态的生成 verilog 代码,当对矢量中的多个位进行重复操作时,或者当进行多个模块的实例引用的重复操作时,使用生成语句能大大简化程序的编写过程。generate 语句有 generate-for、generate-if、generate-case 三种语句。
generate-for语句
(1) 必须用 genvar 关键字定义 for 语句的变量。
(2)for 语句的内容必须加 begin 和 end (即使就一句)。
(3)for 语句必须有个名字,写在 begin 后面。
2、BCD加法器:是一种十进制加法器,通过用位宽为4的二进制式数据表示十进制数字0~9,且每一位最大表示到9,两个十进制操作数之和最大则为19。与二进制加法器不同的是,BCD加法器最少则需要9个input和5个output进行操作数的运算,因为每一个操作数,至少需要4位的二进制数去表示。
继续加油!!!!!