Skip to content

Commit 6404483

Browse files
committed
Improve the code by using shift register, not indexed access
- from looking at ZipCPU wbuart32
1 parent fc64e75 commit 6404483

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Uart8Receiver.v

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ reg [4:0] in_hold_reg = 5'b0; // shift reg for signal hold time checks
3333
reg [3:0] sample_count = 4'b0; // count ticks for 16x oversample
3434
reg [4:0] out_hold_count = 5'b0; // count ticks before clearing output data
3535
reg [2:0] bit_index = 3'b0; // index for 8-bit data
36-
reg [7:0] received_data = 8'b0; // storage for the deserialized data
36+
reg [7:0] received_data = 8'b0; // shift reg for the deserialized data
3737
wire in_sample;
3838
wire [3:0] in_prior_hold_reg;
3939
wire [3:0] in_current_hold_reg;
@@ -155,7 +155,7 @@ always @(posedge clk) begin
155155
if (&sample_count) begin // reached 15
156156
// sample_count wraps around to zero
157157
bit_index <= 3'b1;
158-
received_data <= { 7'b0, in_sample };
158+
received_data <= { in_sample, 7'b0 };
159159
out <= 8'b0;
160160
state <= `DATA_BITS;
161161
end
@@ -165,14 +165,18 @@ always @(posedge clk) begin
165165
/*
166166
* Take 8 baud intervals to receive serial data
167167
*/
168-
sample_count <= sample_count + 4'b1;
168+
sample_count <= sample_count + 4'b1;
169169
if (&sample_count) begin // reached 15 - save one more bit of data
170-
received_data[bit_index] <= in_sample;
171-
bit_index <= bit_index + 3'b1;
170+
// store the bit using a shift register: the hardware
171+
// realization is simple compared to routing the bit
172+
// dynamically, i.e. using received_data[bit_index]
173+
received_data <= { in_sample, received_data[7:1] };
174+
// manage the state transition
175+
bit_index <= bit_index + 3'b1;
172176
if (&bit_index) begin
173177
// bit_index wraps around to zero
174178
// sample_count wraps around to zero
175-
state <= `STOP_BIT;
179+
state <= `STOP_BIT;
176180
end
177181
end
178182
end

Uart8Transmitter.v

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module Uart8Transmitter #(
5050
);
5151

5252
reg [2:0] state = `RESET;
53-
reg [7:0] in_data = 8'b0; // storage for the data to transmit serially
53+
reg [7:0] in_data = 8'b0; // shift reg for the data to transmit serially
5454
reg [2:0] bit_index = 3'b0; // index for 8-bit data
5555

5656
always @(posedge clk) begin
@@ -88,7 +88,12 @@ always @(posedge clk) begin
8888
end
8989

9090
`DATA_BITS: begin // take 8 clock cycles for data bits to be sent
91-
out <= in_data[bit_index];
91+
// grab each input bit using a shift register: the hardware
92+
// realization is simple compared to routing the access
93+
// dynamically, i.e. using in_data[bit_index]
94+
in_data <= { 1'b0, in_data[7:1] };
95+
out <= in_data[0];
96+
// manage the state transition
9297
bit_index <= bit_index + 3'b1;
9398
if (&bit_index) begin
9499
// bit_index wraps around to zero

0 commit comments

Comments
 (0)