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