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

Subversion Repositories copyblaze

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ameziti
/*
2
 *  Copyright © 2003..2008 : Henk van Kampen <henk@mediatronix.com>
3
 *
4
 *      This file is part of pBlazASM.
5
 *
6
 *  pBlazASM is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  pBlazASM is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with pBlazASM.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
 
20
/**
21
 * pBlaze assembler
22
 * @file pBlazASM.c
23
 * @author Henk van Kampen
24
 */
25
 
26
#include <stdlib.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <malloc.h>
30
#include <unistd.h>
31
 
32
#include "pbTypes.h"
33
#include "pbParser.h"
34
#include "pbLibgen.h"
35
 
36
#ifdef TCC
37
#include "getopt.h"
38
#endif
39
 
40
/**
41
 * usage prints usage text
42
 * @param text application name
43
 */
44
static void usage( char * text ) {
45
        printf( "\n%s - %s\n", text, "Picoblaze Assembler V1.2" ) ;
46
        printf( "\nUSAGE:\n" ) ;
47
        printf( "   pBlazASM [-m[<MEMfile>]] [-l[<LSTfile>]] [-k] [-v] [-f] <input file> <input file> <input file> ...\n"
48
                "   where:\n"
49
                "         -m/-M   creates a MEM file (not in combo with -x)\n"
50
                "         -x/-X   creates a HEX file (not in combo with -m)\n"
51
                "         -l      creates a LST file\n"
52
                "         -k      to select KCPSM mode with limited expression handling\n"
53
                "         -v      generates verbose reporting\n"
54
                "         -f      with -l creates a list file without code, to replace the source\n" ) ;
55
        printf( "\nNote: All (max 255) input files are assembled to one output.\n" ) ;
56
}
57
 
58
/**
59
 * main entry
60
 * processes command line and
61
 * calls actual assembler()
62
 */
63
int main( int argc, char **argv ) {
64
        char * src_filenames[ 256 ] =
65
                { NULL } ;
66
        char mem_filename[ 256 ] =
67
                { 0 } ;
68
        char list_filename[ 256 ] =
69
                { 0 } ;
70
        char * pfile, *ppath ;
71
        int result = 0 ;
72
        int nInputfile = 0 ;
73
 
74
        // KCPSM mode, accepts 'NAMEREG' etc
75
        bool bKCPSM_mode = false ;
76
        bool bList_mode = true ;
77
        bool bOptErr = false ;
78
        bool bWantMEM = false ;
79
        bool bWantHEX = false ;
80
        bool bWantLST = false ;
81
        bool bVerbose = false ;
82
        bool bWantZEROs = false ;
83
 
84
        extern char * optarg ;
85
        extern int optind, optopt ;
86
        int optch ;
87
 
88
        while ( ( optch = getopt( argc, argv, "fhkl::m::M::vx::X::" ) ) != -1 ) {
89
                switch ( optch ) {
90
                case 'f' :
91
                        bList_mode = false ;
92
                        break ;
93
                case 'h' :
94
                        bOptErr = true ;
95
                        break ;
96
                case 'm' :
97
                        if ( bWantHEX ) {
98
                                fprintf( stderr, "? conflicting option -%c\n", optch ) ;
99
                                bOptErr = true ;
100
                        } else {
101
                                bWantMEM = true ;
102
                                if ( optarg != NULL )
103
                                        strcpy( mem_filename, optarg ) ;
104
                    }
105
                        break ;
106
                case 'x' :
107
                        if ( bWantMEM ) {
108
                                fprintf( stderr, "? conflicting option -%c\n", optch ) ;
109
                                bOptErr = true ;
110
                        } else {
111
                                bWantHEX = true ;
112
                                if ( optarg != NULL )
113
                                        strcpy( mem_filename, optarg ) ;
114
                        }
115
                        break ;
116
                case 'M' :
117
                        if ( bWantHEX ) {
118
                                fprintf( stderr, "? conflicting option -%c\n", optch ) ;
119
                                bOptErr = true ;
120
                        } else {
121
                                bWantMEM = true ;
122
                                bWantZEROs = true ;
123
                                if ( optarg != NULL )
124
                                        strcpy( mem_filename, optarg ) ;
125
                    }
126
                        break ;
127
                case 'X' :
128
                        if ( bWantMEM ) {
129
                                fprintf( stderr, "? conflicting option -%c\n", optch ) ;
130
                                bOptErr = true ;
131
                        } else {
132
                                bWantHEX = true ;
133
                                bWantZEROs = true ;
134
                                if ( optarg != NULL )
135
                                        strcpy( mem_filename, optarg ) ;
136
                        }
137
                        break ;
138
                case 'l' :
139
                        bWantLST = true ;
140
                        if ( optarg != NULL )
141
                                strcpy( list_filename, optarg ) ;
142
                        break ;
143
                case 'k' :
144
                        bKCPSM_mode = true ;
145
                        break ;
146
                case 'v' :
147
                        bVerbose = true ;
148
                        break ;
149
                case ':' :
150
                        fprintf( stderr, "? missing option -%c\n", optopt ) ;
151
                        bOptErr = true ;
152
                        break ;
153
                default :
154
                        fprintf( stderr, "? unknown option -%c\n", optopt ) ;
155
                        bOptErr = true ;
156
                        break ;
157
                }
158
        }
159
 
160
        if ( bOptErr ) {
161
                usage( basename( argv[ 0 ] ) ) ;
162
                result = -1 ;
163
                goto finally ;
164
        }
165
 
166
        if ( bVerbose && bKCPSM_mode )
167
                fprintf( stdout, "! KCPSM3 compatible mode selected\n" ) ;
168
 
169
        if ( argv[ optind ] == NULL ) {
170
                fprintf( stderr, "? source file(s) missing\n" ) ;
171
                usage( basename( argv[ 0 ] ) ) ;
172
                result = -1 ;
173
                goto finally ;
174
        }
175
 
176
        for ( nInputfile = 0 ; argv[ optind ] != NULL && nInputfile < 256 ; nInputfile += 1, optind += 1 ) {
177
                src_filenames[ nInputfile ] = calloc( strlen( argv[ optind ] ) + 16, sizeof(char) ) ;
178
                strcpy( src_filenames[ nInputfile ], argv[ optind ] ) ;
179
 
180
                if ( strrchr( src_filenames[ nInputfile ], '.' ) == NULL )
181
                        strcat( src_filenames[ nInputfile ], ".psm" ) ;
182
                if ( bVerbose )
183
                        fprintf( stdout, "! Sourcefile: %s\n", src_filenames[ nInputfile ] ) ;
184
        }
185
 
186
        if ( bWantMEM || bWantHEX ) {
187
                if ( strlen( mem_filename ) == 0 ) {
188
                        pfile = filename( src_filenames[ nInputfile - 1 ] ) ;
189
                        ppath = dirname( src_filenames[ nInputfile - 1 ] ) ;
190
                        strcpy( mem_filename, ppath ) ;
191
                        strcat( mem_filename, "/" ) ;
192
                        strcat( mem_filename, pfile ) ;
193
                        strcat( mem_filename, bWantHEX ? ".hex" : ".mem" ) ;
194
                        free( ppath ) ;
195
                        free( pfile ) ;
196
                }
197
                if ( strrchr( mem_filename, '.' ) == NULL )
198
                        strcat( mem_filename, bWantHEX ? ".hex" : ".mem" ) ;
199
                if ( bVerbose )
200
                        fprintf( stdout, "! MEM file: %s\n", mem_filename ) ;
201
        }
202
 
203
        if ( bWantLST ) {
204
                if ( strlen( list_filename ) == 0 ) {
205
                        pfile = filename( src_filenames[ nInputfile - 1 ] ) ;
206
                        ppath = dirname( src_filenames[ nInputfile - 1 ] ) ;
207
                        strcpy( list_filename, ppath ) ;
208
                        strcat( list_filename, "/" ) ;
209
                        strcat( list_filename, pfile ) ;
210
                        strcat( list_filename, ".lst" ) ;
211
                        free( ppath ) ;
212
                        free( pfile ) ;
213
                }
214
                if ( strrchr( list_filename, '.' ) == NULL )
215
                        strcat( list_filename, ".lst" ) ;
216
                if ( bVerbose )
217
                        fprintf( stdout, "! LST file: %s\n", list_filename ) ;
218
        }
219
 
220
        if ( assembler( src_filenames, mem_filename, list_filename, bKCPSM_mode, bList_mode, bWantHEX, bWantZEROs ) )
221
                result = 0 ;
222
        else
223
                result = -1 ;
224
 
225
        finally: {
226
                int i ;
227
 
228
                for ( i = 0 ; i < nInputfile ; i += 1 ) {
229
                        free( src_filenames[ i ] ) ;
230
                }
231
        }
232
        exit( result ) ;
233
}

powered by: WebSVN 2.1.0

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