comparison python/cortexm3.py @ 238:90637d1bbfad

Added test sequence 2
author Windel Bouwman
date Sat, 20 Jul 2013 13:18:04 +0200
parents 81752b0f85a5
children 6ed3d3a82a63
comparison
equal deleted inserted replaced
237:81752b0f85a5 238:90637d1bbfad
343 343
344 @armtarget.instruction 344 @armtarget.instruction
345 class movregreg_ins(ArmInstruction): 345 class movregreg_ins(ArmInstruction):
346 """ mov Rd, Rm """ 346 """ mov Rd, Rm """
347 mnemonic = 'mov' 347 mnemonic = 'mov'
348 operands = (RegOp, RegOp) 348 operands = (Reg8Op, Reg8Op)
349 def __init__(self, rd, rm): 349 def __init__(self, rd, rm):
350 self.rd = rd 350 self.rd = rd
351 self.rm = rm 351 self.rm = rm
352 def encode(self): 352 def encode(self):
353 rd = self.rd.num 353 rd = self.rd.num
354 D = (rd & 0x8) >> 3
355 assert D < 2
356 rd = rd & 0x7
357 rm = self.rm.num 354 rm = self.rm.num
358 assert rm < 16 355 h = 0 | (rm << 3) | rd
359 opcode = self.opcode
360 h = (1 << 14) | (3 << 9) | (D << 7) | (rm << 3) | rd
361 return u16(h) 356 return u16(h)
362 357
363 358
364 359
365 # Arithmatics: 360 # Arithmatics:
459 h = (opcode << 11) | (rn << 8) | imm 454 h = (opcode << 11) | (rn << 8) | imm
460 return u16(h) 455 return u16(h)
461 456
462 # Jumping: 457 # Jumping:
463 458
459 def wrap_negative(x, bits):
460 b = struct.unpack('<I', struct.pack('<i', x))[0]
461 mask = (1 << bits) - 1
462 return b & mask
463
464 class jumpBase_ins(ArmInstruction): 464 class jumpBase_ins(ArmInstruction):
465 operands = (LabelRef,) 465 operands = (LabelRef,)
466 def __init__(self, target_label): 466 def __init__(self, target_label):
467 assert type(target_label) is LabelRef 467 assert type(target_label) is LabelRef
468 self.target = target_label 468 self.target = target_label
469 self.offset = 0 469 self.offset = 0
470 470
471 def resolve(self, f): 471 def resolve(self, f):
472 la = f(self.target.name) 472 la = f(self.target.name)
473 sa = self.address 473 sa = self.address + 4
474 self.offset = (la - sa) 474 self.offset = (la - sa)
475 if self.offset < 0: 475 #if self.offset < 0:
476 # TODO: handle negative jump 476 # # TODO: handle negative jump
477 self.offset = 0 477 # self.offset = 0
478 478
479 def __repr__(self): 479 def __repr__(self):
480 return '{} {}'.format(self.mnemonic, self.target.name) 480 return '{} {}'.format(self.mnemonic, self.target.name)
481 481
482 @armtarget.instruction 482 @armtarget.instruction
483 class b_ins(jumpBase_ins): 483 class b_ins(jumpBase_ins):
484 mnemonic = 'B' 484 mnemonic = 'B'
485 485 def encode(self):
486 def encode(self): 486 imm11 = wrap_negative(self.offset >> 1, 11)
487 imm11 = self.offset >> 1 487 h = (0b11100 << 11) | imm11 # | 1 # 1 to enable thumb mode
488 h = (0b11100 << 11) | imm11 | 1 # 1 to enable thumb mode
489 return u16(h) 488 return u16(h)
490 489
491 class cond_base_ins(jumpBase_ins): 490 class cond_base_ins(jumpBase_ins):
492 def encode(self): 491 def encode(self):
493 imm8 = 0 492 imm8 = wrap_negative(self.offset >> 1, 8)
494 h = (0b1101 << 12) | (self.cond << 8) | imm8 493 h = (0b1101 << 12) | (self.cond << 8) | imm8
495 return u16(h) 494 return u16(h)
496 495
497 @armtarget.instruction 496 @armtarget.instruction
498 class beq_ins(cond_base_ins): 497 class beq_ins(cond_base_ins):