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