-
Notifications
You must be signed in to change notification settings - Fork 1
/
campix.v
158 lines (140 loc) · 4.07 KB
/
campix.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 08/15/2018 08:13:17 AM
// Design Name:
// Module Name: campix
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
// XCLK : 12.xx Mhz
// PCLK : ~50 Mhz
// VGA clock: 25.175Mhz
`define IDLE 2'b00
`define VSON 2'b01
`define VSOF 2'b10
module campix(
input wire clock,
input wire reset,
// ov7670 interface
input wire pclk,
input wire vsync,
input wire href,
input wire [7:0] pdata,
output wire xclk,
output reg HSYNC,
output reg VSYNC,
output wire [3:0] red,
output wire [3:0] green,
output wire [3:0] blue
);
wire nreset, clk100m;
wire vsync_rise, vsync_fall;
wire lcount, pcount;
reg vsync_;
reg [9:0] pcounter;
reg [8:0] lcounter;
reg [1:0] state;
reg [11:0] rgb12bit;
reg vga_clk, den;
reg [7:0] pdata2;
pll_wrapper pll (
.clk_in (clock ),
.locked (nreset ),
.reset (reset ),
.sys_clk(clk100m),
.xclk (xclk )
);
//clock div 2
always @ ( posedge pclk ) begin
if ( nreset == 1'b0 ) vga_clk <= 1'b0;
else vga_clk <= !vga_clk;
end
// vsync delay
always @ ( posedge vga_clk ) begin
if ( nreset == 1'b0 ) vsync_ <= 1'b0;
else vsync_ <= vsync;
end
assign vsync_rise = vsync & !vsync_;
assign vsync_fall = !vsync & vsync_;
// state
always @ (posedge vga_clk ) begin
if ( nreset == 1'b0 ) state <= `IDLE;
else begin
case (state)
`IDLE: if (vsync_rise) state <= `VSON;
`VSON: if (vsync_fall) state <= `VSOF;
`VSOF: if (vsync_rise) state <= `VSON;
default: state <= `IDLE;
endcase
end
end
assign pcount = (pcounter == 9'd784) ? 1'b1 : 1'b0;
assign lcount = (lcounter == 8'd510) ? 1'b1 : 1'b0;
// VGA pixel counter
always @ ( posedge vga_clk ) begin
if ( nreset == 1'b0 ) pcounter <= 9'd0;
else begin
if ( vsync_rise || pcount ) pcounter <= 9'b0;
else pcounter <= pcounter + 9'd1;
end
end
// line counter
always @ ( posedge vga_clk) begin
if ( nreset == 1'b0 ) lcounter <= 8'd0;
else begin
if ( vsync_rise || lcount ) lcounter <= 8'd0;
else if ( pcount ) lcounter <= lcounter + 8'd1;
end
end
// VSYNC
always @ ( posedge vga_clk ) begin
if ( nreset == 1'b0 ) VSYNC <= 1'b1;
else if ( state == `VSON && lcounter < 8'd2 ) VSYNC <= 1'b0;
end
// HSYNC
always @ (posedge vga_clk ) begin
if ( nreset == 1'b0 ) HSYNC <= 1'b1;
else begin
if ( lcounter > 9'd15 )
if ( pcounter > 10'd660 && pcounter < 10'd740 ) HSYNC <= 1'b0;
else HSYNC <= 1'b1;
else HSYNC <= 1'b1;
end
end
// latch the data
always @ ( posedge pclk ) begin
if ( nreset == 1'b0 )
pdata2 <= 8'd0;
else
pdata2 <= pdata;
end
// data enable
always @ ( posedge pclk ) begin
if ( nreset == 1'b0 ) den <= 1'b0;
else
if (href) den <= ~den;
else den <= 1'b0;
end
//
always @ ( posedge pclk ) begin
if ( nreset == 1'b0 ) rgb12bit <= 12'd0;
else begin
if ( den ) rgb12bit <= {pdata2[3:0], pdata};
end
end
assign red = rgb12bit[11:8];
assign green = rgb12bit[7:4];
assign blue = rgb12bit[3:0];
endmodule