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