Browse Source

完成第一次实验

x86-32
423A35C7 7 months ago
parent
commit
cd4dff43d2
3 changed files with 101 additions and 7 deletions
  1. +2
    -2
      labcodes_answer/lab1_result/kern/driver/clock.c
  2. +78
    -5
      labcodes_answer/lab1_result/kern/trap/trap.c
  3. +21
    -0
      labcodes_answer/lab1_result/kern/trap/trap.h

+ 2
- 2
labcodes_answer/lab1_result/kern/driver/clock.c View File

@ -33,8 +33,8 @@ void
clock_init(void) {
// set 8253 timer-chip
outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
outb(IO_TIMER1, TIMER_DIV(100) % 256);
outb(IO_TIMER1, TIMER_DIV(100) / 256);
outb(IO_TIMER1, TIMER_DIV(1000) % 256);
outb(IO_TIMER1, TIMER_DIV(1000) / 256);
// initialize time counter 'ticks' to zero
ticks = 0;

+ 78
- 5
labcodes_answer/lab1_result/kern/trap/trap.c View File

@ -149,7 +149,12 @@ struct trapframe switchk2u, *switchu2k;
/* trap_dispatch - dispatch based on what type of trap occurred */
static void
trap_dispatch(struct trapframe *tf) {
char c;
volatile char c;
//
static enum {STARTED=0, STOPPED=1, WAITING_FOR_INPUT, READY_TO_OUTPUT} state = STOPPED;
// long long
static long milliseconds = 0;
static enum {COUNTDOWN=-1, COUNTUP=1} mode = COUNTUP;
switch (tf->tf_trapno) {
case IRQ_OFFSET + IRQ_TIMER:
@ -159,10 +164,27 @@ trap_dispatch(struct trapframe *tf) {
* (2) Every TICK_NUM cycle, you can print some info using a funciton, such as print_ticks().
* (3) Too Simple? Yes, I think so!
*/
ticks ++;
if (ticks % TICK_NUM == 0) {
print_ticks();
switch (state) {
case STARTED:
milliseconds += mode;
// 0
// mode之后至少为1所以不会转换状态
// 使000
state = READY_TO_OUTPUT * (milliseconds <= 0); //
break;
case READY_TO_OUTPUT:
state = STOPPED;
mode = COUNTUP;
if (milliseconds < 0) milliseconds = 0; // 0
cprintf(MSG_COUNTDOWN_STOP);
break;
default:
break;
}
ticks ++;
// if (ticks % TICK_NUM == 0) {
// print_ticks();
// }
break;
case IRQ_OFFSET + IRQ_COM1:
c = cons_getc();
@ -170,7 +192,58 @@ trap_dispatch(struct trapframe *tf) {
break;
case IRQ_OFFSET + IRQ_KBD:
c = cons_getc();
cprintf("kbd [%03d] %c\n", c, c);
if (state == WAITING_FOR_INPUT) {
long seconds = milliseconds / 1000;
// if (c != '\0') cprintf("%c", c);
//
// break不然就直接跳出中断处理程序了
// c=0
if (c == '\b' || c == '\n' || ('0' <= c && c <= '9')) {
cprintf("%c", c);
} else {
c = '\0';
}
if (c == '\b') {
seconds /= 10;
}
if ('0' <= c && c <= '9') {
seconds *= 10;
seconds += c - '0';
}
milliseconds = seconds * 1000;
if (c == '\n') {
state = STARTED;
}
c = '\0'; //
}
switch (c) {
case 'a':
mode = COUNTUP;
cprintf(MSG_COUNTUP_START);
break;
case 'b':
mode = COUNTDOWN;
cprintf(MSG_COUNTDOWN_START);
state = WAITING_FOR_INPUT;
break;
case 's':
case 'c':
state = STARTED;
cprintf(MSG_START);
break;
case 'p':
state = STOPPED;
cprintf(MSG_PAUSE, milliseconds / 1000, milliseconds % 1000);
break;
case 'e':
state = STOPPED;
cprintf(MSG_STOP, milliseconds / 1000, milliseconds % 1000);
milliseconds = 0;
break;
default:
break;
}
// cprintf("kbd [%03d] %c\n", c, c);
break;
//LAB1 CHALLENGE 1 : YOUR CODE you should modify below codes.
case T_SWITCH_TOU:

+ 21
- 0
labcodes_answer/lab1_result/kern/trap/trap.h View File

@ -47,6 +47,27 @@
#define T_SWITCH_TOU 120 // user/kernel switch
#define T_SWITCH_TOK 121 // user/kernel switch
#define ENGLISH
#ifndef ENGLISH
#define MSG_COUNTDOWN_STOP "倒计时到0了,停止,转到正计时。\n"
#define MSG_COUNTUP_START "正计时\n"
#define MSG_COUNTDOWN_START "倒计时,请输入秒数:\n"
#define MSG_START "开始\n"
#define MSG_PAUSE "暂停,当前秒数:%d.%d\n"
#define MSG_STOP "停止,当前秒数:%d.%d\n"
#else
#define MSG_COUNTDOWN_STOP "Coutdown to 0, stopped, to be countup.\n"
#define MSG_COUNTUP_START "Countup\n"
#define MSG_COUNTDOWN_START "Countdown, please input seconds:\n"
#define MSG_START "Started\n"
#define MSG_PAUSE "Paused, current seconds: %d.%d\n"
#define MSG_STOP "Stopped, current seconds: %d.%d\n"
#endif
/* registers as pushed by pushal */
struct pushregs {
uint32_t reg_edi;

Loading…
Cancel
Save