0
\$\begingroup\$

I am learning Verilog fundamentals recently. I want to drive a signal always high using reg. I wrote this and it didn't work.

    reg b;
    always@* 
    begin
       b <= 1'b1; 
    end

b is always 'X' in simulation.

In VHDL, I was able to do similar thing:

signal b: std_logic;
process(all)
begin
b <= '1';
end process;

b is always high in simulation.

I know we can do this using assign statement with wire. But, is it not possible with reg ?

\$\endgroup\$
  • 1
    \$\begingroup\$ "always high" and "using registers" makes little sense. \$\endgroup\$ – Marcus Müller Aug 30 at 14:42
  • 1
    \$\begingroup\$ I am just trying to learn the difference between reg and wire types in Verilog. I learnt that 'reg' can be used to design combinational circuits like gates. So I think it does not always mean a register. It can be wires too in hardware. \$\endgroup\$ – Meenie Leis Aug 30 at 14:54
  • \$\begingroup\$ Just an observation... In VHDL your 'b <=' 1';' should be a concurrent statement and not in a process. \$\endgroup\$ – TonyM Aug 30 at 15:12
  • \$\begingroup\$ @TonyM I usually write such signals as concurrent statements. But this snippet also did the same job as the concurrent statement when I simulated. I used VHDL-2008. \$\endgroup\$ – Meenie Leis Aug 30 at 15:22
  • 2
    \$\begingroup\$ There's a distinction between VHDL and Verilog to.do with initialization that affects signal assignment. VHDL executes each process at least once. First during initialization then potentially during subsequent simulation (and that's not the case here, there are no signals being evaluated here, the sensitivity list derived from the reserved word all is an empty set). For the Verilog implementation you get initialization using an initial statement as in Dave Tweeds answer. (In VHDL you'd declare b as a constant instead.) \$\endgroup\$ – user8352 Aug 30 at 23:23
2
\$\begingroup\$

The always block works only if there's something to "trigger" it. In your example, there were no variables on the RHS of any statements, so @* effectively reduces to null.

Of course, initial would have worked just fine ... or always begin (without the @*).

And don't forget that you don't need to use begin/end with always or initial — if you have only a single statement, it's perfectly valid to write

reg b;
initial b <= 1;

or

always @(posedge clock) data_pipe <= data_in;

From your comment:

I am just trying to learn the difference between reg and wire types in Verilog. I learnt that 'reg' can be used to design combinational circuits like gates. So I think it does not always mean a register. It can be wires too in hardware.

No, reg does NOT mean "register". It's a very confusing part of Verilog syntax.

Basically, reg variables can be assigned to in process blocks (initial or always statements), while wire variables are assigned to outside such blocks (assign statements, module port connections, etc.)

\$\endgroup\$
2
\$\begingroup\$

In Verilog, you can write

reg b = 1;

And then never make another assignment to it. But that's not a good coding strategy. SystemVerilog simplified the rules and does allow a single assign statement to a variable. You should read this article.

\$\endgroup\$
  • \$\begingroup\$ Hm, so are you saying that we cannot do the same in always block ? \$\endgroup\$ – Meenie Leis Aug 30 at 15:24
  • 1
    \$\begingroup\$ You can, but only if there's something to "trigger" the always block. In your example, there were no variables on the RHS of any statements, so @* effectively reduces to null. Of course, initial would have worked just fine ... or always begin (without the @*). \$\endgroup\$ – Dave Tweed Aug 30 at 15:41
  • 1
    \$\begingroup\$ Again, SystemVerilog solves this by replacing always @* with always_comb which guarantees to trigger at least once at time 0. \$\endgroup\$ – dave_59 Aug 30 at 15:46
  • \$\begingroup\$ Again, the OP is not asking about SystemVerilog, just plain Verilog. \$\endgroup\$ – Dave Tweed Aug 30 at 15:49
  • 1
    \$\begingroup\$ @DaveTweed, I realize that. It helps to explain the problems in Verilog that have been solved by SystemVerilog. And many people still don't realize after 15 years that it's an option available to them. \$\endgroup\$ – dave_59 Aug 30 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.