aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / fbcon.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* fbcon.c -- text console on a linear 32-bit framebuffer; see fbcon.h. */ |
| 18 | #include "fbcon.h" |
| 19 | #include "font8x8.h" |
| 20 | |
| 21 | uint32_t ck_fbcon_scale_for(uint32_t width, uint32_t height) |
| 22 | { |
| 23 | uint32_t s = height / 540; |
| 24 | if (s < 1) |
| 25 | s = 1; |
| 26 | if (s > 4) |
| 27 | s = 4; |
| 28 | while (s > 1 && width / (CK_FBCON_CELL_W * s) < 80) |
| 29 | s--; |
| 30 | return s; |
| 31 | } |
| 32 | |
| 33 | int ck_fbcon_attach(struct ck_fbcon *c, volatile uint32_t *px, uint32_t width, uint32_t height, |
| 34 | uint32_t pitch, uint32_t format) |
| 35 | { |
| 36 | if (!px || format > 1 || pitch < width) |
| 37 | return -1; |
| 38 | uint32_t s = ck_fbcon_scale_for(width, height); |
| 39 | uint32_t cols = width / (CK_FBCON_CELL_W * s), rows = height / (CK_FBCON_CELL_H * s); |
| 40 | if (!cols || !rows) |
| 41 | return -1; |
| 42 | if (cols > CK_FBCON_MAX_COLS) |
| 43 | cols = CK_FBCON_MAX_COLS; |
| 44 | if (rows > CK_FBCON_MAX_ROWS) |
| 45 | rows = CK_FBCON_MAX_ROWS; |
| 46 | c->px = px; |
| 47 | c->width = width; |
| 48 | c->height = height; |
| 49 | c->pitch = pitch; |
| 50 | c->format = format; |
| 51 | c->scale = s; |
| 52 | c->cols = cols; |
| 53 | c->rows = rows; |
| 54 | c->col = 0; |
| 55 | c->row = 0; |
| 56 | c->pending_nl = 0; |
| 57 | c->scrolls = 0; |
| 58 | for (uint32_t r = 0; r < CK_FBCON_MAX_ROWS; r++) |
| 59 | for (uint32_t k = 0; k < CK_FBCON_MAX_COLS; k++) |
| 60 | c->text[r][k] = 0; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int ck_fbcon_setup(struct ck_fbcon *c, volatile uint32_t *px, uint32_t width, uint32_t height, |
| 65 | uint32_t pitch, uint32_t format) |
| 66 | { |
| 67 | if (ck_fbcon_attach(c, px, width, height, pitch, format) != 0) |
| 68 | return -1; |
| 69 | ck_fbcon_clear(c); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | void ck_fbcon_clear(struct ck_fbcon *c) |
| 74 | { |
| 75 | for (uint32_t y = 0; y < c->height; y++) { |
| 76 | volatile uint32_t *p = c->px + (uint64_t)y * c->pitch; |
| 77 | for (uint32_t x = 0; x < c->width; x++) |
| 78 | p[x] = CK_FBCON_BG; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* One 8x8 glyph (times scale) into cell (col, row); the two gap rows of the |
| 83 | * cell are left as the clear made them. */ |
| 84 | static void glyph(struct ck_fbcon *c, uint32_t col, uint32_t row, unsigned char ch) |
| 85 | { |
| 86 | const uint8_t *g = ck_font8x8[ch - CK_FONT_FIRST]; |
| 87 | uint32_t s = c->scale; |
| 88 | uint64_t x0 = (uint64_t)col * CK_FBCON_CELL_W * s; |
| 89 | uint64_t y0 = (uint64_t)row * CK_FBCON_CELL_H * s; |
| 90 | for (uint32_t gy = 0; gy < CK_FONT_H; gy++) { |
| 91 | for (uint32_t sy = 0; sy < s; sy++) { |
| 92 | volatile uint32_t *p = c->px + (y0 + (uint64_t)gy * s + sy) * c->pitch + x0; |
| 93 | for (uint32_t gx = 0; gx < CK_FONT_W; gx++) { |
| 94 | uint32_t v = (g[gy] >> gx) & 1 ? CK_FBCON_FG : CK_FBCON_BG; |
| 95 | for (uint32_t sx = 0; sx < s; sx++) |
| 96 | *p++ = v; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static void draw(struct ck_fbcon *c, unsigned char ch) |
| 103 | { |
| 104 | if (ch < CK_FONT_FIRST || ch > CK_FONT_LAST) |
| 105 | ch = '?'; |
| 106 | c->text[c->row][c->col] = (char)ch; |
| 107 | glyph(c, c->col, c->row, ch); |
| 108 | } |
| 109 | |
| 110 | /* Next row. Bottom row full: clear, move the newest half of the text rows |
| 111 | * to the top and redraw them; writing goes on below them. */ |
| 112 | static void advance(struct ck_fbcon *c) |
| 113 | { |
| 114 | c->col = 0; |
| 115 | if (++c->row < c->rows) |
| 116 | return; |
| 117 | uint32_t keep = c->rows / 2; |
| 118 | ck_fbcon_clear(c); |
| 119 | for (uint32_t r = 0; r < c->rows; r++) |
| 120 | for (uint32_t k = 0; k < c->cols; k++) |
| 121 | c->text[r][k] = r < keep ? c->text[c->rows - keep + r][k] : 0; |
| 122 | for (uint32_t r = 0; r < keep; r++) |
| 123 | for (uint32_t k = 0; k < c->cols; k++) |
| 124 | if (c->text[r][k]) |
| 125 | glyph(c, k, r, (unsigned char)c->text[r][k]); |
| 126 | c->row = keep; |
| 127 | c->scrolls++; |
| 128 | } |
| 129 | |
| 130 | void ck_fbcon_write(struct ck_fbcon *c, const char *s, size_t n) |
| 131 | { |
| 132 | for (size_t i = 0; i < n; i++) { |
| 133 | unsigned char ch = (unsigned char)s[i]; |
| 134 | if (ch == '\n') { |
| 135 | if (c->pending_nl) |
| 136 | advance(c); |
| 137 | c->pending_nl = 1; |
| 138 | continue; |
| 139 | } |
| 140 | if (ch == '\r') { |
| 141 | if (!c->pending_nl) |
| 142 | c->col = 0; |
| 143 | continue; |
| 144 | } |
| 145 | if (ch == '\b') { /* cursor one cell left, within the line; the caller overwrites */ |
| 146 | if (!c->pending_nl && c->col) |
| 147 | c->col--; |
| 148 | continue; |
| 149 | } |
| 150 | if (c->pending_nl) { |
| 151 | advance(c); |
| 152 | c->pending_nl = 0; |
| 153 | } |
| 154 | if (ch == '\t') { |
| 155 | uint32_t next = (c->col / 8 + 1) * 8; |
| 156 | while (c->col < next && c->col < c->cols) { |
| 157 | draw(c, ' '); |
| 158 | c->col++; |
| 159 | } |
| 160 | continue; |
| 161 | } |
| 162 | if (c->col >= c->cols) |
| 163 | advance(c); |
| 164 | draw(c, ch); |
| 165 | c->col++; |
| 166 | } |
| 167 | } |