예제 #1
0
 def test_binary_string_regression(self, print_mock):
     statements = [
         Statement("  NAM LITERAL"),
         Statement("  ORG $0600"),
         Statement("START LDA #%10101010"),
         Statement("  END START"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     program.print_statements()
     self.assertEqual([
         call("-- Assembled Statements --"),
         call(
             "$0000                         NAM LITERAL                        ;                                         "
         ),
         call(
             "$0600                         ORG $0600                          ;                                         "
         ),
         call(
             "$0600 86AA            START   LDA #%10101010                     ;                                         "
         ),
         call(
             "$0602                         END START                          ;                                         "
         ),
     ], print_mock.mock_calls)
예제 #2
0
 def test_print_symbol_table_corect(self, print_mock):
     statement = Statement("POLCAT EQU $FFEE")
     program = Program()
     program.save_symbol(0, statement)
     program.print_symbol_table()
     self.assertEqual([call("-- Symbol Table --"),
                       call("$FFEE POLCAT")], print_mock.mock_calls)
예제 #3
0
def main(args):
    """
    Runs the assembler with the specified arguments.

    :param args: the command-line arguments
    """
    program = Program()
    program.process(args.filename)
    name = program.name or args.name

    if args.symbols:
        program.print_symbol_table()

    if args.print:
        program.print_statements()

    if args.bin_file:
        binary_file = BinaryFile()
        binary_file.open_host_file(args.bin_file)
        binary_file.save_file(None, program.get_binary_array())
        binary_file.close_host_file()

    if args.cas_file:
        if not name:
            print("No name for the program specified, not creating cassette file")
            return
        try:
            binary_file = CassetteFile(program.origin, program.origin)
            binary_file.open_host_file(args.cas_file, append=args.append)
            binary_file.save_file(name, program.get_binary_array())
            binary_file.close_host_file()
        except ValueError as error:
            print("Unable to save cassette file:")
            print(error)
예제 #4
0
 def test_save_symbol_raises_if_redefined(self):
     statement = Statement("BLAH    JMP $FFFF")
     program = Program()
     program.symbol_table = {"BLAH": 1234}
     with self.assertRaises(TranslationError) as context:
         program.save_symbol(0, statement)
     self.assertEqual("'Label [BLAH] redefined'", str(context.exception))
예제 #5
0
 def test_character_literal_regression(self, print_mock):
     statements = [
         Statement("  NAM LITERAL"),
         Statement("  ORG $0600"),
         Statement("START LDA #'C"),
         Statement("  END START"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     program.print_statements()
     self.assertEqual([
         call("-- Assembled Statements --"),
         call(
             "$0000                         NAM LITERAL                        ;                                         "
         ),
         call(
             "$0600                         ORG $0600                          ;                                         "
         ),
         call(
             "$0600 8643            START   LDA #'C                            ;                                         "
         ),
         call(
             "$0602                         END START                          ;                                         "
         ),
     ], print_mock.mock_calls)
예제 #6
0
 def test_indexed_addressing_direct_fixed_size_correct(self):
     statements = [
         Statement("     STX 1,PCR"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xAF, 0x8C, 0x01], program.get_binary_array())
예제 #7
0
 def test_assembly_line_regex_with_inherent_operands(self):
     statements = [
         Statement("     RTS            ;"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0x39], program.get_binary_array())
예제 #8
0
 def test_get_binary_array_all_correct(self):
     statement1 = Statement("    JMP $FFFF")
     statement1.code_pkg.op_code = NumericValue("$DEAD")
     statement1.code_pkg.post_byte = NumericValue("$BEEF")
     statement1.code_pkg.additional = NumericValue("$CAFE")
     program = Program()
     program.statements = [statement1]
     self.assertEqual([0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE], program.get_binary_array())
예제 #9
0
 def test_save_symbol_does_nothing_if_no_label(self):
     statement = Statement("    JMP $FFFF")
     program = Program()
     program.symbol_table = {
         "BLAH": 1234
     }
     program.save_symbol(0, statement)
     self.assertEqual({"BLAH": 1234}, program.symbol_table)
예제 #10
0
 def test_extended_indexed_addressing_extended_fixed_size_correct(self):
     statements = [
         Statement("     STX [$0102,PCR]"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xAF, 0x9D, 0x01, 0x02], program.get_binary_array())
예제 #11
0
 def test_extended_indexed_addressing_expression_rhs_8_bit_correct(self):
     statements = [
         Statement("TEMP  EQU $01"),
         Statement("START STX [1+TEMP,PCR]"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xAF, 0x9C, 0x02], program.get_binary_array())
예제 #12
0
 def test_explicit_extended_addressing_operand(self):
     statements = [
         Statement("         ORG $0100           ;"),
         Statement("START    LDA >$0001            ;"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xB6, 0x00, 0x01], program.get_binary_array())
예제 #13
0
 def test_extended_indexed_expression_with_address_resolves_correct(self):
     statements = [
         Statement("       STX [1+ADDR,PCR] "),
         Statement("ADDR   NOP "),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xAF, 0x9C, 0x04, 0x12], program.get_binary_array())
예제 #14
0
 def test_expression_16_bit_correct(self):
     statements = [
         Statement("VAR      EQU $002"),
         Statement("         LDD #VAR+1"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xCC, 0x00, 0x03], program.get_binary_array())
예제 #15
0
 def test_immediate_symbol_expression(self):
     statements = [
         Statement("VAR      EQU $01             ;"),
         Statement("         ORG $0100           ;"),
         Statement("         LDA #VAR+1          ;"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0x86, 0x02], program.get_binary_array())
예제 #16
0
 def test_string_definition(self):
     statements = [
         Statement("         FCC \"PRESS S TO RESTART,\""),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([
         0x50, 0x52, 0x45, 0x53, 0x53, 0x20, 0x53, 0x20, 0x54, 0x4F, 0x20,
         0x52, 0x45, 0x53, 0x54, 0x41, 0x52, 0x54, 0x2C
     ], program.get_binary_array())
예제 #17
0
 def test_throw_error_works_correctly(self, print_mock):
     statement = Statement("LABEL JMP $FFFF ; comment")
     program = Program()
     with self.assertRaises(SystemExit):
         program.throw_error(TranslationError("test", statement))
     self.assertEqual([
         call("test"),
         call(
             "$                 LABEL   JMP $FFFF                          ; comment                                 "
         )
     ], print_mock.mock_calls)
예제 #18
0
 def test_print_statements_correct(self, print_mock):
     statement = Statement("LABEL JMP $FFFF ; comment")
     program = Program()
     program.statements = [statement]
     program.print_statements()
     self.assertEqual([
         call("-- Assembled Statements --"),
         call(
             "$                 LABEL   JMP $FFFF                          ; comment                                 "
         ),
     ], print_mock.mock_calls)
예제 #19
0
 def test_assembly_line_regex_with_at_symbols_in_operands_and_labels(self):
     statements = [
         Statement("         ORG $0100           ;"),
         Statement("START    LDA #$01            ;"),
         Statement("         LDA X@              ;"),
         Statement("X@       FCB 0               ;"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0x86, 0x01, 0xB6, 0x01, 0x05, 0x00],
                      program.get_binary_array())
예제 #20
0
 def test_expression_subtraction_with_address_on_left(self):
     statements = [
         Statement("     ORG $0E00"),
         Statement("V    STX R-1"),
         Statement("     FCB 0"),
         Statement("R    FCB 0"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0xBF, 0x0E, 0x03, 0x00, 0x00],
                      program.get_binary_array())
예제 #21
0
 def test_program_counter_relative_8_bit_offset_extended_reverse(self):
     statements = [
         Statement("     ORG $0600"),
         Statement("V    FCB 0"),
         Statement("B    LDA $FF"),
         Statement("     STY [V,PCR]"),
         Statement("     END B"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0x00, 0x96, 0xFF, 0x10, 0xAF, 0x9C, 0xF9],
                      program.get_binary_array())
예제 #22
0
 def test_load_effective_address_extended_indexed_program_counter_relative_8_bit(
         self):
     statements = [
         Statement("     ORG $0600"),
         Statement("B    LEAX [Z,PCR]"),
         Statement("     LDA $FF"),
         Statement("Z    RTS  "),
         Statement("     END B"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0x30, 0x9C, 0x02, 0x96, 0xFF, 0x39],
                      program.get_binary_array())
예제 #23
0
 def test_program_counter_relative_8_bit_offset_forward(self):
     statements = [
         Statement("     ORG $0600"),
         Statement("B    LDA $FF"),
         Statement("     STY V,PCR"),
         Statement("     INCA "),
         Statement("V    FCB 0"),
         Statement("     END B"),
     ]
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([0x96, 0xFF, 0x10, 0xAF, 0x8C, 0x01, 0x4C, 0x00],
                      program.get_binary_array())
예제 #24
0
def main(args):
    """
    Runs the assembler with the specified arguments.

    :param args: the command-line arguments
    """
    program = Program(width=args.width)
    program.process(args.filename)
    coco_file = CoCoFile(name=program.name or args.name,
                         load_addr=program.origin,
                         exec_addr=program.origin,
                         data=program.get_binary_array())

    if args.symbols:
        program.print_symbol_table()

    if args.print:
        program.print_statements()

    if args.bin_file:
        try:
            binary_file = BinaryFile()
            binary_file.open_host_file_for_write(args.bin_file,
                                                 append=args.append)
            binary_file.save_to_host_file(coco_file)
            binary_file.close_host_file()
        except ValueError as error:
            print("Unable to save binary file:")
            print(error)

    if args.cas_file:
        if not coco_file.name:
            print(
                "No name for the program specified, not creating cassette file"
            )
            return
        try:
            cas_file = CassetteFile()
            cas_file.open_host_file_for_write(args.cas_file,
                                              append=args.append)
            cas_file.save_to_host_file(coco_file)
            cas_file.close_host_file()
        except ValueError as error:
            print("Unable to save cassette file:")
            print(error)
예제 #25
0
 def test_load_effective_address_extended_indexed_program_counter_relative_16_bit(
         self):
     statements = [
         Statement("     ORG $0600"),
         Statement("B    LEAX [Z,PCR]"),
         Statement("     LDA $FF"),
     ]
     statements.extend([Statement("     NOP  ") for _ in range(255)])
     statements.extend([
         Statement("Z    RTS  "),
         Statement("     END B"),
     ])
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([
         0x30, 0x9D, 0x01, 0x01, 0x96, 0xFF, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x39
     ], program.get_binary_array())
예제 #26
0
 def test_program_counter_relative_indexed_is_16_bit(self):
     statements = [
         Statement("     ORG $0600"),
         Statement("B    LDX Z,PCR"),
     ]
     statements.extend([Statement("     NOP  ") for _ in range(255)])
     statements.extend([
         Statement("Z    RTS  "),
         Statement("     END B"),
     ])
     program = Program()
     program.statements = statements
     program.translate_statements()
     self.assertEqual([
         0xAE, 0x8D, 0x00, 0xFF, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
         0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x39
     ], program.get_binary_array())
예제 #27
0
 def test_print_statements_empty_statements_correct(self, print_mock):
     program = Program()
     program.print_statements()
     self.assertEqual([call("-- Assembled Statements --")], print_mock.mock_calls)
예제 #28
0
 def test_print_empty_symbol_table_corect(self, print_mock):
     program = Program()
     program.print_symbol_table()
     self.assertEqual([call("-- Symbol Table --")], print_mock.mock_calls)
예제 #29
0
 def test_save_symbol_saves_index_of_statement(self):
     statement = Statement("START   JMP $FFEE")
     program = Program()
     program.save_symbol(0x35, statement)
     self.assertEqual("35", program.symbol_table["START"].hex())
예제 #30
0
 def test_save_symbol_saves_value(self):
     statement = Statement("POLCAT EQU $FFEE")
     program = Program()
     program.save_symbol(0, statement)
     self.assertEqual("FFEE", program.symbol_table["POLCAT"].hex())