diff python/msp430.py @ 202:f22b431f4113

Added arm add instruction
author Windel Bouwman
date Sat, 15 Jun 2013 10:02:50 +0200
parents d5debbfc0200
children ca1ea402f6a1
line wrap: on
line diff
--- a/python/msp430.py	Thu Jun 13 00:07:28 2013 +0200
+++ b/python/msp430.py	Sat Jun 15 10:02:50 2013 +0200
@@ -7,9 +7,13 @@
 msp430target = Target("MSP430")
 
 REGISTER_MODE = 1
+SYMBOLIC_MODE = 3
+ABSOLUTE_MODE = 4
 #TODO: add more modes!
 IMMEDIATE_MODE = 7
 
+
+
 # Add a custom operand mapping method:
 def mapOp(self, operand):
     if type(operand) is ASymbol:
@@ -115,35 +119,18 @@
         h1 = (self.opcode << 4)
         return pack_ins(h1)
 
-@msp430target.instruction
-class rrc_ins(OneOpArith):
-    mnemonic = 'rrc'
-    opcode = 0
-
-@msp430target.instruction
-class swpb_ins(OneOpArith):
-    mnemonic = 'swpb'
-    opcode = 1
-
-@msp430target.instruction
-class rra_ins(OneOpArith):
-    mnemonic = 'rra'
-    opcode = 2
+def oneOpIns(mne, opc):
+    """ Helper function to define a one operand arithmetic instruction """
+    members = {'mnemonic': mne, 'opcode': opc}
+    ins_cls = type(mne + '_ins', (OneOpArith,), members)
+    msp430target.addInstruction(ins_cls)
 
-@msp430target.instruction
-class sxt_ins(OneOpArith):
-    mnemonic = 'sxt'
-    opcode = 3
-
-@msp430target.instruction
-class push_ins(OneOpArith):
-    mnemonic = 'push'
-    opcode = 4
-
-@msp430target.instruction
-class call_ins(OneOpArith):
-    mnemonic = 'call'
-    opcode = 5
+oneOpIns('rrc', 0)
+oneOpIns('swpb', 1)
+oneOpIns('rra', 2)
+oneOpIns('sxt', 3)
+oneOpIns('push', 4)
+oneOpIns('call', 5)
 
 #########################
 # Jump instructions:
@@ -217,7 +204,6 @@
             For the destination there are 4.
             The trick is to use also the register to distuingish the
             different modes.
-
         """
         # TODO: Make memory also possible
 
@@ -234,65 +220,34 @@
     def decode(self, data):
         pass
 
-@msp430target.instruction
-class mov_ins(TwoOpArith):
-    mnemonic = 'mov'
-    opcode = 4
 
+def twoOpIns(mne, opc):
+    """ Helper function to define a two operand arithmetic instruction """
+    members = {'mnemonic': mne, 'opcode': opc}
+    ins_cls = type(mne + '_ins', (TwoOpArith,), members)
+    msp430target.addInstruction(ins_cls)
+
+twoOpIns('mov', 4)
+
+# This is equivalent to the helper function twoOpIns:
 @msp430target.instruction
 class add_ins(TwoOpArith):
+    """ Adds the source to the destination """
     mnemonic = 'add'
     opcode = 5
 
-@msp430target.instruction
-class addc_ins(TwoOpArith):
-    mnemonic = 'addc'
-    opcode = 6
-
-@msp430target.instruction
-class subc_ins(TwoOpArith):
-    mnemonic = 'subc'
-    opcode = 7
-
-@msp430target.instruction
-class sub_ins(TwoOpArith):
-    mnemonic = 'sub'
-    opcode = 8
-
-@msp430target.instruction
-class cmp_ins(TwoOpArith):
-    """ Compare, substract source from destination """
-    mnemonic = 'cmp'
-    opcode = 9
+    def operate(self):
+        dst.value = dst.value + src.value
+        setFlags()
 
-@msp430target.instruction
-class dadd_ins(TwoOpArith):
-    """ Decimal add source to destination """
-    mnemonic = 'dadd'
-    opcode = 10
-
-@msp430target.instruction
-class bit_ins(TwoOpArith):
-    mnemonic = 'bit'
-    opcode = 11
-
-@msp430target.instruction
-class bic_ins(TwoOpArith):
-    mnemonic = 'bic'
-    opcode = 12
+twoOpIns('addc', 6)
+twoOpIns('subc', 7)
+twoOpIns('sub', 8)
+twoOpIns('cmp', 9)
+twoOpIns('dadd', 10)
+twoOpIns('bit', 11)
+twoOpIns('bic', 12)
+twoOpIns('bis', 13)
+twoOpIns('xor', 14)
+twoOpIns('and', 15)
 
-@msp430target.instruction
-class bis_ins(TwoOpArith):
-    mnemonic = 'bis'
-    opcode = 13
-
-@msp430target.instruction
-class xor_ins(TwoOpArith):
-    mnemonic = 'xor'
-    opcode = 14
-
-@msp430target.instruction
-class and_ins(TwoOpArith):
-    mnemonic = 'and'
-    opcode = 15
-