Skip to content

Commit 7797630

Browse files
committed
Lesson 20 step 1, the timer
1 parent 1a98cab commit 7797630

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

20-interrupts-timer/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ The timer is easy to configure. First we'll declare an `init_timer()` on `cpu/ti
88
implement it on `cpu/timer.c`. It is just a matter of computing the clock frequency and
99
sending the bytes to the appropriate ports.
1010

11-
## se printa gibberish, pq? mirar primero si se arregla con un kprint_int
12-
## yo tenia una funcion que printaba enteros??!!! pero al rever. hacerla ahora bien y
13-
## en el proximo episodio limpiar codigo y crear una libc
14-
11+
We will now fix `kernel/utils.c int_to_ascii()` to print the numbers in the correct order.
12+
For that, we need to implement `reverse()` and `strlen()`.
1513

1614
Finally, go back to the `kernel/kernel.c` and do two things. Enable interrupts again
1715
(very important!) and then initialize the timer interrupt.
1816

19-
Go `make run` and you'll see the clock ticking! Unfortunately we are not printing the correct values
20-
on screen, so we'll go ahead to `drivers/screen.c` and add a new `kprint_int()` method, also declaring
21-
it on `drivers/screen.h`
17+
Go `make run` and you'll see the clock ticking!

20-interrupts-timer/cpu/timer.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#include "timer.h"
22
#include "../drivers/screen.h"
3+
#include "../kernel/util.h"
34
#include "isr.h"
45

56
u32 tick = 0;
67

78
static void timer_callback(registers_t regs) {
89
tick++;
910
kprint("Tick: ");
10-
kprint(tick);
11+
12+
char *tick_ascii;
13+
int_to_ascii(tick, tick_ascii);
14+
kprint(tick_ascii);
1115
kprint("\n");
1216
}
1317

20-interrupts-timer/kernel/util.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,22 @@ void int_to_ascii(int n, char str[]) {
2626
if (sign < 0) str[i++] = '-';
2727
str[i] = '\0';
2828

29-
/* TODO: implement "reverse" */
29+
reverse(str);
30+
}
31+
32+
/* K&R */
33+
void reverse(char s[]) {
34+
int c, i, j;
35+
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
36+
c = s[i];
37+
s[i] = s[j];
38+
s[j] = c;
39+
}
40+
}
41+
42+
/* K&R */
43+
int strlen(char s[]) {
44+
int i = 0;
45+
while (s[i] != '\0') ++i;
46+
return i;
3047
}

20-interrupts-timer/kernel/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
void memory_copy(char *source, char *dest, int nbytes);
77
void memory_set(u8 *dest, u8 val, u32 len);
88
void int_to_ascii(int n, char str[]);
9+
void reverse(char s[]);
10+
int strlen(char s[]);
911

1012
#endif

0 commit comments

Comments
 (0)