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

Subversion Repositories wbuart32

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

Go to most recent revision | 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
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
16
//
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
int main(int argc, char **argv) {
45
        FILE    *fp, *fout;
46
 
47
        if (argc != 2) {
48
                fprintf(stderr, "Err: USAGE is mkspeech <filename>.txt\n");
49
                exit(EXIT_FAILURE);
50
        } else if ((!argv[1])||(strlen(argv[1])<5)
51
                ||(strcmp(&argv[1][strlen(argv[1])-4], ".txt")!=0)) {
52
                fprintf(stderr, "Err: %s is an invalid text file name\n", argv[1]);
53
                exit(EXIT_FAILURE);
54
        } else if (access(argv[1], F_OK)!=0) {
55
                fprintf(stderr, "Err: %s is not a file\n", argv[1]);
56
                exit(EXIT_FAILURE);
57
        } else if (access(argv[1], R_OK)!=0) {
58
                fprintf(stderr, "Err: Cannot read %s\n", argv[1]);
59
                exit(EXIT_FAILURE);
60
        }
61
 
62
        fp = fopen(argv[1], "r");
63
        if (fp == NULL) {
64
                fprintf(stderr, "Err: Cannot read %s\n", argv[1]);
65
                exit(EXIT_FAILURE);
66
        }
67
 
68
        fout = fopen("speech.hex", "w");
69
        if (fout == NULL) {
70
                fprintf(stderr, "Err: Cannot write %s\n", "speech.hex");
71
                exit(EXIT_FAILURE);
72
        }
73
 
74
        int     linelen = 0;
75
        int     ch, addr = 0;
76
 
77
        fprintf(fout, "@%08x ", addr); linelen += 4+6;
78
        while((ch = fgetc(fp))!=EOF) {
79
                if (ch == '\n') {
80
                        fprintf(fout, "%02x ", '\r' & 0x0ff); linelen += 3; addr++;
81
                        if (linelen >= 77) {
82
                                fprintf(fout, "\n");
83
                                linelen = 0;
84
                                fprintf(fout, "@%08x ", addr); linelen += 4+6;
85
                        }
86
                }
87
                fprintf(fout, "%02x ", ch & 0x0ff); linelen += 3; addr++;
88
 
89
                if (linelen >= 77) {
90
                        fprintf(fout, "\n");
91
                        linelen = 0;
92
                        fprintf(fout, "@%08x ", addr); linelen += 4+6;
93
                }
94
        } fprintf(fout, "\n");
95
 
96
        fclose(fp);
97
        fclose(fout);
98
}

powered by: WebSVN 2.1.0

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