perl

Perl Practical Extraction and Report Language
Perl 指语言本身 perl 指程序运行的解释器

#!/usr/bin/env perl
#################################################################################
#
# The confidential and proprietary information contained in this file may
# only be used by a person authorised under and to the extent permitted
# by a subsisting licensing agreement from ARM Limited.
#
#                 (C) COPYRIGHT 2000-2011 ARM Limited.
#                       ALL RIGHTS RESERVED
#
# This entire notice must be reproduced on all copies of this file
# and copies of this file may only be made by a person if such person is
# permitted to do so under the terms of a subsisting license agreement
# from ARM Limited.
#
#	       Bin2Hex
#	       =======
#
################################################################################

$^W=1; # enable warnings (same as perl -w)

# ensure we are running at least version 5.00502
require 5.00502;

use File::Basename;

my $bigend = (exists $ENV{'VAL_BIGENDIAN'});
my $image36 = 1;

# check if you should directly convert a single binary to stdout
if (not -e "armcode.script") {
  $image36 = 0;
  BinToHex($ARGV[0], "-", $bigend);
  exit 0;
}

$bigend = (defined $ARGV[0] and $ARGV[0] =~ /-bigend/);
BuildHexImages($bigend);
exit 0;

# --------------------------------------------------------------------------------

sub diemsg {
  print "$0: (fatal) @_\n";
  exit 1;
}

sub marine {
    print "Hello, sailor number!\n";
    print "Aloha!\n";
}

# --------------------------------------------------------------------------------
# INDEX:BuildHexImages subroutine:
#
# --------------------------------------------------------------------------------

sub BuildHexImages {
  my $bigend = $_[0];

  my $bin;
  my $binhex;
  my $address;

  open (ARMCODE,    "armcode.script") or diemsg("Could not open armcode.script");
  open (ARMCODEHEX, ">armcode.hex")   or diemsg("Could not open armcode.hex");
  open (IMAGE36HEX, ">image36.hex")   or diemsg("Could not open image36.hex");

  while (<ARMCODE>) {
    ($bin, $address) = split;

    diemsg("malformed armcode.script") if (!defined $bin or !defined $address);

    # derive hex binary filename from binary file
    $binhex = "bin/" . basename($bin) . ".hex";

    # construct armcode.hex entry
    print ARMCODEHEX "$binhex $address\n";

    # write out starting address for image36 file
    printf IMAGE36HEX "1%.16x\n", hex($address);

    BinToHex("$bin", "$binhex", ($bigend && !($bin =~ /rispage/)));
  }

  # write out end delimiter
  print IMAGE36HEX "200000000\n";

  close IMAGE36HEX;
  close ARMCODEHEX;
  close ARMCODE;
}

# --------------------------------------------------------------------------------
# INDEX:subroutine:     BinToHex
#
# This routine converts a binary file to hex text file
#
# BigEndian juggling has been removed, as it is believed to  corrupt the image
# - support has been moved into testbench
#
# Big endian juggling reinstated at the request of Andy Rose - it is apparently
# necessary for endian switchable pure verilog memory models.
#
# Big endian has an impact on mmuris -- mmuris doesn't know the setting of the
# -bigend validation option.  It always creates little endian binary files for
# it's random page tables.
# The output hex files need to always be little endian, which means that if -bigend
# was specified then the binaries need to be byteswapped before creating
# the hex files.  But since the mmuris random page tables are always little
# endian, they don't need to be swapped ever, even if -bigend is specified.
#
# --------------------------------------------------------------------------------

sub BinToHex {
  open(INFILE,  " $_[0]") or diemsg("could not open $_[0]");
  open(OUTFILE, ">$_[1]") or diemsg("could not open $_[1]");

  my $bigend = $_[2];
  my $word;
  my $apple;
  my $low;
  my $high;

  $n = 0;

  if ($bigend) {
    # read in the binary file in word chunks
    while(read INFILE, $word, 4) {
      # write out each word to image36 file
      printf IMAGE36HEX "0%.8x\n", unpack("N*", $word) if ($image36);

      # unpack each word 'string' into a 'long' big-endian and print it as hex
      printf OUTFILE "%.8x\n", unpack("N*", $word);
    }
  }
  else {
    # read in the binary file in word chunks
    while(read INFILE, $word, 4) {
      # write out each word to image36 file
      #printf IMAGE36HEX "0%.8x\n", unpack("V*", $word) if ($image36);

      # unpack each word 'string' into a 'long' little-endian and print it as hex
      #printf OUTFILE "%.16x\n", unpack("V*", $word);
      #print $word;

      $m = $n % 2;
      if ($m)
      {
        $high = unpack("V*", $word);
        $apple = sprintf("%.8x%.8x", $high, $low);
        printf OUTFILE "%s\n", $apple;
      }
      else
      {
        $low = unpack("V*", $word);
      }
      $n += 1;
    }
      #last 8 bytes alain to  16bytes
      $m = $n % 2;
      if ($m)
      {
        $high = 0;
        $apple = sprintf("%.8x%.8x", $high, $low);
        printf OUTFILE "%s\n", $apple;
      }
  }

  close(INFILE);
  close(OUTFILE);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春夏与冬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值