# Can we declare output as “inout” to design a flip flop in VHDL?

I want to design a JK flip flop in VHDL. In this output depends on previous state. One method to implement this condition is by declaring a state as a signal inside the architecture.

Another method is by declaring outputs as "inout". I simulated JK flip flop using "inout" and it worked without any error. In real hardware Q and Q_bar are outputs, so I doubt declaring that as "inout" is a technical mistake.

Which method is correct one .?

Either will work, as you discovered. However, I have run into enough "glitches" with various simulation and synthesis tools when using inout ports that I now avoid them whenever possible. So I would say that the first method is preferable.

You have a number of choices here:

• To use an inout-mode port, and read the value directly
• To use a buffer-mode port, and read the value directly
• To use an out-mode port, and read the value directly (VHDL-2008 only)
• To use an out-mode port, and read the value using the 'driving_value attribute
• To use an internal signal and copy its value to the output
• To model the internal state as a variable and copy its value to the output

The general advice is to use an internal signal, mostly because this is a highly portable solution. But I also like the methods of using the 'driving_value attribute, or reading directly from the out port (VHDL-2008 only). Keeping the state in a variable also looks nice because it prevents declaring unnecessary signals just to make the compiler happy; it is also as portable as using an internal signals, and variables are more efficient than signals from a simulation point of view.

Note that if you use an inout port, other entities connected to the port (and which have nothing to do with your FF model) could potentially change the value read inside your model. On the other hand, if you use buffer mode, you may have connectivity problems in older revisions of VHDL, which specify that buffer-mode ports can only be connected to other buffer-mode ports or internal signals (i.e., you cannot connect it directly to an out-mode port when you instantiate your FF).

• Helpful... And about using variable? – tollin jose Jul 25 '14 at 4:26
• @tollin, thanks for pointing it out, added some notes about using a variable. – rick Jul 25 '14 at 4:39
• thanks for your time and consideration ... This one really helped me. – tollin jose Jul 25 '14 at 4:45
• No problem! If you find it useful, please consider marking the answer as accepted. – rick Jul 25 '14 at 4:47