Skip to content

Commit aeaccd5

Browse files
terriblefiremshr-h
authored andcommitted
Adding verilator support (#32)
* Adding verilator support * updated the readme for verilator updated the package.json for verilator removed notices about errors in submodules. * Update README.md
1 parent c189e3d commit aeaccd5

File tree

4 files changed

+141
-4
lines changed

4 files changed

+141
-4
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Verilog HDL support based on [https://github.com/textmate/verilog.tmbundle](http
2121
* `iverilog`
2222
* `xvlog`
2323
* `modelsim`
24+
* `verilator`
2425
* `none`
2526
* Icarus Verilog (`iverilog`)
2627
- Make sure the path to your Icaurus Verilog installation is present in the `PATH` variable.
@@ -34,7 +35,10 @@ Verilog HDL support based on [https://github.com/textmate/verilog.tmbundle](http
3435
- The "work" library of Modelsim should be present in the workspace directory.
3536
* If not already present, create the work library in the workspace directory by running `vlib work`.
3637
- Use `verilog.linting.modelsim.arguments` setting to add custom arguments to the linter.
37-
38+
* Verilator (`verilator`)
39+
- Make sure the path to "verilator" executable file is present in the `PATH` variable.
40+
- Use `verilog.linting.verilator.arguments` setting to add custom arguments to the linter. The argument `--lint-only -I<document folder>` will be added by the linter automatically
41+
- Use `verilog.linting.verilator.runAtFileLocation` setting to run Verilator at the file location. By default, it will be run at workspace directory, requiring that `` `include`` directives contain file paths relative to the workspace directory.
3842
### In progress
3943
- Icarus Verilog integration
4044
* Working in:
@@ -53,6 +57,13 @@ Verilog HDL support based on [https://github.com/textmate/verilog.tmbundle](http
5357
- Windows: Yes
5458
- Linux: Not Tested
5559
- macOS: Not Tested
60+
- Verilator integration
61+
* Working in:
62+
- Windows: Not Tested
63+
* There are no available windows binaries to test with.
64+
- Linux: Yes
65+
* Tested on Debian 9. Visual Studio Code 1.26.1
66+
- macOS: Not Tested
5667

5768
### In the future
5869
- Please post issue if you have any new idea

package.json

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@
6363
"enum": [
6464
"xvlog",
6565
"iverilog",
66+
"verilator",
6667
"modelsim",
6768
"none"
6869
],
6970
"default": "none",
70-
"description": "Select the verilog linter. Possible values are 'iverilog', 'xvlog' or 'none'"
71+
"description": "Select the verilog linter. Possible values are 'iverilog', 'verilator', xvlog' or 'none'"
7172
},
7273
"verilog.linting.iverilog.arguments": {
7374
"scope": "window",
@@ -86,6 +87,18 @@
8687
"type": "string",
8788
"default": "",
8889
"description": "Add Modelsim arguments here. They will be added to Modelsim while linting."
90+
},
91+
"verilog.linting.verilator.arguments": {
92+
"scope": "window",
93+
"type": "string",
94+
"default": "",
95+
"description": "Add Verilator arguments here (like macros). They will be added to Verilator while linting (The command \"--lint-only -I<document folder>\" will be added by the linter by default)"
96+
},
97+
"verilog.linting.verilator.runAtFileLocation": {
98+
"scope": "window",
99+
"type": "boolean",
100+
"default": false,
101+
"description": "If enabled, Verilator will be ran at the file location for linting. Else it will be run at workspace folder. Disabled by default."
89102
}
90103
}
91104
}
@@ -97,11 +110,15 @@
97110
"update-vscode": "node ./node_modules/vscode/bin/install",
98111
"postinstall": "node ./node_modules/vscode/bin/install"
99112
},
100-
"dependencies": {},
113+
"dependencies": {
114+
"code-generator": "^1.0.7",
115+
"vsce": "^1.49.2",
116+
"yo": "^2.0.5"
117+
},
101118
"devDependencies": {
102119
"@types/mocha": "^2.2.45",
103120
"@types/node": "^7.0.51",
104-
"typescript": "^2.8.1",
121+
"typescript": "^2.9.2",
105122
"vscode": "^1.1.14"
106123
}
107124
}

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {workspace, window, commands, Disposable, Range, ExtensionContext,
55
languages, extensions, Selection, Uri} from "vscode";
66
import BaseLinter from "./linter/BaseLinter";
77
import IcarusLinter from "./linter/IcarusLinter";
8+
import VerilatorLinter from "./linter/VerilatorLinter";
89
import XvlogLinter from "./linter/XvlogLinter";
910
import ModelsimLinter from "./linter/ModelsimLinter";
1011

@@ -66,6 +67,9 @@ function configLinter() {
6667
case "modelsim":
6768
linter = new ModelsimLinter();
6869
break;
70+
case "verilator":
71+
linter = new VerilatorLinter();
72+
break;
6973
default:
7074
console.log("Invalid linter name.")
7175
linter = null;

src/linter/VerilatorLinter.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {workspace, window, Disposable, Range, TextDocument, Diagnostic, DiagnosticSeverity, DiagnosticCollection, languages} from "vscode";
2+
import * as child from 'child_process';
3+
import BaseLinter from "./BaseLinter";
4+
import { join } from "path";
5+
6+
var isWindows = process.platform === "win32";
7+
8+
export default class VerilatorLinter extends BaseLinter {
9+
private verilatorArgs: string;
10+
private runAtFileLocation: boolean;
11+
12+
constructor() {
13+
super("verilator");
14+
15+
workspace.onDidChangeConfiguration(() => {
16+
this.getConfig();
17+
})
18+
this.getConfig();
19+
}
20+
21+
private getConfig() {
22+
this.verilatorArgs = <string>workspace.getConfiguration().get('verilog.linting.verilator.arguments', '');
23+
this.runAtFileLocation = <boolean>workspace.getConfiguration().get('verilog.linting.verilator.runAtFileLocation')
24+
}
25+
26+
protected splitTerms(line: string){
27+
let terms = line.split(':');
28+
29+
for (var i = 0; i < terms.length; i++) {
30+
if (terms[i] == ' ') {
31+
terms.splice(i, 1);
32+
i--;
33+
}
34+
else
35+
{
36+
terms[i] = terms[i].trim();
37+
}
38+
}
39+
40+
return terms;
41+
}
42+
43+
protected getSeverity(severityString: string){
44+
let result = DiagnosticSeverity.Information;
45+
46+
if (severityString.startsWith('Error'))
47+
{
48+
result = DiagnosticSeverity.Error;
49+
}
50+
else if (severityString.startsWith('Warning'))
51+
{
52+
result = DiagnosticSeverity.Warning;
53+
}
54+
55+
return result;
56+
}
57+
58+
protected lint(doc: TextDocument) {
59+
let docUri: string = doc.uri.fsPath //path of current doc
60+
let lastIndex:number = (isWindows == true)? docUri.lastIndexOf("\\") : docUri.lastIndexOf("/");
61+
let docFolder = docUri.substr(0, lastIndex); //folder of current doc
62+
let runLocation: string = (this.runAtFileLocation == true)? docFolder : workspace.rootPath; //choose correct location to run
63+
let command: string = 'verilator --lint-only -I'+docFolder+ ' ' + this.verilatorArgs + ' \"' + doc.fileName +'\"'; //command to execute
64+
var foo: child.ChildProcess = child.exec(command,{cwd:runLocation},(error:Error, stdout:string, stderr:string) => {
65+
let diagnostics: Diagnostic[] = [];
66+
let lines = stderr.split(/\r?\n/g);
67+
68+
// Parse output lines
69+
lines.forEach((line, i) => {
70+
71+
if(line.startsWith('%')){
72+
// remove the %
73+
line = line.substr(1)
74+
75+
// was it for a submodule
76+
if (line.search(doc.fileName) > 0)
77+
{
78+
// remove the filename
79+
line = line.replace(doc.fileName, '');
80+
line = line.replace(/\s+/g,' ').trim();
81+
82+
let terms = this.splitTerms(line);
83+
let severity = this.getSeverity(terms[0]);
84+
let message = terms.slice(2).join(' ')
85+
let lineNum = parseInt(terms[1].trim()) - 1;
86+
87+
if (lineNum != NaN)
88+
{
89+
console.log(terms[1].trim() + ' ' + message);
90+
91+
diagnostics.push({
92+
severity: severity,
93+
range:new Range(lineNum, 0, lineNum, Number.MAX_VALUE),
94+
message: message,
95+
code: 'verilator',
96+
source: 'verilator'
97+
});
98+
}
99+
}
100+
}
101+
})
102+
this.diagnostic_collection.set(doc.uri, diagnostics)
103+
})
104+
}
105+
}

0 commit comments

Comments
 (0)