1 # ==============================================================================
2 #  Authors:
3 #    Patrick Lehmann
4 #
5 # ==============================================================================
6 #  Copyright (C) 2017-2021 Patrick Lehmann - Boetzingen, Germany
7 #  Copyright (C) 2015-2016 Patrick Lehmann - Dresden, Germany
8 #
9 #  This program is free software: you can redistribute it and/or modify
10 #  it under the terms of the GNU General Public License as published by
11 #  the Free Software Foundation, either version 2 of the License, or
12 #  (at your option) any later version.
13 #
14 #  This program is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19 #  You should have received a copy of the GNU General Public License
20 #  along with this program.  If not, see <gnu.org/licenses>.
21 # ==============================================================================
22 
23 # .SYNOPSIS
24 # Script to compile the simulation libraries from Xilinx Vivado for GHDL on Windows.
25 #
26 # .DESCRIPTION
27 # This CmdLet:
28 #   (1) creates a subdirectory in the current working directory
29 #   (2) compiles all Xilinx Vivado simulation libraries and packages
30 #       - unisim (incl. secureip)
31 #       - unimacro
32 #
33 [CmdletBinding()]
34 param(
35 	# Show the embedded help page(s)
36 	[switch]$Help =             $false,
37 
38 	# Compile all libraries and packages.
39 	[switch]$All =              $false,
40 
41 	# Compile the Xilinx simulation library.
42 	[switch]$Unisim =           $false,
43 
44 	# Compile the Xilinx macro library.
45 	[switch]$Unimacro =         $false,
46 
47 	# Compile the Xilinx secureip library.
48 	[switch]$SecureIP =         $false,
49 
50 	# Clean up directory before analyzing.
51 	[switch]$Clean =            $false,
52 
53 	# Set VHDL Standard to '93.
54 	[switch]$VHDL93 =           $false,
55 	# Set VHDL Standard to '08.
56 	[switch]$VHDL2008 =         $false,
57 
58 	# Skip warning messages. (Show errors only.)
59 	[switch]$SuppressWarnings = $false,
60 	# Halt on errors.
61 	[switch]$HaltOnError =      $false,
62 
63 	# Set vendor library source directory.
64 	[string]$Source =           "",
65 	# Set output directory name.
66 	[string]$Output =           "",
67 	# Set GHDL binary directory.
68 	[string]$GHDL =             ""
69 )
70 
71 # ---------------------------------------------
72 # save working directory
73 $WorkingDir =     Get-Location
74 
75 # set default values
76 $EnableDebug =    [bool]$PSCmdlet.MyInvocation.BoundParameters["Debug"]
77 $EnableVerbose =  [bool]$PSCmdlet.MyInvocation.BoundParameters["Verbose"] -or $EnableDebug
78 
79 # load modules from GHDL's 'vendors' library directory
80 $EnableVerbose -and  (Write-Host "Loading modules..." -ForegroundColor Gray  ) | Out-Null
81 $EnableDebug -and    (Write-Host "  Import-Module $PSScriptRoot\config.psm1 -Verbose:`$$false -Debug:`$$false -ArgumentList `"XilinxVivado`"" -ForegroundColor DarkGray  ) | Out-Null
82 Import-Module $PSScriptRoot\config.psm1 -Verbose:$false -ArgumentList "XilinxVivado"
83 $EnableDebug -and    (Write-Host "  Import-Module $PSScriptRoot\shared.psm1 -Verbose:`$$false -Debug:`$$false -ArgumentList @(`"Xilinx Vivado`", `"$WorkingDir`")" -ForegroundColor DarkGray  ) | Out-Null
84 Import-Module $PSScriptRoot\shared.psm1 -Verbose:$false -ArgumentList @("Xilinx Vivado", "$WorkingDir")
85 
86 # Display help if no command was selected
87 $Help = $Help -or (-not ($All -or $Unisim -or $Simprim -or $Unimacro))
88 
89 if ($Help)
90 {	Get-Help $MYINVOCATION.MyCommand.Path -Detailed
91 	Exit-CompileScript
92 }
93 if ($All)
94 {	$Unisim =   $true
95 	$Simprim =  $true
96 	$Unimacro = $true
97 	$SecureIP = $true
98 }
99 
Get-XilinxVivadoDirectory()100 function Get-XilinxVivadoDirectory
101 {	if (Test-Path env:XILINX_VIVADO)
102 	{	return $XILINX_VIVADO + "\" + (Get-VendorToolSourceDirectory)    }
103 	else
104 	{	$EnvSourceDir = ""
105 		foreach ($Drive in Get-PSDrive -PSProvider 'FileSystem')
106 		{	$Path = $Drive.Name + ":\" + "Xilinx\Vivado"
107 			if (Test-Path $Path -PathType Container)
108 			{	foreach ($Major in 2021..2014)
109 				{	foreach ($Minor in 4..1)
110 					{	$Dir = $Path + "\" + $Major + "." + $Minor
111 						if (Test-Path $Dir -PathType Container)
112 						{	$EnvSourceDir = $Dir + "\" + (Get-VendorToolSourceDirectory)
113 							return $EnvSourceDir
114 						}
115 					}
116 				}
117 			}
118 		}
119 	}
120 }
121 
122 $SourceDirectory =      Get-SourceDirectory $Source (Get-XilinxVivadoDirectory)
123 $DestinationDirectory = Get-DestinationDirectory $Output
124 $GHDLBinary =           Get-GHDLBinary $GHDL
125 
126 # create "Altera" directory and change to it
127 New-DestinationDirectory $DestinationDirectory
128 cd $DestinationDirectory
129 
130 if ($VHDL2008)
131 {	Write-Host "Not all Xilinx primitives are VHDL-2008 compatible! Setting HaltOnError to FALSE." -ForegroundColor Red
132 	$HaltOnError =      $false
133 }
134 $VHDLVersion,$VHDLStandard,$VHDLFlavor = Get-VHDLVariables $VHDL93 $VHDL2008
135 
136 # define global GHDL Options
137 $Analyze_Parameters = @(
138 	"-fexplicit",
139 	"-frelaxed-rules",
140 	"--mb-comments",
141 	"-Wbinding"
142 )
143 if (-not $EnableDebug)
144 {	$Analyze_Parameters += @(
145 		"-Wno-hide"
146 	)
147 }
148 if (-not ($EnableVerbose -or $EnableDebug))
149 { $Analyze_Parameters += @(
150 		"-Wno-library",
151 		"-Wno-others",
152 		"-Wno-static"
153 	)
154 }
155 $Analyze_Parameters += @(
156 	"--ieee=$VHDLFlavor",
157 	"--no-vital-checks",
158 	"--std=$VHDLStandard",
159 	"-P$DestinationDirectory"
160 )
161 
162 # extract data from configuration
163 # $SourceDir =      $InstallationDirectory["AlteraQuartus"] + "\quartus\eda\sim_lib"
164 
165 $StopCompiling =  $false
166 $ErrorCount =     0
167 
168 
169 # Cleanup directories
170 # ==============================================================================
171 if ($Clean)
172 {	Write-Host "[ERROR]: '-Clean' is not implemented!" -ForegroundColor Red
173 	Exit-CompileScript -1
174 
175 	Write-Host "Cleaning up vendor directory ..." -ForegroundColor Yellow
176 	rm *.cf
177 }
178 
179 
180 # Library UNISIM
181 # ==============================================================================
182 # compile unisim packages
183 if ((-not $StopCompiling) -and $Unisim)
184 {	$Library = "unisim"
185 	$Files = @(
186 		"unisims\unisim_VPKG.vhd",
187 		"unisims\unisim_VCOMP.vhd",
188 		"unisims\retarget_VCOMP.vhd",
189 		"unisims\unisim_retarget_VCOMP.vhd"
190 	)
191 	$SourceFiles = $Files | % { "$SourceDirectory\$_" }
192 
193 	$ErrorCount += Start-PackageCompilation $GHDLBinary $Analyze_Parameters $DestinationDirectory $Library $VHDLVersion $SourceFiles $SuppressWarnings $HaltOnError -Verbose:$EnableVerbose -Debug:$EnableDebug
194 	$StopCompiling = $HaltOnError -and ($ErrorCount -ne 0)
195 }
196 
197 # compile unisim primitives
198 if ((-not $StopCompiling) -and $Unisim)
199 {	$Library = "unisim"
200 	$SourceFiles = dir "$SourceDirectory\unisims\primitive\*.vhd*"
201 
202 	$ErrorCount += Start-PrimitiveCompilation $GHDLBinary $Analyze_Parameters $DestinationDirectory $Library $VHDLVersion $SourceFiles $SuppressWarnings $HaltOnError -Verbose:$EnableVerbose -Debug:$EnableDebug
203 	$StopCompiling = $HaltOnError -and ($ErrorCount -ne 0)
204 }
205 
206 # compile unisim retarget primitives
207 if ((-not $StopCompiling) -and $Unisim)
208 {	$Library = "unisim"
209 	$SourceFiles = dir "$SourceDirectory\unisims\retarget\*.vhd*"
210 
211 	$ErrorCount += Start-PrimitiveCompilation $GHDLBinary $Analyze_Parameters $DestinationDirectory $Library $VHDLVersion $SourceFiles $SuppressWarnings $HaltOnError -Verbose:$EnableVerbose -Debug:$EnableDebug
212 	$StopCompiling = $HaltOnError -and ($ErrorCount -ne 0)
213 }
214 
215 # compile unisim secureip primitives
216 if ((-not $StopCompiling) -and $Unisim -and $SecureIP)
217 {	$Library = "secureip"
218 	$SourceFiles = dir "$SourceDirectory\unisims\secureip\*.vhd*"
219 
220 	$ErrorCount += Start-PrimitiveCompilation $GHDLBinary $Analyze_Parameters $DestinationDirectory $Library $VHDLVersion $SourceFiles $SuppressWarnings $HaltOnError -Verbose:$EnableVerbose -Debug:$EnableDebug
221 	$StopCompiling = $HaltOnError -and ($ErrorCount -ne 0)
222 }
223 
224 # Library UNIMACRO
225 # ==============================================================================
226 # compile unimacro packages
227 if ((-not $StopCompiling) -and $Unimacro)
228 {	$Library = "unimacro"
229 	$Files = @(
230 		"unimacro\unimacro_VCOMP.vhd"
231 	)
232 	$SourceFiles = $Files | % { "$SourceDirectory\$_" }
233 
234 	$ErrorCount += Start-PackageCompilation $GHDLBinary $Analyze_Parameters $DestinationDirectory $Library $VHDLVersion $SourceFiles $SuppressWarnings $HaltOnError -Verbose:$EnableVerbose -Debug:$EnableDebug
235 	$StopCompiling = $HaltOnError -and ($ErrorCount -ne 0)
236 }
237 
238 # compile unimacro macros
239 if ((-not $StopCompiling) -and $Unimacro)
240 {	$Library = "unimacro"
241 	$SourceFiles = dir "$SourceDirectory\unimacro\*_MACRO.vhd*"
242 
243 	$ErrorCount += Start-PrimitiveCompilation $GHDLBinary $Analyze_Parameters $DestinationDirectory $Library $VHDLVersion $SourceFiles $SuppressWarnings $HaltOnError -Verbose:$EnableVerbose -Debug:$EnableDebug
244 	$StopCompiling = $HaltOnError -and ($ErrorCount -ne 0)
245 }
246 
247 # Library UNIFAST
248 # ==============================================================================
249 # TODO:
250 
251 Write-Host "--------------------------------------------------------------------------------"
252 Write-Host "Compiling Xilinx Vivado libraries " -NoNewline
253 if ($ErrorCount -gt 0)
254 {	Write-Host "[FAILED]" -ForegroundColor Red        }
255 else
256 {	Write-Host "[SUCCESSFUL]" -ForegroundColor Green  }
257 
258 Exit-CompileScript
259