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