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

Subversion Repositories cpu_lecture

[/] [cpu_lecture/] [trunk/] [tools/] [end_conv.cc] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jsauermann
#include "assert.h"
2
#include "ctype.h"
3
#include "stdio.h"
4
#include "string.h"
5
 
6
//-----------------------------------------------------------------------------
7
int
8
main(int argc, const char * argv)
9
{
10
char buffer[2000];
11
int pc, val, val2;
12
 
13
   for (;;)
14
       {
15
         char * s = fgets(buffer, sizeof(buffer) - 2, stdin);
16
         if (s == 0)   return 0;
17
 
18
         // map lines '  xx:' and 'xxxxxxxx; to 2* the hex value.
19
         //
20
         if (
21
             (isxdigit(s[0]) || s[0] == ' ') &&
22
             (isxdigit(s[1]) || s[1] == ' ') &&
23
             (isxdigit(s[2]) || s[2] == ' ') &&
24
              isxdigit(s[3]) && s[4] == ':')   // '  xx:'
25
            {
26
              assert(1 == sscanf(s, " %x:", &pc));
27
              if (pc & 1)       printf("%4X+:", pc/2);
28
              else              printf("%4X:", pc/2);
29
              s += 5;
30
            }
31
         else if (isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) &&
32
                  isxdigit(s[3]) && isxdigit(s[4]) && isxdigit(s[5]) &&
33
                  isxdigit(s[6]) && isxdigit(s[7]))             // 'xxxxxxxx'
34
            {
35
              assert(1 == sscanf(s, "%x", &pc));
36
              if (pc & 1)   printf("%8.8X+:", pc/2);
37
              else          printf("%8.8X:", pc/2);
38
              s += 8;
39
            }
40
         else                             // other: copy verbatim
41
            {
42
              printf("%s", s);
43
              continue;
44
            }
45
 
46
          while (isblank(*s))   printf("%c", *s++);
47
 
48
          // endian swap.
49
          //
50
          while (isxdigit(s[0]) &&
51
                 isxdigit(s[1]) &&
52
                          s[2] == ' ' &&
53
                 isxdigit(s[3]) &&
54
                 isxdigit(s[4]) &&
55
                          s[5] == ' ')
56
             {
57
              assert(2 == sscanf(s, "%x %x ", &val, &val2));
58
              printf("%2.2X%2.2X  ", val2, val);
59
              s += 6;
60
             }
61
 
62
         char * s1 = strstr(s, ".+");
63
         char * s2 = strstr(s, ".-");
64
         if (s1)
65
            {
66
              assert(1 == sscanf(s1 + 2, "%d", &val));
67
              assert((val & 1) == 0);
68
              sprintf(s1, " 0x%X", (pc + val)/2 + 1);
69
              printf(s);
70
              s = s1 + strlen(s1) + 1;
71
            }
72
         else if (s2)
73
            {
74
              assert(1 == sscanf(s2 + 2, "%d", &val));
75
              assert((val & 1) == 0);
76
              sprintf(s2, " 0x%X", (pc - val)/2 + 1);
77
              printf(s);
78
              s = s2 + strlen(s2) + 1;
79
            }
80
 
81
         printf("%s", s);
82
       }
83
}
84
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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