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@111 | 21 | #include "utils.h" |
paul@41 | 22 | |
paul@41 | 23 | |
paul@41 | 24 | |
paul@93 | 25 | /* Initialise a display configuration. */ |
paul@93 | 26 | |
paul@93 | 27 | void init_display(display_config_t *cfg, uint8_t *framebuffer, |
paul@115 | 28 | uint32_t line_length, uint32_t line_count, |
paul@115 | 29 | int frames, uint8_t **screen_starts) |
paul@93 | 30 | { |
paul@93 | 31 | /* Framebuffer address. */ |
paul@93 | 32 | |
paul@93 | 33 | cfg->framebuffer = framebuffer; |
paul@93 | 34 | |
paul@93 | 35 | /* Frame allocation limits. */ |
paul@93 | 36 | |
paul@93 | 37 | cfg->total_lines = (line_count + 1) * frames; |
paul@93 | 38 | cfg->max_frames = frames; |
paul@93 | 39 | |
paul@93 | 40 | /* Screen size and dimensions. */ |
paul@93 | 41 | |
paul@93 | 42 | cfg->screen_size = line_length * line_count; |
paul@93 | 43 | cfg->line_length = line_length; |
paul@93 | 44 | cfg->line_count = line_count; |
paul@93 | 45 | |
paul@93 | 46 | /* Set the number of frames and the current frame. */ |
paul@93 | 47 | |
paul@93 | 48 | cfg->frames = frames; |
paul@93 | 49 | cfg->frame = 0; |
paul@93 | 50 | |
paul@115 | 51 | /* Set the pointer to a collection of screen start pointers, one per |
paul@115 | 52 | frame. */ |
paul@115 | 53 | |
paul@115 | 54 | cfg->screen_starts = screen_starts; |
paul@115 | 55 | |
paul@115 | 56 | init_frames(cfg); |
paul@115 | 57 | init_display_properties(cfg); |
paul@93 | 58 | } |
paul@93 | 59 | |
paul@93 | 60 | /* Initialise the display constraints. */ |
paul@93 | 61 | |
paul@115 | 62 | void init_display_properties(display_config_t *cfg) |
paul@93 | 63 | { |
paul@93 | 64 | /* Fixed address of the frame. */ |
paul@93 | 65 | |
paul@115 | 66 | cfg->frame_start = display_get_frame_start(cfg, cfg->frame); |
paul@93 | 67 | |
paul@93 | 68 | /* Floating address of the screen contents. */ |
paul@93 | 69 | |
paul@115 | 70 | if (cfg->screen_starts) |
paul@115 | 71 | cfg->screen_start = cfg->screen_starts[cfg->frame]; |
paul@115 | 72 | |
paul@115 | 73 | /* Without any way of recording the address, just reset the start. */ |
paul@115 | 74 | |
paul@115 | 75 | else |
paul@115 | 76 | cfg->screen_start = cfg->frame_start; |
paul@93 | 77 | |
paul@93 | 78 | /* Fixed limit of the frame. */ |
paul@93 | 79 | |
paul@93 | 80 | cfg->screen_limit = cfg->frame_start + cfg->screen_size; |
paul@93 | 81 | |
paul@93 | 82 | /* Recalculate the line multiplier. */ |
paul@93 | 83 | |
paul@93 | 84 | cfg->line_multiplier = cfg->scanlines / cfg->line_count; |
paul@93 | 85 | } |
paul@93 | 86 | |
paul@115 | 87 | /* Initialise the screen start addresses for all frames. */ |
paul@115 | 88 | |
paul@115 | 89 | void init_frames(display_config_t *cfg) |
paul@115 | 90 | { |
paul@115 | 91 | int frame; |
paul@115 | 92 | |
paul@115 | 93 | for (frame = 0; frame < cfg->frames; frame++) |
paul@115 | 94 | cfg->screen_starts[frame] = display_get_frame_start(cfg, frame); |
paul@115 | 95 | } |
paul@115 | 96 | |
paul@111 | 97 | |
paul@111 | 98 | |
paul@111 | 99 | /* Select a frame in the framebuffer. */ |
paul@111 | 100 | |
paul@115 | 101 | void display_select_frame(display_config_t *cfg, int frame) |
paul@111 | 102 | { |
paul@111 | 103 | if ((frame < 0) || (frame >= cfg->frames)) |
paul@111 | 104 | return; |
paul@111 | 105 | |
paul@115 | 106 | /* Store the current frame's screen start, if possible. */ |
paul@115 | 107 | |
paul@115 | 108 | if (cfg->screen_starts) |
paul@115 | 109 | cfg->screen_starts[cfg->frame] = cfg->screen_start; |
paul@115 | 110 | |
paul@111 | 111 | /* Update the frame details. */ |
paul@111 | 112 | |
paul@111 | 113 | cfg->frame = frame; |
paul@111 | 114 | |
paul@111 | 115 | /* Set the screen start offset when switching frames. */ |
paul@111 | 116 | |
paul@115 | 117 | init_display_properties(cfg); |
paul@111 | 118 | } |
paul@111 | 119 | |
paul@93 | 120 | /* Set the number of frames in the framebuffer memory. */ |
paul@93 | 121 | |
paul@111 | 122 | void display_set_frames(display_config_t *cfg, int frames) |
paul@93 | 123 | { |
paul@95 | 124 | if ((frames <= 0) || (frames > cfg->max_frames)) |
paul@95 | 125 | return; |
paul@95 | 126 | |
paul@93 | 127 | /* Recalculate the number of lines. */ |
paul@93 | 128 | |
paul@93 | 129 | cfg->line_count = (cfg->total_lines - cfg->max_frames) / frames; |
paul@93 | 130 | |
paul@93 | 131 | /* Recalculate the screen size. */ |
paul@93 | 132 | |
paul@93 | 133 | cfg->screen_size = cfg->line_count * cfg->line_length; |
paul@93 | 134 | |
paul@93 | 135 | /* Set the number of frames and the current frame. */ |
paul@93 | 136 | |
paul@93 | 137 | cfg->frames = frames; |
paul@93 | 138 | cfg->frame = 0; |
paul@93 | 139 | |
paul@115 | 140 | init_frames(cfg); |
paul@115 | 141 | init_display_properties(cfg); |
paul@93 | 142 | } |
paul@93 | 143 | |
paul@93 | 144 | |
paul@93 | 145 | |
paul@115 | 146 | /* Return the start address of the given frame. */ |
paul@115 | 147 | |
paul@115 | 148 | uint8_t *display_get_frame_start(display_config_t *cfg, int frame) |
paul@115 | 149 | { |
paul@115 | 150 | return cfg->framebuffer + (cfg->screen_size + cfg->line_length) * frame; |
paul@115 | 151 | } |
paul@115 | 152 | |
paul@46 | 153 | /* Return the line data position for the given pixel. */ |
paul@46 | 154 | |
paul@111 | 155 | int display_get_position(display_config_t *cfg, int x) |
paul@46 | 156 | { |
paul@106 | 157 | int cell, offset, pos; |
paul@106 | 158 | |
paul@106 | 159 | if (cfg->line_channels < 2) |
paul@106 | 160 | return x; |
paul@106 | 161 | |
paul@106 | 162 | /* Determine which cell is providing the position and the offset of the |
paul@106 | 163 | pixel within the cell. */ |
paul@106 | 164 | |
paul@106 | 165 | cell = x / cfg->cell_size; |
paul@106 | 166 | offset = x % cfg->cell_size; |
paul@106 | 167 | |
paul@106 | 168 | /* Determine the resulting position within the divided-up data. */ |
paul@106 | 169 | |
paul@106 | 170 | pos = (cell / 2) * cfg->cell_size + offset; |
paul@106 | 171 | |
paul@106 | 172 | /* Return the final position within the entire data. All cells in |
paul@106 | 173 | odd-numbered positions occur in the first half, all even-numbered cells |
paul@106 | 174 | in the second half. */ |
paul@46 | 175 | |
paul@49 | 176 | return cell % 2 ? pos + cfg->line_length / 2 : pos; |
paul@46 | 177 | } |
paul@46 | 178 | |
paul@111 | 179 | /* Return the screen start offset. */ |
paul@41 | 180 | |
paul@111 | 181 | uint32_t display_get_start_offset(display_config_t *cfg) |
paul@111 | 182 | { |
paul@111 | 183 | return cfg->screen_start - cfg->frame_start; |
paul@111 | 184 | } |
paul@41 | 185 | |
paul@41 | 186 | |
paul@69 | 187 | |
paul@74 | 188 | /* Copying from/to the display to/from a backing store. */ |
paul@69 | 189 | |
paul@111 | 190 | void display_copy(display_config_t *cfg, uint8_t *store, |
paul@101 | 191 | int width, int height, int ystep, |
paul@74 | 192 | int x, int y, int key, int to_display) |
paul@69 | 193 | { |
paul@111 | 194 | display_copy_section(cfg, store, width, height, |
paul@101 | 195 | 0, 0, width, height, ystep, |
paul@101 | 196 | x, y, key, to_display); |
paul@83 | 197 | } |
paul@83 | 198 | |
paul@83 | 199 | /* Copying from/to the display to/from a backing store region. */ |
paul@83 | 200 | |
paul@111 | 201 | void display_copy_section(display_config_t *cfg, uint8_t *store, |
paul@83 | 202 | int width, int height, |
paul@101 | 203 | int xstart, int ystart, int xsize, int ysize, int ystep, |
paul@83 | 204 | int x, int y, int key, int to_display) |
paul@83 | 205 | { |
paul@74 | 206 | int sx, sy, dx, dy; |
paul@83 | 207 | uint8_t *storeline = store + ystart * width, |
paul@111 | 208 | *displayline = display_wrap_pointer(cfg, cfg->screen_start + y * cfg->line_length), |
paul@74 | 209 | pixel; |
paul@69 | 210 | |
paul@83 | 211 | /* Define the limits of the copying in the store. */ |
paul@83 | 212 | |
paul@83 | 213 | int xlimit = xstart + xsize, ylimit = ystart + ysize; |
paul@83 | 214 | |
paul@83 | 215 | if (xlimit > width) |
paul@83 | 216 | xlimit = width; |
paul@83 | 217 | |
paul@83 | 218 | if (ylimit > height) |
paul@83 | 219 | ylimit = height; |
paul@83 | 220 | |
paul@83 | 221 | /* Perform the copying between the store and display. */ |
paul@83 | 222 | |
paul@101 | 223 | for (sy = ystart, dy = y; (sy < ylimit) && (dy < cfg->line_count); sy += ystep, dy++) |
paul@69 | 224 | { |
paul@83 | 225 | for (sx = xstart, dx = x; (sx < xlimit) && (dx < cfg->line_length); sx++, dx++) |
paul@69 | 226 | { |
paul@74 | 227 | if (to_display) |
paul@74 | 228 | { |
paul@74 | 229 | pixel = storeline[sx]; |
paul@74 | 230 | if ((key < 0) || (pixel != key)) |
paul@111 | 231 | displayline[display_get_position(cfg, dx)] = pixel; |
paul@74 | 232 | } |
paul@74 | 233 | else |
paul@111 | 234 | storeline[sx] = displayline[display_get_position(cfg, dx)]; |
paul@69 | 235 | } |
paul@69 | 236 | |
paul@101 | 237 | storeline += width * ystep; |
paul@111 | 238 | displayline = display_wrap_pointer(cfg, displayline + cfg->line_length); |
paul@69 | 239 | } |
paul@69 | 240 | } |
paul@83 | 241 | |
paul@83 | 242 | /* Scroll the display. */ |
paul@83 | 243 | |
paul@111 | 244 | void display_scroll(display_config_t *cfg, int x, int y) |
paul@83 | 245 | { |
paul@100 | 246 | /* Move the screen start by the given number of bytes and lines, wrapping |
paul@100 | 247 | around the start and end of the framebuffer. */ |
paul@83 | 248 | |
paul@111 | 249 | cfg->screen_start = display_wrap_pointer(cfg, cfg->screen_start + x + |
paul@111 | 250 | y * cfg->line_length); |
paul@100 | 251 | } |
paul@83 | 252 | |
paul@111 | 253 | /* Provide a pattern to test the line data. */ |
paul@100 | 254 | |
paul@111 | 255 | void display_test_linedata(display_config_t *cfg) |
paul@100 | 256 | { |
paul@111 | 257 | int x, y; |
paul@111 | 258 | uint8_t *linedata = cfg->screen_start; |
paul@83 | 259 | |
paul@111 | 260 | for (y = 0; y < cfg->line_count; y++) |
paul@111 | 261 | { |
paul@111 | 262 | for (x = 0; x < cfg->line_length; x++) |
paul@111 | 263 | { |
paul@111 | 264 | /* Pixel: I0RRGGBB = Y0YYYYXX */ |
paul@111 | 265 | |
paul@111 | 266 | linedata[display_get_position(cfg, x)] = (x % 2) ? |
paul@111 | 267 | (((y / (cfg->line_count / 32)) & 0b1) << 7) | |
paul@111 | 268 | (((y / (cfg->line_count / 16)) & 0b1111) << 2) | |
paul@111 | 269 | ((x / (cfg->line_length / 4)) & 0b11) : |
paul@111 | 270 | 0x00; |
paul@111 | 271 | } |
paul@111 | 272 | |
paul@111 | 273 | linedata = display_wrap_pointer(cfg, linedata + cfg->line_length); |
paul@111 | 274 | } |
paul@100 | 275 | } |
paul@83 | 276 | |
paul@100 | 277 | /* Wrap the screen pointer to the current frame. */ |
paul@83 | 278 | |
paul@111 | 279 | uint8_t *display_wrap_pointer(display_config_t *cfg, uint8_t *ptr) |
paul@100 | 280 | { |
paul@100 | 281 | return wrap_pointer(ptr, cfg->frame_start, cfg->screen_limit); |
paul@83 | 282 | } |