1 |
2 |
__alexs__ |
#!/usr/bin/perl -w
|
2 |
|
|
use strict;
|
3 |
|
|
use warnings;
|
4 |
|
|
|
5 |
|
|
# revision history
|
6 |
|
|
# 05/2014 AS 1.0 initial
|
7 |
|
|
|
8 |
|
|
#################################################################################
|
9 |
|
|
use Fatal qw( open );
|
10 |
|
|
use Switch;
|
11 |
|
|
|
12 |
|
|
#################################################################################
|
13 |
|
|
# _____ ____ _ _ _____ _______ _ _ _______ _____
|
14 |
|
|
# / ____/ __ \| \ | |/ ____|__ __|/\ | \ | |__ __/ ____|
|
15 |
|
|
#| | | | | | \| | (___ | | / \ | \| | | | | (___
|
16 |
|
|
#| | | | | | . ` |\___ \ | | / /\ \ | . ` | | | \___ \
|
17 |
|
|
#| |___| |__| | |\ |____) | | |/ ____ \| |\ | | | ____) |
|
18 |
|
|
# \_____\____/|_| \_|_____/ |_/_/ \_\_| \_| |_| |_____/
|
19 |
|
|
#################################################################################
|
20 |
|
|
#
|
21 |
|
|
# command line parameter
|
22 |
|
|
#
|
23 |
|
|
my $input = ""; # input file
|
24 |
|
|
my $output = ""; # output file
|
25 |
|
|
|
26 |
|
|
my $txt_format = "'4/1 \"%02X\"\"\\n\"'"; # default output .txt format
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
#my $length = shift; # length of the output file entries
|
30 |
|
|
#my $start = shift; # start address
|
31 |
|
|
|
32 |
|
|
#
|
33 |
|
|
# line configurations
|
34 |
|
|
#
|
35 |
|
|
my $LINE_LIMIT = 32; # line address limit
|
36 |
|
|
|
37 |
|
|
#
|
38 |
|
|
# DRAM
|
39 |
|
|
#
|
40 |
|
|
my $DRAM_BANKS = 4; # number of DRAM banks
|
41 |
|
|
my $DRAM_ZERO_WORD = "00000000"; # for zero padding
|
42 |
|
|
|
43 |
|
|
my $DRAM_3D_BANKS = 8; # number of 3D DRAM banks
|
44 |
|
|
my $DRAM_3D_WORD = 4; # 4 * 32 bit = 3D DRAM word
|
45 |
|
|
my $DRAM_ND_WORD = 32; # 32 * 32 bit = 1024 No Delay DRAM word
|
46 |
|
|
|
47 |
|
|
#################################################################################
|
48 |
|
|
#__ __ _____ _____ ____ _ ______ _____
|
49 |
|
|
#\ \ / /\ | __ \|_ _| /\ | _ \| | | ____|/ ____|
|
50 |
|
|
# \ \ / / \ | |__) | | | / \ | |_) | | | |__ | (___
|
51 |
|
|
# \ \/ / /\ \ | _ / | | / /\ \ | _ <| | | __| \___ \
|
52 |
|
|
# \ / ____ \| | \ \ _| |_ / ____ \| |_) | |____| |____ ____) |
|
53 |
|
|
# \/_/ \_\_| \_\_____/_/ \_\____/|______|______|_____/
|
54 |
|
|
#################################################################################
|
55 |
|
|
#
|
56 |
|
|
# command line options
|
57 |
|
|
#
|
58 |
|
|
my %par_arguments =(
|
59 |
|
|
-hex => 4, # .bin -> .txt
|
60 |
|
|
-mem => 5, # .txt -> .mem
|
61 |
|
|
-txt => 5, # .mem -> .txt
|
62 |
|
|
-bin => 3 # .txt -> .bin
|
63 |
|
|
);
|
64 |
|
|
my $parameter = "";
|
65 |
|
|
my %parameters = ();
|
66 |
|
|
|
67 |
|
|
#
|
68 |
|
|
# parsing
|
69 |
|
|
#
|
70 |
|
|
my $line = ""; # current line
|
71 |
|
|
my @bytes = (); # current bytes
|
72 |
|
|
my @bytes_rest = (); # rest from previous extraction
|
73 |
|
|
my $start_flag = 0; # no start option
|
74 |
|
|
my $length_flag = 0; # length limit indication
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
#
|
78 |
|
|
# address
|
79 |
|
|
#
|
80 |
|
|
my $addr = 0; # current address
|
81 |
|
|
my $hex_addr = ""; # hex string of address
|
82 |
|
|
my $cur_length = 0; # current length
|
83 |
|
|
my $line_addr = 0; # line address
|
84 |
|
|
|
85 |
|
|
#
|
86 |
|
|
# parsing
|
87 |
|
|
#
|
88 |
|
|
my @chars = (); # hex chars from input line
|
89 |
|
|
my $idx = 0; # index of char array
|
90 |
|
|
my $i = 0; # index of first char
|
91 |
|
|
my $j = 0; # index of second char
|
92 |
|
|
|
93 |
|
|
#
|
94 |
|
|
# length
|
95 |
|
|
#
|
96 |
|
|
my $filelength = 0; # file length -> does not work inside switch
|
97 |
|
|
my $hex_length = ""; # file length in hex format
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
#################################################################################
|
101 |
|
|
# ____ ______ _____ _____ _ _
|
102 |
|
|
#| _ \| ____/ ____|_ _| \ | |
|
103 |
|
|
#| |_) | |__ | | __ | | | \| |
|
104 |
|
|
#| _ <| __|| | |_ | | | | . ` |
|
105 |
|
|
#| |_) | |___| |__| |_| |_| |\ |
|
106 |
|
|
#|____/|______\_____|_____|_| \_|
|
107 |
|
|
#################################################################################
|
108 |
|
|
# ____ ____ _ _ _ _ ____ _ _ ___ _ _ _ _ ____ ___ ____ ____ ____ _ _ ____ ___ ____ ____
|
109 |
|
|
# | | | |\/| |\/| |__| |\ | | \ | | |\ | |___ |__] |__| |__/ |__| |\/| |___ | |___ |__/
|
110 |
|
|
# |___ |__| | | | | | | | \| |__/ |___ | | \| |___ | | | | \ | | | | |___ | |___ | \
|
111 |
|
|
#
|
112 |
|
|
# first parameter = direction
|
113 |
|
|
#
|
114 |
|
|
$parameter = shift;
|
115 |
|
|
|
116 |
|
|
if( exists($par_arguments{$parameter}) ){ # check for valid parameter
|
117 |
|
|
|
118 |
|
|
# ############# DIRECTORY ############################
|
119 |
|
|
$parameters{DIR} = $parameter; $parameters{IDX} = 1; # set directory and start index
|
120 |
|
|
|
121 |
|
|
# ############# OPTIONS ##############################
|
122 |
|
|
$parameter = shift; # get next parameter
|
123 |
|
|
while(defined $parameter){ # if exists
|
124 |
|
|
$parameters{$parameters{IDX}} = $parameter; # store it with current index
|
125 |
|
|
$parameters{IDX}++; # update index
|
126 |
|
|
$parameter = shift; # get next parameter
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
# ############# SANITY CHECK #########################
|
130 |
|
|
if( $parameters{IDX} < 2 ){ # at least directory and input file are expected
|
131 |
|
|
print "WARNING: Incorrect number of arguments, exit!\n";
|
132 |
|
|
print_help();
|
133 |
|
|
exit 1;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
# ############### INPUT FILE #########################
|
137 |
|
|
$parameters{INPUT} = $parameters{1}; # set input file
|
138 |
|
|
$filelength = -s $parameters{INPUT}; # get file length
|
139 |
|
|
|
140 |
|
|
}else{ # parameter invalid
|
141 |
|
|
print "WARNING: No parameter \"$parameter\" defined, exit!\n";
|
142 |
|
|
print_help();
|
143 |
|
|
exit 1;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
# ___ _ ____ ____ ____ ___ ____ ____ _ _ ____ _ _ _ _ ___ ____ _ _
|
148 |
|
|
# | \ | |__/ |___ | | | | |__/ \_/ [__ | | | | | | |__|
|
149 |
|
|
# |__/ | | \ |___ |___ | |__| | \ | ___] |_|_| | | |___ | |
|
150 |
|
|
#
|
151 |
|
|
# different routines depending on directory
|
152 |
|
|
#
|
153 |
|
|
switch( $parameters{DIR} ){
|
154 |
|
|
# ############################# FROM BIN TO .TXT ##########################################
|
155 |
|
|
# ___ _ _ _ ___ _ _ ___
|
156 |
|
|
# |__] | |\ | __ | \/ |
|
157 |
|
|
# |__] | | \| | _/\_ |
|
158 |
|
|
case "-hex" {
|
159 |
|
|
|
160 |
|
|
if( $parameters{IDX} == 4 ){ $parameters{FORMAT} = $parameters{3}; # check for format option
|
161 |
|
|
}else{ $parameters{FORMAT} = $txt_format;} # else default
|
162 |
|
|
|
163 |
|
|
# write file length at first 4 bytes
|
164 |
|
|
# print -s $parameters{INPUT};
|
165 |
|
|
#$bank_idx = -s $parameters{INPUT};
|
166 |
|
|
#print "$filelength\n";
|
167 |
|
|
$hex_length = sprintf( '%08x', $filelength );
|
168 |
|
|
print "$hex_length\n";
|
169 |
|
|
|
170 |
|
|
system( "hexdump -v -e $parameters{FORMAT} $parameters{INPUT}" ); # call extern function
|
171 |
|
|
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
# ############################# FROM .TXT TO .MEM #########################################
|
175 |
|
|
case "-mem" {
|
176 |
|
|
|
177 |
|
|
if( $parameters{IDX} == 3 ){ $addr = $parameters{2}; $start_flag = 4;} # check for start address
|
178 |
|
|
if( $parameters{IDX} == 4 ){ $addr = $parameters{2}; $start_flag = 4; # check for length content
|
179 |
|
|
$length_flag = $parameters{3};}
|
180 |
|
|
|
181 |
|
|
# open input file
|
182 |
|
|
open IN_FILE, "< $parameters{INPUT}"
|
183 |
|
|
or die "ERROR: cannot open $parameters{INPUT}! $!\n";
|
184 |
|
|
|
185 |
|
|
# ########## INPUT FILE READ #########################
|
186 |
|
|
while(<IN_FILE>){
|
187 |
|
|
|
188 |
|
|
#if( $start_flag ){ # start address is set
|
189 |
|
|
$hex_addr = sprintf( '@%x', $addr ); # format output address string
|
190 |
|
|
#}
|
191 |
|
|
|
192 |
|
|
$line = $_; # get current line
|
193 |
|
|
$line =~ s/(.{2})/$1 /g; # insert space every 2 characters = 1 byte
|
194 |
|
|
print "$hex_addr $line"; # print it out with given address string
|
195 |
|
|
|
196 |
|
|
$cur_length += 4; # update length
|
197 |
|
|
#$addr += $start_flag; # update address
|
198 |
|
|
$addr += 4;
|
199 |
|
|
|
200 |
|
|
if( $length_flag ){ # check length
|
201 |
|
|
if( $cur_length >= $length_flag ){ last; } # cancel if reached
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
close IN_FILE;
|
206 |
|
|
|
207 |
|
|
# ########### LENGTH ZERO PADDING ####################
|
208 |
|
|
if( $length_flag ){ # only for given length
|
209 |
|
|
|
210 |
|
|
if( $cur_length < $length_flag ){ print "\n"; } # add newline
|
211 |
|
|
|
212 |
|
|
while( $cur_length < $length_flag ){ # and not arrived length
|
213 |
|
|
|
214 |
|
|
if( $start_flag ){ # if address should also be printed out
|
215 |
|
|
$hex_addr = sprintf( '@%x', $addr ); # format address string
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
$line = "00 00 00 00\n"; # output line ist constant
|
219 |
|
|
print "$hex_addr $line"; # print it out
|
220 |
|
|
|
221 |
|
|
$cur_length += 4; # update length
|
222 |
|
|
$addr += $start_flag; # update address
|
223 |
|
|
|
224 |
|
|
} # while
|
225 |
|
|
} # length_flag
|
226 |
|
|
} # -hex
|
227 |
|
|
|
228 |
|
|
# ############################## FROM .MEM TO .TXT #########################################
|
229 |
|
|
case "-txt" {
|
230 |
|
|
|
231 |
|
|
if( $parameters{IDX} == 3 ){ $addr = $parameters{2}; $start_flag = 4;} # check for start address option
|
232 |
|
|
if( $parameters{IDX} == 4 ){ $addr = $parameters{2}; $start_flag = 4; # check for length options
|
233 |
|
|
$length_flag = $parameters{3};}
|
234 |
|
|
|
235 |
|
|
$filelength = 0; # reset file length information
|
236 |
|
|
|
237 |
|
|
# open input file
|
238 |
|
|
open IN_FILE, "< $parameters{INPUT}"
|
239 |
|
|
or die "ERROR: cannot open $parameters{INPUT}! $!\n";
|
240 |
|
|
|
241 |
|
|
# ############ INPUT FILE READ ######################
|
242 |
|
|
LINE: while(<IN_FILE>){
|
243 |
|
|
|
244 |
|
|
@bytes = split( /\s+/, $_ ); # extract bytes
|
245 |
|
|
|
246 |
|
|
if( $bytes[0] =~ /^\@(\d+)/ ){ # line contains address information = valid data
|
247 |
|
|
|
248 |
|
|
unless( $filelength ){ # file length is not extracted
|
249 |
|
|
$filelength = hex( "$bytes[1]$bytes[2]$bytes[3]$bytes[4]" ); # read file length
|
250 |
|
|
splice( @bytes, 1, 4 ); # remove this information from output
|
251 |
|
|
$length_flag = $filelength + 1; # set length flag
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
splice( @bytes, 0, 1, @bytes_rest ); # replace addres field with reset of previous extraction
|
255 |
|
|
@bytes_rest = (); # clear this array
|
256 |
|
|
|
257 |
|
|
while( (scalar @bytes) > 3 ){ # there are more than 3 bytes in the working array
|
258 |
|
|
|
259 |
|
|
print "$bytes[0]$bytes[1]$bytes[2]$bytes[3]\n"; # print word
|
260 |
|
|
splice( @bytes, 0, 4 ); # cut it
|
261 |
|
|
|
262 |
|
|
$cur_length += 4; # update length
|
263 |
|
|
|
264 |
|
|
if( $length_flag ){ # if length is set
|
265 |
|
|
if( $cur_length > $length_flag ){ last LINE; } # examine and exit if reached
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
splice( @bytes_rest, 0, 0, @bytes ); # update rest
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
close IN_FILE;
|
275 |
|
|
|
276 |
|
|
# ############# LENGTH ZERO PADDING #################
|
277 |
|
|
if( $length_flag ){ # length is given
|
278 |
|
|
while( $cur_length < $length_flag ){ # and not already reached
|
279 |
|
|
print "00000000\n"; # print zero out
|
280 |
|
|
$cur_length += 4; # update length
|
281 |
|
|
}
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
# ################################# FROM .TXT TO .BIN #####################################
|
287 |
|
|
case "-bin" {
|
288 |
|
|
|
289 |
|
|
if( $parameters{IDX} == 3){ # check for output file name
|
290 |
|
|
$parameters{OUTPUT} = $parameters{2}; # set output file name
|
291 |
|
|
}else{ # output file name is mandatory
|
292 |
|
|
print "ERROR: Output file should be specivied on bin mode!\n";
|
293 |
|
|
print_help();
|
294 |
|
|
exit 1;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
|
|
# open input file
|
298 |
|
|
open IN_FILE, "< $parameters{INPUT}"
|
299 |
|
|
or die "ERROR: cannot open $parameters{INPUT}! $!\n";
|
300 |
|
|
#open output file
|
301 |
|
|
open OUT_FILE, "> $parameters{OUTPUT}"
|
302 |
|
|
or die "ERROR: cannot open $parameters{OUTPUT}! $!\n";
|
303 |
|
|
|
304 |
|
|
print "$parameters{INPUT} -> $parameters{OUTPUT}\n"; # print user information
|
305 |
|
|
|
306 |
|
|
binmode(OUT_FILE); # set mode
|
307 |
|
|
|
308 |
|
|
# ############# INPUT FILE READ ###################
|
309 |
|
|
while(<IN_FILE>){
|
310 |
|
|
$line = $_; # get current line
|
311 |
|
|
$line =~ s/(.{2})/$1 /g; # insert space every 2 characters = 1 byte
|
312 |
|
|
@bytes = split( /\s/, $line ); # extract bytes
|
313 |
|
|
foreach( @bytes ){ print OUT_FILE pack( 'C1', hex($_) ); } # write these bytes to output file
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
close OUT_FILE;
|
317 |
|
|
close IN_FILE;
|
318 |
|
|
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
# ################################ UNKNOWN DIRECTORY ######################################
|
322 |
|
|
else {
|
323 |
|
|
print "WARNING: Undefined direction $parameters{DIR}, nothing to do!\n";
|
324 |
|
|
print_help();
|
325 |
|
|
exit 0;
|
326 |
|
|
}
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
exit 0;
|
330 |
|
|
#################################################################################
|
331 |
|
|
# _____ _ _ ____ ______ _ _ _ _ _____ _______ _____ ____ _ _ _____
|
332 |
|
|
# / ____| | | | _ \| ____| | | | \ | |/ ____|__ __|_ _/ __ \| \ | |/ ____|
|
333 |
|
|
# | (___ | | | | |_) | |__ | | | | \| | | | | | || | | | \| | (___
|
334 |
|
|
# \___ \| | | | _ <| __| | | | | . ` | | | | | || | | | . ` |\___ \
|
335 |
|
|
# ____) | |__| | |_) | | | |__| | |\ | |____ | | _| || |__| | |\ |____) |
|
336 |
|
|
# |_____/ \____/|____/|_| \____/|_| \_|\_____| |_| |_____\____/|_| \_|_____/
|
337 |
|
|
#################################################################################
|
338 |
|
|
#
|
339 |
|
|
# help string
|
340 |
|
|
#
|
341 |
|
|
|
342 |
|
|
sub print_help{
|
343 |
|
|
print STDERR "usage: file2out.pl DIR INPUT [OUTPUT]
|
344 |
|
|
creates formatted data depending on DIR
|
345 |
|
|
|
346 |
|
|
Examples:
|
347 |
|
|
file2out.pl -hex test.bmp > data.txt
|
348 |
|
|
file2out.pl -mem memory.txt > memory_in.mem
|
349 |
|
|
file2out.pl -txt memory_ou.mem > memory_out.txt
|
350 |
|
|
file2out.pl -bin memory_out.txt test_out.jp2
|
351 |
|
|
|
352 |
|
|
DIR can be:
|
353 |
|
|
-hex .bin -> .txt
|
354 |
|
|
-mem .txt -> .mem
|
355 |
|
|
-txt .mem -> .txt
|
356 |
|
|
-bin .txt -> .bin OUTPUT is mandatory here
|
357 |
|
|
";
|
358 |
|
|
}
|