Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

One segment vs two segment State Machine style

Hello to everybody,

I am trying to decrease usage of elements in my project for Cyclone IV in Quartus II 13.0. I found the way of improvement synchronous sequential circuits in the book (rtl hardware design using vhdl by pong p. chu) (https://www.dropbox.com/s/x5on6xoytyrl8e0/rtl%20hardware%20design%20using%20vhdl.pdf) on pages 213-250. Author proposed to create two segment logic, where next-state and output logic are a combinational circuit and memory elements is only for saving current state. It helps to divide combinational logic from memory and it is better to perform optimization and to fine-tune circuit by author`s opinion.

When I used examples from this book and rewrote them by two-segments style, I haven`t recognized any differents in logic usage. And when I tried to adopt my project to this style, I found that logic usage is more than 2 times than one segment style.

I tried to test the concept in a simple circuit design, but one-segmented coded style seems to be better in that case too. Example files of two styles are in attachments.

Has anybody there been successful with two-segment logic in projects?

Thanks for advices.

16 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I just posted a similar question about this earlier on. The thing with one-hot state machines is that you'll need a register for each state. So if you have large state-machines (say, 32 states) that would, as a binary state, only require 5 bits. Yet if it's encoded as one-hot, it will require a bit for each state, effectively turning it into 32 registers.

    So if you can achieve the same result with multiple, smaller state-machines that utilize the LUT's better, you could potentially save logic. The more inputs your states require, the bigger your register usage gets as well.

    Last but not least, it's a lot easier to maintain a (few) smaller state-machines than one gigantic monster.

    -Mux
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Altera choses one hot by default as it has better timing performance. IIRC, it will chose numerical (ie a count sequence) for when Nstates > 32 or 64 (cant remember which). You can chose either of these encoding methods, or grey-code, via attributes in your VHDL. You can even specify what code to give to specific states if you really want, again via attributes so you can keep your state type without using std_logic_vector.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    I just posted a similar question about this earlier on. The thing with one-hot state machines is that you'll need a register for each state. So if you have large state-machines (say, 32 states) that would, as a binary state, only require 5 bits. Yet if it's encoded as one-hot, it will require a bit for each state, effectively turning it into 32 registers.

    So if you can achieve the same result with multiple, smaller state-machines that utilize the LUT's better, you could potentially save logic. The more inputs your states require, the bigger your register usage gets as well.

    Last but not least, it's a lot easier to maintain a (few) smaller state-machines than one gigantic monster.

    -Mux

    --- Quote End ---

    To add a little more information to this, I created a single statemachine with less than 8 states for an EPM7128. While the LE count is 2 higher on the segmented statemachine ( selection mux for output ), it uses 10 fewer registers ( 19-vs-29 ) and 16 fewer p-terms (72-vs-88).

    Compiling the same code for a Cyclone, you get somewhat similar results with the seperated statemachine taking up more resources ( 37-vs-34 LE's and 32 -vs- 23 registers).
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    To add a little more information to this, I created a single statemachine with less than 8 states for an EPM7128. While the LE count is 2 higher on the segmented statemachine ( selection mux for output ), it uses 10 fewer registers ( 19-vs-29 ) and 16 fewer p-terms (72-vs-88).

    Compiling the same code for a Cyclone, you get somewhat similar results with the seperated statemachine taking up more resources ( 37-vs-34 LE's and 32 -vs- 23 registers).

    --- Quote End ---

    I have been for years and still asking myself if a design passes timing why should we worry about state machine encoding.

    state machines is an old and generic methodology of design across various areas of engineering(including mechanical for example) and my understanding is that HDL does not really support it in the true sense. It instead implements the idea in registers to keep track of circuit "memory". Thus a counter can be used equally and if I trust a counter will not go wrong then why should I worry about states extra encoding.

    Just a thought. Moreover, the number of extra registers may not matter much unless you have lots of state machines.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The actual implementation matters depending on what you're after. In my case, it's saving as much space as possible. Looking at the RTL design the separate state-machine seems to bury most of the combinatorial logic inside the LE's, which is kinda what you want whereas the single statemachine has a boatload of combinatorial crap hanging off on the right of it. Now I don't know if that's an accurate visualization but it seems to make sense.

    Anything that has lots of combinatorial logic (i.e. large state-machine) will potentially waste registers that can't be used, which means you're not utilizing your resources the fullest extent. It also means that you should get faster performance when you 'front-load' combinatorial logic which is, for all intents and purposes, free.

    Of course, in this day and age this doesn't really matter that much but when you're working with an FPGA with 6144 bits or RAM and only 576 LE's, it does :-)

    -Mux
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    It seems to me that the thread is departing from the initially gained insight that functionally equivalent logic will result in almost identical resource usage. It are the functional differences that matter, e.g. registering or not registering output signals. It has been sufficiently clarified that user requirements are different in this regard. Properties that are considered as advantage by one are unwanted by others. In so far I'm not sure if additional analyses of single versus dual process state machines will bring essentially new results.

    State encoding is a special point. For most FPGA FSM designs, the default one hot encoding is just fine. Besides timing, it's also the decoding effort for binary and similar compact encodings that must be counted. My personal favourite for all critical state machines is "safe" encoding, which uses also one state hot representation.

    Splitting FSM in partial machines may be reasonable in some cases, but "form follows function". I won't do it to save a few LEs.

    I started programmable logic design more than 30 years ago with small GALs and know that tiny CPLD design has other constraints. A CPLD FSM don't use the one state hot default, and it might be necessary to taylor a design down to the function of each register. Thus I won't refer the discussion primarly to CPLD design.