System Ver I Log 101
System Ver I Log 101
To view the schematic of your design, create a *.ys file, then run:
$ yosys file.ys
$ netlistsvg -o file.svg file.json
An *.svg will be in the folder, you could open it using your browser.
$ brave file.svg
modules
Vectors
Signals can be declared as vectors using [N:0] notation.
For example:
●
an 8-bit vector → logic [7:0]
●
an N-bit vector → logic [N-1:0]
Access the ith bit with [i], or a bit slice from jth to ith with [i:j]
modules
Module instantiations
Reusing other modules in a “big” module is frequent.
→ See the example to know how to use this.
modules
→ Naming
Declarations lower_snake_case
Instance names lower_snake_case
Signals (nets or ports) lower_snake_case
Binary → b: 0, 1
Decimal → d: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Hexadecimal → h: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
A literal may contain x or z.
A literal could be signed or unsigned, using “s”
literals
Bitwise operators
assign c[0] = a[0] | b[0]; → assign c = a | b;
assign c[1] = a[1] | b[1];
Reduction operators
assign b = a[0] & a[1] & a[2]; → assign b = &a;
Logical operators
Only produce true or false (1 bit) → Should use for scalar values/signals (true/false)
and → &&, or → ||, not → !
continuous assignments
Shift operators
left logical → <<, right logical → >>, right arithmetic → >>>
Arithmetic operators
add → +, subtract → -,
multiply → *, divide → /, modulus → %, power → **
Conditional operator
mux mux (a_i, b_i, sel_i, c_o) → assign c_o = sel_i ? b_i : a_i;
condition ? result_if_true : result_if_false
continuous assignments
Comparison operators
equal → ==, less than → <, less than or equal → <=
not equal → !=, greater than → >, greater than or equal → >=
module module_name #(
parameter UpperCamelCase = ...,
parameter AnotherUpperCamelCase = ...
) (
...
);
...
endmodule : module_name
Guideline
→ Naming
Tunable Constants (parameter) UpperCamelCase
True Constants (localparam) ALL_CAPS
modules
generate
genvar [index_variables];
for ([initial]; [condition]; [inc/dec]) begin [: optional_label]
[...concurrent_statements…]
end
endgenerate
logic [N-1:0] a;
always_comb begin : proc_example
a = ‘0; // default value
if (sel)
a = b + c;
end
always_comb – blocking assignments
The value of the expression is evaluated and assigned to the variable/signal immediately
→ Next procedural statements will use that variable → Blocking
[variable_name] = [expression]; // blocking assignment
if ([boolean]) begin
[...procedural_statements...]
end else begin
[...procedural_statements...]
end
if ([boolean]) begin
[...procedural_statements...]
end
Remember always assign default values to signals/variables, especially when just one “if” is used.
always_comb – case statements
case [expression]
[item]: begin
[procedural_statements]
end
[item]: begin
[procedural_statements]
end
...
default: begin
[procedural_statements]
end
endcase
Remember always assign default values to signals/variables, All cases should be considered.
Modeling Sequential Circuits
always
The sensitivity list uses the posedge or negedge keyword in front of a signal to specify the edge
transition of that signal.
The wildcard * means all signals in all expressions of procedural statements are in the sensitivity
list. → Look like another way to model combinational logic → but should not use
always – non-blocking assignments
The value of the expression is evaluated and assigned to the variable/signal at the end of always
block.
→ Previous procedural statements will not block the following ones.
→ Not assign a value at least twice
→ Use non-blocking assignments to model sequential logic
initial x = 0; initial x = 0;
always @(posedge clk) begin always @(posedge clk) begin
x = 2; y = x + 1;
y = x + 1; x = 2;
end end
→ x = 2, y = 3 → x = 2, y = 1
always_ff
always_ff notices the compiler the intention of synthesizing flip-flopsuses a sensitivity list to
perform value changes.
→ Use this instead of plain always
Mealy machine → The output depends on the state and the input.
enum – enumerations
→ Suffix
enum _e
modules
Packed Array
● Of single bit data types (logic), enumerated types, and even packed arrays
logic [7:0][3:0] array0;
state_e [2:0] state_array;
logic [3:0][7:0][3:0] packed_array;
● One dimensional packed array → vector → convenient to access a bit or bit slice
array0[2]; array0[3][5:3]; array0[1][1];
Unpacked Array
● Of any data types
logic uarray0[7:0][3:0];
state_e state_uarray[5:0];
logic [3:0][7:0] mix[3:0];
A module requires logic operator data and 2 operands. It could be configured as 1 data:
typedef struct packed{
logic [2:0] logic_operator;
logic [7:0] operand_a;
logic [7:0] operand_b;
} logic_data_s;
logic_data_s data_in;
→ Suffix
signal clusters _s
other typedef _t
package and *.svh files
When working on large projects, a number of parameters or types (enum, struct,…) will
become global.
→ Painful if declaring them again and again in each module file
→ Modify one → Modify all
package [package_name];
[declarations];
endpackage [: package_name]
1. https://github.com/lowRISC/style-guides/blob/master/VerilogCodingStyle.
md
2. https://zyedidia.github.io/notes/sv_guide.pdf
3. http://courses.eees.dei.unibo.it/LABMPHSENG/wp-content/uploads/2016/
02/SystemVerilog_3.1a.pdf