📄 printk.c
字号:
/* * OSV * Copyright (C) 2002 Ciprian DOSOFTEI <rocksoul@mail.com> * All rights reserved. * * http://backster.free.fr/osv * * This file is part of the OSV project. OSV is free software, also known as * "open source"; you can redistribute it and/or modify it under the terms * of the GNU General Public License (GPL), version 2, as published by the Free * Software Foundation (FSF). To explore alternate licensing terms, contact * the author at rocksoul@mail.com or +40740649907. * * OSV is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have * received a copy of the GPL along with OSV; see the file COPYING. If * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA. */#include <asm/io.h>#include <printk.h>#include <types.h>static dword PRINTK_X = 0, PRINTK_Y = PRINTK_HEIGHT - 1;static int PRINTK_printInt(const int i);static int PRINTK_printUnsignedInt(const unsigned int i);static int PRINTK_printHexa(const unsigned int i, const char Xorx);static int PRINTK_printOctal(const unsigned int i);static void PRINTK_setVideoCursor(const dword row, const dword column) { unsigned short position = (row * PRINTK_WIDTH) + column; outb (PRINTK_CRT_INDEX_REG, 0x0F); outb (PRINTK_CRT_DATA_REG, (unsigned char)(position & 0xFF)); outb (PRINTK_CRT_INDEX_REG, 0x0E); outb (PRINTK_CRT_DATA_REG, (unsigned char)((position >> 8) & 0xFF));}void PRINTK_newLine() { int i,j; PRINTK_X = 0; PRINTK_Y++; if (PRINTK_Y >= PRINTK_HEIGHT) { for (j = 0; j < PRINTK_HEIGHT - 1; j++) for (i = 0; i < PRINTK_WIDTH * 2; i++) *((byte *)(PRINTK_VIDEO + j * PRINTK_WIDTH * 2) + i) = \ *((byte *)(PRINTK_VIDEO + (j+1) * PRINTK_WIDTH * 2) + i); for (i = 0; i < PRINTK_WIDTH; i++) *((word *)(PRINTK_VIDEO + PRINTK_WIDTH * (PRINTK_HEIGHT - 1) * 2) + i) = \ PRINTK_SPACE | (7 << 8); PRINTK_Y = PRINTK_HEIGHT - 1; } PRINTK_setVideoCursor(PRINTK_Y, PRINTK_X);}void PRINTK_printChar(const byte ch) { *(word *)(PRINTK_VIDEO + PRINTK_Y * 160 + PRINTK_X * 2) = ch | (7 << 8); PRINTK_X++; if (PRINTK_X >= PRINTK_WIDTH) PRINTK_newLine(); PRINTK_setVideoCursor(PRINTK_Y, PRINTK_X);}int PRINTK_putchar (int c) { byte i; switch ((unsigned char) c) { case '\n' : PRINTK_newLine(); break; case '\r' : PRINTK_X = 0; /* just return to begin of current line */ PRINTK_setVideoCursor(PRINTK_Y, PRINTK_X); break; case '\f' : for (i = 0; i < PRINTK_WIDTH * PRINTK_HEIGHT; i++) *((word *)PRINTK_VIDEO + i) = PRINTK_SPACE | (64 << 8); PRINTK_X = 0; PRINTK_Y = 0; PRINTK_setVideoCursor (PRINTK_Y, PRINTK_X); break; case '\t' : for ( i = 0 ; i < PRINTK_TAB_SIZE ; i++ ) PRINTK_printChar(32); break; case '\b': if (PRINTK_X > 0) { PRINTK_X--; *(byte *)(PRINTK_VIDEO + PRINTK_Y * 160 + PRINTK_X * 2) = PRINTK_SPACE; } else { if (PRINTK_Y > 0) { PRINTK_Y--; PRINTK_X = 79; *(byte *)(PRINTK_VIDEO + PRINTK_Y * 160 + PRINTK_X * 2) = PRINTK_SPACE; } } PRINTK_setVideoCursor(PRINTK_Y, PRINTK_X); break; default : PRINTK_printChar((unsigned char) c); } return c;}int printk (const char *cntrl_string, ...) { int pos = 0, cnt_printed_chars = 0, i; unsigned char* chptr; PRINTK_va_list ap; PRINTK_va_start(ap, cntrl_string); while (cntrl_string[pos]) { if (cntrl_string[pos] == '%') { pos++; switch (cntrl_string[pos]) { case 'c': PRINTK_putchar(PRINTK_va_arg(ap, unsigned char)); cnt_printed_chars++; break; case 's': chptr = PRINTK_va_arg(ap, unsigned char*); i = 0; while (chptr [i]) { cnt_printed_chars++; PRINTK_putchar(chptr [i++]); } break; case 'i': case 'd': cnt_printed_chars += PRINTK_printInt (PRINTK_va_arg (ap, int)); break; case 'u': cnt_printed_chars += PRINTK_printUnsignedInt (PRINTK_va_arg (ap, unsigned int)); break; case 'x': cnt_printed_chars += PRINTK_printHexa (PRINTK_va_arg (ap, unsigned int), 'x'); break; case 'X': cnt_printed_chars += PRINTK_printHexa (PRINTK_va_arg (ap, unsigned int), 'X'); break; case 'o': cnt_printed_chars += PRINTK_printOctal (PRINTK_va_arg (ap, unsigned int)); break; case 'p': PRINTK_putchar ('0'); PRINTK_putchar ('x'); cnt_printed_chars += 2; /* of '0x' */ cnt_printed_chars += PRINTK_printHexa (PRINTK_va_arg (ap, unsigned int), 'x'); break; case '#': pos++; switch (cntrl_string[pos]) { case 'x': PRINTK_putchar ('0'); PRINTK_putchar ('x'); cnt_printed_chars += 2; /* of '0x' */ cnt_printed_chars += PRINTK_printHexa (PRINTK_va_arg (ap, unsigned int), 'x'); break; case 'X': PRINTK_putchar ('0'); PRINTK_putchar ('X'); cnt_printed_chars += 2; /* of '0X' */ cnt_printed_chars += PRINTK_printHexa (PRINTK_va_arg (ap, unsigned int), 'X'); break; case 'o': PRINTK_putchar ('0'); cnt_printed_chars++; cnt_printed_chars += PRINTK_printOctal (PRINTK_va_arg (ap, unsigned int)); break; } break; default: PRINTK_putchar ((unsigned char) cntrl_string[pos]); cnt_printed_chars++; } pos++; } else { PRINTK_putchar ((unsigned char) cntrl_string[pos]); cnt_printed_chars++; pos++; } } PRINTK_va_end (ap); return cnt_printed_chars;}static int PRINTK_printInt(const int i) { int cnt = 0; if (i < 0) { PRINTK_putchar ('-'); cnt++; cnt += PRINTK_printUnsignedInt (-i); } else { cnt += PRINTK_printUnsignedInt (i); } return cnt; /* number of chars printed into the screen */}static int PRINTK_printUnsignedInt (const unsigned int i) { unsigned int n, d = 1000000000; int cnt = 0; /* count printed chars */ while ((i / d == 0) && (d >= 10)) d /= 10; n = i; while (d >= 10) { cnt++; PRINTK_putchar ((unsigned char)((int) '0' + n / d)); n = n % d; d /= 10; } cnt++; PRINTK_putchar ((unsigned char)((int) '0' + n)); return cnt;}static int PRINTK_printHexa (const unsigned int i, const char Xorx) { const unsigned char hexa [16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; const unsigned char Hexa [16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; const unsigned char *phexa; unsigned int n, d = 0x10000000; int cnt = 0; /* count printed chars */ switch (Xorx) { case 'x': phexa = hexa; break; case 'X': phexa = Hexa; break; default: return 0; /* has to be 'x' or 'X' */ } while ((i / d == 0) && (d >= 0x10)) d /= 0x10; n = i; while (d >= 0xF) { cnt++; PRINTK_putchar (*(phexa + n / d)); n = n % d; d /= 0x10; } cnt++; PRINTK_putchar (*(phexa + n)); return cnt;}static int PRINTK_printOctal (const unsigned int i) { unsigned int n, d = 010000000000; int cnt = 0; /* count printed chars */ while ((i / d == 0) && (d >= 010)) d /= 010; n = i; while (d >= 010) { cnt++; PRINTK_putchar ((unsigned char)((int) '0' + n / d)); n = n % d; d /= 010; } cnt++; PRINTK_putchar ((unsigned char)((int) '0' + n)); return cnt;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -