| 1 |
25 |
robfinch |
`timescale 1ns / 1ps
|
| 2 |
|
|
// ============================================================================
|
| 3 |
|
|
// __
|
| 4 |
|
|
// \\__/ o\ (C) 2015-2023 Robert Finch, Waterloo
|
| 5 |
|
|
// \ __ / All rights reserved.
|
| 6 |
|
|
// \/_// robfinch@finitron.ca
|
| 7 |
|
|
// ||
|
| 8 |
|
|
//
|
| 9 |
|
|
//
|
| 10 |
|
|
// BSD 3-Clause License
|
| 11 |
|
|
// Redistribution and use in source and binary forms, with or without
|
| 12 |
|
|
// modification, are permitted provided that the following conditions are met:
|
| 13 |
|
|
//
|
| 14 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
| 15 |
|
|
// list of conditions and the following disclaimer.
|
| 16 |
|
|
//
|
| 17 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 18 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
| 19 |
|
|
// and/or other materials provided with the distribution.
|
| 20 |
|
|
//
|
| 21 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
| 22 |
|
|
// contributors may be used to endorse or promote products derived from
|
| 23 |
|
|
// this software without specific prior written permission.
|
| 24 |
|
|
//
|
| 25 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 26 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 27 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 28 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 29 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 30 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 31 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 32 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 33 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 34 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 35 |
|
|
//
|
| 36 |
|
|
// ============================================================================
|
| 37 |
|
|
//
|
| 38 |
|
|
// Compute the graphics address
|
| 39 |
|
|
//
|
| 40 |
|
|
import gfx_pkg::*;
|
| 41 |
|
|
|
| 42 |
|
|
module gfx_calc_address(clk, base_address_i, color_depth_i, bmp_width_i, x_coord_i, y_coord_i,
|
| 43 |
|
|
address_o, mb_o, me_o, ce_o);
|
| 44 |
|
|
parameter SW = 128; // strip width in bits
|
| 45 |
|
|
parameter BN = 6;
|
| 46 |
|
|
input clk;
|
| 47 |
|
|
input [31:0] base_address_i;
|
| 48 |
|
|
input [1:0] color_depth_i;
|
| 49 |
|
|
input [15:0] bmp_width_i; // pixel per line
|
| 50 |
|
|
input [15:0] x_coord_i;
|
| 51 |
|
|
input [15:0] y_coord_i;
|
| 52 |
|
|
output [31:0] address_o;
|
| 53 |
|
|
output [BN:0] mb_o; // mask begin
|
| 54 |
|
|
output [BN:0] me_o; // mask end
|
| 55 |
|
|
output [BN:0] ce_o; // color bits end
|
| 56 |
|
|
|
| 57 |
|
|
// This coefficient is a fixed point fraction representing the inverse of the
|
| 58 |
|
|
// number of pixels per strip. The inverse (reciprocal) is used for a high
|
| 59 |
|
|
// speed divide operation.
|
| 60 |
|
|
reg [15:0] coeff;
|
| 61 |
|
|
always_comb
|
| 62 |
|
|
case(color_depth_i)
|
| 63 |
|
|
BPP8: coeff = 65536*8/SW;
|
| 64 |
|
|
BPP16: coeff = 65536*16/SW;
|
| 65 |
|
|
BPP24: coeff = 65536*24/SW;
|
| 66 |
|
|
BPP32: coeff = 65536*32/SW;
|
| 67 |
|
|
default: coeff = 65536*16/SW;
|
| 68 |
|
|
endcase
|
| 69 |
|
|
|
| 70 |
|
|
// Bits per pixel minus one.
|
| 71 |
|
|
reg [5:0] bpp;
|
| 72 |
|
|
always_comb
|
| 73 |
|
|
case(color_depth_i)
|
| 74 |
|
|
BPP8: bpp = 7;
|
| 75 |
|
|
BPP16: bpp = 15;
|
| 76 |
|
|
BPP24: bpp = 23;
|
| 77 |
|
|
BPP32: bpp = 31;
|
| 78 |
|
|
default: bpp = 15;
|
| 79 |
|
|
endcase
|
| 80 |
|
|
|
| 81 |
|
|
// Color bits per pixel minus one.
|
| 82 |
|
|
reg [5:0] cbpp;
|
| 83 |
|
|
always_comb
|
| 84 |
|
|
case(color_depth_i)
|
| 85 |
|
|
BPP8: cbpp = 4;
|
| 86 |
|
|
BPP16: cbpp = 11;
|
| 87 |
|
|
BPP24: cbpp = 20;
|
| 88 |
|
|
BPP32: cbpp = 26;
|
| 89 |
|
|
default: cbpp = 11;
|
| 90 |
|
|
endcase
|
| 91 |
|
|
|
| 92 |
|
|
// This coefficient is the number of bits used by all pixels in the strip.
|
| 93 |
|
|
// Used to determine pixel placement in the strip.
|
| 94 |
|
|
reg [7:0] coeff2;
|
| 95 |
|
|
always_comb
|
| 96 |
|
|
case(color_depth_i)
|
| 97 |
|
|
BPP8: coeff2 = SW-(SW % 8);
|
| 98 |
|
|
BPP16: coeff2 = SW-(SW % 16);
|
| 99 |
|
|
BPP24: coeff2 = SW-(SW % 24);
|
| 100 |
|
|
BPP32: coeff2 = SW-(SW % 32);
|
| 101 |
|
|
default: coeff2 = SW-(SW % 16);
|
| 102 |
|
|
endcase
|
| 103 |
|
|
|
| 104 |
|
|
// Compute the fixed point horizonal strip number value. This has 16 binary
|
| 105 |
|
|
// point places.
|
| 106 |
|
|
wire [31:0] strip_num65k = x_coord_i * coeff;
|
| 107 |
|
|
// Truncate off the binary fraction to get the strip number. The strip
|
| 108 |
|
|
// number will be used to form part of the address.
|
| 109 |
|
|
wire [17:0] strip_num = strip_num65k[31:16];
|
| 110 |
|
|
// Calculate pixel position within strip using the fractional part of the
|
| 111 |
|
|
// horizontal strip number.
|
| 112 |
|
|
wire [15:0] strip_fract = strip_num65k[15:0]+16'h7F; // +7F to round
|
| 113 |
|
|
// Pixel beginning bit is ratio of pixel # into all bits used by pixels
|
| 114 |
|
|
wire [15:0] ndx = strip_fract[15:7] * coeff2;
|
| 115 |
|
|
assign mb_o = ndx[15:9]; // Get whole pixel position (discard fraction)
|
| 116 |
|
|
assign me_o = mb_o + bpp; // Set high order position for mask
|
| 117 |
|
|
assign ce_o = mb_o + cbpp;
|
| 118 |
|
|
// num_strips is essentially a constant value unless the screen resolution changes.
|
| 119 |
|
|
// Gain performance here by regstering the multiply so that there aren't two
|
| 120 |
|
|
// cascaded multiplies when calculating the offset.
|
| 121 |
|
|
reg [31:0] num_strips65k;
|
| 122 |
|
|
always_ff @(posedge clk)
|
| 123 |
|
|
num_strips65k <= bmp_width_i * coeff;
|
| 124 |
|
|
wire [15:0] num_strips = num_strips65k[31:16];
|
| 125 |
|
|
|
| 126 |
|
|
wire [31:0] offset = {(({4'b0,num_strips} * y_coord_i) + strip_num),4'h0};
|
| 127 |
|
|
|
| 128 |
|
|
assign address_o = base_address_i + offset;
|
| 129 |
|
|
|
| 130 |
|
|
endmodule
|