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

Subversion Repositories copyblaze

[/] [copyblaze/] [trunk/] [copyblaze/] [sw/] [tools/] [asm/] [pBlazASM/] [cpBlazeMRG/] [cpBlazeMRG.c] - Blame information for rev 68

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ameziti
//--------------------------------------------------------------------------------
2
//-- Company: 
3
//--
4
//-- File: cpBlazeMRG.c
5
//--
6
//-- Description:
7
//--    projet copyBalze
8
//--    initialize a VHDL memory
9
//--
10
//-- File history:
11
//-- v1.0: 20/10/11: Creation
12
//--
13
//-- Targeted device: ProAsic A3P250 VQFP100
14
//-- Author: AbdAllah Meziti
15
//--------------------------------------------------------------------------------
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <ctype.h>
20
#include <math.h>
21
#include <time.h>
22
 
23
#define PROG_NAME               "cpBlazeMRG"
24
#define PROG_REV                "01.00"
25
 
26
#define ko      1024
27
 
28
#define PROGRAM_COUNT   1*ko                    /* total program word */
29
#define MAX_LINE_COUNT  PROGRAM_COUNT   /* max 1000 lines allowed */
30
 
31
#define MAX_LINE_LENTH  200                             /* max number of caractere per line */
32
 
33
#define EXTENSION_IN    ".hex"
34
#define EXTENSION_OUT   ".vhd"
35
 
36
char filename[MAX_LINE_LENTH];
37
FILE *ifp;
38
FILE *ofp;
39
FILE *ffp;
40
 
41
char linebuf[MAX_LINE_LENTH];
42
int line_count = 0;
43
 
44
unsigned program_word[PROGRAM_COUNT]; /* program word array */
45
 
46
 
47
/*====================================== */
48
void error_out(void)
49
{
50
        exit(1);
51
}
52
 
53
/*====================================== */
54
/* write program word in hex format */
55
void write_bin(void)
56
{
57
        int i;
58
        char *ptr;
59
 
60
        ptr = strstr(filename, ".log");
61
        *ptr = '\0';
62
        strcat(filename,".bin");
63
 
64
        ffp = fopen(filename, "w");
65
        if (ffp == NULL){
66
        printf("\nCan not open bin file\n");
67
                exit(1);
68
        }
69
 
70
        for(i = 0; i < PROGRAM_COUNT; i++){
71
                fprintf(ffp, "%3d : %05X\n", i, program_word[i]);
72
        }
73
 
74
        fclose(ffp);
75
}
76
 
77
/*====================================== */
78
/* write vhdl module ROM */
79
void write_vhd(void)
80
{
81
#define DATA_HEX
82
#define WIDTH_ADDRESS           10
83
#define WIDTH_INSTRUCTION       18
84
#define WIDTH_ROM                       20
85
#define WIDTH_LENGTH            PROGRAM_COUNT
86
 
87
        int i, j;
88
        char *ptr;
89
        char basename[MAX_LINE_LENTH];
90
 
91
        time_t rawtime;
92
        struct tm * timeinfo;
93
 
94
  time ( &rawtime );
95
  timeinfo = localtime ( &rawtime );
96
        ptr = strstr(filename, ".bin");
97
        *ptr = '\0';
98
        strcpy(basename, filename);
99
        strcat(filename,".vhd");
100
 
101
        ffp = fopen(filename, "w");
102
        if (ffp == NULL){
103
        printf("\nCan not open vhd file\n");
104
                exit(1);
105
        }
106
 
107
        /* *************** */
108
        /* VHDL generation */
109
        /* *************** */
110
        // VHDL : header
111
        fprintf(ffp,"--------------------------------------------------------------------------------\n");
112
        fprintf(ffp,"-- AUTOMATICALLY GENERATED BY  : %s rev %s                                                                          \n", PROG_NAME, PROG_REV);
113
        fprintf(ffp,"-- Company:                                                                     \n");
114
        fprintf(ffp,"--                                                                              \n");
115
        fprintf(ffp,"-- File: %s.vhd                                                                                                                             \n",basename);
116
        fprintf(ffp,"--                                                                              \n");
117
        fprintf(ffp,"-- Description:                                                                 \n");
118
        fprintf(ffp,"--   projet copyBalze                                                           \n");
119
        fprintf(ffp,"--   copyBalze µP code ROM content : width[%d] x depth[%d]                                          \n", WIDTH_INSTRUCTION, PROGRAM_COUNT);
120
        fprintf(ffp,"--                                                                              \n");
121
        fprintf(ffp,"-- File history:                                                                \n");
122
        fprintf(ffp,"-- v1.0: %s", asctime (timeinfo));
123
        fprintf(ffp,"--                                                                              \n");
124
        fprintf(ffp,"-- Targeted device: ProAsic A3P250 VQFP100                                      \n");
125
        fprintf(ffp,"-- Author: AbdAllah Meziti                                                      \n");
126
        fprintf(ffp,"--------------------------------------------------------------------------------\n");
127
 
128
        // VHDL : library
129
        fprintf(ffp,"library ieee;\n");
130
        fprintf(ffp,"use ieee.std_logic_1164.all;\n");
131
        fprintf(ffp,"use ieee.numeric_std.all;\n\n");
132
 
133
        // VHDL : entity
134
        fprintf(ffp,"--------------------------------------------------------------------------------\n");
135
        fprintf(ffp,"-- Entity: %s                                                         \n",basename);
136
        fprintf(ffp,"--                                                                              \n");
137
        fprintf(ffp,"-- Description:                                                                 \n");
138
        fprintf(ffp,"--                                                                              \n");
139
        fprintf(ffp,"-- REMARQUE:                                                                    \n");
140
        fprintf(ffp,"--                                                                              \n");
141
        fprintf(ffp,"--                                                                              \n");
142
        fprintf(ffp,"-- History:                                                                     \n");
143
        fprintf(ffp,"-- v1.0: %s", asctime (timeinfo));
144
        fprintf(ffp,"-- AM: Creation                                                                     \n");
145
        fprintf(ffp,"-- ---------------------                                                        \n");
146
        fprintf(ffp,"--                                                                                          \n");
147
        fprintf(ffp,"--------------------------------------------------------------------------------\n");
148
 
149
        fprintf(ffp,"entity %s is\n",basename);
150
        fprintf(ffp,"\tgeneric\n");
151
        fprintf(ffp,"\t(\n");
152
        fprintf(ffp,"\t GEN_WIDTH_PC            : positive := %d;\n",WIDTH_ADDRESS);
153
        fprintf(ffp,"\t GEN_WIDTH_INST          : positive := %d\n",WIDTH_INSTRUCTION);
154
        fprintf(ffp,"\t);\n");
155
        fprintf(ffp,"\tport\n");
156
        fprintf(ffp,"\t(\n");
157
        fprintf(ffp,"\t\tClk_i\t\t: in std_ulogic;\n");
158
        fprintf(ffp,"\t\tAddress_i\t: in std_ulogic_vector(GEN_WIDTH_PC-1 downto 0);\n");
159
        fprintf(ffp,"\t\tDout_o\t\t: out std_ulogic_vector(GEN_WIDTH_INST-1 downto 0)\n");
160
        fprintf(ffp,"\t);\n");
161
        fprintf(ffp,"end;\n\n");
162
 
163
        // VHDL : architecture
164
        fprintf(ffp, "--------------------------------------------------------------------------------\n");
165
        fprintf(ffp, "-- Architecture: RTL                                                            \n");
166
        fprintf(ffp, "-- of entity : %s\n", basename);
167
        fprintf(ffp, "--------------------------------------------------------------------------------\n");
168
        fprintf(ffp,"architecture rtl of %s is\n\n", basename);
169
 
170
        fprintf(ffp,"\tconstant ROM_WIDTH\t: INTEGER:= %d;\n",WIDTH_ROM);
171
        fprintf(ffp,"\tconstant ROM_LENGTH\t: INTEGER:= %d;\n\n",WIDTH_LENGTH);
172
 
173
        fprintf(ffp,"\tsubtype rom_word is std_ulogic_vector(ROM_WIDTH-1 downto 0);\n");
174
        fprintf(ffp,"\ttype\trom_table is array (0 to ROM_LENGTH-1) of rom_word;\n\n");
175
 
176
        fprintf(ffp,"constant rom: rom_table := rom_table'(\n");
177
        // VHDL : start initialization of the ROM memory
178
        for(i = 0; i < PROGRAM_COUNT-1; i++){
179
#ifdef  DATA_HEX
180
                fprintf(ffp, "\tx\"");
181
                fprintf(ffp, "%05X", program_word[i]);
182
#else   //DATA_BIN
183
                fprintf(ffp, "\t\"");
184
                for(j = WIDTH_INSTRUCTION-1; j >= 0; j--) fprintf(ffp, "%d", (program_word[i]>>j) & 1); //print binary
185
#endif
186
                fprintf(ffp, "\",");
187
                fprintf(ffp, "\t-- %03X%\n",i);
188
        }
189
 
190
#ifdef  DATA_HEX
191
                fprintf(ffp, "\tx\"");
192
                fprintf(ffp, "%05X", program_word[i]);
193
#else   //DATA_BIN
194
                fprintf(ffp, "\t\"");
195
                for(j = WIDTH_INSTRUCTION-1; j >= 0; j--) fprintf(ffp, "%d", (program_word[i]>>j) & 1); //print binary
196
#endif
197
                fprintf(ffp, "\"");
198
                fprintf(ffp, "\t-- %03X%\n",i);
199
 
200
                fprintf(ffp, "\t);\n\n");
201
        // VHDL : end Initialize the ROM memory
202
 
203
        fprintf(ffp,"\tsignal   iData   : std_ulogic_vector(GEN_WIDTH_INST-1 downto 0);\n");
204
        fprintf(ffp,"\n");
205
        fprintf(ffp,"begin\n");
206
        fprintf(ffp,"\n");
207
 
208
        // VHDL : end Initialize the ROM memory
209
        fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
210
        fprintf(ffp,"\t-- Process : ROM_Proc                                                           \n");
211
        fprintf(ffp,"\t-- Description: ROM Memory                                                      \n");
212
        fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
213
        fprintf(ffp,"\tROM_Proc : process (Clk_i)\n");
214
        fprintf(ffp,"\tbegin\n");
215
        fprintf(ffp,"\t\tif ( rising_edge(Clk_i) ) then\n");
216
        fprintf(ffp,"\t\t\tiData <= rom(to_integer(unsigned(Address_i)))(GEN_WIDTH_INST-1 downto 0);\n");
217
        fprintf(ffp,"\t\tend if;\n");
218
        fprintf(ffp,"\tend process ROM_Proc;\n");
219
        fprintf(ffp,"\n");
220
 
221
        fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
222
        fprintf(ffp,"\t-- Outputs                                                                      \n");
223
        fprintf(ffp,"\t--------------------------------------------------------------------------------\n");
224
        fprintf(ffp,"\tDout_o <= iData(GEN_WIDTH_INST-1 downto 0);\n");
225
 
226
        fprintf(ffp,"\n");
227
        fprintf(ffp,"end rtl;\n");
228
 
229
        fclose(ffp);
230
}
231
 
232
 
233
#define xtod(c) ((c>='0' && c<='9') ? c-'0' : ((c>='A' && c<='F') ? c-'A'+10 : ((c>='a' && c<='f') ? c-'a'+10 : 0)))
234
 
235
int HextoDec(char *hex)
236
{
237
    if (*hex==0) return 0;
238
    return  HextoDec(hex-1)*16 +  xtod(*hex) ;
239
}
240
 
241
int xstrtoi(char *hex)      // hex string to integer
242
{
243
    return HextoDec(hex+strlen(hex)-1);
244
}
245
 
246
/*====================================== */
247
int main(int argc, char **argv)
248
{
249
        char *ptr;
250
        int i;
251
 
252
    if(argc != 2){
253
                printf("\nCommand line syntax:\n\n %s file%s\n", PROG_NAME, EXTENSION_IN);
254
                exit(1);
255
        }
256
 
257
        strcpy(filename, argv[1]);
258
        ptr = strstr(filename, EXTENSION_IN);
259
        if (ptr == NULL){
260
                printf("\nInvalid file type, use %s extension\n", EXTENSION_IN);
261
                exit(1);
262
        }
263
        *ptr = '\0';
264
        strcat(filename,".log");
265
 
266
    // Open the ifp="input file"
267
        ifp = fopen(argv[1], "r");
268
    if (ifp == NULL){
269
        printf("\nCan not open input file\n");
270
                exit(1);
271
        }
272
 
273
        // Open the ofp="output file" (log)
274
        ofp = fopen(filename, "w");
275
        if (ofp == NULL){
276
        printf("\nCan not open output file\n");
277
                exit(1);
278
        }
279
        fprintf(ofp, "ROM Code generation %s logfile\n\n",EXTENSION_IN);
280
 
281
 
282
        printf("Reading input file...\n");
283
        fprintf(ofp,"Reading input file...\n");
284
 
285
        // Initialize the "program_word" table
286
        for(i = 0; i < PROGRAM_COUNT; i++)
287
                program_word[i] = 0xFFFFF;
288
 
289
        // Read the input file
290
        while ( fgets(linebuf, 128, ifp) != NULL ) {
291
                if(line_count >= MAX_LINE_COUNT){
292
                        printf("\nInput exceed maximum line number\n");
293 68 ameziti
            goto error;
294
                        //error_out();
295 2 ameziti
                }
296
 
297
                // Extract the data from In file to program_word
298
                ptr = strstr(linebuf, "\r\n");
299
                *ptr = '\0';
300
 
301
                //xtoi(linebuf, &program_word[line_count]);
302
 
303
                program_word[line_count] = xstrtoi(linebuf);
304
 
305
                //printf("\n%s=>%05X",linebuf,program_word[line_count]);
306
 
307
                line_count++;
308
        }
309
 
310 68 ameziti
error:
311
    //error_out();
312
 
313 2 ameziti
        printf("\nWrite output files...\n");
314
        fprintf(ofp,"Write output files...\n");
315
        write_bin();
316
        write_vhd();
317
 
318
        printf("Program completed...\n");
319
        fprintf(ofp,"Program completed...\n");
320
        fclose(ifp);
321
        fclose(ofp);
322
        return(0);
323
}
324
 

powered by: WebSVN 2.1.0

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