solve merge confict
This commit is contained in:
commit
2ee46782df
|
@ -2,4 +2,7 @@
|
|||
This repository is used to introduce to some projects or modules bases on the mainly lanuage--verilog.
|
||||
# 1 slove the every time git push need to input the username and password.
|
||||
git config --global credential.helper store
|
||||
//then git push again,and next will not need to input the username and password.
|
||||
//then git push again,and next will not need to input the username and password.
|
||||
=======
|
||||
# 2 Useful references
|
||||
[1] https://github.com/sin-x/FPGA/tree/master
|
||||
|
|
|
@ -0,0 +1,155 @@
|
|||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :11—29
|
||||
//--Module : edge_check
|
||||
//--Function: check a edge including: posedge,negedge,both clk
|
||||
//=======================================
|
||||
module edge_check (
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
input wire din_vld,
|
||||
input wire din,
|
||||
|
||||
output wire edge_pos,
|
||||
output wire edge_neg,
|
||||
output wire edge_both
|
||||
);
|
||||
//==========================< 信号 >=========================
|
||||
reg din_r;
|
||||
|
||||
//=========================================================
|
||||
//-- din_r: flip_flop
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
din_r <= 1'b0;
|
||||
end else begin
|
||||
din_r <= din;
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- edge_pos,edge_neg,edge_both
|
||||
//=========================================================
|
||||
assign edge_pos = din_vld & din & ~din_r;
|
||||
assign edge_neg = din_vld & ~din & din_r;
|
||||
assign edge_both= din_vld & edge_pos | edge_neg;
|
||||
|
||||
endmodule
|
||||
|
||||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :11—29
|
||||
//--Module : sequence_modulo3_detector
|
||||
//--Function: Detect if the input signal sequence din is a multiple of 3
|
||||
//=======================================
|
||||
module seq_mod3_detec(
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
input wire din_vld,
|
||||
input wire din,
|
||||
|
||||
output wire detect
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
localparam IDLE = 3'b000;
|
||||
localparam S0 = 3'b001;
|
||||
localparam S1 = 3'b010;
|
||||
localparam S2 = 3'b100;
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg[4-1:0] sta_cur;
|
||||
reg[4-1:0] sta_nxt;
|
||||
|
||||
//=========================================================
|
||||
//-- FSM-1: state transiate
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
sta_cur <= IDLE;
|
||||
end else begin
|
||||
sta_cur <= sta_nxt;
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- FSM-2: state jump condition
|
||||
//=========================================================
|
||||
always @(*) begin
|
||||
case (sta_cur)
|
||||
IDLE: begin
|
||||
if(!din_vld) begin
|
||||
sta_nxt = IDLE;
|
||||
end else begin
|
||||
if(din) begin
|
||||
sta_nxt = S0;
|
||||
end else begin
|
||||
sta_nxt = S1;
|
||||
end
|
||||
end
|
||||
end
|
||||
S0: begin
|
||||
if(!din_vld) begin
|
||||
sta_nxt = S0;
|
||||
end else begin
|
||||
if (din) begin
|
||||
sta_nxt = S1;
|
||||
end else begin
|
||||
sta_nxt = S0;
|
||||
end
|
||||
end
|
||||
end
|
||||
S1 : begin
|
||||
if(!din_vld) begin
|
||||
sta_nxt = S1;
|
||||
end else begin
|
||||
if(din) begin
|
||||
sta_nxt = S0;
|
||||
end else begin
|
||||
sta_nxt = S2;
|
||||
end
|
||||
end
|
||||
end
|
||||
S2: begin
|
||||
if(!din_vld) begin
|
||||
sta_nxt = IDLE;
|
||||
end else begin
|
||||
if(din) begin
|
||||
sta_nxt = S2;
|
||||
end else begin
|
||||
sta_nxt = S1;
|
||||
end
|
||||
end
|
||||
end
|
||||
default: sta_nxt = IDLE;
|
||||
endcase
|
||||
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- FSM-3: state action
|
||||
//=========================================================
|
||||
reg mod3_res;
|
||||
|
||||
always @(posedge clk) begin
|
||||
if(!rst_n)begin
|
||||
mod3_res <= 1'b0;
|
||||
end else begin
|
||||
case (sta_cur)
|
||||
IDLE: mod3_res <= 1'b0;
|
||||
S0: mod3_res <= 1'b1;
|
||||
S1: mod3_res <= 1'b0;
|
||||
S2: mod3_res <= 1'b0;
|
||||
default: mod3_res <= 1'b0;
|
||||
endcase
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- detect
|
||||
//=========================================================
|
||||
assign detect = mod3_res;
|
||||
|
||||
endmodule
|
|
@ -0,0 +1,223 @@
|
|||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :10—28
|
||||
//--Module : frequency_divide_even
|
||||
//--Function: to get the divided frenquency
|
||||
//=======================================
|
||||
module frequency_divide_even#(
|
||||
//==========================< 参数 >=========================
|
||||
parameter DIVISOR = 4
|
||||
)(
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
output wire clk_out
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg [4 -1:0] cnt;
|
||||
reg clk_div_r;
|
||||
|
||||
//=========================================================
|
||||
//-- 计数器
|
||||
//=========================================================
|
||||
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
cnt <= 4'b0;
|
||||
end else begin
|
||||
if(cnt==(DIVISOR/2-1)) begin
|
||||
cnt <= 4'b0;
|
||||
end else begin
|
||||
cnt <= cnt + 1'b1;;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- clk_div_r
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
clk_div_r <= 1'b0;
|
||||
end else begin
|
||||
if(cnt==(DIVISOR/2-1)) begin
|
||||
clk_div_r <= ~clk_div_r;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assign clk_out = clk_div_r;
|
||||
|
||||
endmodule
|
||||
|
||||
|
||||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :10—28
|
||||
//--Module : frequency_divide_odd
|
||||
//--Function: to get the divided frenquency
|
||||
//=======================================
|
||||
|
||||
module frequency_divide_odd#(
|
||||
//==========================< 参数 >=========================
|
||||
parameter DIVISOR = 3
|
||||
)(
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
output wire clk_out_not_50,
|
||||
output wire clk_out_is_50
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg [4 -1:0] cnt_pos, cnt_neg;
|
||||
reg clk_p, clk_n;
|
||||
|
||||
|
||||
//=========================================================
|
||||
//-- cnt_pos, cnt_neg
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
cnt_pos <= 'b0;
|
||||
end else begin
|
||||
if (cnt_pos==DIVISOR-1) begin
|
||||
cnt_pos <= 4'b0;
|
||||
end else begin
|
||||
cnt_pos <= cnt_pos + 1'b1;
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
always @(negedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
cnt_neg <= 'b0;
|
||||
end else begin
|
||||
if (cnt_neg==DIVISOR-1) begin
|
||||
cnt_neg <= 4'b0;
|
||||
end else begin
|
||||
cnt_neg <= cnt_neg + 1'b1;
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- clk_p, clk_n
|
||||
//=========================================================
|
||||
//----上升沿触发
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
clk_p <= 1'b0;
|
||||
end else begin
|
||||
if(cnt_pos==(DIVISOR-1)/2) begin
|
||||
clk_p <= ~clk_p;
|
||||
end else if(cnt_pos==0) begin
|
||||
clk_p <= ~clk_p;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//----下降沿触发
|
||||
always @(negedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
clk_n <= 1'b0;
|
||||
end else begin
|
||||
if (cnt_neg==(DIVISOR-1)/2) begin
|
||||
clk_n <= ~clk_n;
|
||||
end else if (cnt_neg==0) begin
|
||||
clk_n <= ~clk_n;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//----Output
|
||||
assign clk_out_not_50 = clk_p;
|
||||
assign clk_out_is_50 = clk_p | clk_n;
|
||||
|
||||
endmodule
|
||||
|
||||
|
||||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :10—28
|
||||
//--Module : frequency_divide_half
|
||||
//--Function: to get the divided frenquency
|
||||
//=======================================
|
||||
module frequency_divide_half #(
|
||||
//==========================< 参数 >=========================
|
||||
parameter HALF = 3.5
|
||||
)(
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
output wire clk_div
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
//localparam DIVISOR = 2 * HALF;
|
||||
localparam DIVISOR = 7;
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg [4 -1:0] cnt;
|
||||
reg clk_p,clk_n;
|
||||
|
||||
//=========================================================
|
||||
//-- cnt
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
cnt <= 1'b0;
|
||||
end else begin
|
||||
if (cnt==DIVISOR-1) begin
|
||||
cnt <= 'b0;
|
||||
end else begin
|
||||
cnt <= cnt + 1'b1;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- clk_p : clk_p will be traversed when cnt is 0 and 4
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
clk_p <= 1'b0;
|
||||
end else begin
|
||||
if (cnt==0) begin
|
||||
clk_p <= 1;
|
||||
end else if (cnt==(DIVISOR/2)+1) begin //cnt will tranverse when 4
|
||||
clk_p <= 1;
|
||||
end else begin
|
||||
clk_p <= 0;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- clk_n : clk_n will be traversed when cnt is 1 and 4
|
||||
//=========================================================
|
||||
always @(negedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
clk_n <= 1'b0;
|
||||
end else begin
|
||||
if(cnt==1) begin
|
||||
clk_n <= 1;
|
||||
end else if(cnt==(DIVISOR)/2 +1) begin
|
||||
clk_n <= 1;
|
||||
end else begin
|
||||
clk_n <= 0;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- clk_div_half
|
||||
//=========================================================
|
||||
assign clk_div = clk_p | clk_n;
|
||||
|
||||
endmodule
|
|
@ -0,0 +1,143 @@
|
|||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :10—25
|
||||
//--Module : sequence_check_det
|
||||
//--Function: check a sequence using shift_reg
|
||||
//=======================================
|
||||
module sequen_check_det(
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
input wire din,
|
||||
output reg det
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
localparam SEQUENCE_DET = 8'b1100_1101;
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg [8 -1:0] shift_data;
|
||||
|
||||
//=========================================================
|
||||
//-- shift_data
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
shift_data <= 8'b0000_0000;
|
||||
end else begin
|
||||
shift_data <= {shift_data[6:0],din};
|
||||
end
|
||||
end
|
||||
|
||||
assign det = (shift_data==SEQUENCE_DET) ? 1'b1 : 1'b0;
|
||||
|
||||
endmodule
|
||||
|
||||
|
||||
//--way_2
|
||||
|
||||
//=======================================
|
||||
//--Author : colonel
|
||||
//--Date :10—25
|
||||
//--Module : sequence_check_det
|
||||
//--Function: check a sequence using FSM
|
||||
//=======================================
|
||||
module sequen_check_det_fsm(
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
input wire din,
|
||||
output wire det
|
||||
);
|
||||
//==========================< 参数 >==========================
|
||||
localparam SEQUENCE_DET = 4'b1011;
|
||||
localparam IDLE = 4'b0000;
|
||||
localparam S1 = 4'b0001;
|
||||
localparam S2 = 4'b0010;
|
||||
localparam S3 = 4'b0100;
|
||||
localparam S4 = 4'b1000;
|
||||
|
||||
//==========================< 信号 >==========================
|
||||
reg [3:0] sta_cur;
|
||||
reg [3:0] sta_nxt;
|
||||
|
||||
//===========================================================
|
||||
//--FSM-1:fsm transition
|
||||
//===========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
sta_cur <= IDLE;
|
||||
end else begin
|
||||
sta_cur <= sta_nxt;
|
||||
end
|
||||
end
|
||||
|
||||
//===========================================================
|
||||
//--FSM-2:fsm jumb condition
|
||||
//===========================================================
|
||||
always @(*) begin
|
||||
case (sta_cur)
|
||||
IDLE: begin
|
||||
if(din==1'b1) begin
|
||||
sta_nxt <= S1;
|
||||
end else begin
|
||||
sta_nxt <= IDLE;
|
||||
end
|
||||
end
|
||||
|
||||
S1: begin
|
||||
if(din==1'b0) begin
|
||||
sta_nxt <= S2;
|
||||
end else begin
|
||||
sta_nxt <= S1;
|
||||
end
|
||||
end
|
||||
|
||||
S2: begin
|
||||
if(din==1'b1) begin
|
||||
sta_nxt <= S3;
|
||||
end else begin
|
||||
sta_nxt <= IDLE;
|
||||
end
|
||||
end
|
||||
|
||||
S3: begin
|
||||
if(din==1'b1) begin
|
||||
sta_nxt <= S4;
|
||||
end else begin
|
||||
sta_nxt <= S2;
|
||||
end
|
||||
end
|
||||
|
||||
S4: begin
|
||||
if(din==1'b1) begin
|
||||
sta_nxt <= S1;
|
||||
end else begin
|
||||
sta_nxt <= S2;
|
||||
end
|
||||
end
|
||||
default: begin
|
||||
sta_nxt <= IDLE;
|
||||
end
|
||||
endcase
|
||||
end
|
||||
|
||||
//===========================================================
|
||||
//--FSM-3:fsm action
|
||||
//--det logic
|
||||
//===========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
det <= 1'b0;
|
||||
end else begin
|
||||
case (sta_cur)
|
||||
IDLE: det <= 1'b0;
|
||||
S1 : det <= 1'b0;
|
||||
S2 : det <= 1'b0;
|
||||
S3 : det <= 1'b0;
|
||||
S4 : det <= 1'b1;
|
||||
default: det <= 1'b0;
|
||||
endcase
|
||||
end
|
||||
end
|
||||
|
||||
endmodule
|
|
@ -0,0 +1,177 @@
|
|||
//==========================================================
|
||||
//--Author : colonel
|
||||
//--Date : 10-31
|
||||
//--Module : sync_1bit_from_slow_2_fast
|
||||
//--Function: sync 1bit from slow clk to fast clk
|
||||
//==========================================================
|
||||
|
||||
module sync_1bit_from_slow_2_fast(
|
||||
//==========================< 端口 >=========================
|
||||
input wire slow_clk,
|
||||
input wire rst_n,
|
||||
input wire din,
|
||||
input wire fast_clk,
|
||||
output wire sync_dout
|
||||
);
|
||||
//==========================< 信号 >=========================
|
||||
reg din_ff0,din_ff1,din_ff2;
|
||||
|
||||
//=========================================================
|
||||
//-- din_ff0 : sync from slow_clk
|
||||
//=========================================================
|
||||
always @(posedge slow_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
din_ff0 <= 1'b0;
|
||||
end else begin
|
||||
din_ff0 <= din;
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- din_ff1 : sync from fast_clk
|
||||
//=========================================================
|
||||
always @(posedge fast_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
din_ff1 <= 0;
|
||||
din_ff2 <= 0;
|
||||
end else begin
|
||||
din_ff1 <= din_ff0;
|
||||
din_ff2 <= din_ff1;
|
||||
end
|
||||
end
|
||||
|
||||
assign sync_dout = din_ff2;
|
||||
|
||||
endmodule
|
||||
|
||||
//==========================================================
|
||||
//--Author : colonel
|
||||
//--Date : 10-31
|
||||
//--Module : sync_1bit_from_fast_2_slow_pulse_widen
|
||||
//--Function: sync 1bit from fast clk to slow clk
|
||||
//==========================================================
|
||||
module sync_1bit_from_fast_2_slow_pulse_widen(
|
||||
//==========================< 端口 >=========================
|
||||
input wire fast_clk,
|
||||
input wire rst_n,
|
||||
input wire din,
|
||||
input wire slow_clk,
|
||||
output wire dout
|
||||
);
|
||||
//==========================< 信号 >=========================
|
||||
reg din_fast_r;
|
||||
|
||||
//=========================================================
|
||||
//-- din_fast_r: 将脉冲信号在快时钟域展平为电平信号。即展宽脉冲信号。在两次脉冲信号之间为电平信号
|
||||
//=========================================================
|
||||
always @(posedge fast_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
din_fast_r <= 'b0;
|
||||
end else begin
|
||||
din_fast_r <= din ? (~din_fast_r) : din_fast_r;
|
||||
end
|
||||
end
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg data_slow_ff1;
|
||||
reg data_slow_ff2;
|
||||
reg data_slow_ff3;
|
||||
|
||||
//=========================================================
|
||||
//-- data_slow_ff1/2/3: 将展宽的脉冲信号在慢时钟域打三拍,并检测边沿
|
||||
//=========================================================
|
||||
always @(posedge slow_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
data_slow_ff1 <= 'b0;
|
||||
data_slow_ff2 <= 'b0;
|
||||
data_slow_ff3 <= 'b0;
|
||||
end else begin
|
||||
data_slow_ff1 <= din_fast_r;
|
||||
data_slow_ff2 <= data_slow_ff1;
|
||||
data_slow_ff3 <= data_slow_ff2;
|
||||
end
|
||||
end
|
||||
|
||||
assign dout = data_slow_ff3 ^ data_slow_ff2;
|
||||
|
||||
endmodule
|
||||
|
||||
|
||||
//==========================================================
|
||||
//--Author : colonel
|
||||
//--Date : 10-31
|
||||
//--Module : sync_1bit_from_fast_2_slow_handshake
|
||||
//--Function: sync 1bit from fast clk to slow clk
|
||||
//==========================================================
|
||||
|
||||
module sync_1bit_from_fast_2_slow_handshake(
|
||||
//==========================< 端口 >=========================
|
||||
input wire fast_clk,
|
||||
input wire rst_n,
|
||||
input wire din_pulse,
|
||||
input wire slow_clk,
|
||||
output wire dst_pulse
|
||||
);
|
||||
//==========================< 信号 >=========================
|
||||
reg src_sync_req;
|
||||
reg src_sync_ack;
|
||||
reg src_req_ff1,src_req_ff2,src_req_ff3;
|
||||
|
||||
//=========================================================
|
||||
//-- src_sync_req:
|
||||
//=========================================================
|
||||
always @(posedge fast_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
src_sync_req <= 1'b0;
|
||||
end else begin
|
||||
if (din_pulse) begin
|
||||
src_sync_req <= 1'b1;
|
||||
end else if(src_sync_ack) begin
|
||||
src_sync_req <= 1'b0;
|
||||
end else begin
|
||||
src_sync_req <= src_sync_req;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- src_req_ff1/2
|
||||
//=========================================================
|
||||
always @(posedge slow_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
src_req_ff1 <= 'b0;
|
||||
src_req_ff2 <= 'b0;
|
||||
src_req_ff3 <= 'b0;
|
||||
end else begin
|
||||
src_req_ff1 <= src_sync_req;
|
||||
src_req_ff2 <= src_req_ff1;
|
||||
src_req_ff3 <= src_req_ff2;
|
||||
end
|
||||
end
|
||||
|
||||
wire dst_sync_ack = src_req_ff2;
|
||||
|
||||
//=========================================================
|
||||
//-- src_sync_ack
|
||||
//=========================================================
|
||||
reg dst_sync_ack_ff1;
|
||||
reg dst_sync_ack_ff2;
|
||||
|
||||
always @(posedge fast_clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
dst_sync_ack_ff1 <= 'b0;
|
||||
dst_sync_ack_ff2 <= 'b0;
|
||||
end else begin
|
||||
dst_sync_ack_ff1 <= dst_sync_ack;
|
||||
dst_sync_ack_ff2 <= dst_sync_ack_ff1;
|
||||
end
|
||||
end
|
||||
|
||||
assign src_sync_ack = dst_sync_ack_ff2;
|
||||
|
||||
//=========================================================
|
||||
//-- dst_pulse
|
||||
//=========================================================
|
||||
assign dst_pulse = src_req_ff3 & ! src_req_ff2;
|
||||
|
||||
endmodule
|
|
@ -1,6 +1,266 @@
|
|||
//====================================
|
||||
//==========================================================
|
||||
//--Author : colonel
|
||||
//--Date : 10-30
|
||||
//--Module : sync_fifo
|
||||
//--Function: the sync clk fifo logic
|
||||
//====================================
|
||||
//--Module : sync_fifo_cnt
|
||||
//--Function: the sync_fifo logic using cnt way
|
||||
//==========================================================
|
||||
|
||||
module sync_fifo_cnt_way #(
|
||||
//==========================< 参数 >=========================
|
||||
parameter DATA_WIDTH = 8,
|
||||
parameter FIFO_DEPTH = 16
|
||||
) (
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
input wire wr_en,
|
||||
input wire [DATA_WIDTH -1:0] write_data,
|
||||
output wire wr_full,
|
||||
|
||||
input wire rd_en,
|
||||
input wire [DATA_WIDTH -1:0] read_data,
|
||||
output wire rd_empty
|
||||
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
localparam DATA_DEPTH = $clog2(DATA_WIDTH);
|
||||
localparam FIFO_DEPTH_WIDTH = $clog2(FIFO_DEPTH);
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg [DATA_WIDTH -1:0] fifo_mem[FIFO_DEPTH];
|
||||
reg [FIFO_DEPTH_WIDTH -1:0] fifo_cnt;
|
||||
reg [FIFO_DEPTH_WIDTH -1:0] wr_ptr;
|
||||
reg [FIFO_DEPTH_WIDTH -1:0] rd_ptr;
|
||||
|
||||
|
||||
//=========================================================
|
||||
//-- write_data
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
fifo_mem[wr_ptr] <= 0;
|
||||
end else begin
|
||||
if (wr_en && !wr_full) begin
|
||||
fifo_mem[wr_ptr] <= write_data;
|
||||
end else begin
|
||||
fifo_mem[wr_ptr] <= fifo_mem[wr_ptr] ;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- wr_ptr
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
wr_ptr <= 0;
|
||||
end else begin
|
||||
if(!wr_full && wr_en) begin
|
||||
wr_ptr <= wr_ptr + 1'b1;
|
||||
end else begin
|
||||
wr_ptr <= wr_ptr;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- write_data
|
||||
//=========================================================
|
||||
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
fifo_mem[wr_ptr] <= 'b0;
|
||||
end else begin
|
||||
if(!wr_full && wr_en) begin
|
||||
fifo_mem[wr_ptr] <= write_data;
|
||||
end else begin
|
||||
fifo_mem[wr_ptr] <= fifo_mem[wr_ptr];
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- rd_ptr
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
rd_ptr <= 'b0;
|
||||
end else begin
|
||||
if (!rd_empty && rd_en) begin
|
||||
rd_ptr <= rd_ptr + 1'b1;
|
||||
end else begin
|
||||
rd_ptr <= rd_ptr;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- read_data
|
||||
//=========================================================
|
||||
reg [DATA_WIDTH -1:0] read_data_tmp;
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
read_data_tmp <= 'b0;
|
||||
end else begin
|
||||
if (!rd_empty && rd_en) begin
|
||||
read_data_tmp <= fifo_mem[rd_ptr];
|
||||
end else begin
|
||||
read_data_tmp <= read_data_tmp;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assign read_data = read_data_tmp;
|
||||
|
||||
//=========================================================
|
||||
//-- fifo_cnt
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
fifo_cnt <= 'b0;
|
||||
end else begin
|
||||
//--cases:
|
||||
if (wr_en && rd_en) begin
|
||||
fifo_cnt <= fifo_cnt;
|
||||
end else if (wr_en && !rd_en) begin
|
||||
fifo_cnt <= fifo_cnt + 1'b1;
|
||||
end else if (!wr_en && rd_en) begin
|
||||
fifo_cnt <= fifo_cnt - 1'b1;
|
||||
end else begin
|
||||
fifo_cnt <= fifo_cnt;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- wr_full,rd_empty
|
||||
//=========================================================
|
||||
assign wr_full = fifo_cnt == FIFO_DEPTH;
|
||||
assign rd_empty= fifo_cnt == 0;
|
||||
|
||||
endmodule
|
||||
|
||||
|
||||
//==========================================================
|
||||
//--Author : colonel
|
||||
//--Date : 10-31
|
||||
//--Module : sync_fifo_addy_way
|
||||
//--Function: the sync_fifo logic using addr way
|
||||
//==========================================================
|
||||
module sync_fifo_addr_way #(
|
||||
//==========================< 参数 >=========================
|
||||
parameter DATA_WIDTH = 8,
|
||||
parameter FIFO_DEPTH = 16
|
||||
) (
|
||||
//==========================< 端口 >=========================
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
input wire wr_en,
|
||||
input wire [DATA_WIDTH -1:0] write_data,
|
||||
output wire wr_full,
|
||||
|
||||
input wire rd_en,
|
||||
input wire [DATA_WIDTH -1:0] read_data,
|
||||
output wire rd_empty
|
||||
);
|
||||
//==========================< 参数 >=========================
|
||||
localparam DATA_DEPTH = $clog2(DATA_WIDTH);
|
||||
localparam FIFO_DEPTH_WIDTH = $clog2(FIFO_DEPTH);
|
||||
|
||||
//==========================< 信号 >=========================
|
||||
reg [DATA_WIDTH -1:0] fifo_mem[FIFO_DEPTH -1:0];
|
||||
reg [FIFO_DEPTH_WIDTH :0] wr_ptr;
|
||||
reg [FIFO_DEPTH_WIDTH :0] rd_ptr;
|
||||
|
||||
//=========================================================
|
||||
//-- write_data
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) begin
|
||||
fifo_mem[wr_ptr] <= 0;
|
||||
end else begin
|
||||
if (wr_en && !wr_full) begin
|
||||
fifo_mem[wr_ptr] <= write_data;
|
||||
end else begin
|
||||
fifo_mem[wr_ptr] <= fifo_mem[wr_ptr] ;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- wr_ptr
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
wr_ptr <= 0;
|
||||
end else begin
|
||||
if(!wr_full && wr_en && (wr_ptr[FIFO_DEPTH_WIDTH -1:0] < (FIFO_DEPTH -1))) begin
|
||||
wr_ptr <= wr_ptr + 1'b1;
|
||||
end else if(!wr_full && wr_en && (wr_ptr[FIFO_DEPTH_WIDTH -1:0] == (FIFO_DEPTH -1))) begin
|
||||
wr_ptr[FIFO_DEPTH_WIDTH] <= ~wr_ptr[FIFO_DEPTH_WIDTH];
|
||||
wr_ptr[FIFO_DEPTH_WIDTH -1:0] <= 0;
|
||||
end else begin
|
||||
wr_ptr <= wr_ptr;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- write_data
|
||||
//=========================================================
|
||||
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
fifo_mem[wr_ptr] <= 'b0;
|
||||
end else begin
|
||||
if(!wr_full && wr_en) begin
|
||||
fifo_mem[wr_ptr] <= write_data;
|
||||
end else begin
|
||||
fifo_mem[wr_ptr] <= fifo_mem[wr_ptr];
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- rd_ptr
|
||||
//=========================================================
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
rd_ptr <= 'b0;
|
||||
end else begin
|
||||
if (!rd_empty && rd_en && (rd_ptr[FIFO_DEPTH_WIDTH -1:0] < (FIFO_DEPTH-1))) begin
|
||||
rd_ptr <= rd_ptr + 1'b1;
|
||||
end else if (!rd_empty && rd_en && (rd_ptr[FIFO_DEPTH_WIDTH -1:0] == (FIFO_DEPTH-1))) begin
|
||||
rd_ptr[FIFO_DEPTH_WIDTH] <= ~rd_ptr[FIFO_DEPTH_WIDTH];
|
||||
rd_ptr[FIFO_DEPTH_WIDTH -1:0] <= 'b0;
|
||||
end else begin
|
||||
rd_ptr <= rd_ptr;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//=========================================================
|
||||
//-- read_data
|
||||
//=========================================================
|
||||
reg [DATA_WIDTH -1:0] read_data_tmp;
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if(!rst_n) begin
|
||||
read_data_tmp <= 'b0;
|
||||
end else begin
|
||||
if (!rd_empty && rd_en) begin
|
||||
read_data_tmp <= fifo_mem[rd_ptr];
|
||||
end else begin
|
||||
read_data_tmp <= read_data_tmp;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assign read_data = read_data_tmp;
|
||||
|
||||
//=========================================================
|
||||
//-- wr_full,rd_empty
|
||||
//=========================================================
|
||||
assign wr_full = (wr_ptr[FIFO_DEPTH_WIDTH] != rd_ptr[FIFO_DEPTH_WIDTH]) && (wr_ptr[FIFO_DEPTH_WIDTH -1:0] == rd_ptr[FIFO_DEPTH_WIDTH -1:0]);
|
||||
assign rd_empty= (wr_ptr[FIFO_DEPTH_WIDTH] == rd_ptr[FIFO_DEPTH_WIDTH]) && (wr_ptr[FIFO_DEPTH_WIDTH -1:0] == rd_ptr[FIFO_DEPTH_WIDTH -1:0]);
|
||||
|
||||
endmodule
|
Loading…
Reference in New Issue