-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGA_SRAM_interface.v
209 lines (178 loc) · 5.54 KB
/
VGA_SRAM_interface.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
// Copyright by Adam Kinsman and Henry Ko and Nicola Nicolici
// Developed for the Digital Systems Design course (COE3DQ4)
// Department of Electrical and Computer Engineering
// McMaster University
// Ontario, Canada
`timescale 1ns/100ps
`default_nettype none
`include "define_state.h"
// This module generates the address for reading the SRAM
// in order to display the image on the screen
module VGA_SRAM_interface (
input logic Clock,
input logic Resetn,
input logic VGA_enable,
input logic VGA_adjust,
input logic [17:0] SRAM_base_address,
output logic [17:0] SRAM_address,
input logic [15:0] SRAM_read_data,
output logic VGA_CLOCK_O,
output logic VGA_HSYNC_O,
output logic VGA_VSYNC_O,
output logic VGA_BLANK_O,
output logic VGA_SYNC_O,
output logic [9:0] VGA_RED_O,
output logic [9:0] VGA_GREEN_O,
output logic [9:0] VGA_BLUE_O
);
parameter
VIEW_AREA_LEFT = 160,
VIEW_AREA_RIGHT = 480,
VIEW_AREA_TOP = 120,
VIEW_AREA_BOTTOM = 360;
VGA_SRAM_state_type VGA_SRAM_state;
// For VGA
logic [9:0] VGA_red, VGA_green, VGA_blue;
logic [9:0] pixel_X_pos;
logic [9:0] pixel_Y_pos;
logic [15:0] VGA_sram_data [2:0];
// VGA unit
VGA_Controller VGA_unit(
.Clock(Clock),
.Resetn(Resetn),
.iRed(VGA_red),
.iGreen(VGA_green),
.iBlue(VGA_blue),
.oCoord_X(pixel_X_pos),
.oCoord_Y(pixel_Y_pos),
// VGA Side
.oVGA_R(VGA_RED_O),
.oVGA_G(VGA_GREEN_O),
.oVGA_B(VGA_BLUE_O),
.oVGA_H_SYNC(VGA_HSYNC_O),
.oVGA_V_SYNC(VGA_VSYNC_O),
.oVGA_SYNC(VGA_SYNC_O),
.oVGA_BLANK(VGA_BLANK_O),
.oVGA_CLOCK(VGA_CLOCK_O)
);
always_ff @ (posedge Clock or negedge Resetn) begin
if (Resetn == 1'b0) begin
VGA_SRAM_state <= S_VS_WAIT_NEW_PIXEL_ROW;
VGA_red <= 10'd0;
VGA_green <= 10'd0;
VGA_blue <= 10'd0;
SRAM_address <= 18'd0;
VGA_sram_data[2] <= 16'd0;
VGA_sram_data[1] <= 16'd0;
VGA_sram_data[0] <= 16'd0;
end else begin
if (~VGA_enable) begin
VGA_red <= 10'd0;
VGA_green <= 10'd0;
VGA_blue <= 10'd0;
end else begin
case (VGA_SRAM_state)
S_VS_WAIT_NEW_PIXEL_ROW: begin
if (pixel_Y_pos >= VIEW_AREA_TOP && pixel_Y_pos < VIEW_AREA_BOTTOM) begin
if (pixel_X_pos == (VIEW_AREA_LEFT - 3)) begin
if (pixel_Y_pos == VIEW_AREA_TOP)
// Start a new frame
// Provide address for data 1
SRAM_address <= SRAM_base_address;
else
// Start a new row of pixels
// Provide address for data 1
SRAM_address <= SRAM_address - 18'd4;
VGA_SRAM_state <= S_VS_NEW_PIXEL_ROW_DELAY_1;
end
end
VGA_red <= 10'd0;
VGA_green <= 10'd0;
VGA_blue <= 10'd0;
end
S_VS_NEW_PIXEL_ROW_DELAY_1: begin
// Provide address for data 2
SRAM_address <= SRAM_address + 18'h00001;
VGA_SRAM_state <= S_VS_NEW_PIXEL_ROW_DELAY_2;
end
S_VS_NEW_PIXEL_ROW_DELAY_2: begin
// Provide address for data 3
SRAM_address <= SRAM_address + 18'h00001;
VGA_SRAM_state <= S_VS_NEW_PIXEL_ROW_DELAY_3;
end
S_VS_NEW_PIXEL_ROW_DELAY_3: begin
// Buffer data 1
VGA_sram_data[2] <= SRAM_read_data;
VGA_SRAM_state <= S_VS_NEW_PIXEL_ROW_DELAY_4;
end
S_VS_NEW_PIXEL_ROW_DELAY_4: begin
// Provide address for data 1
SRAM_address <= SRAM_address + 18'h00001;
// Buffer data 2
VGA_sram_data[1] <= SRAM_read_data;
VGA_SRAM_state <= S_VS_NEW_PIXEL_ROW_DELAY_5;
end
S_VS_NEW_PIXEL_ROW_DELAY_5: begin
// Provide address for data 2
SRAM_address <= SRAM_address + 18'h00001;
// Buffer data 3
VGA_sram_data[0] <= SRAM_read_data;
VGA_SRAM_state <= S_VS_FETCH_PIXEL_DATA_0;
end
S_VS_FETCH_PIXEL_DATA_0: begin
// Provide address for data 3
SRAM_address <= SRAM_address + 18'h00001;
// Provide RGB data
if (pixel_X_pos != VIEW_AREA_LEFT && VGA_adjust == 1'b1) begin
// Adjust RGB data
end else begin
VGA_red <= {VGA_sram_data[2][15:8], 2'b00};
VGA_green <= {VGA_sram_data[2][7:0], 2'b00};
VGA_blue <= {VGA_sram_data[1][15:8], 2'b00};
end
VGA_SRAM_state <= S_VS_FETCH_PIXEL_DATA_1;
end
S_VS_FETCH_PIXEL_DATA_1: begin
// Buffer data 1
VGA_sram_data[2] <= SRAM_read_data;
VGA_SRAM_state <= S_VS_FETCH_PIXEL_DATA_2;
end
S_VS_FETCH_PIXEL_DATA_2: begin
// Provide address for data 1
SRAM_address <= SRAM_address + 18'h00001;
// Buffer data 2
VGA_sram_data[1] <= SRAM_read_data;
// Provide RGB data
if (VGA_adjust == 1'b1) begin
// Adjust RGB data
end else begin
VGA_red <= {VGA_sram_data[1][7:0], 2'b00};
VGA_green <= {VGA_sram_data[0][15:8], 2'b00};
VGA_blue <= {VGA_sram_data[0][7:0], 2'b00};
end
VGA_SRAM_state <= S_VS_FETCH_PIXEL_DATA_3;
end
S_VS_FETCH_PIXEL_DATA_3: begin
// Provide address for data 2
SRAM_address <= SRAM_address + 18'h00001;
// Buffer data 3
VGA_sram_data[0] <= SRAM_read_data;
if (pixel_X_pos < (VIEW_AREA_RIGHT - 2))
// Still within one row
VGA_SRAM_state <= S_VS_FETCH_PIXEL_DATA_0;
else
VGA_SRAM_state <= S_VS_WAIT_NEW_PIXEL_ROW;
end
default: VGA_SRAM_state <= S_VS_WAIT_NEW_PIXEL_ROW;
endcase
end
// To generate a border
if (pixel_Y_pos == 10'd0 || pixel_Y_pos == 10'd479
|| pixel_X_pos == 10'd0 || pixel_X_pos == 10'd639) begin
VGA_red <= 10'h3FF;
VGA_green <= 10'h3FF;
VGA_blue <= 10'h3FF;
end
end
end
endmodule