0
\$\begingroup\$

This is probably an absolute newbie question but what happens to "unchanged" output register as in the following example(pseudocode):

module TOP(
   input CLK,
   input A,
   input B,
   output reg O1,
   output reg O2
);

reg state = 0;

always @(posedge CLK) begin
   case(state)
      0: begin   
          O1 <= A;
          state <= 1;
      end
      1: begin   
          O2 <= B;
          state <= 0;
      end
   endcase
endmodule

Do O1 and O2 keep the same value as for the last pass where they were specifically set or are they undefined in the pass where they are not specifically set?

Thank you very much in advance!

\$\endgroup\$
  • 1
    \$\begingroup\$ Previous values are retained if not otherwise set on any given path through the always block. \$\endgroup\$ – Dave Tweed Jul 3 at 15:53
1
\$\begingroup\$

Once you assign a register variable* with = or <=, it retains its value until the next time it is assigned.

* Verilog "register variables" do not always represent actual registers in synthesized hardware.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

When working with a hardware description language you must always think of the hardware implementation to better understand the expected result.

It is a good idea to check some available tools especially for simple descriptions like this one. This lets you mastering the HDL like Verilog.

For instance, the Netlist Viewer can show you the hardware implementation for your description. In your case it is the following:

enter image description here

This shows that at the arrival of the clock, depending on the "state" resulting of the previous clock, B or A are assigned. And "state" is complemented and this complemented value will be used in the next clock for assigning A or B. the outputs of the latches are of course maintained between clocks.

| improve this answer | |
\$\endgroup\$

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.