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

Subversion Repositories wbuart32

[/] [wbuart32/] [trunk/] [bench/] [cpp/] [mkspeech.cpp] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    mkspeech.cpp
4
//
5
// Project:     wbuart32, a full featured UART with simulator
6
//
7
// Purpose:     To turn a text file (i.e. the Gettysburg address) into a 
8
//              hex file that can be included via readmemh.
9
//
10
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12
//
13
////////////////////////////////////////////////////////////////////////////////
14
//
15 26 dgisselq
// Copyright (C) 2015-2019, Gisselquist Technology, LLC
16 5 dgisselq
//
17
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// You should have received a copy of the GNU General Public License along
28
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
29
// target there if the PDF file isn't present.)  If not, see
30
// <http://www.gnu.org/licenses/> for a copy.
31
//
32
// License:     GPL, v3, as defined and found on www.gnu.org,
33
//              http://www.gnu.org/licenses/gpl.html
34
//
35
//
36
////////////////////////////////////////////////////////////////////////////////
37
//
38
//
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <unistd.h>
42
#include <string.h>
43
 
44 11 dgisselq
/*
45
* endswith
46
*
47
* Real simple: returns true if the given string ends with the given ending.
48
* Useful for determining if a file ends with the extension .txt.
49
*
50
*/
51
bool    endswith(const char *str, const char *ending) {
52
        int     slen = strlen(str), send = strlen(ending);
53
        if (slen < send)
54
                return false;
55
        if (strcmp(&str[slen-send], ".txt")!=0)
56
                return false;
57
        return true;
58
}
59
 
60
/*
61
* usage()
62
*
63
* Tell the user the calling conventions of this program, and what the program
64
* can be used to accomplish.
65
*/
66
void    usage(void) {
67
        fprintf(stderr, "USAGE:\tmkspeech [-x] <filename>.txt [-o <outfile>]\n");
68
        fprintf(stderr, "\n"
69
"\tConverts a text file to a file such as can be included in a Verilog\n"
70
"\tprogram.  Without the -x argument, the mkspeech program defaults\n"
71
"\tto converting the text file to a hex file, whose output name defaults\n"
72
"\tto \'speech.hex\'.  With the -x argument, mkspeech converts the file\n"
73
"\tinto an include file such as might be used in a Verilog program\n"
74
"\tif and when the synthesis tool doesn\'t support hex files (Xilinx\'s\n"
75
"\tISE).  In this case, the output filename defaults to \'speech.inc\'.\n"
76
"\n\n");
77
}
78
 
79 5 dgisselq
int main(int argc, char **argv) {
80
        FILE    *fp, *fout;
81 11 dgisselq
        const   char    *input_filename = NULL, *output_filename = NULL;
82
        bool    xise_file = false;
83 5 dgisselq
 
84 11 dgisselq
        for(int argn=1; argn < argc; argn++) {
85
                if (argv[argn][0] == '-') {
86
                        if (argv[argn][2] == '\0') {
87
                                if (argv[argn][1] == 'x')
88
                                        xise_file = true;
89
                                else if (argv[argn][1] == 'o') {
90
                                        if (argn+1<argc)
91
                                                output_filename = argv[++argn];
92
                                        else  {
93
                                        fprintf(stderr, "ERR: -o given, but no filename given");
94
                                                usage();
95
                                                exit(EXIT_FAILURE);
96
                                        }
97
                                } else {
98
                                        fprintf(stderr, "ERR: Unknown argument, %s\n", argv[argn]);
99
                                        usage();
100
                                        exit(EXIT_FAILURE);
101
                                }
102
                        } else {
103
                                fprintf(stderr, "ERR: Unknown argument, %s\n", argv[argn]);
104
                                usage();
105
                                exit(EXIT_FAILURE);
106
                        }
107
                } else if (input_filename == NULL) {
108
                        input_filename = argv[argn];
109
                } else {
110
                        fprintf(stderr, "ERR: Too many file names given, %s when I already have %s\n", argv[argn], input_filename);
111
                        usage();
112
                        exit(EXIT_FAILURE);
113
                }
114
        }
115
 
116
        if (input_filename== NULL) {
117
                fprintf(stderr, "No filename given\n");
118
                usage();
119 5 dgisselq
                exit(EXIT_FAILURE);
120 11 dgisselq
        }
121
 
122
        if (!endswith(input_filename, ".txt")) {
123
                fprintf(stderr, "Err: %s is an invalid text file name\n", input_filename);
124 5 dgisselq
                exit(EXIT_FAILURE);
125 11 dgisselq
        }
126
 
127
        if (access(input_filename, F_OK)!=0) {
128
                fprintf(stderr, "Err: %s is not a file\n", input_filename);
129 5 dgisselq
                exit(EXIT_FAILURE);
130 11 dgisselq
        } else if (access(input_filename, R_OK)!=0) {
131
                fprintf(stderr, "Err: Cannot read %s\n", input_filename);
132 5 dgisselq
                exit(EXIT_FAILURE);
133
        }
134
 
135 11 dgisselq
        fp = fopen(input_filename, "r");
136 5 dgisselq
        if (fp == NULL) {
137 11 dgisselq
                fprintf(stderr, "Err: Cannot read %s\n", input_filename);
138 5 dgisselq
                exit(EXIT_FAILURE);
139
        }
140
 
141 11 dgisselq
        if (output_filename == NULL)
142
                output_filename = (xise_file) ? "speech.inc" : "speech.hex";
143
 
144
        fout = fopen(output_filename, "w");
145 5 dgisselq
        if (fout == NULL) {
146 11 dgisselq
                fprintf(stderr, "Err: Cannot write %s\n", output_filename);
147 5 dgisselq
                exit(EXIT_FAILURE);
148
        }
149
 
150 11 dgisselq
        if (xise_file) {
151
                // Build an include file
152
                int     ch, addr = 0;
153
                while((ch = fgetc(fp))!=EOF) {
154
                        if (ch == '\n')
155
                                fprintf(fout, "\t\tmessage[%4d] = 8\'h%02x;\n",
156
                                        addr++, '\n');
157
                        fprintf(fout, "\t\tmessage[%4d] = 8\'h%02x;\n",
158
                                addr++, ch);
159
                }
160 5 dgisselq
 
161 11 dgisselq
                for(; addr<2048; addr++)
162
                        fprintf(fout, "\t\tmessage[%4d] = 8'h%02x;\n", addr, ' ');
163
        } else {
164
                // Bulid a proper hex file
165
                int     linelen = 0;
166
                int     ch, addr = 0;
167
 
168
                fprintf(fout, "@%08x ", addr); linelen += 4+6;
169
                while((ch = fgetc(fp))!=EOF) {
170
                        if (ch == '\n') {
171
                                fprintf(fout, "%02x ", '\r' & 0x0ff); linelen += 3; addr++;
172
                                if (linelen >= 77) {
173
                                        fprintf(fout, "\n");
174
                                        linelen = 0;
175
                                        fprintf(fout, "@%08x ", addr); linelen += 4+6;
176
                                }
177
                        }
178
                        fprintf(fout, "%02x ", ch & 0x0ff); linelen += 3; addr++;
179
 
180 5 dgisselq
                        if (linelen >= 77) {
181
                                fprintf(fout, "\n");
182
                                linelen = 0;
183
                                fprintf(fout, "@%08x ", addr); linelen += 4+6;
184
                        }
185 11 dgisselq
                } fprintf(fout, "\n");
186
        }
187 5 dgisselq
        fclose(fp);
188
        fclose(fout);
189
}

powered by: WebSVN 2.1.0

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