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

Subversion Repositories mips32r1

[/] [mips32r1/] [trunk/] [Software/] [demos/] [util/] [ram_image.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayersg
/*
2
 * File          : ram_image.c
3
 * Project       : University of Utah, XUM Project
4
 * Creator(s)    : Grant Ayers (ayers@cs.utah.edu)
5
 *
6
 * Modification History:
7
 *   Rev   Date         Initials  Description of Change
8
 *   1.0   7-8-2011     GEA       Initial design.
9
 *
10
 * Standards/Formatting:
11
 *   C, 8 hard tab, 80 column
12
 *
13
 * Description:
14
 *   Fills a specific type of Verilog file which contains a
15
 *   Block RAM primitive with the initialization vectors from
16
 *   'code.txt' and outputs 'imem_filled.v'.
17
 *
18
 *   This utility is useful for filling simple and small block
19
 *   RAMs, especially for basic simulations. However it is no
20
 *   longer used for the production XUM project.
21
 */
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
 
26
void quit(int val);
27
int  inject(char* vectors, char* output);
28
 
29
FILE* verilog;
30
FILE* vectors;
31
FILE* output;
32
char* out_buf = NULL;
33
char* vec_buf = NULL;
34
 
35
int main(int argc, char* argv[])
36
{
37
        int verilog_size, vectors_size;
38
        int inst_written;
39
 
40
 
41
        if (argc < 4)
42
        {
43
                fprintf(stderr, "Usage: %s: <in.v> <in_code.txt> <out.v>\n", argv[0]);
44
                fprintf(stderr, "Usage: %s: ram_xilinx.v code.txt ram_image.v\n", argv[0]);
45
                quit(1);
46
        }
47
 
48
        /* Open the Verilog source file and copy it into a buffer */
49
        verilog = fopen(argv[1], "rb");
50
        if (!verilog)
51
        {
52
                fprintf(stderr, "Could not open \"%s\".\n", argv[1]);
53
                quit(1);
54
        }
55
        fseek(verilog, 0L, SEEK_END);
56
        verilog_size = ftell(verilog);
57
        fseek(verilog, 0L, SEEK_SET);
58
        if (verilog_size == 0)
59
        {
60
                fprintf(stderr, "Error: Empty verilog input file.\n");
61
                quit(1);
62
        }
63
        out_buf = malloc(verilog_size);
64
        if (!out_buf)
65
        {
66
                fprintf(stderr, "Error allocating memory.\n");
67
                quit(1);
68
        }
69
        if (fread(out_buf, 1, verilog_size, verilog) != verilog_size)
70
        {
71
                fprintf(stderr, "Error reading input file.\n");
72
                quit(1);
73
        }
74
 
75
 
76
        /* Open code vectors and copy them into a buffer */
77
        vectors = fopen(argv[2], "rb");
78
        if (!vectors)
79
        {
80
                fprintf(stderr, "Could not open \"%s\".\n", argv[2]);
81
                quit(1);
82
        }
83
        fseek(vectors, 0L, SEEK_END);
84
        vectors_size = ftell(vectors);
85
        fseek(vectors, 0L, SEEK_SET);
86
        if (vectors_size == 0)
87
        {
88
                fprintf(stderr, "Error: Empty vectors file.\n");
89
                quit(1);
90
        }
91
        //printf("Vectors size is %d bytes.\n", vectors_size);
92
        vec_buf = malloc(vectors_size+1);
93
        if (!vec_buf)
94
        {
95
                fprintf(stderr, "Error allocating memory.\n");
96
                quit(1);
97
        }
98
        if (fread(vec_buf, 1, vectors_size, vectors) != vectors_size)
99
        {
100
                fprintf(stderr, "Error reading vectors file.\n");
101
                quit(1);
102
        }
103
        vec_buf[vectors_size] = '\0';
104
 
105
        /* Inject code */
106
        inst_written = inject(vec_buf, out_buf);
107
        printf("Wrote %d instructions.\n", inst_written);
108
 
109
        /* Write output file */
110
        output = fopen(argv[3], "wb");
111
        if (output == NULL)
112
        {
113
                fprintf(stderr, "Error writing %s!\n", argv[3]);
114
                quit(1);
115
        }
116
        fwrite(out_buf, 1, verilog_size, output);
117
        fclose(output);
118
 
119
 
120
        // Exit
121
        quit(0);
122
        return 0;
123
}
124
 
125
 
126
int inject(char* vectors, char* output)
127
{
128
        const char* delimeters = " \t\r\n";
129
        char  row_key[15];  // ".INIT_XX(256'h"
130
        char* token;
131
        char* position;
132
        int   row = 0;
133
        int   col = 7;
134
        int   total = 0;
135
 
136
        token = strtok(vectors, delimeters);
137
        snprintf(row_key, 14, ".INIT_%02X(256'h", row);
138
        while ((token != NULL) && (row < 128))
139
        {
140
                //printf("Got a token: \"%s\"\n", token);
141
                if (strlen(token) != 8)
142
                {
143
                        fprintf(stderr, "Error: Vector \"%s\" is not "
144
                                "a 32-bit hexadecimal number.\n", token);
145
                        quit(1);
146
                }
147
                position = strstr(output, row_key);
148
                if (position == NULL)
149
                {
150
                        fprintf(stderr, "Error: Could not find initialization "
151
                                "row %02X (hex) in the Block RAM. Check that "
152
                                "it has sufficient memory.\n", row);
153
                        printf("\n\n\nDEBUG\n%s\n", output);
154
                        quit(1);
155
                }
156
                //position += (14 + col + (8 * col));
157
                position += (14 + (8 * col));
158
                memcpy(position, token, 8);
159
                total++;
160
                col--;
161
                if (col < 0)
162
                {
163
                        col = 7;
164
                        row++;
165
                        snprintf(row_key, 14, ".INIT_%02X(256'h", row);
166
                }
167
                //printf("Col is %d row is %d\n", col, row);
168
                token = strtok(NULL, delimeters);
169
        }
170
        return total;
171
}
172
 
173
 
174
void quit(int val)
175
{
176
        if (verilog) {
177
                fclose(verilog);
178
        }
179
        if (vectors) {
180
                fclose(vectors);
181
        }
182
        if (output) {
183
                fclose(output);
184
        }
185
        if (out_buf) {
186
                free(out_buf);
187
        }
188
        if (vec_buf) {
189
                free(vec_buf);
190
        }
191
 
192
        if (val != 0) {
193
                exit(val);
194
        }
195
}
196
 

powered by: WebSVN 2.1.0

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