Debian Codegraph @

examples/DVI/tmds-channel.v

raw
1 // Implementation of HDMI Spec v1.4a Section 5.4: Encoding, Section 5.2.2.1: Video Guard Band, Section 5.2.3.3: Data Island Guard Bands.
2 // By Sameer Puri https://github.com/sameer
3
4 module tmds_channel
5 #(
6 // TMDS Channel number.
7 // There are only 3 possible channel numbers in HDMI 1.4a: 0, 1, 2.
8 parameter int CN = 0
9 )
10 (
11 input logic clk_pixel,
12 input logic [7:0] video_data,
13 input logic [3:0] data_island_data,
14 input logic [1:0] control_data,
15 input logic [2:0] mode, // Mode select (0 = control, 1 = video, 2 = video guard, 3 = island, 4 = island guard)
16 output logic [9:0] tmds = 10'b1101010100
17 );
18
19 // See Section 5.4.4.1
20 // Below is a direct implementation of Figure 5-7, using the same variable names.
21
22 logic signed [4:0] acc = 5'sd0;
23
24 logic [8:0] q_m;
25 logic [9:0] q_out;
26 logic [9:0] video_coding;
27 assign video_coding = q_out;
28
29 logic [3:0] N1D;
30 logic signed [4:0] N1q_m07;
31 logic signed [4:0] N0q_m07;
32 always_comb
33 begin
34 N1D = video_data[0] + video_data[1] + video_data[2] + video_data[3] + video_data[4] + video_data[5] + video_data[6] + video_data[7];
35 case(q_m[0] + q_m[1] + q_m[2] + q_m[3] + q_m[4] + q_m[5] + q_m[6] + q_m[7])
36 4'b0000: N1q_m07 = 5'sd0;
37 4'b0001: N1q_m07 = 5'sd1;
38 4'b0010: N1q_m07 = 5'sd2;
39 4'b0011: N1q_m07 = 5'sd3;
40 4'b0100: N1q_m07 = 5'sd4;
41 4'b0101: N1q_m07 = 5'sd5;
42 4'b0110: N1q_m07 = 5'sd6;
43 4'b0111: N1q_m07 = 5'sd7;
44 4'b1000: N1q_m07 = 5'sd8;
45 default: N1q_m07 = 5'sd0;
46 endcase
47 N0q_m07 = 5'sd8 - N1q_m07;
48 end
49
50 logic signed [4:0] acc_add;
51
52 integer i;
53
54 always_comb
55 begin
56 if (N1D > 4'd4 || (N1D == 4'd4 && video_data[0] == 1'd0))
57 begin
58 q_m[0] = video_data[0];
59 for(i = 0; i < 7; i++)
60 q_m[i + 1] = q_m[i] ~^ video_data[i + 1];
61 q_m[8] = 1'b0;
62 end
63 else
64 begin
65 q_m[0] = video_data[0];
66 for(i = 0; i < 7; i++)
67 q_m[i + 1] = q_m[i] ^ video_data[i + 1];
68 q_m[8] = 1'b1;
69 end
70 if (acc == 5'sd0 || (N1q_m07 == N0q_m07))
71 begin
72 if (q_m[8])
73 begin
74 acc_add = N1q_m07 - N0q_m07;
75 q_out = {~q_m[8], q_m[8], q_m[7:0]};
76 end
77 else
78 begin
79 acc_add = N0q_m07 - N1q_m07;
80 q_out = {~q_m[8], q_m[8], ~q_m[7:0]};
81 end
82 end
83 else
84 begin
85 if ((acc > 5'sd0 && N1q_m07 > N0q_m07) || (acc < 5'sd0 && N1q_m07 < N0q_m07))
86 begin
87 q_out = {1'b1, q_m[8], ~q_m[7:0]};
88 acc_add = (N0q_m07 - N1q_m07) + (q_m[8] ? 5'sd2 : 5'sd0);
89 end
90 else
91 begin
92 q_out = {1'b0, q_m[8], q_m[7:0]};
93 acc_add = (N1q_m07 - N0q_m07) - (~q_m[8] ? 5'sd2 : 5'sd0);
94 end
95 end
96 end
97
98 always_ff @(posedge clk_pixel) acc <= mode != 3'd1 ? 5'sd0 : acc + acc_add;
99
100 // See Section 5.4.2
101 logic [9:0] control_coding;
102 always_comb
103 begin
104 unique case(control_data)
105 2'b00: control_coding = 10'b1101010100;
106 2'b01: control_coding = 10'b0010101011;
107 2'b10: control_coding = 10'b0101010100;
108 2'b11: control_coding = 10'b1010101011;
109 endcase
110 end
111
112 // See Section 5.4.3
113 logic [9:0] terc4_coding;
114 always_comb
115 begin
116 unique case(data_island_data)
117 4'b0000 : terc4_coding = 10'b1010011100;
118 4'b0001 : terc4_coding = 10'b1001100011;
119 4'b0010 : terc4_coding = 10'b1011100100;
120 4'b0011 : terc4_coding = 10'b1011100010;
121 4'b0100 : terc4_coding = 10'b0101110001;
122 4'b0101 : terc4_coding = 10'b0100011110;
123 4'b0110 : terc4_coding = 10'b0110001110;
124 4'b0111 : terc4_coding = 10'b0100111100;
125 4'b1000 : terc4_coding = 10'b1011001100;
126 4'b1001 : terc4_coding = 10'b0100111001;
127 4'b1010 : terc4_coding = 10'b0110011100;
128 4'b1011 : terc4_coding = 10'b1011000110;
129 4'b1100 : terc4_coding = 10'b1010001110;
130 4'b1101 : terc4_coding = 10'b1001110001;
131 4'b1110 : terc4_coding = 10'b0101100011;
132 4'b1111 : terc4_coding = 10'b1011000011;
133 endcase
134 end
135
136 // See Section 5.2.2.1
137 logic [9:0] video_guard_band;
138 generate
139 if (CN == 0 || CN == 2)
140 assign video_guard_band = 10'b1011001100;
141 else
142 assign video_guard_band = 10'b0100110011;
143 endgenerate
144
145 // See Section 5.2.3.3
146 logic [9:0] data_guard_band;
147 generate
148 if (CN == 1 || CN == 2)
149 assign data_guard_band = 10'b0100110011;
150 else
151 assign data_guard_band = control_data == 2'b00 ? 10'b1010001110
152 : control_data == 2'b01 ? 10'b1001110001
153 : control_data == 2'b10 ? 10'b0101100011
154 : 10'b1011000011;
155 endgenerate
156
157 // Apply selected mode.
158 always @(posedge clk_pixel)
159 begin
160 case (mode)
161 3'd0: tmds <= control_coding;
162 3'd1: tmds <= video_coding;
163 3'd2: tmds <= 0;
164 3'd3: tmds <= 0;
165 3'd4: tmds <= 0;
166 endcase
167 end
168
169 endmodule