annotate io.c @ 1:42ba76f77035 default tip

the kernel with output and mem alloc
author meillo@marmaro.de
date Sun, 01 Nov 2009 23:50:51 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
1 #include <stdarg.h>
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
2 #include "io.h"
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
3
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
4 void prints(const char* str);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
5
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
6 static unsigned short* vidmem = (unsigned short*) 0xb8000;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
7 static unsigned short color = 0x0700;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
8 static int pos = 0;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
9
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
10 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
11 cls(void)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
12 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
13 unsigned short* v;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
14
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
15 for (v = vidmem; v < vidmem+(80*25); v++) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
16 *v = 0;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
17 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
18 pos = 0;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
19 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
20
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
21
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
22 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
23 setcolor(int fg, int bg)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
24 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
25 color = (char) ((fg<<2) & (bg<<3));
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
26 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
27
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
28 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
29 setpos(int line, int col)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
30 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
31 pos = line * 80 + col;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
32 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
33
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
34 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
35 relpos(int move)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
36 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
37 pos += move;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
38 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
39
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
40
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
41 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
42 printp(unsigned int n)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
43 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
44 int i, h;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
45 char str[11];
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
46 //int n = (int) p;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
47
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
48 str[0] = '0';
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
49 str[1] = 'x';
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
50 str[10] = '\0';
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
51 for (i=9; i>1; i--) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
52 h = n % 16;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
53 str[i] = ('0' + h);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
54 if (h > 9) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
55 str[i] += 'a' - '9' - 1;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
56 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
57 n /= 16;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
58 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
59 prints(str);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
60 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
61
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
62
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
63
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
64
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
65 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
66 printc(char c)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
67 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
68 putchar(c);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
69 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
70
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
71 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
72 prints(const char* str)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
73 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
74 const char* c;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
75
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
76 for (c = str; *c; c++) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
77 printc(*c);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
78 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
79 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
80
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
81 void
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
82 printd(int n)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
83 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
84 int i;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
85 char str[32];
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
86
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
87 str[31] = '\0';
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
88 for (i=30; i>0; i--) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
89 str[i] = ('0' + n%10);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
90 if (n == 0) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
91 i++;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
92 break;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
93 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
94 n /= 10;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
95 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
96 prints(&str[i]);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
97 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
98
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
99
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
100 int
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
101 putchar(int c)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
102 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
103 if (c == '\n') {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
104 pos = ((pos/80)+1) * 80;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
105 } else {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
106 vidmem[pos++] = (unsigned short)c | color;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
107 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
108 return (unsigned char)c;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
109 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
110
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
111 int
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
112 puts(const char* s)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
113 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
114 prints(s);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
115 putchar('\n');
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
116 return 1;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
117 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
118
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
119 int
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
120 printf(const char* fmt, ...)
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
121 {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
122 va_list ap;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
123 int i;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
124
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
125 va_start(ap, fmt);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
126 for (i=0; fmt[i]; i++) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
127 if (fmt[i] != '%') {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
128 printc(fmt[i]);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
129 continue;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
130 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
131 switch (fmt[++i]) {
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
132 case '%':
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
133 printc('%');
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
134 break;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
135 case 'c':
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
136 printc((char)va_arg(ap, int));
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
137 break;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
138 case 's':
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
139 prints(va_arg(ap, char*));
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
140 break;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
141 case 'd':
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
142 printd(va_arg(ap, int));
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
143 break;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
144 case 'p':
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
145 printp(va_arg(ap, unsigned int));
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
146 break;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
147 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
148 }
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
149 va_end(ap);
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
150 return 0;
42ba76f77035 the kernel with output and mem alloc
meillo@marmaro.de
parents:
diff changeset
151 }