⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 console.c

📁 一个最简单的操作系统,boot用NASM写的,使用最简单的汇编知识,大家下载后自己看吧
💻 C
字号:
/*
	A console for developing my Kernel
		2004 by yaosihai
*/

#include "kernel.h"
void write_char(int line,int pos,char c,byte color);
static void scroll_a_line();
static void get_char(int line,int pos,char *c,byte *color);
void delete_char(int count,int line,int pos);
void write_static_string(char *str,byte color);
void write_dynamic_string(char *str,byte color);
void set_cursor_pos(int,int);
void get_cursor_pos(int*,int*);

int kernel_console_x=0;
int kernel_console_y=0;

static void scroll_a_line()
{
    char c;
    byte color;
    int line,pos;
    for(line=1;line<25;line++)
    {
        for(pos=0;pos<80;pos++)
        {
            get_char(line,pos,&c,&color);
            write_char(line-1,pos,c,color);
        }
    }

    for(pos=0;pos<80;pos++)
    {
        write_char(24,pos,' ',0xf);
    }
    
}
static void get_char(int line,int pos,char *c,byte *color)
{
    char *pChar=0xb8000;
    line=line*80*2+pos*2;
    pChar+=line;
    _asm
    {
        push eax;
        mov esi,pChar;
        mov al,byte ptr gs:[esi];
        mov ah,byte ptr gs:[esi+1];
        mov esi,c;
        mov byte ptr [esi],al;
        mov esi,color;
        mov byte ptr [esi] ,ah;
        pop eax;
    }
}

void write_char(int line,int pos,char c,byte color)
{
    char *pChar=0xb8000;

    line=line*80*2+pos*2;
    pChar+=line;
    _asm
    {
        mov edi,pChar;
        push eax;
        mov ah,c;
        mov al,color;
        mov byte ptr gs:[edi],ah;
        mov byte ptr gs:[edi+1],al;
        pop eax;
    }
    
}
    

void write_console(char *str,byte color)
{
    int line;
    int pos;

    get_cursor_pos(&line,&pos);
    while(*str!='\0')
    {
        if(*str=='\n')
        {
                pos=0;
                line++;
                if(line>=25)
                {
                        line=24;
                        scroll_a_line();
                }
                set_cursor_pos(line,0);
                str++;
                continue;
        }
        write_char(line,pos,*str,color);
        pos++;
        if(pos>=80)
        {
                pos=0;
                line++;
                if(line>=25)
                {
                        line=24;
                        scroll_a_line();
                }
                set_cursor_pos(line,0);
        }
        else
                set_cursor_pos(line,pos);
        str++;                      
                        

    }
}

        
	

void delete_char(int count,int line,int pos)
{
    while(count>0)
    {
        count--;
        write_char(line,pos,' ',0xf);
        pos++;
        if(pos>=80)
        {
            line++;
            if(line>=25)
                break;
            pos=0;
        }
    }
}
void set_cursor_pos(int line,int pos)
{
	dword xy=line*80+pos;
	uint _curds_;
	int *x_y;


    outb(0x0f,0x3d4);
    outb((byte)xy&0xff,0x3d5);
    outb(0x0e,0x3d4);
    outb((byte)(xy>>8)&0xff,0x3d5);

	x_y=adjust_address(&kernel_console_x);
	*x_y =pos;
	x_y=adjust_address(&kernel_console_y);	
	*x_y =line;
}
void get_cursor_pos(int *line,int *pos)
{
	int l,p;
	int *xy;

	xy=adjust_address(&kernel_console_x);
	p=*xy;
	*pos=p;

	xy=adjust_address(&kernel_console_y);	
	l=*xy;
	*line=l;	
}



void h2c(char *p,unsigned int hex)
{
	//p's size = 8
	int i;
	unsigned int mask,old=hex;
	
	for(i=7;i>=0;i--)
	{
		hex&=0xf;
		if(hex<=9)
			hex+='0';
		else
                        hex='a'+(hex-0xa);
		*(p+i)=(char)hex;
		old>>=4;
		hex=old;
	}
        *(p+8)='h';
        *(p+9)='\0';
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -