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