annotate python/target/msp430.py @ 336:d1ecc493384e

Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
author Windel Bouwman
date Wed, 19 Feb 2014 22:32:15 +0100
parents 6f4753202b9a
children 4d204f6f7d4e
rev   line source
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
1 from .basetarget import Register, Instruction, Target
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 292
diff changeset
2 from ppci.asmnodes import ASymbol, ANumber
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
3 from ppci import CompilerError
290
7b38782ed496 File moves
Windel Bouwman
parents: 203
diff changeset
4 import struct
7b38782ed496 File moves
Windel Bouwman
parents: 203
diff changeset
5 import types
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
6
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
7 # Create the target class (singleton):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
8 msp430target = Target("MSP430")
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
9
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
10 REGISTER_MODE = 1
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
11 SYMBOLIC_MODE = 3
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
12 ABSOLUTE_MODE = 4
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
13 #TODO: add more modes!
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
14 IMMEDIATE_MODE = 7
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
15
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
16 # Target description for the MSP430 processor
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
17
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
18 class MSP430Reg(Register):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
19 def __init__(self, num, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
20 super().__init__(name)
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
21 self.num = num
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
22
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
23 # 8 bit registers:
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
24 PCB = MSP430Reg(0, 'r0')
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
25 rpc = PCB
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
26 r11 = MSP430Reg(11, 'r11')
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
27 r12 = MSP430Reg(12, 'r12')
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
28 r13 = MSP430Reg(13, 'r13')
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
29 r14 = MSP430Reg(14, 'r14')
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
30 r15 = MSP430Reg(15, 'r15')
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
31
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
32 class MSP430Mem:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
33 pass
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
34
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
35 msp430target.registers.append(r11)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
36 msp430target.registers.append(r12)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
37 msp430target.registers.append(r13)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
38 msp430target.registers.append(r14)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
39 msp430target.registers.append(r15)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
40
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
41 # .. etc
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
42
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
43 #GR8 = RegisterClass((PCB, R15B))
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
44
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
45 class MSP430Operand:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
46 def __init__(self, mode, param):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
47 self.mode = mode
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
48 self.param = param
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
49 def regField(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
50 if self.mode == REGISTER_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
51 return self.param
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
52 elif self.mode == IMMEDIATE_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
53 return rpc.num
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
54 def asField(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
55 if self.mode == REGISTER_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
56 return 0
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
57 elif self.mode == IMMEDIATE_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
58 return 3
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
59 def adField(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
60 if self.mode == REGISTER_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
61 return 0
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
62 elif self.mode == IMMEDIATE_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
63 raise CompilerError('Cannot use immediate mode for destination operand')
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
64 def extraBytes(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
65 if self.mode == IMMEDIATE_MODE:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
66 return pack_ins(self.param)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
67 return bytes()
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
68
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
69 @classmethod
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
70 def Create(cls, vop):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
71 if type(vop) is ASymbol:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
72 # try to map to register:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
73 regs = {}
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
74 for r in msp430target.registers:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
75 regs[r.name] = r
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
76 if vop.name in regs:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
77 reg = regs[vop.name]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
78 return cls(REGISTER_MODE, reg.num)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
79 elif type(vop) is ANumber:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
80 # Immediate mode:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
81 return cls(IMMEDIATE_MODE, vop.number)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
82
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
83 def pack_ins(h):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
84 return struct.pack('<H', h)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
85
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
86 class MSP430Instruction(Instruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
87 b = 0
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
88
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
89 class BInstruction:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
90 pass
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
91
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
92 class MSP430CoreInstruction(Instruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
93 pass
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
94
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
95 #########################
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
96 # Single operand arithmatic:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
97 #########################
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
98
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
99 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
100 class reti_ins(MSP430Instruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
101 mnemonic = 'reti'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
102 operands = ()
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
103 def encode(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
104 h = 0x1300
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
105 return pack_ins(h)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
106
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
107 class OneOpArith(MSP430Instruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
108 operands = (MSP430Reg, )
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
109 def __init__(self, op1):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
110 self.op1 = op1
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
111 def encode(self):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
112 # TODO:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
113 bits[15:10] = '00100'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
114 h1 = (self.opcode << 4)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
115 return pack_ins(h1)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
116
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
117 def oneOpIns(mne, opc):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
118 """ Helper function to define a one operand arithmetic instruction """
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
119 members = {'mnemonic': mne, 'opcode': opc}
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
120 ins_cls = type(mne + '_ins', (OneOpArith,), members)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
121 msp430target.addInstruction(ins_cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
122
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
123 oneOpIns('rrc', 0)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
124 oneOpIns('swpb', 1)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
125 oneOpIns('rra', 2)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
126 oneOpIns('sxt', 3)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
127 oneOpIns('push', 4)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
128 oneOpIns('call', 5)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
129
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
130 #########################
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
131 # Jump instructions:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
132 #########################
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
133
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
134 class JumpInstruction(Instruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
135 def __init__(self, offset):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
136 self.offset = offset
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
137
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
138 def encode(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
139 h = (1 << 13) | (self.condition << 10) | (self.offset)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
140 return pack_ins(h)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
141
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
142 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
143 class jnz_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
144 mnemonic = 'jnz'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
145 condition = 0
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
146
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
147 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
148 class jz_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
149 mnemonic = 'jz'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
150 condition = 1
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
151
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
152 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
153 class jnc_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
154 mnemonic = 'jnc'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
155 condition = 2
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
156
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
157 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
158 class jc_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
159 mnemonic = 'jc'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
160 condition = 3
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
161
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
162 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
163 class jn_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
164 mnemonic = 'jn'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
165 condition = 4
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
166
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
167 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
168 class jge_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
169 mnemonic = 'jge'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
170 condition = 5
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
171
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
172 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
173 class jl_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
174 mnemonic = 'jl'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
175 condition = 6
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
176
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
177 @msp430target.instruction
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
178 class jmp_ins(JumpInstruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
179 mnemonic = 'jmp'
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
180 condition = 7
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
181
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
182 #########################
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
183 # Two operand arithmatic instructions:
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
184 #########################
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
185
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
186
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
187 class TwoOpArith(MSP430Instruction):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
188 operands = (MSP430Operand, MSP430Operand)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
189 def __init__(self, src, dst):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
190 self.op1 = src
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
191 self.op2 = dst
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
192
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
193 def encode(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
194 """
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
195 Smart things have been done by MSP430 designers.
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
196 As (2 bits) is the source addressing mode selector.
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
197 Ad (1 bit) is the destination adressing mode selector.
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
198 For the source there are 7 different addressing mode.
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
199 For the destination there are 4.
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
200 The trick is to use also the register to distuingish the
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
201 different modes.
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
202 """
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
203 # TODO: Make memory also possible
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
204
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
205 As = self.op1.asField() # addressing mode for the source
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
206 Ad = self.op2.adField() # Addressing mode for dst
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
207 b = self.b # When b=1, the operation is byte mode
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
208 source = self.op1.regField()
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
209 destination = self.op2.regField()
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
210 h = (self.opcode << 12) | (source << 8)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
211 h |= (self.b << 6) | (As << 4) | (Ad << 7) | destination
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
212 additions = self.op1.extraBytes() + self.op2.extraBytes()
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
213 return pack_ins(h) + additions
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
214
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
215 def decode(self, data):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
216 pass
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
217
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
218
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
219 def twoOpIns(mne, opc):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
220 """ Helper function to define a two operand arithmetic instruction """
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
221 members = {'mnemonic': mne, 'opcode': opc}
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
222 ins_cls = type(mne + '_ins', (TwoOpArith,), members)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
223 msp430target.addInstruction(ins_cls)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
224
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
225 twoOpIns('mov', 4)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
226
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
227 # This is equivalent to the helper function twoOpIns:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
228 @msp430target.instruction
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
229 class add_ins(TwoOpArith):
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
230 """ Adds the source to the destination """
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
231 mnemonic = 'add'
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
232 opcode = 5
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
233
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
234 def operate(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
235 dst.value = dst.value + src.value
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
236 setFlags()
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
237
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
238 twoOpIns('addc', 6)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
239 twoOpIns('subc', 7)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
240 twoOpIns('sub', 8)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
241 twoOpIns('cmp', 9)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
242 twoOpIns('dadd', 10)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
243 twoOpIns('bit', 11)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
244 twoOpIns('bic', 12)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
245 twoOpIns('bis', 13)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
246 twoOpIns('xor', 14)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
247 twoOpIns('and', 15)