Line 43... |
Line 43... |
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
//
|
//
|
// CVS Revision History
|
// CVS Revision History
|
//
|
//
|
// $Log: not supported by cvs2svn $
|
// $Log: not supported by cvs2svn $
|
|
// Revision 1.1.1.1 2002/12/20 16:39:21 mohor
|
|
// Initial
|
|
//
|
//
|
//
|
//
|
//
|
|
|
// synopsys translate_off
|
// synopsys translate_off
|
`include "timescale.v"
|
`include "timescale.v"
|
Line 114... |
Line 117... |
|
|
reg [8:0] clk_cnt;
|
reg [8:0] clk_cnt;
|
reg clk_en;
|
reg clk_en;
|
|
|
|
|
reg hard_sync_blocked;
|
reg sync_blocked;
|
reg resync_blocked;
|
|
reg monitored_bit;
|
reg monitored_bit;
|
|
|
|
|
/* Needed for edge detection */
|
/* Needed for edge detection */
|
always @ (posedge clk or posedge rst)
|
always @ (posedge clk or posedge rst)
|
begin
|
begin
|
if (rst)
|
if (rst)
|
monitored_bit <= 1'b0;
|
monitored_bit <= 1'b1;
|
else if(clk_en)
|
else if(clk_en)
|
monitored_bit <=#Tp rx;
|
monitored_bit <=#Tp rx;
|
end
|
end
|
|
|
|
|
reg sampled_bit;
|
reg sampled_bit;
|
reg [7:0] quant_cnt;
|
reg [7:0] quant_cnt;
|
|
reg [3:0] dodatek;
|
|
wire odstevek;
|
|
|
|
wire [7:0] difference;
|
|
|
|
|
|
reg rx_early;
|
|
|
|
|
|
|
/* Generating general enable signal that defines baud rate.
|
/* Generating general enable signal that defines baud rate.
|
Hard synchronization is done here. */
|
Hard synchronization is done here. */
|
wire [8:0] preset_cnt = (baud_r_presc + 1'b1)<<1; // (BRP+1)*2
|
wire [8:0] preset_cnt = (baud_r_presc + 1'b1)<<1; // (BRP+1)*2
|
wire hard_sync = idle & (~monitored_bit) & sampled_bit & (~hard_sync_blocked);
|
wire hard_sync = idle & (~rx) & sampled_bit & (~sync_blocked);
|
wire resync = (~idle) & (~monitored_bit) & sampled_bit & (~resync_blocked);
|
wire resync = (~idle) & (~rx) & sampled_bit & (~sync_blocked);
|
|
|
|
|
|
|
/* Generating enable signal (can clock) */
|
/* Generating enable signal (can clock) */
|
always @ (posedge clk or posedge rst)
|
always @ (posedge clk or posedge rst)
|
Line 162... |
Line 172... |
clk_en <=#Tp 1'b0;
|
clk_en <=#Tp 1'b0;
|
end
|
end
|
end
|
end
|
|
|
|
|
/* Hard Synchronization */
|
|
|
assign difference = time_segment1 + time_segment2 + 3 - quant_cnt;
|
|
|
|
|
|
|
|
/* Synchronization */
|
|
/*
|
|
always @ (posedge clk or posedge rst)
|
|
begin
|
|
if (rst)
|
|
begin
|
|
quant_cnt <=#Tp 0;
|
|
sync_blocked <=#Tp 1'b0;
|
|
dodatek <=#Tp 0;
|
|
odstevek <=#Tp 0;
|
|
end
|
|
else if (clk_en)
|
|
begin
|
|
if (hard_sync) // Hard synchronization
|
|
begin
|
|
quant_cnt <=#Tp 1;
|
|
sync_blocked <=#Tp 1'b1;
|
|
end
|
|
else if (resync) // resynchronization
|
|
begin
|
|
sync_blocked <=#Tp 1'b1;
|
|
if (quant_cnt == 0) // Right on time
|
|
quant_cnt <=#Tp quant_cnt + 1;
|
|
else if (rx_early) // Too early
|
|
begin
|
|
quant_cnt <=#Tp 1;
|
|
odstevek <=#Tp (difference > (sync_jump_width + 1))? (sync_jump_width + 1) : difference;
|
|
end
|
|
else // Too late // Take smaller (SJW : quant_cnt)
|
|
begin
|
|
dodatek <=#Tp (quant_cnt > (sync_jump_width + 1))? (sync_jump_width + 1) : quant_cnt;
|
|
quant_cnt <=#Tp quant_cnt + 1;
|
|
end
|
|
end
|
|
else if (quant_cnt == (time_segment1 + time_segment2 + 2 + dodatek))
|
|
// else if (quant_cnt == (time_segment1 + time_segment2 + 2 + dodatek - odstevek))
|
|
begin
|
|
quant_cnt <=#Tp 0;
|
|
sync_blocked <=#Tp 1'b0;
|
|
dodatek <=#Tp 0;
|
|
odstevek <=#Tp 0;
|
|
end
|
|
else
|
|
quant_cnt <=#Tp quant_cnt + 1;
|
|
end
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
reg sample_pulse;
|
|
// Sampling data
|
|
always @ (posedge clk or posedge rst)
|
|
begin
|
|
if (rst)
|
|
begin
|
|
sampled_bit <= 1;
|
|
sample_pulse <= 0;
|
|
end
|
|
else if (clk_en & (quant_cnt == (time_segment1 + 1 + dodatek)) & (~idle)) // (~idle) blocks sampling so hard sync works in all cases
|
|
begin
|
|
sampled_bit <=#Tp rx;
|
|
sample_pulse <=#Tp 1;
|
|
end
|
|
else
|
|
sample_pulse <=#Tp 0;
|
|
end
|
|
|
|
|
|
|
|
always @ (posedge clk or posedge rst)
|
|
begin
|
|
if (rst)
|
|
begin
|
|
rx_early <= 1'b0;
|
|
end
|
|
else if (clk_en & rx & (quant_cnt == (time_segment1 + 1)))
|
|
begin
|
|
rx_early <=#Tp 1'b1;
|
|
end
|
|
else if (clk_en & (quant_cnt == 0))
|
|
rx_early <=#Tp 1'b0;
|
|
end
|
|
|
|
|
|
*/
|
|
|
|
|
|
reg sync;
|
|
reg seg1;
|
|
reg seg2;
|
|
reg resync_latched;
|
|
|
|
|
|
wire go_sync = clk_en & (seg2 & (~resync) & ((quant_cnt == time_segment2)));
|
|
wire go_seg1 = clk_en & (sync | hard_sync | (resync & seg2 & odstevek) | (resync_latched & odstevek));
|
|
wire go_seg2 = clk_en & (seg1 & (quant_cnt == (time_segment1 + dodatek)));
|
|
|
|
|
always @ (posedge clk or posedge rst)
|
always @ (posedge clk or posedge rst)
|
begin
|
begin
|
if (rst)
|
if (rst)
|
begin
|
resync_latched <= 1'b0;
|
quant_cnt <=#Tp 0;
|
else if (resync & seg2 & (~odstevek))
|
hard_sync_blocked <=#Tp 1'b0;
|
resync_latched <=#Tp 1'b1;
|
|
else if (go_seg1)
|
|
resync_latched <= 1'b0;
|
end
|
end
|
else if (clk_en)
|
|
begin
|
|
if (hard_sync || (quant_cnt == (time_segment1 + time_segment2 + 2))) // Hard synchronization
|
|
|
|
|
always @ (posedge clk or posedge rst)
|
begin
|
begin
|
quant_cnt <=#Tp 0;
|
if (rst)
|
hard_sync_blocked <=#Tp hard_sync;
|
sync <= 1;
|
|
else if (go_sync)
|
|
sync <=#Tp 1'b1;
|
|
else if (go_seg1)
|
|
sync <=#Tp 1'b0;
|
end
|
end
|
else
|
|
|
|
|
always @ (posedge clk or posedge rst)
|
begin
|
begin
|
quant_cnt <=#Tp quant_cnt + 1;
|
if (rst)
|
end
|
seg1 <= 0;
|
end
|
else if (go_seg1)
|
|
seg1 <=#Tp 1'b1;
|
|
else if (go_seg2)
|
|
seg1 <=#Tp 1'b0;
|
end
|
end
|
|
|
|
|
/* Resynchronization */
|
|
always @ (posedge clk or posedge rst)
|
always @ (posedge clk or posedge rst)
|
begin
|
begin
|
if (rst)
|
if (rst)
|
begin
|
seg2 <= 0;
|
resync_blocked <=#Tp 1'b0;
|
else if (go_seg2)
|
|
seg2 <=#Tp 1'b1;
|
|
else if (go_sync | go_seg1)
|
|
seg2 <=#Tp 1'b0;
|
end
|
end
|
else if (clk_en)
|
|
begin
|
|
if (resync)
|
|
begin
|
|
if (quant_cnt == (time_segment1 + time_segment2 + 2)) // Right on time
|
|
dodatek = 0;
|
|
else if (sample_point_passed) // Too late
|
|
dodatek = quant_cnt; // Take smaller (SJW : quant_cnt)
|
|
else // Too early
|
|
reseti clock to 0 so we start with new bit sooner
|
|
|
|
|
|
|
always @ (posedge clk or posedge rst)
|
|
begin
|
|
if (rst)
|
|
quant_cnt <= 0;
|
|
else if (go_sync || go_seg1 || go_seg2)
|
|
quant_cnt <=#Tp 0;
|
|
else if (clk_en)
|
|
quant_cnt <=#Tp quant_cnt + 1'b1;
|
|
end
|
|
|
|
|
/* sample_point_passed is needed for phase error detection. Signal is set only when resynchronization is possible (high to low transition) */
|
always @ (posedge clk or posedge rst)
|
reg sample_point_passed;
|
|
always @ (posedge clk)
|
|
begin
|
|
if (clk_en & (quant_cnt == (time_segment1 + time_segment2 + 2)))
|
|
begin
|
begin
|
if(rx)
|
if (rst)
|
sample_point_passed <=#Tp 1'b0;
|
dodatek <= 0;
|
else
|
else if (clk_en & resync & seg1)
|
sample_point_passed <=#Tp 1'b1;
|
dodatek <=#Tp (quant_cnt > sync_jump_width)? (sync_jump_width + 1) : (quant_cnt + 1);
|
end
|
else if (go_sync | go_seg1)
|
end
|
dodatek <=#Tp 0;
|
|
end
|
|
|
|
/*
|
|
always @ (posedge clk or posedge rst)
|
|
begin
|
|
if (rst)
|
|
odstevek <= 0;
|
|
else if (clk_en & resync & seg2)
|
|
odstevek <=#Tp ((time_segment2 + 1 - quant_cnt) > sync_jump_width)? (sync_jump_width + 1) : (time_segment2 + 1 - quant_cnt);
|
|
else if (go_sync | go_seg1)
|
|
odstevek <=#Tp 0;
|
|
end
|
|
*/
|
|
|
|
assign odstevek = ((time_segment2 - quant_cnt) < ( sync_jump_width + 1));
|
|
|
|
|
/* Sampling data */
|
|
wire sample_time =
|
|
|
|
|
reg sample_pulse;
|
|
// Sampling data
|
always @ (posedge clk or posedge rst)
|
always @ (posedge clk or posedge rst)
|
begin
|
begin
|
if (rst)
|
if (rst)
|
begin
|
begin
|
sampled_bit <= 1;
|
sampled_bit <= 1;
|
|
sample_pulse <= 0;
|
end
|
end
|
else if (clk_en & (quant_cnt == time_segment1))
|
else if (go_seg2)
|
begin
|
begin
|
sampled_bit <=#Tp rx;
|
sampled_bit <=#Tp rx;
|
|
sample_pulse <=#Tp 1;
|
end
|
end
|
|
else
|
|
sample_pulse <=#Tp 0;
|
end
|
end
|
|
|
|
|
|
|
Detect phase error and change the above flip-flop
|
/* Blocking synchronization (can occur only once in a bit time) */
|
|
always @ (posedge clk or posedge rst)
|
|
begin
|
|
if (rst)
|
|
sync_blocked <=#Tp 1'b0;
|
|
else if (clk_en)
|
|
begin
|
|
if (hard_sync || resync)
|
|
sync_blocked <=#Tp 1'b1;
|
|
else if (seg2 & quant_cnt == time_segment2)
|
|
sync_blocked <=#Tp 1'b0;
|
|
end
|
|
end
|
|
|
|
|
|
|
|
|
|
|
endmodule
|
endmodule
|