Browse Source

Lesson 20 step 1, the timer

Carlos 10 năm trước cách đây
mục cha
commit
7797630a24

+ 3 - 7
20-interrupts-timer/README.md

@@ -8,14 +8,10 @@ The timer is easy to configure. First we'll declare an `init_timer()` on `cpu/ti
 implement it on `cpu/timer.c`. It is just a matter of computing the clock frequency and
 sending the bytes to the appropriate ports.
 
-## se printa gibberish, pq? mirar primero si se arregla con un kprint_int
-## yo tenia una funcion que printaba enteros??!!! pero al rever. hacerla ahora bien y
-## en el proximo episodio limpiar codigo y crear una libc
-
+We will now fix `kernel/utils.c int_to_ascii()` to print the numbers in the correct order.
+For that, we need to implement `reverse()` and `strlen()`.
 
 Finally, go back to the `kernel/kernel.c` and do two things. Enable interrupts again
 (very important!) and then initialize the timer interrupt.
 
-Go `make run` and you'll see the clock ticking! Unfortunately we are not printing the correct values
-on screen, so we'll go ahead to `drivers/screen.c` and add a new `kprint_int()` method, also declaring
-it on `drivers/screen.h`
+Go `make run` and you'll see the clock ticking!

+ 5 - 1
20-interrupts-timer/cpu/timer.c

@@ -1,5 +1,6 @@
 #include "timer.h"
 #include "../drivers/screen.h"
+#include "../kernel/util.h"
 #include "isr.h"
 
 u32 tick = 0;
@@ -7,7 +8,10 @@ u32 tick = 0;
 static void timer_callback(registers_t regs) {
     tick++;
     kprint("Tick: ");
-    kprint(tick);
+    
+    char *tick_ascii;
+    int_to_ascii(tick, tick_ascii);
+    kprint(tick_ascii);
     kprint("\n");
 }
 

+ 18 - 1
20-interrupts-timer/kernel/util.c

@@ -26,5 +26,22 @@ void int_to_ascii(int n, char str[]) {
     if (sign < 0) str[i++] = '-';
     str[i] = '\0';
 
-    /* TODO: implement "reverse" */
+    reverse(str);
+}
+
+/* K&R */
+void reverse(char s[]) {
+    int c, i, j;
+    for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
+        c = s[i];
+        s[i] = s[j];
+        s[j] = c;
+    }
+}
+
+/* K&R */
+int strlen(char s[]) {
+    int i = 0;
+    while (s[i] != '\0') ++i;
+    return i;
 }

+ 2 - 0
20-interrupts-timer/kernel/util.h

@@ -6,5 +6,7 @@
 void memory_copy(char *source, char *dest, int nbytes);
 void memory_set(u8 *dest, u8 val, u32 len);
 void int_to_ascii(int n, char str[]);
+void reverse(char s[]);
+int strlen(char s[]);
 
 #endif