From 2e959a0e059e8028a17a4307474f7f7841eb353c Mon Sep 17 00:00:00 2001 From: Dan White Date: Thu, 15 Dec 2011 17:56:05 -0600 Subject: [PATCH] Utilities for dealing with rcf, hex, include, and mmap files --- Makefile | 15 ++++++++++++ inc2syms.py | 49 +++++++++++++++++++++++++++++++++++++ mmap2h.sh | 10 ++++++++ mmap2inc.sh | 10 ++++++++ rcf2ihex.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ rename_regs.sh | 6 +++++ 6 files changed, 155 insertions(+) create mode 100644 Makefile create mode 100755 inc2syms.py create mode 100755 mmap2h.sh create mode 100755 mmap2inc.sh create mode 100755 rcf2ihex.py create mode 100755 rename_regs.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ce74e20 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ + + +%.hex: %.rcf + ./rcf2ihex.py $< > $@ + +%.asm: %.hex ns430-atoi.inc + msp430-objdump --disassemble-all --architecture=msp:54 $< \ + | ./inc2syms.py ns430-atoi.inc | ./rename_regs.sh > $@ + +%.h: %.mmap + ./mmap2h.sh $< > $@ + +%.inc: %.mmap + ./mmap2inc.sh $< > $@ + diff --git a/inc2syms.py b/inc2syms.py new file mode 100755 index 0000000..ec036ed --- /dev/null +++ b/inc2syms.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +import sys + +VERBOSE = False + +if len(sys.argv) < 2 or sys.argv[1] == '-h': + print>>sys.stderr, 'Usage: %s syms.inc [file.asm]' + sys.exit(1) + +incfile = open(sys.argv[1]) + +if len(sys.argv) == 2: + asmfile = sys.stdin +else: + asmfile = open(sys.argv[2]) + + +syms = {} +addrs = {} +print 'Symbol Table:' +for line in incfile: + if 'equ' in line: + sp = line.strip().split() + syms[sp[0]] = '0x' + sp[2][:-1] + addrs[syms[sp[0]]] = sp[0] + print ' %s: %s' % (sp[0], syms[sp[0]]) + + +lines = asmfile.readlines() + +newlines = [] +for line in lines: + oldline = line + replaced = False + for addr in addrs.iterkeys(): + if addr in line: + line = line.replace(addr, addrs[addr]) + if VERBOSE: + if replaced: + print>>sys.stderr, '*** double replacement !!!' + print>>sys.stderr, 'old:', oldline.rstrip() + print>>sys.stderr, 'new:', line.rstrip() + replaced = True + + newlines.append(line) + +print ''.join(newlines) +#sys.stdout.write(''.join(newlines)) diff --git a/mmap2h.sh b/mmap2h.sh new file mode 100755 index 0000000..c58d47e --- /dev/null +++ b/mmap2h.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Extract header definitions from the memory map +# e.g. #FOO_NAME = 4002h +# transform to C-style #define's +egrep -o '#.*' $1 \ + | cut -c 2- \ + | tr -d =h \ + | awk '{print "#define " $1 " 0x" $2}' + diff --git a/mmap2inc.sh b/mmap2inc.sh new file mode 100755 index 0000000..3e7e82f --- /dev/null +++ b/mmap2inc.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Extract header definitions from the memory map +# e.g. #FOO_NAME = 4002h +# transform to assembler equ's +egrep -o '#.*' $1 \ + | cut -c 2- \ + | tr -d = \ + | awk '{print $1 " equ " $2}' + diff --git a/rcf2ihex.py b/rcf2ihex.py new file mode 100755 index 0000000..f6d0ff5 --- /dev/null +++ b/rcf2ihex.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +import sys + +if len(sys.argv) < 2: + print>>sys.stderr, 'Usage %s rom.rcf' % sys.argv[0] + sys.exit(1) + +#rcf = open('rom.rcf') +rcf = open(sys.argv[1]) +#ihex = open('rom.hex', 'w') +ihex = sys.stdout + +start_addr = int('3000', 16) + +bytes = [] +for line in rcf: + word = line.strip() + b1 = int(word[0:8], 2) + b0 = int(word[8:], 2) + + bytes.append(b0) + bytes.append(b1) + + +def checksum(vec): + s = sum(vec) + c = s & 0xFF + c ^= 0xFF + c += 1 + c &= 0xFF + return c + #return -sum(vec) & 0xFF + + +#v = [0,0,0,0,0] +#print checksum(v), (sum(v)+checksum(v)) & 0xFF +#v = [2,2,2,2,2] +#print checksum(v), (sum(v)+checksum(v)) & 0xFF +#v = [0,1,2,3,240] +#print checksum(v), (sum(v)+checksum(v)) & 0xFF + + + +# count, addr, type, hexstring, checksum +ihex_fmt = ':%02X%04X%02X%s%02X' +nbytes = 16 +addr = start_addr +for i in range(len(bytes)/nbytes + 1): + bvec = bytes[i*nbytes:(i+1)*nbytes] + + if not bvec: break + + sumvec = [len(bvec), ((addr >> 8) & 0xFF), (addr & 0xFF), 0] + sumvec.extend(bvec) + cs = checksum(sumvec) + + bstr = ''.join(['%02X' % b for b in bvec]) + + print >>ihex, ihex_fmt % (16, addr, 0, bstr, cs) + + addr += nbytes + +print >>ihex, ':00000001FF' + diff --git a/rename_regs.sh b/rename_regs.sh new file mode 100755 index 0000000..58da61d --- /dev/null +++ b/rename_regs.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +sed -r -e 's/[^;]r0([ \t,\)])/pc\1/g' \ + -e 's/[^;]r1([ \t,\)])/sp\1/g' \ + -e 's/[^;]r2([ \t,\)])/sr\1/g' + -- 2.25.1