参考链接:HDLBits导学
Problem 80 : D flip-flop (Dff)
问题:实现一个D触发器
解决:
module top_module (
input clk, // Clocks are used in sequential circuits
input d,
output reg q );//
// Use a clocked always block
// copy d to q at every positive edge of clk
// Clocked always blocks should use non-blocking assignments
always @(posedge clk) begin
q <= d;
end
endmodule
Problem 81 : D flip-flops (Dff8)
问题:实现8个D触发器
解决:
module top_module (
input clk,
input [7:0] d,
output [7:0] q
);
always @(posedge clk) begin
q <= d;
end
endmodule
Problem 82 : DFF with reset (Dff8r)
问题:实现8个带高电平同步复位的D触发器
解决:
module top_module (
input clk,
input reset, // Synchronous reset
input [7:0] d,
output [7:0] q
);
always @(posedge clk) begin
if(reset)
q <= 8'd0;
else
q <= d;
end
endmodule
Problem 83 : DFF with reset value (Dff8p)
问题:实现具有高电平同步复位的8个D触发器,同时复位值为0x34,下降沿触发
解决:
module top_module (
input clk,
input reset, // Synchronous reset
input [7:0] d,
output [7:0] q
);
always @(negedge clk) begin
if(reset)
q <= 8'h34;
else
q <= d;
end
endmodule
Problem 84 : DFF with asynchronous reset (Dff8ar)
问题:实现具有高电平异步复位的8个D触发器,上升沿触发
思路:可以在敏感列表中加入复位信号作为异步信号,使用or连接
解决:
module top_module (
input clk,
input areset, // active high asynchronous reset
input [7:0] d,
output [7:0] q
);
always @(posedge clk or posedge areset) begin
if(areset)
q <= 8'd0;
else
q <= d;
end
endmodule
Problem 85 : DFF with byte enable(Dff16e)
问题:本题中需要创建一个 16 路 D触发器。部分情况下,只需要多路触发器中的一部分触发器工作,此时可以通过 ena 使能端进行控制。使能端 ena 信号有效时,触发器在时钟上升沿工作。
byteena [1] 作为 d[15:8] 高位字节的使能端,byteena [0] 则控制 d 的低位字节。
resetn 为同步,低电平有效复位信号。
所有的触发器在时钟上升沿被触发
解决:
module top_module (
input clk,
input resetn,
input [1:0] byteena,
input [15:0] d,
output [15:0] q
);
always @(posedge clk) begin
if(!resetn)
q <= 16'd0;
else begin
case(byteena)
2'b00: q <= q;
2'b01: q[7:0] <= d[7:0];
2'b10: q[15:8] <= d[15:8];
2'b11: q <= d;
endcase
end
end
endmodule
Problem 86 : D latch(Exams/m2014 q4a)
概述:在ena有效的q等于a,无效的时候q保持,此时就生成了一个锁存器
问题:实现下面的电路(锁存器)
解决:
module top_module (
input d,
input ena,
output q);
assign q = ena ? d : q;
endmodule
Problem 87 : DFF (Exams/m2014 q4b)
问题:实现下面的电路
思路:从图中可以看出时钟是上升沿触发,复位信号是高电平的,代码中提示是异步的
解决:
module top_module (
input clk,
input d,
input ar, // asynchronous reset//异步复位
output q);
always @(posedge clk or posedge ar) begin
if(ar)
q <= 1'b0;
else
q <= d;
end
endmodule
Problem 88 : DFF (Exams/m2014 q4c)
问题:实现下图中的电路
思路:时钟上升沿有效,复位信号为高电平,且为同步信号
解决:
module top_module (
input clk,
input d,
input r, // synchronous reset
output q);
always @(posedge clk) begin
if(r)
q <= 1'b0;
else
q <= d;
end
endmodule
Problem 89 : DFF+gate(Exams/m2014 q4d)
问题:实现如下电路
解决:
module top_module (
input clk,
input in,
output out);
wire d = in ^ out;
always @(posedge clk)begin
out <= d;
end
endmodule
Problem 90 Mux and DFF
问题:考虑如下电路:
我们用3个包含触发器和多路选择器的子模块来实现图中电路。题目要求我们写出包含一个触发器和一个多路选择器的子模块
解决:
module top_module (
input clk,
input L,
input r_in,
input q_in,
output reg Q);
wire d = L ? r_in : q_in;
always @(posedge clk) begin
Q <= d;
end
endmodule
Problem 91 Mux and DFF
问题:考虑如下电路:
实现包含选择器和触发器的部分
解决:
module top_module (
input clk,
input w, R, E, L,
output Q
);
wire d = L ? R : (E ? w : Q) ;
always @(posedge clk) begin
Q <= d;
end
endmodule
Problem 92 DFFS and gates
问题:如下图所示的状态机,假设D触发器在状态机启动之前初始化为0,实现该电路:
解决:
module top_module (
input clk,
input x,
output z
);
reg q1,q2,q3;
wire d1 = x ^ q1;
wire d2 = x & !q2;
wire d3 = x | !q3;
assign z = ~(q1 | q2 | q3);
always @(posedge clk) begin
q1 <= d1;
q2 <= d2;
q3 <= d3;
end
endmodule
Problem 93 Create circuit from truth table
问题:JK触发器的真值表如下图所示,仅使用D触发器和门电路来实现该JK触发器。其中Qold是D触发器在时钟上升沿之前的输出
解决:
module top_module (
input clk,
input j,
input k,
output Q);
always @(posedge clk) begin
case({j,k})
2'b00: Q <= Q;
2'b01: Q <= 1'b0;
2'b10: Q <= 1'b1;
2'b11: Q <= !Q;
endcase
end
endmodule
Problem 94 Detect an Edge
问题:对于每个8bit的变量,检测这些信号什么时候从0变为1(类似检测上升沿),输出应该在0到1 变化之后才有值。
下图给我们展示了输入in[1]和输出pedge[1]的时序关系图:
思路:边沿检测十分常用,保存信号在时钟沿前后的两个值,在时钟沿到来时,就可以通过两者的不同来检测是什么边沿信号
解决:
module top_module (
input clk,
input [7:0] in,
output [7:0] pedge
);
wire [7:0] in_1,in_2;
assign pedge = in_1 & ~in_2;
always @(posedge clk) begin
//电路是同时进行的 下面这种写法不会出现 in_2 = in_1 = in;
//in_1 和 in_2相当于是有效时钟沿到来时输入信号的当前状态和上一个时钟沿的状态
in_1 <= in;
in_2 <= in_1;
end
endmodule
Problem 95 Detect both edges
问题:在一个8bit的变量中,从一个周期到另一个周期期间,检测输入信号变化。即上升沿变化或下降沿变化。输出应在0变为1后产生。
如下图所示为输入与输出的时序关系
解决:
module top_module (
input clk,
input [7:0] in,
output [7:0] anyedge
);
wire [7:0] in_1,in_2;
assign anyedge = (in_1 & ~in_2) | (~in_1 & in_2);
//大佬写法
//assign anyedge = in_1 ^ in_2;
always @(posedge clk) begin
in_1 <= in;
in_2 <= in_1;
end
endmodule
Problem 96 Edge capture register
问题:对于32bit中的每一个变量,我们需要捕获输入信号的下降沿。其中捕获的意思就是说在寄存器复位之前,输出一直保持为 ‘1’ 。
每一个输出bit类似SR触发器:输出信号从1变0发生时会保持一个周期。输出会在时钟上升沿和reset信号为高时复位。如果上述事件在同一时间发生,reset有更高的优先级。在下图所示的最后4个周期内,reset信号比set信号早一个周期,所以没有冲突发生
思路:这个题对输入信号的上升沿检测有要求,如果仿照我上面的写法,会造成输出延迟一个时钟周期,所以我这里选择了直接使用in信号来作为之前的in_1信号。同时,认真读题看图都是关键。
捕获下降沿 捕获到一次上升沿后,只要复位信号是无效的,输出就要一直保持是 1,想了很久想到了使用 或‘|’
解决:
module top_module (
input clk,
input reset,
input [31:0] in,
output [31:0] out
);
wire [31:0] in_1;
wire [31:0]pedge = ~in & in_1;
always @(posedge clk) begin
in_1 <= in;
if(reset)
out <= 32'd0;
else
out <= out | pedge;
end
endmodule
Probelm 97 Dual-edge triggered flip-flop
问题:设计一个双边沿检测的触发器,时序如下图所示
提示:您无法在 FPGA 上创建双边沿触发触发器。但是您可以创建正沿触发和负沿触发触发器
解决:
module top_module (
input clk,
input d,
output q
);
wire q_p,q_n;
assign q = clk ? q_p : q_n;
always @(posedge clk) begin
q_p <= d;
end
always @(negedge clk) begin
q_n <= d;
end
endmodule