def check(src1, src2): if not os.path.exists(src1): IO.writestr("! checker: {src} not found".format(src=src1)) return None if not os.path.exists(src2): IO.writestr("! checker: {src} not found".format(src=src2)) return None cmd = "checker\\displaychecker.exe {0} {1}".format(src1, src2) # print("> {cmd}".format(cmd=cmd)) res = os.popen(cmd) res_lines = res.readlines() res_lines = [line.strip() for line in res_lines] valid_lines = [] for l in res_lines: if len(l) != 0 and l[0] != '-': valid_lines.append(l) # print(l) res_lines = valid_lines # print(res_lines) if res_lines[-1].strip() == 'Accepted': return ('Accepted', 'Accepted') elif res_lines[-1].strip() == 'Wrong Answer': return ('Wrong Answer', "\n".join(res_lines[0:-1])) else: return None
def loadtest(self, testcase: Testcase): in_name = self.path + '/' + self.globconf['inputfilename'] hexname = testcase.path + '/' + testcase.hex if not os.path.exists(hexname): IO.writestr('! Runner.loadtest: Hex Machine Code Not Exist') return False shutil.copyfile(src=hexname, dst=in_name) return True
def run(self, testcase, out): r = self.loadtest(testcase) if not r: IO.writestr('! Runner.run: load testcase error.') return False outpath = "{path}/out".format(path=self.path) if not os.path.exists(outpath): os.mkdir(outpath) ###### Code Here ###### return True
def run(self, testcase, out): r = super().run(testcase, out) if not r: return False # iverilog run vvp = (self.iv_path + os.sep if self.iv_path else "") + "vvp" r = os.system("cd {path} && {vvp} src_unzip/mips.vvp > out/{out}".format(path=self.path, vvp=vvp, out=out)) if r != 0: IO.writestr("! Error Occured on iVerilog Running") return False return True
def judge(self): # check runner if self.runner_type == None: IO.writestr('! Judger: not supported simulation tool') if self.mode == 'standard': return self.stdmode() elif self.mode == 'pat': return self.patmode() else: IO.writestr('! Judger: invalid mode') return False
def timetrim(src, dst): if not os.path.exists(src): IO.writestr("! timetrim: {src} not found.".format(src=src)) return False if not os.path.exists(os.path.dirname(dst)): os.mkdir(os.path.dirname(dst)) cmd = "checker\\timeprocess.exe < {0} > {1}".format(src, dst) r = os.system(cmd) if r != 0: IO.writestr("! timetrim: error running timeprocess.exe") return None
def compile(self): if self.iv_path == None: IO.writestr("! Runner(iverilog).run: iverilog not found.") return False iverilog = (self.iv_path + os.sep if self.iv_path else "") + "iverilog" src_unzip = self.path + "/src_unzip" mips = " ".join(self.v_list) # print(self.v_list) r = os.system("cd {src} && {iverilog} -o mips.vvp tb.v {mips}".format(src=src_unzip, iverilog=iverilog,mips=mips)) if r != 0: IO.writestr("! Runner(iverilog).run: Error Occured on iVerilog Compiling") return False return True
def run(self, testcase, out): r = super().run(testcase, out) if not r: return False tcl = "\n".join(self.tcl_run) tcl_name = 'run.do' with open(self.path + '/' + tcl_name, "w") as fp: fp.write(tcl) # modelsim compile and run vsim = (self.sim_path + '\\' if self.sim_path else '') + 'vsim.exe' cmd = 'cd {path} && \"{vsim}\" -c -do {tcl} > out/{out}'.format( path=self.path, vsim=vsim, tcl=tcl_name, out=out) r = os.system(cmd) if r != 0: IO.writestr('! Error Occured on ModelSim Running') return False return True
def compile(self): if self.sim_path == None: IO.writestr( '! Runner(modelsim).run: ModelSim Simulator not configured.') return False if self.tcl_compile == None: IO.writestr( '! Runner(modelsim).run: Modelsim Compile Tcl Batch not configured.' ) return False tcl = "\n".join(self.tcl_compile) for v in self.v_list: tcl += '\nvlog src_unzip/{v}'.format(v=v) tcl_name = 'compile.do' with open(self.path + '/' + tcl_name, "w") as fp: fp.write(tcl) vsim = (self.sim_path + '\\' if self.sim_path else '') + 'vsim.exe' cmd = 'cd {path} && \"{vsim}\" -c -do {tcl} >nul 2>nul'.format( path=self.path, vsim=vsim, tcl=tcl_name) r = os.system(cmd) if r != 0: IO.writestr('! Error Occured on ModelSim Compileing') return False return True
def run(self, testcase, out): if self.batch_run == None: IO.writestr('! Runner(ise).run: Batch not configured.') return False if self.tcl == None: IO.writestr('! Runner(ise).run: Tcl not configured.') return False r = super().run(testcase, out) if not r: return False tcl = "\n".join(self.tcl) with open(self.path + '\\ise.tcl', "w") as fout: fout.write(tcl) # Run # r = os.system("{path}\\mips.exe -tclbatch {path}\\ise.tcl > {path}\\out\\{out}".format(path=self.path, out=out)) bat = "\r\n".join(self.batch_run).format(root=self.ise_root, bin=self.ise_bin, path=self.path, out=out) bat = "@echo off\r\n" + bat with open(self.path + "\\run.bat", "w") as fout: fout.write(bat) # Compile r = os.system("cd {path} && call run.bat".format(path=self.path)) if r != 0: IO.writestr('! Error Occured on ISE Running') return False return True
def loadcode(self): if not os.path.exists(self.src): IO.writestr("! Runner.loadcode: Source Not Exist!") return False src_unzip = self.path + '/' + 'src_unzip' self.src_unzip = src_unzip if os.path.exists(src_unzip): shutil.rmtree(src_unzip) os.mkdir(self.path + '/' + 'src_unzip') try: zip = zipfile.ZipFile(self.src) zip.extractall(src_unzip) zip.close() except: IO.writestr("! Runner.loadcode: Error occured on extracting zip") return False self.v_list = [] self._addIncProtect(src_unzip, '') # copy testbench tb = self.globconf['testbench'] shutil.copyfile(src=tb, dst=src_unzip + '/tb.v') return True
def compile(self): if self.ise_root == None: IO.writestr('! Runner(ise).compile: ISE Path not configured.') return False if self.batch_compile == None: IO.writestr('! Runner(ise).compile: Batch not configured.') return False src_list = " ".join(["src_unzip\\" + v for v in self.v_list]) bat = "\r\n".join(self.batch_compile).format(root=self.ise_root, bin=self.ise_bin, path=self.path, src=src_list) bat = "@echo off\r\n" + bat with open(self.path + "\\compile.bat", "w") as fout: fout.write(bat) # Compile r = os.system("cd {path} && call compile.bat".format(path=self.path)) if r != 0: IO.writestr('! Error Occured on ISE Compiling') return False return True
def stdmode(self): IO.writestr('Judger Identifier: {id}'.format( id=Config.getValue('configs/global.json', 'identifier'))) IO.writestr('Simulation Tool: {sim}'.format(sim=self.task['tool'])) IO.writestr(' - Standard Mode - ') runner = self.runner_type(self.task['src'], self.path) r = runner.compile() if not r: IO.writestr('Compile Error') return for test in self.testcaseSet: if not test.display: IO.writestr( '# Test Case #<{name}>: Omitted\nComment: Standard Answer not ready.' .format(name=test.name)) continue outstr = 'Test Case #<{name}>: '.format(name=test.name) r = runner.run(test, 'out.txt') if not r: outstr += "{res} \nComment: {comment}".format( res='Runtime Error', comment='Runtime Error') IO.writestr(outstr) break Checker.timetrim(test.path + '/' + test.display, self.path + '/out/std_t.txt') Checker.timetrim(self.path + '/out/out.txt', self.path + '/out/out_t.txt') # res = Checker.check(test.path + '/' + test.display, self.path + '/out/out.txt') res = Checker.check(self.path + '/out/std_t.txt', self.path + '/out/out_t.txt') outstr = 'Test Case #<{name}>: '.format(name=test.name) if res == None: outstr += 'Checker Error' else: outstr += "{res} \nComment: {comment}".format(res=res[0], comment=res[1]) IO.writestr(outstr)
def patmode(self): IO.writestr('Judger Identifier: {id}'.format( id=Config.getValue('configs/global.json', 'identifier'))) IO.writestr('Simulation Tool: {sim}'.format(sim=self.task['tool'])) IO.writestr(' - Pat Mode - ') runner1 = self.runner_type(self.task['src1'], self.path) r = runner1.compile() if not r: IO.writestr('src1 Compile Error') return runner2 = self.runner_type(self.task['src2'], self.path) r = runner2.compile() if not r: IO.writestr('src2 Compile Error') return for test in self.testcaseSet: outstr = 'Test Case #<{name}>: '.format(name=test.name) r = runner1.run(test, 'out1.txt') if not r: outstr += "{res} \nComment: {comment}".format( res='Runtime Error', comment='src1 Runtime Error') r = runner2.run(test, 'out2.txt') if not r: outstr += "{res} \nComment: {comment}".format( res='Runtime Error', comment='src2 Runtime Error') Checker.timetrim(self.path + '/out/out1.txt', self.path + '/out/out1_t.txt') Checker.timetrim(self.path + '/out/out2.txt', self.path + '/out/out2_t.txt') res = Checker.check(self.path + '/out/out1_t.txt', self.path + '/out/out2_t.txt') if res == None: outstr += 'Checker Error' else: outstr += "{res} \nComment: {comment}".format(res=res[0], comment=res[1]) IO.writestr(outstr)