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

Subversion Repositories openfire2

[/] [openfire2/] [trunk/] [utils/] [pc.pl] - Blame information for rev 8

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

Line No. Rev Author Line
1 2 toni32
####*****************************************************************************************
2
####**
3
####**  Disclaimer: LIMITED WARRANTY AND DISCLAMER. These designs are 
4
####**              provided to you "as is". Xilinx and its licensors make and you 
5
####**              receive no warranties or conditions, express, implied, statutory 
6
####**              or otherwise, and Xilinx specifically disclaims any implied 
7
####**              warranties of merchantability, non-infringement, or fitness for a 
8
####**              particular purpose. Xilinx does not warrant that the functions 
9
####**              contained in these designs will meet your requirements, or that the
10
####**              operation of these designs will be uninterrupted or error free, or 
11
####**              that defects in the Designs will be corrected. Furthermore, Xilinx 
12
####**              does not warrant or make any representations regarding use or the 
13
####**              results of the use of the designs in terms of correctness, accuracy, 
14
####**              reliability, or otherwise. 
15
####**
16
####**              LIMITATION OF LIABILITY. In no event will Xilinx or its licensors be 
17
####**              liable for any loss of data, lost profits, cost or procurement of 
18
####**              substitute goods or services, or for any special, incidental, 
19
####**              consequential, or indirect damages arising from the use or operation 
20
####**              of the designs or accompanying documentation, however caused and on 
21
####**              any theory of liability. This limitation will apply even if Xilinx 
22
####**              has been advised of the possibility of such damage. This limitation 
23
####**              shall apply not-withstanding the failure of the essential purpose of 
24
####**              any limited remedies herein. 
25
####**
26
####*****************************************************************************************
27
 
28
print "**********************************\n";
29
print "*\n";
30
print "*        Preliminary script v1.01\n";
31
print "*        Author: Stephan Neuhold\n";
32
print "*\n";
33
print "**********************************\n\n";
34
#
35
#
36
#
37
#
38
################################
39
################################
40
#Default commandline switch settings
41
%default = (    -f              =>      mcs,
42
                                -swap   =>      off,
43
                                -uf             =>      user.txt,
44
                                -pf             =>      prom.mcs);
45
#
46
#
47
#
48
#
49
################################
50
################################
51
#Define USAGE of script
52
sub usage
53
{
54
        if ($argument eq "-f")
55
        {
56
                print "You have not specified a PROM file format!\n"
57
        }
58
        elsif ($argument eq "-swap")
59
        {
60
                print "You have not specified if bits should be swapped or not!\n"
61
        }
62
        elsif ($argument eq "-uf")
63
        {
64
                print "You have not specified a file containing user data!\n"
65
        }
66
        elsif ($argument eq "-pf")
67
        {
68
                print "You have not specifed a PROM file!\n"
69
        }
70
        print "usage:\n
71
                pc.pl [-f : PROM file format {mcs|hex}] [-swap : bits should be swapped {on|off}] [-uf : user data file {<filename.ext>}] [-pf : PROM file {filename.ext}]\n
72
                -f = PROM file format used
73
                        mcs => Intel file format
74
                        hex => Simple hex file format\n
75
 
76
                -swap = Specify if bits are to be swapped
77
                        on => swaps bits in every byte
78
                        off => bits are not swapped\n
79
 
80
                -uf = File containing user data to be added to PROM file\n
81
 
82
                -pf = PROM file to which user data is to be added\n\n";
83
}
84
#
85
#
86
#
87
#
88
################################
89
################################
90
#Place command line arguments into hash                         
91
%commandline_arguments = @ARGV;
92
#
93
#
94
#
95
#
96
################################
97
################################
98
#Check if all necessary arguments have been given and store them seperately
99
foreach $argument(keys (%default))
100
{
101
        if (exists($commandline_arguments{$argument}))
102
        {
103
                if ($argument eq "-f")
104
                {
105
                        $format = $commandline_arguments{$argument};
106
                }
107
                elsif ($argument eq "-swap")
108
                {
109
                        $do_swap = $commandline_arguments{$argument};
110
                }
111
                elsif ($argument eq "-uf")
112
                {
113
                        $user_file = $commandline_arguments{$argument};
114
                }
115
                elsif ($argument eq "-pf")
116
                {
117
                        $prom_file = $commandline_arguments{$argument};
118
                }
119
        }
120
        else
121
        {
122
                print usage($argument);
123
                exit;
124
        }
125
}
126
#
127
#
128
#
129
#
130
################################
131
################################
132
#Initialise all variables
133
$prom_line_number = 0;
134
$user_line_number = 0;
135
#
136
#
137
#
138
#
139
################################
140
################################
141
#Print settings used
142
print "\n\n";
143
print "Running script with following settings:\n";
144
print " PROM file format        ==>     $format\n";
145
print " Bit swapping            ==>     $do_swap\n";
146
print " User data file          ==>     $user_file\n";
147
print " Original PROM file      ==>     $prom_file\n\n\n";
148
print "New PROM file is         ==>     new_$prom_file\n\n";
149
#
150
#
151
#
152
#
153
################################
154
################################
155
#Open files and begin processing
156
open (PROM_FILE, "<$prom_file") || die "Cannot open file $prom_file: $!";
157
open (NEW_PROM_FILE, ">new_$prom_file") || die "Cannot open file new_$prom_file: $!";
158
open (USER_DATA, "<$user_file") || die "Cannot open file $user_file: $!";
159
while (<PROM_FILE>)
160
{
161
        $current_prom_line = $_;
162
 
163
        #Process "mcs" file format
164
        if ($format eq "mcs")
165
        {
166
                print "Copying original PROM line number $prom_line_number...           \r";
167
                $prom_line_number = $prom_line_number + 1;
168
 
169
                #Get the last information for record type "04"
170
                get_current_mcs_prom_line_data();
171
 
172
                #If the current PROM line is not the last line in the PROM
173
        #file then just print it into the new PROM file. If the line
174
        #is the last line in the PROM file then we take the previous
175
        #line and find its address, so we can add a new line, with
176
        #new data at the next address. Also, we calculate a checksum
177
        #for the new line in the PROM file and append that to the new
178
        #PROM line.
179
                if ($current_prom_line =~ /\:00000001FF$/)
180
                {
181
                        print "\n";
182
                        #New address offset starts at 0
183
                        $new_address_offset = 0;
184
                        #Add user data to PROM file
185
                        while (<USER_DATA>)
186
                        {
187
                                chomp;
188
                                if ($_ =~ /^\#/)
189
                                {
190
                                        #Ignoring comments
191
                                        print "Ignoring comment\r";
192
                                }
193
                                elsif ($_ =~ /\#/g)
194
                                {
195
                                        @split_user_line = split(/\#/, $_);
196
                                        $current_user_line = @split_user_line[0];
197
                                        print "Processing USER line $user_line_number...                \r";
198
                                        $user_line_number = $user_line_number + 1;
199
                                        #Calculate the new address
200
                                        get_mcs_address();
201
                                }
202
                                else
203
                                {
204
                                        $current_user_line = $_;
205
                                        print "Processing USER line $user_line_number...                \r";
206
                                        $user_line_number = $user_line_number + 1;
207
                                        #Calculate the new address
208
                                        get_mcs_address();
209
                                }
210
                        }
211
                        print NEW_PROM_FILE $current_prom_line;
212
                }
213
                else
214
                {
215
                        #Print the line to the new PROM file unchanged
216
                        print NEW_PROM_FILE $current_prom_line;
217
                        #Store the current line for use in next iteration
218
                        $previous_prom_line = $current_prom_line;
219
                }
220
        }
221
        elsif ($format eq "hex")
222
        {
223
            #Print the original PROM file contents to the new PROM file.
224
            if ($do_swap eq "on")
225
            {
226
                $current_prom_line = $_;
227
                print "Copying original PROM line number $prom_line_number...\r";
228
                        $prom_line_number = $prom_line_number + 1;
229
                print NEW_PROM_FILE $current_prom_line;
230
                print "\n";
231
                while (<USER_DATA>)
232
                {
233
                        if ($_ =~ /^\#/)
234
                        {
235
                                print "Ignoring comment\r";
236
                        }
237
                        elsif ($_ =~ /\#/g)
238
                        {
239
                                chomp;
240
                                @split_user_line = split(/\#/, $_);
241
                                $current_user_line = @split_user_line[0];
242
                                print "Processing USER line $user_line_number...                        \r";
243
                                        $user_line_number = $user_line_number + 1;
244
                                        #Extract bytes from user data
245
                                (@bytes_hex) = unpack("A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2", $current_user_line);
246
                                foreach $byte_hex(@bytes_hex)
247
                                {
248
                                        #Convert hex to decimal
249
                                        $byte_dec = hex($byte_hex);
250
                                        #Convert decimal to binary
251
                                        $byte_binary = decimal2binary($byte_dec);
252
                                        #Get the last eight bits
253
                                        $last_eight_bits = substr($byte_binary, -8);
254
                                        #Extract each bit
255
                                        (@last_eight_bits_not_swapped) = unpack("A1 A1 A1 A1 A1 A1 A1 A1", $last_eight_bits);
256
                                        #Bit swap each bit
257
                                        @last_eight_bits_swapped = reverse(@last_eight_bits_not_swapped);
258
                                        $byte_swapped_bin = 0;
259
                                        #Concatenate the bits to form a byte
260
                                        foreach $bit(@last_eight_bits_swapped)
261
                                        {
262
                                                $byte_swapped_bin = $byte_swapped_bin.$bit;
263
                                        }
264
                                        #Convert binary to decimal
265
                                        $byte_swapped_dec = binary2decimal($byte_swapped_bin);
266
                                        #Convert decimal to hex
267
                                        $byte_swapped_hex = sprintf "%lx", $byte_swapped_dec;
268
                                        #Get the last byte
269
                                        $byte_hex = substr($byte_swapped_hex, -2);
270
                                        #If the value is less than 0x0F then concatenate a "0" to the front
271
                                        if ($byte_swapped_dec <= 15)
272
                                        {
273
                                                $byte_hex = "0$byte_hex";
274
                                        }
275
                                        print NEW_PROM_FILE uc("$byte_hex");
276
                                }
277
                                }
278
                                else
279
                                {
280
                                        chomp;
281
                                        $current_user_line = $_;
282
                                        print "Processing USER line $user_line_number...                        \r";
283
                                        $user_line_number = $user_line_number + 1;
284
                                        #Extract bytes from user data
285
                                (@bytes_hex) = unpack("A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2 A2", $current_user_line);
286
                                foreach $byte_hex(@bytes_hex)
287
                                {
288
                                        #Convert hex to decimal
289
                                        $byte_dec = hex($byte_hex);
290
                                        #Convert decimal to binary
291
                                        $byte_binary = decimal2binary($byte_dec);
292
                                        #Get the last eight bits
293
                                        $last_eight_bits = substr($byte_binary, -8);
294
                                        #Extract each bit
295
                                        (@last_eight_bits_not_swapped) = unpack("A1 A1 A1 A1 A1 A1 A1 A1", $last_eight_bits);
296
                                        #Bit swap each bit
297
                                        @last_eight_bits_swapped = reverse(@last_eight_bits_not_swapped);
298
                                        $byte_swapped_bin = 0;
299
                                        #Concatenate the bits to form a byte
300
                                        foreach $bit(@last_eight_bits_swapped)
301
                                        {
302
                                                $byte_swapped_bin = $byte_swapped_bin.$bit;
303
                                        }
304
                                        #Convert binary to decimal
305
                                        $byte_swapped_dec = binary2decimal($byte_swapped_bin);
306
                                        #Convert decimal to hex
307
                                        $byte_swapped_hex = sprintf "%lx", $byte_swapped_dec;
308
                                        #Get the last byte
309
                                        $byte_hex = substr($byte_swapped_hex, -2);
310
                                        #If the value is less than 0x0F then concatenate a "0" to the front
311
                                        if ($byte_swapped_dec <= 15)
312
                                        {
313
                                                $byte_hex = "0$byte_hex";
314
                                        }
315
                                        print NEW_PROM_FILE uc("$byte_hex");
316
                                }
317
                                }
318
                }
319
        }
320
        #Simply print the original file and then the user data
321
        #into the new PROM file.
322
        elsif ($do_swap eq "off")
323
        {
324
                $current_prom_line = $_;
325
                print "Copying original PROM line number $prom_line_number...\r";
326
                        $prom_line_number = $prom_line_number + 1;
327
                print NEW_PROM_FILE $current_prom_line;
328
                print "\n";
329
                while (<USER_DATA>)
330
                {
331
                        if ($_ =~ /^\#/)
332
                        {
333
                                print "Ignoring comment\r";
334
                        }
335
                        elsif ($_ =~ /\#/g)
336
                        {
337
                                chomp;
338
                                @split_user_line = split(/\#/, $_);
339
                                $current_user_line = @split_user_line[0];
340
                                print "Processing USER line $user_line_number...                        \r";
341
                                        $user_line_number = $user_line_number + 1;
342
                                print NEW_PROM_FILE uc("$current_user_line");
343
                                }
344
                                else
345
                                {
346
                                        chomp;
347
                                        print "Processing USER line $user_line_number...                        \r";
348
                                        $user_line_number = $user_line_number + 1;
349
                                $current_user_line = $_;
350
                                print NEW_PROM_FILE uc("$current_user_line");
351
                                }
352
                }
353
        }
354
        }
355
}
356
close (USER_DATA) || die "Cannot close file $user_file: $!";
357
close (PROM_FILE) || die "Cannot close file $prom_file: $!";
358
close (NEW_PROM_FILE) || die "Cannot close file new_$prom_file: $!";
359
print "\nDONE...\n";
360
#
361
#
362
#
363
#
364
################################
365
################################
366
#Get the data from the current prom line
367
sub get_current_mcs_prom_line_data
368
{
369
        ($start_character, $byte_count_hex) = unpack("A1 A2", $current_prom_line);
370
        #Convert byte count from hex to decimal
371
        $byte_count_dec = hex($byte_count_hex);
372
        $byte_count_dec = $byte_count_dec * 2;
373
        #Based on byte count get other fields from the PROM line
374
        (       $start_character,
375
                $byte_count_hex,
376
                $address_hex[0],
377
                $address_hex[1],
378
                $record_type_hex,
379
                $all_data_hex,
380
                $checksum_hex
381
        ) = unpack("A1 A2 A2 A2 A2 A$byte_count_dec A2", $current_prom_line);
382
        #If this is a "04" record type then store its information
383
        if ($record_type eq "04")
384
        {
385
                $last_04_record_data_hex = $all_data_hex;
386
        }
387
}
388
#
389
#
390
#
391
#
392
################################
393
################################
394
#Calculate the new address to be used
395
sub get_mcs_address
396
{
397
        #Get the address from the PROM file
398
        ($address_hex) = unpack("x3 A4", $previous_prom_line);
399
        #Convert the hex address to decimal
400
        $address_dec = hex($address_hex);
401
        #Calculate new address value based on existing address value
402
        if ($address_dec eq "65520")
403
        {
404
                #Store current user data temporarily
405
                $temporary_current_user_line = $current_user_line;
406
                #New address starts at zero
407
                $new_address_dec = 0;
408
                $new_address_hex = "0000";
409
                ($address_hex[0], $address_hex[1]) = unpack("A2 A2", $new_address_hex);
410
            $address_dec[0] = hex($address_hex[0]);
411
        $address_dec[1] = hex($address_hex[1]);
412
                $new_address_offset = 0;
413
                #Convert hex record data to decimal
414
                $new_04_record_data_dec = hex($last_04_record_data_hex);
415
                #Calculate new record "04" data
416
                $new_04_record_data_dec = $new_04_record_data_dec + 1;
417
                #Convert to hex
418
                $new_04_record_data_hex = sprintf "%lx", $new_04_record_data_dec;
419
                #Store for next use
420
                $last_04_record_data_hex = $new_04_record_data_hex;
421
                #Make data at least 4 characters long (i.e. 2 data bytes)
422
                $length = length($new_04_record_data_hex);
423
                if ($length > 4)
424
                {
425
                        die "Record data is too large....Quitting: $!";
426
                }
427
                else
428
                {
429
                        for ($i = 0; $i < 4 - $length; $i++)
430
                        {
431
                                $new_04_record_data_hex = "0$new_04_record_data_hex";
432
                        }
433
                }
434
                #Define new record data
435
                $byte_count_hex = "02";
436
                $byte_count_dec = hex($byte_count_hex);
437
                $record_type_hex = "04";
438
                $record_type_dec = hex($record_type_hex);
439
                $current_user_line = $new_04_record_data_hex;
440
                #Calculate checksum for new "04" record
441
                calculate_mcs_checksum();
442
                #Print new "04" record to new PROM file
443
                print NEW_PROM_FILE uc(":$byte_count_hex$new_address_hex$record_type_hex$new_04_record_data_hex$checksum_hex\n");
444
                $previous_prom_line = uc(":$byte_count_hex$new_address_hex$record_type_hex$new_04_record_data_hex$checksum_hex\n");
445
                #Restore current user data from temporary storage
446
                $current_user_line = $temporary_current_user_line;
447
        }
448
        else
449
        {
450
                #Calculate the offset for the next address to be used in the PROM file
451
                $new_address_offset = 16;#$new_address_offset + 16;
452
                #Calculate the new address
453
                $new_address_dec = $address_dec + $new_address_offset;
454
        }
455
        #Convert new address to hex
456
        $new_address_hex = sprintf "%lx", $new_address_dec;
457
        #Make address at least 4 characters long (i.e. 2 address bytes)
458
        $length = length($new_address_hex);
459
        if ($length > 4)
460
        {
461
                die "Address is too large....Quitting: $!";
462
        }
463
        else
464
        {
465
                for ($i = 0; $i < 4 - $length; $i++)
466
                {
467
                        $new_address_hex = "0$new_address_hex";
468
                }
469
        }
470
        ($address_hex[0], $address_hex[1]) = unpack("A2 A2", $new_address_hex);
471
    $address_dec[0] = hex($address_hex[0]);
472
    $address_dec[1] = hex($address_hex[1]);
473
        $byte_count_dec = "16";
474
        $byte_count_hex = "10";
475
        $record_type_dec = "00";
476
        $record_type_hex = "00";
477
        #Calculate checksum for current user data
478
        calculate_mcs_checksum();
479
        #Print new data to new PROM file
480
        print NEW_PROM_FILE uc(":$byte_count_hex$new_address_hex$record_type_hex$current_user_line$checksum_hex\n");
481
        $previous_prom_line = uc(":$byte_count_hex$new_address_hex$record_type_hex$current_user_line$checksum_hex\n");
482
}
483
#
484
#
485
#
486
#
487
################################
488
################################
489
#Calculate the checksum for the new line
490
sub calculate_mcs_checksum
491
{
492
        $skip = 0;
493
        $data_sum_hex = 0;
494
        $data_sum_dec = 0;
495
        #Based on byte count get individual data bytes and their sum
496
        for ($d = 0; $d < $byte_count_dec; $d++)
497
        {
498
                ($data_hex[$d]) = unpack("x$skip A2", $current_user_line);
499
                $skip = $skip + 2;
500
                #Convert data byte to decimal format
501
                $data_dec[$d] = hex($data_hex[$d]);
502
                #Add all data bytes together
503
                $data_sum_dec = $data_sum_dec + $data_dec[$d];
504
                #convert decimal to hex format
505
                $data_sum_hex = sprintf "%lx", $data_sum_dec;
506
        }
507
        #Add all fields together
508
        $all_sum_dec = $data_sum_dec + $byte_count_dec + $address_dec[0] + $address_dec[1] + $record_type_dec;
509
    #Convert the decimal sum to hex format
510
    $all_sum_hex = sprintf "%lx", $all_sum_dec;
511
    #Get the last two bytes of the hex sum
512
    $last_two_bytes_of_sum_hex = substr($all_sum_hex, -2);
513
    #Convert the last two bytes of the hex sum to decimal format
514
    $last_two_bytes_of_sum_dec = hex($last_two_bytes_of_sum_hex);
515
    #Invert the bits - 1's complement
516
    $inverted_dec = $last_two_bytes_of_sum_dec ^ 255;
517
    #Convert the 1's complement to hex format
518
    $inverted_hex = sprintf "%lx", $inverted_dec;
519
    #Get the last two bytes of 1's complement in hex format
520
    $last_two_bytes_of_inverted_dec = hex($inverted_hex);
521
    #Add 1 to last two bytes - 2's complement
522
    $checksum_dec = $last_two_bytes_of_inverted_dec + 1;
523
    #Convert 2's complement to hex format
524
    $last_two_bytes_2s_hex = uc(sprintf "%lx", $checksum_dec);
525
    #Get the last two bytes of hex 2's compliment
526
    ($checksum_he) = substr($last_two_bytes_2s_hex, -2);
527
    #If value is less than two characters then add a '0'
528
    if ($checksum_dec <= 15)
529
    {
530
        $checksum_hex = "0$checksum_he";
531
    }
532
    else
533
    {
534
        $checksum_hex = $checksum_he;
535
    }
536
}
537
#
538
#
539
#
540
#
541
################################
542
################################
543
#Decimal to binary representation conversion
544
sub decimal2binary
545
{
546
    my $bin_value = unpack("B32", pack("N", shift));
547
    $bin_value =~ s/^0+(?=d)//;
548
    return $bin_value;
549
}
550
#
551
#
552
#
553
#
554
################################
555
################################
556
#Binary to decimal representation conversion
557
sub binary2decimal
558
{
559
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
560
}

powered by: WebSVN 2.1.0

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