AMCC
Member level 2
VHDL and State Machine and if..then... with no else... and other code Styling
Hi All,
This is a post about GOOD/BAD coding style or even ACCEPTABLE code style. I hope to have (with YOUR contribution) some guidelines for avoiding some particular problems... that seems to be more common than desired.
My thanks in advance.
I have recently received a task of improving some features in an existing VHDL design.
After some looks to the code, which has a QUITE different style of my own ... I was raised with some question.
I will just post one process (a hell of big process... Sorry.. I was about to clip-it when I realized that you might miss some "features".)
So... what I submit to you the code and some styling code choices made... and submit to your consideration. Please feel free to comment on SHOULD and SHOULDN'T throughout this code. I express my personal opinion...
I highly appreciate your feedback. Thank you very much for your (LONG) reading of this .
Best Regards
AMCC
P.S... AND NOW THE CODE:
Hi All,
This is a post about GOOD/BAD coding style or even ACCEPTABLE code style. I hope to have (with YOUR contribution) some guidelines for avoiding some particular problems... that seems to be more common than desired.
My thanks in advance.
I have recently received a task of improving some features in an existing VHDL design.
After some looks to the code, which has a QUITE different style of my own ... I was raised with some question.
I will just post one process (a hell of big process... Sorry.. I was about to clip-it when I realized that you might miss some "features".)
So... what I submit to you the code and some styling code choices made... and submit to your consideration. Please feel free to comment on SHOULD and SHOULDN'T throughout this code. I express my personal opinion...
- Single Process State Machine: This is a Moore machine, which I prefer to use two or three processes. Mixing everything seems (to me) a very big mess. Your comments? (Would you go to two or three processes: state-nextstate; state transition; output combinatorial).
- Synchronous State machine and Initialization: Seems fine to me. Initialization is made on 'Reset'. Any negative consideration?
- (BIG DOUBT!!) 'Initialization/Default' on 'first' "else": What is you opinion regarding these assignments? (ex. Rd_EnReg <= Rd_En; on line 14). This will be interpreted by the synthesizer as a 'default' value. Your thoughts?
- (ANOTHER BIG DOUBT!!: 'Rd_AckReg' is assigned in TWO different 'if' statements (lines 25, 30)... STILL PRIOR to state machine description. For me this is a 'bomb', either for code analysis... or even for synthesis. What do you think?
- if...then... end if: What is you opinion regarding the use of if..then statements WITHOUT any 'else' statement? This is something that I tend to avoid (due to implicit memory of VHDL). Do you find it an acceptable coding? Do you have argument either in favor or against this?
- I have found a good coding style guide (**broken link removed**)... that include some issues above... but not all. In any case you view is very welcome.
I highly appreciate your feedback. Thank you very much for your (LONG) reading of this .
Best Regards
AMCC
P.S... AND NOW THE CODE:
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 process (Reset, clk) begin if clk'event and clk = '1' then if Reset = '1' then State <= IDLE; RAM_EnReg <= '0'; RAM_WrReg <= '0'; RdReg <= '0'; Rd_EnReg <= '0'; Rd_AckReg <= '0'; else RAM_EnReg <= '0'; RAM_WrReg <= '0'; RAM_Wr_Last <= '0'; Rd_EnReg <= Rd_En; if Rd_EnReg = '1' and RdReg = '0' then RdReg <= '1'; -- will disactivate after completing this read operation Rd_AddrReg((Rd_Addr'high - 3) downto 0) <= Rd_Addr(Rd_Addr'high downto 3); -- converting to 64b wise addressing Rd_SizeReg <= (others => '0'); if Rd_Size(2 downto 0) = 0 then Rd_SizeReg((Rd_Size'high - 3) downto 0) <= Rd_Size(Rd_Size'high downto 3); else Rd_SizeReg((Rd_Size'high - 2) downto 0) <= ('0' & Rd_Size(Rd_Size'high downto 3)) + 1; end if; Rd_AckReg <= '1'; end if; if Rd_EnReg = '0' then Rd_AckReg <= '0'; end if; RAM_DataIn <= FIFO_In_Data; case State is when IDLE => if RAM_Rdy = '1' then if RAM_Wr_Rdy = '1' and FIFO_In_AlmostEmpty = '0' and FIFO_In_Addr(FIFO_In_Addr'high) = '1' then -- BL8 write State <= WRITING_BL8; RAM_Cmd <= "000"; -- write command RAM_EnReg <= '1'; RAM_Addr <= FIFO_In_Addr(RAM_Addr'high downto 0); RAM_WrReg <= '1'; elsif RAM_Wr_Rdy = '1' and FIFO_In_Empty = '0' and FIFO_In_Addr(FIFO_In_Addr'high) = '0' then -- BL4/BC4 write RAM_Cmd <= "000"; -- write command RAM_EnReg <= '1'; RAM_Addr <= FIFO_In_Addr(RAM_Addr'high downto 0); RAM_WrReg <= '1'; RAM_Wr_Last <= '1'; elsif RdReg = '1' and Rd_AckReg = '0' and FIFO_Out_ProgFull = '0' then -- BL8 read RAM_Cmd <= "001"; -- read command RAM_EnReg <= '1'; RAM_Addr <= Rd_AddrReg(RAM_Addr'high downto 0); if Rd_SizeReg >= 8 then Rd_AddrReg <= Rd_AddrReg + 8; -- BL8 read Rd_SizeReg <= Rd_SizeReg - 8; else Rd_SizeReg <= (others => '0'); RdReg <= '0'; end if; end if; end if; when WRITING_BL8 => if RAM_Wr_Rdy = '1' then State <= IDLE; RAM_WrReg <= '1'; RAM_Wr_Last <= '1'; end if; when others => State <= IDLE; end case; end if; end if; end process;
Last edited: