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_get_labels(self): expected = {} expected['WAITVBLANK'] = 0xC000 expected['palette'] = 0xE000 expected['sprites'] = 0xE000 + 32 actual = get_labels(self.ast) self.assertEquals(expected['WAITVBLANK'], actual['WAITVBLANK']) self.assertEquals(expected['palette'], actual['palette']) self.assertEquals(expected['sprites'], actual['sprites'])
def test_compare_labels(self): labels_file = splitext(self._input_c)[0] + '.fns' labels_c = {} with open(labels_file, 'r') as f: for line in f: if not line.startswith(';'): key, value = [d.strip() for d in line.split('=')] labels_c[key] = value from nesasm.compiler import lexical, syntax, get_labels with open(self._input_py) as f: source = f.read() ast = syntax(lexical(source)) try: _items = get_labels(ast).iteritems except AttributeError: _items = get_labels(ast).items labels_py = {k: '${:02X}'.format(v) for k, v in _items()} self.assertEquals(labels_c, labels_py)
def test_instructions_with_labels(self): code = ''' .org $C000 WAITVBLANK: BIT $2002 BPL WAITVBLANK RTS''' tokens = list(lexical(code)) ast = syntax(tokens) self.assertEquals(4, len(ast)) self.assertEquals('S_DIRECTIVE', ast[0]['type']) self.assertEquals('S_ABSOLUTE', ast[1]['type']) self.assertEquals(['WAITVBLANK'], ast[1]['labels']) labels = get_labels(ast) expected = {'WAITVBLANK': 0xc000} self.assertEquals(expected, labels)
def get_labels(self, source, start_addr=0): cart = Cartridge() if start_addr != 0: cart.set_org(start_addr) return get_labels(syntax(lexical(source)), cart)