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