FPGA logo large

Syntax Highlighting Revisited

If you have been following my blog, you may recall from the Verilog Introduction post that I was looking for a plugin that supported syntax highlighting. I intend to write a lot more Verilog code, and if all goes well, this code will become more involved. So I really would like to utilize syntax highlighting to make reading this blog a little easier.

Syntax Highlighting Plugins

WordPress is great, and the plugin space is vast. It turns out there is a popular syntax highlighting plugin called SyntaxHighlighter Evolved (SH-E). Unfortunately, while that plugin supports a great many programming languages, it does not support Verilog. That’s unfortunate. So I spent time searching for other plugins that might support Verilog with no luck so far.

Like many WordPress plugins, SH-E is open-source and available through github. So I decided to briefly take a closer look at it to see what would be required to add Verilog support. Interestingly, I found that there is an extension model in the plugin that allows you to extend the languages supported. You do this by creating and registering your own “brush.” Brushes are themselves WordPress plugins.

Creating A Plugin And A Brush

Writing a Verilog brush was not difficult. A brush is a small Javascript file. The trickiest part was probably locating the list of Verilog keywords, functions and data types. Since Verilog is influenced by C/C++ I started from the C++ brush. I then updated the code to reflect the specifics of Verilog. Then I had to package my new Verilog brush inside a WordPress plugin. That also was not difficult.

A basic WordPress plugin involves a little bit of PHP code to define the plugin, register it with WordPress, and in the case of a SH-E brush to register it with SH-E. I made a few mistakes. And the available documentation was out of date on some aspects. But after peeking at a few other brushes I was able to fix the issues.

I completed these steps and tested against a Staging server. After a number of iterations it was working to my satisfaction. At that point, I installed and activated the plugin on the live site for this blog.

Comparing The Results

So what does the new Verilog syntax highlighting look like? And more importantly was it worth the effort? I will let you be the judge! Below I’ve copied the code from the D-Flip Flop project.

// declare our D-FF module
module dff(
    input clk,
    input d,
    output reg q
    );
    
    // on rising clock sample d and output on q
    always @(posedge clk)
        q <= d;
        
endmodule

And below is the same code, only with syntax highlighting enabled.

// declare our D-FF module
module dff(
    input clk,
    input d,
    output reg q
    );
    
    // on rising clock sample d and output on q
    always @(posedge clk)
        q <= d;
        
endmodule

I don’t know about you, but I find the second version to be easier on the eyes. And finally, here is a larger example taken from the configurable counter project.

module counter_n
    #(BITS = 4)
    (
    input clk,
    input rst,
    output tick,
    output [BITS - 1 : 0] q
    );
    
    // counter register
    reg [BITS - 1 : 0] rCounter;
    
    // increment or reset the counter
    always @(posedge clk, posedge rst)
        if (rst)
            rCounter <= 0;
        else
            rCounter <= rCounter + 1;

    // connect counter register to the output wires
    assign q = rCounter;

    // set the tick output
    assign tick = (rCounter == 2 ** BITS - 1) ? 1'b1 : 1'b0;
        
endmodule

Wrapping It Up

There are still some bugs of course. For example, look at the sized numbers above. The brush thinks everything between the single quotes is a string literal. I will find and fix those issues as I discover them. But on the whole I find this a significant improvement. And I will now go back and update previous posts to use the new capability. Sometimes in software you have to make the thing that you need. In this case that wasn’t as difficult as I’d originally feared.

The source for my Verilog brush is available here. If there is interest, I may see what it takes to formally publish the plugin on WordPress.org. But that will have to be a project for another time!

That’s all for now. If you have feedback, questions or suggestions, please leave me a comment!

Discover more from FPGA Coding

Subscribe now to keep reading and get access to the full archive.

Continue reading