FreeFlyOS【五】:VGA部分详解

vga.c

#include "vga.h"
#include "../asm/asm.h"
#include "../serial/serial.h"
#include "../keyboard/keyboard.h"
#define NULL ((void *)0)
//the standard window is 80*25, max of cursor_x is 25, max of cursor_y is 80
static unsigned char cursor_x=0;
static unsigned char cursor_y=0;
//the start address of CGA is 0xB8000,此时开启了分页,应加上一个线性映射地址
//避免后面新建页表时,未映射前面一段物理内存而导致BUG
static unsigned short *vga_memory=(unsigned short *)(0xB8000+0xC0000000);
/* print cursor */ 
inline void print_cursor(unsigned char x,unsigned char y){
    unsigned short pos;
    //calculate the pos from (0,0)
    pos=x*80+y;

    //write register data to port 0x3d4,set register_cursor_x
    outb(VGA_register,register_cursor_x);
    //set cursor_x
    outb(VGA_data,(pos>>8)&0xFF);
    //write register data to port 0x3d4,set register_cursor_y
    outb(VGA_register,register_cursor_y);
    //set cursor_y
    outb(VGA_data,pos&0xFF);
}
/* use 'space' to clear the screen */
inline void clear(){
    unsigned short attribute=((0<<4)&0xf0|(15&0xf))<<8;
    unsigned short space=' '|attribute;
    for(unsigned short i=0;i<25*80;i++){
        vga_memory[i]=space;
    }

    cursor_x=0;
    cursor_y=0;

    print_cursor(cursor_x,cursor_y);
}
/* print char */ 
inline void print_char(char c,color_type background,color_type foreground){
    unsigned short attribute=0,character=0,pos;
   
    if(cursor_x>=25){
        clear();
    }
        
    //calculate the pos from (0,0)
    pos=cursor_x*80+cursor_y;

    switch(c)
    {
        case '\n':
            cursor_y=0;
            cursor_x++;
            break;
        case '\t':
            cursor_y=(cursor_y+8)&~(8-1);
            break;
        default:
            //set the first byte of character
            character=c;
            //set the last byte of character
            attribute=((background<<4)&0xf0|(foreground&0xf))<<8;
            character=character|attribute;
            //send character to vga memory
            vga_memory[pos]=character;

            cursor_y++;
            if(cursor_y>=80)
            {
                cursor_y=0;
                cursor_x++;
            }
            break;
    }
    print_cursor(cursor_x,cursor_y);
}
//print string
inline void print_string(char *str,color_type background,color_type foreground
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值