#前言#

>_<" 这里主要是多任务的初探~比较简单,主要是尝试保存当前任务、任务切换、恢复当前任务,而真正的多任务要比这个复杂的多,因为包含互不干扰,甚至是高度优化的并行技术!

 

一、保存当前任务:

>_<" 当向CPU发出切换命令的时候,CPU会先把寄存器中的值全部写入内存中,这样当切换回来时,可以从中断的地方继续执行。接下来,为了运行下一个程序,CPU会把所有的寄存器中的值从内存中读取出来,这就完成了一次切换。

>_<" 这里的结构TSS32是task status segment,是32位任务状态段,其也是内存段的一种,需要再GDT中进行定以后使用。

1 struct TSS32 {//task status segment 任务状态段
2     int backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3;//保存的不是寄存器的数据,而是与任务设置相关的信息,在执行任务切换的时候这些成员不会被写入(backlink除外,某些情况下会被写入)
3     int eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;//32位寄存器
4     int es, cs, ss, ds, fs, gs;//16位寄存器
5     int ldtr, iomap;//有关任务设置部分
6 };

 

二、任务切换:从任务A到任务B[12a]

>_<" 接下来要在main函数中创建两个TSS:任务A的TSS和任务B的TSS,然后对其初始化,接着再在GDT中定义...见下面的main函数(有删减,这里只展示核心部分):

 18 void HariMain(void)
 19 {
 25     int mx, my, i ,cursor_x, cursor_c, task_b_esp;//cursor_x是记录光标位置的变量,cursor_c表示光标现在的颜色,每隔0.5秒变化一次,任务B的栈
 41     struct TSS32 tss_a, tss_b;//首先建立2个TSS,任务a的TSS和任务b的TSS
110 tss_a.ldtr = 0;//先这样设置好 111 tss_a.iomap = 0x40000000; 112 tss_b.ldtr = 0; 113 tss_b.iomap = 0x40000000; 114 set_segmdesc(gdt + 3, 103, (int) &tss_a, AR_TSS32);//定义在gdt的3号,段长限制为103字节 115 set_segmdesc(gdt + 4, 103, (int) &tss_b, AR_TSS32); 116 //向TR寄存器写入这个值,因为刚才把当前运行任务的GDT定义为3号,TR寄存器是让CPU记住当前正在运行哪一个任务 117 //每当进行任务切换时,TR寄存器的值也会自动变换,task register 118 //每次给TR赋值的时候,必须把GDT的编号乘以8 119 load_tr(3 * 8); 120 task_b_esp = memman_alloc_4k(memman, 64 * 1024) + 64 * 1024;//要为任务B专门分配栈,直接用任务A的栈就会乱成一团糟 121 tss_b.eip = (int) &task_b_main;//任务b的开始地址 122 tss_b.eflags = 0x00000202; /* IF = 1; */ 123 tss_b.eax = 0; 124 tss_b.ecx = 0; 125 tss_b.edx = 0; 126 tss_b.ebx = 0; 127 tss_b.esp = task_b_esp; 128 tss_b.ebp = 0; 129 tss_b.esi = 0; 130 tss_b.edi = 0; 131 tss_b.es = 1 * 8; 132 tss_b.cs = 2 * 8; 133 tss_b.ss = 1 * 8; 134 tss_b.ds = 1 * 8; 135 tss_b.fs = 1 * 8; 136 tss_b.gs = 1 * 8; 137 138 for (;;) {140 if (fifo32_status(&fifo) == 0) {142 } else {145 if (256 <= i && i <= 511) {//键盘数据165 }else if (512 <= i && i <= 767) {//鼠标数据202 }else if(i==10){//10s定时 204 taskswitch4();//程序10s以后进行任务切换 205 }else if (i == 3){}219 } 220 } 221 }300 ///////////////////////////////////////////////////////////////////////////////////// 301 //功能:任务b的函数 302 //参数: 303 void task_b_main(void) 304 { 305 for (;;) { io_hlt(); } 306 }
  1 /* bootpack */
  2 
  3 #include "bootpack.h"
  4 #include <stdio.h>
  5 
  6 void make_window8(unsigned char *buf, int xsize, int ysize, char *title);
  7 void putfonts8_asc_sht(struct SHEET *sht, int x, int y, int c, int b, char *s, int l);//字符串显示
  8 void make_textbox8(struct SHEET *sht, int x0, int y0, int sx, int sy, int c);
  9 
 10 struct TSS32 {//task status segment 任务状态段
 11     int backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3;//保存的不是寄存器的数据,而是与任务设置相关的信息,在执行任务切换的时候这些成员不会被写入(backlink除外,某些情况下会被写入)
 12     int eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;//32位寄存器
 13     int es, cs, ss, ds, fs, gs;//16位寄存器
 14     int ldtr, iomap;//有关任务设置部分
 15 };
 16 void task_b_main(void);
 17 
 18 void HariMain(void)
 19 {
 20     struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
 21     struct FIFO32 fifo;//定时器FIFO
 22     char s[40];
 23     int fifobuf[128];
 24     struct TIMER *timer,*timer2,*timer3;//3个定时器
 25     int mx, my, i ,cursor_x, cursor_c, task_b_esp;//cursor_x是记录光标位置的变量,cursor_c表示光标现在的颜色,每隔0.5秒变化一次,任务B的栈
 26     unsigned int memtotal;
 27     struct MOUSE_DEC mdec;
 28     struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
 29     struct SHTCTL *shtctl;
 30     struct SHEET *sht_back, *sht_mouse, *sht_win;
 31     unsigned char *buf_back, buf_mouse[256], *buf_win;
 32     static char keytable[0x54] = {//键盘映射表
 33         0,   0,   '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '^', 0,   0,
 34         'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '@', '[', 0,   0,   'A', 'S',
 35         'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', ':', 0,   0,   ']', 'Z', 'X', 'C', 'V',
 36         'B', 'N', 'M', ',', '.', '/', 0,   '*', 0,   ' ', 0,   0,   0,   0,   0,   0,
 37         0,   0,   0,   0,   0,   0,   0,   '7', '8', '9', '-', '4', '5', '6', '+', '1',
 38         '2', '3', '0', '.'
 39     };
 40     
 41     struct TSS32 tss_a, tss_b;//首先建立2个TSS,任务a的TSS和任务b的TSS
 42     struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT;
 43     
 44     init_gdtidt();//在dsctbl.c中,负责分区和中断分区初始化[包括键盘和鼠标中断设定]
 45     init_pic();//在int.c中,负责中断初始化(硬件)
 46     io_sti();//在naskfunc.nas中,仅仅执行STI指令,是CLI的逆指令,前者是开中断,后者是禁止中断
 47     /*
 48     同一占用一个fifo,这里:
 49     0~1               光标闪烁用定时器
 50     3                 3秒定时器
 51     10                10秒定时器
 52     256~511            键盘输入(从键盘控制器读入的值再加上256)
 53     512~767            鼠标输入(从键盘控制器读入的值再加上512)
 54     */
 55     fifo32_init(&fifo, 128, fifobuf);//初始化fifo
 56     init_pit();//负责计时器初始化100hz
 57     init_keyboard(&fifo, 256);//初始化键盘控制电路//在fifo.c中,负责缓冲区初始化(缓冲区结构体,大小,缓冲区首址)
 58     enable_mouse(&fifo, 512, &mdec);//使能鼠标
 59     /*这里IMR是(interrupt mask register),意思是“中断屏蔽寄存器”,是8位寄存器,分别对应8路IRQ信号,如果一路是1则该路被屏蔽,因为键盘中断是IRQ1,鼠标中断是IRQ12,且PIC分主从2个,从PIC连接主PIC的IRQ2,所以想要有鼠标和键盘中断,要PIC0的IRQ1和IRQ2,和PIC1的IRQ4*/
 60     io_out8(PIC0_IMR, 0xf8); /* (11111000) *///PIT,PIC1,键盘许可
 61     io_out8(PIC1_IMR, 0xef); /* (11101111) */
 62 
 63     timer = timer_alloc();//3个定时器
 64     timer_init(timer, &fifo, 10);
 65     timer_settime(timer, 1000);
 66     timer2 = timer_alloc();
 67     timer_init(timer2, &fifo, 3);
 68     timer_settime(timer2, 300);
 69     timer3 = timer_alloc();
 70     timer_init(timer3, &fifo, 1);
 71     timer_settime(timer3, 50);
 72 
 73     //memman需要32KB内存
 74     memtotal = memtest(0x00400000, 0xbfffffff);//计算总量memtatal
 75     memman_init(memman);
 76     memman_free(memman, 0x00001000, 0x0009e000); /* 0x00001000 - 0x0009efff 将现在不用的字节以0x1000个字节为单位注册到memman里*/
 77     memman_free(memman, 0x00400000, memtotal - 0x00400000);
 78     
 79     init_palette();//调色板
 80     shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);//图层初始化函数
 81     sht_back  = sheet_alloc(shtctl);//分配一个背景窗口
 82     sht_mouse = sheet_alloc(shtctl);//分配一个鼠标窗口
 83     sht_win   = sheet_alloc(shtctl);//分配一个小窗口
 84     buf_back  = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny);//为背景窗口和普通小窗口分配缓存空间
 85     buf_win   = (unsigned char *) memman_alloc_4k(memman, 160 * 52);
 86     sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); /* 设定涂层缓冲区的大小和透明色的函数 */
 87     sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99);
 88     sheet_setbuf(sht_win, buf_win, 160, 52, -1); /* 设定涂层缓冲区的大小和透明色的函数 */
 89     init_screen8(buf_back, binfo->scrnx, binfo->scrny);//初始化屏幕,画矩形,形成最初的窗口界面
 90     init_mouse_cursor8(buf_mouse, 99);//准备鼠标指针(16*16),99是窗口背景颜色
 91     make_window8(buf_win, 160, 52, "WINDOW");//就像制作背景和鼠标一样,先准备一张图,然后在图层内描绘一个貌似窗口的图就可以了
 92     make_textbox8(sht_win, 8, 28, 144, 16, COL8_FFFFFF);//窗口上的文件编辑部分
 93     cursor_x = 8;
 94     cursor_c = COL8_FFFFFF;
 95     sheet_slide(sht_back, 0, 0);//上下左右移动窗口,即移动窗口至0,0
 96     mx = (binfo->scrnx - 16) / 2; /* 计算鼠标初始位置 */
 97     my = (binfo->scrny - 28 - 16) / 2;
 98     sheet_slide(sht_mouse, mx, my);//移动鼠标窗口
 99     sheet_slide(sht_win, 80, 72);//移动消息窗口
100     sheet_updown(sht_back,  0);//设置窗口对的高度,背景在最下面
101     sheet_updown(sht_win,   1);
102     sheet_updown(sht_mouse, 2);
103     sprintf(s, "(%3d, %3d)", mx, my);//显示鼠标位置
104     putfonts8_asc_sht(sht_back, 0, 0, COL8_FFFFFF, COL8_008484, s, 10);
105     sprintf(s, "memory %dMB   free : %dKB",
106             memtotal / (1024 * 1024), memman_total(memman) / 1024);
107     putfonts8_asc_sht(sht_back, 0, 32, COL8_FFFFFF, COL8_008484, s, 40);
108     sheet_refresh(sht_back, 0, 0, binfo->scrnx, 48);
109 
110     tss_a.ldtr = 0;//先这样设置好
111     tss_a.iomap = 0x40000000;
112     tss_b.ldtr = 0;
113     tss_b.iomap = 0x40000000;
114     set_segmdesc(gdt + 3, 103, (int) &tss_a, AR_TSS32);//定义在gdt的3号,段长限制为103字节
115     set_segmdesc(gdt + 4, 103, (int) &tss_b, AR_TSS32);
116     //向TR寄存器写入这个值,因为刚才把当前运行任务的GDT定义为3号,TR寄存器是让CPU记住当前正在运行哪一个任务
117     //每当进行任务切换时,TR寄存器的值也会自动变换,task register
118     //每次给TR赋值的时候,必须把GDT的编号乘以8
119     load_tr(3 * 8);
120     task_b_esp = memman_alloc_4k(memman, 64 * 1024) + 64 * 1024;//要为任务B专门分配栈,直接用任务A的栈就会乱成一团糟
121     tss_b.eip = (int) &task_b_main;//任务b的开始地址
122     tss_b.eflags = 0x00000202; /* IF = 1; */
123     tss_b.eax = 0;
124     tss_b.ecx = 0;
125     tss_b.edx = 0;
126     tss_b.ebx = 0;
127     tss_b.esp = task_b_esp;
128     tss_b.ebp = 0;
129     tss_b.esi = 0;
130     tss_b.edi = 0;
131     tss_b.es = 1 * 8;
132     tss_b.cs = 2 * 8;
133     tss_b.ss = 1 * 8;
134     tss_b.ds = 1 * 8;
135     tss_b.fs = 1 * 8;
136     tss_b.gs = 1 * 8;
137     
138     for (;;) {
139         io_cli();
140         if (fifo32_status(&fifo) == 0) {
141             io_sti();
142         } else {
143             i = fifo32_get(&fifo);
144             io_sti();
145             if (256 <= i && i <= 511) {//键盘数据
146                 sprintf(s, "%02X", i - 256);//减去256
147                 putfonts8_asc_sht(sht_back, 0, 16, COL8_FFFFFF, COL8_008484, s, 2);//清,显,刷
148                 if (i < 0x54 + 256) {
149                     if (keytable[i - 256] != 0 && cursor_x < 144) {//一般字符
150                         /* 显示一次就前移一次光标 */
151                         s[0] = keytable[i - 256];
152                         s[1] = 0;
153                         putfonts8_asc_sht(sht_win, cursor_x, 28, COL8_000000, COL8_FFFFFF, s, 1);
154                         cursor_x += 8;//记录光标位置
155                     }
156                 }
157                 if (i == 256 + 0x0e && cursor_x > 8) { /* 退格键 */
158                     /* 用空格键把光标消去后,后移1次光标 */
159                     putfonts8_asc_sht(sht_win, cursor_x, 28, COL8_000000, COL8_FFFFFF, " ", 1);
160                     cursor_x -= 8;
161                 }
162                 /* 光标再显示 */
163                 boxfill8(sht_win->buf, sht_win->bxsize, cursor_c, cursor_x, 28, cursor_x + 7, 43);
164                 sheet_refresh(sht_win, cursor_x, 28, cursor_x + 8, 44);
165             }else if (512 <= i && i <= 767) {//鼠标数据
166                 //已经收集了3字节的数据,所以显示出来
167                 if (mouse_decode(&mdec, i - 512) != 0) {
168                     sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y);
169                     if ((mdec.btn & 0x01) != 0) {
170                         s[1] = 'L';
171                     }
172                     if ((mdec.btn & 0x02) != 0) {
173                         s[3] = 'R';
174                     }
175                     if ((mdec.btn & 0x04) != 0) {
176                         s[2] = 'C';
177                     }
178                     putfonts8_asc_sht(sht_back, 32, 16, COL8_FFFFFF, COL8_008484, s, 15);//清,显,刷
179                     /* 移动鼠标 */
180                     mx += mdec.x;
181                     my += mdec.y;
182                     if (mx < 0) {
183                         mx = 0;
184                     }
185                     if (my < 0) {
186                         my = 0;
187                     }
188                     if (mx > binfo->scrnx - 1) {
189                         mx = binfo->scrnx - 1;
190                     }
191                     if (my > binfo->scrny - 1) {
192                         my = binfo->scrny - 1;
193                     }
194                     sprintf(s, "(%3d, %3d)", mx, my);
195                     putfonts8_asc_sht(sht_back, 0, 0, COL8_FFFFFF, COL8_008484, s, 10);//清,显,刷
196                     sheet_slide(sht_mouse, mx, my);
197                     //移动窗口计算
198                     if((mdec.btn & 0x01)!=0){
199                         sheet_slide(sht_win,mx-80,my-80);
200                     }//按下左键移动sht_win
201                 }
202             }else if(i==10){//10s定时
203                 putfonts8_asc_sht(sht_back, 0, 64, COL8_FFFFFF, COL8_008484, "10[sec]", 7);//清,显,刷、
204                 taskswitch4();//程序10s以后进行任务切换
205             }else if (i == 3) { //3秒定时器
206                 putfonts8_asc_sht(sht_back, 0, 80, COL8_FFFFFF, COL8_008484, "3[sec]", 6);
207             }else if (i<=1) { //光标用定时器
208                 if (i != 0) {
209                     timer_init(timer3, &fifo, 0); /* 師偼0傪 */
210                     cursor_c = COL8_000000;
211                 } else {
212                     timer_init(timer3, &fifo, 1); /* 師偼1傪 */
213                     cursor_c = COL8_FFFFFF;
214                 }
215                 timer_settime(timer3, 50);
216                 boxfill8(sht_win->buf, sht_win->bxsize, cursor_c, cursor_x, 28, cursor_x + 7, 43);
217                 sheet_refresh(sht_win, cursor_x, 28, cursor_x + 8, 44);
218             }
219         }
220     }
221 }
222 /////////////////////////////////////////////////////////////////////////////////////
223 //功能:就像制作背景和鼠标一样,先准备一张图,然后在图层内描绘一个貌似窗口的图就可以了
224 //参数:
225 void make_window8(unsigned char *buf, int xsize, int ysize, char *title)
226 {
227     static char closebtn[14][16] = {
228         "OOOOOOOOOOOOOOO@",
229         "OQQQQQQQQQQQQQ$@",
230         "OQQQQQQQQQQQQQ$@",
231         "OQQQ@@QQQQ@@QQ$@",
232         "OQQQQ@@QQ@@QQQ$@",
233         "OQQQQQ@@@@QQQQ$@",
234         "OQQQQQQ@@QQQQQ$@",
235         "OQQQQQ@@@@QQQQ$@",
236         "OQQQQ@@QQ@@QQQ$@",
237         "OQQQ@@QQQQ@@QQ$@",
238         "OQQQQQQQQQQQQQ$@",
239         "OQQQQQQQQQQQQQ$@",
240         "O$$$$$$$$$$$$$$@",
241         "@@@@@@@@@@@@@@@@"
242     };
243     int x, y;
244     char c;
245     boxfill8(buf, xsize, COL8_C6C6C6, 0,         0,         xsize - 1, 0        );
246     boxfill8(buf, xsize, COL8_FFFFFF, 1,         1,         xsize - 2, 1        );
247     boxfill8(buf, xsize, COL8_C6C6C6, 0,         0,         0,         ysize - 1);
248     boxfill8(buf, xsize, COL8_FFFFFF, 1,         1,         1,         ysize - 2);
249     boxfill8(buf, xsize, COL8_848484, xsize - 2, 1,         xsize - 2, ysize - 2);
250     boxfill8(buf, xsize, COL8_000000, xsize - 1, 0,         xsize - 1, ysize - 1);
251     boxfill8(buf, xsize, COL8_C6C6C6, 2,         2,         xsize - 3, ysize - 3);
252     boxfill8(buf, xsize, COL8_000084, 3,         3,         xsize - 4, 20       );
253     boxfill8(buf, xsize, COL8_848484, 1,         ysize - 2, xsize - 2, ysize - 2);
254     boxfill8(buf, xsize, COL8_000000, 0,         ysize - 1, xsize - 1, ysize - 1);
255     putfonts8_asc(buf, xsize, 24, 4, COL8_FFFFFF, title);
256     for (y = 0; y < 14; y++) {
257         for (x = 0; x < 16; x++) {
258             c = closebtn[y][x];
259             if (c == '@') {
260                 c = COL8_000000;
261             } else if (c == '$') {
262                 c = COL8_848484;
263             } else if (c == 'Q') {
264                 c = COL8_C6C6C6;
265             } else {
266                 c = COL8_FFFFFF;
267             }
268             buf[(5 + y) * xsize + (xsize - 21 + x)] = c;
269         }
270     }
271     return;
272 }
273 /////////////////////////////////////////////////////////////////////////////////////
274 //功能:先图上背景颜色,再显示字符,最后完成刷新
275 //参数:图层,位置,字体颜色,背景颜色,字符串,字符串长度
276 void putfonts8_asc_sht(struct SHEET *sht, int x, int y, int c, int b, char *s, int l)
277 {
278     boxfill8(sht->buf, sht->bxsize, b, x, y, x + l * 8 - 1, y + 15);
279     putfonts8_asc(sht->buf, sht->bxsize, x, y, c, s);
280     sheet_refresh(sht, x, y, x + l * 8, y + 16);
281     return;
282 }
283 /////////////////////////////////////////////////////////////////////////////////////
284 //功能:描绘文字输入背景的
285 //参数:
286 void make_textbox8(struct SHEET *sht, int x0, int y0, int sx, int sy, int c)
287 {
288     int x1 = x0 + sx, y1 = y0 + sy;
289     boxfill8(sht->buf, sht->bxsize, COL8_848484, x0 - 2, y0 - 3, x1 + 1, y0 - 3);
290     boxfill8(sht->buf, sht->bxsize, COL8_848484, x0 - 3, y0 - 3, x0 - 3, y1 + 1);
291     boxfill8(sht->buf, sht->bxsize, COL8_FFFFFF, x0 - 3, y1 + 2, x1 + 1, y1 + 2);
292     boxfill8(sht->buf, sht->bxsize, COL8_FFFFFF, x1 + 2, y0 - 3, x1 + 2, y1 + 2);
293     boxfill8(sht->buf, sht->bxsize, COL8_000000, x0 - 1, y0 - 2, x1 + 0, y0 - 2);
294     boxfill8(sht->buf, sht->bxsize, COL8_000000, x0 - 2, y0 - 2, x0 - 2, y1 + 0);
295     boxfill8(sht->buf, sht->bxsize, COL8_C6C6C6, x0 - 2, y1 + 1, x1 + 0, y1 + 1);
296     boxfill8(sht->buf, sht->bxsize, COL8_C6C6C6, x1 + 1, y0 - 2, x1 + 1, y1 + 1);
297     boxfill8(sht->buf, sht->bxsize, c,           x0 - 1, y0 - 1, x1 + 0, y1 + 0);
298     return;
299 }
300 /////////////////////////////////////////////////////////////////////////////////////
301 //功能:任务b的函数
302 //参数:
303 void task_b_main(void)
304 {
305     for (;;) { io_hlt(); }
306 }
bootpack.c 全部代码

相关文章:

  • 2021-06-11
  • 2021-11-30
  • 2021-10-24
  • 2022-12-23
  • 2022-03-02
  • 2021-10-07
  • 2021-07-02
猜你喜欢
  • 2021-08-20
  • 2022-01-02
  • 2022-02-06
  • 2021-05-29
  • 2021-11-17
  • 2021-11-30
  • 2022-12-23
相关资源
相似解决方案