OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [software/] [c64libc/] [source/] [stdio.c] - Blame information for rev 41

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 robfinch
//#include "c:\AtlysCores\RTF65000\trunk\software\stdio.h"
2
extern pascal int prtdbl(double,int,int,char E);
3
 
4
typedef union tagval {
5
    __int64 i;
6
    double d;
7
} uval;
8
 
9
pascal void putch(char ch)
10
{
11
        asm {
12
                addui   sp,sp,#-8
13
                sw              r6,[sp]
14
                lw              r1,32[bp]
15
                ldi     r6,#14    ; Teletype output function
16
        sys     #10       ; Video BIOS call
17
        lw              r6,[sp]
18
                addui   sp,sp,#8
19
        }
20
}
21
 
22
pascal void putnum(int num, int wid, char sep, char padchar)
23
{
24
        int n, m10;
25
        char sign;
26
        static char numwka[200];
27
 
28
        asm {
29
            ldi   r1,#80
30
            sc    r1,hs:LEDS
31
        }
32
        if (wid < 0 or wid > 200)        // take care of nutty parameter
33
                wid = 0;
34
        sign = num < 0 ? '-' : '+';
35
        if (num < 0) num = -num;
36
        n = 0;
37
        do {
38
                if ((n % 4)==3 && sep) {
39
                        numwka[n]=sep;
40
                        n++;
41
                }
42
                m10 = num % 10;
43
                numwka[n] = m10 + '0';
44
                num = num / 10;
45
                n++;
46
        } until (num == 0 or n > 18);
47
        if (sign=='-') {
48
                numwka[n] = sign;
49
                n++;
50
        }
51
        asm {
52
            ldi   r1,#88
53
            sc    r1,hs:LEDS
54
        }
55
        for (; n < wid; wid--)
56
                putch(padchar);
57
        asm {
58
            ldi   r1,#89
59
            sc    r1,hs:LEDS
60
        }
61
        while (n > 0) {
62
                --n;
63
                putch(numwka[n]);
64
        }
65
        asm {
66
            ldi   r1,#90
67
            sc    r1,hs:LEDS
68
        }
69
}
70
 
71
pascal void puthexnum(int num, int wid, int ul, char padchar)
72
{
73
        int n, m;
74
        char sign;
75
        char numwka[200];
76
 
77
        if (wid < 0 or wid > 200)        // take care of nutty parameter
78
                wid = 0;
79
        n = 0;
80
        sign = num < 0 ? '-' : '+';
81
        if (num < 0) num = -num;
82
        do {
83
                m = num & 15;
84
                if (m < 10)
85
                        numwka[n] = m + '0';
86
                else if (ul)
87
                        numwka[n] = m + 'A'-10;
88
                else
89
                        numwka[n] = m + 'a'-10;
90
                num = num >> 4;
91
                n++;
92
        }
93
        while (num != 0 && n < 18);
94
        if (sign=='-') {
95
                numwka[n] = sign;
96
                n++;
97
        }
98
        while (n < wid) {
99
                putch(sign=='-' ? ' ' : padchar);
100
                wid--;
101
        }
102
        while (n > 0) {
103
                --n;
104
                putch(numwka[n]);
105
        }
106
}
107
 
108
pascal int putstr(char *p, int maxchars)
109
{
110
        char *q;
111
 
112
        for (q = p; *p && maxchars > 0; p++, maxchars--)
113
                putch(*p);
114
        return p-q;
115
}
116
 
117
pascal void putstr2(char *p)
118
{
119
    asm {
120
                addui   sp,sp,#-8
121
                sw              r6,[sp]
122
        lw      r1,24[bp]
123
        ldi     r6,#$1B   ; Video BIOS DisplayString16 function
124
        sys     #10
125
                lw              r6,[sp]
126
                addui   sp,sp,#8
127
    }
128
}
129
 
130
int getcharNoWait()
131
{
132
    return KeybdGetBufferedCharNoWait();
133
}
134
 
135
/*
136
naked int getcharNoWait()
137
{
138
        asm {
139
        push    lr
140
        bsr     KeybdGetBufferedCharNoWait_
141
        pop     lr
142
        rtl
143
        push    r6
144
        ld      r6,#3    ; KeybdGetCharNoWait
145
        sys     #10
146
        pop     r6
147
        rtl
148
        }
149
}
150
*/
151
 
152
int (getchar)()
153
{
154
        int ch;
155
 
156
        do {
157
                ch = getcharNoWait();
158
        }
159
        while (ch==-1);
160
        return ch & 255;
161
}
162
 
163
 
164
int printf(char *p, ...)
165
{
166
        int *q;
167
        int fmtwidth;
168
        int maxwidth;
169
        int wd;
170
        uval v;
171
        char padchar;
172
        q = &p;
173
 
174
        for (; *p; p++) {
175
        padchar = ' ';
176
                if (*p=='%') {
177
                        fmtwidth = 0;
178
                        maxwidth = 65535;
179
                        p++;
180
j1:
181
                        switch(*p) {
182
                        case '%':
183
                                putch('%');
184
                                break;
185
                        case 'c':
186
                                q++;
187
                                putch(*q);
188
                                break;
189
                        case 'd':
190
                                q++;
191
                                putnum(*q,fmtwidth,0,padchar);
192
                                break;
193
                        case 'e':
194
            case 'E':
195
                                q++;
196
                                v.i = *q;
197
                                prtdbl(v.d,fmtwidth,maxwidth,*p);
198
                                break;
199
                        case 'x':
200
                                q++;
201
                                puthexnum(*q,fmtwidth,0,padchar);
202
                                break;
203
                        case 'X':
204
                                q++;
205
                                puthexnum(*q,fmtwidth,1,padchar);
206
                                break;
207
                        case 's':
208
                                q++;
209
                                wd = putstr(*q,maxwidth);
210
                                //while (wd < fmtwidth) {
211
                                //      putch(' ');
212
                                //      wd++;
213
                                //}
214
                                break;
215
                        // width specification
216
                        case '0':
217
                padchar = '0';
218
            case '1','2','3','4','5','6','7','8','9':
219
                                fmtwidth = *p - '0';
220
                                p++;
221
                                while (isdigit(*p)) {
222
                                        fmtwidth *= 10;
223
                                        fmtwidth += *p - '0';
224
                                        p++;
225
                                }
226
                                if (*p != '.')
227
                                        goto j1;
228
                        case '.':
229
                                p++;
230
                                if (!isdigit(*p))
231
                                        goto j1;
232
                                maxwidth = *p - '0';
233
                                p++;
234
                                while (isdigit(*p)) {
235
                                        maxwidth *= 10;
236
                                        maxwidth += *p - '0';
237
                                        p++;
238
                                }
239
                                goto j1;
240
                        }
241
                }
242
                else
243
                        putch(*p);
244
        }
245
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.