-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec.c
More file actions
317 lines (294 loc) · 10.3 KB
/
Copy pathcodec.c
File metadata and controls
317 lines (294 loc) · 10.3 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2026 The libuci Authors
* SPDX-License-Identifier: Apache-2.0
* https://github.com/c64uploader/libuci
*
* codec.c - Character encoding conversions for C64.
*
* Bridges the gap between the compiler's native encoding
* (PETSCII under cc65, ASCII under oscar64) and the encoding
* needed for network protocols, screen output, or firmware.
*
* See include/uci_codec.h for the public API.
*/
#include "uci_codec.h"
/* cc65 does not support __noinline; define it away. */
#if defined(__CC65__)
#define __noinline
#endif
/* Single-character conversions */
char uci_c_a(char c) {
unsigned char uc = (unsigned char)c;
#if defined(__CC65__)
/* cc65: native is PETSCII - reverse the inversion. */
if (uc >= UCI_UC_MIN && uc <= UCI_UC_MAX) return (char)(uc - 0x80);
if (uc >= UCI_LC_MIN && uc <= UCI_LC_MAX) return (char)(uc + 0x20);
#endif
/* oscar64: native is ASCII - no conversion needed. */
(void)uc;
return c;
}
char uci_c_p(char c) {
unsigned char uc = (unsigned char)c;
#if defined(__CC65__)
/* cc65: strings are PETSCII, so ASCII must be explicitly marked.
* Convert ASCII letters to PETSCII alt range (0xC1-0xDA) for
* display via printf/BSOUT in the Lowercase/Uppercase charset. */
if (uc >= 0x41 && uc <= 0x5A) return (char)(uc + 0x80);
if (uc >= 0x61 && uc <= 0x7A) return (char)(uc - 0x20);
#endif
/* oscar64: strings are ASCII, no conversion needed for network ASCII. */
return c;
}
char uci_c_s(char c) {
unsigned char uc = (unsigned char)c;
/* Native -> screen code in the Lowercase/Uppercase character set. */
#if defined(__CC65__)
if (uc >= 0xC1 && uc <= 0xDA) return (char)(uc - 0xA0); /* PETSCII uppercase */
if (uc >= 0x41 && uc <= 0x5A) return (char)(uc - 0x40); /* PETSCII lowercase */
#else
if (uc >= 0x41 && uc <= 0x5A) return (char)(uc - 0x20); /* ASCII uppercase */
if (uc >= 0x61 && uc <= 0x7A) return (char)(uc - 0x60); /* ASCII lowercase */
#endif
return c;
}
char uci_c_su(char c) {
unsigned char uc = (unsigned char)c;
/* Native -> screen code in the Uppercase/Graphics character set.
* In this charset, only uppercase A-Z are available as letters
* (screen codes 0x01-0x1A). Both uppercase and lowercase source
* letters map to uppercase display glyphs. */
#if defined(__CC65__)
if (uc >= 0xC1 && uc <= 0xDA) return (char)(uc - 0xC0); /* PETSCII uppercase */
if (uc >= 0x41 && uc <= 0x5A) return (char)(uc - 0x40); /* PETSCII lowercase -> uppercase screen */
#else
if (uc >= 0x41 && uc <= 0x5A) return (char)(uc - 0x40); /* ASCII uppercase */
if (uc >= 0x61 && uc <= 0x7A) return (char)(uc - 0x60); /* ASCII lowercase -> uppercase screen */
#endif
return c;
}
char uci_c_au(char c) {
unsigned char uc = (unsigned char)c;
#if defined(__CC65__)
/* cc65: PETSCII uppercase (0xC1-0xDA) -> ASCII uppercase (0x41-0x5A).
* PETSCII lowercase (0x41-0x5A) is already in ASCII uppercase range. */
if (uc >= UCI_UC_MIN && uc <= UCI_UC_MAX) return (char)(uc - 0x80);
#else
/* oscar64: ASCII lowercase (0x61-0x7A) -> ASCII uppercase (0x41-0x5A).
* ASCII uppercase is already correct. */
if (uc >= UCI_LC_MIN && uc <= UCI_LC_MAX) uc -= 0x20;
#endif
return (char)uc;
}
char uci_c_d(char c) {
unsigned char uc = (unsigned char)c;
#if defined(__CC65__)
/* cc65: device data is standard PETSCII - convert to alt PETSCII
* for printf display in the Lowercase/Uppercase charset. */
if (uc >= 0x61 && uc <= 0x7A) return (char)(uc + 0x60); /* standard upper -> alt upper */
else if (uc >= 0x41 && uc <= 0x5A) return (char)(uc + 0x80); /* standard lower -> alt upper */
else if (uc >= 0xC1 && uc <= 0xDA) return (char)(uc); /* alt upper - unchanged */
#else
/* oscar64: device data is standard PETSCII - convert to ASCII,
* which displays as readable letters via BSOUT in the
* hardware-default Uppercase/Graphics charset. */
if (uc >= 0x61 && uc <= 0x7A) return (char)(uc - 0x20); /* standard upper -> ASCII upper */
if (uc >= 0x41 && uc <= 0x5A) return (char)(uc); /* standard lower -> ASCII upper (same byte!) */
if (uc >= 0xC1 && uc <= 0xDA) return (char)(uc - 0x80); /* alt upper -> ASCII upper */
#endif
return c;
}
/* Buffer conversions: NUL-terminated src */
__noinline char *uci_s_a(char *dst, const char *src) {
char *out = dst;
while (*src) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0xC1 && c <= 0xDA) c -= 0x80;
else if (c >= 0x41 && c <= 0x5A) c += 0x20;
#endif
*dst++ = (char)c;
}
*dst = '\0';
return out;
}
__noinline char *uci_s_au(char *dst, const char *src) {
char *out = dst;
while (*src) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
/* cc65: PETSCII uppercase (0xC1-0xDA) -> ASCII uppercase (0x41-0x5A).
* PETSCII lowercase (0x41-0x5A) is already in ASCII uppercase range. */
if (c >= UCI_UC_MIN && c <= UCI_UC_MAX) c -= 0x80;
#else
/* oscar64: ASCII lowercase (0x61-0x7A) -> ASCII uppercase (0x41-0x5A).
* ASCII uppercase is already correct. */
if (c >= UCI_LC_MIN && c <= UCI_LC_MAX) c -= 0x20;
#endif
*dst++ = (char)c;
}
*dst = '\0';
return out;
}
__noinline char *uci_s_p(char *dst, const char *src) {
char *out = dst;
while (*src) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0x41 && c <= 0x5A) c += 0x80;
else if (c >= 0x61 && c <= 0x7A) c -= 0x20;
#else
/* oscar64: native is ASCII - straight copy */
#endif
*dst++ = (char)c;
}
*dst = '\0';
return out;
}
__noinline char *uci_s_s(char *dst, const char *src) {
char *out = dst;
while (*src) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0xC1 && c <= 0xDA) c -= 0xA0;
else if (c >= 0x41 && c <= 0x5A) c -= 0x40;
#else
if (c >= 0x41 && c <= 0x5A) c -= 0x20;
else if (c >= 0x61 && c <= 0x7A) c -= 0x60;
#endif
*dst++ = (char)c;
}
*dst = '\0';
return out;
}
__noinline char *uci_s_su(char *dst, const char *src) {
char *out = dst;
while (*src) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0xC1 && c <= 0xDA) c -= 0xC0;
else if (c >= 0x41 && c <= 0x5A) c -= 0x40;
#else
if (c >= 0x41 && c <= 0x5A) c -= 0x40;
else if (c >= 0x61 && c <= 0x7A) c -= 0x60;
#endif
*dst++ = (char)c;
}
*dst = '\0';
return out;
}
__noinline char *uci_s_d(char *dst, const char *src) {
char *out = dst;
while (*src) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
/* cc65: device data is standard PETSCII - convert to alt PETSCII
* for printf display in the Lowercase/Uppercase charset. */
if (c >= 0x61 && c <= 0x7A) c += 0x60; /* standard upper -> alt upper */
else if (c >= 0x41 && c <= 0x5A) c += 0x80; /* standard lower -> alt upper */
else if (c >= 0xC1 && c <= 0xDA) ; /* alt upper - unchanged */
#else
/* oscar64: device data is standard PETSCII - convert to ASCII,
* which displays as readable letters via BSOUT in the
* hardware-default Uppercase/Graphics charset. */
if (c >= 0x61 && c <= 0x7A) c -= 0x20; /* standard upper -> ASCII upper */
else if (c >= 0xC1 && c <= 0xDA) c -= 0x80; /* alt upper -> ASCII upper */
/* 0x41-0x5A: standard lower -> ASCII upper (same byte - unchanged) */
#endif
*dst++ = (char)c;
}
*dst = '\0';
return out;
}
/* Length-bounded conversions (n bytes, no NUL) */
__noinline char *uci_m_a(char *dst, const char *src, uint16_t n) {
char *out = dst;
uint16_t i;
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0xC1 && c <= 0xDA) c -= 0x80;
else if (c >= 0x41 && c <= 0x5A) c += 0x20;
#endif
*dst++ = (char)c;
}
return out;
}
__noinline char *uci_m_au(char *dst, const char *src, uint16_t n) {
char *out = dst;
uint16_t i;
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= UCI_UC_MIN && c <= UCI_UC_MAX) c -= 0x80;
#else
if (c >= UCI_LC_MIN && c <= UCI_LC_MAX) c -= 0x20;
#endif
*dst++ = (char)c;
}
return out;
}
__noinline char *uci_m_p(char *dst, const char *src, uint16_t n) {
char *out = dst;
uint16_t i;
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0x41 && c <= 0x5A) c += 0x80;
else if (c >= 0x61 && c <= 0x7A) c -= 0x20;
#else
if (c >= 0x61 && c <= 0x7A) c -= 0x20;
#endif
*dst++ = (char)c;
}
return out;
}
__noinline char *uci_m_s(char *dst, const char *src, uint16_t n) {
char *out = dst;
uint16_t i;
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0xC1 && c <= 0xDA) c -= 0xA0;
else if (c >= 0x41 && c <= 0x5A) c -= 0x40;
#else
if (c >= 0x41 && c <= 0x5A) c -= 0x20;
else if (c >= 0x61 && c <= 0x7A) c -= 0x60;
#endif
*dst++ = (char)c;
}
return out;
}
__noinline char *uci_m_su(char *dst, const char *src, uint16_t n) {
char *out = dst;
uint16_t i;
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0xC1 && c <= 0xDA) c -= 0xC0;
else if (c >= 0x41 && c <= 0x5A) c -= 0x40;
#else
if (c >= 0x41 && c <= 0x5A) c -= 0x40;
else if (c >= 0x61 && c <= 0x7A) c -= 0x60;
#endif
*dst++ = (char)c;
}
return out;
}
__noinline char *uci_m_d(char *dst, const char *src, uint16_t n) {
char *out = dst;
uint16_t i;
for (i = 0; i < n; i++) {
unsigned char c = (unsigned char)*src++;
#if defined(__CC65__)
if (c >= 0x61 && c <= 0x7A) c += 0x60;
else if (c >= 0x41 && c <= 0x5A) c += 0x80;
else if (c >= 0xC1 && c <= 0xDA) ;
#else
if (c >= 0x61 && c <= 0x7A) c -= 0x20;
else if (c >= 0xC1 && c <= 0xDA) c -= 0x80;
#endif
*dst++ = (char)c;
}
return out;
}