aboutsummaryrefslogtreecommitdiff
path: root/src/output.c
blob: 0dcd20d7c80e1dfe4baead9f65401c48a5684892 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Raw screen writing code.
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include <stdarg.h> // va_list

#include "farptr.h" // GET_VAR
#include "util.h" // bprintf
#include "biosvar.h" // struct bregs

static void
screenc(u8 c)
{
    struct bregs br;
    memset(&br, 0, sizeof(br));
    br.ah = 0x0e;
    br.al = c;
    call16_int(0x10, &br);
}

// XXX
#define PORT_DEBUG  0x403

// Write a charcter to the framebuffer.
static void
putc(u16 action, char c)
{
    outb(c, PORT_DEBUG);
    if (action) {
        if (c == '\n')
            screenc('\r');
        screenc(c);
    }
}

// Write a string to the framebuffer.
static void
puts(u16 action, const char *s)
{
    for (; *s; s++)
        putc(action, *s);
}

// Write a string to the framebuffer.
static void
puts_cs(u16 action, const char *s)
{
    for (;; s++) {
        char c = GET_VAR(CS, (u8)*s);
        if (!c)
            break;
        putc(action, c);
    }
}

// Write an unsigned integer to the screen.
static void
putuint(u16 action, u32 val)
{
    char buf[12];
    char *d = &buf[sizeof(buf) - 1];
    *d-- = '\0';
    for (;;) {
        *d = (val % 10) + '0';
        val /= 10;
        if (!val)
            break;
        d--;
    }
    puts(action, d);
}

// Write a single digit hex character to the screen.
static inline void
putsinglehex(u16 action, u32 val)
{
    if (val <= 9)
        val = '0' + val;
    else
        val = 'a' + val - 10;
    putc(action, val);
}

// Write an integer in hexadecimal to the screen.
static void
puthex(u16 action, u32 val)
{
    putsinglehex(action, (val >> 28) & 0xf);
    putsinglehex(action, (val >> 24) & 0xf);
    putsinglehex(action, (val >> 20) & 0xf);
    putsinglehex(action, (val >> 16) & 0xf);
    putsinglehex(action, (val >> 12) & 0xf);
    putsinglehex(action, (val >> 8) & 0xf);
    putsinglehex(action, (val >> 4) & 0xf);
    putsinglehex(action, (val >> 0) & 0xf);
}

void
bprintf(u16 action, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    const char *s = fmt;
    for (;; s++) {
        char c = GET_VAR(CS, (u8)*s);
        if (!c)
            break;
        if (c != '%') {
            putc(action, c);
            continue;
        }
        const char *n = s+1;
        c = GET_VAR(CS, (u8)*n);
        s32 val;
        const char *sarg;
        switch (c) {
        case '%':
            putc(action, '%');
            break;
        case 'd':
            val = va_arg(args, s32);
            if (val < 0) {
                putc(action, '-');
                val = -val;
            }
            putuint(action, val);
            break;
        case 'u':
            val = va_arg(args, s32);
            putuint(action, val);
            break;
        case 'x':
            val = va_arg(args, s32);
            puthex(action, val);
            break;
        case 'c':
            val = va_arg(args, int);
            putc(action, val);
            break;
        case 's':
            sarg = va_arg(args, const char *);
            puts_cs(action, sarg);
            break;
        default:
            putc(action, '%');
            n = s;
        }
        s = n;
    }
    va_end(args);
}

static void
dump_regs(const char *fname, const char *type, struct bregs *regs)
{
    if (!regs) {
        bprintf(0, "%s %s: NULL\n", type, fname);
        return;
    }
    bprintf(0, "%s %s: a=%x b=%x c=%x d=%x si=%x di=%x\n"
            , type, fname, regs->eax, regs->ebx, regs->ecx, regs->edx
            , regs->esi, regs->edi);
    bprintf(0, "  ds=%x es=%x bp=%x sp=%x ip=%x cs=%x f=%x\n"
            , regs->ds, regs->es, regs->ebp, regs->esp
            , regs->ip, regs->cs, regs->flags);
}

void
__debug_isr(const char *fname, struct bregs *regs)
{
    puts_cs(0, fname);
    putc(0, '\n');
}

// Function called on handler startup.
void
__debug_enter(const char *fname, struct bregs *regs)
{
    // XXX - implement run time suppression test
    dump_regs(fname, "enter", regs);
}

void
__debug_exit(const char *fname, struct bregs *regs)
{
    if (! (regs->flags & F_CF))
        return;
    dump_regs(fname, "exit", regs);
}

void
__debug_stub(const char *fname, struct bregs *regs)
{
    dump_regs(fname, "stub", regs);
}