123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
-
-
-
- #include <memory.h>
- #include <stdarg.h>
- #include <math.h>
-
- void dprint(const char * sz)
- {
- __asm__ __volatile__(
- "mov r2, %0\n"
- "mov r0, #0xC0\n"
- "lsl r0, #0x8\n"
- "mov r1, #0xDE\n"
- "orr r0, r1\n"
- "lsl r0, #0x8\n"
- "mov r1, #0xD0\n"
- "orr r0, r1\n"
- "lsl r0, #8\n"
- "mov r1, #0x0D\n"
- "orr r0, r1\n"
- "mov r1, #0\n"
- "and r0, r0, r0\n":
- :
- "r" (sz):
- "r0", "r1", "r2");
- }
-
- u32 mini_strlen(const char * s) {
- u32 len = 0;
- while (s[len] != '\0') len++;
- return len;
- }
-
- u32 mini_itoa(int value, u32 radix, u32 uppercase, u32 unsig, char * buffer, u32 zero_pad) {
- char * pbuffer = buffer;
- s32 negative = 0;
- u32 i, len;
-
- if (radix > 16)
- return 0;
- if (value < 0 && !unsig) {
- negative = 1;
- value = -value;
- }
-
- if (radix == 16) {
- do {
- u32 digit = value & 0xF;
- * (pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
- value >>= 4;
- } while (value > 0);
- } else {
- do {
- u32 digit = value % radix;
- * (pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
- value /= radix;
- } while (value > 0);
- }
- for (i = (pbuffer - buffer); i < zero_pad; i++)
- * (pbuffer++) = '0';
- if (negative)
- {
- * (pbuffer++) = '-'; * (pbuffer) = '\0';
- }
-
-
- len = (pbuffer - buffer);
- for (i = 0; i < len / 2; i++) {
- char j = buffer[i];
- buffer[i] = buffer[len - i - 1];
- buffer[len - i - 1] = j;
- }
- return len;
- }
-
- int mini_vsnprintf(char * buffer, u32 buffer_len,
- const char * fmt, va_list va)
- {
- char * pbuffer = buffer;
- char bf[24];
- char ch;
- int _putc(char ch)
- {
- if ((u32)((pbuffer - buffer) + 1) >= buffer_len)
- return 0;
- * (pbuffer++) = ch;
- * (pbuffer) = '\0';
- return 1;
- }
- int _puts(char * s, u32 len)
- {
- u32 i;
- if (buffer_len - (pbuffer - buffer) - 1 < len)
- len = buffer_len - (pbuffer - buffer) - 1;
-
- for (i = 0; i < len; i++)
- * (pbuffer++) = s[i];
- * (pbuffer) = '\0';
- return len;
- }
- while ((ch = * (fmt++))) {
- if ((u32)((pbuffer - buffer) + 1) >= buffer_len)
- break;
- if (ch != '%')
- _putc(ch);
- else {
- char zero_pad = 0;
- char * ptr;
- u32 len;
- ch = * (fmt++);
-
- if (ch == '0') {
- ch = * (fmt++);
- if (ch == '\0')
- return pbuffer - buffer;
- if (ch >= '0' && ch <= '9')
- zero_pad = ch - '0';
- ch = * (fmt++);
- }
- switch (ch) {
- case 0:
- return pbuffer - buffer;
- case 'u':
- case 'd':
- len = mini_itoa(va_arg(va, u32), 10, 0, (ch == 'u'), bf, zero_pad);
- _puts(bf, len);
- break;
- case 'x':
- case 'X':
- len = mini_itoa(va_arg(va, u32), 16, (ch == 'X'), 1, bf, zero_pad);
- _puts(bf, len);
- break;
- case 'c':
- _putc((char)(va_arg(va, int)));
- break;
- case 's':
- ptr = va_arg(va, char * );
- _puts(ptr, mini_strlen(ptr));
- break;
- default:
- _putc(ch);
- break;
- }
- }
- }
- return pbuffer - buffer;
- }
-
- void dprintf(const char * str, ...)
- {
- char __outstr[256];
- va_list args;
- va_start(args, str);
- mini_vsnprintf(__outstr, 256, str, args);
- va_end(args);
- dprint(__outstr);
- }
|