Assign a synthesizable initial value to a reg in Verilog

Go To StackoverFlow.com

15

I'm an FPGA noob trying to learn Verilog. How can I "assign" a value to a reg in an always block, either as an initial value, or as a constant. I'm trying to do something like this in the code below. I get an error because the 8 bit constant doesn't count as input. I also don't want to trigger the always off of a clock. I just want to assign a register to a specific value. As I want it to be synthesisable I can't use an initial block. Thanks a lot.

module top
(
    input wire clk,
    output wire [7:0] led   
 );


reg [7:0] data_reg ; 
always @*
begin
    data_reg = 8'b10101011;
end

assign led = data_reg;

endmodule
2012-04-04 05:30
by Frank Dejay


20

You can combine the register declaration with initialization.

reg [7:0] data_reg = 8'b10101011;

Or you can use an initial block

reg [7:0] data_reg;
initial data_reg = 8'b10101011;
2012-04-04 17:48
by Nathan Farrington
It works for me with Xilinx XST - Nathan Farrington 2012-04-04 19:30
@Tim Also works in Quartus synthesizing for Altera's Cyclone II - Roman Starkov 2013-04-28 00:39
@NathanFarrington is it possible to initialize the reg with a variable (say a input parameter of the module). I tried doing it directly as above but it doesn't work. - ishan3243 2013-11-14 19:59
@anonymous It is possible to initialize a reg with a parameter, but not a regular input - alex.forencich 2013-11-16 02:07
Will this synthesize - augurar 2014-02-19 21:51
It depends on your synthisis tool vendor, version and settings. Traditionally synthisis tools ignored initial conditions but some now support them - plugwash 2015-11-18 16:04


6

The other answers are all good. For Xilinx FPGA designs, it is best not to use explicit reset lines, and use initial blocks for reset conditions. Here is the white paper from Ken Chapman (Xilinx FPGA guru)

http://japan.xilinx.com/support/documentation/white_papers/wp272.pdf

2014-03-18 17:32
by Mark Lakata


4

The always @* would never trigger as no Right hand arguments change. Why not use a wire with assign?

module top (
    input wire clk,
    output wire [7:0] led   
);

wire [7:0] data_reg ; 
assign data_reg   = 8'b10101011;
assign led        = data_reg;

endmodule

If you actually want a flop where you can change the value, the default would be in the reset clause.

module top
(
    input        clk,
    input        rst_n,
    input  [7:0] data,
    output [7:0] led   
 );

reg [7:0] data_reg ; 
always @(posedge clk or negedge rst_n) begin
  if (!rst_n)
    data_reg <= 8'b10101011;
  else
    data_reg <= data ; 
end

assign led = data_reg;

endmodule

Hope this helps

2012-04-04 07:05
by Morgan


4

You should use what your FPGA documentation recommends. There is no portable way to initialize register values other than using a reset net. This has a hardware cost associated with it on most synthesis targets.

2012-04-04 20:47
by NoName


3

When a chip gets power all of it's registers contain random values. It's not possible to have an an initial value. It will always be random.

This is why we have reset signals, to reset registers to a known value. The reset is controlled by something off chip, and we write our code to use it.

always @(posedge clk) begin
    if (reset == 1) begin // For an active high reset
        data_reg = 8'b10101011;
    end else begin
        data_reg = next_data_reg;
    end
end
2012-04-04 07:08
by Paul S
What you say is true for ASICs in general but not for FPGAs specifically. When you download the bitfile, all memory cells are initialized. Resets are often not needed or helpful for FPGA-based designs, and lead to larger area and possibly lower Fmax. The best reset is downloading the bitfile again - Nathan Farrington 2012-04-04 17:44
@NathanFarrington Thanks. I've got a similar issue I'm trying to understand. I have a state register for a FSM that doesn't seem to initialize unless I assign it to drive an output wire. I opened another post link. The behavior seems to be related to what we're discussing here. Maybe you could check it out and tell me what you think. It's Verilog coded targeting Xilinx FPGA. Thank - Frank Dejay 2012-04-05 18:01
Ads