26

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
toolic
  • 57,801
  • 17
  • 75
  • 117
Frank Dejay
  • 689
  • 5
  • 13
  • 17

5 Answers5

36

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;
Nathan Farrington
  • 1,890
  • 1
  • 17
  • 27
6

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.

5

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

Morgan
  • 19,934
  • 8
  • 58
  • 84
5

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

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

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • 1
    That isn't what KC's paper says. He just says that reset lines can be expensive, and you should think about whether each part of the design actually needs to be reset, given that it might quickly clock out of an unknown state, or might not be implementable with a reset (an SRL16, for example). It is absolutely the wrong conclusion that you should avoid explicit reset lines. – EML Apr 03 '19 at 19:46
  • I updated my answer to use the word **global**. KC clearly says global reset lines are bad (expensive, time consuming and unnecessary), and that reset circuitry should only be used where necessary. – Mark Lakata Apr 10 '19 at 21:26
4

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
Paul S
  • 7,645
  • 2
  • 24
  • 36
  • 8
    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 Apr 04 '12 at 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](http://stackoverflow.com/questions/10033459/verilog-fpga-use-of-an-unitialized-register). 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. Thanks – Frank Dejay Apr 05 '12 at 18:01