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

Subversion Repositories xgate

[/] [xgate/] [trunk/] [sw/] [tools/] [srec_2_verilog/] [srec_2_verilog.c] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 rehayes
/* Verilog ROM Generator/Programer  Rev B March 12 1996  Bob Hayes            */
2
/* Started work on S-Record Writer Nov. 6, 1996 -- working except parity      */
3
 
4
/* Rev 1.1 Sept. 11, 2009 - Bob Hayes - Update to create output file name     */
5
/*   from input file name by changing extension to .v                         */
6
 
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10
 
11
FILE * open_read_file (char *);
12
FILE * open_write_file (char *);
13
int Get_ROM_Byte (FILE *, int *);
14
void Make_Ref_Mem (FILE *, FILE *);
15
 
16
 
17
/* ************************************************************************** */
18
 
19
/* ************************************************************************** */
20
int main(int argc, char *argv[])
21
{
22
    int i, j, k = 0;
23
    char c;
24
    FILE *file1, *file2;
25
    file1 = NULL;
26
    file2 = NULL;
27
 
28
    printf("\nConvert S-record to Verilog memory file\n");
29
 
30
    if (argc != 2)
31
    {
32
      printf("Usage: %s filename\n", argv[0]);
33
      exit(1);
34
    }
35
 
36
    file1 = open_read_file(argv[1]);
37
    file2 = open_write_file(argv[1]);
38
    Make_Ref_Mem(file1, file2);
39
    fclose(file1);
40
    fclose(file2);
41
 
42
    printf("\n All done now!\n");
43
    return 0;
44
}
45
 
46
 
47
/* ************************************************************************** */
48
/* 45678901234567890123456789012345678901234567890123456789012345678901234567 */
49
/* ************************************************************************** */
50
 
51
/* ************************************************************************** */
52
int Get_ROM_Byte (FILE *f1, int *S_Addr)
53
{
54
    static int Addr = 0, Count = 0, Sum = 0, Flag = 0;
55
    static int Max_Count = 0, Parity;
56
    static unsigned char c, *ch;
57
    static char EOL_str[80];
58
    static int i, j, k, OK, Byte;
59
    static long int Line_cnt = 0, Byte_cnt = 0;
60
 
61
    if (Flag == 0 )
62
    {
63
        Flag = 1;
64
        /* This code is to read first line of miscellaneous data */
65
        OK = fscanf(f1, "%c", &c);           /* Read the "S"         */
66
        OK = fscanf(f1, "%1x", &i);          /* Read the type number */
67
        OK = fscanf(f1, "%2x", &Max_Count);  /* Read the byte count  */
68
        if ( c != 'S' )
69
        {
70
            printf("Error in S-Records, No -S-, S = %c, i = %i, Max = %i\n", c, i, Max_Count);
71
            printf("Line Number = %i, Byte Number = %i\n", Line_cnt, Byte_cnt);
72
        }
73
        if ( i != 0 )
74
           rewind(f1);  /* Back-up if there is no comment line */
75
        else
76
        {
77
            printf("First S-Record info line is:\n");
78
            for (j = 1; j <= Max_Count-1; ++j)
79
              {
80
                 fscanf(f1, "%2x", &k);
81
                 if (( k >= ' ') && (k <= '~'))
82
                    printf("%c",k);
83
              }
84
            printf("\n\n");
85
            fgets(EOL_str, 10, f1); /* pick-up parity and other junk */
86
        }
87
    }
88
 
89
    if ( Count == 0 )
90
    {
91
        Sum = 0;
92
        OK = fscanf(f1, "%c", &c);           /* Read the "S"         */
93
        OK = fscanf(f1, "%1x", &i);          /* Read the type number */
94
        OK = fscanf(f1, "%2x", &Max_Count);  /* Read the byte count  */
95
        if ( c != 'S' )
96
        {
97
            printf("Error in S-Records, No -S-, S = %c, i = %i, Max = %i\n", c, i, Max_Count);
98
            printf("Line Number = %i, Byte Number = %i\n", Line_cnt, Byte_cnt);
99
        }
100
        switch (i)
101
        {
102
             case 1:
103
             case 9:
104
                 Max_Count = Max_Count - 2;
105
                 OK = fscanf(f1, "%4x", &Addr);
106
                 break;
107
             case 2:
108
             case 8:
109
                 Max_Count = Max_Count - 3;
110
                 OK = fscanf(f1, "%6x", &Addr);
111
                 break;
112
             case 3:
113
             case 7:
114
                 Max_Count = Max_Count - 4;
115
                 OK = fscanf(f1, "%8x", &Addr);
116
                 break;
117
             default :
118
                 printf("Error in S-Record file! Unrecognized TYPE Number\n");
119
                 break;
120
        }
121
    }
122
 
123
    if ( Max_Count > 1)               /* Make sure there is at least one byte of data */
124
      {
125
          OK = fscanf(f1, "%2x", &Byte); /* Read a Byte of Data        */
126
          Sum = Sum + Byte;              /* Increment the parity count */
127
          ++Count;
128
          ++Byte_cnt;
129
      }
130
 
131
    if ( Count == (Max_Count - 1))    /* Do the parity check        */
132
    {
133
        Count = 0;
134
        OK = fscanf(f1, "%2x", &Parity);  /* Read the parity from the S-Record File */
135
        Sum = 0xFF & ~Sum;
136
/*        printf("Line = %d, Sum = %2x, Parity = %2x\n", Line_cnt, Sum, Parity); */
137
        ++Line_cnt;
138
 
139
        c = ' ';
140
        while (( c != 'S') && ((k = feof(f1)) == 0))
141
          OK = fscanf(f1, "%c", &c);        /* Eat up end of line */
142
 
143
        if (( k = feof(f1)) != 0)
144
           printf("found EOF in S-Record file\n   %i Lines Processed\n", Line_cnt);
145
        else
146
           ungetc(c, f1);
147
    }
148
 
149
/*    printf("Read Byte = %x, Count = %i, Max_Count = %i \n", Byte, Count, Max_Count); */
150
 
151
    *S_Addr = Addr;
152
    return Byte;
153
}
154
 
155
 
156
/* *************************************************************************************** */
157
/* Convert S-record to Verilog memory file *********************************************** */
158
 
159
void Make_Ref_Mem (FILE *f1, FILE *f2)
160
{
161
 
162
    int Byte, S_Addr, S_Addr_Old;
163
    int i, j, k;
164
 
165
    i = 0;
166
    S_Addr_Old = 1;
167
    printf("// Making Verilog Reference Memory File \n");
168
 
169
            while (( i = feof(f1)) == 0)
170
            {
171
                Byte = Get_ROM_Byte(f1, &S_Addr);
172
                if ( S_Addr != S_Addr_Old )
173
                {
174
                    fprintf(f2, "\n@%4.4X", S_Addr );
175
                    S_Addr_Old = S_Addr;
176
                }
177
                fprintf(f2, " %2.2X", Byte);
178
            }
179
 
180
    printf("Conversion All Done Now\n");
181
    return;
182
}
183
 
184
/* ************************************************************************** */
185
FILE * open_read_file (char file_name[80])
186
{
187
    char c;
188
    FILE *file_num;
189
 
190
    file_num = NULL;
191
    printf("Input File Name => %s\n", file_name);
192
    file_num = fopen(file_name, "rb");
193
    if (file_num == NULL)
194
    {
195
       printf("\nError in opening read file!!\n");
196
    }
197
    return file_num;
198
}
199
 
200
/* ************************************************************************** */
201
FILE * open_write_file (char file_name[80])
202
{
203
    FILE *file_num;
204
    char c, out_file_name[80];
205
    int i, j, k;
206
 
207
    i = strlen(file_name);
208
    strcpy(out_file_name, file_name);
209
    while (out_file_name[i] != '.')
210
    {
211
      out_file_name[i] = 0;
212
      i--;
213
    }
214
    strcat(out_file_name, "v");
215
 
216
    file_num = NULL;
217
    printf("Output File Name => %s\n", out_file_name);
218
    file_num = fopen(out_file_name, "w");
219
    if (file_num == NULL)
220
      {
221
         printf("\nError in opening write file!!\n");
222
      }
223
    printf("\n");
224
    return file_num;
225
}
226
 

powered by: WebSVN 2.1.0

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