-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvga_ball.sv
144 lines (119 loc) · 4.18 KB
/
vga_ball.sv
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
/*
* Avalon memory-mapped peripheral that generates VGA
*
* Stephen A. Edwards
* Columbia University
*
* UNI: mgc2171, pw2593
*/
module vga_ball(
input logic clk,
input logic reset,
input logic [10:0] xcoor,
input logic [9:0] ycoor,
output logic [7:0] VGA_R, VGA_G, VGA_B,
output logic VGA_CLK, VGA_HS, VGA_VS,
VGA_BLANK_n,
output logic VGA_SYNC_n
);
logic [10:0] hcount;
logic [9:0] vcount;
logic [7:0] background_r, background_g, background_b;
vga_counters counters(.clk50(clk), .*);
logic ballsprite [2047:0];
logic [5:0] i;
logic [4:0] j;
logic ballfilled;
initial begin
$readmemb("image.hex", ballsprite); // Pixels are arranged by columns
end
always_ff @(posedge clk)
if (reset) begin
background_r <= 8'h0;
background_g <= 8'h0;
background_b <= 8'h80;
end
assign i = hcount - xcoor + 1; // Relative horizontal coordination
assign j = vcount - ycoor + 1; // Relative vertical coordination
always_ff @(posedge clk) begin
if (hcount >= xcoor - 1 && hcount <= xcoor + 62 && vcount >= ycoor - 1 && vcount <= ycoor + 30) begin // Sequential logic have one cycle delay
ballfilled <= ballsprite[{j, i}]; // Relative position in the image, j * 64 + i
end else
ballfilled <= 1'b0;
end
always_comb begin
//{VGA_R, VGA_G, VGA_B} = {8'h0, 8'h0, 8'h0};
if (VGA_BLANK_n )
if (ballfilled)
{VGA_R, VGA_G, VGA_B} = {8'hff, 8'hff, 8'hff};
else
{VGA_R, VGA_G, VGA_B} =
{background_r, background_g, background_b};
else
{VGA_R, VGA_G, VGA_B} = {8'h0, 8'h0, 8'h0};
end
endmodule
module vga_counters(
input logic clk50, reset,
output logic [10:0] hcount, // hcount[10:1] is pixel column
output logic [9:0] vcount, // vcount[9:0] is pixel row
output logic VGA_CLK, VGA_HS, VGA_VS, VGA_BLANK_n, VGA_SYNC_n);
/*
* 640 X 480 VGA timing for a 50 MHz clock: one pixel every other cycle
*
* HCOUNT 1599 0 1279 1599 0
* _______________ ________
* ___________| Video |____________| Video
*
*
* |SYNC| BP |<-- HACTIVE -->|FP|SYNC| BP |<-- HACTIVE
* _______________________ _____________
* |____| VGA_HS |____|
*/
// Parameters for hcount
parameter HACTIVE = 11'd 1280,
HFRONT_PORCH = 11'd 32,
HSYNC = 11'd 192,
HBACK_PORCH = 11'd 96,
HTOTAL = HACTIVE + HFRONT_PORCH + HSYNC +
HBACK_PORCH; // 1600
// Parameters for vcount
parameter VACTIVE = 10'd 480,
VFRONT_PORCH = 10'd 10,
VSYNC = 10'd 2,
VBACK_PORCH = 10'd 33,
VTOTAL = VACTIVE + VFRONT_PORCH + VSYNC +
VBACK_PORCH; // 525
logic endOfLine;
always_ff @(posedge clk50 or posedge reset)
if (reset) hcount <= 0;
else if (endOfLine) hcount <= 0;
else hcount <= hcount + 11'd 1;
assign endOfLine = hcount == HTOTAL - 1;
logic endOfField;
always_ff @(posedge clk50 or posedge reset)
if (reset) vcount <= 0;
else if (endOfLine)
if (endOfField) vcount <= 0;
else vcount <= vcount + 10'd 1;
assign endOfField = vcount == VTOTAL - 1;
// Horizontal sync: from 0x520 to 0x5DF (0x57F)
// 101 0010 0000 to 101 1101 1111
assign VGA_HS = !( (hcount[10:8] == 3'b101) &
!(hcount[7:5] == 3'b111));
assign VGA_VS = !( vcount[9:1] == (VACTIVE + VFRONT_PORCH) / 2);
assign VGA_SYNC_n = 1'b0; // For putting sync on the green signal; unused
// Horizontal active: 0 to 1279 Vertical active: 0 to 479
// 101 0000 0000 1280 01 1110 0000 480
// 110 0011 1111 1599 10 0000 1100 524
assign VGA_BLANK_n = !( hcount[10] & (hcount[9] | hcount[8]) ) &
!( vcount[9] | (vcount[8:5] == 4'b1111) );
/* VGA_CLK is 25 MHz
* __ __ __
* clk50 __| |__| |__|
*
* _____ __
* hcount[0]__| |_____|
*/
assign VGA_CLK = hcount[0]; // 25 MHz clock: rising edge sensitive
endmodule