diff python/cortexm3.py @ 237:81752b0f85a5

Added burn led test program
author Windel Bouwman
date Wed, 17 Jul 2013 22:31:54 +0200
parents 8786811a5a59
children 90637d1bbfad
line wrap: on
line diff
--- a/python/cortexm3.py	Mon Jul 15 20:15:31 2013 +0200
+++ b/python/cortexm3.py	Wed Jul 17 22:31:54 2013 +0200
@@ -201,8 +201,18 @@
     mnemonic = 'dcd'
     operands = (Imm32,)
     def __init__(self, expr):
-        assert isinstance(expr, Imm32)
-        self.expr = expr.imm
+        if isinstance(expr, Imm32):
+            self.expr = expr.imm
+            self.label = None
+        elif isinstance(expr, LabelRef):
+            self.expr = 0
+            self.label = expr
+        else:
+            raise NotImplementedError()
+
+    def resolve(self, f):
+        if self.label:
+            self.expr = f(self.label.name)
 
     def encode(self):
         return u32(self.expr)
@@ -451,35 +461,48 @@
 
 # Jumping:
 
-@armtarget.instruction
-class jmp_ins(ArmInstruction):
-    operands = (Label,)
-    mnemonic = 'jmp'
+class jumpBase_ins(ArmInstruction):
+    operands = (LabelRef,)
     def __init__(self, target_label):
-        assert type(target_label) is Label
+        assert type(target_label) is LabelRef
         self.target = target_label
-    def fixUp(self):
-        pass
-    def encode(self):
-        h = 0 # TODO
-        return u16(h)
+        self.offset = 0
+
+    def resolve(self, f):
+        la = f(self.target.name)
+        sa = self.address
+        self.offset = (la - sa)
+        if self.offset < 0:
+            # TODO: handle negative jump
+            self.offset = 0
+
     def __repr__(self):
-        return 'B {0}'.format(self.target.name)
+        return '{} {}'.format(self.mnemonic, self.target.name)
 
 @armtarget.instruction
-class beq_ins(ArmInstruction):
-    operands = (Label,)
+class b_ins(jumpBase_ins):
+    mnemonic = 'B'
+
+    def encode(self):
+        imm11 = self.offset >> 1
+        h = (0b11100 << 11) | imm11 | 1 # 1 to enable thumb mode
+        return u16(h)
+
+class cond_base_ins(jumpBase_ins):
+    def encode(self):
+        imm8 = 0
+        h = (0b1101 << 12) | (self.cond << 8) | imm8
+        return u16(h)
+
+@armtarget.instruction
+class beq_ins(cond_base_ins):
     mnemonic = 'beq'
-    def __init__(self, target_label):
-        assert type(target_label) is Label
-        self.target = target_label
-    def fixUp(self):
-        pass
-    def encode(self):
-        h = 0 # TODO
-        return u16(h)
-    def __repr__(self):
-        return 'BEQ {0}'.format(self.target.name)
+    cond = 0
+
+@armtarget.instruction
+class beq_ins(cond_base_ins):
+    mnemonic = 'bne'
+    cond = 1
 
 @armtarget.instruction
 class push_ins(ArmInstruction):