pplibc.c (8687B)
1 // PingPongOS - PingPong Operating System 2 // © Prof. Carlos A. Maziero, DINF UFPR 3 // Versão 2.1 -- 06/2026 4 5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO; 6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO. 7 8 // Implementação simplificada de algumas funções básicas da biblioteca C, 9 // que devem ser definidas aqui para nos liberar da GLibC. 10 11 #define __PPOS__ 12 13 #ifdef __PPOS__ 14 #include "hardware/cpu.h" 15 #include "hardware/serial.h" 16 #define PUTCHAR hw_putchar 17 #define IRQ_ENABLE hw_irq_enable 18 #else 19 #include <stdio.h> 20 #define PUTCHAR putchar 21 #define IRQ_ENABLE(x) 22 #endif 23 24 #include <stdbool.h> 25 #include "pplibc.h" 26 27 // macros do compilador para funções variádicas (usadas no printk) 28 #define va_list __builtin_va_list 29 #define va_start __builtin_va_start 30 #define va_end __builtin_va_end 31 #define va_arg __builtin_va_arg 32 33 // estado interno do gerador de números pseudoaleatórios 34 // https://en.wikipedia.org/wiki/Linear_congruential_generator 35 static unsigned int rand_number = 0; 36 37 //---------------------------------------------------------------------- 38 39 void randseed(unsigned long seed) 40 { 41 rand_number = seed; 42 } 43 44 //---------------------------------------------------------------------- 45 46 unsigned long randnum(void) 47 { 48 // gerador pseudo-aleatorio simples por congruência linear 49 rand_number = (1664525L * rand_number + 1013904223L) % (1L << 31); 50 return (rand_number); 51 } 52 53 //---------------------------------------------------------------------- 54 55 int abs(int num) 56 { 57 if (num < 0) 58 return (-num); 59 else 60 return (num); 61 } 62 63 //---------------------------------------------------------------------- 64 65 int mem_copy(const char *orig, char *dest, int size) 66 { 67 if (!orig || !dest || size <= 0) 68 return (ERROR); 69 70 for (int i = 0; i < size; i++) 71 dest[i] = orig[i]; 72 73 return (NOERROR); 74 } 75 76 //---------------------------------------------------------------------- 77 78 void putch(char c) 79 { 80 PUTCHAR(c); // macro 81 } 82 83 //---------------------------------------------------------------------- 84 85 void putst(const char *s) 86 { 87 if (!s) 88 s = "NULL"; 89 90 while (*s) 91 { 92 PUTCHAR(*s); // macro 93 s++; 94 } 95 } 96 97 //---------------------------------------------------------------------- 98 99 // adaptado de https://operating-system-in-1000-lines.vercel.app/en/ 100 void printk(const char *fmt, ...) 101 { 102 bool left_align; 103 //bool fill_zeros = false; 104 //bool signal_space = false; 105 short width; 106 va_list vargs; 107 108 // inibe "interrupções" do hardware 109 IRQ_ENABLE(0); 110 111 va_start(vargs, fmt); 112 113 while (*fmt) 114 { 115 // find a % mark, process it 116 if (*fmt == '%') 117 { 118 // skip '%' 119 fmt++; 120 121 // space for signal (% ) 122 /*if (*fmt == ' ') 123 { 124 signal_space = true; 125 fmt++; 126 }*/ 127 128 // left alignment (%-) 129 left_align = false; 130 if (*fmt == '-') 131 { 132 left_align = true; 133 fmt++; 134 } 135 136 // fill with zeros (%0) 137 /*if (*fmt == '0') 138 { 139 fill_zeros = true; 140 fmt++; 141 }*/ 142 143 // width modifier (%NN) 144 width = 0; 145 while (*fmt && *fmt >= '1' && *fmt <= '9') 146 { 147 width = 10 * width + (*fmt - '0'); 148 fmt++; 149 } 150 151 switch (*fmt) 152 { 153 154 // "%%" or '%' at the end of the format string 155 case '%': 156 case '\0': 157 putch('%'); 158 break; 159 160 // %c: char 161 case 'c': 162 { 163 int c = va_arg(vargs, int); 164 putch(c); 165 break; 166 } 167 168 // %s: string 169 case 's': 170 { 171 const char *s = va_arg(vargs, const char *); 172 char *aux; 173 174 // null string 175 if (!s) 176 s = "(null)"; 177 178 // decrement width according to string size 179 aux = (char *) s; 180 while (*aux) 181 { 182 width--; 183 aux++; 184 } 185 186 // print whitespaces before 187 if (!left_align) 188 while (width > 0) 189 { 190 putch(' '); 191 width--; 192 } 193 194 // print string chars 195 while (*s) 196 { 197 putch(*s); 198 s++; 199 } 200 201 // print whitespaces after 202 if (left_align) 203 while (width > 0) 204 { 205 putch(' '); 206 width--; 207 } 208 209 break; 210 } 211 212 // %d %i: signed integer, printed in decimal 213 case 'i': 214 case 'd': 215 { 216 bool negative; 217 int divisor; 218 int value = va_arg(vargs, int); 219 220 width--; 221 222 // treat negative number 223 negative = false; 224 if (value < 0) 225 { 226 value = -value; 227 negative = true; 228 width--; 229 } 230 231 // calculate number of digits to print 232 divisor = 1; 233 while (value / divisor > 9) 234 { 235 divisor *= 10; 236 width--; 237 } 238 239 // print whitespaces before 240 if (!left_align) 241 while (width > 0) 242 { 243 putch(' '); 244 width--; 245 } 246 247 // print number signal 248 if (negative) 249 putch('-'); 250 251 // print number digits 252 while (divisor > 0) 253 { 254 putch('0' + value / divisor); 255 value %= divisor; 256 divisor /= 10; 257 } 258 259 // print whitespaces after 260 if (left_align) 261 while (width > 0) 262 { 263 putch(' '); 264 width--; 265 } 266 267 break; 268 } 269 270 // %u: unsigned integer, printed in decimal 271 case 'u': 272 { 273 int divisor; 274 unsigned int value = va_arg(vargs, unsigned int); 275 276 width--; 277 278 // calculate number of digits to print 279 divisor = 1; 280 while (value / divisor > 9) 281 { 282 divisor *= 10; 283 width--; 284 } 285 286 // print whitespaces before 287 if (!left_align) 288 while (width > 0) 289 { 290 putch(' '); 291 width--; 292 } 293 294 // print number digits 295 while (divisor > 0) 296 { 297 putch('0' + value / divisor); 298 value %= divisor; 299 divisor /= 10; 300 } 301 302 // print whitespaces after 303 if (left_align) 304 while (width > 0) 305 { 306 putch(' '); 307 width--; 308 } 309 310 break; 311 } 312 313 // %p: long, printed in hexadecimal 314 case 'p': 315 { 316 unsigned nibble; 317 unsigned long value = va_arg(vargs, unsigned long); 318 319 if (value) 320 { 321 putst("0x"); 322 for (int i = 15; i >= 0; i--) 323 { 324 nibble = (value >> (i * 4)) & 0xf; 325 putch("0123456789abcdef"[nibble]); 326 } 327 } 328 else 329 putst("(nil)"); 330 331 break; 332 } 333 334 // %? unrecognized 335 default: 336 { 337 putst("%?"); 338 } 339 } 340 } 341 // not a placeholder, just print it 342 else 343 { 344 putch(*fmt); 345 } 346 347 // take the next char in format 348 if (*fmt) 349 fmt++; 350 } 351 va_end(vargs); 352 353 // libera "interrupções" do hardware 354 IRQ_ENABLE(1); 355 } 356 357 //----------------------------------------------------------------------