Project 2, Part 3: The Transmitter and Receiver FSMs
The previous article implemented the baud rate generator. This article designs the transmitter and receiver finite state machines (FSMs). The transmitter converts parallel bytes into serial waveforms, while the receiver extracts parallel data from asynchronous serial inputs.
The Transmitter: PISO Shift Register FSM
The transmitter sequences through four states: TX_IDLE, TX_START, TX_DATA, and TX_STOP.
It incorporates a parallel-in, serial-out (PISO) shift register from Section 3.4:
typedef enum logic [1:0] {TX_IDLE, TX_START, TX_DATA, TX_STOP} tx_state_t;
tx_state_t tx_state, tx_next_state;
logic [7:0] tx_shift_reg;
logic [2:0] bit_count;
logic tx_line;
// State register: advances on baud rate enable pulses
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) tx_state <= TX_IDLE;
else if (baud_tick) tx_state <= tx_next_state;
end
// Next-state combinational logic
always_comb begin
tx_next_state = tx_state;
unique case (tx_state)
TX_IDLE: if (tx_start) tx_next_state = TX_START;
TX_START: tx_next_state = TX_DATA;
TX_DATA: if (bit_count == 3'd7) tx_next_state = TX_STOP;
TX_STOP: tx_next_state = TX_IDLE;
default: tx_next_state = TX_IDLE;
endcase
end
// Datapath shift register and serial output logic
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
tx_line <= 1'b1; // Idle state is logic 1
bit_count <= 3'b0;
end else if (baud_tick) begin
unique case (tx_state)
TX_IDLE: begin
tx_line <= 1'b1;
if (tx_start) tx_shift_reg <= tx_data_in;
end
TX_START: begin
tx_line <= 1'b0; // Drive start bit low
bit_count <= 3'b0;
end
TX_DATA: begin
tx_line <= tx_shift_reg[0]; // Transmit LSB first
tx_shift_reg <= {1'b0, tx_shift_reg[7:1]};
bit_count <= bit_count + 1'b1;
end
TX_STOP: begin
tx_line <= 1'b1; // Drive stop bit high
end
endcase
end
end
The transmitter synchronizes all bit transitions to baud_tick.
It transmits data least significant bit first and terminates with a logic 1 stop bit.
The Receiver: Mid-Bit Sampling FSM
The receiver monitors the asynchronous line, verifies the start bit, and samples data at bit centers:
typedef enum logic [1:0] {RX_IDLE, RX_START, RX_DATA, RX_STOP} rx_state_t;
rx_state_t rx_state, rx_next_state;
logic [3:0] tick_count; // Counts 16x oversampling sub-ticks
logic [2:0] bit_count;
logic [7:0] rx_shift_reg;
logic framing_error;
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) rx_state <= RX_IDLE;
else rx_state <= rx_next_state;
end
always_comb begin
rx_next_state = rx_state;
unique case (rx_state)
RX_IDLE: if (!rx_line) rx_next_state = RX_START;
RX_START: if (tick_16x && tick_count == 4'd7)
rx_next_state = rx_line ? RX_IDLE : RX_DATA; // Reject noise glitch
RX_DATA: if (tick_16x && tick_count == 4'd15 && bit_count == 3'd7)
rx_next_state = RX_STOP;
RX_STOP: if (tick_16x && tick_count == 4'd15)
rx_next_state = RX_IDLE;
default: rx_next_state = RX_IDLE;
endcase
end
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
tick_count <= 0;
bit_count <= 0;
framing_error <= 1'b0;
end else if (tick_16x) begin
unique case (rx_state)
RX_IDLE: begin
tick_count <= 0;
bit_count <= 0;
end
RX_START: begin
tick_count <= (tick_count == 4'd7) ? 4'd0 : tick_count + 1'b1;
end
RX_DATA: begin
if (tick_count == 4'd15) begin
rx_shift_reg <= {rx_line, rx_shift_reg[7:1]}; // Sample center bit
bit_count <= bit_count + 1'b1;
tick_count <= 0;
end else begin
tick_count <= tick_count + 1'b1;
end
end
RX_STOP: begin
if (tick_count == 4'd15) begin
framing_error <= !rx_line; // Flag error if stop bit is low
tick_count <= 0;
end else begin
tick_count <= tick_count + 1'b1;
end
end
endcase
end
end
The start-bit verification step checks the line after 8 sub-ticks (tick_count == 4'd7).
If noise pulled the line low transiently, the receiver returns to RX_IDLE.
Once verified, the receiver resets tick_count and samples incoming bits every 16 sub-ticks.
Practice Exercise
Trace the receiver counters tick_count and bit_count during data reception.
Confirm that exactly eight data samples occur before transitioning to RX_STOP.
Solution
Upon entering RX_DATA, tick_count counts from 0 to 15.
At sub-tick 15, the receiver captures the first data bit, sets bit_count to 1, and clears tick_count.
Each subsequent 16-tick interval samples another bit.
On the eighth interval, bit_count equals 7 and tick_count reaches 15.
The FSM captures the final data bit and transitions to RX_STOP.
This sequencing guarantees that exactly eight data bits enter the shift register.
The transmitter serializes data bytes using a PISO shift register and 1x baud ticks. The receiver samples incoming bits at their midpoints using 16x oversampled ticks. The next article integrates the transmitter, receiver, and baud generators into a complete loopback testbench.