1 1.7 khorben /* $NetBSD: console.c,v 1.7 2021/05/04 21:09:16 khorben Exp $ */ 2 1.1 maxv 3 1.1 maxv /* 4 1.6 maxv * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved. 5 1.1 maxv * 6 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation 7 1.1 maxv * by Maxime Villard. 8 1.1 maxv * 9 1.1 maxv * Redistribution and use in source and binary forms, with or without 10 1.1 maxv * modification, are permitted provided that the following conditions 11 1.1 maxv * are met: 12 1.1 maxv * 1. Redistributions of source code must retain the above copyright 13 1.1 maxv * notice, this list of conditions and the following disclaimer. 14 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 maxv * notice, this list of conditions and the following disclaimer in the 16 1.1 maxv * documentation and/or other materials provided with the distribution. 17 1.1 maxv * 18 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 maxv * POSSIBILITY OF SUCH DAMAGE. 29 1.1 maxv */ 30 1.1 maxv 31 1.1 maxv #include "prekern.h" 32 1.1 maxv 33 1.1 maxv extern vaddr_t atdevbase; 34 1.1 maxv #define CONS_WID 80 35 1.1 maxv #define CONS_HEI 25 36 1.1 maxv 37 1.1 maxv static char *cons_start; 38 1.1 maxv static size_t cons_x, cons_y; 39 1.1 maxv static char cons_buffer[CONS_WID * 2 * CONS_HEI]; 40 1.1 maxv 41 1.2 maxv void init_cons(void) 42 1.1 maxv { 43 1.1 maxv cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN); 44 1.1 maxv cons_x = 0; 45 1.1 maxv cons_y = 0; 46 1.1 maxv } 47 1.1 maxv 48 1.2 maxv static void check_scroll(void) 49 1.1 maxv { 50 1.1 maxv char *src, *dst; 51 1.1 maxv size_t i; 52 1.1 maxv 53 1.1 maxv if (cons_y != CONS_HEI) 54 1.1 maxv return; 55 1.1 maxv 56 1.1 maxv for (i = 0; i < CONS_HEI-1; i++) { 57 1.1 maxv dst = &cons_buffer[0] + i * (CONS_WID * 2); 58 1.1 maxv src = &cons_buffer[0] + (i + 1) * (CONS_WID * 2); 59 1.1 maxv memcpy(dst, src, (CONS_WID * 2)); 60 1.1 maxv } 61 1.1 maxv memset(&cons_buffer[0] + (CONS_WID * 2) * (CONS_HEI-1), 0, 62 1.1 maxv (CONS_WID * 2)); 63 1.1 maxv cons_y--; 64 1.4 maxv memcpy(cons_start, &cons_buffer[0], CONS_WID * 2 * CONS_HEI); 65 1.1 maxv } 66 1.1 maxv 67 1.5 maxv static void putc(int color, char c) 68 1.1 maxv { 69 1.1 maxv char *ptr, *scr; 70 1.1 maxv 71 1.5 maxv if (c == '\n') { 72 1.5 maxv cons_x = 0; 73 1.5 maxv cons_y++; 74 1.5 maxv check_scroll(); 75 1.5 maxv } else { 76 1.5 maxv if (cons_x + 1 == CONS_WID) { 77 1.1 maxv cons_x = 0; 78 1.1 maxv cons_y++; 79 1.1 maxv check_scroll(); 80 1.1 maxv } 81 1.5 maxv ptr = (cons_start + 2 * cons_x + 160 * cons_y); 82 1.5 maxv scr = (cons_buffer + 2 * cons_x + 160 * cons_y); 83 1.5 maxv ptr[0] = scr[0] = c; 84 1.5 maxv ptr[1] = scr[1] = color; 85 1.5 maxv cons_x++; 86 1.5 maxv } 87 1.5 maxv } 88 1.5 maxv 89 1.5 maxv void print_ext(int color, char *buf) 90 1.5 maxv { 91 1.5 maxv size_t i; 92 1.5 maxv 93 1.5 maxv for (i = 0; buf[i] != '\0'; i++) { 94 1.5 maxv putc(color, buf[i]); 95 1.1 maxv } 96 1.1 maxv } 97 1.1 maxv 98 1.1 maxv void print(char *buf) 99 1.1 maxv { 100 1.1 maxv print_ext(WHITE_ON_BLACK, buf); 101 1.1 maxv } 102 1.1 maxv 103 1.7 khorben void print_state(state_t state, char *buf) 104 1.1 maxv { 105 1.1 maxv print("["); 106 1.7 khorben switch (state) 107 1.7 khorben { 108 1.7 khorben case STATE_NORMAL: 109 1.7 khorben print_ext(GREEN_ON_BLACK, "+"); 110 1.7 khorben break; 111 1.7 khorben case STATE_ERROR: 112 1.7 khorben print_ext(RED_ON_BLACK, "!"); 113 1.7 khorben break; 114 1.7 khorben case STATE_WARNING: 115 1.7 khorben print_ext(YELLOW_ON_BLACK, "*"); 116 1.7 khorben break; 117 1.7 khorben default: 118 1.7 khorben print_ext(WHITE_ON_BLACK, "?"); 119 1.7 khorben break; 120 1.7 khorben } 121 1.1 maxv print("] "); 122 1.1 maxv print(buf); 123 1.1 maxv print("\n"); 124 1.1 maxv } 125 1.1 maxv 126 1.2 maxv void print_banner(void) 127 1.1 maxv { 128 1.3 maxv char *banner = 129 1.1 maxv " __________ __ \n" 130 1.1 maxv " \\______ \\_______ ____ | | __ ___________ ____ \n" 131 1.1 maxv " | ___/\\_ __ \\_/ __ \\| |/ // __ \\_ __ \\/ \\ \n" 132 1.1 maxv " | | | | \\/\\ ___/| <\\ ___/| | \\/ | \\\n" 133 1.1 maxv " |____| |__| \\___ >__|_ \\\\___ >__| |___| /\n" 134 1.1 maxv " \\/ \\/ \\/ \\/ Version 1.0\n" 135 1.1 maxv ; 136 1.1 maxv print(banner); 137 1.1 maxv } 138