-
Notifications
You must be signed in to change notification settings - Fork 0
/
vga.v
235 lines (209 loc) · 7 KB
/
vga.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
`timescale 1ns/1ps
`default_nettype none
// vga #(
// .RED_BITS (),
// .GREEN_BITS (),
// .BLUE_BITS (),
// .X_DISPLAY (),
// .X_FRONT_PORCH (),
// .X_SYNC (),
// .X_BACK_PORCH (),
// .Y_DISPLAY (),
// .Y_FRONT_PORCH (),
// .Y_SYNC (),
// .Y_BACK_PORCH ()
// ) vga_inst (
// .clock (),
// .reset (),
// .enable (),
// .rgb_ready_o (),
// .rgb_valid_i (),
// .rgb_data_red_i (),
// .rgb_data_green_i (),
// .rgb_data_blue_i (),
// .rgb_last_i (),
// .vga_red_o (),
// .vga_green_o (),
// .vga_blue_o (),
// .vga_hsync_o (),
// .vga_vsync_o (),
// .sync_error (),
// .in_display_area ()
// );
module vga #(
parameter integer RED_BITS = 4,
parameter integer GREEN_BITS = 4,
parameter integer BLUE_BITS = 4,
parameter integer X_DISPLAY = 10,
parameter integer X_FRONT_PORCH = 2,
parameter integer X_SYNC = 2,
parameter integer X_BACK_PORCH = 2,
parameter integer Y_DISPLAY = 10,
parameter integer Y_FRONT_PORCH = 2,
parameter integer Y_SYNC = 2,
parameter integer Y_BACK_PORCH = 2
) (
input wire clock,
input wire reset,
input wire enable, // ~programming_done
output wire rgb_ready_o,
input wire rgb_valid_i,
input wire [RED_BITS-1:0] rgb_data_red_i,
input wire [GREEN_BITS-1:0] rgb_data_green_i,
input wire [BLUE_BITS-1:0] rgb_data_blue_i,
input wire rgb_last_i,
output wire [RED_BITS-1:0] vga_red_o,
output wire [GREEN_BITS-1:0] vga_green_o,
output wire [BLUE_BITS-1:0] vga_blue_o,
output wire vga_hsync_o,
output wire vga_vsync_o,
// debug signals
output wire sync_error_o,
output wire in_display_area_o,
output wire end_of_frame_o
);
localparam integer X_DISPLAY_HIGH = X_DISPLAY - 1;
localparam integer X_FRONT_PORCH_HIGH = X_DISPLAY_HIGH + X_FRONT_PORCH;
localparam integer X_SYNC_HIGH = X_FRONT_PORCH_HIGH + X_SYNC;
localparam integer X_BACK_PORCH_HIGH = X_SYNC_HIGH + X_BACK_PORCH;
localparam integer X_COUNT_HIGH = X_BACK_PORCH_HIGH;
localparam integer Y_DISPLAY_HIGH = Y_DISPLAY - 1;
localparam integer Y_FRONT_PORCH_HIGH = Y_DISPLAY_HIGH + Y_FRONT_PORCH;
localparam integer Y_SYNC_HIGH = Y_FRONT_PORCH_HIGH + Y_SYNC;
localparam integer Y_BACK_PORCH_HIGH = Y_SYNC_HIGH + Y_BACK_PORCH;
localparam integer Y_COUNT_HIGH = Y_BACK_PORCH_HIGH;
// instantiate my own X/Y counter synced to rgb_last to generate rgb_ready and the sync pulses
wire [$clog2(X_COUNT_HIGH)-1:0] x_count;
wire [$clog2(Y_COUNT_HIGH)-1:0] y_count;
wire rollover;
reg hsync;
reg vsync;
reg in_display_area;
reg end_of_frame;
register #(
.RESET_VALUE (0),
.WIDTH (RED_BITS)
) red_register_inst (
.clk (clock),
.reset (reset),
.write_enable (rgb_ready_o && rgb_valid_i),
.data_i (rgb_data_red_i),
.data_o (vga_red_o)
);
register #(
.RESET_VALUE (0),
.WIDTH (GREEN_BITS)
) green_register_inst (
.clk (clock),
.reset (reset),
.write_enable (rgb_ready_o && rgb_valid_i),
.data_i (rgb_data_green_i),
.data_o (vga_green_o)
);
register #(
.RESET_VALUE (0),
.WIDTH (BLUE_BITS)
) blue_register_inst (
.clk (clock),
.reset (reset),
.write_enable (rgb_ready_o && rgb_valid_i),
.data_i (rgb_data_blue_i),
.data_o (vga_blue_o)
);
double_counter #(
.X_HIGH (X_COUNT_HIGH),
.Y_HIGH (Y_COUNT_HIGH)
) xy_counter_inst (
.clk (clock),
.clock_enable_i (enable),
.sync_reset_i (reset),
.enable_i (rgb_valid_i),
.x_o (x_count),
.y_o (y_count),
.tc_o (rollover)
);
always @(*) begin : hsync_generation_proc
if (x_count > X_FRONT_PORCH_HIGH && x_count <= X_SYNC_HIGH) begin
hsync = 1'b0;
end else begin
hsync = 1'b1;
end
end
always @(*) begin : vsync_generation_proc
if (y_count > Y_FRONT_PORCH_HIGH && y_count <= Y_SYNC_HIGH) begin
vsync = 1'b0;
end else begin
vsync = 1'b1;
end
end
register #(
.RESET_VALUE (1),
.WIDTH (1)
) hsync_register_inst (
.clk (clock),
.reset (reset),
.write_enable (enable),
.data_i (hsync),
.data_o (vga_hsync_o)
);
register #(
.RESET_VALUE (1),
.WIDTH (1)
) vsync_register_inst (
.clk (clock),
.reset (reset),
.write_enable (enable),
.data_i (vsync),
.data_o (vga_vsync_o)
);
register #(
.RESET_VALUE (0),
.WIDTH (1)
) in_display_area_register_inst (
.clk (clock),
.reset (reset),
.write_enable (enable),
.data_i (in_display_area),
.data_o (in_display_area_o)
);
always @(*) begin : in_display_area_proc
if (x_count <= X_DISPLAY_HIGH && y_count <= Y_DISPLAY_HIGH) begin
in_display_area = 1'b1;
end else begin
in_display_area = 1'b0;
end
end
always @(*) begin : end_of_frame_proc
if (rollover) begin
end_of_frame = 1'b1;
end else begin
end_of_frame = 1'b0;
end
end
assign rgb_ready_o = in_display_area & enable; // consider registering this?
assign end_of_frame_o = end_of_frame;
wire last_beat_was_last;
wire first_rgb_beat; // sync reset for counter?
register #(
.RESET_VALUE (0),
.WIDTH (1)
) last_register_inst (
.clk (clock),
.reset (reset),
.write_enable (rgb_ready_o && rgb_valid_i),
.data_i (rgb_last_i),
.data_o (last_beat_was_last)
);
// register #(
// .RESET_VALUE (0),
// .WIDTH (1)
// ) end_of_frame_register_inst (
// .clk (clock),
// .reset (reset),
// .write_enable (rgb_ready_o && rgb_valid_i),
// .data_i (end_of_frame),
// .data_o (end_of_frame_o)
// );
assign first_rgb_beat = rgb_ready_o && rgb_valid_i && ~last_beat_was_last;
assign sync_error_o = (rgb_ready_o && rgb_valid_i && last_beat_was_last) ? (x_count != X_DISPLAY_HIGH || y_count != Y_DISPLAY_HIGH) : (1'b0);
endmodule