<?xml version="1.0" encoding="UTF-8"?>
<!-- 
 /*******************************************************************************
 * Copyright (c) 2014 Elphel, Inc.
 * This file is a part of VDT plug-in.
 * VDT plug-in is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VDT plug-in is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse or Eclipse plugins (or a modified version of those libraries),
 * containing parts covered by the terms of EPL/CPL, the licensors of this
 * Program grant you additional permission to convey the resulting work.
 * {Corresponding Source for a non-source form of such a combination shall
 * include the source code for the parts of Eclipse or Eclipse plugins used
 * as well as that of the covered work.}
 *******************************************************************************/
 -->
<vdt-project>

	<interface name="IVerilog" extends="FPGAPprojectInterface">
		<!-- Syntax definitions -->

		<syntax name="D_ParamSyntax" format="-D%%ParamName" />
		<syntax name="g_ParamSyntax" format="-g%%ParamName" />
		<syntax name="NospecifySyntax" format="-gno-specify" />
		<syntax name="TopModuleSyntax" format="-s%%TopModule" />
		<syntax name="TopModulesOtherSyntax" format="%(-s%%ParamValue%| %)" />
		<syntax name="ModuleLibrarySyntax" format="%(-y%%ParamValue%| %)" />
		<syntax name="IncludeDirSyntax" format="%(-I%%ParamValue%| %)" />
		<syntax name="ExtraFilesSyntax" format="%(%%ParamValue%| %)" />
		<syntax name="SwitchSyntax" format="-%%ParamName" />
		<syntax name="GrepFindSyntax"
			format="| { grep --line-buffered -E &quot;%%ParamValue&quot; || true; }" />
		<syntax name="GrepSkipSyntax"
			format="| { grep --line-buffered -v &quot;%%ParamValue&quot; || true; }" />
			
		<syntax name="LogFileSyntax" format="%%ParamValue-%%BuildStamp.log" />
		<syntax name="OutFileSyntax" format="%%ParamValue-%%BuildStamp.ivlg" />
		<syntax name="FstFileSyntax" format="%%ParamValue-%%BuildStamp.fst" />

		<syntax name="LogFileLatestSyntax"
		    format="ln -sf %%ParamValue-%%BuildStamp.log %SimulDir/%%ParamValue-latest.log;" />
		<syntax name="OutputFileLatestSyntax"
			format="ln -sf %%ParamValue-%%BuildStamp.ivlg %SimulDir/%%ParamValue-latest.ivlg;" />
		<syntax name="FstFileLatestSyntax"
		    format="ln -sf %%ParamValue-%%BuildStamp.fst %SimulDir/%%ParamValue-latest.fst;" />

		<!-- include file has target, not link value -->
		
 		<syntax name="FstParamFileSyntax"
			format="parameter fstname=&quot;%%ParamValue-%%BuildStamp.fst&quot;;" />
		<syntax name="IncludeParamSyntax" format="%(%%ParamValue%|\n%)" />
		
		
		<!-- Types definitions -->

		<typedef name="ModuleLibraryType" list="true">
			<paramtype kind="string" default="" textkind="dir"
				maxlength="256" sensitivity="sensitive" />
		</typedef>
		<typedef name="IncludeDirType" list="true">
			<paramtype kind="string" default="" textkind="dir"
				maxlength="256" sensitivity="sensitive" />
		</typedef>
		
		
		<typedef name="SwitchType">
			<paramtype kind="bool" formatTrue="-%%ParamName"
				formatFalse="" />
		</typedef>
		<typedef name="D_ParamType">
			<paramtype kind="bool" formatTrue="-D%%ParamName"
				formatFalse="" />
		</typedef>
		<typedef name="g_ParamType">
			<paramtype kind="bool" formatTrue="-g%%ParamName"
				formatFalse="" />
		</typedef>

	</interface>

	<tool name="iverilog" project="FPGA_project" label="Icarus Verilog compiler"
		shell="bash"
		package="FPGA_package"
		interface="IVerilog" errors="(.*):([0-9]+): [a-z_\- ]*error: (.*)"
		warnings="(.*):([0-9]+): [a-z_\- ]*warning: (.*)"
		info="(.*):([0-9]+): [a-z_\-  ]*sorry: (.*)"
		top-file="%IcarusTopFile"
		define="IVERILOG"
		>

		<extensions-list>
			<extension mask="v" />
			<extension mask="tf" />
		</extensions-list>

		<action-menu>
			<action label="Simulate project " resource="%IcarusTopFile"
				check-extension="false" check-existence="true" icon="iverilog.ico" />
			<action label="Simulate selected" resource="%%SelectedFile"
				check-extension="true" check-existence="true" icon="iverilog_pointer.png" />
			<action label="Verify" resource="%%SelectedFile"
				check-extension="true" check-existence="true" icon="iverilog_check.png" />
<!-- 		<action label="Empty" resource="" icon="sample.gif" />
			<action label="Just try for" resource="%%OS" />  -->	
		</action-menu>

        <parameter id="IcarusTopFile"
                        label="Simulation top file" tooltip="IVerilog simulator top file"
                        default="%SimulationTopFile"
                        type="String" format="CopyValue" />


		<parameter id="Param_Shell_Options" label="Param_Shell_Options"
			type="String" format="CopyValue" default="-c" readonly="false"
			visible="true" />

		<parameter id="Param_PreExe" label="Param_PreExe" type="String"
			format="CopyValue" default="" readonly="false" visible="true" />

		<parameter id="Param_Exe" label="Param_Exe" type="Filename"
			format="CopyValue" default="iverilog" readonly="false"
			visible="true" />

		<parameter id="VVP_prefix" label="VVP command prefix" type="String"
			format="CopyValue" default="time stdbuf -i0 -o0 -e0" omit="" readonly="false"
			visible="true" />

		<parameter id="VVP_Exe" label="VVP_Exe" type="Filename"
			format="CopyValue" default="vvp" readonly="false"
			visible="true" />

		<parameter id="GtkWave_Exe" label="GtkWave_Exe" type="Filename"
			format="CopyValue" default="gtkwave" readonly="false"
			visible="true" />

		<parameter id="Param_TopModule" label="Top module extracted from the chosen target file"
			type="String" format="TopModuleSyntax" default="%%TopModule"
			readonly="true" visible="true" />

		<parameter id="TopModulesOther" type="Stringlist"
			format="TopModulesOtherSyntax" default="" omit=""
			label="Select top modules not referenced by the chosen target"
			readonly="false" visible="true" />

		<parameter id="ModuleLibrary" type="ModuleLibraryType"
			format="ModuleLibrarySyntax" default="" label="Select additional libraries to include"
			omit="" readonly="false" visible="true" />


		<parameter id="ExtraFiles" type="Filelist" format="ExtraFilesSyntax"
			default="" label="Select additional files to include" readonly="false"
			visible="true" />

		<parameter id="IncludeDir" type="IncludeDirType"
			format="IncludeDirSyntax" default="" label="Select include file directories"
			omit="" readonly="false" visible="true" />


		<parameter id="legacy_model" outid="legacy_model" type="D_ParamType"
			format="D_ParamSyntax" default="true" label="Use legacy model" />

		<parameter id="no_specify" outid="no-specify" type="g_ParamType"
			format="NospecifySyntax" default="true" label="Use no-specify" />
			
		<!-- -->
		
		<parameter id="v" type="SwitchType" format="SwitchSyntax"
			default="true" label="Verbose" />

<!-- 	<parameter id="Param_SourceList" label="Param_SourceList"
			type="Stringlist" format="SourceListSyntax" default="" readonly="true"
			visible="true" />  -->
		<parameter id="SourceListPar"  label="Param_SourceList" type="Filelist"
			format="ParamListSyntax" default="%%FilteredSourceList" readonly="true" visible="false" />


		<parameter id="ShowNoProblem" type="BoolYesNo" format="None"
			default="false" label="Show output with no errors/warnings" />

		<parameter id="ShowWarnings" type="BoolYesNo" format="None"
			default="true" label="Show output warnings" />

		<parameter id="RemoveBugs" type="BoolYesNo" format="None"
			default="false" label="Remove buggy simulator output" />

		<parameter id="SaveLogsPreprocessor" type="BoolYesNo"
			format="None" default="false" label="Save simulator preprocessor log output" />

		<parameter id="SaveLogsSimulator" type="BoolYesNo" format="None"
			default="false" label="Save simulator log output" />

		<parameter id="ShowWaves" type="BoolYesNo" format="None"
			default="true" label="Show simulation result in waveform viewer" />

		<!-- Advanced Section -->


		<parameter id="LogFile" label="Simulator log file prefix"
			type="String" default="%%CurrentFileBase" format="LogFileSyntax"
			readonly="false" />
		<parameter id="OutFile" label="Simulator intermediate file prefix"
			type="String" default="%%CurrentFileBase" format="OutFileSyntax"
			readonly="false" />
		<parameter id="FstDumpFile" label="Simulator FST dump file prefix"
			type="String" default="%%CurrentFileBase" format="FstFileSyntax"
			readonly="false" />

		<parameter id="GTKWaveSavFile" label="GTKWave sav file"
			type="Filename" default="%%CurrentFileBase.sav" format="CopyValue"
			omit="" readonly="false" />

		<parameter id="GrepFindErr" label="Grep pattern for errors only"
			type="String" format="GrepFindSyntax" default="error" readonly="false"
			visible="true" />

		<parameter id="GrepFindErrWarn" label="Grep pattern for both errors and warnings"
			type="String" format="GrepFindSyntax" default="error|warning|sorry"
			readonly="false" visible="true" />

		<parameter id="GrepSkip1" label="Grep skip pattern" type="String"
			format="GrepSkipSyntax" default="(null)" readonly="false" visible="true" />
			
		<parameter id="IVerilogOther" label="Other IVerilog options"
			type="String" format="CopyValue" default="" omit=""
			readonly="false" visible="true" />

		<parameter id="IncludeParametersList" type="Stringlist"
			format="IncludeParamSyntax" default="" omit=""
			label="Verilog parameters definition to be included in the test fixture"
			readonly="false" visible="true" />
			
<!-- Temporary inserted into the command line, will be removed -->

		<parameter id="Param_1" label="Param_1" type="String"
			format="CopyValue" default="" omit="" readonly="false" visible="true" />

		<parameter id="Param_2" label="Param_2" type="String"
			format="CopyValue" default="" omit="" readonly="false" visible="true" />

		<parameter id="Param_3" label="Param_3" type="String"
			format="CopyValue" default="" omit="" readonly="false" visible="true" />

		<parameter id="Param_4" label="Param_4" type="String"
			format="CopyValue" default="" omit="" readonly="false" visible="true" />
			
			
		<!-- Invisible parameters, just pass calculated default values -->
		
		<parameter id="IVerilogActionIndex" default="%%ChosenActionIndex"
		 type="String" format="CopyValue" visible="false"  />

<!-- 	<parameter id="SourceList" format="SourceListSyntax"
			type="Stringlist" default="" readonly="true" visible="false" /> -->
		<parameter id="SourceListPar"  label="Param_SourceList" type="Filelist"
			format="ParamListSyntax" default="%%FilteredSourceList" readonly="true" visible="false" />

		<parameter id="iverilog_include_file" default="IVERILOG_INCLUDE.v"
			type="Filename" format="CopyValue" visible="false" />

		<parameter id="FstDumpFileParameter" default="%FstDumpFileFull" 
			type="String" format="FstParamFileSyntax" visible="false" />
			
		<parameter id="LogFileLatest" default="%LogFile" 
			type="String" format="LogFileLatestSyntax" visible="false" />
			
		<parameter id="OutFileLatest" type="String" default="%OutFile" 
			format="OutputFileLatestSyntax" visible="false" />
			
		<parameter id="FstDumpFileLatest" default="%FstDumpFile" 
			type="String" format="FstFileLatestSyntax" visible="false" />

		<parameter id="LogFileFull" default="%SimulDir/%LogFile" 
			visible="false" type="String" format="LogFileSyntax"/>
			
		<parameter id="OutFileFull" default="%SimulDir/%OutFile" 
		    visible="false"	type="String" format="OutFileSyntax"/>
		    
		<parameter id="FstDumpFileFull" default="%SimulDir/%FstDumpFile" 
			visible="false"	type="String" format="FstFileSyntax"/>

		<input>
			<group name="files" label="Files">
				<!-- "SimulationTopFile" -->
				"IcarusTopFile"
				"Param_TopModule"
				"TopModulesOther"
				"ExtraFiles"
				"ModuleLibrary"
				"IncludeDir"
				"GTKWaveSavFile"
				"IncludeParametersList"
			</group>
			<group name="options" label="Options">
				"ShowWaves"
				"ShowNoProblem"
				"ShowWarnings"
				"RemoveBugs"
				"SaveLogsPreprocessor"
				"SaveLogsSimulator"
				"v"
				"legacy_model"
				"no_specify"
			</group>
			<group name="Advanced" label="Advanced">
				"Param_PreExe"
				"Param_Exe"
				"Param_Shell_Options"
				"IVerilogOther"
				"VVP_prefix"
				"VVP_Exe"
				"GtkWave_Exe"
				"LogFile"
				"OutFile"
				"FstDumpFile"
				"GrepFindErr"
				"GrepFindErrWarn"
				"GrepSkip1"
				"Param_1"
				"Param_2"
				"Param_3"
				"Param_4"
			</group>
		</input>
		<output>
		<!-- TODO: watch for new lines inserted inside quoted tokens during autoformat - they break output
		Maybe add filter to the code to transform white spaces -->
			<line name="command_line" sep=" ">
				"%Param_Shell_Options"
				"%Param_PreExe"
<!--  MKDIR - ALWAYS -->				
			    "mkdir -p"
			    "%SimulDir"
			    ";"
				<if SaveLogsPreprocessor="true"
				    SaveLogsSimulator="true">
					"touch"
					"%LogFileFull"
					";"
					"%LogFileLatest"
				</if>
				
				"trap 'killall iverilog; ' EXIT;"
				"echo 'current PID='$$;"
				"%Param_Exe"
				"-o"
				<if IVerilogActionIndex="2">
					"/dev/null"
				</if>
				<if-not IVerilogActionIndex="2">
					"%OutFileFull"
				</if-not>
				" -DIVERILOG"
				"%IVerilogOther"
				"%Param_TopModule"
				"%TopModulesOther"
				"%ModuleLibrary"
				"%IncludeDir"
				"%legacy_model"
				"%no_specify"
				"%v"
				"%SourceListPar"
				"%ExtraFiles"
				<if ShowNoProblem="false">
					"2&gt;&amp;1"
				</if>
				<if SaveLogsPreprocessor="true">
					"| tee -a"
					"%LogFileFull"
				</if>
				<if ShowNoProblem="false">
					<if ShowWarnings="true">
						"%GrepFindErrWarn"
					</if>
					<if ShowWarnings="false">
						"%GrepFindErr"
					</if>
					<if RemoveBugs="true">
						"%GrepSkip1"
					</if>
				</if>
				"|| { echo '*** iverilog failed ***'; exit 1; } ;"
				"trap '' EXIT;"
				<if-not IVerilogActionIndex="2">
				    "%OutFileLatest" 
					<!-- "time vvp -v" -->
					"%Param_1"
					"trap 'killall vvp; ' EXIT;"
					"%VVP_prefix"
					"%VVP_Exe -v"
					"%Param_2"
					"%OutFileFull"
					"-fst "
					<if SaveLogsSimulator="true">
						"| tee -a"
						"%LogFileFull"
					</if>
					"|| { echo '*** vvp failed ***'; exit 1; } ;"
					"%FstDumpFileLatest"
					 <!-- no trap for GTKWave -->
					"trap '' EXIT;"
					<!-- "%Param_2" -->
					"%Param_3"
					<if ShowWaves="true">
						"%GtkWave_Exe"
						"%FstDumpFileFull"
						"%GTKWaveSavFile"
						"&amp; "
					</if>
					"%Param_4"
				</if-not>
			</line>
			<line name="IverilogIncludeFile" dest="iverilog_include_file"
				sep="\n">
				"%FstDumpFileParameter"
				"%IncludeParametersList"
			</line>
		</output>


	</tool>

</vdt-project>