diff options
Diffstat (limited to 'sys')
| -rw-r--r-- | sys/dev/cons/cons.c | 165 | ||||
| -rw-r--r-- | sys/inc/dev/cons/cons.h | 71 | ||||
| -rw-r--r-- | sys/kern/kern_init.c | 6 | ||||
| -rw-r--r-- | sys/os/os_trace.c | 16 |
4 files changed, 256 insertions, 2 deletions
diff --git a/sys/dev/cons/cons.c b/sys/dev/cons/cons.c new file mode 100644 index 0000000..706e1b0 --- /dev/null +++ b/sys/dev/cons/cons.c @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/errno.h> +#include <sys/types.h> +#include <sys/param.h> +#include <lib/string.h> +#include <dev/cons/font.h> +#include <dev/cons/cons.h> + +#define DEFAULT_FG 0x808080 +#define DEFAULT_BG 0x000000 + +/* + * Render a character to the screen + */ +static void +cons_blit_ch(struct console *cons, uint32_t x, uint32_t y, char c) +{ + struct vram_dev *vram; + size_t idx; + uint8_t *glyph; + uint32_t fg, bg; + + vram = &cons->vram; + glyph = &g_CONS_FONT[(int)c*16]; + + fg = cons->fg; + bg = cons->bg; + + for (uint32_t cy = 0; cy < FONT_HEIGHT; ++cy) { + idx = vram_index(vram, x + (FONT_WIDTH - 1), y + cy); + for (uint32_t cx = 0; cx < FONT_WIDTH; ++cx) { + vram->io[idx--] = ISSET(glyph[cy], BIT(cx)) ? fg : bg; + } + } +} + +/* + * Clear the console to its background color + */ +static void +cons_clear(struct console *cons) +{ + struct vram_dev *vram; + + vram = &cons->vram; + if (vram->io == NULL) { + return; + } + + memset(vram->io, cons->bg, vram->pitch * vram->height); +} + +/* + * Print a newline to the console + */ +static void +cons_newline(struct console *cons) +{ + struct vram_dev *vram; + + vram = &cons->vram; + cons->ty += FONT_HEIGHT; + cons->tx = 0; + if (cons->ty >= vram->height - FONT_HEIGHT) { + cons->tx = 0; + cons->ty = 0; + cons_clear(cons); + } +} + +/* + * Handle a special character + * + * Returns the character given if it is a special + * character, otherwise -1 + */ +static int +cons_special(struct console *cons, char c) +{ + switch (c) { + case '\n': + cons_newline(cons); + return c; + } + + return -1; +} + +/* + * Write a single character to the console + */ +static void +console_putch(struct console *cons, char c) +{ + struct vram_dev *vram; + + if (cons_special(cons, c) == c) { + return; + } + + vram = &cons->vram; + cons_blit_ch(cons, cons->tx, cons->ty, c); + cons->tx += FONT_WIDTH; + if (cons->tx >= vram->width - FONT_WIDTH) { + cons_newline(cons); + } +} + +int +console_write(struct console *cons, const char *s, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + console_putch(cons, *s++); + } +} + +int +console_reset(struct console *cons) +{ + int error; + + if (cons == NULL) { + return -EINVAL; + } + + /* Try to acquire the VRAM descriptor */ + if ((error = vram_getdev(&cons->vram)) < 0) { + return error; + } + + cons->fg = DEFAULT_FG; + cons->bg = DEFAULT_BG; + cons->tx = 0; + cons->ty = 0; + cons->active = 1; + return 0; +} diff --git a/sys/inc/dev/cons/cons.h b/sys/inc/dev/cons/cons.h new file mode 100644 index 0000000..8038f17 --- /dev/null +++ b/sys/inc/dev/cons/cons.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _CONS_CONS_H_ +#define _CONS_CONS_H_ 1 + +#include <sys/types.h> +#include <dev/video/vram.h> + +/* + * Represents a system console, these fields are mostly + * internal and should not be written to directly. + * + * @vram: VRAM descriptor in-use + * @fg: Foreground color in-use + * @bg: Background color in-use + * @tx: Text X position + * @ty: Text Y position + * @active: Set if active + */ +struct console { + struct vram_dev vram; + uint32_t fg; + uint32_t bg; + size_t tx; + size_t ty; + uint8_t active : 1; +}; + +/* + * Reset a console into a known state, typically used + * for initialization. + * + * Returns zero on success. + */ +int console_reset(struct console *cons); + +/* + * Write a stream of bytes to the console. + * + * Returns zero on success. + */ +int console_write(struct console *cons, const char *s, size_t len); + +#endif /* !_CONS_CONS_H_ */ diff --git a/sys/kern/kern_init.c b/sys/kern/kern_init.c index b37fb84..bba8cea 100644 --- a/sys/kern/kern_init.c +++ b/sys/kern/kern_init.c @@ -28,11 +28,15 @@ */ #include <sys/types.h> +#include <dev/cons/cons.h> +#include <os/trace.h> +struct console g_bootcons; void kmain(void); void kmain(void) { - (void)0; + console_reset(&g_bootcons); + trace("bootcons: console online\n"); } diff --git a/sys/os/os_trace.c b/sys/os/os_trace.c index f8c989d..dd14cae 100644 --- a/sys/os/os_trace.c +++ b/sys/os/os_trace.c @@ -27,15 +27,29 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include <sys/types.h> #include <os/trace.h> +#include <dev/cons/cons.h> #include <kern/serial.h> #include <lib/stdarg.h> #include <lib/string.h> +extern struct console g_bootcons; + static void trace_write(const char *s) { - serial_write(s, strlen(s)); + size_t len; + + if (s == NULL) { + return; + } + + len = strlen(s); + serial_write(s, len); + if (g_bootcons.active) { + console_write(&g_bootcons, s, len); + } } void |
