1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/*
   Copyright (C) 2019 Andreas Franz Borchert
   ----------------------------------------------------------------------------
   The Astl Library for ULMasm is free software; you can redistribute it
   and/or modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either version
   2 of the License, or (at your option) any later version.

   The Astl Library for ULMasm is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

sub convert_hex_value(literal) {
   var val = 0;
   var res = literal =~ m{0x(.*)};
   literal = res[0];
   while (res = literal =~ m{(.)(.*)}) {
      var digit = res[0];
      var digit_val;
      if (digit =~ m{[A-Fa-f]}) {
         if (digit =~ m{[Aa]}) {
            digit_val = 10;
         } elsif (digit =~ m{[Bb]}) {
            digit_val = 11;
         } elsif (digit =~ m{[Cc]}) {
            digit_val = 12;
         } elsif (digit =~ m{[Dd]}) {
            digit_val = 13;
         } elsif (digit =~ m{[Ee]}) {
            digit_val = 14;
         } else {
            digit_val = 15;
         }
      } else {
         digit_val = digit;
      }
      val = val * 16 + digit_val;
      literal = res[1];
   }
   return val;
}

sub hex_digit(value) {
   var digits = ["A", "B", "C", "D", "E", "F"];
   if (value < 10) {
      return value & "";
   } else {
      return digits[value - 10];
   }
}

sub make_hex_str(digits, value) {
   var s = "";
   while (digits > 0) {
      s = hex_digit(value mod 16) & s;
      --digits; value = value div 16;
   }
   return s;
}

sub convert_to_hex_string(node, unsigned, bits, value) {
   if (unsigned) {
      if (value < 0 || value >= 2^bits) {
         if (node != null) {
            println(stderr, cmdname, ": ", location(node), ": value ", value,
               " out of range [0, 2^",
               bits, ")");
         } else {
            println(stderr, cmdname, ": value ", value,
               " out of range [0, 2^", bits, ")");
         }
         exit(1);
      }
   } else {
      if (value < - 2^(bits-1) || value >= 2^(bits-1)) {
         if (node != null) {
            println(stderr, cmdname, ": ", location(node), ": value ", value,
               " out of range [-2^", bits-1,
               ", 2^", bits-1, ")");
         } else {
            println(stderr, cmdname, ": value ", value,
               " out of range [-2^", bits-1,
               ", 2^", bits-1, ")");
         }
         exit(1);
      }
      if (value < 0) {
         value += 2^bits;
      }
   }
   var digits = bits div 4;
   var s = "";
   while (digits > 0) {
      s = hex_digit(value mod 16) & s;
      --digits; value = value div 16;
   }
   return s;
}