The synthesizable SystemVerilog subset accepted by the SVlog online simulator. One module per file; only the first module is parsed. See also the Guide.
module counter #(parameter W = 8) ( // optional parameters
input logic clk, rst,
input logic en,
output logic [W-1:0] q
);
logic [W-1:0] next; // net / variable declarations
assign next = q + 1'b1; // continuous assignment
always_ff @(posedge clk)
if (rst) q <= '0;
else if (en) q <= next;
endmodule
Ports use the ANSI list: input / output, optional logic, wire or int, optional signed and [msb:lsb]. Comments: // line and /* block */ (not nested).
logic [7:0] a, b; // unsigned vector
logic signed [7:0] s; // signed vector
wire [3:0] w;
int i; // signed 32 bits (also integer)
logic [3:0][7:0] pk; // packed array: 4 bytes = 32 bits
logic [7:0] mem [0:255]; // unpacked array (memory)
logic [7:0] regs [8]; // same as [0:7]
Values are 2-state: '0 and '1 fill to the context width; x / z literals are not supported.
| Form | Meaning |
|---|---|
8'hA5 4'b1010 3'd5 | sized, unsigned |
8'sd5 4'sb1111 | sized, signed |
42 | plain decimal: signed 32 bits |
'0 '1 | all bits 0 / 1 |
1_000 | _ separators allowed |
assign y = a | b;
assign x[3:0] = y[7:4];
assign {cout, sum} = a + b; // concatenation target
Concatenation operands are self-determined; unsized constants inside {} are errors. assign targets need constant indexes.
always_comb begin
y = a; // default avoids latches
if (sel == 2'd1) y = b;
else if (sel == 2'd2) y = a + b;
end
= onlyx++ x-- += -= *= &= |= ^= <<= >>=sum = sum + a[i] worksA signal that is not assigned on every path (an if without else, a case without default that does not cover every value) gives a latch warning. So do bits left unassigned, as in y[0] = a; for a 4-bit y. Reading a signal before the block assigns it (y = y + a;, a temporary used before it is set) gives a read before assignment warning.
Each bit should be assigned in one block only (assign, always_comb or always_ff): the same bits written in two blocks give a multiple driver warning.
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) q <= '0;
else q <= d;
<= onlyor / ,) are asynchronous resets[unique|priority] case (sel) // also casez, casex
2'd0, 2'd1: y = a;
2'd2: y = b;
default: y = '0;
endcase
casez the digits ? / z are don't care (4'b1???); casex adds xunique / priority are accepted but not checkedtypedef enum logic [1:0] {IDLE, RUN, DONE = 2'd3} state_t;
state_t state, next;
enum logic {OFF, ON} mode; // anonymous
logic/bit [..] or int (default)module m #(parameter W = 8, localparam M = W*2) (...);
parameter N = 4;
localparam [7:0] K = 8'hA5;
localparam int D = N * 2;
for loopsalways_comb begin
cnt = '0;
for (int i = 0; i < 8; i++)
cnt += a[i];
end
always_comb and always_ffi++ i-- ++i --i i += e i -= e i = e; at most 65 536 iterationsw[2] w[2][3:0] w[2:1] // packed: one select per dim
mem[addr] <= din; // variable index ok
logic [7:0] rom [0:3] = '{8'h11, 8'h22, 8'h33, 8'h44};
logic [15:0] t [0:9] = '{2: 200, 5: 500, default: 0};
logic [1:0] r [16] = '{2'b01, 2'b10, default: 2'b00};
mem[a:b] or second unpacked dimension$signed(x) / $unsigned(x) to convert/* stimulus 32
rst 1 0
en 1*12 0*3 1
d 0x9 0b0101 -3
*/
stimulus <cycles>, then one line per input: values with optional *count repeats; the last value lasts to the end. Decimal, 0x hex, 0b binary, negative decimals.
| Group | Operators | |
|---|---|---|
| 1 | Primary | a[i] a[7:4] {a, b} {N{a}} (e) |
| 2 | Unary | ~ ! -, reduction & ~& | ~| ^ ~^, $signed(), $unsigned() |
| 3 | Multiplicative | * / (% ** not supported) |
| 4 | Additive | + - |
| 5 | Shift | << >> <<< >>> |
| 6 | Relational | < <= > >= |
| 7 | Equality | == != |
| 8 | Bitwise AND | & |
| 9 | Bitwise XOR | ^ ~^ |
| 10 | Bitwise OR | | |
| 11 | Logical AND | && |
| 12 | Logical OR | || |
| 13 | Conditional | c ? a : b (right-associative) |
/ truncates toward zero; division by zero gives 0.
generate blocksassign delaysalways, initialwhile, repeat, forever% ** operatorsreg, tri, var, byte, shortint and other typesx/z values