class CartridgeTest(unittest.TestCase): def setUp(self): self.cart = Cartridge() def tearDown(self): self.cart = None def test_inesprg_1(self): self.cart.set_iNES_prg(1) self.assertEquals(1, self.cart.inespgr) def test_inesprg_2(self): self.cart.set_iNES_prg(2) self.assertEquals(2, self.cart.inespgr) def test_ineschr(self): self.cart.set_iNES_chr(1) self.assertEquals(1, self.cart.ineschr) def test_inesmap(self): self.cart.set_iNES_map(1) self.assertEquals(1, self.cart.inesmap) def test_inesmir(self): self.cart.set_iNES_mir(1) self.assertEquals(1, self.cart.inesmir) def test_bank_1(self): self.cart.set_bank_id(0) self.assertEquals(1, len(self.cart.banks)) def test_bank_2(self): self.cart.set_bank_id(0) self.cart.set_bank_id(1) self.assertEquals(2, len(self.cart.banks)) def test_org_1(self): self.cart.set_bank_id(0) self.cart.set_org(0xc000) self.assertEquals(0xc000, self.cart.banks[0]['start']) def test_append_code(self): code = [0x4e, 0x45, 0x53, 0x1a] self.cart.append_code(code) self.assertEquals(4, self.cart.pc) self.assertEquals(code, self.cart.get_code()) def test_using_org_to_jump(self): self.cart.set_bank_id(0) self.cart.set_org(0xc000) code = [0x4e, 0x45, 0x53, 0x1a] self.cart.append_code(code) self.cart.set_org(0xc000 + 8) self.cart.append_code(code) self.assertEquals( [0x4e, 0x45, 0x53, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x4e, 0x45, 0x53, 0x1a], self.cart.get_code() )
def semantic(ast, iNES=False, cart=None): if cart is None: cart = Cartridge() labels = get_labels(ast) address = 0 # translate statments to opcode for leaf in ast: if leaf['type'] == 'S_RS': labels[leaf['children'][0]['value']] = cart.rs cart.rs += get_value(leaf['children'][2]) elif leaf['type'] == 'S_DIRECTIVE': directive = leaf['children'][0]['value'] if len(leaf['children']) == 2: argument = get_value(leaf['children'][1], labels) else: argument = leaf['children'][1:] if directive in directive_list: directive_list[directive](argument, cart) else: raise Exception('UNKNOW DIRECTIVE') else: if leaf['type'] in ['S_IMPLIED', 'S_ACCUMULATOR']: instruction = leaf['children'][0]['value'] address = False elif leaf['type'] == 'S_RELATIVE': instruction = leaf['children'][0]['value'] address = get_value(leaf['children'][1], labels) elif leaf['type'] == 'S_IMMEDIATE_WITH_MODIFIER': instruction = leaf['children'][0]['value'] modifier = leaf['children'][1]['value'] address = get_value(leaf['children'][3], labels) if modifier == '#LOW': address = (address & 0x00ff) elif modifier == '#HIGH': address = (address & 0xff00) >> 8 elif leaf['type'] in [ 'S_RELATIVE', 'S_IMMEDIATE', 'S_ZEROPAGE', 'S_ABSOLUTE', 'S_ZEROPAGE_X', 'S_ZEROPAGE_Y', 'S_ABSOLUTE_X', 'S_ABSOLUTE_Y' ]: instruction = leaf['children'][0]['value'] address = get_value(leaf['children'][1], labels) elif leaf['type'] in ['S_INDIRECT_X', 'S_INDIRECT_Y']: instruction = leaf['children'][0]['value'] address = get_value(leaf['children'][2], labels) address_mode = address_mode_def[leaf['type']]['short'] opcode = opcodes[instruction][address_mode] if address_mode != 'sngl' and address_mode != 'acc': if 'rel' == address_mode: address = 126 + (address - cart.pc) if address == 128: address = 0 elif address < 128: address = address | 0b10000000 elif address > 128: address = address & 0b01111111 if address_mode_def[leaf['type']]['size'] == 2: cart.append_code([opcode, address]) else: arg1 = (address & 0x00ff) arg2 = (address & 0xff00) >> 8 cart.append_code([opcode, arg1, arg2]) else: cart.append_code([opcode]) # nes_code = [] if iNES: return cart.get_ines_code() else: return cart.get_code()
def semantic(ast, iNES=False, cart=None ): if cart == None: cart = Cartridge() labels = get_labels(ast) address = 0 #translate statments to opcode for leaf in ast: if leaf['type'] == 'S_RS': labels[leaf['children'][0]['value']] = cart.rs; cart.rs += get_value(leaf['children'][2]); elif leaf['type'] == 'S_DIRECTIVE': directive = leaf['children'][0]['value'] if len(leaf['children']) == 2: argument = get_value(leaf['children'][1], labels) else: argument = leaf['children'][1:] if directive in directive_list: directive_list[directive](argument, cart) else: raise Exception('UNKNOW DIRECTIVE') else: if leaf['type'] in ['S_IMPLIED', 'S_ACCUMULATOR']: instruction = leaf['children'][0]['value'] address = False elif leaf['type'] == 'S_RELATIVE': instruction = leaf['children'][0]['value'] address = get_value(leaf['children'][1], labels) elif leaf['type'] == 'S_IMMEDIATE_WITH_MODIFIER': instruction = leaf['children'][0]['value'] modifier = leaf['children'][1]['value'] address = get_value(leaf['children'][3], labels); if modifier == '#LOW': address = (address & 0x00ff) elif modifier == '#HIGH': address = (address & 0xff00) >> 8; elif leaf['type'] in [ 'S_RELATIVE', 'S_IMMEDIATE', 'S_ZEROPAGE', 'S_ABSOLUTE', 'S_ZEROPAGE_X', 'S_ZEROPAGE_Y', 'S_ABSOLUTE_X', 'S_ABSOLUTE_Y']: instruction = leaf['children'][0]['value'] address = get_value(leaf['children'][1], labels) elif leaf['type'] in ['S_INDIRECT_X', 'S_INDIRECT_Y']: instruction = leaf['children'][0]['value'] address = get_value(leaf['children'][2], labels) address_mode = address_mode_def[leaf['type']]['short'] opcode = opcodes[instruction][address_mode] if address_mode != 'sngl' and address_mode != 'acc': if 'rel' == address_mode: address = 126 + (address - cart.pc) if address == 128: address = 0 elif address < 128: address = address | 0b10000000 elif address > 128: address = address & 0b01111111 if address_mode_def[leaf['type']]['size'] == 2: cart.append_code([opcode, address]) else: arg1 = (address & 0x00ff) arg2 = (address & 0xff00) >> 8 cart.append_code([opcode, arg1, arg2]) else: cart.append_code([opcode]) nes_code = [] if iNES: return cart.get_ines_code() else: return cart.get_code()
def semantic(ast, iNES=False, cart=None): if cart is None: cart = Cartridge() labels = get_labels(ast) address = 0 # translate statments to opcode for leaf in ast: if leaf["type"] == "S_RS": labels[leaf["children"][0]["value"]] = cart.rs cart.rs += get_value(leaf["children"][2]) elif leaf["type"] == "S_DIRECTIVE": directive = leaf["children"][0]["value"] if len(leaf["children"]) == 2: argument = get_value(leaf["children"][1], labels) else: argument = leaf["children"][1:] if directive in directive_list: directive_list[directive](argument, cart) else: raise Exception("UNKNOW DIRECTIVE") else: if leaf["type"] in ["S_IMPLIED", "S_ACCUMULATOR"]: instruction = leaf["children"][0]["value"] address = False elif leaf["type"] == "S_RELATIVE": instruction = leaf["children"][0]["value"] address = get_value(leaf["children"][1], labels) elif leaf["type"] == "S_IMMEDIATE_WITH_MODIFIER": instruction = leaf["children"][0]["value"] modifier = leaf["children"][1]["value"] address = get_value(leaf["children"][3], labels) if modifier == "#LOW": address = address & 0x00FF elif modifier == "#HIGH": address = (address & 0xFF00) >> 8 elif leaf["type"] in [ "S_RELATIVE", "S_IMMEDIATE", "S_ZEROPAGE", "S_ABSOLUTE", "S_ZEROPAGE_X", "S_ZEROPAGE_Y", "S_ABSOLUTE_X", "S_ABSOLUTE_Y", ]: instruction = leaf["children"][0]["value"] address = get_value(leaf["children"][1], labels) elif leaf["type"] in ["S_INDIRECT_X", "S_INDIRECT_Y"]: instruction = leaf["children"][0]["value"] address = get_value(leaf["children"][2], labels) address_mode = address_mode_def[leaf["type"]]["short"] opcode = opcodes[instruction][address_mode] if address_mode != "sngl" and address_mode != "acc": if "rel" == address_mode: address = 126 + (address - cart.pc) if address == 128: address = 0 elif address < 128: address = address | 0b10000000 elif address > 128: address = address & 0b01111111 if address_mode_def[leaf["type"]]["size"] == 2: cart.append_code([opcode, address]) else: arg1 = address & 0x00FF arg2 = (address & 0xFF00) >> 8 cart.append_code([opcode, arg1, arg2]) else: cart.append_code([opcode]) # nes_code = [] if iNES: return cart.get_ines_code() else: return cart.get_code()