From fedca16fb57e1094e044d24cbaf344d180dbc5b9 Mon Sep 17 00:00:00 2001 From: 423A35C7 <609514299@qq.com> Date: Wed, 27 Mar 2024 10:35:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=96=84=E4=BA=86=E8=B5=B0=E6=97=B6?= =?UTF-8?q?=E4=B8=8D=E5=87=86=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E5=80=92?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E6=89=8B=E5=8A=A8=E5=81=9C=E6=AD=A2=E5=90=8E?= =?UTF-8?q?=E8=BD=AC=E4=B8=BA=E6=AD=A3=E8=AE=A1=E6=97=B6=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E4=B8=AD=E8=8B=B1=E6=96=87=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E7=9A=84=E8=AF=B4=E6=98=8E=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- labcodes_answer/lab1_result/kern/driver/clock.c | 5 +++-- labcodes_answer/lab1_result/kern/trap/trap.c | 6 ++++-- labcodes_answer/lab1_result/kern/trap/trap.h | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) 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"