-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.c
executable file
·84 lines (69 loc) · 1.43 KB
/
console.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
#include "console.h"
#include "uart.h"
#include "util.h"
static const char digits[] = "0123456789abcdef";
void con_init( void ) {
uart_init();
}
unsigned char con_getc( void ) {
return uart_getc();
}
void con_putc( unsigned char c ) {
if( c == '\n' ) {
uart_putc( '\r' );
}
uart_putc( c );
}
void con_putaddr( addr_t a ) {
con_putc( digits[ (a & 0xf000) >> 12 ] );
con_putc( digits[ (a & 0x0f00) >> 8 ] );
con_putc( digits[ (a & 0x00f0) >> 4 ] );
con_putc( digits[ (a & 0x000f) ] );
}
void con_putword( word_t c ) {
con_putc( digits[ (c & 0xf0) >> 4 ] );
con_putc( digits[ (c & 0x0f) ] );
}
void con_puts( unsigned char *s ) {
char *pos = (char*)s;
while( *pos ) {
con_putc( *pos++ );
}
}
void con_crlf( void ) {
con_putc('\n');
}
/* Simple string to number converter */
int number( const char *string, int base ) {
char *c = string;
int result = 0;
char negative = 0;
if( base > 16 )
return 0;
while( *c == ' ' )
c++;
if( *c == '-' )
negative = 1;
while( *c != 0 ) {
int digit = 0;
if( *c >= '0' && *c <= '9' ) {
digit = *c - '0';
}
else if( *c >= 'a' && *c <= 'f' ) {
digit = 10 + *c - 'a';
}
else {
/* invalid char */
return 0;
}
if( digit > base - 1 ) {
/* char out of range for number base */
return 0;
}
result = (result * base) + digit;
c++;
}
if( negative )
result = -result;
return result;
}