source: trunk/hal/x86_64/core/x86_printf.c @ 84

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

scroll the console

File size: 5.2 KB
RevLine 
[29]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>
[39]25#include <hal_internal.h>
[29]26
27#include <memcpy.h>
28#include <thread.h>
29#include <string.h>
30#include <process.h>
31#include <printk.h>
32#include <vmm.h>
33#include <core.h>
34#include <cluster.h>
35
36#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
[84]37#define CONS_X_SIZE     80
38#define CONS_Y_SIZE     26
[29]39
[84]40static char cons_buffer[CONS_X_SIZE * 2 * CONS_Y_SIZE] __in_kdata;
[29]41extern intptr_t iom_base;
42size_t cons_ptr __in_kdata = 0;
43
[39]44void x86_panic(char *msg)
45{
46        x86_printf("!!!!! PANIC !!!!!\n");
47        x86_printf("-> %s\n", msg);
48        x86_printf("!!!!!!!!!!!!!!!!!\n");
49        while (1);
50}
51
[84]52static void check_scroll()
53{
54        char *base = (uint64_t)iom_base + (0xB8000 - IOM_BEGIN);
55        char *src, *dst;
56        size_t i;
57
58        if (cons_ptr < (CONS_X_SIZE * 2) * CONS_Y_SIZE) {
59                return;
60        }
61
62        for (i = 0; i < CONS_Y_SIZE - 1; i++) {
63                dst = (char *)&cons_buffer[0] + i * (CONS_X_SIZE * 2);
64                src = (char *)&cons_buffer[0] + (i + 1) * (CONS_X_SIZE * 2);
65
66                memcpy(dst, src, (CONS_X_SIZE * 2));
67        }
68
69        memset(base, 0, CONS_X_SIZE * 2 * CONS_Y_SIZE);
70
71        cons_ptr -= (CONS_X_SIZE * 2);
72        memcpy(base, &cons_buffer[0], (CONS_X_SIZE * 2) * (CONS_Y_SIZE - 1));
73}
74
[29]75static void x86_putc(char c)
76{
77        if (c == '\n') {
[84]78                cons_ptr = roundup(cons_ptr, CONS_X_SIZE * 2);
79                check_scroll();
[29]80                return;
81        }
82
83        char *video = (char *)iom_base + (0xB8000 - IOM_BEGIN) + cons_ptr;
[84]84        char *buf = &cons_buffer[cons_ptr];
[29]85        *video = c;
[84]86        *buf = c;
87        cons_ptr++, video++, buf++;
[29]88        *video = 0x7;
[84]89        *buf = 0x7;
90        cons_ptr++, video++, buf++;
91
92        check_scroll();
[29]93}
94
95static void x86_itoa(char *buf, unsigned long int n, int base)
96{
97        unsigned long int tmp;
98        int i, j;
99
100        tmp = n;
101        i = 0;
102
103        do {
104                tmp = n % base;
105                buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
106        } while (n /= base);
107        buf[i--] = 0;
108
109        for (j = 0; j < i; j++, i--) {
110                tmp = buf[j];
111                buf[j] = buf[i];
112                buf[i] = tmp;
113        }
114}
115
[32]116static void x86_ztoa(char *buf, uint64_t n, uint64_t base)
117{
118        uint64_t tmp;
119        int i, j;
120
121        tmp = n;
122        i = 0;
123
124        do {
125                tmp = n % base;
126                buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
127        } while (n /= base);
128        buf[i--] = 0;
129
130        for (j = 0; j < i; j++, i--) {
131                tmp = buf[j];
132                buf[j] = buf[i];
133                buf[i] = tmp;
134        }
135}
136
[29]137void x86_printf(char *s, ...)
138{
139        va_list ap;
140
141        char buf[16];
142        int i, j, size, buflen, neg;
143
144        unsigned char c;
145        int ival;
146        unsigned int uival;
[32]147        uint64_t zval;
[29]148
149        va_start(ap, s);
150
151        while ((c = *s++)) {
152                size = 0;
153                neg = 0;
154
155                if (c == 0)
156                        break;
157                else if (c == '%') {
158                        c = *s++;
159                        if (c >= '0' && c <= '9') {
160                                size = c - '0';
161                                c = *s++;
162                        }
163
[32]164                        if (c == 'z') {
165                                zval = va_arg(ap, uint64_t);
166                                x86_ztoa(buf, zval, 10);
167
168                                buflen = strlen(buf);
169                                if (buflen < size)
170                                        for (i = size, j = buflen; i >= 0; i--, j--)
171                                                buf[i] = (j >= 0) ? buf[j] : '0';
172
173                                x86_printf(buf);
174                        } else if (c == 'Z') {
175                                zval = va_arg(ap, uint64_t);
176                                x86_ztoa(buf, zval, 16);
177
178                                buflen = strlen(buf);
179                                if (buflen < size)
180                                        for (i = size, j = buflen; i >= 0; i--, j--)
181                                                buf[i] = (j >= 0) ? buf[j] : '0';
182
183                                x86_printf("0x%s", buf);
184                        } else if (c == 'd') {
[29]185                                ival = va_arg(ap, int);
186                                if (ival < 0) {
187                                        uival = 0 - ival;
188                                        neg++;
189                                } else
190                                        uival = ival;
191                                x86_itoa(buf, uival, 10);
192
193                                buflen = strlen(buf);
194                                if (buflen < size)
195                                        for (i = size, j = buflen; i >= 0;
196                                             i--, j--)
197                                                buf[i] =
198                                                    (j >=
199                                                     0) ? buf[j] : '0';
200
201                                if (neg)
202                                        x86_printf("-%s", buf);
203                                else
204                                        x86_printf(buf);
205                        } else if (c == 'u') {
206                                uival = va_arg(ap, int);
207                                x86_itoa(buf, uival, 10);
208
209                                buflen = strlen(buf);
210                                if (buflen < size)
211                                        for (i = size, j = buflen; i >= 0;
212                                             i--, j--)
213                                                buf[i] =
214                                                    (j >=
215                                                     0) ? buf[j] : '0';
216
217                                x86_printf(buf);
218                        } else if (c == 'x' || c == 'X') {
219                                uival = va_arg(ap, int);
220                                x86_itoa(buf, uival, 16);
221
222                                buflen = strlen(buf);
223                                if (buflen < size)
224                                        for (i = size, j = buflen; i >= 0;
225                                             i--, j--)
226                                                buf[i] =
227                                                    (j >=
228                                                     0) ? buf[j] : '0';
229
230                                x86_printf("0x%s", buf);
231                        } else if (c == 'p') {
232                                uival = va_arg(ap, int);
233                                x86_itoa(buf, uival, 16);
234                                size = 8;
235
236                                buflen = strlen(buf);
237                                if (buflen < size)
238                                        for (i = size, j = buflen; i >= 0;
239                                             i--, j--)
240                                                buf[i] =
241                                                    (j >=
242                                                     0) ? buf[j] : '0';
243
244                                x86_printf("0x%s", buf);
245                        } else if (c == 's') {
[47]246                                x86_printf((char *) va_arg(ap, uint64_t));
[29]247                        } 
248                } else
249                        x86_putc(c);
250        }
251
252        return;
253}
Note: See TracBrowser for help on using the repository browser.