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

Subversion Repositories c16

[/] [c16/] [trunk/] [test.c] - Blame information for rev 27

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

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

powered by: WebSVN 2.1.0

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