source: trunk/hal/x86_64/x86_printf.c @ 31

Last change on this file since 31 was 31, checked in by max@…, 7 years ago

Silence a few gcc warnings.

File size: 3.5 KB
Line 
1/*
2 * x86_printf.c - A printf function for x86 (debug only).
3 *
4 * Copyright (c) [don't know exactly, found on the internet... anyway, this
5 *                file will be removed soon...]
6 *
7 * This file is part of ALMOS-MKH.
8 *
9 * ALMOS-MKH is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS-MKH is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <hal_types.h>
24#include <hal_boot.h>
25
26#include <memcpy.h>
27#include <thread.h>
28#include <string.h>
29#include <process.h>
30#include <printk.h>
31#include <vmm.h>
32#include <core.h>
33#include <cluster.h>
34
35#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
36#define CONS_X_SIZE     160
37#define CONS_Y_SIZE     80
38
39extern intptr_t iom_base;
40size_t cons_ptr __in_kdata = 0;
41
42static void x86_putc(char c)
43{
44        if (c == '\n') {
45                cons_ptr = roundup(cons_ptr, CONS_X_SIZE);
46                return;
47        }
48
49        char *video = (char *)iom_base + (0xB8000 - IOM_BEGIN) + cons_ptr;
50        *video = c;
51        cons_ptr++, video++;
52        *video = 0x7;
53        cons_ptr++, video++;
54}
55
56static void x86_itoa(char *buf, unsigned long int n, int base)
57{
58        unsigned long int tmp;
59        int i, j;
60
61        tmp = n;
62        i = 0;
63
64        do {
65                tmp = n % base;
66                buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
67        } while (n /= base);
68        buf[i--] = 0;
69
70        for (j = 0; j < i; j++, i--) {
71                tmp = buf[j];
72                buf[j] = buf[i];
73                buf[i] = tmp;
74        }
75}
76
77void x86_printf(char *s, ...)
78{
79        va_list ap;
80
81        char buf[16];
82        int i, j, size, buflen, neg;
83
84        unsigned char c;
85        int ival;
86        unsigned int uival;
87
88        va_start(ap, s);
89
90        while ((c = *s++)) {
91                size = 0;
92                neg = 0;
93
94                if (c == 0)
95                        break;
96                else if (c == '%') {
97                        c = *s++;
98                        if (c >= '0' && c <= '9') {
99                                size = c - '0';
100                                c = *s++;
101                        }
102
103                        if (c == 'd') {
104                                ival = va_arg(ap, int);
105                                if (ival < 0) {
106                                        uival = 0 - ival;
107                                        neg++;
108                                } else
109                                        uival = ival;
110                                x86_itoa(buf, uival, 10);
111
112                                buflen = strlen(buf);
113                                if (buflen < size)
114                                        for (i = size, j = buflen; i >= 0;
115                                             i--, j--)
116                                                buf[i] =
117                                                    (j >=
118                                                     0) ? buf[j] : '0';
119
120                                if (neg)
121                                        x86_printf("-%s", buf);
122                                else
123                                        x86_printf(buf);
124                        } else if (c == 'u') {
125                                uival = va_arg(ap, int);
126                                x86_itoa(buf, uival, 10);
127
128                                buflen = strlen(buf);
129                                if (buflen < size)
130                                        for (i = size, j = buflen; i >= 0;
131                                             i--, j--)
132                                                buf[i] =
133                                                    (j >=
134                                                     0) ? buf[j] : '0';
135
136                                x86_printf(buf);
137                        } else if (c == 'x' || c == 'X') {
138                                uival = va_arg(ap, int);
139                                x86_itoa(buf, uival, 16);
140
141                                buflen = strlen(buf);
142                                if (buflen < size)
143                                        for (i = size, j = buflen; i >= 0;
144                                             i--, j--)
145                                                buf[i] =
146                                                    (j >=
147                                                     0) ? buf[j] : '0';
148
149                                x86_printf("0x%s", buf);
150                        } else if (c == 'p') {
151                                uival = va_arg(ap, int);
152                                x86_itoa(buf, uival, 16);
153                                size = 8;
154
155                                buflen = strlen(buf);
156                                if (buflen < size)
157                                        for (i = size, j = buflen; i >= 0;
158                                             i--, j--)
159                                                buf[i] =
160                                                    (j >=
161                                                     0) ? buf[j] : '0';
162
163                                x86_printf("0x%s", buf);
164                        } else if (c == 's') {
165                                x86_printf((char *) va_arg(ap, int));
166                        } 
167                } else
168                        x86_putc(c);
169        }
170
171        return;
172}
Note: See TracBrowser for help on using the repository browser.