def __init__(self, testname): HexTestCase.__init__(self, testname) f = open('fixtures/movingsprite/movingsprite.asm') code = f.read() f.close() tokens = lexical(code) self.ast = syntax(tokens)
def test_several_lists_with_labels(self): code = ''' .org $E000 palette: .db $0F,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F .db $0F,$30,$31,$32,$33,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$0F sprites: .db $80, $00, $03, $80; Y pos, tile id, attributes, X pos ''' tokens = list(lexical(code)) ast = syntax(tokens) self.assertEquals(4, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) self.assertEquals('.org', ast[0]['children'][0]['value']) self.assertEquals('S_DIRECTIVE', ast[1]['type']) self.assertEquals('.db', ast[1]['children'][0]['value']) self.assertEquals(['palette'], ast[1]['labels']) self.assertEquals('S_DIRECTIVE', ast[2]['type']) self.assertEquals('.db', ast[2]['children'][0]['value']) self.assertEquals('S_DIRECTIVE', ast[3]['type']) self.assertEquals('.db', ast[3]['children'][0]['value']) self.assertEquals(['sprites'], ast[3]['labels']) labels = get_labels(ast) expected = {'palette': 0xE000, 'sprites': 0xE000 + 32} self.assertEquals(expected, labels)
def test_compile_list(self): code = ''' palette: .db $0F,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F .db $0F,$30,$31,$32,$33,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$0F ''' tokens = list(lexical(code)) ast = syntax(tokens) self.assertEquals(2, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) self.assertEquals('.db', ast[0]['children'][0]['value']) self.assertEquals(32, len(ast[0]['children'])) palette1 = [ 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ] for i, p in enumerate(palette1): h = '$%02X' % p self.assertEquals(h, ast[0]['children'][i * 2 + 1]['value']) self.assertEquals('S_DIRECTIVE', ast[1]['type']) self.assertEquals('S_DIRECTIVE', ast[1]['type']) self.assertEquals('.db', ast[0]['children'][0]['value']) self.assertEquals(32, len(ast[1]['children'])) palette2 = [ 0x0f, 0x30, 0x31, 0x32, 0x33, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x0f ] for i, p in enumerate(palette2): h = '$%02X' % p self.assertEquals(h, ast[1]['children'][i * 2 + 1]['value'])
def execute(self, event): source = self.ui.editor.GetText() start_addr = 0xC000 cart = Cartridge() if start_addr != 0: cart.set_org(start_addr) opcodes = semantic(syntax(lexical(source)), False, cart) self.ui.display.active_scene.input_opcodes(opcodes, 0xC000)
def test_bank_0(self): tokens = list(lexical('.bank 0')) self.assertEquals(2, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) self.assertEquals('T_DECIMAL_ARGUMENT', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type'])
def test_org_fffa(self): tokens = list(lexical('.org $FFFA')) self.assertEquals(2, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type'])
def test_example_5_14(self): example_5_14 = ''' ; Example 5-14. Simple 16-bit square root. ; ; Returns the 8-bit square root in $20 of the ; 16-bit number in $20 (low) and $21 (high). The ; remainder is in location $21. sqrt16: LDY #$01 ; lsby of first odd number = 1 STY $22 DEY STY $23 ; msby of first odd number (sqrt = 0) again: SEC LDA $20 ; save remainder in X register TAX ; subtract odd lo from integer lo SBC $22 STA $20 LDA $21 ; subtract odd hi from integer hi SBC $23 STA $21 ; is subtract result negative? BCC nomore ; no. increment square root INY LDA $22 ; calculate next odd number ADC #$01 STA $22 BCC again INC $23 JMP again nomore: STY $20 ; all done, store square root STX $21 ; and remainder RTS ''' tokens = list(lexical(example_5_14)) self.assertEquals(74, len(tokens)) self.assertEquals('T_ENDLINE', tokens[0]['type']) self.assertEquals('T_ENDLINE', tokens[1]['type']) self.assertEquals('T_ENDLINE', tokens[2]['type']) self.assertEquals('T_ENDLINE', tokens[3]['type']) self.assertEquals('T_ENDLINE', tokens[4]['type']) self.assertEquals('T_ENDLINE', tokens[5]['type']) self.assertEquals('T_ENDLINE', tokens[6]['type']) self.assertEquals('T_LABEL', tokens[7]['type']) self.assertEquals('T_INSTRUCTION', tokens[8]['type']) self.assertEquals('T_HEX_NUMBER', tokens[9]['type']) self.assertEquals('T_ENDLINE', tokens[10]['type']) self.assertEquals('T_INSTRUCTION', tokens[11]['type']) self.assertEquals('T_ADDRESS', tokens[12]['type']) self.assertEquals('T_ENDLINE', tokens[13]['type']) self.assertEquals('T_INSTRUCTION', tokens[14]['type']) self.assertEquals('T_ENDLINE', tokens[15]['type']) self.assertEquals('T_INSTRUCTION', tokens[16]['type']) self.assertEquals('T_ADDRESS', tokens[17]['type']) self.assertEquals('T_ENDLINE', tokens[18]['type'])
def test_lda_abs(self): tokens = list(lexical('LDA $1234')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ABSOLUTE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0xad, 0x34, 0x12])
def test_ror_imm_with_binary(self): tokens = list(lexical('ROR #%00000100')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_BINARY_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_IMMEDIATE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x6a, 0x04])
def test_ror_imm_with_decimal(self): tokens = list(lexical('ROR #10')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_DECIMAL_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_IMMEDIATE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x6a, 0x0a])
def test_lsr_acc(self): tokens = list(lexical('LSR A')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ACCUMULATOR', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ACCUMULATOR', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x4a])
def test_rol_imm(self): tokens = list(lexical('ROL #$10')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_HEX_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_IMMEDIATE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x2a, 0x10])
def test_dec_zp(self): tokens = list(lexical('DEC $00')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ZEROPAGE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0xc6, 0x00])
def test_eor(self): 'Test logical EOR operation with lowercase' tokens = list(lexical('eor #$10')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_HEX_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_IMMEDIATE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x49, 0x10])
def test_inesmir(self): tokens = list(lexical('.inesmir 1')) self.assertEquals(2, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) self.assertEquals('T_DECIMAL_ARGUMENT', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) code = semantic(ast, True) # self.assertEquals(1, get_var('inesmir')) self.assertEquals(code[7], 1)
def test_dec_zpx(self): tokens = list(lexical('DEC $10,X')) self.assertEquals(4, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) self.assertEquals('T_SEPARATOR', tokens[2]['type']) self.assertEquals('T_REGISTER', tokens[3]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ZEROPAGE_X', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0xd6, 0x10])
def test_eor_zp(self): '''Test logical EOR operation between the content of the Accumulator and the content of zero page $00''' tokens = list(lexical('EOR $00')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ZEROPAGE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x45, 0x00])
def test_eor_imm_with_binary(self): '''Test logical EOR operation between binary %00000100 (Decimal 4) and the content of the Accumulator''' tokens = list(lexical('EOR #%00000100')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_BINARY_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_IMMEDIATE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x49, 0x04])
def test_db_3(self): tokens = list(lexical('.db $80, $00, $03, $80')) self.assertEquals(8, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) # self.assertEquals('T_HEX_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) code = semantic(ast) self.assertIsNotNone(code) expected = [0x80, 0x0, 0x03, 0x80] self.assertEquals(expected, code)
def test_eor_imm(self): '''Test logical EOR operation between $10 (Decimal 16) and the content of the Accumulator''' tokens = list(lexical('EOR #$10')) self.assertEquals(2, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_HEX_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_IMMEDIATE', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x49, 0x10])
def test_eor_absy(self): tokens = list(lexical('EOR $1234,Y')) self.assertEquals(4, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) self.assertEquals('T_SEPARATOR', tokens[2]['type']) self.assertEquals('T_REGISTER', tokens[3]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ABSOLUTE_Y', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x59, 0x34, 0x12])
def test_dec_absx(self): tokens = list(lexical('DEC $1234,X')) self.assertEquals(4, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_ADDRESS', tokens[1]['type']) self.assertEquals('$1234', tokens[1]['value']) self.assertEquals('T_SEPARATOR', tokens[2]['type']) self.assertEquals('T_REGISTER', tokens[3]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_ABSOLUTE_X', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0xde, 0x34, 0x12])
def test_example_16_bit_subtraction_routine(self): ex_2 = ''' SEC ;clear the carry LDA $20 ;get the low byte of the first number SBC $22 ;add to it the low byte of the second STA $24 ;store in the low byte of the result LDA $21 ;get the high byte of the first number SBC $23 ;add to it the high byte of the second, plus carry STA $25 ;store in high byte of the result ''' tokens = list(lexical(ex_2)) self.assertEquals(21, len(tokens)) ast = syntax(tokens) self.assertEquals(7, len(ast))
def test_eor_indy(self): tokens = list(lexical('EOR ($20),Y')) self.assertEquals(6, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_OPEN', tokens[1]['type']) self.assertEquals('T_ADDRESS', tokens[2]['type']) self.assertEquals('T_CLOSE', tokens[3]['type']) self.assertEquals('T_SEPARATOR', tokens[4]['type']) self.assertEquals('T_REGISTER', tokens[5]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_INDIRECT_Y', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x51, 0x20])
def test_compile_decimal(self): code = ''' LDA #128 STA $0203 ''' tokens = list(lexical(code)) self.assertEquals(7, len(tokens)) self.assertEquals('T_ENDLINE', tokens[0]['type']) self.assertEquals('T_INSTRUCTION', tokens[1]['type']) self.assertEquals('T_DECIMAL_NUMBER', tokens[2]['type']) self.assertEquals('T_ENDLINE', tokens[3]['type']) self.assertEquals('T_INSTRUCTION', tokens[4]['type']) self.assertEquals('T_ADDRESS', tokens[5]['type']) self.assertEquals('T_ENDLINE', tokens[6]['type'])
def test_compile_more_than_on_instruction(self): code = ''' SEC ;clear the carry LDA $20 ;get the low byte of the first number ''' tokens = list(lexical(code)) self.assertEquals(6, len(tokens)) self.assertEquals('T_ENDLINE', tokens[0]['type']) self.assertEquals('T_INSTRUCTION', tokens[1]['type']) self.assertEquals('T_ENDLINE', tokens[2]['type']) self.assertEquals('T_INSTRUCTION', tokens[3]['type']) self.assertEquals('T_ADDRESS', tokens[4]['type']) self.assertEquals('T_ENDLINE', tokens[5]['type']) ast = syntax(tokens) self.assertEquals(2, len(ast))
def test_sta_indx(self): tokens = list(lexical('STA ($20,X)')) self.assertEquals(6, len(tokens)) self.assertEquals('T_INSTRUCTION', tokens[0]['type']) self.assertEquals('T_OPEN', tokens[1]['type']) self.assertEquals('T_ADDRESS', tokens[2]['type']) self.assertEquals('$20', tokens[2]['value']) self.assertEquals('T_SEPARATOR', tokens[3]['type']) self.assertEquals('T_REGISTER', tokens[4]['type']) self.assertEquals('T_CLOSE', tokens[5]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_INDIRECT_X', ast[0]['type']) code = semantic(ast) self.assertEquals(code, [0x81, 0x20])
def test_db_5_with_binary(self): code = ('.db %00000000, %00010000, %01010000, ' '%00010000, %00000000, %00000000, ' '%00000000, %00110000') tokens = list(lexical(code)) self.assertEquals(16, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) self.assertEquals('T_BINARY_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) code = semantic(ast) self.assertIsNotNone(code) expected = [0x00, 0x10, 0x50, 0x10, 0x00, 0x00, 0x00, 0x30] self.assertEquals(expected, code)
def test_db_4(self): code = '''.db $80, $00, $03, $80 .db $01, $02, $03, $04 ''' tokens = list(lexical(code)) self.assertEquals(18, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) # self.assertEquals('T_HEX_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(2, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) code = semantic(ast) self.assertIsNotNone(code) expected = [0x80, 0x0, 0x03, 0x80, 1, 2, 3, 4] self.assertEquals(expected, code)
def test_db_2(self): code = ( '.db $0F,$30,$31,$32,$33,$35,$36,$37,' # One-liner string ' $38,$39,$3A,$3B,$3C,$3D,$3E,$0F') tokens = list(lexical(code)) self.assertEquals(32, len(tokens)) self.assertEquals('T_DIRECTIVE', tokens[0]['type']) # self.assertEquals('T_HEX_NUMBER', tokens[1]['type']) ast = syntax(tokens) self.assertEquals(1, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) code = semantic(ast) self.assertIsNotNone(code) expected = [ 0x0f, 0x30, 0x31, 0x32, 0x33, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x0F ] self.assertEquals(expected, code)