# verilog-mode.tcl - mode for editing verilog code
######################################################################

global stead
global MODE EDITPREFS

########################################################################
global COLOR_PROC  ; set COLOR_PROC blue
global COLOR_COMMENT  ; set COLOR_COMMENT green4
###########################################################################
# cette variable contient une liste de fichier à éditer dans ce mode
global MODE_FAST_EDIT
#/tmp_mnt/auto/m1/cadence/tools.sun4/verilog/examples/HDL
set MODE_FAST_EDIT {
    /usr/valid/tools/verilog/examples/HDL/sram.v
    /usr/valid/tools/verilog/examples/HDL/rcount.v
    /usr/valid/tools/verilog/examples/HDL/shreg.v
    /usr/valid/tools/verilog/examples/HDL/sio85.v
    /usr/valid/tools/verilog/examples/HDL/mos01.v
}

global TE_MODE_SEARCH_STRING TE_MODE_NAVIG_SHOW_TEXT
# chaine de recherche 
set TE_MODE_SEARCH_STRING "\n\[ \t\]*module\[ \t\]*(\[a-zA-Z0-9_\]*)"
# nom de l'element de recherche
set TE_MODE_NAVIG_SHOW_TEXT "modules"

# # # variable de gestion des electric-alias
# # global EA_8bit_mark EA_8bit_begin EA_8bit_end EA_8bit_var
# # set M $EA_8bit_mark
# # set B $EA_8bit_begin
# # set E $EA_8bit_end
# # set V $EA_8bit_var


# Pour simplifier l'écriture des electricAlias : on déclare
# les variables M, B, E et V au niveau local pour 
# 
#      M :  Marque temporaire
#      B :  début de postcommande 
#      E :  fin de postcommande
#      V :  Inclusion d'une variable TCL
# 
electricAlias-use8bitsCars



###########################################################################
# aide sur le mode verilog :
###########################################################################

global verilog_HELP
set verilog_HELP {
aide du mode verilog prototype :

alias          résultat
^^^^^          ^^^^^^^^

md             bloc interactif "module (..) ... endmodule"
fn             bloc interactif "function  ... endfunction" 
task           bloc interactif "task  ... endtask" 

ini            bloc " initial begin ... end"
al             bloc " always begin ... end" 
fv             bloc " forever begin ... end"

for            bloc "for (;;) begin ... end"
while  (wh)    bloc "while () begin ... end"
repeat  (rp)   bloc "repeat () begin ... end"
case           bloc "case ... endcase"

i              "input ;"
o              "output ;"
io             "inout ;"

=             "//=========================//" 
==            "//================================================//"


Remarques :

<Alt>+<space> pour executer l'alias
<Ctrl>+<Tab> pour changer de champ

}



###########################################################################

# Aliases for Verilog mode

electricAlias-def ${M}verilog ${M}verilog ${M}verilog
electricAlias-var verilog bb "begin"
electricAlias-var verilog eb "end"
electricAlias-var verilog bc "//"
electricAlias-var verilog ec ""
electricAlias-def ${M}verilog =   "//=======================//\n$M"
electricAlias-def ${M}verilog ==   "//=============================================//\n$M"
electricAlias-def ${M}verilog \{       "$V{bb}\n${B}i${E}$M\n$V{eb}$M"
electricAlias-def ${M}verilog fork     "fork\n${B}i${E}$M\njoin\n$M"
electricAlias-def ${M}verilog i    "input  $M;\n$M"
electricAlias-def ${M}verilog io    "inout  $M;\n$M"
electricAlias-def ${M}verilog o   "output $M;\n$M"
electricAlias-def ${M}verilog wire     "wire   $M;\n$M"
electricAlias-def ${M}verilog tri      "tri    $M;\n$M"
electricAlias-def ${M}verilog reg      "reg    $M;\n$M"
electricAlias-def ${M}verilog assign   "assign $M = $M;\n$M"

set t1 "¤Çaskalias {module name} NAMEÈmodule ¤{NAME} ($M);\n${B}i${E}$M\n"
set t2 "endmodule¤{bc} ¤{NAME}¤{ec}\n\n$M"
electricAlias-def ${M}verilog md $t1$t2

set t1 "${B}askalias {function name} NAME${E}function $V{NAME};\n"
set t2 "${B}i${E}$M\nendfunction$V{bc} $V{NAME}$V{ec}\n\n$M"
electricAlias-def ${M}verilog fn $t1$t2

set t1 "${B}askalias {task name} NAME${E}task $V{NAME};\n"
set t2 "${B}i${E}$M\nendtask$V{bc} $V{NAME}$V{ec}\n\n$M"
electricAlias-def ${M}verilog task $t1$t2

set t1 "for ($M;$M;$M) $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}for$V{ec}$M"
electricAlias-def ${M}verilog for $t1$t2

set t1 "while ($M) $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}while$V{ec}$M"
electricAlias-def ${M}verilog wh $t1$t2

set t1 "while ($M) $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}while$V{ec}$M"
electricAlias-def ${M}verilog while $t1$t2

set t1 "repeat ($M) $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}repeat$V{ec}$M"
electricAlias-def ${M}verilog rp $t1$t2

set t1 "repeat ($M) $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}repeat$V{ec}$M"
electricAlias-def ${M}verilog repeat $t1$t2


set t1 "forever $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}forever$V{ec}$M"
electricAlias-def ${M}verilog fv $t1$t2

set t1 "if ($M) $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}if$V{ec}$M"
electricAlias-def ${M}verilog if $t1$t2

set t1 "initial $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}initial$V{ec}$M"
electricAlias-def ${M}verilog ini $t1$t2

set t1 "always $V{bb}\n${B}i${E}$M\n"
set t2 "$V{eb} $V{bc}always$V{ec}$M"
electricAlias-def ${M}verilog al $t1$t2

electricAlias-def ${M}verilog elseif   "\b$V{eb} else if ($M) $V{bb}\n$M"
electricAlias-def ${M}verilog else     "\b$V{eb} else $V{bb}\n$M"
electricAlias-def ${M}verilog case      "case  ($M)\n${B}i${E}$M\nendcase$M"
electricAlias-def ${M}verilog casex     "casex ($M)\n${B}i${E}$M\nendcase$M"
electricAlias-def ${M}verilog casez     "casez ($M)\n${B}i${E}$M\nendcase$M"



###########################################################################
# tri des modules par ordre alphabetique
###########################################################################

proc te:mode:navig_sort_hook {l} {
  return [lsort -command te:mode:sort_command $l]
}

###########################################################################
# proc separator {{NbLines 1}}
# Insère "Nblines" lignes (défaut : 1) correspondant au separateur 
# souhaité ;
# Cette procédure est automatiquement liée à <Meta-1>
###########################################################################
proc separator {{NbLines 1} } {
   
   insert_fill_lines "/" $NbLines 

} ;# endproc separator

######################################################################
# define the VERILOG menu:

te:mode:make_menu $MODE

.menu.verilog.m add command -label {Colorize Modules} \
-command {te:cmd:colorise $TE_MODE_SEARCH_STRING all $COLOR_PROC}
.menu.verilog.m add command -label {Colorize Comments //} \
-command {
           te:cmd:colorise "//\[^\n\]*"                   all $COLOR_COMMENT
           te:cmd:colorise {/\*(([^*]+)|(\*+[^*/]))+\*+/} all $COLOR_COMMENT
         }       

         
.menu.verilog.m add separator
.menu.verilog.m add command -label {Clear Modules colour} \
-command {stead:coloursResetForeground_BOF all blue}
.menu.verilog.m add command -label {Clear Comments colour} \
-command {stead:coloursResetForeground_BOF all magenta}

.menu.verilog.m add separator
.menu.verilog.m add command -label {Aide du mode verilog}  \
   -command {stead -r 1 -text $verilog_HELP }  
                      
.menu.verilog.m add cascade -label {Files example}  \
   -menu ".menu.verilog.m.files"
                         
menu .menu.verilog.m.files
foreach {file} $MODE_FAST_EDIT {
   .menu.verilog.m.files add command -label $file \
      -command "exec $stead(exe) $file &"
} ;# endforeach  
                    
###########################################################################
# definit le menu des mots-cles
#
#te:mode:make_menu {templates}
#
#te:cmd:create_alias_menu .menu.templates.m
#


###########################################################################
# On crée le menu template s'il n'existe pas
###########################################################################

###########################################################################
# Remplace la commande suivante :
# te:mode:make_menu templates

if {[winfo exists .menu.template]} {
   destroy .menu.templates
}
menubutton .menu.templates -text "Templates" -menu .menu.templates.m
menu .menu.templates.m
pack append .menu .menu.templates left

###########################################################################
# Remplissage de menu template
###########################################################################

.menu.templates.m add command -label "Insérer un separateur" \
                                -command {separator 1}
.menu.templates.m add command -label {separateur} \
                                -command {electricAlias-wrap =}
.menu.templates.m add command -label {separateur large} \
                                -command {electricAlias-wrap ==} 
                                
.menu.templates.m add separator                                                               
.menu.templates.m add command -label {module} \
                                -command {electricAlias-wrap md}
.menu.templates.m add command -label {function} \
                                -command {electricAlias-wrap fn}
.menu.templates.m add command -label {task} \
                                -command {electricAlias-wrap task}              

.menu.templates.m add separator
.menu.templates.m add command -label {initial} \
                                -command {electricAlias-wrap ini}
.menu.templates.m add command -label {always} \
                                -command {electricAlias-wrap al}
.menu.templates.m add command -label {forever} \
                                -command {electricAlias-wrap fv} 
                                                  
.menu.templates.m add separator
.menu.templates.m add command -label {for} \
                                -command {electricAlias-wrap for}
.menu.templates.m add command -label {while} \
                                -command {electricAlias-wrap wh}
.menu.templates.m add command -label {repeat} \
                                -command {electricAlias-wrap rp}                                
.menu.templates.m add command -label {case} \
                                -command {electricAlias-wrap case}
                                
.menu.templates.m add separator
.menu.templates.m add cascade -label Alias -menu .menu.templates.m.alias
menu .menu.templates.m.alias
foreach alias [electricAlias-softNames] {
   .menu.templates.m.alias add command -label $alias \
   -command "te:cmd:expand_alias [te:no_interp $alias]"
}
###########################################################################

 
################################################################################
# define the prefix for verilog mode

set EDITPREFS(prefix) "// "

######################################################################
# cleanup for verilog mode
######################################################################

proc stead:modeCleanup {} "
global MODE
te:mode:destroy_menu $MODE
te:mode:destroy_menu templates
te:cmd:destroy_alias_menu .menu.templates.m
"
