读书笔记《30天自制操作系统》day08

来源:互联网 发布:淘宝ab单危险 编辑:程序博客网 时间:2024/05/29 04:39

http://blog.csdn.net/ltbylc/article/details/8309257


1. 鼠标的显示,这个与显示字符是一样的道理,写像素

[cpp] view plaincopy
  1. void init_mouse_cursor8(char *mouse, char bc)  
  2. {  
  3.     static char cursor[16][16] = {  
  4.         "**************..",  
  5.         "*OOOOOOOOOOO*...",  
  6.         "*OOOOOOOOOO*....",  
  7.         "*OOOOOOOOO*.....",  
  8.         "*OOOOOOOO*......",  
  9.         "*OOOOOOO*.......",  
  10.         "*OOOOOOO*.......",  
  11.         "*OOOOOOOO*......",  
  12.         "*OOOO**OOO*.....",  
  13.         "*OOO*..*OOO*....",  
  14.         "*OO*....*OOO*...",  
  15.         "*O*......*OOO*..",  
  16.         "**........*OOO*.",  
  17.         "*..........*OOO*",  
  18.         "............*OO*",  
  19.         ".............***"  
  20.     };  
  21.     int x, y;  
  22.   
  23.     for (y = 0; y < 16; y++) {  
  24.         for (x = 0; x < 16; x++) {  
  25.             if (cursor[y][x] == '*') {  
  26.                 mouse[y * 16 + x] = COL8_000000;  
  27.             }  
  28.             if (cursor[y][x] == 'O') {  
  29.                 mouse[y * 16 + x] = COL8_FFFFFF;  
  30.             }  
  31.             if (cursor[y][x] == '.') {  
  32.                 mouse[y * 16 + x] = bc;  
  33.             }  
  34.         }  
  35.     }  
  36.     return;  
  37. }  
[cpp] view plaincopy
  1. void putblock8_8(char *vram, int vxsize, int pxsize,  
  2.     int pysize, int px0, int py0, char *buf, int bxsize)  
  3. {  
  4.     int x, y;  
  5.     for (y = 0; y < pysize; y++) {  
  6.         for (x = 0; x < pxsize; x++) {  
  7.             vram[(py0 + y) * vxsize + (px0 + x)] = buf[y * bxsize + x];  
  8.         }  
  9.     }  
  10.     return;  
  11. }  

2. 响应鼠标中断需要先激活鼠标控制电路,鼠标控制电路包含在键盘控制电路中。

asmhead.nas中也有类似代码,等待键盘控制电路准备好。

[plain] view plaincopy
  1. waitkbdout:  
  2.         IN       AL,0x64  
  3.         AND      AL,0x02  
  4.         JNZ     waitkbdout        
  5.         RET  
[cpp] view plaincopy
  1. #define PORT_KEYDAT             0x0060  
  2. #define PORT_KEYSTA             0x0064  
  3. #define PORT_KEYCMD             0x0064  
  4. #define KEYSTA_SEND_NOTREADY    0x02  
  5. #define KEYCMD_WRITE_MODE       0x60  
  6. #define KBC_MODE                0x47  
  7.   
  8. void wait_KBC_sendready(void)  
  9. {  
  10.     for (;;) {  
  11.         if ((io_in8(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY) == 0) {  
  12.             break;  
  13.         }  
  14.     }  
  15.     return;  
  16. }  
  17.   
  18. void init_keyboard(void)  
  19. {  
  20.     wait_KBC_sendready();  
  21.     io_out8(PORT_KEYCMD, KEYCMD_WRITE_MODE);  
  22.     wait_KBC_sendready();  
  23.     io_out8(PORT_KEYDAT, KBC_MODE);  
  24.     return;  
  25. }  
  26. define KEYCMD_SENDTO_MOUSE      0xd4  
  27. #define MOUSECMD_ENABLE         0xf4  
  28.   
  29. void enable_mouse(void)  
  30. {  
  31.     wait_KBC_sendready();  
  32.     io_out8(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE);  
  33.     wait_KBC_sendready();  
  34.     io_out8(PORT_KEYDAT, MOUSECMD_ENABLE);  
  35.     return;   
  36. }  

3. 中断处理程序要通知PIC1 IRQ12已经完成PIC0 IRQ2已经完成

[cpp] view plaincopy
  1. struct FIFO8 mousefifo;  
  2.   
  3. void inthandler2c(int *esp)  
  4. {  
  5.     unsigned char data;  
  6.     io_out8(PIC1_OCW2, 0x64);     
  7.     io_out8(PIC0_OCW2, 0x62);     
  8.     data = io_in8(PORT_KEYDAT);  
  9.     fifo8_put(&mousefifo, data);  
  10.     return;  
  11. }  

4. 鼠标发送来数据的处理,鼠标第一个先发送一个0xfa,之后发送以3个字节为单位的信息。

[cpp] view plaincopy
  1. struct MOUSE_DEC {  
  2.     unsigned char buf[3], phase;  
  3.     /*buf[0]与鼠标移动和按键滚轮都有关buf[1]与鼠标左右移动有关buf[2]与鼠标上下移动有关*/  
  4.     int x, y, btn;  
  5.     /*xy坐标和btn按键状态*/  
  6. };  
  7. int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat)  
  8. {  
  9.     if (mdec->phase == 0) {  
  10.           
  11.         if (dat == 0xfa) {  
  12.             mdec->phase = 1;  
  13.         }  
  14.         return 0;  
  15.     }  
  16.     if (mdec->phase == 1) {  
  17.           
  18.         if ((dat & 0xc8) == 0x08) {  
  19.               
  20.             mdec->buf[0] = dat;  
  21.             mdec->phase = 2;  
  22.         }  
  23.         return 0;  
  24.     }  
  25.     if (mdec->phase == 2) {  
  26.           
  27.         mdec->buf[1] = dat;  
  28.         mdec->phase = 3;  
  29.         return 0;  
  30.     }  
  31.     if (mdec->phase == 3) {  
  32.           
  33.         mdec->buf[2] = dat;  
  34.         mdec->phase = 1;  
  35.         mdec->btn = mdec->buf[0] & 0x07;  
  36.         mdec->x = mdec->buf[1];  
  37.         mdec->y = mdec->buf[2];  
  38.         if ((mdec->buf[0] & 0x10) != 0) {  
  39.             mdec->x |= 0xffffff00;  
  40.         }  
  41.         if ((mdec->buf[0] & 0x20) != 0) {  
  42.             mdec->y |= 0xffffff00;  
  43.         }  
  44.         mdec->y = - mdec->y;   
  45.         return 1;  
  46.     }  
  47.     return -1;   
  48. }  

 

[cpp] view plaincopy
  1. /*MariMain 处理鼠标信息部分代码*/  
  2. i = fifo8_get(&mousefifo);  
  3. io_sti();  
  4. if (mouse_decode(&mdec, i) != 0) {                    
  5.     sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y);  
  6.     if ((mdec.btn & 0x01) != 0) {  
  7.         s[1] = 'L';  
  8.     }  
  9.     if ((mdec.btn & 0x02) != 0) {  
  10.         s[3] = 'R';  
  11.     }  
  12.     if ((mdec.btn & 0x04) != 0) {  
  13.         s[2] = 'C';  
  14.     }  
  15.     boxfill8(binfo->vram, binfo->scrnx, COL8_008484, 32, 16, 32 + 15 * 8 - 1, 31);  
  16.     putfonts8_asc(binfo->vram, binfo->scrnx, 32, 16, COL8_FFFFFF, s);  
  17.                       
  18.     boxfill8(binfo->vram, binfo->scrnx, COL8_008484, mx, my, mx + 15, my + 15);   
  19.     mx += mdec.x;  
  20.     my += mdec.y;  
  21.     if (mx < 0) {  
  22.         mx = 0;  
  23.     }  
  24.     if (my < 0) {  
  25.         my = 0;  
  26.     }  
  27.     if (mx > binfo->scrnx - 16) {  
  28.         mx = binfo->scrnx - 16;  
  29.     }  
  30.     if (my > binfo->scrny - 16) {  
  31.         my = binfo->scrny - 16;  
  32.     }  
  33.     sprintf(s, "(%3d, %3d)", mx, my);  
  34.     boxfill8(binfo->vram, binfo->scrnx, COL8_008484, 0, 0, 79, 15);   
  35.     putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s);   
  36.     putblock8_8(binfo->vram, binfo->scrnx, 16, 16, mx, my, mcursor, 16);  

原创粉丝点击