forked from danielevardito/ProgettoSO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklog.c
88 lines (74 loc) · 2.61 KB
/
klog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* @file klog.c
* @author Maldus512
* @brief Small library that implements a circular log buffer. When properly traced (with ASCII representation),
* `klog_buffer` displays a series of printed lines.
*/
#define KLOG_LINES 64 // Number of lines in the buffer. Adjustable, only limited by available memory
#define KLOG_LINE_SIZE 42 // Length of a single line in characters
static void next_line(void);
static void next_char(void);
unsigned int klog_line_index = 0; // Index of the next line to fill
unsigned int klog_char_index = 0; // Index of the current character in the line
char klog_buffer[KLOG_LINES][KLOG_LINE_SIZE] = {0}; // Actual buffer, to be traced in uMPS3
// Print str to klog
void klog_print(char *str) {
while (*str != '\0') {
// If there is a newline skip to the next one
if (*str == '\n') {
next_line();
str++;
}
// Otherwise just fill the current one
else {
klog_buffer[klog_line_index][klog_char_index] = *str++;
next_char();
}
}
}
/*
* Funzione per la stampa di numeri nei registri di memoria usati per il debugging.
* N.B. La funzione stampa numeri in un intervallo compreso tra 0 e 99
*/
void klog_print_dec(unsigned int num) {
const char digits[] = "0123456789";
if(num < 10){
do {
klog_buffer[klog_line_index][klog_char_index] = digits[num % 10];
num /= 10;
next_char();
} while (num > 0);
}else{
int buff = num % 10;
num /= 10;
klog_buffer[klog_line_index][klog_char_index] = digits[num % 10];
next_char();
klog_buffer[klog_line_index][klog_char_index] = digits[buff];
next_char();
}
}
// Princ a number in hexadecimal format (best for addresses)
void klog_print_hex(unsigned int num) {
const char digits[] = "0123456789ABCDEF";
do {
klog_buffer[klog_line_index][klog_char_index] = digits[num % 16];
num /= 16;
next_char();
} while (num > 0);
}
// Move onto the next character (and into the next line if the current one overflows)
static void next_char(void) {
if (++klog_char_index >= KLOG_LINE_SIZE) {
klog_char_index = 0;
next_line();
}
}
// Skip to next line
static void next_line(void) {
klog_line_index = (klog_line_index + 1) % KLOG_LINES;
klog_char_index = 0;
// Clean out the rest of the line for aesthetic purposes
for (unsigned int i = 0; i < KLOG_LINE_SIZE; i++) {
klog_buffer[klog_line_index][i] = ' ';
}
}