安德鲁

[原创].基于SyntaxHighlighter的Verilog HDL高亮组件.[Verilog]

0
阅读(2248)

引子

一直在用PreCode SnppnetLiver Writer中处理代码高亮;用起来蛮方便的。但是没有我喜欢的Verilog HDL的高亮。今天我在loydsen的启发下,决定写个基于SyntaxHighlighter的Verilog HDL组件,其实很简单。目前还在整理期间,一些关键字和函数还没有加入,还请各位长辈、行家多提意见和建议。

源代码

shVerilogEnhanced.js

01 /*
02  * Name: SyntaxHighlighter.brushes.Verilog 
03  * Author: Yuphone Chang
04  * Email: yuphone@qq.com/
05  * Create Date: 5.17, 2010
06  */
07  
08 SyntaxHighlighter.brushes.Verilog = function()
09 {
10   var datatypes = 'reg integar unsigned ' +
11                   'wire tri wand triand tri0 tri1 supply0 supply1 trireg ' +
12                   'parameter specparam defparam event ';
13  
14   var primitives = 'and nand or nor xor xnor ' +
15                    'buf not ' +
16                    'bufif0 bufif1 notif0 notif1 '
17                    'pullup pulldown ' +
18                    'pmos rpmos nmos rnmos ';
19  
20   var keywords  = 'module endmodule ' +
21                   'input output inout ' +
22                   'begin end ' +
23                   'assign deassign always initial genvar ' +
24                   'forever repeat disable wait ' +
25                   'function endfunction' +
26                   'task endtask ' +
27                   'generate endgenerate ' +
28                   'specify endspecify ' +
29                   'posedge negedge ' +
30                   'if else for while ' +
31                   'case casex casez endcase default ' +
32                   'include timescale ' +
33                   'ifdef endif ' +
34                   'celldefine endcelldefine ' +
35                   'attribute '
36                   'fork join ';
37                    
38   var functions = 'display displayb displayo displayh ' +
39                   'write writeb writeo writeh ' +
40                   'strobe strobeb strobeh strobeo ' +
41                   'monitor monitorb monitoro monitorh ' +
42                   'fopen fclose ' +
43                   'readmemb readmemh ' +
44                   'finish stop ' +
45                   'time stime realtime timeformat ' +
46                   'printtimescale ' +
47                   'setup hold setuphold skew recovery period width ';
48  
49   this.regexList = [
50     // one line comments
51     { regex: SyntaxHighlighter.regexLib.singleLineCComments,css: 'comments' },
52     // multiline comments
53     { regex: SyntaxHighlighter.regexLib.multiLineCComments, css: 'comments' },
54     // double quoted strings
55     { regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' },
56     // single quoted strings
57     { regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'string' },
58     // constants
59     { regex: new RegExp("[0-9]+['][bBoOdDhHeEfFtT][0-9a-fA-FzZxX_]+", 'g'), css: 'constants' },
60     // datatypes
61     { regex: new RegExp(this.getKeywords(datatypes), 'gm'), css: 'color1 bold' },
62     // primitives 
63     { regex: new RegExp(this.getKeywords(primitives), 'gm'), css: 'color2 bold' },
64     // keywords
65     { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword bold' },
66     // functions
67     { regex: new RegExp(this.getKeywords(functions), 'gm'), css: 'functions bold' }
68     ];
69 };
70  
71 SyntaxHighlighter.brushes.Verilog.prototype = new SyntaxHighlighter.Highlighter();
72 SyntaxHighlighter.brushes.Verilog.aliases   = ['verilog', 'v'];

 

如何使用

1. 在博客园——管理——设置——页首Html代码中添加以下代码。

01 <!--SyntaxHighlighter的核心脚步-->
02 <script type="text/javascript" src="http://files.cnblogs.com/yuphone/shCore.js"></script>
03 <!--自定义的SyntaxHighlighter的Verilog高亮-->
04 <script type="text/javascript" src="http://files.cnblogs.com/yuphone/shVerilogEnhanced.js"></script>
05 <!--SyntaxHighlighter的核心样式表-->
06 <link type="text/css" rel="stylesheet" href="http://files.cnblogs.com/yuphone/shCore.css">
07 <!--SyntaxHighlighter的默认主题样式表-->
08 <link type="text/css" rel="stylesheet" href="http://files.cnblogs.com/yuphone/shThemeDefault.css">
09 <!--打开SyntaxHighlighter高亮-->
10 <script type="text/javascript">
11   SyntaxHighlighter.all();
12 </script>

注:此处的的shCore.js、shCore.css和shThemeDefault.css为必需组件,shVerilogEnhanced.js是我自定义Verilog高亮组件。

2. 在Liver Writer或博客园的在线编辑器上,切换到HTML模式,加上pre标签,即可实现Verilog高亮。

1 <pre class="brush:v;">
2 module water_led(
3   input        CLOCK_50,                // 板载时钟50MHz
4   input        Q_KEY,                   // 板载按键RST
5   output [8:1] LED                      // LED[1] ~ LED[8]
6 );    
7 </pre>

 

测试代码

water_led.v

01 module water_led(
02   input        CLOCK_50,                // 板载时钟50MHz
03   input        Q_KEY,                   // 板载按键RST
04   output [8:1] LED                      // LED[1] ~ LED[8]
05 );    
06  
07 //++++++++++++++++++++++++++++++++++++++
08 // 分频部分 开始
09 //++++++++++++++++++++++++++++++++++++++
10 reg [23:0] cnt;                         // 计数子
11  
12 // 溢出后自动重新计数 
13 always @ (posedge CLOCK_50, negedge Q_KEY)
14   if (!Q_KEY)
15     cnt <= 0;
16   else
17     cnt <= cnt + 1'b1;
18    
19 wire led_clk = cnt[23];                 // 每(2^24/50M = 0.3355)sec取一次
20 //--------------------------------------
21 // 分频部分 结束
22 //--------------------------------------
23  
24  
25 //++++++++++++++++++++++++++++++++++++++
26 // 流水灯部分 开始
27 //++++++++++++++++++++++++++++++++++++++
28 reg [8:1] led_r;                        // 定义输出寄存器
29 reg       dir;                          // 循环方向控制
30  
31 always @ (posedge led_clk, negedge Q_KEY)
32   if (!Q_KEY)                           // 复位后右移
33     dir <= 0;                        
34   else
35     // 到达左右端点否?到达则换向
36     if(led_r == 8'h7F && dir == 0)
37       dir <= 1;
38     else if(led_r == 8'h01 && dir == 1)
39       dir <= 0;
40  
41 always @ (posedge led_clk, negedge Q_KEY)
42   if (!Q_KEY)                           // 复位后右移               
43     led_r <= 8'h01;          
44   else
45     // 根据dir,左右移位
46     // 注意:LED实际移位方向与led_r移位方向相反
47     // 因为开发板上LED[1]在左,LED[8]在右
48     if(!dir) 
49       led_r <= (led_r << 1) + 1'b1;     // LED右移;加法比移位的运算优先级高
50     else
51       led_r <= (led_r >> 1);            // LED左移
52  
53 // 为什么要取反?
54 // 因为开发板上的LED是送0亮,送1灭
55 assign LED = ~led_r;                    // 寄存器输出
56 //--------------------------------------
57 // 流水灯部分 结束
58 //--------------------------------------
59  
60 endmodule

 

参考

1 Lloyd Sheng Blog.玩转博客园的5个小技巧

2 SyntaxHighlighter.Developing a custom brush