Utilities for dealing with rcf, hex, include, and mmap files
authorDan White <dan@whiteaudio.com>
Thu, 15 Dec 2011 23:56:05 +0000 (17:56 -0600)
committerDan White <dan@whiteaudio.com>
Thu, 15 Dec 2011 23:56:05 +0000 (17:56 -0600)
Makefile [new file with mode: 0644]
inc2syms.py [new file with mode: 0755]
mmap2h.sh [new file with mode: 0755]
mmap2inc.sh [new file with mode: 0755]
rcf2ihex.py [new file with mode: 0755]
rename_regs.sh [new file with mode: 0755]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
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 (executable)
index 0000000..ec036ed
--- /dev/null
@@ -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 (executable)
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 (executable)
index 0000000..3e7e82f
--- /dev/null
@@ -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 (executable)
index 0000000..f6d0ff5
--- /dev/null
@@ -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 (executable)
index 0000000..58da61d
--- /dev/null
@@ -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'
+