| 1 |
2 |
robfinch |
// ============================================================================
|
| 2 |
|
|
// 2011 Robert Finch
|
| 3 |
|
|
// robfinch@<remove>sympatico.ca
|
| 4 |
|
|
//
|
| 5 |
|
|
// HVCounter.v
|
| 6 |
|
|
// Horizontal / Vertical counter:
|
| 7 |
|
|
//
|
| 8 |
|
|
//
|
| 9 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
| 10 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
| 11 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
| 12 |
|
|
// (at your option) any later version.
|
| 13 |
|
|
//
|
| 14 |
|
|
// This source file is distributed in the hope that it will be useful,
|
| 15 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 |
|
|
// GNU General Public License for more details.
|
| 18 |
|
|
//
|
| 19 |
|
|
// You should have received a copy of the GNU General Public License
|
| 20 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 21 |
|
|
//
|
| 22 |
|
|
// ============================================================================
|
| 23 |
|
|
|
| 24 |
|
|
// horizontal pixel prescale counter
|
| 25 |
|
|
// Each pixel may be multiple clocks wide
|
| 26 |
|
|
//
|
| 27 |
|
|
// 37 slices / 64 LUTs / 161.525 MHz
|
| 28 |
|
|
// 28 ff's / 1 Mult
|
| 29 |
|
|
|
| 30 |
|
|
module HVCounter(
|
| 31 |
|
|
rst, vclk, pixcce, sync, cnt_offs, pixsz, maxpix, nxt_pix, pos, nxt_pos, ctr
|
| 32 |
|
|
);
|
| 33 |
|
|
input rst;
|
| 34 |
|
|
input vclk; // video clock
|
| 35 |
|
|
input pixcce; // pixel counter clock enable
|
| 36 |
|
|
input sync; // synchronization input (eol or eof)
|
| 37 |
|
|
input [11:0] cnt_offs; // counter offset: top or left of display area
|
| 38 |
|
|
input [3:0] pixsz; // size of a pixel in video clock
|
| 39 |
|
|
input [4:0] maxpix; // maximum pixels for width / height of character
|
| 40 |
|
|
output nxt_pix; // when the next pixel will happen
|
| 41 |
|
|
output [11:0] pos; // current row or column position
|
| 42 |
|
|
output nxt_pos; // flag: when the row or column is about to change
|
| 43 |
|
|
output [11:0] ctr; // counter output
|
| 44 |
|
|
|
| 45 |
|
|
reg [11:0] pos;
|
| 46 |
|
|
reg [11:0] ctr;
|
| 47 |
|
|
reg nxt_pix;
|
| 48 |
|
|
|
| 49 |
|
|
wire [11:0] ctr1;
|
| 50 |
|
|
wire nxp;
|
| 51 |
|
|
reg [23:0] x4096;
|
| 52 |
|
|
|
| 53 |
|
|
// Lookup reciprocal of number of pixels per character
|
| 54 |
|
|
// - used to calculate the column position
|
| 55 |
|
|
reg [11:0] inv;
|
| 56 |
|
|
always @(posedge vclk)
|
| 57 |
|
|
case(maxpix)
|
| 58 |
|
|
5'd00: inv <= 12'd4095;
|
| 59 |
|
|
5'd01: inv <= 12'd2048;
|
| 60 |
|
|
5'd02: inv <= 12'd1365;
|
| 61 |
|
|
5'd03: inv <= 12'd1024;
|
| 62 |
|
|
5'd04: inv <= 12'd0819;
|
| 63 |
|
|
5'd05: inv <= 12'd0683;
|
| 64 |
|
|
5'd06: inv <= 12'd0585;
|
| 65 |
|
|
5'd07: inv <= 12'd0512;
|
| 66 |
|
|
5'd08: inv <= 12'd0455;
|
| 67 |
|
|
5'd09: inv <= 12'd0409;
|
| 68 |
|
|
5'd10: inv <= 12'd0372;
|
| 69 |
|
|
5'd11: inv <= 12'd0341;
|
| 70 |
|
|
5'd12: inv <= 12'd0315;
|
| 71 |
|
|
5'd13: inv <= 12'd0292;
|
| 72 |
|
|
5'd14: inv <= 12'd0273;
|
| 73 |
|
|
5'd15: inv <= 12'd0256;
|
| 74 |
|
|
5'd16: inv <= 12'd0240;
|
| 75 |
|
|
5'd17: inv <= 12'd0227;
|
| 76 |
|
|
5'd18: inv <= 12'd0215;
|
| 77 |
|
|
5'd19: inv <= 12'd0204;
|
| 78 |
|
|
5'd20: inv <= 12'd0195;
|
| 79 |
|
|
5'd21: inv <= 12'd0186;
|
| 80 |
|
|
5'd22: inv <= 12'd0178;
|
| 81 |
|
|
5'd23: inv <= 12'd0170;
|
| 82 |
|
|
5'd24: inv <= 12'd0163;
|
| 83 |
|
|
5'd25: inv <= 12'd0157;
|
| 84 |
|
|
5'd26: inv <= 12'd0151;
|
| 85 |
|
|
5'd27: inv <= 12'd0146;
|
| 86 |
|
|
5'd28: inv <= 12'd0141;
|
| 87 |
|
|
5'd29: inv <= 12'd0136;
|
| 88 |
|
|
5'd30: inv <= 12'd0132;
|
| 89 |
|
|
5'd31: inv <= 12'd0128;
|
| 90 |
|
|
endcase
|
| 91 |
|
|
|
| 92 |
|
|
|
| 93 |
|
|
// Calculate character position
|
| 94 |
|
|
// - divide the raw count by the number of pixels per character
|
| 95 |
|
|
// - done by multiplying by the reciprocal
|
| 96 |
|
|
always @(posedge vclk)
|
| 97 |
|
|
x4096 <= ctr * inv;
|
| 98 |
|
|
always @(x4096)
|
| 99 |
|
|
pos <= x4096[23:12];
|
| 100 |
|
|
always @(posedge vclk) // pipeline delay
|
| 101 |
|
|
ctr <= ctr1;
|
| 102 |
|
|
always @(posedge vclk)
|
| 103 |
|
|
nxt_pix <= nxp;
|
| 104 |
|
|
|
| 105 |
|
|
// Pixel width counter
|
| 106 |
|
|
// Controls number of clock cycles per pixel
|
| 107 |
|
|
VT163 #(4) u1
|
| 108 |
|
|
(
|
| 109 |
|
|
.clk(vclk),
|
| 110 |
|
|
.clr_n(!rst),
|
| 111 |
|
|
.ent(pixcce),
|
| 112 |
|
|
.enp(1'b1),
|
| 113 |
|
|
.ld_n(!sync & !nxp), // synchronize count to start of scan
|
| 114 |
|
|
.d(4'hF-pixsz),
|
| 115 |
|
|
.q(),
|
| 116 |
|
|
.rco(nxp)
|
| 117 |
|
|
);
|
| 118 |
|
|
|
| 119 |
|
|
|
| 120 |
|
|
// Pixel counter
|
| 121 |
|
|
// - raw pixel count
|
| 122 |
|
|
// - just increments every time the nxt_pix signal is active
|
| 123 |
|
|
// - synchronized to the end-of-line or end-of-frame signal
|
| 124 |
|
|
VT163 #(12) u2
|
| 125 |
|
|
(
|
| 126 |
|
|
.clk(vclk),
|
| 127 |
|
|
.clr_n(!rst),
|
| 128 |
|
|
.ent(nxp),
|
| 129 |
|
|
.enp(1'b1),
|
| 130 |
|
|
.ld_n(!sync), // synchronize count to start of scan
|
| 131 |
|
|
.d(12'h000-cnt_offs),
|
| 132 |
|
|
.q(ctr1),
|
| 133 |
|
|
.rco()
|
| 134 |
|
|
);
|
| 135 |
|
|
|
| 136 |
|
|
|
| 137 |
|
|
// Detect when the position changes
|
| 138 |
|
|
// - compare current pos to previous pos when the position might change
|
| 139 |
|
|
change_det #(12) u3
|
| 140 |
|
|
(
|
| 141 |
|
|
.rst(rst),
|
| 142 |
|
|
.clk(vclk),
|
| 143 |
|
|
.ce(nxt_pix),
|
| 144 |
|
|
.i(pos),
|
| 145 |
|
|
.cd(nxt_pos)
|
| 146 |
|
|
);
|
| 147 |
|
|
|
| 148 |
|
|
endmodule
|