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