summaryrefslogtreecommitdiff
path: root/test/tx/cmake/riscv/bsp/printf.c
blob: 4aa185cc349e0552381a685fad2284b71103494b (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
/***************************************************************************
 * Copyright (c) 2026 10xEngineers
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/* Minimal printf/puts for RISC-V QEMU virt regression tests.
   Avoids linking newlib's stdio which is compiled with mcmodel=medlow
   and causes relocation overflow on RV64 (DRAM at 0x80000000).
   Only the format specifiers used by the test suite are supported:
   %s, %d, %u, %lu, %ld, %%, and plain strings. */

#include <stdarg.h>
#include <stddef.h>

extern int _write(int fd, const char *buf, int count);

static void print_str(const char *s)
{
    const char *p = s;
    while (*p)
        p++;
    _write(1, s, (int)(p - s));
}

static void print_unsigned(unsigned long val)
{
    char buf[21];
    int i = 0;

    if (val == 0)
    {
        _write(1, "0", 1);
        return;
    }

    while (val > 0)
    {
        buf[i++] = (char)('0' + (int)(val % 10u));
        val /= 10u;
    }

    /* Reverse. */
    for (int j = i - 1; j >= 0; j--)
        _write(1, &buf[j], 1);
}

static void print_hex(unsigned long val)
{
    static const char hex[] = "0123456789abcdef";
    char buf[17];
    int i = 0;

    if (val == 0)
    {
        _write(1, "0", 1);
        return;
    }

    while (val > 0)
    {
        buf[i++] = hex[val & 0xfu];
        val >>= 4;
    }

    for (int j = i - 1; j >= 0; j--)
        _write(1, &buf[j], 1);
}

static void print_signed(long val)
{
    if (val < 0)
    {
        _write(1, "-", 1);
        print_unsigned((unsigned long)(-val));
    }
    else
    {
        print_unsigned((unsigned long)val);
    }
}

int printf(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);

    const char *p = fmt;
    const char *seg = p;

    while (*p)
    {
        if (*p == '%')
        {
            /* Flush preceding literal segment. */
            if (p > seg)
                _write(1, seg, (int)(p - seg));

            p++;
            /* Check for 'l' length modifier. */
            int is_long = 0;
            if (*p == 'l')
            {
                is_long = 1;
                p++;
            }

            switch (*p)
            {
            case 's':
                print_str(va_arg(ap, const char *));
                break;
            case 'd':
                if (is_long)
                    print_signed(va_arg(ap, long));
                else
                    print_signed((long)va_arg(ap, int));
                break;
            case 'u':
                if (is_long)
                    print_unsigned(va_arg(ap, unsigned long));
                else
                    print_unsigned((unsigned long)va_arg(ap, unsigned int));
                break;
            case 'x':
                if (is_long)
                    print_hex(va_arg(ap, unsigned long));
                else
                    print_hex((unsigned long)va_arg(ap, unsigned int));
                break;
            case '%':
                _write(1, "%", 1);
                break;
            default:
                /* Unknown specifier — print as-is. */
                _write(1, "%", 1);
                if (is_long)
                    _write(1, "l", 1);
                _write(1, p, 1);
                break;
            }
            p++;
            seg = p;
        }
        else
        {
            p++;
        }
    }

    /* Flush remaining literal segment. */
    if (p > seg)
        _write(1, seg, (int)(p - seg));

    va_end(ap);
    return 0;
}

int puts(const char *s)
{
    print_str(s);
    _write(1, "\n", 1);
    return 0;
}