comparison python/testasm.py @ 201:d5debbfc0200

Added all 27 core instructions of msp430
author Windel Bouwman
date Thu, 13 Jun 2013 00:07:28 +0200
parents 5e391d9a3381
children f22b431f4113
comparison
equal deleted inserted replaced
200:5e391d9a3381 201:d5debbfc0200
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import unittest, cProfile 3 import unittest, cProfile
4 from ppci import CompilerError 4 from ppci import CompilerError
5 from asm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber, tokenize, Assembler 5 from asmnodes import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber
6 from asm import tokenize, Assembler
6 import msp430 7 import msp430
7 8
8 class AssemblerLexingCase(unittest.TestCase): 9 class AssemblerLexingCase(unittest.TestCase):
9 """ Tests the assemblers lexer """ 10 """ Tests the assemblers lexer """
10 11
81 class AssemblerOtherTestCase(unittest.TestCase): 82 class AssemblerOtherTestCase(unittest.TestCase):
82 def testWithoutTarget(self): 83 def testWithoutTarget(self):
83 a = Assembler() 84 a = Assembler()
84 with self.assertRaises(CompilerError): 85 with self.assertRaises(CompilerError):
85 a.assemble_line('') 86 a.assemble_line('')
87
86 @unittest.skip 88 @unittest.skip
87 def testX86(self): 89 def testX86(self):
88 testsrc = """ ; tst 90 testsrc = """ ; tst
89 begin: 91 begin:
90 mov rax, rbx ; 0x48, 0x89, 0xd8 92 mov rax, rbx ; 0x48, 0x89, 0xd8
96 # Compare with nasm output: 98 # Compare with nasm output:
97 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1] 99 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
98 100
99 class AssemblerMSP430TestCase(unittest.TestCase): 101 class AssemblerMSP430TestCase(unittest.TestCase):
100 def setUp(self): 102 def setUp(self):
101 self.t = msp430.MSP430() 103 self.t = msp430.msp430target
102 self.a = Assembler(target=self.t) 104 self.a = Assembler(target=self.t)
103 105
104 def testMapInstruction(self): 106 def testMapMovInstruction(self):
105 i = AInstruction('mov', [ASymbol('r14'), ASymbol('r15')]) 107 i = AInstruction('mov', [ASymbol('r14'), ASymbol('r15')])
106 self.t.mapInstruction(i) 108 ri = self.t.mapInstruction(i)
107 109
110 def testMapRetiInstruction(self):
111 i = AInstruction('reti', [])
112 ri = self.t.mapInstruction(i)
113
114 @unittest.skip
108 def testMapOperand(self): 115 def testMapOperand(self):
109 o = ASymbol('r14') 116 o = ASymbol('r14')
110 mo = self.t.mapOperand(o) 117 mo = self.t.mapOperand(o)
111 self.assertEqual(mo, msp430.r14) 118 self.assertEqual(mo, msp430.r14)
112 119
120 @unittest.skip
113 def testMapOperandIndirection(self): 121 def testMapOperandIndirection(self):
114 o = AUnop('[]', ASymbol('r14')) 122 o = AUnop('[]', ASymbol('r14'))
115 mo = self.t.mapOperand(o) 123 mo = self.t.mapOperand(o)
116 124
117 def testMov(self): 125 def testMov(self):
118 line1 = "mov r14, r15" 126 line1 = "mov r14, r15"
119 self.a.assemble_line(line1) 127 self.a.assemble_line(line1)
128 self.assertEqual(bytes([0x0F, 0x4E]), self.a.binout)
129
130 def testMov1337(self):
131 line1 = "mov 0x1337, r12"
132 self.a.assemble_line(line1)
133 self.assertEqual(bytes([0x3C, 0x40, 0x37, 0x13]), self.a.binout)
120 134
121 def testAdd(self): 135 def testAdd(self):
122 line1 = "add r14, r15" 136 line1 = "add r15, r13"
123 self.a.assemble_line(line1) 137 self.a.assemble_line(line1)
138 self.assertEqual(bytes([0x0D, 0x5F]), self.a.binout)
139
140 def testReti(self):
141 line1 = "reti"
142 self.a.assemble_line(line1)
143 self.assertEqual(bytes([0x0, 0x13]), self.a.binout)
144
145 def testMSPinstructionCount(self):
146 """ Check that there are 27 instructions """
147 self.assertEqual(27, len(self.t.instructions))
124 148
125 149
126 if __name__ == '__main__': 150 if __name__ == '__main__':
127 # cProfile.run('unittest.main()') 151 # cProfile.run('unittest.main()')
128 unittest.main() 152 unittest.main()