📄 printk.c
字号:
// printk.c//// Reference from geekOS#include <stdarg.h>#include "hos.h"#define NUM_SCREEN_DWORDS ((NUMROWS * NUMCOLS * 2) / 4)#define NUM_SCROLL_DWORDS (((NUMROWS - 1) * NUMCOLS * 2) / 4)#define NUM_DWORDS_PER_LINE ((NUMCOLS * 2) / 4)static int s_row, s_col;static unsigned char s_currentAttr;#define FILL_DWORD (0x00200020 | (s_currentAttr << 24) | (s_currentAttr << 8))/* Scroll the display one line */static void scroll(void){ unsigned int *v; int i, n = NUM_SCROLL_DWORDS; unsigned int fill = FILL_DWORD; /* Move lines from 1 to NUMROW-1 up by one */ for(v = (unsigned int *)VIDEO_MEM, i = 0; i < n; ++i) { *v = *(v + NUM_DWORDS_PER_LINE); ++v; } /* clear out last line */ for(v = (unsigned int *)VIDEO_MEM + n, i = 0; i < NUM_DWORDS_PER_LINE; ++i) *v++ = fill;}/* Clear current cusor position to end of line */static void clear_to_eol(void){ int n = (NUMCOLS - s_col); unsigned char *v = VIDEO_MEM + s_row * (NUMCOLS * 2) + s_col * 2; while(n-- > 0) { *v++ = ' '; *v++ = s_currentAttr; }}/* Move to the beginning of the next line */static void newline(void){ ++s_row; s_col = 0; if(s_row == NUMROWS) { scroll(); s_row = NUMROWS - 1; }}static put_graphic_char(int c){ unsigned char *v = VIDEO_MEM + s_row * (NUMCOLS * 2) + s_col * 2; *v++ = (unsigned char) c; *v = s_currentAttr; if(s_col < NUMCOLS - 1) ++s_col; else newline();}void put_char_imp(int c){ int num_spaces; switch(c) { case '\n': clear_to_eol(); newline(); break; case '\t': num_spaces = TABWIDTH - (s_col % TABWIDTH); while(num_spaces-- > 0) put_graphic_char(' '); break; default: put_graphic_char(c); break; }}static void update_cursor(void){ unsigned character_pos = (s_row * NUMCOLS) + s_col; unsigned char orig_addr; orig_addr = inbyte(CRT_ADDR_REG); iodelay(); outbyte(CRT_ADDR_REG, CRT_CURSOR_LOC_HIGH_REG); iodelay(); outbyte(CRT_DATA_REG, (character_pos >> 8) & 0xff); iodelay(); outbyte(CRT_ADDR_REG, CRT_CURSOR_LOC_LOW_REG); iodelay(); outbyte(CRT_DATA_REG, character_pos & 0xff); iodelay(); outbyte(CRT_ADDR_REG, orig_addr);}static void format_d(char *buf, int val){ char stack[16]; int top = 0; int is_neg; unsigned uval; if(val < 0) { is_neg = 1; uval = -val; } else { is_neg = 0; uval = val; } do { int digit = uval % 10; stack[top++] = digit + '0'; uval /= 10; } while (uval > 0); if(is_neg) *buf++ = '-'; do { *buf++ = stack[--top]; } while (top > 0); *buf = '\0';}static void format_x(char *buf, int val){ int i; for(i = 28; i >= 0; i -= 4) { int x = (val >> i) & 0xf; *buf++ = "0123456789abcdef"[x]; } *buf = '\0';}/*===============================================================*//*===============================================================*/void init_screen(void){ s_row = s_col = 0; s_currentAttr = ATTRIB(BLACK, GRAY); clear_screen();}void clear_screen(void){ unsigned int *v = (unsigned int *)VIDEO_MEM; int i; unsigned int fill = FILL_DWORD; for(i = 0; i < NUM_SCREEN_DWORDS; ++i) *v++ = fill;}void get_cursor(int *row, int *col){ *row = s_row; *col = s_col;}void put_cursor(int row, int col){ s_row = row; s_col = col; update_cursor();}unsigned char get_current_attr(void){ return s_currentAttr;}void set_current_attr(unsigned char attrib){ s_currentAttr = attrib;}void put_char(int c){ put_char_imp(c); update_cursor();}void put_string(const char *s){ while(*s != '\0') { put_char_imp(*s++); } update_cursor();}void put_buf(const char *buf, unsigned long length){ while(length > 0) { put_char_imp(*buf++); --length; } update_cursor();}void printk(const char * fmt, ...){ char buf[64]; va_list args; int ival; const char *sval; va_start(args, fmt); while(*fmt != '\0') { switch (*fmt) { case '%': ++fmt; switch(*fmt) { case 'd': ival = va_arg(args, int); format_d(buf, ival); put_string(buf); break; case 'x': ival = va_arg(args, int); format_x(buf, ival); put_string(buf); break; case 's': sval = va_arg(args, const char *); put_string(sval); break; case 'c': ival = va_arg(args, int); put_char_imp(ival & 0xff); break; default: put_char_imp(*fmt); break; } default: put_char_imp(*fmt); break; } ++fmt; } update_cursor(); va_end(args);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -