2 from openpower
.test
.common
import TestAccumulatorBase
3 from openpower
.endian
import bigendian
4 from openpower
.simulator
.program
import Program
5 from openpower
.decoder
.selectable_int
import SelectableInt
6 from openpower
.decoder
.power_enums
import XER_bits
7 from openpower
.decoder
.isa
.caller
import special_sprs
8 from openpower
.test
.state
import ExpectedState
12 class ALUTestCase(TestAccumulatorBase
):
14 def case_1_regression(self
):
15 lst
= [f
"add. 3, 1, 2"]
16 initial_regs
= [0] * 32
17 initial_regs
[1] = 0xc523e996a8ff6215
18 initial_regs
[2] = 0xe1e5b9cc9864c4a8
19 e
= ExpectedState(pc
=4)
20 e
.intregs
[1] = 0xc523e996a8ff6215
21 e
.intregs
[2] = 0xe1e5b9cc9864c4a8
22 e
.intregs
[3] = 0xa709a363416426bd
24 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
26 def case_2_regression(self
):
28 initial_regs
= [0] * 32
29 initial_regs
[1] = 0xb6a1fc6c8576af91
30 e
= ExpectedState(pc
=4)
31 e
.intregs
[1] = 0xb6a1fc6c8576af91
32 e
.intregs
[3] = 0xffffffff8576af91
33 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
35 lst
= [f
"subf 3, 1, 2"]
36 initial_regs
= [0] * 32
37 initial_regs
[1] = 0x3d7f3f7ca24bac7b
38 initial_regs
[2] = 0xf6b2ac5e13ee15c2
39 e
= ExpectedState(pc
=4)
40 e
.intregs
[1] = 0x3d7f3f7ca24bac7b
41 e
.intregs
[2] = 0xf6b2ac5e13ee15c2
42 e
.intregs
[3] = 0xb9336ce171a26947
43 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
45 lst
= [f
"subf 3, 1, 2"]
46 initial_regs
= [0] * 32
47 initial_regs
[1] = 0x833652d96c7c0058
48 initial_regs
[2] = 0x1c27ecff8a086c1a
49 e
= ExpectedState(pc
=4)
50 e
.intregs
[1] = 0x833652d96c7c0058
51 e
.intregs
[2] = 0x1c27ecff8a086c1a
52 e
.intregs
[3] = 0x98f19a261d8c6bc2
53 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
56 initial_regs
= [0] * 32
57 initial_regs
[1] = 0x7f9497aaff900ea0
58 e
= ExpectedState(pc
=4)
59 e
.intregs
[1] = 0x7f9497aaff900ea0
60 e
.intregs
[3] = 0xffffffffffffffa0
61 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
63 lst
= [f
"add 3, 1, 2"]
64 initial_regs
= [0] * 32
65 initial_regs
[1] = 0x2e08ae202742baf8
66 initial_regs
[2] = 0x86c43ece9efe5baa
67 e
= ExpectedState(pc
=4)
68 e
.intregs
[1] = 0x2e08ae202742baf8
69 e
.intregs
[2] = 0x86c43ece9efe5baa
70 e
.intregs
[3] = 0xb4cceceec64116a2
71 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
74 insns
= ["add", "add.", "subf"]
76 choice
= random
.choice(insns
)
77 lst
= [f
"{choice} 3, 1, 2"]
78 initial_regs
= [0] * 32
79 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
80 initial_regs
[2] = random
.randint(0, (1 << 64)-1)
82 e
= ExpectedState(pc
=4)
83 e
.intregs
[1] = initial_regs
[1]
84 e
.intregs
[2] = initial_regs
[2]
86 result
= initial_regs
[1] + initial_regs
[2]
88 e
.intregs
[3] = (result
+ 2**64) & ((2**64)-1)
90 e
.intregs
[3] = result
& ((2**64)-1)
91 elif choice
== "add.":
92 result
= initial_regs
[1] + initial_regs
[2]
94 e
.intregs
[3] = (result
+ 2**64) & ((2**64)-1)
96 e
.intregs
[3] = result
& ((2**64)-1)
100 if (e
.intregs
[3] & (1<<63)) != 0:
102 elif e
.intregs
[3] == 0:
106 e
.crregs
[0] = (eq
<<1) |
(gt
<<2) |
(le
<<3)
107 elif choice
== "subf":
108 result
= ~initial_regs
[1] + initial_regs
[2] + 1
110 e
.intregs
[3] = (result
+ 2**64) & ((2**64)-1)
112 e
.intregs
[3] = result
& ((2**64)-1)
114 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
116 def case_addme_ca_0(self
):
117 insns
= ["addme", "addme.", "addmeo", "addmeo."]
119 lst
= [f
"{choice} 6, 16"]
120 for value
in [0x7ffffffff,
122 initial_regs
= [0] * 32
123 initial_regs
[16] = value
125 xer
= SelectableInt(0, 64)
126 xer
[XER_bits
['CA']] = 0 # input carry is 0 (see test below)
127 initial_sprs
[special_sprs
['XER']] = xer
129 # create expected results. pc should be 4 (one instruction)
130 e
= ExpectedState(pc
=4)
131 # input value should not be modified
132 e
.intregs
[16] = value
133 # carry-out should always occur
135 # create output value
136 if value
== 0x7ffffffff:
137 e
.intregs
[6] = 0x7fffffffe
139 e
.intregs
[6] = 0xffff7ffff
140 # CR version needs an expected CR
143 self
.add_case(Program(lst
, bigendian
),
144 initial_regs
, initial_sprs
,
147 def case_addme_ca_1(self
):
148 insns
= ["addme", "addme.", "addmeo", "addmeo."]
150 lst
= [f
"{choice} 6, 16"]
151 for value
in [0x7ffffffff, # fails, bug #476
153 initial_regs
= [0] * 32
154 initial_regs
[16] = value
156 xer
= SelectableInt(0, 64)
157 xer
[XER_bits
['CA']] = 1 # input carry is 1 (differs from above)
158 initial_sprs
[special_sprs
['XER']] = xer
159 e
= ExpectedState(pc
=4)
160 e
.intregs
[16] = value
162 if value
== 0x7ffffffff:
163 e
.intregs
[6] = 0x7ffffffff
165 e
.intregs
[6] = 0xffff80000
168 self
.add_case(Program(lst
, bigendian
),
169 initial_regs
, initial_sprs
, expected
=e
)
171 def case_addme_ca_so_4(self
):
172 """test of SO being set
174 lst
= ["addmeo. 6, 16"]
175 initial_regs
= [0] * 32
176 initial_regs
[16] = 0x7fffffffffffffff
178 xer
= SelectableInt(0, 64)
179 xer
[XER_bits
['CA']] = 1
180 initial_sprs
[special_sprs
['XER']] = xer
181 e
= ExpectedState(pc
=4)
182 e
.intregs
[16] = 0x7fffffffffffffff
183 e
.intregs
[6] = 0x7fffffffffffffff
186 self
.add_case(Program(lst
, bigendian
),
187 initial_regs
, initial_sprs
, expected
=e
)
189 def case_addme_ca_so_3(self
):
190 """bug where SO does not get passed through to CR0
192 lst
= ["addme. 6, 16"]
193 initial_regs
= [0] * 32
194 initial_regs
[16] = 0x7ffffffff
196 xer
= SelectableInt(0, 64)
197 xer
[XER_bits
['CA']] = 1
198 xer
[XER_bits
['SO']] = 1
199 initial_sprs
[special_sprs
['XER']] = xer
200 e
= ExpectedState(pc
=4)
201 e
.intregs
[16] = 0x7ffffffff
202 e
.intregs
[6] = 0x7ffffffff
206 self
.add_case(Program(lst
, bigendian
),
207 initial_regs
, initial_sprs
, expected
=e
)
209 def case_addze(self
):
210 insns
= ["addze", "addze.", "addzeo", "addzeo."]
212 lst
= [f
"{choice} 6, 16"]
213 initial_regs
= [0] * 32
214 initial_regs
[16] = 0x00ff00ff00ff0080
215 e
= ExpectedState(pc
=4)
216 e
.intregs
[16] = 0xff00ff00ff0080
217 e
.intregs
[6] = 0xff00ff00ff0080
220 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
222 def case_addis_nonzero_r0_regression(self
):
223 lst
= [f
"addis 3, 0, 1"]
225 initial_regs
= [0] * 32
227 e
= ExpectedState(initial_regs
, pc
=4)
228 e
.intregs
[3] = 0x10000
229 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
231 def case_addis_nonzero_r0(self
):
233 imm
= random
.randint(-(1 << 15), (1 << 15)-1)
234 lst
= [f
"addis 3, 0, {imm}"]
236 initial_regs
= [0] * 32
237 initial_regs
[0] = random
.randint(0, (1 << 64)-1)
238 e
= ExpectedState(pc
=4)
239 e
.intregs
[0] = initial_regs
[0]
240 e
.intregs
[3] = (imm
<< 16) & ((1<<64)-1)
241 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
243 def case_rand_imm(self
):
244 insns
= ["addi", "addis", "subfic"]
246 choice
= random
.choice(insns
)
247 imm
= random
.randint(-(1 << 15), (1 << 15)-1)
248 lst
= [f
"{choice} 3, 1, {imm}"]
250 initial_regs
= [0] * 32
251 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
253 e
= ExpectedState(pc
=4)
254 e
.intregs
[1] = initial_regs
[1]
256 result
= initial_regs
[1] + imm
257 e
.intregs
[3] = result
& ((2**64)-1)
258 elif choice
== "addis":
259 result
= initial_regs
[1] + (imm
<<16)
260 e
.intregs
[3] = result
& ((2**64)-1)
261 elif choice
== "subfic":
262 result
= ~initial_regs
[1] + imm
+ 1
263 value
= (~initial_regs
[1]+2**64) + (imm
) + 1
266 carry_out
= value
& (1<<64) != 0
267 value
= (~initial_regs
[1]+2**64 & 0xffff_ffff) + imm
+ 1
270 carry_out32
= value
& (1<<32) != 0
271 e
.intregs
[3] = result
& ((2**64)-1)
272 e
.ca
= carry_out |
(carry_out32
<<1)
274 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
276 def case_0_adde(self
):
277 lst
= ["adde. 5, 6, 7"]
279 initial_regs
= [0] * 32
280 initial_regs
[6] = random
.randint(0, (1 << 64)-1)
281 initial_regs
[7] = random
.randint(0, (1 << 64)-1)
283 xer
= SelectableInt(0, 64)
284 xer
[XER_bits
['CA']] = 1
285 initial_sprs
[special_sprs
['XER']] = xer
286 # calculate result *including carry* and mask it to 64-bit
287 # (if it overflows, we don't care, because this is not addeo)
288 result
= 1 + initial_regs
[6] + initial_regs
[7]
289 carry_out
= result
& (1<<64) != 0 # detect 65th bit as carry-out?
290 carry_out32
= (initial_regs
[6] & 0xffff_ffff) + \
291 (initial_regs
[7] & 0xffff_ffff) & (1<<32) != 0
292 result
= result
& ((1<<64)-1) # round
296 if (result
& (1<<63)) != 0:
302 # now construct the state
303 e
= ExpectedState(pc
=4)
304 e
.intregs
[6] = initial_regs
[6] # should be same as initial
305 e
.intregs
[7] = initial_regs
[7] # should be same as initial
306 e
.intregs
[5] = result
307 # carry_out goes into bit 0 of ca, carry_out32 into bit 1
308 e
.ca
= carry_out |
(carry_out32
<<1)
309 # eq goes into bit 1 of CR0, gt into bit 2, le into bit 3.
310 # SO goes into bit 0 but overflow doesn't occur here [we hope]
311 e
.crregs
[0] = (eq
<<1) |
(gt
<<2) |
(le
<<3)
313 self
.add_case(Program(lst
, bigendian
),
314 initial_regs
, initial_sprs
, expected
=e
)
317 lst
= ["subf. 1, 6, 7",
319 initial_regs
= [0] * 32
320 initial_regs
[6] = 0x10
321 initial_regs
[7] = 0x05
322 e
= ExpectedState(pc
=8)
325 e
.intregs
[1] = 0xfffffffffffffff5
328 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
331 lst
= ["cmp cr2, 0, 2, 3"]
332 initial_regs
= [0] * 32
333 initial_regs
[2] = 0xffffffffaaaaaaaa
334 initial_regs
[3] = 0x00000000aaaaaaaa
335 e
= ExpectedState(pc
=4)
336 e
.intregs
[2] = 0xffffffffaaaaaaaa
337 e
.intregs
[3] = 0xaaaaaaaa
339 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
341 lst
= ["cmp cr2, 0, 4, 5"]
342 initial_regs
= [0] * 32
343 initial_regs
[4] = 0x00000000aaaaaaaa
344 initial_regs
[5] = 0xffffffffaaaaaaaa
345 e
= ExpectedState(pc
=4)
346 e
.intregs
[4] = 0xaaaaaaaa
347 e
.intregs
[5] = 0xffffffffaaaaaaaa
349 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
352 lst
= ["cmp cr2, 1, 2, 3"]
353 initial_regs
= [0] * 32
354 initial_regs
[2] = 0xffffffffaaaaaaaa
355 initial_regs
[3] = 0x00000000aaaaaaaa
356 e
= ExpectedState(pc
=4)
357 e
.intregs
[2] = 0xffffffffaaaaaaaa
358 e
.intregs
[3] = 0xaaaaaaaa
360 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
362 lst
= ["cmp cr2, 1, 4, 5"]
363 initial_regs
= [0] * 32
364 initial_regs
[4] = 0x00000000aaaaaaaa
365 initial_regs
[5] = 0xffffffffaaaaaaaa
366 e
= ExpectedState(pc
=4)
367 e
.intregs
[4] = 0xaaaaaaaa
368 e
.intregs
[5] = 0xffffffffaaaaaaaa
370 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
372 def case_cmpl_microwatt_0(self
):
374 115b8: 40 50 d1 7c .long 0x7cd15040 # cmpl 6, 0, 17, 10
375 register_file.vhdl: Reading GPR 11 000000000001C026
376 register_file.vhdl: Reading GPR 0A FEDF3FFF0001C025
377 cr_file.vhdl: Reading CR 35055050
378 cr_file.vhdl: Writing 35055058 to CR mask 01 35055058
381 lst
= ["cmpl 6, 0, 17, 10"]
382 initial_regs
= [0] * 32
383 initial_regs
[0x11] = 0x1c026
384 initial_regs
[0xa] = 0xFEDF3FFF0001C025
388 e
= ExpectedState(pc
=4)
389 e
.intregs
[10] = 0xfedf3fff0001c025
390 e
.intregs
[17] = 0x1c026
400 self
.add_case(Program(lst
, bigendian
), initial_regs
,
401 initial_sprs
= {'XER': XER
},
402 initial_cr
= CR
, expected
=e
)
404 def case_cmpl_microwatt_0_disasm(self
):
405 """microwatt 1.bin: disassembled version
406 115b8: 40 50 d1 7c .long 0x7cd15040 # cmpl 6, 0, 17, 10
407 register_file.vhdl: Reading GPR 11 000000000001C026
408 register_file.vhdl: Reading GPR 0A FEDF3FFF0001C025
409 cr_file.vhdl: Reading CR 35055050
410 cr_file.vhdl: Writing 35055058 to CR mask 01 35055058
413 dis
= ["cmpl 6, 0, 17, 10"]
414 lst
= bytes([0x40, 0x50, 0xd1, 0x7c]) # 0x7cd15040
415 initial_regs
= [0] * 32
416 initial_regs
[0x11] = 0x1c026
417 initial_regs
[0xa] = 0xFEDF3FFF0001C025
421 e
= ExpectedState(pc
=4)
422 e
.intregs
[10] = 0xfedf3fff0001c025
423 e
.intregs
[17] = 0x1c026
433 p
= Program(lst
, bigendian
)
434 p
.assembly
= '\n'.join(dis
)+'\n'
435 self
.add_case(p
, initial_regs
,
436 initial_sprs
= {'XER': XER
},
437 initial_cr
= CR
, expected
=e
)
439 def case_cmplw_microwatt_1(self
):
441 10d94: 40 20 96 7c cmplw cr1,r22,r4
442 gpr: 00000000ffff6dc1 <- r4
443 gpr: 0000000000000000 <- r22
446 lst
= ["cmpl 1, 0, 22, 4"]
447 initial_regs
= [0] * 32
448 initial_regs
[4] = 0xffff6dc1
453 e
= ExpectedState(pc
=4)
454 e
.intregs
[4] = 0xffff6dc1
467 self
.add_case(Program(lst
, bigendian
), initial_regs
,
468 initial_sprs
= {'XER': XER
},
469 initial_cr
= CR
, expected
=e
)
471 def case_cmpli_microwatt(self
):
472 """microwatt 1.bin: cmpli
473 123ac: 9c 79 8d 2a cmpli cr5,0,r13,31132
474 gpr: 00000000301fc7a7 <- r13
475 cr : 0000000090215393
476 xer: so 1 ca 0 32 0 ov 0 32 0
480 lst
= ["cmpli 5, 0, 13, 31132"]
481 initial_regs
= [0] * 32
482 initial_regs
[13] = 0x301fc7a7
486 e
= ExpectedState(pc
=4)
487 e
.intregs
[13] = 0x301fc7a7
499 self
.add_case(Program(lst
, bigendian
), initial_regs
,
500 initial_sprs
= {'XER': XER
},
501 initial_cr
= CR
, expected
=e
)
503 def case_extsb(self
):
504 insns
= ["extsb", "extsh", "extsw"]
506 choice
= random
.choice(insns
)
507 lst
= [f
"{choice} 3, 1"]
509 initial_regs
= [0] * 32
510 initial_regs
[1] = random
.randint(0, (1 << 64)-1)
512 e
= ExpectedState(pc
=4)
513 e
.intregs
[1] = initial_regs
[1]
514 if choice
== "extsb":
515 s
= ((initial_regs
[1] & 0x1000_0000_0000_0080)>>7)&0x1
517 value
= 0xffff_ffff_ffff_ff<<8
520 e
.intregs
[3] = value |
(initial_regs
[1] & 0xff)
521 elif choice
== "extsh":
522 s
= ((initial_regs
[1] & 0x1000_0000_0000_8000)>>15)&0x1
524 value
= 0xffff_ffff_ffff<<16
527 e
.intregs
[3] = value |
(initial_regs
[1] & 0xffff)
529 s
= ((initial_regs
[1] & 0x1000_0000_8000_0000)>>31)&0x1
531 value
= 0xffff_ffff<<32
534 e
.intregs
[3] = value |
(initial_regs
[1] & 0xffff_ffff)
536 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)
538 def case_cmpeqb(self
):
539 lst
= ["cmpeqb cr1, 1, 2"]
541 initial_regs
= [0] * 32
543 initial_regs
[2] = 0x0001030507090b0f
545 e
= ExpectedState(pc
=4)
547 e
.intregs
[2] = 0x1030507090b0f
548 matlst
= [ 0x00, 0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x0f ]
553 self
.add_case(Program(lst
, bigendian
), initial_regs
, expected
=e
)