video.c

来自「国内的一个小型操作系统」· C语言 代码 · 共 118 行

C
118
字号
#include"color.h"

void update_cursor(int row, int col) //set cursor to x,y
{
    unsigned short position = (row*80) + col;  //16bit
    // cursor LOW port to vga INDEX register
    outportb(0x3D4, 0x0F);
    outportb(0x3D5, (unsigned char)(position&0xFF));
    // cursor HIGH port to vga INDEX register
    outportb(0x3D4, 0x0E);
    outportb(0x3D5, (unsigned char)((position>>8)&0xFF));
}

unsigned short get_cursor() //get cursor offset
{
    unsigned short offset;
    unsigned short high;
    unsigned short low;
    // get cursor LOW port
    outportb(0x3D4, 0x0f);
    low = (unsigned short)inportb(0x3D5);
    // get cursor HIGH port
    outportb(0x3D4, 0x0E);
    high = (unsigned short)inportb(0x3D5);
    offset = ((high<<8)|low) << 1;
    return offset;
}

void move_cursor(int i)
{
    unsigned short offset = get_cursor();
    unsigned short position = (offset>>1) + i;    
    outportb(0x3D4, 0x0F);
    outportb(0x3D5, (unsigned char)(position&0xFF));
    // cursor HIGH port to vga INDEX register
    outportb(0x3D4, 0x0E);
    outportb(0x3D5, (unsigned char)((position>>8)&0xFF));
}

void clr() //clear the entire text screen
{
    unsigned char *vidmem = (unsigned char *)0xb8000;
    unsigned int i = 0;
    while(i < (80*2*25))
    {
        vidmem[i] = ' ';
        i++;
        vidmem[i] = WHITE_TXT;
        i++;
    }
}

unsigned int hprintf(char *message, unsigned int line) //should input line
{
    unsigned char *vidmem = (unsigned char *)0xb8000;
    unsigned int i = 0;

    i=(line*80*2); //i是将要输入显存的位置

    while(*message != 0) // 24h
    {
        if(*message == 0x2F)    //   "/"
        {
            *message++;
            if(*message == 0x6e)//   "n"
            {
                line++;         //获得/n则换行
                i = (line*80*2);
                *message++;
                if(*message == 0)//获得/0则结束
                {
                    return 1;
                }
            }
        }
        vidmem[i] = *message;
        *message++;
        i++;
        vidmem[i] = WHITE_TXT;
        i++;
    }
    return 1;
}

/* ===================================================================  */
unsigned int panic(char *message) // just a modified version of hprintf
{
    char *vidmem = (char *)0xb8000;
    unsigned int i = 0;
    unsigned int line = 24;

    i = (line*80*2);

    while(*message != 0) // 24h
    {
        if(*message == 0x2F)
        {
            *message++;
            if(*message == 0x6e)
            {
                line++;
                i = (line*80*2);
                *message++;
                if(*message == 0)
                {
                    return 1;
                }
            }
        }
        vidmem[i] = *message;
        *message++;
        i++;
        vidmem[i] = 0x9; // use blue text
        i++;
    }
    return 1;
}

⌨️ 快捷键说明

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