annotate python/ppci/target/arm/instructions.py @ 375:19eacf4f7270

Started on memory manager
author Windel Bouwman
date Sun, 23 Mar 2014 15:44:06 +0100
parents 98ff43cfdd36
children 6df89163e114
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
1
364
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
2 from ..basetarget import Instruction, LabelAddress
365
98ff43cfdd36 Nasty bug in adr instruction
Windel Bouwman
parents: 364
diff changeset
3 from ...bitfun import encode_imm32
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
4
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
5 from .token import ArmToken
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
6 from .registers import R0, SP, ArmRegister
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
7
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
8
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
9
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
10 # Instructions:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
11
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
12 class ArmInstruction(Instruction):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
13 def __init__(self):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
14 self.token = ArmToken()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
15
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
16
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
17 class ConstantData(ArmInstruction):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
18 def __init__(self, v):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
19 super().__init__()
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
20 assert isinstance(v, int)
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
21 self.v = v
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
22
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
23
364
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
24 class Dcd(ArmInstruction):
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
25 def __init__(self, v):
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
26 super().__init__()
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
27 assert isinstance(v, int) or isinstance(v, LabelAddress)
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
28 self.v = v
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
29
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
30 def encode(self):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
31 if type(self.v) is int:
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
32 self.token[0:32] = self.v
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
33 else:
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
34 self.token[0:32] = 0
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
35 return self.token.encode()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
36
364
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
37 def relocations(self):
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
38 if type(self.v) is LabelAddress:
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
39 return [(self.v.name, 'absaddr32')]
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
40 return []
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
41
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
42 def __repr__(self):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
43 if type(self.v) is int:
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
44 return 'DCD {}'.format(hex(self.v))
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
45 else:
c49459768aaa Work on globals
Windel Bouwman
parents: 362
diff changeset
46 return 'DCD ={}'.format(self.v.name)
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
47
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
48
353
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
49 class Db(ConstantData):
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
50 def encode(self):
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
51 assert self.v < 256
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
52 return bytes([self.v])
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
53
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
54 def __repr__(self):
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
55 return 'DB {}'.format(hex(self.v))
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
56
b8ad45b3a573 Started with strings
Windel Bouwman
parents: 352
diff changeset
57
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
58 def Mov(*args):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
59 if len(args) == 2:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
60 if isinstance(args[1], int):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
61 return Mov1(*args)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
62 elif isinstance(args[1], ArmRegister):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
63 return Mov2(*args)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
64 raise Exception()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
65
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
66
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
67 class Mov1(ArmInstruction):
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
68 """ Mov Rd, imm16 """
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
69 def __init__(self, reg, imm):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
70 super().__init__()
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
71 assert type(imm) is int
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
72 self.reg = reg
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
73 self.imm = imm
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
74
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
75 def encode(self):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
76 self.token[0:12] = encode_imm32(self.imm)
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
77 self.token.Rd = self.reg.num
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
78 self.token[16:20] = 0
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
79 self.token[20] = 0 # Set flags
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
80 self.token[21:28] = 0b0011101
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
81 self.token.cond = AL
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
82 return self.token.encode()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
83
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
84 def __repr__(self):
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
85 return 'Mov {}, {}'.format(self.reg, self.imm)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
86
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
87
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
88 class Mov2(ArmInstruction):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
89 def __init__(self, rd, rm):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
90 super().__init__()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
91 self.rd = rd
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
92 self.rm = rm
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
93
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
94 def encode(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
95 self.token[0:4] = self.rm.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
96 self.token[4:12] = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
97 self.token[12:16] = self.rd.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
98 self.token[16:20] = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
99 self.token.S = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
100 self.token[21:28] = 0xD
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
101 self.token.cond = AL
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
102 return self.token.encode()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
103
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
104 def __repr__(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
105 return 'MOV {}, {}'.format(self.rd, self.rm)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
106
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
107
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
108 def Cmp(*args):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
109 if len(args) == 2:
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
110 if isinstance(args[1], int):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
111 return Cmp1(*args)
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
112 elif isinstance(args[1], ArmRegister):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
113 return Cmp2(*args)
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
114 raise Exception()
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
115
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
116
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
117 class Cmp1(ArmInstruction):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
118 """ CMP Rn, imm """
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
119 def __init__(self, reg, imm):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
120 super().__init__()
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
121 assert type(imm) is int
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
122 self.reg = reg
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
123 self.imm = imm
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
124
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
125 def encode(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
126 self.token[0:12] = encode_imm32(self.imm)
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
127 self.token.Rn = self.reg.num
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
128 self.token[20:28] = 0b00110101
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
129 self.token.cond = AL
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
130 return self.token.encode()
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
131
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
132 def __repr__(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
133 return 'CMP {}, {}'.format(self.reg, self.imm)
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
134
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
135
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
136 class Cmp2(ArmInstruction):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
137 """ CMP Rn, Rm """
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
138 def __init__(self, rn, rm):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
139 super().__init__()
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
140 self.rn = rn
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
141 self.rm = rm
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
142
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
143 def encode(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
144 self.token.Rn = self.rn.num
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
145 self.token.Rm = self.rm.num
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
146 self.token[7:16] = 0
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
147 self.token[20:28] = 0b10101
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
148 self.token.cond = AL
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
149 return self.token.encode()
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
150
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
151 def __repr__(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
152 return 'CMP {}, {}'.format(self.rn, self.rm)
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
153
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
154
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
155 def Add(*args):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
156 if len(args) == 3 and isinstance(args[0], ArmRegister) and \
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
157 isinstance(args[1], ArmRegister):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
158 if isinstance(args[2], ArmRegister):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
159 return Add1(args[0], args[1], args[2])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
160 elif isinstance(args[2], int):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
161 return Add2(args[0], args[1], args[2])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
162 raise Exception()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
163
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
164 def Sub(*args):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
165 if len(args) == 3 and isinstance(args[0], ArmRegister) and \
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
166 isinstance(args[1], ArmRegister):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
167 if isinstance(args[2], ArmRegister):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
168 return Sub1(args[0], args[1], args[2])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
169 elif isinstance(args[2], int):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
170 return Sub2(args[0], args[1], args[2])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
171 raise Exception()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
172
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
173
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
174 def Mul(*args):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
175 return Mul1(args[0], args[1], args[2])
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
176
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
177
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
178 class Mul1(ArmInstruction):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
179 def __init__(self, rd, rn, rm):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
180 super().__init__()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
181 self.rd = rd
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
182 self.rn = rn
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
183 self.rm = rm
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
184
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
185 def encode(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
186 self.token[0:4] = self.rn.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
187 self.token[4:8] = 0b1001
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
188 self.token[8:12] = self.rm.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
189 self.token[16:20] = self.rd.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
190 self.token.S = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
191 self.token.cond = AL
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
192 return self.token.encode()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
193
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
194
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
195 class OpRegRegReg(ArmInstruction):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
196 """ add rd, rn, rm """
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
197 def __init__(self, rd, rn, rm, shift=0):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
198 super().__init__()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
199 self.rd = rd
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
200 self.rn = rn
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
201 self.rm = rm
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
202
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
203 def encode(self):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
204 self.token[0:4] = self.rm.num
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
205 self.token[4] = 0
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
206 self.token[5:7] = 0
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
207 self.token[7:12] = 0 # Shift
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
208 self.token.Rd = self.rd.num
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
209 self.token.Rn = self.rn.num
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
210 self.token.S = 0 # Set flags
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
211 self.token[21:28] = self.opcode
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
212 self.token.cond = 0xE # Always!
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
213 return self.token.encode()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
214
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
215 def __repr__(self):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
216 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
217
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
218
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
219 class Add1(OpRegRegReg):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
220 mnemonic = 'ADD'
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
221 opcode = 0b0000100
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
222
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
223
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
224 class Sub1(OpRegRegReg):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
225 mnemonic = 'SUB'
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
226 opcode = 0b0000010
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
227
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
228
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
229 class Orr1(OpRegRegReg):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
230 mnemonic = 'ORR'
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
231 opcode = 0b0001100
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
232
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
233
356
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
234 class And1(OpRegRegReg):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
235 mnemonic = 'AND'
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
236 opcode = 0b0000000
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
237
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
238
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
239 class ShiftBase(ArmInstruction):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
240 """ ? rd, rn, rm """
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
241 def __init__(self, rd, rn, rm):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
242 super().__init__()
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
243 self.rd = rd
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
244 self.rn = rn
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
245 self.rm = rm
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
246
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
247 def encode(self):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
248 self.token[0:4] = self.rn.num
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
249 self.token[4:8] = self.opcode
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
250 self.token[8:12] = self.rm.num
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
251 self.token[12:16] = self.rd.num
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
252 self.token.S = 0 # Set flags
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
253 self.token[21:28] = 0b1101
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
254 self.token.cond = 0xE # Always!
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
255 return self.token.encode()
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
256
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
257 def __repr__(self):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
258 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
259
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
260
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
261 class Lsr1(ShiftBase):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
262 mnemonic = 'LSR'
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
263 opcode = 0b0011
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
264
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
265
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
266 class Lsl1(ShiftBase):
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
267 mnemonic = 'LSL'
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
268 opcode = 0b0001
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
269
52492b304adf Added newline to print
Windel Bouwman
parents: 354
diff changeset
270
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
271 class OpRegRegImm(ArmInstruction):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
272 """ add rd, rn, imm12 """
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
273 def __init__(self, rd, rn, imm):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
274 super().__init__()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
275 self.rd = rd
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
276 self.rn = rn
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
277 self.imm2 = encode_imm32(imm)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
278 self.imm = imm
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
279
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
280 def encode(self):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
281 self.token[0:12] = self.imm2
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
282 self.token.Rd = self.rd.num
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
283 self.token.Rn = self.rn.num
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
284 self.token.S = 0 # Set flags
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
285 self.token[21:28] = self.opcode
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
286 self.token.cond = 0xE # Always!
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
287 return self.token.encode()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
288
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
289 def __repr__(self):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
290 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.imm)
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
291
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
292
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
293 class Add2(OpRegRegImm):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
294 mnemonic = 'ADD'
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
295 opcode = 0b0010100
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
296
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
297
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
298 class Sub2(OpRegRegImm):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
299 mnemonic = 'SUB'
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
300 opcode = 0b0010010
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
301
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
302
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
303
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
304 # Branches:
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
305
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
306 class BranchBaseRoot(ArmInstruction):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
307 def __init__(self, target):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
308 super().__init__()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
309 self.target = target
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
310
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
311 def encode(self):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
312 self.token.cond = self.cond
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
313 self.token[24:28] = self.opcode
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
314 return self.token.encode()
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
315
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
316 def relocations(self):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
317 return [(self.target, 'b_imm24')]
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
318
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
319 def __repr__(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
320 mnemonic = self.__class__.__name__
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
321 return '{} {}'.format(mnemonic, self.target)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
322
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
323
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
324 EQ, NE, CS, CC, MI, PL, VS, VC, HI, LS, GE, LT, GT, LE, AL = range(15)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
325
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
326 class BranchBase(BranchBaseRoot):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
327 opcode = 0b1010
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
328
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
329 class BranchLinkBase(BranchBaseRoot):
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
330 opcode = 0b1011
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
331
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
332 class Bl(BranchLinkBase):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
333 cond = AL
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
334
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
335 class B(BranchBase):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
336 cond = AL
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
337
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
338 class Beq(BranchBase):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
339 cond = EQ
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
340
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
341 class Bgt(BranchBase):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
342 cond = GT
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
343
360
42343d189e14 Bugfix in for loop
Windel Bouwman
parents: 356
diff changeset
344 class Bge(BranchBase):
42343d189e14 Bugfix in for loop
Windel Bouwman
parents: 356
diff changeset
345 cond = GE
42343d189e14 Bugfix in for loop
Windel Bouwman
parents: 356
diff changeset
346
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
347 class Ble(BranchBase):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
348 cond = LE
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
349
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
350 class Blt(BranchBase):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
351 cond = LT
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
352
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
353 class Bne(BranchBase):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 350
diff changeset
354 cond = NE
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
355
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
356 # Memory:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
357
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
358 def reg_list_to_mask(reg_list):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
359 mask = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
360 for reg in reg_list:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
361 mask |= (1 << reg.num)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
362 return mask
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
363
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
364
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
365 class Push(ArmInstruction):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
366 def __init__(self, register_set):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
367 super().__init__()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
368 self.reg_list = register_set
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
369
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
370 def encode(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
371 self.token.cond = AL
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
372 self.token[16:28] = 0b100100101101
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
373 reg_list = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
374 self.token[0:16] = reg_list_to_mask(self.reg_list)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
375 return self.token.encode()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
376
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
377 def __repr__(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
378 return 'PUSH {}'.format(self.reg_list)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
379
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
380
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
381 class Pop(ArmInstruction):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
382 def __init__(self, register_set):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
383 super().__init__()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
384 self.reg_list = register_set
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
385
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
386 def encode(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
387 self.token.cond = AL
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
388 self.token[16:28] = 0b100010111101
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
389 self.token[0:16] = reg_list_to_mask(self.reg_list)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
390 return self.token.encode()
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
391
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
392 def __repr__(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
393 return 'POP {}'.format(self.reg_list)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
394
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
395
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
396 def Ldr(*args):
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
397 """ Convenience function that creates the correct instruction """
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
398 if len(args) == 3:
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
399 if isinstance(args[1], ArmRegister):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
400 return Ldr1(*args)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
401 elif len(args) == 2:
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
402 if isinstance(args[1], ArmRegister):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
403 return Ldr1(args[0], args[1], 0)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
404 elif isinstance(args[1], str):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
405 return Ldr3(*args)
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
406 raise Exception()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
407
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
408
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 365
diff changeset
409 def LdrPseudo(rt, lab):
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 365
diff changeset
410 """ Ldr rt, =lab ==> ldr rt, [pc, offset in litpool] ... dcd lab """
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 365
diff changeset
411 return Ldr(rt, R0)
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 365
diff changeset
412
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
413 def Str(*args):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
414 if len(args) == 3 and isinstance(args[1], ArmRegister):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
415 return Str1(*args)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
416 elif len(args) == 2 and isinstance(args[1], ArmRegister):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
417 return Str1(args[0], args[1], 0)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
418 raise Exception()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
419
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
420
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
421 class LdrStrBase(ArmInstruction):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
422 def __init__(self, rt, rn, offset):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
423 super().__init__()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
424 self.rt = rt
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
425 self.rn = rn
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
426 self.offset = offset
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
427
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
428 def encode(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
429 self.token.cond = AL
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
430 self.token.Rn = self.rn.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
431 self.token[25:28] = self.opcode
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
432 self.token[20] = self.bit20
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
433 self.token[12:16] = self.rt.num
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
434 self.token[24] = 1 # Index
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
435 if self.offset >= 0:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
436 self.token[23] = 1 # U == 1 'add'
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
437 self.token[0:12] = self.offset
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
438 else:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
439 self.token[23] = 0
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
440 self.token[0:12] = -self.offset
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
441 return self.token.encode()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
442
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
443 def __repr__(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
444 return '{} {}, [{}, {}]'.format(self.mnemonic, self.rt, self.rn,
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
445 hex(self.offset))
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
446
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
447
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
448 class Str1(LdrStrBase):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
449 opcode = 0b010
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
450 bit20 = 0
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
451 mnemonic = 'STR'
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
452
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
453
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
454 class Ldr1(LdrStrBase):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
455 opcode = 0b010
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
456 bit20 = 1
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
457 mnemonic = 'LDR'
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
458
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
459
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
460 class Adr(ArmInstruction):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
461 def __init__(self, rd, label):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
462 super().__init__()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
463 self.rd = rd
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
464 self.label = label
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
465
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
466 def __repr__(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
467 return 'ADR {}, {}'.format(self.rd, self.label)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
468
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
469 def relocations(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
470 return [(self.label, 'adr_imm12')]
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
471
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
472 def encode(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
473 self.token.cond = AL
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
474 self.token[0:12] = 0 # Filled by linker
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
475 self.token[12:16] = self.rd.num
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
476 self.token[16:20] = 0b1111
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
477 self.token[25] = 1
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
478 return self.token.encode()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
479
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 353
diff changeset
480
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
481 class Ldr3(ArmInstruction):
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
482 """ Load PC relative constant value
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
483 LDR rt, label
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
484 encoding A1
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
485 """
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
486 def __init__(self, rt, label):
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
487 super().__init__()
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
488 self.rt = rt
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
489 self.label = label
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
490
350
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
491 def __repr__(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
492 return 'LDR {}, {}'.format(self.rt, self.label)
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
493
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
494 def relocations(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
495 return [(self.label, 'ldr_imm12')]
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
496
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
497 def encode(self):
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
498 self.token.cond = AL
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
499 self.token[0:12] = 0 # Filled by linker
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
500 self.token[12:16] = self.rt.num
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
501 self.token[16:23] = 0b0011111
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
502 self.token[24:28] = 0b0101
2b02bd286fe9 Fixed A9 hello worle
Windel Bouwman
parents: 346
diff changeset
503 return self.token.encode()
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
504
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
505
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
506 class McrBase(ArmInstruction):
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
507 """ Mov arm register to coprocessor register """
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
508 def __init__(self, coproc, opc1, rt, crn, crm, opc2):
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
509 super().__init__()
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
510 self.coproc = coproc
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
511 self.opc1 = opc1
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
512 self.rt = rt
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
513 self.crn = crn
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
514 self.crm = crm
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
515 self.opc2 = opc2
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
516
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
517 def encode(self):
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
518 self.token[0:4] = self.crm
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
519 self.token[4] = 1
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
520 self.token[5:8] = self.opc2
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
521 self.token[8:12] = self.coproc
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
522 self.token[12:16] = self.rt.num
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
523 self.token[16:20] = self.crn
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
524 self.token[20] = self.b20
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
525 self.token[21:24] = self.opc1
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
526 self.token[24:28] = 0b1110
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
527 self.token.cond = AL
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
528 return self.token.encode()
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
529
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
530
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
531 class Mcr(McrBase):
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
532 b20 = 0
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
533
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
534
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
535 class Mrc(McrBase):
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 360
diff changeset
536 b20 = 1