diff --git a/labcodes_answer/lab1_result/kern/driver/clock.c b/labcodes_answer/lab1_result/kern/driver/clock.c index 41c862e..74a7297 100644 --- a/labcodes_answer/lab1_result/kern/driver/clock.c +++ b/labcodes_answer/lab1_result/kern/driver/clock.c @@ -33,8 +33,9 @@ void clock_init(void) { // set 8253 timer-chip outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT); - outb(IO_TIMER1, TIMER_DIV(1000) % 256); - outb(IO_TIMER1, TIMER_DIV(1000) / 256); + // 经过多次试验发现原始的10毫秒一次中断(即除以100)是最准时的,不要擅自改中断频率 + outb(IO_TIMER1, TIMER_DIV(100) % 256); + outb(IO_TIMER1, TIMER_DIV(100) / 256); // initialize time counter 'ticks' to zero ticks = 0; diff --git a/labcodes_answer/lab1_result/kern/trap/trap.c b/labcodes_answer/lab1_result/kern/trap/trap.c index c209f08..89cb96f 100644 --- a/labcodes_answer/lab1_result/kern/trap/trap.c +++ b/labcodes_answer/lab1_result/kern/trap/trap.c @@ -9,7 +9,7 @@ #include #include #include -#define TICK_NUM 100 +#define TICK_NUM 1 static void print_ticks() { cprintf("%d ticks\n",TICK_NUM); @@ -167,7 +167,8 @@ trap_dispatch(struct trapframe *tf) { switch (state) { case STARTED: if (ticks % TICK_NUM == 0) { - milliseconds += mode * TICK_NUM; + // 10毫秒触发一次时钟中断,TICK_NUM次中断触发一次更新,所以毫秒数应该乘TICK_NUM * 10 + milliseconds += mode * TICK_NUM * 10; // 当时间倒计时到0时转成停止状态 // 而正计时加了mode之后至少为1所以不会转换状态 // 使用小于等于0而不是等于0是为了防止万一出现异常导致小于0的情况 @@ -239,6 +240,7 @@ trap_dispatch(struct trapframe *tf) { break; case 'e': state = STOPPED; + mode = COUNTUP; cprintf(MSG_STOP, milliseconds / 1000, milliseconds % 1000); milliseconds = 0; break; diff --git a/labcodes_answer/lab1_result/kern/trap/trap.h b/labcodes_answer/lab1_result/kern/trap/trap.h index 305950c..174e96f 100644 --- a/labcodes_answer/lab1_result/kern/trap/trap.h +++ b/labcodes_answer/lab1_result/kern/trap/trap.h @@ -47,7 +47,8 @@ #define T_SWITCH_TOU 120 // user/kernel switch #define T_SWITCH_TOK 121 // user/kernel switch -#define ENGLISH +// 在VSCode中可以正常看到中文,但在QEMU中看到的是乱码 +#define ENGLISH // 使用中文则不需要这一行 #ifndef ENGLISH #define MSG_COUNTDOWN_STOP "倒计时到0了,停止,转到正计时。\n"