paul@41 | 1 | /* |
paul@41 | 2 | * Common display-related functions. |
paul@41 | 3 | * |
paul@41 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@41 | 5 | * |
paul@41 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@41 | 7 | * it under the terms of the GNU General Public License as published by |
paul@41 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@41 | 9 | * (at your option) any later version. |
paul@41 | 10 | * |
paul@41 | 11 | * This program is distributed in the hope that it will be useful, |
paul@41 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@41 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@41 | 14 | * GNU General Public License for more details. |
paul@41 | 15 | * |
paul@41 | 16 | * You should have received a copy of the GNU General Public License |
paul@41 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@41 | 18 | */ |
paul@41 | 19 | |
paul@41 | 20 | #include "display.h" |
paul@41 | 21 | |
paul@41 | 22 | |
paul@41 | 23 | |
paul@93 | 24 | /* Initialise a display configuration. */ |
paul@93 | 25 | |
paul@93 | 26 | void init_display(display_config_t *cfg, uint8_t *framebuffer, |
paul@93 | 27 | uint32_t line_length, uint32_t line_count, int frames) |
paul@93 | 28 | { |
paul@93 | 29 | /* Framebuffer address. */ |
paul@93 | 30 | |
paul@93 | 31 | cfg->framebuffer = framebuffer; |
paul@93 | 32 | |
paul@93 | 33 | /* Frame allocation limits. */ |
paul@93 | 34 | |
paul@93 | 35 | cfg->total_lines = (line_count + 1) * frames; |
paul@93 | 36 | cfg->max_frames = frames; |
paul@93 | 37 | |
paul@93 | 38 | /* Screen size and dimensions. */ |
paul@93 | 39 | |
paul@93 | 40 | cfg->screen_size = line_length * line_count; |
paul@93 | 41 | cfg->line_length = line_length; |
paul@93 | 42 | cfg->line_count = line_count; |
paul@93 | 43 | |
paul@93 | 44 | /* Set the number of frames and the current frame. */ |
paul@93 | 45 | |
paul@93 | 46 | cfg->frames = frames; |
paul@93 | 47 | cfg->frame = 0; |
paul@93 | 48 | |
paul@93 | 49 | init_display_properties(cfg, 0); |
paul@93 | 50 | } |
paul@93 | 51 | |
paul@93 | 52 | /* Initialise the display constraints. */ |
paul@93 | 53 | |
paul@93 | 54 | void init_display_properties(display_config_t *cfg, uint32_t offset) |
paul@93 | 55 | { |
paul@93 | 56 | /* Fixed address of the frame. */ |
paul@93 | 57 | |
paul@93 | 58 | cfg->frame_start = cfg->framebuffer + |
paul@93 | 59 | (cfg->screen_size + cfg->line_length) * cfg->frame; |
paul@93 | 60 | |
paul@93 | 61 | /* Floating address of the screen contents. */ |
paul@93 | 62 | |
paul@93 | 63 | cfg->screen_start = cfg->frame_start + offset; |
paul@93 | 64 | |
paul@93 | 65 | /* Fixed limit of the frame. */ |
paul@93 | 66 | |
paul@93 | 67 | cfg->screen_limit = cfg->frame_start + cfg->screen_size; |
paul@93 | 68 | |
paul@93 | 69 | /* Recalculate the line multiplier. */ |
paul@93 | 70 | |
paul@93 | 71 | cfg->line_multiplier = cfg->scanlines / cfg->line_count; |
paul@93 | 72 | } |
paul@93 | 73 | |
paul@93 | 74 | /* Set the number of frames in the framebuffer memory. */ |
paul@93 | 75 | |
paul@93 | 76 | void set_frames(display_config_t *cfg, int frames) |
paul@93 | 77 | { |
paul@95 | 78 | if ((frames <= 0) || (frames > cfg->max_frames)) |
paul@95 | 79 | return; |
paul@95 | 80 | |
paul@93 | 81 | /* Recalculate the number of lines. */ |
paul@93 | 82 | |
paul@93 | 83 | cfg->line_count = (cfg->total_lines - cfg->max_frames) / frames; |
paul@93 | 84 | |
paul@93 | 85 | /* Recalculate the screen size. */ |
paul@93 | 86 | |
paul@93 | 87 | cfg->screen_size = cfg->line_count * cfg->line_length; |
paul@93 | 88 | |
paul@93 | 89 | /* Set the number of frames and the current frame. */ |
paul@93 | 90 | |
paul@93 | 91 | cfg->frames = frames; |
paul@93 | 92 | cfg->frame = 0; |
paul@93 | 93 | |
paul@93 | 94 | init_display_properties(cfg, 0); |
paul@93 | 95 | } |
paul@93 | 96 | |
paul@93 | 97 | /* Select a frame in the framebuffer. */ |
paul@93 | 98 | |
paul@93 | 99 | void select_frame(display_config_t *cfg, int frame, uint32_t offset) |
paul@93 | 100 | { |
paul@93 | 101 | if ((frame < 0) || (frame >= cfg->frames)) |
paul@93 | 102 | return; |
paul@93 | 103 | |
paul@93 | 104 | /* Update the frame details. */ |
paul@93 | 105 | |
paul@93 | 106 | cfg->frame = frame; |
paul@93 | 107 | |
paul@93 | 108 | /* Set the screen start offset when switching frames. */ |
paul@93 | 109 | |
paul@93 | 110 | init_display_properties(cfg, offset); |
paul@93 | 111 | } |
paul@93 | 112 | |
paul@93 | 113 | /* Return the screen start offset. */ |
paul@93 | 114 | |
paul@93 | 115 | uint32_t get_start_offset(display_config_t *cfg) |
paul@93 | 116 | { |
paul@93 | 117 | return cfg->screen_start - cfg->frame_start; |
paul@93 | 118 | } |
paul@93 | 119 | |
paul@46 | 120 | /* Return the line data position for the given pixel. */ |
paul@46 | 121 | |
paul@49 | 122 | int get_position(display_config_t *cfg, int x) |
paul@46 | 123 | { |
paul@106 | 124 | int cell, offset, pos; |
paul@106 | 125 | |
paul@106 | 126 | if (cfg->line_channels < 2) |
paul@106 | 127 | return x; |
paul@106 | 128 | |
paul@106 | 129 | /* Determine which cell is providing the position and the offset of the |
paul@106 | 130 | pixel within the cell. */ |
paul@106 | 131 | |
paul@106 | 132 | cell = x / cfg->cell_size; |
paul@106 | 133 | offset = x % cfg->cell_size; |
paul@106 | 134 | |
paul@106 | 135 | /* Determine the resulting position within the divided-up data. */ |
paul@106 | 136 | |
paul@106 | 137 | pos = (cell / 2) * cfg->cell_size + offset; |
paul@106 | 138 | |
paul@106 | 139 | /* Return the final position within the entire data. All cells in |
paul@106 | 140 | odd-numbered positions occur in the first half, all even-numbered cells |
paul@106 | 141 | in the second half. */ |
paul@46 | 142 | |
paul@49 | 143 | return cell % 2 ? pos + cfg->line_length / 2 : pos; |
paul@46 | 144 | } |
paul@46 | 145 | |
paul@41 | 146 | /* Provide a pattern to test the line data. */ |
paul@41 | 147 | |
paul@49 | 148 | void test_linedata(display_config_t *cfg) |
paul@41 | 149 | { |
paul@41 | 150 | int x, y; |
paul@83 | 151 | uint8_t *linedata = cfg->screen_start; |
paul@41 | 152 | |
paul@49 | 153 | for (y = 0; y < cfg->line_count; y++) |
paul@41 | 154 | { |
paul@49 | 155 | for (x = 0; x < cfg->line_length; x++) |
paul@41 | 156 | { |
paul@41 | 157 | /* Pixel: I0RRGGBB = Y0YYYYXX */ |
paul@41 | 158 | |
paul@49 | 159 | linedata[get_position(cfg, x)] = (x % 2) ? |
paul@49 | 160 | (((y / (cfg->line_count / 32)) & 0b1) << 7) | |
paul@49 | 161 | (((y / (cfg->line_count / 16)) & 0b1111) << 2) | |
paul@49 | 162 | ((x / (cfg->line_length / 4)) & 0b11) : |
paul@46 | 163 | 0x00; |
paul@41 | 164 | } |
paul@41 | 165 | |
paul@100 | 166 | linedata = wrap_screen_pointer(cfg, linedata + cfg->line_length); |
paul@41 | 167 | } |
paul@41 | 168 | } |
paul@69 | 169 | |
paul@74 | 170 | /* Copying from/to the display to/from a backing store. */ |
paul@69 | 171 | |
paul@101 | 172 | void copy_display(display_config_t *cfg, uint8_t *store, |
paul@101 | 173 | int width, int height, int ystep, |
paul@74 | 174 | int x, int y, int key, int to_display) |
paul@69 | 175 | { |
paul@101 | 176 | copy_display_section(cfg, store, width, height, |
paul@101 | 177 | 0, 0, width, height, ystep, |
paul@101 | 178 | x, y, key, to_display); |
paul@83 | 179 | } |
paul@83 | 180 | |
paul@83 | 181 | /* Copying from/to the display to/from a backing store region. */ |
paul@83 | 182 | |
paul@83 | 183 | void copy_display_section(display_config_t *cfg, uint8_t *store, |
paul@83 | 184 | int width, int height, |
paul@101 | 185 | int xstart, int ystart, int xsize, int ysize, int ystep, |
paul@83 | 186 | int x, int y, int key, int to_display) |
paul@83 | 187 | { |
paul@74 | 188 | int sx, sy, dx, dy; |
paul@83 | 189 | uint8_t *storeline = store + ystart * width, |
paul@100 | 190 | *displayline = wrap_screen_pointer(cfg, cfg->screen_start + y * cfg->line_length), |
paul@74 | 191 | pixel; |
paul@69 | 192 | |
paul@83 | 193 | /* Define the limits of the copying in the store. */ |
paul@83 | 194 | |
paul@83 | 195 | int xlimit = xstart + xsize, ylimit = ystart + ysize; |
paul@83 | 196 | |
paul@83 | 197 | if (xlimit > width) |
paul@83 | 198 | xlimit = width; |
paul@83 | 199 | |
paul@83 | 200 | if (ylimit > height) |
paul@83 | 201 | ylimit = height; |
paul@83 | 202 | |
paul@83 | 203 | /* Perform the copying between the store and display. */ |
paul@83 | 204 | |
paul@101 | 205 | for (sy = ystart, dy = y; (sy < ylimit) && (dy < cfg->line_count); sy += ystep, dy++) |
paul@69 | 206 | { |
paul@83 | 207 | for (sx = xstart, dx = x; (sx < xlimit) && (dx < cfg->line_length); sx++, dx++) |
paul@69 | 208 | { |
paul@74 | 209 | if (to_display) |
paul@74 | 210 | { |
paul@74 | 211 | pixel = storeline[sx]; |
paul@74 | 212 | if ((key < 0) || (pixel != key)) |
paul@74 | 213 | displayline[get_position(cfg, dx)] = pixel; |
paul@74 | 214 | } |
paul@74 | 215 | else |
paul@74 | 216 | storeline[sx] = displayline[get_position(cfg, dx)]; |
paul@69 | 217 | } |
paul@69 | 218 | |
paul@101 | 219 | storeline += width * ystep; |
paul@100 | 220 | displayline = wrap_screen_pointer(cfg, displayline + cfg->line_length); |
paul@69 | 221 | } |
paul@69 | 222 | } |
paul@83 | 223 | |
paul@83 | 224 | /* Scroll the display. */ |
paul@83 | 225 | |
paul@83 | 226 | void scroll_display(display_config_t *cfg, int x, int y) |
paul@83 | 227 | { |
paul@100 | 228 | /* Move the screen start by the given number of bytes and lines, wrapping |
paul@100 | 229 | around the start and end of the framebuffer. */ |
paul@83 | 230 | |
paul@100 | 231 | cfg->screen_start = wrap_screen_pointer(cfg, cfg->screen_start + x + |
paul@100 | 232 | y * cfg->line_length); |
paul@100 | 233 | } |
paul@83 | 234 | |
paul@100 | 235 | /* Wrap a pointer within the given limits. */ |
paul@100 | 236 | |
paul@100 | 237 | uint8_t *wrap_pointer(uint8_t *ptr, uint8_t *lower, uint8_t *upper) |
paul@100 | 238 | { |
paul@100 | 239 | uint32_t size = upper - lower; |
paul@83 | 240 | |
paul@100 | 241 | if (ptr < lower) |
paul@100 | 242 | return upper - (lower - ptr) % size; |
paul@100 | 243 | else if (ptr >= upper) |
paul@100 | 244 | return lower + (ptr - upper) % size; |
paul@100 | 245 | else |
paul@100 | 246 | return ptr; |
paul@100 | 247 | } |
paul@83 | 248 | |
paul@100 | 249 | /* Wrap the screen pointer to the current frame. */ |
paul@83 | 250 | |
paul@100 | 251 | uint8_t *wrap_screen_pointer(display_config_t *cfg, uint8_t *ptr) |
paul@100 | 252 | { |
paul@100 | 253 | return wrap_pointer(ptr, cfg->frame_start, cfg->screen_limit); |
paul@83 | 254 | } |