1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
###############################################################################
# Copyright (c) 2011-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform.  As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose.  Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
#   @userware
#       Quartus Link
#   @section
#       Link to Other Tools
#   @description
#       RTLvision PRO userware to implement a GUI showing the use of
#       `server.tcl` and `client.tcl`.
#
#       Start Quartus in the background, start a server inside Quartus and
#       connect to this server (RTLvision is the client). Now a Quartus project
#       can be created, the timing netlist can be transferred and the timing
#       paths can be queried from Quartus and visualized in RTLvision.
#   @files
#       quartus/quartusLink.tcl
#   @tag
#       rtl verilog fpga
###############################################################################


# =============================================================================
# Load Quartus II Tcl Project package
# =============================================================================
#
set Q(dir) [file normalize [file dirname [info script]]]
source [file join $Q(dir) client.tcl]
source [file join $Q(dir) pathVision.tcl]

##
# Initialize variables to store Quartus specific settings.
#
set Q(families)  {}
set Q(filenames) {}
set Q(prjName)   {}
set Q(topName)   {}
set Q(family)    {}


# -----------------------------------------------------------------------------
# _browse - Browse for a Verilog fileset or for Verilog files.
# -----------------------------------------------------------------------------
#
proc Q:_browse {} {
    global Q

    set fTypes {{"Fileset" {.f}} {"Verilog" {.v}}}
    set fnames [gui window fileDialog openFiles "Open Files" $fTypes]
    if {$fnames == {}} {
        return
    }
    set Q(filenames) $fnames
}


# =============================================================================
# CreateProject - Create a Quartus project based on the given fileset or files.
# =============================================================================
#
proc Q:CreateProject {} {
    global Q

    toplevel      .qcreate -class Dialog
    wm transient  .qcreate [winfo toplevel [winfo parent .qcreate]]
    wm title .qcreate "Create Project"

    label         .qcreate.prjl   -text "Name:"
    ttk::entry    .qcreate.prje   -textvariable Q(prjName)

    label         .qcreate.topl   -text "Top:"
    ttk::entry    .qcreate.tope   -textvariable Q(topName)

    label         .qcreate.faml   -text "Family:"
    ttk::combobox .qcreate.famc   -textvariable Q(family) -values $Q(families)

    label        .qcreate.filel  -text "File(s):"
    ttk::entry   .qcreate.filee  -textvariable Q(filenames)
    ttk::button  .qcreate.browse -text "Browse" -command "Q:_browse"

    ttk::button  .qcreate.ok     -text Ok      -command "Q:_create"
    ttk::button  .qcreate.cancel -text Cancel  -command "destroy .qcreate"
    ttk::button  .qcreate.curr   -text Current -command "Q:_current"

    grid .qcreate.prjl   -row 0 -column 0 -sticky w
    grid .qcreate.prje   -row 0 -column 1 -sticky we -columnspan 2
    grid .qcreate.topl   -row 1 -column 0 -sticky w
    grid .qcreate.tope   -row 1 -column 1 -sticky we -columnspan 2
    grid .qcreate.faml   -row 2 -column 0 -sticky w
    grid .qcreate.famc   -row 2 -column 1 -sticky we -columnspan 2
    grid .qcreate.filel  -row 3 -column 0 -sticky w
    grid .qcreate.filee  -row 3 -column 1 -sticky we -columnspan 2
    grid .qcreate.browse -row 3 -column 3
    grid .qcreate.ok     -row 4 -column 0 -sticky w
    grid .qcreate.cancel -row 4 -column 1 -sticky w
    grid .qcreate.curr   -row 4 -column 3 -sticky w
    grid columnconfigure .qcreate 1 -weight 1
    grid columnconfigure .qcreate 2 -weight 1
}


# -----------------------------------------------------------------------------
# _current - Guess values for creating a Quartus project based on the
#            currently loaded design.
# -----------------------------------------------------------------------------
#
proc Q:_current {} {
    global Q

    set db [gui database get]

    set Q(filenames) {}
    $db foreach top t { set Q(topName) [$db oid oname $t]; break; }
    $db spos foreachfile fname moddate {
        lappend Q(filenames) $fname
    }
    set Q(prjName)   $Q(topName)
}


# -----------------------------------------------------------------------------
# _create - Check that all given values are OK and create the Quartus project.
# -----------------------------------------------------------------------------
#
proc Q:_create {} {
    global Q

    destroy .qcreate

    set files   {}
    set fileset {}
    foreach f $Q(filenames) {
        if {[file extension $f] == ".f"} {
            set fileset $f
        } else {
            lappend files $f
        }
    }
    if {($files != {}) && ($fileset != {})} {
        gui console print "multiple files ignored, if a fileset is given"
        set files {}
    }

    Client:Cmd [list QuUtils:CreateProject $Q(prjName) $Q(topName) \
                                $Q(family) $files $fileset true]
}


# =============================================================================
# LoadProject - Show a dialog to load an already existing Quartus project.
# =============================================================================
#
proc Q:LoadProject {} {
    global Q

    toplevel     .qload -class Dialog
    wm transient .qload [winfo toplevel [winfo parent .qload]]
    wm title .qload "Load Project"

    label       .qload.prjl   -text "Name:"
    ttk::entry  .qload.prje   -textvariable Q(prjName)

    ttk::button .qload.ok     -text Ok      -command "Q:_load"
    ttk::button .qload.cancel -text Cancel  -command "destroy .qload"

    grid .qload.prjl   -row 0 -column 0 -sticky w
    grid .qload.prje   -row 0 -column 1 -sticky we -columnspan 2
    grid .qload.ok     -row 4 -column 0 -sticky w
    grid .qload.cancel -row 4 -column 1 -sticky w
    grid columnconfigure .qload 1 -weight 1
    grid columnconfigure .qload 2 -weight 1
}


# -----------------------------------------------------------------------------
# _load - Send commands to Quartus to open a project.
# -----------------------------------------------------------------------------
#
proc Q:_load {} {
    global Q db

    ##
    # Destroy the .qload dialog.
    #
    destroy .qload

    ##
    # Send the command to open a project.
    #
    Client:Cmd "project_open $Q(prjName)"
    Client:Cmd "create_timing_netlist"

    ##
    # Transfer the timing netlist and enable progress bar.
    #
    zprogress begin
    Client:Cmd "AccessDB:Copy2zdb %s 1"
    zprogress end

    ##
    # Send the command to close a project.
    #
    Client:Cmd "project_close"
    gui database changed $db
}


# =============================================================================
# GetSTAReport - Get the STA report based on the timing netlist.
# =============================================================================
#
proc Q:GetSTAReport {} {
    global Q

    ##
    # Add the result of 'get_timing_paths' to the output file.
    #
    Client:Cmd "project_open $Q(prjName)"
    Client:Cmd "create_timing_netlist"
    Client:Cmd "Quartus_sta_report:Main \"q_sta_report_file.tcl\" false"
    Client:Cmd "project_close"

    ##
    # Source the tmp file created by the code above.
    #
    source q_sta_report_file.tcl
}


# -----------------------------------------------------------------------------
# _getFamilies - Get a list of all families.
# -----------------------------------------------------------------------------
#
proc Q:_getFamilies {} {
    global Q

    set Q(families) {}
    Client:Cmd "puts %s \"set Q(families) {\[get_family_list\]}\""
    set Q(families) [lsort $Q(families)]
    set Q(family)   [lindex $Q(families) 0]
}


# =============================================================================
# StartServer - Start the 'quartus_sta' binary in the background with the
#               'server.tcl' script to run a server inside Quartus. Then send
#               a source command through the socket to load the 'accessDB.tcl'
#               file that defines procedures to access the Quartus DB.
# =============================================================================
#
proc Q:StartServer {} {
    global Q

    ##
    # Execute the 'quartus_sta' binary with the script that starts the server.
    #
    set pid [exec quartus_sta -t [file join $Q(dir) server.tcl] &]

    ##
    # Wait for the server.
    #
    Client:WaitForServer
    gui console print "Server started"

    ##
    # Source the 'accessDB.tcl' script on the Quartus side.
    #
    Client:Cmd "source [file join $Q(dir) accessDB.tcl]"

    ##
    # Source the 'sta_report.tcl' script on the Quartus side. To avoid an
    # autostart of this script set the 'Quartus_sta_report_autostart' variable
    # to false on the Quartus side before sending the source command.
    #
    Client:Cmd "set Quartus_sta_report_autostart false"
    Client:Cmd "source [file join $Q(dir) sta_report.tcl]"

    Q:_getFamilies
    set Q(serverStarted) 1
}


# =============================================================================
# StopServer - Stop the Quartus server.
# =============================================================================
#
proc Q:StopServer {} {
    global Q

    Client:Cmd "Server:Shutdown"
    gui console print "Server stopped"
    set Q(serverStarted) 0
}


# -----------------------------------------------------------------------------
# _menuCb - Enable/Disable the menu entries based on the socket state.
# -----------------------------------------------------------------------------
#
proc Q:_menuCb {startItem menuId} {
    global Q

    if {$Q(serverStarted)} {
        if {$startItem} {
            set state "disable"
        } else {
            set state "normal"
        }
    } else {
        if {$startItem} {
            set state "normal"
        } else {
            set state "disable"
        }
    }
    $menuId  entryconfigure end -state $state
}


##
# If running the first time then add a new main menu entry 'Quartus'.
# Add all further commands below this entry.
#
if {![info exists Q(serverStarted)]} {
    set Q(serverStarted) 0

    gui menu command     {"Quartus" "Start Quartus Server"} {Q:StartServer}
    gui menu command     {"Quartus" "Create Project" }      {Q:CreateProject}
    gui menu command     {"Quartus" "Get Timing Netlist"}   {Q:LoadProject}
    gui menu command     {"Quartus" "Get STA Report"}       {Q:GetSTAReport}
    gui menu command     {"Quartus" "Stop  Quartus Server"} {Q:StopServer}
    gui menu customizeEntry {"Quartus" "Start Quartus Server"} {Q:_menuCb 1}
    gui menu customizeEntry {"Quartus" "Create Project" }      {Q:_menuCb 0}
    gui menu customizeEntry {"Quartus" "Get Timing Netlist"}   {Q:_menuCb 0}
    gui menu customizeEntry {"Quartus" "Get STA Report"}       {Q:_menuCb 0}
    gui menu customizeEntry {"Quartus" "Stop  Quartus Server"} {Q:_menuCb 0}
}