kernel源码(十八)字符设备-console.c
这个文件的核心是con_write(struct tty_struct * tty)函数,用于将缓冲队列write_q中的字符显示在控制台,所有其他函数都是围绕con_write函数。另外还有一个函数con_init()函数用于初始化控制台,这个函数在main.c中被调用(tty_init调用了con_init)。
源码
/* * linux/kernel/console.c * * (C) 1991 Linus Torvalds */ /* * console.c * * This module implements the console io functions * 'void con_init(void)' * 'void con_write(struct tty_queue * queue)' * Hopefully this will be a rather complete VT102 implementation. * * Beeping thanks to John T Kohl. */ /* * NOTE!!! We sometimes disable and enable interrupts for a short while * (to put a word in video IO), but this will work even for keyboard * interrupts. We know interrupts aren't enabled when getting a keyboard * interrupt, as we use trap-gates. Hopefully all is well. */ /* * Code to check for different video-cards mostly by Galen Hunt, * <g-hunt@ee.utah.edu> */ #include <linux/sched.h> #include <linux/tty.h> #include <asm/io.h> #include <asm/system.h> /* * These are set up by the setup-routine at boot-time: */ #define ORIG_X (*(unsigned char *)0x90000) #define ORIG_Y (*(unsigned char *)0x90001) #define ORIG_VIDEO_PAGE (*(unsigned short *)0x90004) #define ORIG_VIDEO_MODE ((*(unsigned short *)0x90006) & 0xff) #define ORIG_VIDEO_COLS (((*(unsigned short *)0x90006) & 0xff00) >> 8) #define ORIG_VIDEO_LINES (25) #define ORIG_VIDEO_EGA_AX (*(unsigned short *)0x90008) #define ORIG_VIDEO_EGA_BX (*(unsigned short *)0x9000a) #define ORIG_VIDEO_EGA_CX (*(unsigned short *)0x9000c) #define VIDEO_TYPE_MDA 0x10 /* Monochrome Text Display */ #define VIDEO_TYPE_CGA 0x11 /* CGA Display */ #define VIDEO_TYPE_EGAM 0x20 /* EGA/VGA in Monochrome Mode */ #define VIDEO_TYPE_EGAC 0x21 /* EGA/VGA in Color Mode */ #define NPAR 16 extern void keyboard_interrupt(void); static unsigned char video_type; /* Type of display being used */ static unsigned long video_num_columns; /* Number of text columns */ static unsigned long video_size_row; /* Bytes per row */ static unsigned long video_num_lines; /* Number of test lines */ static unsigned char video_page; /* Initial video page */ static unsigned long video_mem_start; /* Start of video RAM */ static unsigned long video_mem_end; /* End of video RAM (sort of) */ static unsigned short video_port_reg; /* Video register select port */ static unsigned short video_port_val; /* Video register value port */ static unsigned short video_erase_char; /* Char+Attrib to erase with */ static unsigned long origin; /* Used for EGA/VGA fast scroll */ static unsigned long scr_end; /* Used for EGA/VGA fast scroll */ static unsigned long pos; static unsigned long x,y; static unsigned long top,bottom; static unsigned long state=0; static unsigned long npar,par[NPAR]; static unsigned long ques=0; static unsigned char attr=0x07; static void sysbeep(void); /* * this is what the terminal answers to a ESC-Z or csi0c * query (= vt100 response). */ #define RESPONSE "\033[?1;2c" /* NOTE! gotoxy thinks x==video_num_columns is ok */ static inline void gotoxy(unsigned int new_x,unsigned int new_y) { if (new_x > video_num_columns || new_y >= video_num_lines) return; x=new_x; y=new_y; pos=origin + y*video_size_row + (x<<1); } static inline void set_origin(void) { cli(); outb_p(12, video_port_reg); outb_p(0xff&((origin-video_mem_start)>>9), video_port_val); outb_p(13, video_port_reg); outb_p(0xff&((origin-video_mem_start)>>1), video_port_val); sti(); } static void scrup(void) { if (video_type == VIDEO_TYPE_EGAC || video_type == VIDEO_TYPE_EGAM) { if (!top && bottom == video_num_lines) { origin += video_size_row; pos += video_size_row; scr_end += video_size_row; if (scr_end > video_mem_end) { __asm__("cld\n\t" "rep\n\t" "movsl\n\t" "movl _video_num_columns,%1\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((video_num_lines-1)*video_num_columns>>1), "D" (video_mem_start), "S" (origin) :"cx","di","si"); scr_end -= origin-video_mem_start; pos -= origin-video_mem_start; origin = video_mem_start; } else { __asm__("cld\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" (video_num_columns), "D" (scr_end-video_size_row) :"cx","di"); } set_origin(); } else { __asm__("cld\n\t" "rep\n\t" "movsl\n\t" "movl _video_num_columns,%%ecx\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((bottom-top-1)*video_num_columns>>1), "D" (origin+video_size_row*top), "S" (origin+video_size_row*(top+1)) :"cx","di","si"); } } else /* Not EGA/VGA */ { __asm__("cld\n\t" "rep\n\t" "movsl\n\t" "movl _video_num_columns,%%ecx\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((bottom-top-1)*video_num_columns>>1), "D" (origin+video_size_row*top), "S" (origin+video_size_row*(top+1)) :"cx","di","si"); } } static void scrdown(void) { if (video_type == VIDEO_TYPE_EGAC || video_type == VIDEO_TYPE_EGAM) { __asm__("std\n\t" "rep\n\t" "movsl\n\t" "addl $2,%%edi\n\t" /* %edi has been decremented by 4 */ "movl _video_num_columns,%%ecx\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((bottom-top-1)*video_num_columns>>1), "D" (origin+video_size_row*bottom-4), "S" (origin+video_size_row*(bottom-1)-4) :"ax","cx","di","si"); } else /* Not EGA/VGA */ { __asm__("std\n\t" "rep\n\t" "movsl\n\t" "addl $2,%%edi\n\t" /* %edi has been decremented by 4 */ "movl _video_num_columns,%%ecx\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((bottom-top-1)*video_num_columns>>1), "D" (origin+video_size_row*bottom-4), "S" (origin+video_size_row*(bottom-1)-4) :"ax","cx","di","si"); } } static void lf(void) { if (y+1<bottom) { y++; pos += video_size_row; return; } scrup(); } static void ri(void) { if (y>top) { y--; pos -= video_size_row; return; } scrdown(); } static void cr(void) { pos -= x<<1; x=0; } static void del(void) { if (x) { pos -= 2; x--; *(unsigned short *)pos = video_erase_char; } } static void csi_J(int par) { long count __asm__("cx"); long start __asm__("di"); switch (par) { case 0: /* erase from cursor to end of display */ count = (scr_end-pos)>>1; start = pos; break; case 1: /* erase from start to cursor */ count = (pos-origin)>>1; start = origin; break; case 2: /* erase whole display */ count = video_num_columns * video_num_lines; start = origin; break; default: return; } __asm__("cld\n\t" "rep\n\t" "stosw\n\t" ::"c" (count), "D" (start),"a" (video_erase_char) :"cx","di"); } static void csi_K(int par) { long count __asm__("cx"); long start __asm__("di"); switch (par) { case 0: /* erase from cursor to end of line */ if (x>=video_num_columns) return; count = video_num_columns-x; start = pos; break; case 1: /* erase from start of line to cursor */ start = pos - (x<<1); count = (x<video_num_columns)?x:video_num_columns; break; case 2: /* erase whole line */ start = pos - (x<<1); count = video_num_columns; break; default: return; } __asm__("cld\n\t" "rep\n\t" "stosw\n\t" ::"c" (count), "D" (start),"a" (video_erase_char) :"cx","di"); } void csi_m(void) { int i; for (i=0;i<=npar;i++) switch (par[i]) { case 0:attr=0x07;break; case 1:attr=0x0f;break; case 4:attr=0x0f;break; case 7:attr=0x70;break; case 27:attr=0x07;break; } } static inline void set_cursor(void) { cli(); outb_p(14, video_port_reg); outb_p(0xff&((pos-video_mem_start)>>9), video_port_val); outb_p(15, video_port_reg); outb_p(0xff&((pos-video_mem_start)>>1), video_port_val); sti(); } static void respond(struct tty_struct * tty) { char * p = RESPONSE; cli(); while (*p) { PUTCH(*p,tty->read_q); p++; } sti(); copy_to_cooked(tty); } static void insert_char(void) { int i=x; unsigned short tmp, old = video_erase_char; unsigned short * p = (unsigned short *) pos; while (i++<video_num_columns) { tmp=*p; *p=old; old=tmp; p++; } } static void insert_line(void) { int oldtop,oldbottom; oldtop=top; oldbottom=bottom; top=y; bottom = video_num_lines; scrdown(); top=oldtop; bottom=oldbottom; } static void delete_char(void) { int i; unsigned short * p = (unsigned short *) pos; if (x>=video_num_columns) return; i = x; while (++i < video_num_columns) { *p = *(p+1); p++; } *p = video_erase_char; } static void delete_line(void) { int oldtop,oldbottom; oldtop=top; oldbottom=bottom; top=y; bottom = video_num_lines; scrup(); top=oldtop; bottom=oldbottom; } static void csi_at(unsigned int nr) { if (nr > video_num_columns) nr = video_num_columns; else if (!nr) nr = 1; while (nr--) insert_char(); } static void csi_L(unsigned int nr) { if (nr > video_num_lines) nr = video_num_lines; else if (!nr) nr = 1; while (nr--) insert_line(); } static void csi_P(unsigned int nr) { if (nr > video_num_columns) nr = video_num_columns; else if (!nr) nr = 1; while (nr--) delete_char(); } static void csi_M(unsigned int nr) { if (nr > video_num_lines) nr = video_num_lines; else if (!nr) nr=1; while (nr--) delete_line(); } static int saved_x=0; static int saved_y=0; static void save_cur(void) { saved_x=x; saved_y=y; } static void restore_cur(void) { gotoxy(saved_x, saved_y); } void con_write(struct tty_struct * tty) { int nr; char c; nr = CHARS(tty->write_q); while (nr--) { GETCH(tty->write_q,c); switch(state) { case 0: if (c>31 && c<127) { if (x>=video_num_columns) { x -= video_num_columns; pos -= video_size_row; lf(); } __asm__("movb _attr,%%ah\n\t" "movw %%ax,%1\n\t" ::"a" (c),"m" (*(short *)pos) :"ax"); pos += 2; x++; } else if (c==27) state=1; else if (c==10 || c==11 || c==12) lf(); else if (c==13) cr(); else if (c==ERASE_CHAR(tty)) del(); else if (c==8) { if (x) { x--; pos -= 2; } } else if (c==9) { c=8-(x&7); x += c; pos += c<<1; if (x>video_num_columns) { x -= video_num_columns; pos -= video_size_row; lf(); } c=9; } else if (c==7) sysbeep(); break; case 1: state=0; if (c=='[') state=2; else if (c=='E') gotoxy(0,y+1); else if (c=='M') ri(); else if (c=='D') lf(); else if (c=='Z') respond(tty); else if (x=='7') save_cur(); else if (x=='8') restore_cur(); break; case 2: for(npar=0;npar<NPAR;npar++) par[npar]=0; npar=0; state=3; if (ques=(c=='?')) break; case 3: if (c==';' && npar<NPAR-1) { npar++; break; } else if (c>='0' && c<='9') { par[npar]=10*par[npar]+c-'0'; break; } else state=4; case 4: state=0; switch(c) { case 'G': case '`': if (par[0]) par[0]--; gotoxy(par[0],y); break; case 'A': if (!par[0]) par[0]++; gotoxy(x,y-par[0]); break; case 'B': case 'e': if (!par[0]) par[0]++; gotoxy(x,y+par[0]); break; case 'C': case 'a': if (!par[0]) par[0]++; gotoxy(x+par[0],y); break; case 'D': if (!par[0]) par[0]++; gotoxy(x-par[0],y); break; case 'E': if (!par[0]) par[0]++; gotoxy(0,y+par[0]); break; case 'F': if (!par[0]) par[0]++; gotoxy(0,y-par[0]); break; case 'd': if (par[0]) par[0]--; gotoxy(x,par[0]); break; case 'H': case 'f': if (par[0]) par[0]--; if (par[1]) par[1]--; gotoxy(par[1],par[0]); break; case 'J': csi_J(par[0]); break; case 'K': csi_K(par[0]); break; case 'L': csi_L(par[0]); break; case 'M': csi_M(par[0]); break; case 'P': csi_P(par[0]); break; case '@': csi_at(par[0]); break; case 'm': csi_m(); break; case 'r': if (par[0]) par[0]--; if (!par[1]) par[1] = video_num_lines; if (par[0] < par[1] && par[1] <= video_num_lines) { top=par[0]; bottom=par[1]; } break; case 's': save_cur(); break; case 'u': restore_cur(); break; } } } set_cursor(); } /* * void con_init(void); * * This routine initalizes console interrupts, and does nothing * else. If you want the screen to clear, call tty_write with * the appropriate escape-sequece. * * Reads the information preserved by setup.s to determine the current display * type and sets everything accordingly. */ void con_init(void) { register unsigned char a; char *display_desc = "????"; char *display_ptr; video_num_columns = ORIG_VIDEO_COLS; video_size_row = video_num_columns * 2; video_num_lines = ORIG_VIDEO_LINES; video_page = ORIG_VIDEO_PAGE; video_erase_char = 0x0720; if (ORIG_VIDEO_MODE == 7) /* Is this a monochrome display? */ { video_mem_start = 0xb0000; video_port_reg = 0x3b4; video_port_val = 0x3b5; if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10) { video_type = VIDEO_TYPE_EGAM; video_mem_end = 0xb8000; display_desc = "EGAm"; } else { video_type = VIDEO_TYPE_MDA; video_mem_end = 0xb2000; display_desc = "*MDA"; } } else /* If not, it is color. */ { video_mem_start = 0xb8000; video_port_reg = 0x3d4; video_port_val = 0x3d5; if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10) { video_type = VIDEO_TYPE_EGAC; video_mem_end = 0xbc000; display_desc = "EGAc"; } else { video_type = VIDEO_TYPE_CGA; video_mem_end = 0xba000; display_desc = "*CGA"; } } /* Let the user known what kind of display driver we are using */ display_ptr = ((char *)video_mem_start) + video_size_row - 8; while (*display_desc) { *display_ptr++ = *display_desc++; display_ptr++; } /* Initialize the variables used for scrolling (mostly EGA/VGA) */ origin = video_mem_start; scr_end = video_mem_start + video_num_lines * video_size_row; top = 0; bottom = video_num_lines; gotoxy(ORIG_X,ORIG_Y); set_trap_gate(0x21,&keyboard_interrupt); outb_p(inb_p(0x21)&0xfd,0x21); a=inb_p(0x61); outb_p(a|0x80,0x61); outb(a,0x61); } /* from bsd-net-2: */ void sysbeepstop(void) { /* disable counter 2 */ outb(inb_p(0x61)&0xFC, 0x61); } int beepcount = 0; static void sysbeep(void) { /* enable counter 2 */ outb_p(inb_p(0x61)|3, 0x61); /* set command for counter 2, 2 byte write */ outb_p(0xB6, 0x43); /* send 0x637 for 750 HZ */ outb_p(0x37, 0x42); outb(0x06, 0x42); /* 1/8 second */ beepcount = HZ/8; }
一些宏,值取自物理地址0x90000处。0x90000处的值是在setup.s中存入的。参考:https://www.cnblogs.com/zhenjingcool/p/15944047.html。比如在setup.s中会调用BIOS中断0x10获取光标位置和显示卡参数,存放到0x90000开始的内存区域。这里取出来这些参数放在宏定义中。
#define ORIG_X (*(unsigned char *)0x90000) //初始光标列号 #define ORIG_Y (*(unsigned char *)0x90001) //初始光标行号 #define ORIG_VIDEO_PAGE (*(unsigned short *)0x90004) //显示的页面 #define ORIG_VIDEO_MODE ((*(unsigned short *)0x90006) & 0xff) //显示模式 #define ORIG_VIDEO_COLS (((*(unsigned short *)0x90006) & 0xff00) >> 8) //字符的列数 #define ORIG_VIDEO_LINES (25) //显示的行号 #define ORIG_VIDEO_EGA_AX (*(unsigned short *)0x90008) #define ORIG_VIDEO_EGA_BX (*(unsigned short *)0x9000a) //内存大小和彩色模式 #define ORIG_VIDEO_EGA_CX (*(unsigned short *)0x9000c) //显示卡的特性参数 #define VIDEO_TYPE_MDA 0x10 /* Monochrome Text Display */单色文本 #define VIDEO_TYPE_CGA 0x11 /* CGA Display */ CGA #define VIDEO_TYPE_EGAM 0x20 /* EGA/VGA in Monochrome Mode */ EGA或者VGA的单色模式 #define VIDEO_TYPE_EGAC 0x21 /* EGA/VGA in Color Mode */ EGA或者VGA的彩色模式 #define NPAR 16 //转移序列中最大参数的个数
键盘中断处理程序
extern void keyboard_interrupt(void);
static unsigned char video_type; //当前使用的显示类型 /* Type of display being used */ static unsigned long video_num_columns; //屏幕文本列数 /* Number of text columns */ static unsigned long video_size_row; //屏幕每行使用的字节数 /* Bytes per row */ static unsigned long video_num_lines; //屏幕文本的行数 /* Number of test lines */ static unsigned char video_page; //初始显示的页面 /* Initial video page */ static unsigned long video_mem_start; //显示内存的起始的地址 /* Start of video RAM */ static unsigned long video_mem_end; //显示内存的结束地址 /* End of video RAM (sort of) */ static unsigned short video_port_reg; //显示控制器索引寄存器的端口 /* Video register select port */ static unsigned short video_port_val; //显示控制数据寄存器的端口 /* Video register value port */ static unsigned short video_erase_char; //擦除字符的属性以及字符 /* Char+Attrib to erase with */ static unsigned long origin; //EGA或者VGA快速卷屏的。卷屏起始内存的地址 /* Used for EGA/VGA fast scroll */ static unsigned long scr_end; //滚屏末端内存的地址 /* Used for EGA/VGA fast scroll */ static unsigned long pos;//光标 static unsigned long x,y;//当前的列号和行号 static unsigned long top,bottom; //滚动的时候顶行的行号和底行的行号 static unsigned long state=0; //nsr转义字符序列中表示状态 static unsigned long npar,par[NPAR]; //npar表示转义字符序列参数的个数。par是转义字符序列参数数组 static unsigned long ques=0; //?字符的标志 static unsigned char attr=0x07; //字符的属性,0x07表示黑底白字
系统蜂鸣的函数
static void sysbeep(void);
csi是控制字符引导码。主机通过发送设备属性控制序列,要求终端应答一个设备属性控制序列。终端就发送'\033[?1;2c'来响应主机。
/* * this is what the terminal answers to a ESC-Z or csi0c * query (= vt100 response). */ #define RESPONSE "\033[?1;2c" //代表ESC[?1;2c表示终端是高级视频的终端
根据新的行号和列号,定位新的内存的位置pos
static inline void gotoxy(unsigned int new_x,unsigned int new_y) { if (new_x > video_num_columns || new_y >= video_num_lines) //行号和列号是否超过范围 return; x=new_x; y=new_y; pos=origin + y*video_size_row + (x<<1); //行号和列号对应的内存的地址,x左移1位表示其除以2,因为一列用两个字节表示 }
设置滚屏的起始显示内存的地址
static inline void set_origin(void) { cli(); //关中断 outb_p(12, video_port_reg); //向video_port_reg输出12,用它来选择显示控制数据寄存器22 outb_p(0xff&((origin-video_mem_start)>>9), video_port_val); //写入卷屏的起始地址的低字节右移9位(右移8位再除以2),高字节放到video_port_val当中 outb_p(13, video_port_reg); //选择控制数据寄存器23 outb_p(0xff&((origin-video_mem_start)>>1), video_port_val); //写入卷屏起始地址的低字节除以2(1个字符占用2个字节),低字节放入video_port_val当中 sti(); //开中断 }
向上卷动1行
static void scrup(void) { if (video_type == VIDEO_TYPE_EGAC || video_type == VIDEO_TYPE_EGAM) //显卡类型是EGA或者VGA时 { if (!top && bottom == video_num_lines) { //如果顶层行top=0或者最底下一行bottom=最大能显示的行数。表示整个屏幕都向下移动 origin += video_size_row; // pos += video_size_row; scr_end += video_size_row; if (scr_end > video_mem_end) { __asm__("cld\n\t" "rep\n\t" "movsl\n\t" "movl _video_num_columns,%1\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((video_num_lines-1)*video_num_columns>>1), "D" (video_mem_start), "S" (origin) :"cx","di","si"); scr_end -= origin-video_mem_start; pos -= origin-video_mem_start; origin = video_mem_start; } else { __asm__("cld\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" (video_num_columns), "D" (scr_end-video_size_row) :"cx","di"); } set_origin(); } else { __asm__("cld\n\t" "rep\n\t" "movsl\n\t" "movl _video_num_columns,%%ecx\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((bottom-top-1)*video_num_columns>>1), "D" (origin+video_size_row*top), "S" (origin+video_size_row*(top+1)) :"cx","di","si"); } } else /* Not EGA/VGA */ { __asm__("cld\n\t" "rep\n\t" "movsl\n\t" "movl _video_num_columns,%%ecx\n\t" "rep\n\t" "stosw" ::"a" (video_erase_char), "c" ((bottom-top-1)*video_num_columns>>1), "D" (origin+video_size_row*top), "S" (origin+video_size_row*(top+1)) :"cx","di","si"); } }
光标位置下移一行
static void lf(void) { if (y+1<bottom) { y++; pos += video_size_row; return; } scrup(); }
在同一列上移1行
static void ri(void) { if (y>top) { y--; pos -= video_size_row; return; } scrdown(); }
光标回到行首
static void cr(void) { pos -= x<<1; x=0; }
删除光标之前的一个字符
static void del(void) { if (x) { pos -= 2; x--; *(unsigned short *)pos = video_erase_char; } }
删除屏幕上与光标位置相关的部分
static void csi_J(int par) { long count __asm__("cx"); long start __asm__("di"); switch (par) { case 0: /* erase from cursor to end of display */ count = (scr_end-pos)>>1; start = pos; break; case 1: /* erase from start to cursor */ count = (pos-origin)>>1; start = origin; break; case 2: /* erase whole display */ count = video_num_columns * video_num_lines; start = origin; break; default: return; } __asm__("cld\n\t" "rep\n\t" "stosw\n\t" ::"c" (count), "D" (start),"a" (video_erase_char) :"cx","di"); }
删除一行中与光标位置相关的部分
static void csi_K(int par) { long count __asm__("cx"); long start __asm__("di"); switch (par) { case 0: /* erase from cursor to end of line */ if (x>=video_num_columns) return; count = video_num_columns-x; start = pos; break; case 1: /* erase from start of line to cursor */ start = pos - (x<<1); count = (x<video_num_columns)?x:video_num_columns; break; case 2: /* erase whole line */ start = pos - (x<<1); count = video_num_columns; break; default: return; } __asm__("cld\n\t" "rep\n\t" "stosw\n\t" ::"c" (count), "D" (start),"a" (video_erase_char) :"cx","di"); }
设置显示字符的属性
void csi_m(void) { int i; for (i=0;i<=npar;i++) switch (par[i]) { case 0:attr=0x07;break; case 1:attr=0x0f;break; case 4:attr=0x0f;break; case 7:attr=0x70;break; case 27:attr=0x07;break; } }
static void respond(struct tty_struct * tty) { char * p = RESPONSE; cli(); while (*p) { PUTCH(*p,tty->read_q); p++; } sti(); copy_to_cooked(tty); }
在光标位置插入一个空格字符
static void insert_char(void) { int i=x; unsigned short tmp, old = video_erase_char; unsigned short * p = (unsigned short *) pos; while (i++<video_num_columns) { tmp=*p; *p=old; old=tmp; p++; } }
光标位置插入1行
static void insert_line(void) { int oldtop,oldbottom; oldtop=top; oldbottom=bottom; top=y; bottom = video_num_lines; scrdown(); top=oldtop; bottom=oldbottom; }
在光标位置处插入nr个字符
static void csi_at(unsigned int nr) { if (nr > video_num_columns) nr = video_num_columns; else if (!nr) nr = 1; while (nr--) insert_char(); }
在光标位置插入nr个行
static void csi_L(unsigned int nr) { if (nr > video_num_lines) nr = video_num_lines; else if (!nr) nr = 1; while (nr--) insert_line(); }
删除光标位置的nr个字符
static void csi_P(unsigned int nr) { if (nr > video_num_columns) nr = video_num_columns; else if (!nr) nr = 1; while (nr--) delete_char(); }
删除光标位置的nr行
static void csi_M(unsigned int nr) { if (nr > video_num_lines) nr = video_num_lines; else if (!nr) nr=1; while (nr--) delete_line(); }
保存光标的行号和列号,外部可访问
static int saved_x=0; static int saved_y=0;
保存光标位置
static void save_cur(void) { saved_x=x; saved_y=y; }
恢复到保存的光标位置
static void restore_cur(void) { gotoxy(saved_x, saved_y); }
重点来了,从终端对应的tty写缓冲队列当中取字符,针对每个字符进行分析,如果是控制字符或者转义字符控制序列,则进行光标的定位、字符删除等一些控制;对普通字符我们直接显示在光标的位置
void con_write(struct tty_struct * tty) { int nr; char c; nr = CHARS(tty->write_q); //nr为从缓冲队列中取出的字符个数 while (nr--) { GETCH(tty->write_q,c); //取出一个字符放到c当中 switch(state) { //对state进行测试。初始化时是0 case 0: if (c>31 && c<127) { //是否是普通的显示字符 if (x>=video_num_columns) { //x位置是否在行的末尾的位置,在末尾则进行换行操作 x -= video_num_columns; pos -= video_size_row; lf(); } __asm__("movb _attr,%%ah\n\t" //写字符。//把显示的属性_attr赋给ah "movw %%ax,%1\n\t" //把ax当中的内容放到pos位置 ::"a" (c),"m" (*(short *)pos) //输入参数:第一个参数:c赋给eax,第二个参数pos :"ax"); pos += 2; //pos位置加2 x++; } else if (c==27) //是否是ESC,说明可能是一个转义序列 state=1; else if (c==10 || c==11 || c==12) //是否是换行符|垂直制表|分页符 lf(); //换行 else if (c==13) //是否是回车符 cr(); //光标回到行开始位置 else if (c==ERASE_CHAR(tty)) //是否是DEL(127) del(); //擦除光标左边的字符 else if (c==8) { //如果是backSpace if (x) { //光标左移一个格 x--; pos -= 2; } } else if (c==9) { //是否是水平制表 c=8-(x&7); x += c; pos += c<<1; if (x>video_num_columns) { x -= video_num_columns; pos -= video_size_row; lf(); } c=9; } else if (c==7) //是否是响铃对应的ascii码 sysbeep(); //蜂鸣 break; case 1: //上面case=0时,如果遇到ESC字符,则设置state=1,然后获取下一个字符,执行到这里。判断ESC下一个字符的类型 state=0; //state复位 if (c=='[') //如果是[,说明确实是转义序列,state赋值2 state=2; else if (c=='E') //如果是ESC+E组合,则光标位置移动到下一行的第一个位置处 gotoxy(0,y+1); else if (c=='M') //如果是ESC+M组合,则光标上移 ri(); else if (c=='D') //如果ESC+D,则贯标下移 lf(); else if (c=='Z') //如果是ESC+Z组合,则执行response操作,发送终端应答字符序列 respond(tty); else if (x=='7') //如果x='7',则保存光标位置 save_cur(); else if (x=='8') //如果x='8',则恢复光标位置 restore_cur(); break; case 2: for(npar=0;npar<NPAR;npar++) //首先把par数组清零 par[npar]=0; npar=0; //npar指向首项 state=3; //state=3 if (ques=(c=='?')) //判断是否是? break; //如果是?则表明这个序列是终端设备私有的序列 ,返回不再循环 case 3: if (c==';' && npar<NPAR-1) { //如果是;且索引值没有满 npar++; //索引值加1 break; } else if (c>='0' && c<='9') { //如果是数字字符 par[npar]=10*par[npar]+c-'0'; //把字符转换成数值 break; } else state=4; case 4: state=0; //state置0 switch(c) { case 'G': case '`': //如果是G或者` if (par[0]) par[0]--; gotoxy(par[0],y); //光标右移一格 break; case 'A': if (!par[0]) par[0]++; gotoxy(x,y-par[0]); //光标上移一行 break; case 'B': case 'e': if (!par[0]) par[0]++; gotoxy(x,y+par[0]); //下移一行 break; case 'C': case 'a': if (!par[0]) par[0]++; gotoxy(x+par[0],y); //参数为0的右移一格 break; case 'D': if (!par[0]) par[0]++; gotoxy(x-par[0],y); //参数为0的左移一格 break; case 'E': if (!par[0]) par[0]++; gotoxy(0,y+par[0]); break; case 'F': if (!par[0]) par[0]++; gotoxy(0,y-par[0]); break; case 'd': if (par[0]) par[0]--; gotoxy(x,par[0]); break; case 'H': case 'f': if (par[0]) par[0]--; if (par[1]) par[1]--; gotoxy(par[1],par[0]); break; case 'J': csi_J(par[0]); //删除光标处的字符,csr_J,csr_K,csr_L等删除的方式各不相同。 break; case 'K': csi_K(par[0]); break; case 'L': csi_L(par[0]); break; case 'M': csi_M(par[0]); break; case 'P': csi_P(par[0]); break; case '@': csi_at(par[0]); break; case 'm': csi_m(); break; case 'r': if (par[0]) par[0]--; if (!par[1]) par[1] = video_num_lines; if (par[0] < par[1] && par[1] <= video_num_lines) { top=par[0]; bottom=par[1]; } break; case 's': save_cur(); break; case 'u': restore_cur(); break; } } } set_cursor(); //根据上面光标的显示操作,设置光标的位置,set_cursor定义如下 }
static inline void set_cursor(void) { cli(); outb_p(14, video_port_reg); outb_p(0xff&((pos-video_mem_start)>>9), video_port_val); outb_p(15, video_port_reg); outb_p(0xff&((pos-video_mem_start)>>1), video_port_val); sti(); }
初始化控制台终端,在main.c中被调用过(main.c中调用了tty_init(),tty_init()中调用了con_init())
/* * void con_init(void); * * This routine initalizes console interrupts, and does nothing * else. If you want the screen to clear, call tty_write with * the appropriate escape-sequece. * * Reads the information preserved by setup.s to determine the current display * type and sets everything accordingly. */ void con_init(void) { register unsigned char a; char *display_desc = "????"; char *display_ptr; video_num_columns = ORIG_VIDEO_COLS; //显示器显示字符的列数 video_size_row = video_num_columns * 2; //每行数据使用的字节数 video_num_lines = ORIG_VIDEO_LINES; //显示器显示字符的行数 video_page = ORIG_VIDEO_PAGE; //当前的显示页面 video_erase_char = 0x0720; //0x0720是擦除字符 if (ORIG_VIDEO_MODE == 7) //单色显示 /* Is this a monochrome display? */ { video_mem_start = 0xb0000; //显示内存的起始位置 video_port_reg = 0x3b4; //单显的索引寄存器的端口 video_port_val = 0x3b5; //单显的数据寄存器的端口 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10) //EGA卡 { video_type = VIDEO_TYPE_EGAM; //显示类型为EGA的单色模式 video_mem_end = 0xb8000; //显示内存末端 display_desc = "EGAm"; //显示描述字符串为EGAm } else { video_type = VIDEO_TYPE_MDA; //单色显示卡MDA video_mem_end = 0xb2000; //显示内存结束地址 display_desc = "*MDA"; //描述字符串 } } else //彩色显示卡 /* If not, it is color. */ { video_mem_start = 0xb8000; //显示内存的起始地址是0xb8000 video_port_reg = 0x3d4; //显示索引控制器端口 video_port_val = 0x3d5; //显示数据控制器端口 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10) //EGA或者VGA的单色 { video_type = VIDEO_TYPE_EGAC; video_mem_end = 0xbc000; display_desc = "EGAc"; } else //CGA显示卡 { video_type = VIDEO_TYPE_CGA; video_mem_end = 0xba000; display_desc = "*CGA"; } } /* Let the user known what kind of display driver we are using */ display_ptr = ((char *)video_mem_start) + video_size_row - 8; //显示在屏幕第一行右边4个字节位置 while (*display_desc) { *display_ptr++ = *display_desc++; display_ptr++; } /* Initialize the variables used for scrolling (mostly EGA/VGA) */ origin = video_mem_start; //初始化用于滚屏的变量,起始地址 scr_end = video_mem_start + video_num_lines * video_size_row; //结束地址 top = 0; //最顶行行号 bottom = video_num_lines; //最低行行号 gotoxy(ORIG_X,ORIG_Y); //初始化当前光标所在位置 set_trap_gate(0x21,&keyboard_interrupt); //设置keyboard_interrupt陷阱门描述符为0x21 outb_p(inb_p(0x21)&0xfd,0x21); //取消8259A对键盘中断的屏蔽 a=inb_p(0x61); //读取键盘端口0x61 8255a的端口pb(pb7-pb0) outb_p(a|0x80,0x61); //禁止键盘工作 outb(a,0x61); //键盘复位 }
蜂鸣器
void sysbeepstop(void) //停止蜂鸣 { /* disable counter 2 */ outb(inb_p(0x61)&0xFC, 0x61); //禁止定时器2 } int beepcount = 0; static void sysbeep(void) //蜂鸣的实现 { /* enable counter 2 */ outb_p(inb_p(0x61)|3, 0x61); //开启定时器2 /* set command for counter 2, 2 byte write */ outb_p(0xB6, 0x43); //设置定时器命令 /* send 0x637 for 750 HZ */ outb_p(0x37, 0x42); //设置频率 outb(0x06, 0x42); /* 1/8 second */ beepcount = HZ/8; //蜂鸣时间设置为1/8秒 }