1 conditional
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//
wire [7:0] min1,min2;
// assign intermediate_result1 = compare? true: false;
assign min1=(a<b)?a:b;//min1 = min(a,b)
assign min2=(c<d)?c:d;//min2=min(c,d)
assign min=(min1<min2)?min1:min2;
endmodule
2 reduction
& 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]
These are unary operators that have only one operand (similar to the NOT operators ! and ~). 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
偶校验
module top_module (
input [7:0] in,
output parity);
assign parity=^in;
endmodule
3 gate 100
和上一篇差不多;
module top_module(
input [99:0] in,
output out_and,
output out_or,
output out_xor
);
assign out_or=| in;
assign out_and=∈
assign out_xor=^in;
endmodule
4 vector[100]
module top_module(
input [99:0] in,
output [99:0] out
);
integer i;
always@(*)
begin
for(i=0;i<100;i++)
out[i]=in[99-i];
end
endmodule
5Popcount255
官网答案呢就下面的。跳过去
module top_module (
input [254:0] in,
output reg [7:0] out
);
always @(*) begin // Combinational always block
out = 0;
for (int i=0;i<255;i++)
out = out + in[i];
end
endmodule
6 adder 100
hint 里面推荐了generate语句
摘抄下其他blog的介绍
generate生成语句可以动态的生成verilog代码,当对矢量中的多个位进行 重复操作 时,或者当进行多个模块的实例引用的重复操作时,或者根据参数的定义来确定程序中是否应该包含某段Verilog代码的时候,使用生成语句能大大简化程序的编写过程。
生成语句生成的实例范围,关键字generate-endgenerate用来指定该范围。生成实例可以是以下的一个或多个类型:
(1)模块;(2)用户定义原语;(3)门级语句;(4)连续赋值语句;(5)initial和always块。
generate语句有generate-for,generate-if,generate-case三种语句。
generate-for语句
(1) 必须有genvar关键字定义for语句的变量。
(2)for语句的内容必须加begin和end(即使就一句)。
(3)for语句必须有个名字。(begin 后)
module top_module(
input [99:0] a, b,
input cin,
output [99:0] cout,
output [99:0] sum );
generate
genvar i;
for(i=0;i<100;i++)begin:adder100
if(i!=0)
assign {cout[i],sum[i]} = a[i]+b[i]+cout[i-1];
else
assign{cout[i],sum[i]}=a[0]+b[0]+cin;
end
endgenerate
endmodule
7 bcdadder100
略
这篇博客介绍了Verilog语言在实现各种逻辑运算中的应用,包括条件选择(1conditional)、位操作(reduction)、与或非运算(gate100)、向量反转(vector[100])、计数(Popcount255)、加法器(adder100)以及BCD加法器(bcdadder100)。通过示例代码展示了如何用Verilog进行组合逻辑和时序逻辑的设计。
1720

被折叠的 条评论
为什么被折叠?



