I expect to share many more Xilinx constraint files in the future. And that being the case, it seems to make sense to take the time to create a plugin for Xilinx Constraint files (XDC). Given what I learned through creation of the Verilog syntax highlighting WordPress plugin I have a strong foundation to build on. And since I did this twice, I decided to share what it required.
Building on prior examples
Starting from the Verilog plugin I removed elements that do not exist in constraint files. In this case, I remove the data types, functions and multiline comment definitions. And then I replace the Verilog keywords with the handful of keywords for XDC files. The resulting code is short enough that I can show it all. Below is the entirety of the custom brush JavaScript code.
(function()
{
// CommonJS
SyntaxHighlighter = SyntaxHighlighter || (typeof require !== 'undefined'? require('shCore').SyntaxHighlighter : null);
function Brush()
{
var keywords = 'set_property get_ports create_clock name current_design';
this.regexList = [
{ regex: /##.*$/gm, css: 'comments' }, // one line comments
{ regex: /(R|L|U|u|u8)?"([^\\"\n]|\\.)*"/g, css: 'string' }, // special character
{ regex: /(R|L|U|u|u8)?'([^\\'\n]|\\.)*'/g, css: 'string' }, // special string
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // strings
{ regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword bold' }
];
};
Brush.prototype = new SyntaxHighlighter.Highlighter();
Brush.aliases = ['xdc', 'XDC'];
SyntaxHighlighter.brushes.XDC = Brush;
// CommonJS
typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();
That is not too difficult. But that makes sense because an XDC file is not complex. Frankly, the hardest part was remembering enough about regular expression syntax to construct the expression for XDC file comments on line 12. Looking closely at line 12 one thing that you may notice is that the built-in syntax highlighter brush gets confused high. I will have to file a bug for that.
Below is the entirety of the PHP code that defines and registers the brush as a WordPress plugin. Much of the PHP code is simple boilerplate.
<?php
/*
Plugin Name: SyntaxHighlighter Evolved: XDC Brush
Description: Adds support for the Xilinx Design Constraint Files to the SyntaxHighlighter Evolved plugin.
Author: Mark Seminatore
Version: 1.0.0
Author URI: https://fpgacoding.com
*/
// SyntaxHighlighter Evolved doesn't do anything until early in the "init" hook, so best to wait until after that
add_action( 'init', 'syntaxhighlighter_xdc_regscript' );
// Tell SyntaxHighlighter Evolved about this new language/brush
add_filter( 'syntaxhighlighter_brushes', 'syntaxhighlighter_xdc_addlang' );
add_filter( 'syntaxhighlighter_brush_names', 'syntaxhighlighter_xdc_addname' );
// Register the brush file with WordPress
function syntaxhighlighter_xdc_regscript() {
wp_register_script( 'syntaxhighlighter-brush-xdc', plugins_url( 'shBrushConstraint.js', __FILE__ ), array('syntaxhighlighter-core'), '1.0.0', true );
}
// Filter SyntaxHighlighter Evolved's language array
function syntaxhighlighter_xdc_addlang( $brushes ) {
$brushes['xdc'] = 'xdc';
return $brushes;
}
// Filter SyntaxHighlighter Evolved's name array
function syntaxhighlighter_xdc_addname( $names ) {
$names['xdc'] = 'xdc';
return $names;
}
?>
Comparing the results
To see how well the new plugin works, let’s compare the results and see! The first example is the example constraint file from earlier post on constraint files. This example uses the stock WordPress code block.
## This file is a general .xdc for the Basys3 rev B board
## To use it in a project:
## - uncomment the lines corresponding to used pins
## - rename the used ports (in each line, after get_ports) according to the top level signal names in the project
## Clock signal
set_property PACKAGE_PIN W5 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports clk]
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk]
## LEDs
set_property PACKAGE_PIN U16 [get_ports {led}]
set_property IOSTANDARD LVCMOS33 [get_ports {led}]
And next is the version using the SyntaxHighlighter Evolved plugin with our custom XDC file brush plugin. Maybe it is just me but I find this much easier to read and comprehend. This is especially true as the size of the XDC file grows.
## This file is a general .xdc for the Basys3 rev B board
## To use it in a project:
## - uncomment the lines corresponding to used pins
## - rename the used ports (in each line, after get_ports) according to the top level signal names in the project
## Clock signal
set_property PACKAGE_PIN W5 [get_ports clk]
set_property IOSTANDARD LVCMOS33 [get_ports clk]
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk]
## LEDs
set_property PACKAGE_PIN U16 [get_ports {led}]
set_property IOSTANDARD LVCMOS33 [get_ports {led}]
That’s it for today. The source code for the XDC brush is available on github. If you have feedback, a question or a suggestion, please leave a comment!