geek OS project 0 (下)
现在我们环境已经搭好了,参考 geekos实验环境的搭建
在main.c中新加个函数,命名为projecto,函数的代码如下:
1/*
2 * GeekOS C code entry point
3 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
5 * Copyright (c) 2004, Iulian Neamtiu <neamtiu@cs.umd.edu>
6 * $Revision: 1.51 $
7 *
8 * This is free software. You are permitted to use,
9 * redistribute, and modify it as specified in the file "COPYING".
10 */
11
12#include <geekos/bootinfo.h>
13#include <geekos/string.h>
14#include <geekos/screen.h>
15#include <geekos/mem.h>
16#include <geekos/crc32.h>
17#include <geekos/tss.h>
18#include <geekos/int.h>
19#include <geekos/kthread.h>
20#include <geekos/trap.h>
21#include <geekos/timer.h>
22#include <geekos/keyboard.h>
23
24//added by 111qqz for project 0
25void project0()
26{
27 Print("To Exit hit Ctrl + d.\n");
28 Print("Hello from 111qqz !\n");
29 Print("This is a test of project0 !\n");
30
31
32
33 Keycode keycode;
34 while(1)
35 {
36 if( Read_Key(&keycode) ) //读取键盘按键状态
37 {
38 if(!( (keycode & KEY_SPECIAL_FLAG) || (keycode & KEY_RELEASE_FLAG)) ) //只处理非特殊按键的按下事件
39 {
40 int asciiCode = keycode & 0xff; //低8位为Ascii码
41
42 if( (keycode & KEY_CTRL_FLAG)==KEY_CTRL_FLAG && asciiCode=='d') //按下Ctrl键
43 {
44 Print("\n---------BYE!--------\n");
45 Exit(1);
46 }else
47 {
48 Print("%c",(asciiCode=='\r') ? '\n' : asciiCode);
49 }
50 }
51 }
52 }
53}
54
55
56/*
57 * Kernel C code entry point.
58 * Initializes kernel subsystems, mounts filesystems,
59 * and spawns init process.
60 */
61void Main(struct Boot_Info* bootInfo)
62{
63 Init_BSS();
64 Init_Screen();
65 Init_Mem(bootInfo);
66 Init_CRC32();
67 Init_TSS();
68 Init_Interrupts();
69 Init_Scheduler();
70 Init_Traps();
71 Init_Timer();
72 Init_Keyboard();
73
74
75 Set_Current_Attr(ATTRIB(BLACK, GREEN|BRIGHT));
76 Print("Welcome 111qqz to GeekOS!\n");
77 Set_Current_Attr(ATTRIB(BLACK, GRAY));
78
79
80
81 // TODO("Start a kernel thread to echo pressed keys and print counts");
82 //added by 111qqz for project 0
83 struct Kernel_Thread *thread;
84 thread = Start_Kernel_Thread(&project0,0,PRIORITY_NORMAL,false);
85 // added for project 0 end
86
87
88 /* Now this thread is done. */
89 Exit(0);
90}
再修改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中引导系统。 运行效果如下图所示:
