annotate python/target/msp430.py @ 341:4d204f6f7d4e devel

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