Vlog Mode for Emacs

Brief Overview of Features

- Has lots of automatic templates that insert code for you. For example, if you type "case ", it will ask you for the name of the signal to switch on, insert the case statement with begin and endcase, and then ask for the particular cases.
- Most of the templates are bound to control keys. For example, C-c C-b causes vlog-mode to insert a begin-end pair at the current point.
- Colorization of text is the same as Verilog-mode (with a couple of minor additions).
- Vlog-mode treats variable names that include an underscore as a single word. This allows operations like forward-word to work correctly.
- Using M-/, (Escape then /), causes variable name completion. This way you can have long descriptive signal names without having to type as much.
- High level functions such as "update module port list from input/output declarations" and "instantiate module", which automatically do some of the tedious processes of connecting modules.
- Tabbing is different than Verilog mode.

Enabling Vlog-mode

If you use Bill Mann's standard .emacs file, and you have the latest copy, then Vlog-mode will be enabled automatically. Otherwise, you should add this code to your .emacs file to enable Vlog-mode.

Features in Detail

Tabs

The Tab key operates differently in Vlog mode than in Verilog mode.

  • If the cursor is at the beginning, Tab will tab over to line up with the start of code in the previous line.
  • If the cursor is on the first character of the line of code, and this position is the same as or greater than the start of the previous line, insert a tab of vlog-indent-level length.
  • If the cursor is in the middle of a line of text or at the end of a line of text, the Tab key will move the line to line up with the previous line.

    Auto-Inserting Code

    Typing many common keywords will cause a template to execute that will assist in your code writing. The user will be prompted for variable names at certain times. All these templates can be individually modified in your personal .emacs file, or globally disabled. I give examples of both under Customization.

    Useful key bindings and functions

    C-c C-b  Insert begin/end pair with blank line in between
    C-c C-a  Insert always block.  It prompts for sensitivity list
    C-c C-f  (vlog-find-file) If point rests on a verilog module name, 
             that module_name.v will be opened from the current directory.
    C-c i    (vlog-if-else-begin) Insert an if-else block
    C-c m    (vlog-insert-module-instance) Insert module instantiation.
               Prompts for filename of module.
    C-c e    (vlog-enable-reg) Insert a inferred flip-flop with enable
    C-c r    (vlog-std-reg) Insert an inferred flip-flop
    C-c ]    (end-of-vlog-module)
    C-c [    (beginning-of-vlog-module)
    ,,       (stutter-comma) Typing two commas replaces the commas with " <= "
    

    Quickly declaring wires and registers

    I've made a couple of short cuts for declaring regs and wires, while you're coding. These functions take the name of the reg/wire that your cursor is on top of, asks for the bus width of the signal, and puts a reg or wire declaration at the top of your code. You don't see this happen; your view of the code is unchanged. It makes it easy to declare these as your are writing or adding code.

    The regs/wires declarations are all positioned underneath a text string declared in the variable vlog-local-regs-wires-str. The default value is the string "//### local regs/wires".

    C-c g    (vlog-declare-reg) Declares a reg for the signal that the
              cursor is on.
    C-c w	 (vlog-declare-wire) Declares a wire for the signal that the
              cursor is on.
    

    Functions for Structural Level Connectivity

  • vlog-insert-module-instance This function reads a file and instantiates a port-by-name instance. It also creates wire declarations for each port signal. These wire declarations can then be moved to the top of the module.
  • vlog-update-module-ports This function will update the port list of the current module from the input, output, and inout declarations.
  • vlog-update-module-instance Point must be somewhere in the module to be updated. If the module is in the same dir as the current buffer's file, the module instance will be updated, ie. deleted and re-instantiated.

    Restrictions on I/O declarations: The above three functions operate best when only 1 signal name used per input, output or inout declaration.

    Modifying Automatic Instance Names

    The default module instance name used in vlog-insert-module-instance is umodule_name . After instantiation, the cursor is left next to the instance name, for manual modification, if necessary. I have also added some controls to the automatic instance name generation.
  • vlog-use-u Set to nil to disable adding the "u" to the instance name. Default is true.
  • vlog-shorten-instance-names Setting this 't will enable the use of vlog-shorten-inst-len. Default is false.
  • vlog-shorten-inst-len Specifies the number of characters to delete from the beginning of the module_name when creating an instance. Eg. If the module is oboe_flit_fwd.v, vlog-shorten-instance-names is true, vlog-use-u is true, and vlog-shorten-inst-len is set to 5, then the module instance name will be uflit_fwd.

    To set any of these variables, use the following example. Note that true is 't in emacs-lisp and false is nil.
    (add-hook 'vlog-mode-hook (function (lambda () (setq vlog-shorten-instance-name 't))))

    Customization

    Examples of things to add to your .emacs file to customize Vlog-mode:

  • Set number of spaces for indent to, say, 3
    (add-hook 'vlog-mode-hook (function (lambda () (setq mode-indent-level 3))))

  • Turn off those darn auto-inserting templates. I want to type more!
    (add-hook 'vlog-mode-hook (function (lambda () (abbrev-mode 0))))

  • Turn on auto-fill-mode:
    (add-hook 'vlog-mode-hook 'turn-on-auto-fill)

  • Bind vlog-update-module-ports to the F4 key:
    (add-hook 'vlog-mode-hook (function (lambda () (local-set-key [f4] 'vlog-update-module-ports))))

  • Bind vlog-update-module-instance to the F8 key:
    (add-hook 'vlog-mode-hook (function (lambda () (local-set-key [f8] 'vlog-update-module-instance))))

  • Redefine a template, in this case vlog-enable-reg to use clk_50_sys
    (add-hook 'vlog-mode-hook
    	(function
    	   (lambda ()
    		 (put 'vlog-enable-reg nil
    			  '("Reg name: "
    				"always @ (posedge clk_50_sys or negedge reset_l) begin" \n
    				> "if (!reset_l) begin" \n
    				> str " <= 0;" \n
    				< "end" \n
    				"else begin" \n
    				> "if () begin" \n
    				> str " <= ;" \n
    				< "end" \n
    				< "end" \n
    				< "end//always "))
    		 )))
    

    Known Bugs (and workarounds)

  • If C-style comment (/* ... */) is the very first character in the file, keyword expansion will occur within that comment. Workaround: Don't do that! If necessary, start first line of file with "//" comments.