| 1 |
//Credit to the Implementation of HDMI Spec v1.4a by Sameer Puri https://github.com/sameer |
| 2 |
`default_nettype none |
| 3 |
/* |
| 4 |
Expected Result: |
| 5 |
A new colour should wipe across the screen over a couple of seconds. This should happen repeatedly. |
| 6 |
*/ |
| 7 |
module dvi_pattern |
| 8 |
( |
| 9 |
input wire clk, |
| 10 |
input logic key_i, |
| 11 |
output wire tmds_clk_n, |
| 12 |
output wire tmds_clk_p, |
| 13 |
output wire [2:0] tmds_d_n, |
| 14 |
output wire [2:0] tmds_d_p, |
| 15 |
output wire div_led |
| 16 |
); |
| 17 |
|
| 18 |
wire key; |
| 19 |
assign key = key_i ^ `INV_BTN; |
| 20 |
wire clk_pixel, clk_pixel_x5, pll_lock, reset; |
| 21 |
logic [2:0] tmds; |
| 22 |
logic tmds_clock; |
| 23 |
|
| 24 |
reg sys_resetn = 0; |
| 25 |
reg [3:0] reset_cnt = 15; // reset for 15 cycles |
| 26 |
always @(posedge clk) begin |
| 27 |
reset_cnt <= reset_cnt == 0 ? 0 : reset_cnt - 1; |
| 28 |
if (reset_cnt == 0) |
| 29 |
sys_resetn <= key; |
| 30 |
end |
| 31 |
assign reset = ~sys_resetn; |
| 32 |
|
| 33 |
PLL480 pll_dvi ( |
| 34 |
.clkin(clk), |
| 35 |
.clkout(clk_pixel_x5), |
| 36 |
.lock(pll_lock) |
| 37 |
); |
| 38 |
|
| 39 |
|
| 40 |
CLKDIV #(.DIV_MODE("5")) clk_div ( |
| 41 |
.CLKOUT(clk_pixel), |
| 42 |
.HCLKIN(clk_pixel_x5), |
| 43 |
.RESETN(pll_lock) |
| 44 |
); |
| 45 |
|
| 46 |
|
| 47 |
reg [23:0] counter; |
| 48 |
always @(posedge clk_pixel) |
| 49 |
counter<=counter+1'b1; |
| 50 |
|
| 51 |
assign div_led = counter[23]; |
| 52 |
|
| 53 |
localparam BIT_WIDTH = 10; |
| 54 |
localparam BIT_HEIGHT = 10; |
| 55 |
localparam START_X = 0; |
| 56 |
localparam START_Y = 0; |
| 57 |
localparam int NUM_CHANNELS = 3; |
| 58 |
logic hsync; |
| 59 |
logic vsync; |
| 60 |
|
| 61 |
logic [BIT_WIDTH-1:0] cx = START_X; |
| 62 |
logic [BIT_HEIGHT-1:0] cy = START_Y; |
| 63 |
|
| 64 |
|
| 65 |
localparam real VIDEO_RATE = 25.2E6; |
| 66 |
|
| 67 |
localparam [BIT_WIDTH-1:0] FRAME_WIDTH = 800; |
| 68 |
localparam [BIT_HEIGHT-1:0] FRAME_HEIGHT = 525; |
| 69 |
localparam [BIT_WIDTH-1:0] SCREEN_WIDTH = 640; |
| 70 |
localparam [BIT_HEIGHT-1:0] SCREEN_HEIGHT = 480; |
| 71 |
localparam [BIT_WIDTH-1:0] HSYNC_PULSE_START = 16; |
| 72 |
localparam [BIT_WIDTH-1:0] HSYNC_PULSE_SIZE = 96; |
| 73 |
localparam [BIT_HEIGHT-1:0] VSYNC_PULSE_START = 10; |
| 74 |
localparam [BIT_HEIGHT-1:0] VSYNC_PULSE_SIZE = 2; |
| 75 |
localparam INVERT = 1; |
| 76 |
|
| 77 |
|
| 78 |
wire next = screen_partition == (SCREEN_WIDTH -1'B1); |
| 79 |
reg [BIT_WIDTH-1: 0] screen_partition; |
| 80 |
|
| 81 |
reg [23:0] old_color, new_color; |
| 82 |
wire [23:0] rgb; |
| 83 |
wire [7:0] r_lfsr, g_lfsr, b_lfsr; |
| 84 |
|
| 85 |
// LFSR8 #(.seed(20)) red_lsfr(.reset(reset), .next(next), .clk(clk_pixel), .out_val(r_lfsr)); |
| 86 |
// LFSR8 #(.seed(81)) green_lsfr(.reset(reset), .next(next), .clk(clk_pixel), .out_val(g_lfsr)); |
| 87 |
// LFSR8 #(.seed(130)) blue_lsfr(.reset(reset), .next(next), .clk(clk_pixel), .out_val(b_lfsr)); |
| 88 |
|
| 89 |
LFSR24 #(.seed(24'hFA311)) rgb_lfsr(.reset(reset), .next(next), .clk(clk_pixel), .out_val({r_lfsr, g_lfsr, b_lfsr})); |
| 90 |
|
| 91 |
|
| 92 |
always @(posedge clk_pixel) begin |
| 93 |
if (next) begin |
| 94 |
screen_partition <= 0; |
| 95 |
old_color <= new_color; |
| 96 |
new_color <= {r_lfsr, g_lfsr, b_lfsr}; |
| 97 |
end |
| 98 |
else if ((cx == SCREEN_WIDTH-1'b1) && (cy == SCREEN_HEIGHT-1'b1)) begin |
| 99 |
screen_partition <= screen_partition + 1'b1; |
| 100 |
end |
| 101 |
end |
| 102 |
|
| 103 |
assign rgb = (cx < screen_partition)? new_color: old_color; |
| 104 |
|
| 105 |
always_comb begin |
| 106 |
hsync <= INVERT ^ (cx >= SCREEN_WIDTH + HSYNC_PULSE_START && cx < SCREEN_WIDTH + HSYNC_PULSE_START + HSYNC_PULSE_SIZE); |
| 107 |
// vsync pulses should begin and end at the start of hsync, so special |
| 108 |
// handling is required for the lines on which vsync starts and ends |
| 109 |
if (cy == SCREEN_HEIGHT + VSYNC_PULSE_START) |
| 110 |
vsync <= INVERT ^ (cx >= SCREEN_WIDTH + HSYNC_PULSE_START); |
| 111 |
else if (cy == SCREEN_HEIGHT + VSYNC_PULSE_START + VSYNC_PULSE_SIZE) |
| 112 |
vsync <= INVERT ^ (cx < SCREEN_WIDTH + HSYNC_PULSE_START); |
| 113 |
else |
| 114 |
vsync <= INVERT ^ (cy >= SCREEN_HEIGHT + VSYNC_PULSE_START && cy < SCREEN_HEIGHT + VSYNC_PULSE_START + VSYNC_PULSE_SIZE); |
| 115 |
end |
| 116 |
|
| 117 |
|
| 118 |
// Wrap-around pixel position counters indicating the pixel to be generated by the user in THIS clock and sent out in the NEXT clock. |
| 119 |
always_ff @(posedge clk_pixel) |
| 120 |
begin |
| 121 |
if (reset) |
| 122 |
begin |
| 123 |
cx <= BIT_WIDTH'(START_X); |
| 124 |
cy <= BIT_HEIGHT'(START_Y); |
| 125 |
end |
| 126 |
else |
| 127 |
begin |
| 128 |
cx <= cx == FRAME_WIDTH-1'b1 ? BIT_WIDTH'(0) : cx + 1'b1; |
| 129 |
cy <= cx == FRAME_WIDTH-1'b1 ? cy == FRAME_HEIGHT-1'b1 ? BIT_HEIGHT'(0) : cy + 1'b1 : cy; |
| 130 |
end |
| 131 |
end |
| 132 |
|
| 133 |
// See Section 5.2 |
| 134 |
logic video_data_period = 0; |
| 135 |
always_ff @(posedge clk_pixel) |
| 136 |
begin |
| 137 |
if (reset) |
| 138 |
video_data_period <= 0; |
| 139 |
else |
| 140 |
video_data_period <= cx < SCREEN_WIDTH && cy < SCREEN_HEIGHT; |
| 141 |
end |
| 142 |
|
| 143 |
logic [2:0] mode = 3'd1; |
| 144 |
logic [23:0] video_data = 24'd0; |
| 145 |
logic [5:0] control_data = 6'd0; |
| 146 |
logic [11:0] data_island_data = 12'd0; |
| 147 |
|
| 148 |
|
| 149 |
always_ff @(posedge clk_pixel) |
| 150 |
begin |
| 151 |
if (reset) |
| 152 |
begin |
| 153 |
mode <= 3'd0; |
| 154 |
video_data <= 24'd0; |
| 155 |
control_data <= 6'd0; |
| 156 |
end |
| 157 |
else |
| 158 |
begin |
| 159 |
mode <= video_data_period ? 3'd1 : 3'd0; |
| 160 |
video_data <= rgb; |
| 161 |
control_data <= {4'b0000, {vsync, hsync}}; // ctrl3, ctrl2, ctrl1, ctrl0, vsync, hsync |
| 162 |
end |
| 163 |
end |
| 164 |
|
| 165 |
|
| 166 |
// All logic below relates to the production and output of the 10-bit TMDS code. |
| 167 |
logic [9:0] tmds_internal [NUM_CHANNELS-1:0] /* verilator public_flat */ ; |
| 168 |
genvar i; |
| 169 |
generate |
| 170 |
// TMDS code production. |
| 171 |
for (i = 0; i < NUM_CHANNELS; i++) |
| 172 |
begin: tmds_gen |
| 173 |
tmds_channel #(.CN(i)) tmds_channel ( |
| 174 |
.clk_pixel(clk_pixel), |
| 175 |
.video_data(video_data[i*8+7:i*8]), |
| 176 |
.data_island_data(0), |
| 177 |
.control_data(control_data[i*2+1:i*2]), |
| 178 |
.mode(mode), |
| 179 |
.tmds(tmds_internal[i]) |
| 180 |
); |
| 181 |
end |
| 182 |
endgenerate |
| 183 |
|
| 184 |
|
| 185 |
OSER10 gwSer0( |
| 186 |
.Q( tmds[ 0 ] ), |
| 187 |
.D0( tmds_internal[ 0 ][ 0 ] ), |
| 188 |
.D1( tmds_internal[ 0 ][ 1 ] ), |
| 189 |
.D2( tmds_internal[ 0 ][ 2 ] ), |
| 190 |
.D3( tmds_internal[ 0 ][ 3 ] ), |
| 191 |
.D4( tmds_internal[ 0 ][ 4 ] ), |
| 192 |
.D5( tmds_internal[ 0 ][ 5 ] ), |
| 193 |
.D6( tmds_internal[ 0 ][ 6 ] ), |
| 194 |
.D7( tmds_internal[ 0 ][ 7 ] ), |
| 195 |
.D8( tmds_internal[ 0 ][ 8 ] ), |
| 196 |
.D9( tmds_internal[ 0 ][ 9 ] ), |
| 197 |
.PCLK( clk_pixel ), |
| 198 |
.FCLK( clk_pixel_x5 ), |
| 199 |
.RESET( reset ) ); |
| 200 |
|
| 201 |
OSER10 gwSer1( |
| 202 |
.Q( tmds[ 1 ] ), |
| 203 |
.D0( tmds_internal[ 1 ][ 0 ] ), |
| 204 |
.D1( tmds_internal[ 1 ][ 1 ] ), |
| 205 |
.D2( tmds_internal[ 1 ][ 2 ] ), |
| 206 |
.D3( tmds_internal[ 1 ][ 3 ] ), |
| 207 |
.D4( tmds_internal[ 1 ][ 4 ] ), |
| 208 |
.D5( tmds_internal[ 1 ][ 5 ] ), |
| 209 |
.D6( tmds_internal[ 1 ][ 6 ] ), |
| 210 |
.D7( tmds_internal[ 1 ][ 7 ] ), |
| 211 |
.D8( tmds_internal[ 1 ][ 8 ] ), |
| 212 |
.D9( tmds_internal[ 1 ][ 9 ] ), |
| 213 |
.PCLK( clk_pixel ), |
| 214 |
.FCLK( clk_pixel_x5 ), |
| 215 |
.RESET( reset ) ); |
| 216 |
|
| 217 |
OSER10 gwSer2( |
| 218 |
.Q( tmds[ 2 ] ), |
| 219 |
.D0( tmds_internal[ 2 ][ 0 ] ), |
| 220 |
.D1( tmds_internal[ 2 ][ 1 ] ), |
| 221 |
.D2( tmds_internal[ 2 ][ 2 ] ), |
| 222 |
.D3( tmds_internal[ 2 ][ 3 ] ), |
| 223 |
.D4( tmds_internal[ 2 ][ 4 ] ), |
| 224 |
.D5( tmds_internal[ 2 ][ 5 ] ), |
| 225 |
.D6( tmds_internal[ 2 ][ 6 ] ), |
| 226 |
.D7( tmds_internal[ 2 ][ 7 ] ), |
| 227 |
.D8( tmds_internal[ 2 ][ 8 ] ), |
| 228 |
.D9( tmds_internal[ 2 ][ 9 ] ), |
| 229 |
.PCLK( clk_pixel ), |
| 230 |
.FCLK( clk_pixel_x5 ), |
| 231 |
.RESET( reset ) ); |
| 232 |
|
| 233 |
assign tmds_clock = clk_pixel; |
| 234 |
|
| 235 |
generate |
| 236 |
if (`IS_ELVDS) |
| 237 |
ELVDS_OBUF tmds_bufds [3:0] ( |
| 238 |
.I({clk_pixel, tmds}), |
| 239 |
.O({tmds_clk_p, tmds_d_p}), |
| 240 |
.OB({tmds_clk_n, tmds_d_n}) |
| 241 |
); |
| 242 |
else |
| 243 |
TLVDS_OBUF tmds_bufds [3:0] ( |
| 244 |
.I({clk_pixel, tmds}), |
| 245 |
.O({tmds_clk_p, tmds_d_p}), |
| 246 |
.OB({tmds_clk_n, tmds_d_n}) |
| 247 |
); |
| 248 |
endgenerate |
| 249 |
endmodule |
| 250 |
|
| 251 |
|
| 252 |
// 8-bit LFSR |
| 253 |
module LFSR8 #( |
| 254 |
parameter seed = 10 |
| 255 |
) ( |
| 256 |
input wire reset, |
| 257 |
input wire clk, |
| 258 |
input wire next, |
| 259 |
output reg [7:0] out_val |
| 260 |
); |
| 261 |
|
| 262 |
|
| 263 |
always @(posedge clk) begin |
| 264 |
if (reset) |
| 265 |
out_val <= seed; |
| 266 |
else if (next) |
| 267 |
out_val <= {out_val[6:0], ((out_val[7] ^~ out_val[5]) ^~ out_val[4]) ^~ out_val[3]}; |
| 268 |
end |
| 269 |
endmodule |
| 270 |
|
| 271 |
// 24-bit LFSR |
| 272 |
module LFSR24 #( |
| 273 |
parameter seed = 10 |
| 274 |
) ( |
| 275 |
input wire reset, |
| 276 |
input wire clk, |
| 277 |
input wire next, |
| 278 |
output reg [23:0] out_val |
| 279 |
); |
| 280 |
|
| 281 |
always @(posedge clk) begin |
| 282 |
if (reset) |
| 283 |
out_val <= seed; |
| 284 |
else if (next) |
| 285 |
out_val <= {out_val[22:0], ((out_val[23] ^~ out_val[22]) ^~ out_val[21]) ^~ out_val[16]}; |
| 286 |
end |
| 287 |
endmodule |