#!/usr/bin/env python3
# -*- coding: utf-8 -*-
########################
#    Author: Dingrq
#    Date  : 2018.11.8
########################

import xlsxwriter
import subprocess as sp
import re
import sys
from path import *
from label import *

class corelog:
    """
    Processing a single log file of core!
    Output: None
    """
    def __init__(self,coreid):
        self.id=coreid
        self.workingPhaseClks=[0,0,0,0x7FFFFFFF] #[clk_sum,phase_cnt,clk_max,clk_min]
        self.homeDir=HOMEDIR
        self.cyclogDir=CYCLOGDIR
        self.logDir=LOCALLOG
    def setDir(self,homedir='',cyclogdir='',logdir=''):
        if homedir !='':
            self.homeDir=homedir
        if cyclogdir !='':
            self.cyclogDir=cyclogdir
        if logdir !='':
            self.logDir=logdir
    def processCoreLog(self):
        print('Processing the log of '+self.id+'\n')
        filename=self.cyclogDir+'rc_clc_mem_logx'+str(self.id[1])+'_y'+str(self.id[0])+'.txt'
        phaseStart=-1
        phaseEnd=-1
        with open(filename,'r') as f:
            while True:
                lines=f.readlines(0x100000)
                if lines:
                    for line in lines:
                        for key in gpioLabelDict:
                            if gpioLabelDict[key] in line:
                                clk=re.split('\s',line)
                                if key == 'phaseStart':
                                    phaseStart=int(clk[1])
                                elif key == 'phaseEnd':
                                    phaseEnd=int(clk[1])
                                    if phaseStart>=0:
                                        phaseEnd=phaseEnd-phaseStart
                                        self.workingPhaseClks[0]=self.workingPhaseClks[0]+phaseEnd
                                        self.workingPhaseClks[1]=self.workingPhaseClks[1]+1
                                        if phaseEnd > self.workingPhaseClks[2]:
                                            self.workingPhaseClks[2]=phaseEnd
                                        if phaseEnd < self.workingPhaseClks[3]:
                                            self.workingPhaseClks[3]=phaseEnd
                                    phaseStart=-1
                                break
                                
                else:
                    break
    def genXlsxFile(self,worksheet,row,col):
        print('Writing information of '+str(self.id)+' to xlsx\n')
        worksheet.write(row,col,'core '+str(self.id))
        col+=1
        worksheet.write(row,col,round(self.workingPhaseClks[0]/self.workingPhaseClks[1],2))
        col+=1
        worksheet.write(row,col,self.workingPhaseClks[2])
        col+=1
        worksheet.write(row,col,self.workingPhaseClks[3])
    def riscvMapFileParse(self):
        mapfile=self.cyclogDir+'../../model/riscv/clib/risccode@'+str(self.id)+'.map'
        veclabel='.intvecs'
        vecstart=-1
        textlabel='.text'
        textsize=-1
        textstart=-1
        try:
            with open(mapfile,'r') as f:
                while True:
                    lines=f.readlines(0x400)
                    if lines:
                        for line in lines:
                            if vecstart==-1:
                                if veclabel in line:
                                    ele=re.split(r'\s+',line)
                                    if ele[3]=='':
                                        vecstart=int(ele[1],16)
                            elif textlabel in line:
                                ele=re.split(r'\s+',line)
                                if ele[3]=='':
                                    textstart=int(ele[1],16)
                                    textsize=int(ele[2],16)
                                romsize=textstart-vecstart+textsize
                                return romsize
                    else:
                        break
        except FileNotFoundError:
            romsize=-1
        return romsize
        
        
        
        
class caselog:
    """
    Processing the log files generated by tj3cycleacc project!
    Output: Flodername.xlsx
    """
    def __init__(self,name):
        self.chipShowStatistic=True
        self.coreShowStatistic=False
        self.caseName=name
        self.corelist=[]
        self.homeDir=HOMEDIR
        self.cyclogDir=CYCLOGDIR
        self.logDir=LOCALLOG
    def setDir(self,homedir='',cyclogdir='',logdir=''):
        if homedir !='':
            self.homeDir=homedir
        if cyclogdir !='':
            self.cyclogDir=cyclogdir
        if logdir !='':
            self.logDir=logdir
    def process(self):
        statis=[]
        maxromsize=0
        maxromid=-1
        for coreid in self.corelist:
            core=corelog(coreid)
            romsize=core.riscvMapFileParse()
            if romsize > maxromsize:
                maxromsize=romsize
                maxromid=coreid
        statis.append(('MaxRomSize',str(round(maxromsize/1024,2))+'KB('+str(maxromid)+')',maxromsize))
        return statis
    def genXlsxFile(self,name=''):
        if name=='':
            realname=self.caseName+'.xlsx'
        else:
            realname=name+'.xlsx'        
        workbook = xlsxwriter.Workbook(realname)
        worksheet = workbook.add_worksheet()
        cell_format = workbook.add_format()
        cell_format.set_bold()
        cell_format.set_align('center')
        worksheet.set_row(0,40,cell_format)
        worksheet.set_column('A:D',10)
        worksheet.write('B1','工作phase\n平均时钟数')
        worksheet.write('C1','工作phase\n最大时钟数')
        worksheet.write('D1','工作phase\n最小时钟数')
        row=1
        for core in self.corelist:
            core.genXlsxFile(worksheet,row,0)
            row+=1
        workbook.close()
        



if __name__ == '__main__':
    if len(sys.argv)==1:
        casename=''
    else:
        if(sys.argv[1][-1]!='/'):
            casename=sys.argv[1]
        else:
            casename=sys.argv[1][:-1]
    case=corelog((0,1))
    size=case.riscvMapFileParse()
    #case=caselog(casename)
    #case.createCoreLog()
    #case.processCoreLog()
    #case.genXlsxFile()

f66b331a25b0f83e4fcd8065b06828df72ecd25f
<mask token>


class corelog:
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>


class caselog:
    """
    Processing the log files generated by tj3cycleacc project!
    Output: Flodername.xlsx
    """

    def __init__(self, name):
        self.chipShowStatistic = True
        self.coreShowStatistic = False
        self.caseName = name
        self.corelist = []
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def process(self):
        statis = []
        maxromsize = 0
        maxromid = -1
        for coreid in self.corelist:
            core = corelog(coreid)
            romsize = core.riscvMapFileParse()
            if romsize > maxromsize:
                maxromsize = romsize
                maxromid = coreid
        statis.append(('MaxRomSize', str(round(maxromsize / 1024, 2)) +
            'KB(' + str(maxromid) + ')', maxromsize))
        return statis

    def genXlsxFile(self, name=''):
        if name == '':
            realname = self.caseName + '.xlsx'
        else:
            realname = name + '.xlsx'
        workbook = xlsxwriter.Workbook(realname)
        worksheet = workbook.add_worksheet()
        cell_format = workbook.add_format()
        cell_format.set_bold()
        cell_format.set_align('center')
        worksheet.set_row(0, 40, cell_format)
        worksheet.set_column('A:D', 10)
        worksheet.write('B1', '工作phase\n平均时钟数')
        worksheet.write('C1', '工作phase\n最大时钟数')
        worksheet.write('D1', '工作phase\n最小时钟数')
        row = 1
        for core in self.corelist:
            core.genXlsxFile(worksheet, row, 0)
            row += 1
        workbook.close()


<mask token>

<mask token>


class corelog:
    <mask token>

    def __init__(self, coreid):
        self.id = coreid
        self.workingPhaseClks = [0, 0, 0, 2147483647]
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def processCoreLog(self):
        print('Processing the log of ' + self.id + '\n')
        filename = self.cyclogDir + 'rc_clc_mem_logx' + str(self.id[1]
            ) + '_y' + str(self.id[0]) + '.txt'
        phaseStart = -1
        phaseEnd = -1
        with open(filename, 'r') as f:
            while True:
                lines = f.readlines(1048576)
                if lines:
                    for line in lines:
                        for key in gpioLabelDict:
                            if gpioLabelDict[key] in line:
                                clk = re.split('\\s', line)
                                if key == 'phaseStart':
                                    phaseStart = int(clk[1])
                                elif key == 'phaseEnd':
                                    phaseEnd = int(clk[1])
                                    if phaseStart >= 0:
                                        phaseEnd = phaseEnd - phaseStart
                                        self.workingPhaseClks[0
                                            ] = self.workingPhaseClks[0] + phaseEnd
                                        self.workingPhaseClks[1
                                            ] = self.workingPhaseClks[1] + 1
                                        if phaseEnd > self.workingPhaseClks[2]:
                                            self.workingPhaseClks[2] = phaseEnd
                                        if phaseEnd < self.workingPhaseClks[3]:
                                            self.workingPhaseClks[3] = phaseEnd
                                    phaseStart = -1
                                break
                else:
                    break

    def genXlsxFile(self, worksheet, row, col):
        print('Writing information of ' + str(self.id) + ' to xlsx\n')
        worksheet.write(row, col, 'core ' + str(self.id))
        col += 1
        worksheet.write(row, col, round(self.workingPhaseClks[0] / self.
            workingPhaseClks[1], 2))
        col += 1
        worksheet.write(row, col, self.workingPhaseClks[2])
        col += 1
        worksheet.write(row, col, self.workingPhaseClks[3])

    def riscvMapFileParse(self):
        mapfile = self.cyclogDir + '../../model/riscv/clib/risccode@' + str(
            self.id) + '.map'
        veclabel = '.intvecs'
        vecstart = -1
        textlabel = '.text'
        textsize = -1
        textstart = -1
        try:
            with open(mapfile, 'r') as f:
                while True:
                    lines = f.readlines(1024)
                    if lines:
                        for line in lines:
                            if vecstart == -1:
                                if veclabel in line:
                                    ele = re.split('\\s+', line)
                                    if ele[3] == '':
                                        vecstart = int(ele[1], 16)
                            elif textlabel in line:
                                ele = re.split('\\s+', line)
                                if ele[3] == '':
                                    textstart = int(ele[1], 16)
                                    textsize = int(ele[2], 16)
                                romsize = textstart - vecstart + textsize
                                return romsize
                    else:
                        break
        except FileNotFoundError:
            romsize = -1
        return romsize


class caselog:
    """
    Processing the log files generated by tj3cycleacc project!
    Output: Flodername.xlsx
    """

    def __init__(self, name):
        self.chipShowStatistic = True
        self.coreShowStatistic = False
        self.caseName = name
        self.corelist = []
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def process(self):
        statis = []
        maxromsize = 0
        maxromid = -1
        for coreid in self.corelist:
            core = corelog(coreid)
            romsize = core.riscvMapFileParse()
            if romsize > maxromsize:
                maxromsize = romsize
                maxromid = coreid
        statis.append(('MaxRomSize', str(round(maxromsize / 1024, 2)) +
            'KB(' + str(maxromid) + ')', maxromsize))
        return statis

    def genXlsxFile(self, name=''):
        if name == '':
            realname = self.caseName + '.xlsx'
        else:
            realname = name + '.xlsx'
        workbook = xlsxwriter.Workbook(realname)
        worksheet = workbook.add_worksheet()
        cell_format = workbook.add_format()
        cell_format.set_bold()
        cell_format.set_align('center')
        worksheet.set_row(0, 40, cell_format)
        worksheet.set_column('A:D', 10)
        worksheet.write('B1', '工作phase\n平均时钟数')
        worksheet.write('C1', '工作phase\n最大时钟数')
        worksheet.write('D1', '工作phase\n最小时钟数')
        row = 1
        for core in self.corelist:
            core.genXlsxFile(worksheet, row, 0)
            row += 1
        workbook.close()


<mask token>

<mask token>


class corelog:
    """
    Processing a single log file of core!
    Output: None
    """

    def __init__(self, coreid):
        self.id = coreid
        self.workingPhaseClks = [0, 0, 0, 2147483647]
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def processCoreLog(self):
        print('Processing the log of ' + self.id + '\n')
        filename = self.cyclogDir + 'rc_clc_mem_logx' + str(self.id[1]
            ) + '_y' + str(self.id[0]) + '.txt'
        phaseStart = -1
        phaseEnd = -1
        with open(filename, 'r') as f:
            while True:
                lines = f.readlines(1048576)
                if lines:
                    for line in lines:
                        for key in gpioLabelDict:
                            if gpioLabelDict[key] in line:
                                clk = re.split('\\s', line)
                                if key == 'phaseStart':
                                    phaseStart = int(clk[1])
                                elif key == 'phaseEnd':
                                    phaseEnd = int(clk[1])
                                    if phaseStart >= 0:
                                        phaseEnd = phaseEnd - phaseStart
                                        self.workingPhaseClks[0
                                            ] = self.workingPhaseClks[0] + phaseEnd
                                        self.workingPhaseClks[1
                                            ] = self.workingPhaseClks[1] + 1
                                        if phaseEnd > self.workingPhaseClks[2]:
                                            self.workingPhaseClks[2] = phaseEnd
                                        if phaseEnd < self.workingPhaseClks[3]:
                                            self.workingPhaseClks[3] = phaseEnd
                                    phaseStart = -1
                                break
                else:
                    break

    def genXlsxFile(self, worksheet, row, col):
        print('Writing information of ' + str(self.id) + ' to xlsx\n')
        worksheet.write(row, col, 'core ' + str(self.id))
        col += 1
        worksheet.write(row, col, round(self.workingPhaseClks[0] / self.
            workingPhaseClks[1], 2))
        col += 1
        worksheet.write(row, col, self.workingPhaseClks[2])
        col += 1
        worksheet.write(row, col, self.workingPhaseClks[3])

    def riscvMapFileParse(self):
        mapfile = self.cyclogDir + '../../model/riscv/clib/risccode@' + str(
            self.id) + '.map'
        veclabel = '.intvecs'
        vecstart = -1
        textlabel = '.text'
        textsize = -1
        textstart = -1
        try:
            with open(mapfile, 'r') as f:
                while True:
                    lines = f.readlines(1024)
                    if lines:
                        for line in lines:
                            if vecstart == -1:
                                if veclabel in line:
                                    ele = re.split('\\s+', line)
                                    if ele[3] == '':
                                        vecstart = int(ele[1], 16)
                            elif textlabel in line:
                                ele = re.split('\\s+', line)
                                if ele[3] == '':
                                    textstart = int(ele[1], 16)
                                    textsize = int(ele[2], 16)
                                romsize = textstart - vecstart + textsize
                                return romsize
                    else:
                        break
        except FileNotFoundError:
            romsize = -1
        return romsize


class caselog:
    """
    Processing the log files generated by tj3cycleacc project!
    Output: Flodername.xlsx
    """

    def __init__(self, name):
        self.chipShowStatistic = True
        self.coreShowStatistic = False
        self.caseName = name
        self.corelist = []
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def process(self):
        statis = []
        maxromsize = 0
        maxromid = -1
        for coreid in self.corelist:
            core = corelog(coreid)
            romsize = core.riscvMapFileParse()
            if romsize > maxromsize:
                maxromsize = romsize
                maxromid = coreid
        statis.append(('MaxRomSize', str(round(maxromsize / 1024, 2)) +
            'KB(' + str(maxromid) + ')', maxromsize))
        return statis

    def genXlsxFile(self, name=''):
        if name == '':
            realname = self.caseName + '.xlsx'
        else:
            realname = name + '.xlsx'
        workbook = xlsxwriter.Workbook(realname)
        worksheet = workbook.add_worksheet()
        cell_format = workbook.add_format()
        cell_format.set_bold()
        cell_format.set_align('center')
        worksheet.set_row(0, 40, cell_format)
        worksheet.set_column('A:D', 10)
        worksheet.write('B1', '工作phase\n平均时钟数')
        worksheet.write('C1', '工作phase\n最大时钟数')
        worksheet.write('D1', '工作phase\n最小时钟数')
        row = 1
        for core in self.corelist:
            core.genXlsxFile(worksheet, row, 0)
            row += 1
        workbook.close()


<mask token>

import xlsxwriter
import subprocess as sp
import re
import sys
from path import *
from label import *


class corelog:
    """
    Processing a single log file of core!
    Output: None
    """

    def __init__(self, coreid):
        self.id = coreid
        self.workingPhaseClks = [0, 0, 0, 2147483647]
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def processCoreLog(self):
        print('Processing the log of ' + self.id + '\n')
        filename = self.cyclogDir + 'rc_clc_mem_logx' + str(self.id[1]
            ) + '_y' + str(self.id[0]) + '.txt'
        phaseStart = -1
        phaseEnd = -1
        with open(filename, 'r') as f:
            while True:
                lines = f.readlines(1048576)
                if lines:
                    for line in lines:
                        for key in gpioLabelDict:
                            if gpioLabelDict[key] in line:
                                clk = re.split('\\s', line)
                                if key == 'phaseStart':
                                    phaseStart = int(clk[1])
                                elif key == 'phaseEnd':
                                    phaseEnd = int(clk[1])
                                    if phaseStart >= 0:
                                        phaseEnd = phaseEnd - phaseStart
                                        self.workingPhaseClks[0
                                            ] = self.workingPhaseClks[0] + phaseEnd
                                        self.workingPhaseClks[1
                                            ] = self.workingPhaseClks[1] + 1
                                        if phaseEnd > self.workingPhaseClks[2]:
                                            self.workingPhaseClks[2] = phaseEnd
                                        if phaseEnd < self.workingPhaseClks[3]:
                                            self.workingPhaseClks[3] = phaseEnd
                                    phaseStart = -1
                                break
                else:
                    break

    def genXlsxFile(self, worksheet, row, col):
        print('Writing information of ' + str(self.id) + ' to xlsx\n')
        worksheet.write(row, col, 'core ' + str(self.id))
        col += 1
        worksheet.write(row, col, round(self.workingPhaseClks[0] / self.
            workingPhaseClks[1], 2))
        col += 1
        worksheet.write(row, col, self.workingPhaseClks[2])
        col += 1
        worksheet.write(row, col, self.workingPhaseClks[3])

    def riscvMapFileParse(self):
        mapfile = self.cyclogDir + '../../model/riscv/clib/risccode@' + str(
            self.id) + '.map'
        veclabel = '.intvecs'
        vecstart = -1
        textlabel = '.text'
        textsize = -1
        textstart = -1
        try:
            with open(mapfile, 'r') as f:
                while True:
                    lines = f.readlines(1024)
                    if lines:
                        for line in lines:
                            if vecstart == -1:
                                if veclabel in line:
                                    ele = re.split('\\s+', line)
                                    if ele[3] == '':
                                        vecstart = int(ele[1], 16)
                            elif textlabel in line:
                                ele = re.split('\\s+', line)
                                if ele[3] == '':
                                    textstart = int(ele[1], 16)
                                    textsize = int(ele[2], 16)
                                romsize = textstart - vecstart + textsize
                                return romsize
                    else:
                        break
        except FileNotFoundError:
            romsize = -1
        return romsize


class caselog:
    """
    Processing the log files generated by tj3cycleacc project!
    Output: Flodername.xlsx
    """

    def __init__(self, name):
        self.chipShowStatistic = True
        self.coreShowStatistic = False
        self.caseName = name
        self.corelist = []
        self.homeDir = HOMEDIR
        self.cyclogDir = CYCLOGDIR
        self.logDir = LOCALLOG

    def setDir(self, homedir='', cyclogdir='', logdir=''):
        if homedir != '':
            self.homeDir = homedir
        if cyclogdir != '':
            self.cyclogDir = cyclogdir
        if logdir != '':
            self.logDir = logdir

    def process(self):
        statis = []
        maxromsize = 0
        maxromid = -1
        for coreid in self.corelist:
            core = corelog(coreid)
            romsize = core.riscvMapFileParse()
            if romsize > maxromsize:
                maxromsize = romsize
                maxromid = coreid
        statis.append(('MaxRomSize', str(round(maxromsize / 1024, 2)) +
            'KB(' + str(maxromid) + ')', maxromsize))
        return statis

    def genXlsxFile(self, name=''):
        if name == '':
            realname = self.caseName + '.xlsx'
        else:
            realname = name + '.xlsx'
        workbook = xlsxwriter.Workbook(realname)
        worksheet = workbook.add_worksheet()
        cell_format = workbook.add_format()
        cell_format.set_bold()
        cell_format.set_align('center')
        worksheet.set_row(0, 40, cell_format)
        worksheet.set_column('A:D', 10)
        worksheet.write('B1', '工作phase\n平均时钟数')
        worksheet.write('C1', '工作phase\n最大时钟数')
        worksheet.write('D1', '工作phase\n最小时钟数')
        row = 1
        for core in self.corelist:
            core.genXlsxFile(worksheet, row, 0)
            row += 1
        workbook.close()


if __name__ == '__main__':
    if len(sys.argv) == 1:
        casename = ''
    elif sys.argv[1][-1] != '/':
        casename = sys.argv[1]
    else:
        casename = sys.argv[1][:-1]
    case = corelog((0, 1))
    size = case.riscvMapFileParse()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
########################
#    Author: Dingrq
#    Date  : 2018.11.8
########################

import xlsxwriter
import subprocess as sp
import re
import sys
from path import *
from label import *

class corelog:
    """
    Processing a single log file of core!
    Output: None
    """
    def __init__(self,coreid):
        self.id=coreid
        self.workingPhaseClks=[0,0,0,0x7FFFFFFF] #[clk_sum,phase_cnt,clk_max,clk_min]
        self.homeDir=HOMEDIR
        self.cyclogDir=CYCLOGDIR
        self.logDir=LOCALLOG
    def setDir(self,homedir='',cyclogdir='',logdir=''):
        if homedir !='':
            self.homeDir=homedir
        if cyclogdir !='':
            self.cyclogDir=cyclogdir
        if logdir !='':
            self.logDir=logdir
    def processCoreLog(self):
        print('Processing the log of '+self.id+'\n')
        filename=self.cyclogDir+'rc_clc_mem_logx'+str(self.id[1])+'_y'+str(self.id[0])+'.txt'
        phaseStart=-1
        phaseEnd=-1
        with open(filename,'r') as f:
            while True:
                lines=f.readlines(0x100000)
                if lines:
                    for line in lines:
                        for key in gpioLabelDict:
                            if gpioLabelDict[key] in line:
                                clk=re.split('\s',line)
                                if key == 'phaseStart':
                                    phaseStart=int(clk[1])
                                elif key == 'phaseEnd':
                                    phaseEnd=int(clk[1])
                                    if phaseStart>=0:
                                        phaseEnd=phaseEnd-phaseStart
                                        self.workingPhaseClks[0]=self.workingPhaseClks[0]+phaseEnd
                                        self.workingPhaseClks[1]=self.workingPhaseClks[1]+1
                                        if phaseEnd > self.workingPhaseClks[2]:
                                            self.workingPhaseClks[2]=phaseEnd
                                        if phaseEnd < self.workingPhaseClks[3]:
                                            self.workingPhaseClks[3]=phaseEnd
                                    phaseStart=-1
                                break
                                
                else:
                    break
    def genXlsxFile(self,worksheet,row,col):
        print('Writing information of '+str(self.id)+' to xlsx\n')
        worksheet.write(row,col,'core '+str(self.id))
        col+=1
        worksheet.write(row,col,round(self.workingPhaseClks[0]/self.workingPhaseClks[1],2))
        col+=1
        worksheet.write(row,col,self.workingPhaseClks[2])
        col+=1
        worksheet.write(row,col,self.workingPhaseClks[3])
    def riscvMapFileParse(self):
        mapfile=self.cyclogDir+'../../model/riscv/clib/risccode@'+str(self.id)+'.map'
        veclabel='.intvecs'
        vecstart=-1
        textlabel='.text'
        textsize=-1
        textstart=-1
        try:
            with open(mapfile,'r') as f:
                while True:
                    lines=f.readlines(0x400)
                    if lines:
                        for line in lines:
                            if vecstart==-1:
                                if veclabel in line:
                                    ele=re.split(r'\s+',line)
                                    if ele[3]=='':
                                        vecstart=int(ele[1],16)
                            elif textlabel in line:
                                ele=re.split(r'\s+',line)
                                if ele[3]=='':
                                    textstart=int(ele[1],16)
                                    textsize=int(ele[2],16)
                                romsize=textstart-vecstart+textsize
                                return romsize
                    else:
                        break
        except FileNotFoundError:
            romsize=-1
        return romsize
        
        
        
        
class caselog:
    """
    Processing the log files generated by tj3cycleacc project!
    Output: Flodername.xlsx
    """
    def __init__(self,name):
        self.chipShowStatistic=True
        self.coreShowStatistic=False
        self.caseName=name
        self.corelist=[]
        self.homeDir=HOMEDIR
        self.cyclogDir=CYCLOGDIR
        self.logDir=LOCALLOG
    def setDir(self,homedir='',cyclogdir='',logdir=''):
        if homedir !='':
            self.homeDir=homedir
        if cyclogdir !='':
            self.cyclogDir=cyclogdir
        if logdir !='':
            self.logDir=logdir
    def process(self):
        statis=[]
        maxromsize=0
        maxromid=-1
        for coreid in self.corelist:
            core=corelog(coreid)
            romsize=core.riscvMapFileParse()
            if romsize > maxromsize:
                maxromsize=romsize
                maxromid=coreid
        statis.append(('MaxRomSize',str(round(maxromsize/1024,2))+'KB('+str(maxromid)+')',maxromsize))
        return statis
    def genXlsxFile(self,name=''):
        if name=='':
            realname=self.caseName+'.xlsx'
        else:
            realname=name+'.xlsx'        
        workbook = xlsxwriter.Workbook(realname)
        worksheet = workbook.add_worksheet()
        cell_format = workbook.add_format()
        cell_format.set_bold()
        cell_format.set_align('center')
        worksheet.set_row(0,40,cell_format)
        worksheet.set_column('A:D',10)
        worksheet.write('B1','工作phase\n平均时钟数')
        worksheet.write('C1','工作phase\n最大时钟数')
        worksheet.write('D1','工作phase\n最小时钟数')
        row=1
        for core in self.corelist:
            core.genXlsxFile(worksheet,row,0)
            row+=1
        workbook.close()
        



if __name__ == '__main__':
    if len(sys.argv)==1:
        casename=''
    else:
        if(sys.argv[1][-1]!='/'):
            casename=sys.argv[1]
        else:
            casename=sys.argv[1][:-1]
    case=corelog((0,1))
    size=case.riscvMapFileParse()
    #case=caselog(casename)
    #case.createCoreLog()
    #case.processCoreLog()
    #case.genXlsxFile()
