annotate python/msp430.py @ 244:58155c7c4a8e

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