Ejemplo n.º 1
0
    def testA(self):
        self.infile = open(self.infileName, 'w')
        self.infile.write('''
# simple loop test with no stack usage.  Starts heap from bottom
	MOV $0, 7
	MOV $1, 13
	MOV $4, 14
	MOV $2, 42

TESTPARITY:
	MOV $3, $4
	AND $3, 1
	CMP $3, 0
	JE EVEN 		# Jump if $4 is even
	ADD $0, $1
	DECR $4
	CMP $4, 0
	JNE TESTPARITY
	JE WASTELOOP

EVEN:
	ADD $1, $0
	DECR $4
	CMP 0, $4		# other kind of compare
	JNE TESTPARITY

WASTELOOP:
	SUB $1, 0	# 1984 is now the current fib value in hex.  Hold it on the write line
	OR $1, 0
	NOT $5, 0
	JNE WASTELOOP
''')
        self.infile.close()
        assembler.parse(self.infileName, self.outfileName)
        self.outfile = open(self.outfileName, 'r')
        out_str = self.outfile.read()
        self.outfile.close()
        test_str = '''f007
f10d
f40e
f22a
3f4
6301
4300
2c00d
21
3d400
4400
28004
2c011
120
3d400
5400
28004
3100
7100
9500
28011
'''
        assert out_str == test_str
Ejemplo n.º 2
0
def run(args):
    filename = args.input # these match the "dest": dest="input"
    output_filename = args.output # from dest="output"
    if(filename==None):
        run=True
        print("\n-------------------------POWER Sim V0.1 Console----------------------\n")
        while(run):
            line=input(">>>")
            if(line not in ["quit()","q()","exit","quit"]):
                try:
                    asm.parse(line)
                except:
                    print("Error thrown")
            else:
                run=False
    else:
        if(output_filename==None):
            with open(filename, "r") as f:
                lc=1
                #line=f.readline()[:-1]
                #while(line!=""):
                for line in f:
                    print("\n->line "+str(lc)+"\n")
                    try:
                        asm.parse(line)
                    except:
                        print("Syntax Error at line: "+str(lc))
                        break
                    #line=f.readline()[:-1]
                    lc+=1
Ejemplo n.º 3
0
def OpenFile():
    global addressCounter

    name = askopenfilename(filetypes=(("Assembly File", "*.s"), ("All Files",
                                                                 "*.*")),
                           title="Choose a file.")
    with open(name) as f:
        mem._init_()
        if (mode == "asm"):
            consoleList.insert(END, "Assembling source at: '" + name + "'")
            lc = 1
            for line in f:
                consoleList.insert(END, "->line " + str(lc))
                try:
                    if (line != "\n"):
                        addressCounter = first_pass.par(line, addressCounter)
                        if (line.rstrip() != ""):
                            consoleList.insert(
                                END,
                                str(addressCounter) + ": " + line.rstrip())
                        imc = asm.parse(line)
                        if (imc.rstrip() != ""):
                            consoleList.insert(END,
                                               "Assembly code:" + imc.strip())
                        else:
                            consoleList.insert(END, "Not(yet) an instruction!")
                except Exception as e:
                    consoleList.insert(
                        END, "Syntax Error at line: " + str(lc) + ": " + line +
                        "\n" + str(e))

                    break
                lc += 1
        else:
            consoleList.insert(END, "Processing source at: '" + name + "'")
            lc = 1
            for line in f:
                consoleList.insert(END, "->line " + str(lc))
                try:
                    if (line != "\n"):
                        addressCounter = first_pass.par(line, addressCounter)
                        imc = asm.parse(line)
                        proc.processline(imc)
                        if (imc.rstrip() != ""):
                            consoleList.insert(
                                END, line + " assembled binary: " + imc)
                except Exception as e:
                    consoleList.insert(
                        END, "Syntax Error at line: " + str(lc) + ": " + line +
                        "\n" + str(e))
                    break
                lc += 1
        readmemory()
        readregisters()
Ejemplo n.º 4
0
def update_cout():
    if (code != ""):
        if (mode == "asm"):
            result = asm.parse(code)
        elif (mode == "proc"):
            imc = asm.parse(code)
            proc.processline(imc)
            result = imc + " Assembled!"
    consoleList.insert(END, ">> " + code)
    if (code != ""):
        consoleList.insert(END, result)
    consoleList.yview(END)
    readmemory()
    readregisters()
Ejemplo n.º 5
0
def update_cout():
    if(code!=""):
        if(mode=="asm"):
            result = asm.parse(code)
        elif(mode=="proc"):
            pass
    consoleList.insert(END, ">> "+code)
    if(code!=""):
        consoleList.insert(END, result)
    consoleList.yview(END)
Ejemplo n.º 6
0
 def assemble(self):
     text = self.editor.GetText()
     try:
         self.reset(False)
         self.program = assembler.parse(preprocessor.preprocess(text))
         self.emu.load(self.program.assemble())
         self.program_list.update(self.program.instructions)
         self.refresh_debug_info()
     except Exception as e:
         self.reset(False)
         dialog = wx.MessageDialog(self, str(e), 'Error',
             wx.ICON_ERROR | wx.OK)
         dialog.ShowModal()
         dialog.Destroy()
Ejemplo n.º 7
0
 def assemble(self):
     text = self.editor.GetText()
     try:
         self.reset(False)
         self.program = assembler.parse(preprocessor.preprocess(text))
         self.emu.load(self.program.assemble())
         self.program_list.update(self.program.instructions)
         self.refresh_debug_info()
     except Exception as e:
         self.reset(False)
         dialog = wx.MessageDialog(self, str(e), 'Error',
                                   wx.ICON_ERROR | wx.OK)
         dialog.ShowModal()
         dialog.Destroy()
Ejemplo n.º 8
0
    def assembleClicked(self):
        code = self.assembly.toPlainText().upper()
        tokens = sap1.tokenize(code)
        obj = sap1.parse(tokens)

        if type(obj) == str:
            self.errorMessage.setText(obj)
            self.errorMessage.show()
            self.errorMessage.setStyleSheet("color: rgb(200,10,10);")
            self.binary.setPlainText("")
        else:
            self.errorMessage.show()
            self.errorMessage.setStyleSheet("color: black")
            self.errorMessage.setText("Assembling...")
            self.outputBinary(obj)
            self.errorMessage.setText("Done.")
Ejemplo n.º 9
0
def run(args):
    filename = args.input  # these match the "dest": dest="input"
    output_filename = args.output  # from dest="output"
    r_mode = args.mode

    mem_mod._init_()

    if (r_mode == "asm"):
        if (filename == None):
            run = True
            print(
                "\n-------------------------POWER Sim V0.1 Assembler Console----------------------\n"
            )
            while (run):
                line = input(">>>")
                if (line not in ["quit()", "q()", "exit", "quit"]):
                    try:
                        imc = asm.parse(line)
                        print(imc)
                    except:
                        print("Error thrown")
                else:
                    run = False
        else:
            if (output_filename == None):
                with open(filename, "r") as f:
                    lc = 1
                    #line=f.readline()[:-1]
                    #while(line!=""):
                    for line in f:
                        print("\n->line " + str(lc) + "\n")
                        try:
                            imc = asm.parse(line)
                            print(imc)
                        except Exception as e:
                            print("Syntax Error at line: " + str(lc) + ": " +
                                  line + "\n" + str(e))
                            break
                        #line=f.readline()[:-1]
                        lc += 1
            else:
                with open(filename, "r") as f:
                    lc = 1
                    fw = open(output_filename, "w")
                    #line=f.readline()[:-1]
                    #while(line!=""):
                    for line in f:
                        print("\n->line " + str(lc) + "\n")
                        try:
                            imc = asm.parse(line)
                            fw.write(imc)
                        except:
                            print("Syntax Error at line: " + str(lc))
                            break
                        #line=f.readline()[:-1]
                        lc += 1
    elif (r_mode == "proc"):
        if (filename == None):
            run = True
            print(
                "\n-------------------------POWER Sim V0.1 Processor Console----------------------\n"
            )
            while (run):
                line = input(">>>")
                if (line not in ["quit()", "q()", "exit", "quit"]):
                    try:
                        imc = asm.parse(line)
                        prcs.processline(imc)
                    except:
                        print("Error thrown")
                else:
                    run = False
Ejemplo n.º 10
0
if args.version:
    print('BlueSpace 1.1\nCopyright (C) 2014 Christopher Smith')
    sys.exit()

if args.sourcepath == '-':
    sourcefile = sys.stdin
else:
    try:
        sourcefile = open(args.sourcepath, 'r')
    except OSError:
        print('BlueSpace: Failed to open ' + sourcefile, file=sys.stderr)
        sys.exit(1)

if args.input == 'assembly':
    try:
        program = list(assembler.parse(sourcefile))
    except RuntimeError:
        sys.exit(1)
else:
    if args.input == 'printable':
        tr = {'s': ' ', 't': '\t', 'n': '\n'}
        source = ''.join(tr[x] for x in sourcefile.read() if x in tr)
    else:
        source = ''.join(x for x in sourcefile.read() if x in ' \t\n')
    try:
        program = parser(source).parse()
    except RuntimeError as err:
        sys.exit(err)

if args.convertto == 'whitespace':
    for stmt in program:
Ejemplo n.º 11
0
def run(args):
    global addressCounter

    filename = args.input  # these match the "dest": dest="input"
    output_filename = args.output  # from dest="output"
    r_mode = args.mode

    mem_mod._init_()

    if (r_mode == "asm"):
        if (filename == None):
            run = True
            print(
                "\n-------------------------POWER Sim V0.7x Assembler Console----------------------\n"
            )
            while (run):
                line = input(">>>")
                if (line not in ["quit()", "q()", "exit", "quit"]):
                    try:
                        addressCounter = first_pass.par(line, addressCounter)
                        imc = asm.parse(line)
                        print(str(addressCounter) + "\t" + line.rstrip())
                        print(imc.strip())
                    except:
                        print("Error thrown")
                else:
                    run = False

        else:
            if (output_filename == None):
                with open(filename, "r") as f:
                    lc = 1
                    for line in f:
                        print("->line " + str(lc))
                        try:
                            if (line != "\n"):
                                addressCounter = first_pass.par(
                                    line, addressCounter)
                                imc = asm.parse(line)
                                if (line.rstrip() != ""):
                                    print(
                                        str(addressCounter) + "\t" +
                                        line.rstrip())
                                if (imc.rstrip() != ""):
                                    print(imc.strip())
                        except Exception as e:
                            print("Syntax Error at line: " + str(lc) + ": " +
                                  line + "\n" + str(e))
                            break
                        lc += 1
            else:
                with open(filename, "r") as f:
                    lc = 1
                    fw = open(output_filename, "w")
                    for line in f:
                        print("->line " + str(lc))
                        try:
                            if (line != "\n"):
                                addressCounter = first_pass.par(
                                    line, addressCounter)
                                imc = asm.parse(line)
                                fw.write(imc)
                        except:
                            print("Syntax Error at line: " + str(lc))
                            break
                        lc += 1
    elif (r_mode == "proc"):
        if (filename == None):
            run = True
            print(
                "\n-------------------------POWER Sim V0.7x Processor Console----------------------\n"
            )
            while (run):
                line = input(">>>")
                if (line not in ["quit()", "q()", "exit", "quit"]):
                    try:
                        addressCounter = first_pass.par(line, addressCounter)
                        imc = asm.parse(line)
                        prcs.processline(imc)
                    except:
                        print("Error thrown")
                else:
                    run = False

        else:
            if (output_filename == None):
                with open(filename, "r") as f:
                    lc = 1
                    for line in f:
                        print("->line " + str(lc))
                        try:
                            if (line != "\n"):
                                addressCounter = first_pass.par(
                                    line, addressCounter)
                                imc = asm.parse(line)
                                prcs.processline(imc)
                        except Exception as e:
                            print("Syntax Error at line: " + str(lc) + ": " +
                                  str(line) + "\n" + str(e))
                            break
                        lc += 1
            else:
                with open(filename, "r") as f:
                    lc = 1
                    fw = open(output_filename, "w")
                    for line in f:
                        print("->line " + str(lc))
                        try:
                            if (line != "\n"):
                                addressCounter = first_pass.par(
                                    line, addressCounter)
                                imc = asm.parse(line)
                                fw.write(imc)
                                prcs.processline(imc)
                        except:
                            print("Syntax Error at line: " + str(lc))
                            break
                        lc += 1
Ejemplo n.º 12
0
def asm_bytes(asm):
    ast, labels = assembler.parse(asm)
    output = assembler.assemble(ast, labels)
    return assembler.nytes_to_bytes(output)
Ejemplo n.º 13
0
def asm(asm):
    ast, labels = assembler.parse(asm)
    return assembler.assemble(ast, labels)