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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [core/] [sim/] [rtl_sim/] [src-c/] [dhrystone_v2.1/] [mylib/] [cprintf.c] - Blame information for rev 211

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 211 olivier.gi
#include <stdarg.h>
2
 
3
#include "cprintf.h"
4
 
5
typedef unsigned char byte;
6
 
7
static char hex[] = "0123456789abcdef";
8
 
9
void
10
cput_nibble (int n)
11
{
12
  tty_putc (hex[n&0x0f]);
13
}
14
 
15
void
16
cput_hex_byte (int n)
17
{
18
  cput_nibble (n >> 4);
19
  cput_nibble (n);
20
}
21
 
22
void
23
cput_binary_byte (int n)
24
{
25
  int i;
26
  for (i=7; i>=0; i--)
27
    tty_putc((n & (1<<i)) ? '1' : '0');
28
}
29
 
30
void
31
cput_hex_word (int n)
32
{
33
  cput_hex_byte (n >> 8);
34
  cput_hex_byte (n);
35
}
36
 
37
void
38
cput_hex_long (long int n)
39
{
40
  cput_hex_byte (n >> 24);
41
  cput_hex_byte (n >> 16);
42
  cput_hex_byte (n >> 8);
43
  cput_hex_byte (n);
44
}
45
 
46
void
47
cput_hex_block (char *block, int n)
48
{
49
  int i = 0;
50
  while (n)
51
    {
52
      cput_hex_byte (*block++);
53
      if (--n == 0)
54
        break;
55
      i++;
56
      if ((i & 7) == 0)
57
        tty_putc (' ');
58
      else
59
        tty_putc (':');
60
    }
61
}
62
 
63
void
64
cput_nibble_block (char *block, int n)
65
{
66
  int i = 0;
67
  while (n)
68
    {
69
      cput_nibble (*block);
70
      if (--n == 0)
71
        break;
72
      i++;
73
      if ((i & 7) == 0)
74
        tty_putc (' ');
75
    }
76
}
77
 
78
void
79
cput_number (int n)
80
{
81
  char buf[20];
82
  int i = 0;
83
  if (n < 0)
84
    {
85
      tty_putc ('-');
86
      n = -n;
87
    }
88
  while (n > 9)
89
    {
90
      buf[i++] = (n%10) + '0';
91
      n /= 10;
92
    }
93
  buf[i++] = (n%10) + '0';
94
  while (i > 0)
95
    tty_putc (buf[--i]);
96
}
97
 
98
void
99
cprintf (const char *fmt, ...)
100
{
101
  va_list v;
102
  int i;
103
  char *s;
104
 
105
  va_start (v, fmt);
106
 
107
  while (*fmt)
108
    {
109
      if (*fmt != '%')
110
        tty_putc (*fmt);
111
      else
112
        switch (*++fmt)
113
          {
114
          case '%':
115
            tty_putc ('%');
116
            break;
117
          case 'c':
118
            i = va_arg (v, int);
119
            tty_putc(i);
120
            break;
121
          case 'd':
122
            i = va_arg (v, int);
123
            cput_number(i);
124
            break;
125
          case 'b':
126
            i = va_arg (v, int);
127
            cput_hex_byte (i);
128
            break;
129
          case 'B':
130
            i = va_arg (v, int);
131
            cput_binary_byte (i);
132
            break;
133
          case 'w':
134
            i = va_arg (v, int);
135
            cput_hex_word (i);
136
            break;
137
          case 'l':
138
            i = va_arg (v, int);
139
            cput_hex_long (i);
140
            break;
141
          case 'x':
142
            s = va_arg (v, char *);
143
            i = va_arg (v, int);
144
            cput_hex_block (s, i);
145
            break;
146
          case 'n':
147
            s = va_arg (v, char *);
148
            i = va_arg (v, int);
149
            cput_nibble_block (s, i);
150
            break;
151
          case 's':
152
            s = va_arg (v, char *);
153
            tty_putc (s);
154
            break;
155
          }
156
      fmt ++;
157
    }
158
}

powered by: WebSVN 2.1.0

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