Esempio n. 1
0
    def test_output(self):
        instructions = [
            MlInstruction(
                address=None,
                ram_write=0,
                alu_opcode=0x8,
                input_select=None,
                output_select=None,
                output_enable=0,
                save_core_selection=0,
                comment=None,
                global_command=0
            ),
            MlInstruction(
                address=None,
                ram_write=None,
                alu_opcode=None,
                input_select=None,
                output_select=None,
                output_enable=None,
                save_core_selection=None,
                comment="This has a comment.",
                global_command=None
            ),
            MlInstruction(
                address=None,
                ram_write=0,
                alu_opcode=0x8,
                input_select=None,
                output_select=None,
                output_enable=0,
                save_core_selection=0,
                comment="This has a comment!",
                global_command=0
            ),
            MlInstruction(
                address=0xFF,
                ram_write=0,
                alu_opcode=None,
                input_select=None,
                output_select=None,
                output_enable=0,
                save_core_selection=0,
                comment=None,
                global_command=0
            ),
        ]

        self.outputter.output(instructions, self.output)

        lines = self.output.getvalue().split("\n")

        self.assertEqual(len(lines), 5)  # Last index is blank line
        self.assertEqual(lines[0], '00008')
        self.assertEqual(lines[1], '// This has a comment.')
        self.assertEqual(lines[2], '00008 // This has a comment!')
        self.assertEqual(lines[3], '0ff0c')
Esempio n. 2
0
def generate_rotate_left(asm: AsmInstruction,
                         instructions: List[MlInstruction]):
    bits = int(asm.operands[0])
    address = int(asm.operands[1])

    for i in range(bits % 16):
        instructions.append(
            MlInstruction(address=None,
                          ram_write=0,
                          alu_opcode=0x3,
                          input_select=None,
                          output_select=None,
                          output_enable=0,
                          save_core_selection=0,
                          comment=asm.comment if i == 0 else None))

    instructions.append(
        MlInstruction(address=address * 4 + int(bits / 16),
                      ram_write=1,
                      alu_opcode=0x2,
                      input_select=None,
                      output_select=0,
                      output_enable=0,
                      save_core_selection=0,
                      comment=asm.comment if bits % 16 == 0 else None))

    instructions.append(
        MlInstruction(address=address * 4 + ((3 + int(bits / 16)) % 4),
                      ram_write=1,
                      alu_opcode=0x2,
                      input_select=None,
                      output_select=0,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None))

    instructions.append(
        MlInstruction(address=address * 4 + ((2 + int(bits / 16)) % 4),
                      ram_write=1,
                      alu_opcode=0x2,
                      input_select=None,
                      output_select=0,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None))

    instructions.append(
        MlInstruction(address=address * 4 + ((1 + int(bits / 16)) % 4),
                      ram_write=1,
                      alu_opcode=0x2,
                      input_select=None,
                      output_select=0,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None))
    def test_zero_instructions(self):
        ml = MlInstruction(address=0,
                           ram_write=0,
                           alu_opcode=0,
                           input_select=0,
                           output_select=0,
                           output_enable=0,
                           save_core_selection=0,
                           comment=None)

        self.assertTrue(ml.has_instruction())
    def test_instruction_and_comment(self):
        ml = MlInstruction(address=None,
                           ram_write=0,
                           alu_opcode=0x8,
                           input_select=None,
                           output_select=None,
                           output_enable=0,
                           save_core_selection=0,
                           comment="This has a comment!")

        self.assertTrue(ml.has_comment())
        self.assertTrue(ml.has_instruction())
    def test_comment_only(self):
        ml = MlInstruction(address=None,
                           ram_write=None,
                           alu_opcode=None,
                           input_select=None,
                           output_select=None,
                           output_enable=None,
                           save_core_selection=None,
                           comment="This has a comment.")

        self.assertTrue(ml.has_comment())
        self.assertFalse(ml.has_instruction())
    def test_instruction_only_blank_comment(self):
        ml = MlInstruction(address=None,
                           ram_write=0,
                           alu_opcode=0x8,
                           input_select=None,
                           output_select=None,
                           output_enable=0,
                           save_core_selection=0,
                           comment='')

        self.assertFalse(ml.has_comment())
        self.assertTrue(ml.has_instruction())
    def test_zero_instructions(self):
        ml = MlInstruction(
            address=0,
            ram_write=0,
            alu_opcode=0,
            input_select=0,
            output_select=0,
            output_enable=0,
            save_core_selection=0,
            comment=None,
            global_command=0
        )

        self.assertTrue(ml.has_instruction())
    def test_instruction_and_comment(self):
        ml = MlInstruction(
            address=None,
            ram_write=0,
            alu_opcode=0x8,
            input_select=None,
            output_select=None,
            output_enable=0,
            save_core_selection=0,
            comment="This has a comment!",
            global_command=0
        )

        self.assertTrue(ml.has_comment())
        self.assertTrue(ml.has_instruction())
    def test_comment_only(self):
        ml = MlInstruction(
            address=None,
            ram_write=None,
            alu_opcode=None,
            input_select=None,
            output_select=None,
            output_enable=None,
            save_core_selection=None,
            comment="This has a comment.",
            global_command=None
        )

        self.assertTrue(ml.has_comment())
        self.assertFalse(ml.has_instruction())
    def test_instruction_only_blank_comment(self):
        ml = MlInstruction(
            address=None,
            ram_write=0,
            alu_opcode=0x8,
            input_select=None,
            output_select=None,
            output_enable=0,
            save_core_selection=0,
            comment='',
            global_command=0
        )

        self.assertFalse(ml.has_comment())
        self.assertTrue(ml.has_instruction())
Esempio n. 11
0
def generate_read(asm: AsmInstruction, instructions: List[MlInstruction]):
    address = int(asm.operands[0])

    instructions.append(MlInstruction(
        address=address * 4,
        ram_write=0,
        alu_opcode=None,
        input_select=None,
        output_select=1,
        output_enable=1,
        save_core_selection=0,
        comment=asm.comment
    ))

    instructions.append(MlInstruction(
        address=address * 4 + 1,
        ram_write=0,
        alu_opcode=None,
        input_select=None,
        output_select=1,
        output_enable=1,
        save_core_selection=0,
        comment=None
    ))

    instructions.append(MlInstruction(
        address=address * 4 + 2,
        ram_write=0,
        alu_opcode=None,
        input_select=None,
        output_select=1,
        output_enable=1,
        save_core_selection=0,
        comment=None
    ))

    instructions.append(MlInstruction(
        address=address * 4 + 3,
        ram_write=0,
        alu_opcode=None,
        input_select=None,
        output_select=1,
        output_enable=1,
        save_core_selection=0,
        comment=None
    ))
def generate_constant(asm: AsmInstruction, instructions: List[MlInstruction]):
    instructions.append(
        MlInstruction(address=None,
                      ram_write=0,
                      alu_opcode=0x0,
                      input_select=00,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=asm.comment))
def generate_transmit(asm: AsmInstruction, instructions: List[MlInstruction]):
    instructions.append(
        MlInstruction(address=None,
                      ram_write=0,
                      alu_opcode=None,
                      input_select=None,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=asm.comment,
                      global_command=3))
Esempio n. 14
0
def generate_nonce(asm: AsmInstruction, instructions: List[MlInstruction]):
    address = int(asm.operands[0])

    instructions.append(
        MlInstruction(address=address,
                      ram_write=0,
                      alu_opcode=0x0,
                      input_select=0,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=asm.comment))
Esempio n. 15
0
 def _generate_instruction(self, asm: AsmInstruction,
                           instructions: List[MlInstruction]):
     if asm.operator is None:  # This means that the instruction
         # is only a comment
         instructions.append(
             MlInstruction(None, None, None, None, None, None, None,
                           asm.comment, None))
     elif asm.operator == "Add":
         return generate_add.generate_add(asm, instructions)
     elif asm.operator == "Constant":
         return generate_constant.generate_constant(asm, instructions)
     elif asm.operator == "CoreId":
         return generate_core_id.generate_core_id(asm, instructions)
     elif asm.operator == "Count":
         return generate_count.generate_count(asm, instructions)
     elif asm.operator == "IncrementNonce":
         return generate_increment_nonce.generate_increment_nonce(
             asm, instructions)
     elif asm.operator == "Load":
         return generate_load.generate_load(asm, instructions)
     elif asm.operator == "Nonce":
         return generate_nonce.generate_nonce(asm, instructions)
     elif asm.operator == "Read":
         return generate_read.generate_read(asm, instructions)
     elif asm.operator == "RotateLeft":
         return generate_rotate_left.generate_rotate_left(asm, instructions)
     elif asm.operator == "Save":
         return generate_save.generate_save(asm, instructions)
     elif asm.operator == "SaveBitCounter":
         return generate_save_bit_counter.generate_save_bit_counter(
             asm, instructions)
     elif asm.operator == "SaveBitsOff":
         return generate_save_bits_off.generate_save_bits_off(
             asm, instructions)
     elif asm.operator == "SaveComparator":
         return generate_save_comparator.generate_save_comparator(
             asm, instructions)
     elif asm.operator == "SaveNonce":
         return generate_save_nonce.generate_save_nonce(asm, instructions)
     elif asm.operator == "SelectCore":
         return generate_select_core.generate_select_core(asm, instructions)
     elif asm.operator == "SelectCoreNonce":
         return generate_select_core_nonce.generate_select_core_nonce(
             asm, instructions)
     elif asm.operator == "Transmit":
         return generate_transmit.generate_transmit(asm, instructions)
     elif asm.operator == "XOR":
         return generate_xor.generate_xor(asm, instructions)
     else:
         raise NotImplementedError(
             "No generation code for {} exists.".format(asm.operator))
Esempio n. 16
0
    def _output_instruction(self, ml: MlInstruction, out_file, format="default"):
        """Writes a Machine Language instruction to a file as ASCII-encoded hex

        Atrributes:
            ml -- a Machine Language Instruction
            out_file -- an output file
            format -- output style, use 'core_sim' for Core Simulator compatability
        """

        if ml.has_instruction():
            # Default values
            address = ml.address if ml.address is not None else 0
            ram_write = ml.ram_write if ml.ram_write is not None else 0
            alu_opcode = ml.alu_opcode if ml.alu_opcode is not None else 0xC
            input_select = (
                ml.input_select if ml.input_select is not None else 0
            )
            output_select = (
                ml.output_select if ml.output_select is not None else 0
            )
            output_enable = (
                ml.output_enable if ml.output_enable is not None else 0
            )
            save_core_selection = (
                ml.save_core_selection if (
                    ml.save_core_selection is not None
                ) else 0
            )
            global_command = (
                ml.global_command if (
                    ml.global_command is not None
                ) else 0
            )

            binary = (
                (save_core_selection << 17) |
                (ram_write << 16) |
                (address << 8) |
                (input_select << 6) |
                (output_select << 5) |
                (output_enable << 4) |
                (alu_opcode << 0)
            )

            if self.output_format == 'full':
                binary |= (global_command << 18)

            out_file.write("{0:0{1}x}".format(binary, 5))

        # Place space between instruction an comment
        if ml.has_instruction() and ml.has_comment():
            out_file.write(" ")

        if ml.has_comment():
            out_file.write("// {}".format(ml.comment))

        if ml.has_comment() or ml.has_instruction():
            out_file.write("\n")
Esempio n. 17
0
def generate_save_bits_off(asm: AsmInstruction,
                           instructions: List[MlInstruction]):
    address = int(asm.operands[0])

    instructions.append(MlInstruction(
        address=address * 4,
        ram_write=1,
        alu_opcode=0xE,
        input_select=None,
        output_select=0,
        output_enable=0,
        save_core_selection=0,
        comment=asm.comment,
        global_command=0
    ))
Esempio n. 18
0
    def test_constant_instruction(self):
        """Since the "Constant" instruction is zero for every field, it can
        result in an edge case where it is not generated. This is because of
        confusion between how Python treats None and 0. 0 == None is False,
        however "if 0" and "if None" are equivalent.
        """
        instruction = MlInstruction(address=0,
                                    ram_write=0,
                                    alu_opcode=0,
                                    input_select=0,
                                    output_select=0,
                                    output_enable=0,
                                    save_core_selection=0,
                                    comment=None)

        self.outputter._output_instruction(instruction, self.output)

        lines = self.output.getvalue().split("\n")

        self.assertEqual(len(lines), 2)  # Last index is blank line
        self.assertEqual(lines[0], '00000')
Esempio n. 19
0
    def _output_instruction(self,
                            ml: MlInstruction,
                            out_file,
                            format="default"):
        """Writes a Machine Language instruction to a file as ASCII-encoded hex

        Atrributes:
            ml -- a Machine Language Instruction
            out_file -- an output file
            format -- output style, use 'core_sim' for Core Simulator compatability
        """

        if ml.has_instruction():
            # Default values
            address = ml.address if ml.address is not None else 0
            ram_write = ml.ram_write if ml.ram_write is not None else 0
            alu_opcode = ml.alu_opcode if ml.alu_opcode is not None else 0xC
            input_select = (ml.input_select
                            if ml.input_select is not None else 0)
            output_select = (ml.output_select
                             if ml.output_select is not None else 0)
            output_enable = (ml.output_enable
                             if ml.output_enable is not None else 0)
            save_core_selection = (ml.save_core_selection if
                                   (ml.save_core_selection is not None) else 0)
            global_command = (ml.global_command if
                              (ml.global_command is not None) else 0)

            binary = ((save_core_selection << 17) | (ram_write << 16) |
                      (address << 8) | (input_select << 6) |
                      (output_select << 5) | (output_enable << 4) |
                      (alu_opcode << 0))

            if self.output_format == 'full':
                binary |= (global_command << 18)

            out_file.write("{0:0{1}x}".format(binary, 5))

        # Place space between instruction an comment
        if ml.has_instruction() and ml.has_comment():
            out_file.write(" ")

        if ml.has_comment():
            out_file.write("// {}".format(ml.comment))

        if ml.has_comment() or ml.has_instruction():
            out_file.write("\n")
Esempio n. 20
0
    def _output_instruction(self, ml: MlInstruction, out_file):
        """Writes a Machine Language instruction to a file as ASCII-encoded hex

        Atrributes:
            ml -- a Machine Language Instruction
            out_file -- an output file
        """

        if ml.has_instruction():
            # Default values
            address = ml.address if ml.address is not None else 0
            ram_write = ml.ram_write if ml.ram_write is not None else 0
            alu_opcode = ml.alu_opcode if ml.alu_opcode is not None else 0xC
            input_select = (ml.input_select
                            if ml.input_select is not None else 0)
            output_select = (ml.output_select
                             if ml.output_select is not None else 0)
            output_enable = (ml.output_enable
                             if ml.output_enable is not None else 0)
            save_core_selection = (ml.save_core_selection if
                                   (ml.save_core_selection is not None) else 0)

            binary = ((save_core_selection << 17) | (ram_write << 16) |
                      (address << 8) | (input_select << 6) |
                      (output_select << 5) | (output_enable << 4) |
                      (alu_opcode << 0))

            out_file.write("{0:0{1}x}".format(binary, 5))

        # Place space between instruction an comment
        if ml.has_instruction() and ml.has_comment():
            out_file.write(" ")

        if ml.has_comment():
            out_file.write("// {}".format(ml.comment))

        if ml.has_comment() or ml.has_instruction():
            out_file.write("\n")
Esempio n. 21
0
def generate_load(asm: AsmInstruction, instructions: List[MlInstruction]):
    address = int(asm.operands[0])
    register = asm.operands[1]

    instructions.append(
        MlInstruction(address=None,
                      ram_write=0,
                      alu_opcode=0x6,
                      input_select=None,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=asm.comment,
                      global_command=0))

    instructions.append(
        MlInstruction(address=address * 4 + 3,
                      ram_write=0,
                      alu_opcode=0x1 if register == "Primary" else 0x5,
                      input_select=1,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))

    instructions.append(
        MlInstruction(address=None,
                      ram_write=0,
                      alu_opcode=0x6,
                      input_select=None,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))

    instructions.append(
        MlInstruction(address=address * 4 + 2,
                      ram_write=0,
                      alu_opcode=0x1 if register == "Primary" else 0x5,
                      input_select=1,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))

    instructions.append(
        MlInstruction(address=None,
                      ram_write=0,
                      alu_opcode=0x6,
                      input_select=None,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))

    instructions.append(
        MlInstruction(address=address * 4 + 1,
                      ram_write=0,
                      alu_opcode=0x1 if register == "Primary" else 0x5,
                      input_select=1,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))

    instructions.append(
        MlInstruction(address=None,
                      ram_write=0,
                      alu_opcode=0x6,
                      input_select=None,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))

    instructions.append(
        MlInstruction(address=address * 4,
                      ram_write=0,
                      alu_opcode=0x1 if register == "Primary" else 0x5,
                      input_select=1,
                      output_select=None,
                      output_enable=0,
                      save_core_selection=0,
                      comment=None,
                      global_command=0))