?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. @setlocal enabledelayedexpansion && python -x "%~f0" %* & exit /b !ERRORLEVEL!
  2. #!/usr/bin/env python
  3.  
  4. # as2gbmap - asxxxx to gb map file converter
  5. #
  6. # Copyright (c) 2010 Borut Razem
  7. #
  8. # This file is part of sdcc.
  9. #
  10. #  This software is provided 'as-is', without any express or implied
  11. #  warranty.  In no event will the authors be held liable for any damages
  12. #  arising from the use of this software.
  13. #
  14. #  Permission is granted to anyone to use this software for any purpose,
  15. #  including commercial applications, and to alter it and redistribute it
  16. #  freely, subject to the following restrictions:
  17. #
  18. #  1. The origin of this software must not be misrepresented; you must not
  19. #     claim that you wrote the original software. If you use this software
  20. #     in a product, an acknowledgment in the product documentation would be
  21. #     appreciated but is not required.
  22. #  2. Altered source versions must be plainly marked as such, and must not be
  23. #     misrepresented as being the original software.
  24. #  3. This notice may not be removed or altered from any source distribution.
  25. #
  26. #  Borut Razem
  27. #  borut.razem@siol.net
  28.  
  29. from __future__ import print_function
  30.  
  31. import sys
  32. import os
  33. import re
  34. from optparse import OptionParser
  35. import operator
  36.  
  37. def main():
  38.     '''asxxxx to gb map file converter'''
  39.     usage = "usage: %prog [options] [<input_asxxxx_map_file> [<output_gb_file>]]"
  40.     parser = OptionParser(usage = usage, version = "1.0")
  41.     parser.set_defaults(no_gmb = False)
  42.     parser.add_option("-j", "--no$gmb", action = "store_true", dest = "no_gmb", help = "generate no$gmb symbol file (default: rrgb)")
  43.     (options, args) = parser.parse_args()
  44.  
  45.     if len(args) > 0 and args[0] != "-":
  46.         try:
  47.             fin = open(args[0], "r")
  48.         except IOError as xxx_todo_changeme:
  49.             (errno, strerror) = xxx_todo_changeme.args
  50.             print("%s: can't open %s: %s" % (os.path.basename(sys.argv[0]), args[0], strerror), file=sys.stderr)
  51.             return 1
  52.     else:
  53.         fin = sys.stdin
  54.  
  55.     if len(args) > 1 and args[1] != "-":
  56.         try:
  57.             fout = open(args[1], "w")
  58.         except IOError as xxx_todo_changeme1:
  59.             (errno, strerror) = xxx_todo_changeme1.args
  60.             print("%s: can't create %s: %s" % (os.path.basename(sys.argv[1]), args[1], strerror), file=sys.stderr)
  61.             return 1
  62.     else:
  63.         fout = sys.stdout;
  64.  
  65.     areas = []
  66.     modules = []
  67.     libraries = []
  68.     ubads = []
  69.  
  70.     radix = 'HEX'
  71.     state = None
  72.     area = None
  73.    
  74.     # process asxxxx map file
  75.     for line in fin:
  76.         if re.match(r"^Hexadecimal$", line):
  77.             radix = 'HEX';
  78.             continue
  79.  
  80.         if re.match(r"^Area +Addr +Size +Decimal +Bytes +\(Attributes\)$", line):
  81.             line = next(fin)
  82.             if re.match(r"^[- ]+$", line):
  83.                 line = next(fin)
  84.                 m = re.match(r"^([^ ]+) +([0-9A-Fa-f]{4}) +([0-9A-Fa-f]{4}) += +\d+\. +\w+ +\(([^\)]+)\)$", line)
  85.                 if m:
  86.                     if area:
  87.                         if m.group(1) != area['area']:
  88.                             areas.append(area)
  89.                             area = {'area': m.group(1), 'radix': radix, 'base': int(m.group(2), 16), 'size': int(m.group(3), 16), 'attrib': m.group(4).replace(',', ' '), 'globals': []}
  90.                     else:
  91.                         area = {'area': m.group(1), 'radix': radix, 'base': int(m.group(2), 16), 'size': int(m.group(3), 16), 'attrib': m.group(4).replace(',', ' '), 'globals': []}
  92.                     state = 'IN_AREA'
  93.             continue
  94.  
  95.         m = re.match(r"^ +([0-9A-Fa-f]{4}) +([^ ]+) +$", line)
  96.         if state == 'IN_AREA' and m:
  97.             area['globals'].append({'value': int(m.group(1), 16), 'global': m.group(2)})
  98.             continue
  99.  
  100.         m = re.match(r"Files Linked +\[ module\(s\) \]$", line)
  101.         if m:
  102.             state = 'IN_MODULES'
  103.             continue
  104.  
  105.         m = re.match(r"Libraries Linked +\[ object file \]$", line)
  106.         if m:
  107.             state = 'IN_LIBRARIES'
  108.             continue
  109.  
  110.         m = re.match(r"User Base Address Definitions$", line)
  111.         if m:
  112.             state = 'IN_UBAD'
  113.             continue
  114.  
  115.         m = re.match(r"^([^ ]+) +\[ ([^ ]*) \]$", line)
  116.         if m:
  117.             if state == 'IN_MODULES':
  118.                 modules.append({'file': m.group(1), 'name': m.group(2)})
  119.                 continue
  120.  
  121.             if state == 'IN_LIBRARIES':
  122.                 libraries.append({'library': m.group(1), 'module': m.group(2)})
  123.                 continue
  124.  
  125.         m = re.match(r"^([^ ]+) += +0x([0-9A-Fa-f]{4})$", line)
  126.         if state == 'IN_UBAD' and m:
  127.             ubads.append({'symbol': m.group(1), 'value': m.group(2)})
  128.             continue
  129.  
  130.     if area:
  131.         areas.append(area)
  132.  
  133.  
  134.     if options.no_gmb:
  135.         # generate no$gmp map file
  136.         print('; no$gmb format .sym file', file=fout)
  137.         print('; Generated automagically by %s' % os.path.basename(sys.argv[0]), file=fout)
  138.         for e in areas:
  139.             print('; Area: %s' % e['area'], file=fout)
  140.             if e['globals']:
  141.                 e['globals'].sort(key = operator.itemgetter('value'))
  142.                 for g in e['globals']:
  143.                    if g['global'][0:3] != 'l__':
  144.                         if g['value'] > 0x7FFF:
  145.                             print('00:%04X %s' % (g['value'], g['global']), file=fout)
  146.                         else:
  147.                             print('%02X:%04X %s' % (g['value'] // 16384, g['value'], g['global']), file=fout)
  148.     else:
  149.         # generate rrgb map file
  150.         for e in areas:
  151.             print('AREA %s' % e['area'], file=fout)
  152.             print('\tRADIX %s' % e['radix'], file=fout)
  153.             print('\tBASE %04X' % e['base'], file=fout)
  154.             print('\tSIZE %04X' % e['size'], file=fout)
  155.             print('\tATTRIB %s' % e['attrib'], file=fout)
  156.             if e['globals']:
  157.                 e['globals'].sort(key = operator.itemgetter('value'))
  158.                 print('\tGLOBALS', file=fout)
  159.                 for g in e['globals']:
  160.                     print('\t\t%s\t%04X' % (g['global'], g['value']), file=fout)
  161.    
  162.         if modules:
  163.             print('MODULES', file=fout)
  164.             for m in modules:
  165.                 print('\tFILE %s' % m['file'], file=fout)
  166.                 if m['name']:
  167.                     print('\t\tNAME %s' % m['name'], file=fout)
  168.    
  169.         if libraries:
  170.             print('LIBRARIES', file=fout)
  171.             for m in libraries:
  172.                 print('\tLIBRARY %s' % m['library'], file=fout)
  173.                 print('\t\tMODULE %s' % m['module'], file=fout)
  174.    
  175.         if ubads:
  176.             print('USERBASEDEF', file=fout)
  177.             for m in ubads:
  178.                 print('\t%s = 0x%04X' % (m['symbol'], int(m['value'], 16)), file=fout)
  179.         return 0
  180.  
  181. if __name__ == '__main__':
  182.     sys.exit(main())
  183.