Is there any way to highlight a line of code in Confluence code block? - confluence

Using Confluence and using the code block macro and the engineer would like to highlight a line of code. The tools up top are grayed out once I am inside the macro. How can I apply a yellow highlight or bold to text when inside the code block macro?

Sadly, you can't (AFAIK). However, you could try adding comments inside the code. For example, if the code is
int myVar = 5;
myVar = myVar * myVar;
and your engineer would like to highlight myVar = myVar * myVar;, you can edit the macro to look like this:
int myVar = 5;
//
// !IMPORTANT
//
myVar = myVar * myVar;
or, more elegantly
int myVar = 5;
myVar = myVar * myVar; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
However, the comment symbols might change from programming language to programming language, so consult with the engineer what style would be appropriate.

Related

replace `define with let construct

I'm trying to avoid using `define pre-processor and start using "let" since it's a language construct.
Here is my example:
`define MY_REGISTER_RANGE 15:0
logic [`MY_REGISTER_RANGE] my_array;
How can I do the same with let construct? My code is very simple but it assumes that I'm `include the file where my macro MY_REGISTER_RANGE is defined (in another file).
Thanks.
You can only substitute certain kinds of expressions using let. You can't just define a range.
You should use a typedef instead of let
typedef logic [15:0] MY_REGISTER_t;
MY_REGISTER_t my_array;
In this particular case you do not need the let construct. Moreover, the construct has nothing to do with declarations of variables.
You can start using parameters instead of text macros. For example
parameter MY_REGISTER_WIDTH = 16;
...
logic [MY_REGISTER_WIDTH-1:0] my_array;
The let construct defines a simple function containing a single expression. In this respect it could be used to replace certain types of macros, e.g.
`define MULT(A,B) A * B
...
R = `MULT(O1,O2);
could be replaced with
let MULT(A,B) = A * B;
...
R = MULT(O1,O2);

1-line try/catch equivalent in MATLAB

I have a situation in MATLAB where I want to try to assign a struct field into a new variable, like this:
swimming = fish.carp;
BUT the field carp may or may not be defined. Is there a way to specify a default value in case carp is not a valid field? For example, in Perl I would write
my $swimming = $fish{carp} or my $swimming = 0;
where 0 is the default value and or specifies the action to be performed if the assignment fails. Seems like something similar should exist in MATLAB, but I can't seem to find any documentation of it. For the sake of code readability I'd rather not use an if statement or a try/catch block, if I can help it.
You can make your own function to handle this and keep the code rather clear. Something like:
swimming = get_struct(fish, 'carp', 0);
with
function v = get_struct(s, f, d)
if isfield(s, f)
v = s.(f); % Struct value
else
v = d; % Default value
end
Best,
From what I know, you can't do it in one line in MATLAB. MATLAB logical constructs require explicit if/else statements and can't do it in one line... like in Perl or Python.
What you can do is check to see if the fish structure contains the carp field. If it isn't, then you can set the default value to be 0.
Use isfield to help you do that. Therefore:
if isfield(fish, 'carp')
swimming = fish.carp;
else
swimming = 0;
end
Also, as what Ratbert said, you can put it into one line with commas... but again, you still need that if/else construct:
if isfield(fish,'carp'), swimming = fish.carp; else, swimming = 0;
Another possible workaround is to declare a custom function yourself that takes in a structure and a field, and allow it to return the value at the field, or 0.
function [out] = get_field(S, field)
if isfield(S, field)
out = S.(field);
else
out = 0;
end
Then, you can do this:
swimming = get_field(fish, 'carp');
swimming will either by 0, or fish.carp. This way, it doesn't sacrifice code readability, but you'll need to create a custom function to do what you want.
If you don't like to define a custom function in a separate function file - which is certainly a good option - you can define two anonymous functions at the beginning of your script instead.
helper = {#(s,f) 0, #(s,f) s.(f)}
getfieldOrDefault = #(s,f) helper{ isfield(s,f) + 1 }(s,f)
With the definition
fish.carp = 42
and the function calls
a = getfieldOrDefault(fish,'carp')
b = getfieldOrDefault(fish,'codfish')
you get for the first one
a = 42
and the previous defined default value for the second case
b = 0

How to convert a string to a variable name?

I would like to have a construct like below to declare variable names based on a string queue. The below doesn't compile. So I would like to know if a similar approach is possible in Systemverilog.
Below is a simplified version of what I want to actually implement.
`define declare_int(NAME) int ``NAME`` = 1;
string str_q[$] = {"a", "b", "c"};
foreach (str_q[i]) begin
`declare_int(str_q[i])
end
NOTE: I am aware that `declare_int(a) will translate to int a = 1;. But as shown in the example above, I need a foreach loop to call this macro multiple times and so the input of the macro has to be some datatype, like a string in this case. The purpose is to auto-declare stuff as the queue changes with time.
In other words, how can I define the `declare_int macro so that `declare_int("a") translates to int a = 1;?
As Verilog is not interpreted but compiled in simulation, I doubt theres any way to dynamically declare variables at runtime. However, there are work arounds that achieve a similar effect.
I think the closest thing you could get is an associative array with the keys as your names (a, b, c) and your values for the values. For example, instead of your code, you'd have:
int vars[string];
string str_q[$] = {"a", "b", "c"};
foreach (str_q[i]) begin
vars[str_q[i]] = 1;
end
...
// To use the variable, just do:
vars["a"];
For more on associative arrays: http://www.asic-world.com/systemverilog/data_types13.html

SystemVerilog case statement does not work

Anyone know why this case statement doesn't work:
int width;
width = 8;
case (width === 16)
1'b0: begin
// correct code
end
1'b1: begin
// we end up here
end
endcase
I am using VCS. I tried running this with DVE debugger, and the code worked correctly when running with the debugger. Also, this code is nested within another case statement, not shown here.
I can't directly answer you question, but I think synopsys will not make that stupid mistake. If they did, please let me know.
In some language, the return value '0' means true, I am not sure it is the same here.
But to avoid this issue, I think the code can be changed to other ways:
One way:
case (width)
16 : begin
// correct code
end
default : begin
// Other code
end
endcase
Or:
if (width === 16) begin
// correct code
end

CoffeeScript variable scope

Is there any way to declare a variable at "file" scope (which will be closured by CS), without initializing it? A contrived example:
init = ->
counter = 0
inc = ->
counter += 1
This won't work, because you need to declare "counter". Adding "counter = 0" to the top would make it work, but the "= 0" is unnecessary. (A more realistic example would involve something that accesses the DOM on page load - there's no way to correctly initialize it in "file" scope.)
You'll have to define it on the outer scope, as you mentioned.
counter = null
init = ->
counter = 0
inc = ->
counter += 1
If your functions where part of an object you could use #counter, like this:
obj =
init: ->
#counter = 0
inc: ->
#counter += 1
You can say `var counter;` with backticks and that is passed literally through to the generated javascript.
When you have a problem like this, look at the generated javascript. It will be extremely clear that the variable scope is lexically limited to the function.
Looking at the generated javascript is often a good way to understand what the behavior of coffeescript constructs is.