How to Correct VHDL Code for Serial Input State Machine?

  • Thread starter Thread starter ellis91
  • Start date Start date
  • Tags Tags
    Machines State
Click For Summary

Discussion Overview

The discussion revolves around the development of VHDL code for a state machine designed to accept serial input in the form of bytes. The focus is on implementing a system that detects a special byte marking the beginning of data frames, manages error detection, and handles data reception based on user-defined parameters.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant outlines the requirements for the VHDL code, including the need to identify a special byte, manage error counts, and resume data reception based on the special byte's detection.
  • Another participant requests additional design documentation, such as flowcharts or graphs, to better understand the approach and assist in correcting the code.
  • A participant expresses a desire for feedback on their approach and seeks guidance on structuring the logic of the program, indicating uncertainty about the correctness of their current implementation.
  • Repeated emphasis on the necessity of using state machines to achieve the desired functionality is noted, along with the participant's acknowledgment of potential issues in their code.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the effectiveness of the proposed approach, and multiple viewpoints regarding the design and implementation remain. The discussion is characterized by requests for clarification and suggestions rather than definitive solutions.

Contextual Notes

Limitations include the lack of detailed design documentation and the potential for misunderstandings regarding the state machine's structure and logic. The code provided may contain unresolved issues that participants have yet to address.

ellis91
Messages
2
Reaction score
0
VHDL help in state machines!

I have to write a vhdl code to accept inputs serially in the form of bytes(1 byte at a time). These bytes are in a particular order i.e they are received in packets where each packet is a frame with a specific number of bytes. This number of bytes in a frame can be defined by the user and is fixed. The main purpose of my code is to:
1. Assign one special byte to mark the beginning of each frame.
2. Search for the special byte in order to start receving data.
3. If the special byte is not found at regular intervals i.e after the number of bytes that the user defined, an error is indicated. Variables to keep count of the errors and the number of special bytes are assigned.
4. If the special byte is found before or after the specific number of bytes, then besides indicating an error, the code should resume receiving data from that special byte.

I know that i have to go about it using state machines, but i don't exactly understnd how the structure of the program and states should be

This was my attempt at the program and i know it is wrong. how can i correct it to do what i want it to do:
Code:
-- [B][B][B]entity packet_format_error_detection is
port (

      data_receive, special_byte : in STD_LOGIC_VECTOR(0 to 7);
        no_of_bytes : in STD_LOGIC_VECTOR(0 to 7);
        clock, subclock : in STD_LOGIC
       
        );
           
end final_packet_format_error_detection;

architecture statemachine of packet_format_error_detection is
 
 type statetype is (IDLE, START_DETECTION, RECEIVE_DATA);
 signal state, next_state: statetype;
 signal no_error_bytes : integer range  0 to 1024;
 signal no_special_bytes : integer range 0 to 1024;
 signal byte_count : integer range 0 to 1024;
begin


operation: process(state, data_receive, special_byte, no_of_bytes, no_error_bytes, no_special_bytes )

begin
 
no_error_bytes <= 0;
no_special_bytes <= 0;
byte_count <= 0;

case state is

when IDLE => if(data_receive = special_byte) then
                 next_state <=    START_DETECTION;
                 else next_state <= IDLE;
                 end if;

when START_DETECTION => if( clock'event and clock = '1') then
                                    next_state <= RECEIVE_DATA;
                                else
                                    next_state <= START_DETECTION;
                                end if;
                               
when RECEIVE_DATA => if( subclock'event and subclock = '1') then
                                byte_count <= byte_count + 1;
                                 if(data_receive = special_byte) then
                                    if(byte_count = no_of_bytes) then
                                        no_special_bytes <= no_special_bytes + 1;
                                        byte_count <= 0;
                                        next_state <= IDLE;
                                    else
                                        no_special_bytes <= no_special_bytes + 1;
                                        no_error_bytes <= no_error_bytes + 1;
                                        byte_count <= 0;
                                        next_state <= RECEIVE_DATA;
                                    end if;
                                 else
                                    next_state <= RECEIVE_DATA;
                                 end if;
                            else
                                    next_state <= RECEIVE_DATA;
                            end if;
end case;
                end process;
end statemachine;
[/B][/B][/B]


I can explain it in even better detail if necessary. Please help me at the earliest.
 
Last edited by a moderator:
Physics news on Phys.org


Can you provide some documentation on the design you are using? Graphs, flowcharts, processes, etc.

It is very difficult to "correct" code when you don't know the design information.
 


the main reason i put up the code was to give you an idea as to how i was approachin it.. i want help in finding out if this approach is good or there are other better ways of achieving the result.. i would like to know how to frame the logic of this program.. thanks in advance..
 


ellis91 said:
I have to write a vhdl code to accept inputs serially in the form of bytes(1 byte at a time). These bytes are in a particular order i.e they are received in packets where each packet is a frame with a specific number of bytes. This number of bytes in a frame can be defined by the user and is fixed. The main purpose of my code is to:
1. Assign one special byte to mark the beginning of each frame.
2. Search for the special byte in order to start receving data.
3. If the special byte is not found at regular intervals i.e after the number of bytes that the user defined, an error is indicated. Variables to keep count of the errors and the number of special bytes are assigned.
4. If the special byte is found before or after the specific number of bytes, then besides indicating an error, the code should resume receiving data from that special byte.

I know that i have to go about it using state machines, but i don't exactly understnd how the structure of the program and states should be

This was my attempt at the program and i know it is wrong. how can i correct it to do what i want it to do:
Code:
-- [B][B][B]entity packet_format_error_detection is
port (

      data_receive, special_byte : in STD_LOGIC_VECTOR(0 to 7);
        no_of_bytes : in STD_LOGIC_VECTOR(0 to 7);
        clock, subclock : in STD_LOGIC
       
        );
           
end final_packet_format_error_detection;

architecture statemachine of packet_format_error_detection is
 
 type statetype is (IDLE, START_DETECTION, RECEIVE_DATA);
 signal state, next_state: statetype;
 signal no_error_bytes : integer range  0 to 1024;
 signal no_special_bytes : integer range 0 to 1024;
 signal byte_count : integer range 0 to 1024;
begin


operation: process(state, data_receive, special_byte, no_of_bytes, no_error_bytes, no_special_bytes )

begin
 
no_error_bytes <= 0;
no_special_bytes <= 0;
byte_count <= 0;

case state is

when IDLE => if(data_receive = special_byte) then
                 next_state <=    START_DETECTION;
                 else next_state <= IDLE;
                 end if;

when START_DETECTION => if( clock'event and clock = '1') then
                                    next_state <= RECEIVE_DATA;
                                else
                                    next_state <= START_DETECTION;
                                end if;
                               
when RECEIVE_DATA => if( subclock'event and subclock = '1') then
                                byte_count <= byte_count + 1;
                                 if(data_receive = special_byte) then
                                    if(byte_count = no_of_bytes) then
                                        no_special_bytes <= no_special_bytes + 1;
                                        byte_count <= 0;
                                        next_state <= IDLE;
                                    else
                                        no_special_bytes <= no_special_bytes + 1;
                                        no_error_bytes <= no_error_bytes + 1;
                                        byte_count <= 0;
                                        next_state <= RECEIVE_DATA;
                                    end if;
                                 else
                                    next_state <= RECEIVE_DATA;
                                 end if;
                            else
                                    next_state <= RECEIVE_DATA;
                            end if;
end case;
                end process;
end statemachine;
[/B][/B][/B]


I can explain it in even better detail if necessary. Please help me at the earliest.

ellis91 said:
the main reason i put up the code was to give you an idea as to how i was approachin it.. i want help in finding out if this approach is good or there are other better ways of achieving the result.. i would like to know how to frame the logic of this program.. thanks in advance..

Welcome to the PF.

I agree with KingNothing -- please at least post your State Diagram to help make it easier to go through your code. Thanks.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 6 ·
Replies
6
Views
3K