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

Subversion Repositories c16

[/] [c16/] [trunk/] [polled.c] - Blame information for rev 29

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

Line No. Rev Author Line
1 6 jsauermann
/*******************************************************************************
2
********************************************************************************
3
**                                                                            **
4
**                     UTILITY FUNCTIONS                                      **
5
**                                                                            **
6
********************************************************************************
7
*******************************************************************************/
8
 
9
void interrupt() {}
10
 
11
int strlen(const char * buffer)
12
{
13
const char * from = buffer;
14
 
15
    while (*buffer)   buffer++;
16
 
17
   return buffer - from;
18
}
19
/*******************************************************************************
20
********************************************************************************
21
**                                                                            **
22
**                     SERIAL OUTPUT                                          **
23
**                                                                            **
24
********************************************************************************
25
*******************************************************************************/
26
 
27
int putchr(char c)
28
{
29
   while (ASM(" IN (IN_STATUS), RU") & 0x02)   /* wait */ ;
30
   ASM(" MOVE 2(SP), RU");
31
   ASM(" OUT  R, (OUT_TX_DATA)");
32
}
33
//-----------------------------------------------------------------------------
34
void print_string(const char * buffer)
35
{
36
    while (*buffer)   putchr(*buffer++);
37
}
38
//-----------------------------------------------------------------------------
39
void print_hex(char * dest, unsigned int value, const char * hex)
40
{
41
   if (value >= 0x1000)   *dest++ = hex[(value >> 12) & 0x0F];
42
   if (value >=  0x100)   *dest++ = hex[(value >>  8) & 0x0F];
43
   if (value >=   0x10)   *dest++ = hex[(value >>  4) & 0x0F];
44
   *dest++ = hex[value  & 0x0F];
45
   *dest = 0;
46
}
47
//-----------------------------------------------------------------------------
48
void print_unsigned(char * dest, unsigned int value)
49
{
50 13 jsauermann
   if (value >= 10000)    *dest++ = '0' + (value / 10000);
51
   if (value >=  1000)    *dest++ = '0' + (value /  1000) % 10;
52
   if (value >=   100)    *dest++ = '0' + (value /   100) % 10;
53
   if (value >=    10)    *dest++ = '0' + (value /    10) % 10;
54
   *dest++ = '0' + value % 10;
55 6 jsauermann
   *dest = 0;
56
}
57
//-----------------------------------------------------------------------------
58
int print_item(const char * buffer, char flags, char sign, char pad,
59
               const char * alt, int field_w, int min_w, char min_p)
60
{
61
   // [fill] [sign] [alt] [pad] [buffer] [fill]
62
   //        ----------- len ----------- 
63
int filllen = 0;
64
int signlen = 0;
65
int altlen  = 0;
66
int padlen  = 0;
67
int buflen  = strlen(buffer);
68
int len;
69
int i;
70
 
71
   if (min_w > buflen)          padlen = min_w - buflen;
72
   if (sign)                    signlen = 1;
73
   if (alt && (flags & 0x01))   altlen = strlen(alt);
74
 
75
   len = signlen + altlen + padlen + buflen;
76
 
77
   if (0x02 & ~flags)   // right align
78
      {
79
        for (i = len; i < field_w; i++)   putchr(pad);
80
      }
81
 
82
   if (sign)   putchr(sign);
83
   if (alt)
84
      {
85
        if (flags & 0x01)   print_string(alt);
86
      }
87
 
88
   for (i = 0; i < padlen; i++)   putchr(min_p);
89
   print_string(buffer);
90
 
91
   if (0x02 & flags)   // left align
92
      {
93
        for (i = len; i < field_w; i++)   putchr(pad);
94
      }
95
 
96
   return len;
97
}
98
//-----------------------------------------------------------------------------
99
int printf(const char * format, ...)
100
{
101
const char **  args = 1 + &format;
102
int            len = 0;
103
char           c;
104
char           flags;
105
char           sign;
106
char           pad;
107
const char *   alt;
108
int            field_w;
109
int            min_w;
110
unsigned int * which_w;
111
char           buffer[12];
112
 
113
   while (c = *format++)
114
       {
115
         if (c != '%')   { len +=putchr(c);   continue; }
116
 
117
         flags   = 0;
118
         sign    = 0;
119
         pad     = ' ';
120
         field_w = 0;
121
         min_w   = 0;
122
         which_w = &field_w;
123
         for (;;)
124
             {
125
               switch(c = *format++)
126
                  {
127
                    case 'X': print_hex(buffer, (unsigned int)*args++,
128
                                        "0123456789ABCDEF");
129
                              len += print_item(buffer, flags, sign, pad,
130
                                                "0X", field_w, min_w, '0');
131
                              break;
132
 
133
                    case 'd': if (((int)*args) < 0)
134
                                 {
135
                                   sign = '-';
136
                                   *args = (char *)(- ((int)*args));
137
                                 }
138
                              print_unsigned(buffer, (unsigned int)*args++);
139
                              len += print_item(buffer, flags, sign, pad,
140
                                                "", field_w, min_w, '0');
141
                              break;
142
 
143
                    case 's': len += print_item(*args++, flags & 0x02, 0, ' ',
144
                                                "", field_w, min_w, ' ');
145
                              break;
146
 
147
                    case 'u': print_unsigned(buffer, (unsigned int)*args++);
148
                              len += print_item(buffer, flags, sign, pad,
149
                                                "", field_w, min_w, '0');
150
                              break;
151
 
152
                    case 'x': print_hex(buffer, (unsigned int)*args++,
153
                                        "0123456789abcdef");
154
                              len += print_item(buffer, flags, sign, pad,
155
                                                "0x", field_w, min_w, '0');
156
                              break;
157
 
158
                    case 'c': len += putchr((int)*args++);    break;
159
 
160
                    case '#': flags |= 0x01;                  continue;
161
                    case '-': flags |= 0x02;                  continue;
162
                    case ' ': if (!sign)  sign = ' ';         continue;
163
                    case '+': sign = '+';                     continue;
164
                    case '.': which_w = &min_w;               continue;
165
 
166
                    case '0': if (*which_w)   *which_w *= 10;
167
                              else            pad = '0';
168
                              continue;
169
 
170
                    case '1': *which_w = 10 * *which_w + 1;   continue;
171
                    case '2': *which_w = 10 * *which_w + 2;   continue;
172
                    case '3': *which_w = 10 * *which_w + 3;   continue;
173
                    case '4': *which_w = 10 * *which_w + 4;   continue;
174
                    case '5': *which_w = 10 * *which_w + 5;   continue;
175
                    case '6': *which_w = 10 * *which_w + 6;   continue;
176
                    case '7': *which_w = 10 * *which_w + 7;   continue;
177
                    case '8': *which_w = 10 * *which_w + 8;   continue;
178
                    case '9': *which_w = 10 * *which_w + 9;   continue;
179
                    case '*': *which_w = (int)*args++;        continue;
180
 
181
                    case 0:   format--;   // premature end of format
182
                              break;
183
 
184
                    default:  len += putchr(c);
185
                              break;
186
                  }
187
                break;
188
             }
189
       }
190
   return len;
191
}
192
/*******************************************************************************
193
********************************************************************************
194
**                                                                            **
195
**                     SERIAL INPUT                                           **
196
**                                                                            **
197
********************************************************************************
198
*******************************************************************************/
199
 
200
//-----------------------------------------------------------------------------
201
int _getchr()
202
{
203
   while (! (ASM(" IN   (IN_STATUS), RU") & 0x01))   /* wait */ ;
204
   ASM("IN   (IN_RX_DATA), RU");
205
}
206
//-----------------------------------------------------------------------------
207
char last_char = 0;
208
int getchr()
209
{
210
char ret = last_char;
211
   last_char = 0;
212
 
213
   if (!ret)   ret = _getchr();
214
   return ret;
215
}
216
//-----------------------------------------------------------------------------
217
int peekchr()
218
{
219
   if (!last_char)   last_char = _getchr();
220
   return last_char;
221
}
222
//-----------------------------------------------------------------------------
223
char getnibble(char echo)
224
{
225
char c  = peekchr();
226
int ret = -1;
227
 
228
   if      ((c >= '0') && (c <= '9'))   ret = c - '0';
229
   else if ((c >= 'A') && (c <= 'F'))   ret = c - 0x37;
230
   else if ((c >= 'a') && (c <= 'f'))   ret = c - 0x57;
231
 
232
   if (ret != -1)   // valid hex char
233
      {
234
        getchr();
235
        if (echo)   putchr(c);
236
      }
237
   return ret;
238
}
239
//-----------------------------------------------------------------------------
240
int gethex(char echo)
241
{
242
int  ret = 0;
243
char c;
244
 
245
   while ((c = getnibble(echo)) != -1)   ret = (ret << 4) | c;
246
   return ret;
247
}
248
/*******************************************************************************
249
********************************************************************************
250
**                                                                            **
251
**                     main and its helpers                                   **
252
**                                                                            **
253
********************************************************************************
254
*******************************************************************************/
255
 
256 13 jsauermann
struct test
257
{
258
   int a;
259
   char b;
260
   int c;
261
};
262
 
263
void show_test(struct test t)
264
{
265
   printf("t.a = %d\n", t.a);
266
   printf("t.b = %c\n", t.b);
267
   printf("t.c = %d\n", t.c);
268
}
269 6 jsauermann
//-----------------------------------------------------------------------------
270
void display_memory(unsigned char * address)
271
{
272
char c;
273
int  row;
274
int  col;
275
 
276
   for (row = 0; row < 16; row++)
277
       {
278
         printf("%4.4X:", address);
279
         for (col = 0; col < 16; col++)   printf(" %2.2X", *address++);
280
         address -= 16;
281
         printf(" - ");
282
         for (col = 0; col < 16; col++)
283
             {
284
               c = *address++;
285
               if (c < ' ')         putchr('.');
286
               else if (c < 0x7F)   putchr(c);
287
               else                 putchr('.');
288
             }
289
         printf("\r\n");
290
       }
291
}
292
//-----------------------------------------------------------------------------
293
int main(int argc, char * argv[])
294
{
295
char            c;
296
char            noprompt;
297
char            last_c;
298
unsigned char * address;
299
 
300
   for (;;)
301
      {
302
        last_c = c;
303
        if (!noprompt)   printf("-> ");
304
        noprompt = 0;
305
        switch(c = getchr())
306
           {
307
             case '\r':
308
             case '\n':
309
                  if (last_c == 'd')
310
                     {
311
                       address += 0x100;
312
                       printf("\b\b\b\b");
313
                       display_memory(address);
314
                       c = 'd';
315
                     }
316
                  noprompt = 1;
317
                  break;
318
 
319 13 jsauermann
             case 'a':
320
                  {
321
                    char t[] = { 1, '=', 2 };
322
                    // show_test(t);
323
                    break;
324
                  }
325
 
326 6 jsauermann
             case 'D':
327
             case 'd':
328
                  last_c = 'd';
329
                  printf("Display ");
330
                  address = (unsigned char *)gethex(1);
331
                  printf("\r\n");
332
                  getchr();
333
                  display_memory(address);
334
                  break;
335
 
336
             case 'E':
337
             case 'e':
338
                  printf("LEDs ");
339
                  gethex(1);    ASM(" OUT R, (OUT_LEDS)");
340
                  printf("\r\n");
341
                  getchr();
342
                  break;
343
 
344
             case 'K':
345
             case 'k':
346
                  printf("Stack is at 0x%X\r\n",
347
                         ASM(" LEA 0(SP), RR"));
348
                  break;
349
 
350
             case 'M':
351
             case 'm':
352
                  printf("Memory ");
353
                  address = (unsigned char *)gethex(1);
354
                  printf(" Value ");
355
                  getchr();
356
                  *address = gethex(1);
357
                  getchr();
358
                  printf("\r\n");
359
                  break;
360
 
361
             case 'S':
362
             case 's':
363
                  printf("DIP switch is 0x%X\r\n",
364
                         ASM(" IN (IN_DIP_SWITCH), RU"));
365
                  break;
366
 
367
             case 'T':
368
             case 't':
369
                  {
370
                    int temp = ASM(" IN (IN_TEMPERAT), RU");
371
                    printf("Temperature is %d degrees Celsius\r\n", temp);
372
                  break;
373
                 }
374
 
375
             case 'Q':
376
             case 'q':
377
             case 'X':
378
             case 'x': printf("Halted.\r\n");
379
                       ASM(" HALT");
380
                  break;
381
 
382
             default:
383
                  printf("\r\n"
384
                         "C - show time\r\n"
385
                         "D - display memory\r\n"
386
                         "E - set LEDs\r\n"
387
                         "K - show stack\r\n"
388
                         "M - modify memory\r\n"
389
                         "S - read DIP switch\r\n"
390
                         "T - read temperature\r\n"
391
                         "Q - quit\r\n"
392
                         "X - exit\r\n"
393
                         "\r\n");
394
           }
395
      }
396
}
397
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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