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

Subversion Repositories rise

[/] [rise/] [trunk/] [tools/] [bin2vhd.c] - Blame information for rev 138

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 137 cwalter
/*
2
 * RISE microprocessor bin2vhd utility
3
 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 * 3. The name of the author may not be used to endorse or promote products
15
 *    derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 *
28 138 cwalter
 * File: $Id: bin2vhd.c,v 1.2 2007-01-25 21:08:25 cwalter Exp $
29 137 cwalter
 */
30
 
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <getopt.h>
35
#include <errno.h>
36
#include <assert.h>
37
 
38
#define PROGNAME                "bin2vhd"
39 138 cwalter
#define VHDL_ENTITY_NAME        "pgrom"
40
#define VHDL_ARCHITECTURE_NAME  "pgrom_rtl"
41 137 cwalter
 
42 138 cwalter
#define ADDRESS_BITS            16
43
#define DATA_BITS               16
44 137 cwalter
#define IS_SYNCHRONOUS          0
45
#define PC_INCREMENT            2
46
 
47
#define NELEMS( x )             ( sizeof( x )/ sizeof( x[0] ) )
48
 
49
void            vPrintUsage( void );
50
void            vWriteEntity( FILE * pxOutputFile );
51
void            vWriteArchitectureHeader( FILE * pxOutputFile );
52
void            vWriteArchitectureData( FILE * pxOutputFile, const unsigned char *pucData,
53
                                        int iNBytes );
54
void            vWriteArchitectureFooter( FILE * pxOutputFile );
55
const char     *pcData2Hex( unsigned int uiHexValue );
56
const char     *pcAddress2Hex( unsigned int uiHexValue );
57
 
58
int
59
main( int argc, char **argv )
60
{
61
    int             iExitStatus = EXIT_FAILURE;
62
    FILE           *pxInputFile, *pxOutputFile;
63
    unsigned char   arucBuffer[80];
64
    size_t          xNBytesRead;
65
    size_t          xNBytesOffset;
66
 
67
    if( argc != 3 )
68
    {
69
        vPrintUsage(  );
70
    }
71
    else if( ( pxInputFile = fopen( argv[1], "r" ) ) == NULL )
72
    {
73
        fprintf( stderr, "%s: can't open input file %s: %s\n", PROGNAME, argv[1],
74
                 strerror( errno ) );
75
    }
76
    else if( ( pxOutputFile = fopen( argv[2], "w" ) ) == NULL )
77
    {
78
        fprintf( stderr, "%s: can't open input file %s: %s\n", PROGNAME, argv[1],
79
                 strerror( errno ) );
80
    }
81
    else
82
    {
83
        assert( ( DATA_BITS % 8 ) == 0 );
84
        assert( ( ADDRESS_BITS % 8 ) == 0 );
85
        vWriteEntity( pxOutputFile );
86
 
87
        vWriteArchitectureHeader( pxOutputFile );
88
        xNBytesOffset = 0;
89
        do
90
        {
91
            xNBytesRead = fread( &arucBuffer[xNBytesOffset], 1,
92
                                 NELEMS( arucBuffer ) - xNBytesOffset, pxInputFile );
93
            if( xNBytesRead > 0 )
94
            {
95
                xNBytesOffset = xNBytesRead % ( DATA_BITS / 8 );
96
                vWriteArchitectureData( pxOutputFile, arucBuffer, xNBytesRead - xNBytesOffset );
97
            }
98
        }
99
        while( xNBytesRead > 0 );
100
        vWriteArchitectureFooter( pxOutputFile );
101
 
102
        ( void )fclose( pxOutputFile );
103
        ( void )fclose( pxInputFile );
104
 
105
    }
106
}
107
 
108
void
109
vPrintUsage(  )
110
{
111
    fprintf( stderr, "Usage:\n" );
112
    fprintf( stderr, "  bin2vhd source dest\n" );
113
}
114
 
115
void
116
vWriteEntity( FILE * pxOutputFile )
117
{
118 138 cwalter
    fprintf( pxOutputFile, "library IEEE;\n" );
119 137 cwalter
    fprintf( pxOutputFile, "use IEEE.STD_LOGIC_1164.all;\n" );
120
    fprintf( pxOutputFile, "use IEEE.NUMERIC_STD.all;\n" );
121
    fprintf( pxOutputFile, "entity %s is\n", VHDL_ENTITY_NAME );
122
    fprintf( pxOutputFile, "port (\n" );
123
    fprintf( pxOutputFile, "  clk   : in std_logic;\n" );
124
    fprintf( pxOutputFile, "  addr  : in std_logic_vector(%d downto 0 );\n", ( ADDRESS_BITS - 1 ) );
125
    fprintf( pxOutputFile, "  data  : out std_logic_vector(%d downto 0 ) );\n", ( DATA_BITS - 1 ) );
126 138 cwalter
    fprintf( pxOutputFile, "end %s;\n", VHDL_ENTITY_NAME );
127 137 cwalter
    fprintf( pxOutputFile, "\n" );
128
}
129
 
130
void
131
vWriteArchitectureHeader( FILE * pxOutputFile )
132
{
133
    fprintf( pxOutputFile, "architecture %s of %s is\n", VHDL_ARCHITECTURE_NAME, VHDL_ENTITY_NAME );
134 138 cwalter
    fprintf( pxOutputFile, "  signal sig_data_next :  std_logic_vector(%d downto 0 );\n",
135 137 cwalter
             ( DATA_BITS - 1 ) );
136
    if( IS_SYNCHRONOUS )
137
    {
138 138 cwalter
        fprintf( pxOutputFile, "  signal sig_data_int :  std_logic_vector(%d downto 0 ) );\n",
139 137 cwalter
                 ( DATA_BITS - 1 ) );
140
        fprintf( pxOutputFile, "begin\n" );
141 138 cwalter
        fprintf( pxOutputFile, "  data <= sig_data_int\n" );
142 137 cwalter
        fprintf( pxOutputFile, "process (clk)\n" );
143
        fprintf( pxOutputFile, "  if clk'event and clk = '1' then\n" );
144 138 cwalter
        fprintf( pxOutputFile, "    sig_data_int <= sig_data_next;\n" );
145 137 cwalter
        fprintf( pxOutputFile, "  end if;\n" );
146
        fprintf( pxOutputFile, "end process;\n" );
147
    }
148
    else
149
    {
150
        fprintf( pxOutputFile, "begin\n" );
151 138 cwalter
        fprintf( pxOutputFile, "  data <= sig_data_next;\n" );
152 137 cwalter
    }
153
    fprintf( pxOutputFile, "\n" );
154
    fprintf( pxOutputFile, "  process( addr )\n" );
155
    fprintf( pxOutputFile, "  begin\n" );
156
    fprintf( pxOutputFile, "    case addr is\n" );
157
}
158
 
159
void
160
vWriteArchitectureData( FILE * pxOutputFile, const unsigned char *pucData, int iNBytes )
161
{
162
    static char     arucBuffer[( DATA_BITS / 4 ) + 1];
163
    static unsigned int uiProgrammCounter = 0;
164
    int             iBytePos, i;
165
 
166
    for( iBytePos = 0; iBytePos < iNBytes; )
167
    {
168
        for( i = 0; i < ( DATA_BITS / 4 ); i += 2, iBytePos++ )
169
        {
170
            sprintf( &arucBuffer[i], "%02X", pucData[iBytePos] );
171
        }
172 138 cwalter
        fprintf( pxOutputFile, "      when x\"%s\" => sig_data_next <= x\"%*s\";\n",
173 137 cwalter
                 pcAddress2Hex( uiProgrammCounter ), DATA_BITS / 4, arucBuffer );
174
        uiProgrammCounter += PC_INCREMENT;
175
    }
176
}
177
 
178
void
179
vWriteArchitectureFooter( FILE * pxOutputFile )
180
{
181 138 cwalter
    fprintf( pxOutputFile, "      when others  => sig_data_next <= ( others => '0' );\n" );
182 137 cwalter
    fprintf( pxOutputFile, "    end case;\n" );
183
    fprintf( pxOutputFile, "  end process;\n" );
184
    fprintf( pxOutputFile, "\n" );
185 138 cwalter
    fprintf( pxOutputFile, "end %s;", VHDL_ARCHITECTURE_NAME );
186 137 cwalter
}
187
 
188
const char     *
189
pcAddress2Hex( unsigned int uiHexValue )
190
{
191
    static char     arucBuffer[ADDRESS_BITS / 4 + 1];
192
 
193
    snprintf( arucBuffer, NELEMS( arucBuffer ), "%0*x", DATA_BITS / 4, uiHexValue );
194
    return arucBuffer;
195
}

powered by: WebSVN 2.1.0

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