HDLBits练习汇总-05-Verilog语言--拓展部分
三目运算符
Verilog 有一个三元条件运算符 ( ? : ) 很像 C:
(condition ? if_true : if_false)
这可用于根据一行上的条件(多路复用器)选择两个值之一,而无需在组合 always 块中使用 if-then。
Examples:
(0 ? 3 : 5) // This is 5 because the condition is false.
(sel ? b : a) // A 2-to-1 multiplexer between a and b selected by sel.
always @(posedge clk) // A T-flip-flop.
q <= toggle ? ~q : q;
always @(*) // State transition logic for a one-input FSM
case (state)
A: next = w ? B : A;
B: next = w ? A : B;
endcase
assign out = ena ? q : 1'bz; // A tri-state buffer
((sel[1:0] == 2'h0) ? a : // A 3-to-1 mux
(sel[1:0] == 2'h1) ? b :
c )
练习
给定四个无符号数,找出最小值。无符号数可以与标准比较运算符 (a < b) 进行比较。使用条件运算符创建两路最小电路,然后组合其中的一些来创建 4 路最小电路。可能需要一些用于中间结果的线向量。
Module Declaration
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);
答案
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//
wire [7:0] out_t0;
wire [7:0] out_t1;
assign out_t0 = (a<b)?a:b;
assign out_t1 = (c<d)?c:d;
assign min = (out_t0<out_t1) ? out_t0:out_t1;
endmodule
归约运算符
已经熟悉两个值之间的按位运算,例如a & b或a ^ b。有时,您想创建一个对一个向量的所有位进行操作的宽门,例如(a[0] & a[1] & a[2] & a[3] … ),如果向量很长。
的减少运营商可以做AND,OR,以及向量的比特的XOR,产生输出的一个比特:
& a[3:0] // AND: a[3]&a[2]&a[1]&a[0]. Equivalent to (a[3:0] == 4'hf)
| b[3:0] // OR: b[3]|b[2]|b[1]|b[0]. Equivalent to (b[3:0] != 4'h0)
^ c[2:0] // XOR: c[2]^c[1]^c[0]
这些是只有一个操作数的一元操作符(类似于NOT操作符!和~ )。你也可以将这些门的输出倒转来创建NAND、NOR和XNOR门,例如(~& d[7:0])。
练习
当通过不完善的信道传输数据时,奇偶校验通常用作检测错误的简单方法。创建一个电路来计算 8 位字节的奇偶校验位(这将向字节添加第 9 位)。我们将使用“偶数”奇偶校验,其中奇偶校验位只是所有 8 个数据位的异或。
Module Declaration
module top_module (
input [7:0] in,
output parity);
答案
module top_module (
input [7:0] in,
output parity);
assign parity = ^ in[7:0];
endmodule
归约练习
问题
在 [99:0] 中构建一个具有 100 个输入的组合电路。
有3个输出:
- out_and:100 输入与门的输出。
- out_or:100 输入或门的输出。
- out_xor:100 输入异或门的输出。
Module Declaration
module top_module(
input [99:0] in,
output out_and,
output out_or,
output out_xor
);
答案
module top_module(
input [99:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_and = &in[99:0];
assign out_or = |in[99:0];
assign out_xor = ^in[99:0];
endmodule
反转
问题
给定一个 100 位的输入向量 [99:0],反转其位顺序。
Module Declaration
module top_module(
input [99:0] in,
output [99:0] out
);
答案
module top_module(
input [99:0] in,
output [99:0] out
);
genvar i ;
generate
for (i = 0 ; i <= 99 ; i = i+1)
begin : loop0
assign out[i]=in[99-i];
end
endgenerate
endmodule
人口计数电路
问题
“人口计数”电路计算输入向量中“1”的数量。为 255 位输入向量构建人口计数电路。
Module Declaration
module top_module(
input [254:0] in,
output [7:0] out );
答案
module top_module(
input [254:0] in,
output [7:0] out );
int i;
always @ (*)begin
out = 8'b0000_0000;
for (i=0; i<=254; i++)begin
if(in[i] == 1'b1)
out = out + 1'b1;
else
out = out + 1'b0;
end
end
endmodule
100位加法器
问题
通过实例化 100 个全加器来创建一个 100 位二进制纹波进位加法器。加法器将两个 100 位数字和一个进位相加,以产生 100 位总和并进位。为了鼓励您实际实例化全加器,还要输出纹波进位加法器中每个全加器的进位。cout[99] 是最后一个全加器的最后一个进位,也是你经常看到的进位。
Module Declaration
module top_module(
input [99:0] a, b,
input cin,
output [99:0] cout,
output [99:0] sum );
答案
module top_module(
input [99:0] a, b,
input cin,
output [99:0] cout,
output [99:0] sum );
genvar i ;
generate
for (i = 0 ; i <= 99 ; i = i+1)
begin : loop0
if(i==0)begin
assign {cout[i],sum[i]} = a[i]+b[i]+cin;
end
else begin
assign {cout[i],sum[i]} = a[i]+b[i]+cout[i-1];
end
end
endgenerate
endmodule
100位BCD加法器
问题
为您提供了一个名为bcd_fadd的 BCD 一位加法器,它将两个 BCD 数字和进位相加,并产生一个总和和进位。
module bcd_fadd {
input [3:0] a,
input [3:0] b,
input cin,
output cout,
output [3:0] sum );
实例化 100 个bcd_fadd副本以创建 100 位 BCD 纹波进位加法器。您的加法器应将两个 100 位 BCD 数(打包成 400 位向量)和一个进位相加,以产生 100 位总和并执行。
Module Declaration
module top_module(
input [399:0] a, b,
input cin,
output cout,
output [399:0] sum );
答案
module top_module(
input [399:0] a, b,
input cin,
output cout,
output [399:0] sum );
wire [99:0] cout_in;
genvar i ;
generate
for (i = 0 ; i <= 99 ; i = i+1)
begin : loop0
if(i==0)begin
bcd_fadd u_bcd_fadd(
.a (a[4 * i + 3: 4 * i] ),
.b (b[4 * i + 3: 4 * i] ),
.cin (cin ),
.cout(cout_in[i]),
.sum (sum[4 * i + 3: 4 * i])
);
end
else begin
bcd_fadd ui_bcd_fadd(
.a (a[4 * i + 3: 4 * i] ),
.b (b[4 * i + 3: 4 * i] ),
.cin (cout_in[i-1] ),
.cout(cout_in[i]),
.sum (sum[4 * i + 3: 4 * i])
);
end
end
endgenerate
assign cout = cout_in[99];
endmodule
相关文章
- 【测开技能】Java语言系列(十七)数组操作--遍历与排序
- 2022-10-28:以下go语言代码输出什么?A:false false;B:true false;C:true true;D:false true。 pac
- Golang语言情怀--第98期 区块链技术-以太坊公链合约部署-第4节:MetaMask钱包连接到本地环境
- Golang语言情怀--第100期 区块链技术-以太坊公链NFT图片去中心化存储-第1节:IPFS存储方案
- Golang语言情怀--第103期 区块链技术-关于区块链中的智能合约:您想知道的一切
- ACL 2022 杰出论文:华为&港大提出SOTA预训练语言模型量化压缩方法
- c语言程序设计 向艳主编 第5章课后习题答案-C语言程序设计(第2版)-- 课后题答案
- 轻量型模型比肩千亿大模型,周明团队提出中文语言模型孟子,刷新CLUE纪录
- JAVA语言实现二叉树的层次遍历的非递归算法及递归算法详解编程语言
- 数据库深度探索C语言与Redis数据库(c语言redis)
- xmlLinux C 语言解析 XML 数据的实现方式(linuxc解析)
- 从C语言入门到Linux编程(c语言入门linux)
- 精通Linux: 语言配置指南(linux配置语言)
- Go语言操作Oracle轻松实现数据库编程(go语言访问oracle)
- 发挥语言的威力--融合PHP与ASP