geek OS project 0 (下)
现在我们环境已经搭好了,参考 geekos实验环境的搭建
在main.c中新加个函数,命名为projecto,函数的代码如下:
/*
 * GeekOS C code entry point
 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
 * Copyright (c) 2004, Iulian Neamtiu <neamtiu@cs.umd.edu>
 * $Revision: 1.51 $
 * 
 * This is free software.  You are permitted to use,
 * redistribute, and modify it as specified in the file "COPYING".
 */
 1#include <geekos/bootinfo.h>
 2#include <geekos/string.h>
 3#include <geekos/screen.h>
 4#include <geekos/mem.h>
 5#include <geekos/crc32.h>
 6#include <geekos/tss.h>
 7#include <geekos/int.h>
 8#include <geekos/kthread.h>
 9#include <geekos/trap.h>
10#include <geekos/timer.h>
11#include <geekos/keyboard.h>
1//added by 111qqz for project 0
2void project0()
3{
4    Print("To Exit hit Ctrl + d.\n");
5    Print("Hello from 111qqz !\n"); 
6    Print("This is a test of project0 !\n");
1    Keycode keycode;
2    while(1)
3    {
4        if( Read_Key(&keycode) )    //读取键盘按键状态
5        {
6            if(!( (keycode & KEY_SPECIAL_FLAG) || (keycode & KEY_RELEASE_FLAG)) ) //只处理非特殊按键的按下事件
7            {                
8                 int asciiCode = keycode &  0xff;    //低8位为Ascii码
 1                if( (keycode & KEY_CTRL_FLAG)==KEY_CTRL_FLAG  &&  asciiCode=='d')    //按下Ctrl键
 2                {
 3                    Print("\n---------BYE!--------\n");
 4                    Exit(1);                      
 5                }else
 6                {
 7                    Print("%c",(asciiCode=='\r') ? '\n' : asciiCode);
 8                }
 9            } 
10        }
11    }
12}
 1/*
 2 * Kernel C code entry point.
 3 * Initializes kernel subsystems, mounts filesystems,
 4 * and spawns init process.
 5 */
 6void Main(struct Boot_Info* bootInfo)
 7{
 8    Init_BSS();
 9    Init_Screen();
10    Init_Mem(bootInfo);
11    Init_CRC32();
12    Init_TSS();
13    Init_Interrupts();
14    Init_Scheduler();
15    Init_Traps();
16    Init_Timer();
17    Init_Keyboard();
1    Set_Current_Attr(ATTRIB(BLACK, GREEN|BRIGHT));
2    Print("Welcome 111qqz to GeekOS!\n");
3    Set_Current_Attr(ATTRIB(BLACK, GRAY));
1   // TODO("Start a kernel thread to echo pressed keys and print counts");
2    //added by 111qqz for project 0
3          struct Kernel_Thread *thread;
4    thread = Start_Kernel_Thread(&project0,0,PRIORITY_NORMAL,false);
5      // added for project 0 end
1    /* Now this thread is done. */
2    Exit(0);
3}
再修改Main函数,将TODO(“…..这一行替换为以下代码:
struct Kernel_Thread *thread; thread = Start_Kernel_Thread(&project0,0,PRIORITY_NORMAL,false);
替换的意思是,要把TODO那一行注释掉。。。
TODO语句的定义在src/project0/include/geekos/kassert.h中
1#define TODO(message)					\
2do {							\
3    Set_Current_Attr(ATTRIB(BLUE, GRAY|BRIGHT));	\
4    Print("Unimplemented feature: %s\n", (message));	\
5    while (1)						\
6	;						\
7} while (0)
可以看到,这是一个宏打印错误提示后,就直接进入一个死循环中,也就是执行到TODO之后程序就不会继续往下运行了,所以要继续进行调试project0就必须删除或者注释掉那条TODO。
保存代码,按上一篇文章中的方法编译,并在bochs中引导系统。 运行效果如下图所示:
