Skip to content

Commit 1cf88ed

Browse files
committed
doc: clarifying comment behavior in framework files, #268
1 parent e50213e commit 1cf88ed

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

learn-silice/Documentation.md

+52-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
This is the Silice main documentation.
55

6-
When designing with Silice your code describes circuits. If not done already, I recommended to [watch the introductory video](https://www.youtube.com/watch?v=_OhxEY72qxI) (youtube) to get more familiar with this notion and what it entails. The video is slightly outdated in terms of what Silice can do, but still useful when getting started.
6+
When designing with Silice your code describes circuits. If not done already, I recommended to [watch the introductory video](https://www.youtube.com/watch?v=_OhxEY72qxI) (youtube) to get more familiar with this notion and what it entails. The video is slightly outdated in terms of what Silice can do, but still useful when getting started. Another more recent video that focuses on graphics applications and showcases new capabilities is [here](https://www.youtube.com/watch?v=XycwTFPDZ6w).
77

88
## Table of content
99

@@ -2420,6 +2420,57 @@ For practical usage examples please refer to the [*Getting started* guide](../Ge
24202420
24212421
New frameworks for additional boards [can be easily created](../frameworks/boards/README.md).
24222422
2423+
## Lua pre-processor in frameworks
2424+
2425+
Frameworks can 'escape' Verilog to add [Lua preprocessor](#lua-preprocessor) instructions.
2426+
Beware that this happens before the Verilog file is parsed, so the Verilog syntax
2427+
does not influence these pre-processor lines. This is particularly the case
2428+
for comments.
2429+
2430+
For instance, consider the following framework file:
2431+
2432+
``` verilog
2433+
// This is a Verilog framework 'glue' file
2434+
2435+
// Next some Verilog defines, these are only seen by the FPGA/ASIC/simulation
2436+
// toolchain in the final compiled file.
2437+
`define ICEBREAKER 1
2438+
`define ICE40 1
2439+
`default_nettype none
2440+
// Next some lines escaping to the Lua preprocessor, these lines are visible
2441+
// to Silice at compile time, but are oblivious to the surrounding Verilog code.
2442+
$$ICEBREAKER = 1
2443+
$$ICE40 = 1
2444+
$$HARDWARE = 1
2445+
// For instance consider the following
2446+
/*
2447+
$$VAR1 = 1
2448+
*/
2449+
// The comment around the $$ escaped line is invisible to Silice, so VAR1 is
2450+
// indeed defined in Silice!
2451+
// This is also true of something like this:
2452+
`ifdef NOT_DEFINED
2453+
$$VAR2 = 1
2454+
`endif
2455+
// The variable VAR2 will be defined as seen by the preprocessor, even though
2456+
// NOT_DEFINED is not defined in the Verilog part. Again the Lua preprocessor
2457+
// executes before Verilog is parsed by the toolchain.
2458+
2459+
// Here are several possible ways to resolve this:
2460+
// 1) Use a Lua comment
2461+
$$ -- VAR3 = 3
2462+
// ^^ this is a comment in the preprocessor language (Lua), the line is ignored
2463+
// 2) Use preprocessor conditions to disable multiple lines
2464+
$$ if false then
2465+
$$ VAR4 = 4
2466+
$$ VAR5 = 5
2467+
$$ end
2468+
// ^^ here the definition of VAR3 is skipped
2469+
// 3) Use a single line comment, this is properly taken into account by the
2470+
// framework parser, for convenience.
2471+
// $$ VAR6 = 6
2472+
```
2473+
24232474
## VGA and OLED simulation
24242475
24252476
The Silice verilator framework supports VGA and OLED display out of the box. For instance see the [VGA demos project](../projects/vga_demo/README.md).

src/SiliceCompiler.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ void SiliceCompiler::gatherUnitBody(AutoPtr<Algorithm> unit, antlr4::tree::Parse
306306

307307
// -------------------------------------------------
308308

309+
std::string ltrim(const std::string s)
310+
{
311+
std::string r = s;
312+
r.erase(r.begin(),
313+
std::find_if(r.begin(), r.end(), [](unsigned char c) { return !std::isspace(c); }
314+
));
315+
return r;
316+
}
317+
309318
void SiliceCompiler::prepareFramework(std::string fframework, std::string& _lpp, std::string& _verilog)
310319
{
311320
// if we don't have a framework (as for the formal board),
@@ -322,6 +331,7 @@ void SiliceCompiler::prepareFramework(std::string fframework, std::string& _lpp,
322331
}
323332
std::string line;
324333
while (std::getline(infile, line)) {
334+
line = ltrim(line);
325335
if (line.substr(0, 2) == "$$") {
326336
_lpp += line.substr(2) + "\n";
327337
} else {

tests/comments1.si

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
unit main(output uint8 leds)
3+
{
4+
5+
// This is a comment
6+
/*
7+
This is a comment
8+
*/
9+
10+
$$ VAR1 = 1 -- this is a comment in preprocessor escape (Lua language)
11+
12+
// $$ VAR2 = 2
13+
// ^^ ignored as commented in Silice source
14+
15+
/*
16+
$$ VAR3 = 3
17+
^^ ignored as commented in Silice source
18+
*/
19+
20+
$$ -- VAR4 = 4 => ignored as commented in preprocessor (leading --)
21+
22+
$$ print('VAR1 = ' .. VAR1)
23+
$$ if VAR2 then print('VAR2 defined') else print('VAR2 NOT defined') end
24+
$$ if VAR3 then print('VAR3 defined') else print('VAR3 NOT defined') end
25+
$$ if VAR4 then print('VAR4 defined') else print('VAR4 NOT defined') end
26+
27+
always {
28+
__finish();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)