-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUART_SRAM_interface.v
172 lines (141 loc) · 4.25 KB
/
UART_SRAM_interface.v
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright by Adam Kinsman and Henry Ko and Nicola Nicolici
// Developed for the Digital Systems Design course (COE3DQ4)
// Department of Electrical and Computer Engineering
// McMaster University
// Ontario, Canada
`timescale 1ns/100ps
`default_nettype none
`include "define_state.h"
// This module monitors the data from UART
// It also assembles and writes the data into the SRAM
module UART_SRAM_interface (
input logic Clock,
input logic Resetn,
input logic UART_RX_I,
input logic Initialize,
input logic Enable,
output logic [17:0] SRAM_address,
output logic [15:0] SRAM_write_data,
output logic SRAM_we_n,
output logic [3:0] Frame_error
);
UART_SRAM_state_type UART_SRAM_state;
logic UART_rx_unload_data;
logic UART_rx_empty;
logic UART_rx_enable;
logic [7:0] UART_rx_data;
logic [1:0] new_line_count;
// UART_Receiver
UART_Receive_Controller UART_RX (
.Clock_50(Clock),
.Resetn(Resetn),
.Enable(UART_rx_enable),
.Unload_data(UART_rx_unload_data),
.RX_data(UART_rx_data),
.Empty(UART_rx_empty),
.Overrun(),
.Frame_error(Frame_error),
// UART pin
.UART_RX_I(UART_RX_I)
);
// Receive data from UART
always_ff @ (posedge Clock or negedge Resetn) begin
if (~Resetn) begin
SRAM_we_n <= 1'b1;
SRAM_write_data <= 16'd0;
SRAM_address <= 18'd0;
UART_rx_enable <= 1'b0;
UART_rx_unload_data <= 1'b0;
new_line_count <= 2'd0;
UART_SRAM_state <= S_US_IDLE;
end else begin
if (Initialize == 1'b1) begin
UART_rx_enable <= 1'b0;
UART_rx_unload_data <= 1'b0;
SRAM_we_n <= 1'b1;
SRAM_write_data <= 16'd0;
SRAM_address <= 18'd0;
new_line_count <= 2'd0;
UART_SRAM_state <= S_US_IDLE;
end else begin
case (UART_SRAM_state)
S_US_IDLE: begin
if (Enable == 1'b1) begin
// Start receiving data from UART
UART_SRAM_state <= S_US_START_FIRST_BYTE_RECEIVE;
UART_rx_enable <= 1'b1;
SRAM_address <= 18'd0;
end
end
S_US_STRIP_FILE_HEADER_1: begin
if (UART_rx_empty == 1'b0) begin
// a byte of data is available
UART_rx_unload_data <= 1'b1;
// The header consists of 3 lines
// so detect a the line feed data (8'h0A in ASCII) for 3 times
if (UART_rx_data == 8'h0A && new_line_count < 2'd3)
new_line_count <= new_line_count + 2'd1;
UART_SRAM_state <= S_US_STRIP_FILE_HEADER_2;
end
end
S_US_STRIP_FILE_HEADER_2: begin
if (UART_rx_empty == 1'b1) begin
// Clear the unload flag
UART_rx_unload_data <= 1'b0;
if (new_line_count == 2'd3)
// Header is stripped out
UART_SRAM_state <= S_US_START_FIRST_BYTE_RECEIVE;
else
UART_SRAM_state <= S_US_STRIP_FILE_HEADER_1;
end
end
S_US_START_FIRST_BYTE_RECEIVE: begin
if (UART_rx_empty == 1'b0) begin
// a byte of data is available
UART_rx_unload_data <= 1'b1;
// Assemble the first byte of data
SRAM_write_data[15:8] <= UART_rx_data;
UART_SRAM_state <= S_US_WRITE_FIRST_BYTE;
end
end
S_US_WRITE_FIRST_BYTE: begin
if (UART_rx_empty == 1'b1) begin
// Clear the unload flag
UART_rx_unload_data <= 1'b0;
UART_SRAM_state <= S_US_START_SECOND_BYTE_RECEIVE;
end
end
S_US_START_SECOND_BYTE_RECEIVE: begin
if (UART_rx_empty == 1'b0) begin
// a byte of data is available
UART_rx_unload_data <= 1'b1;
// Assemble the second byte of data
SRAM_write_data[7:0] <= UART_rx_data;
// Write the data into the RAM
SRAM_we_n <= 1'b0;
UART_SRAM_state <= S_US_WRITE_SECOND_BYTE;
end
end
S_US_WRITE_SECOND_BYTE: begin
if (UART_rx_empty == 1'b1) begin
// Clear the unload flag
UART_rx_unload_data <= 1'b0;
SRAM_we_n <= 1'b1;
if (SRAM_address < 18'h3FFFF) begin
SRAM_address <= SRAM_address + 9'd1;
UART_SRAM_state <= S_US_START_FIRST_BYTE_RECEIVE;
end else begin
// Only receive at most 512 KB of data
// That's the capacity of the SRAM
UART_SRAM_state <= S_US_IDLE;
SRAM_address <= 18'h3FFFF;
UART_rx_enable <= 1'b0;
end
end
end
default: UART_SRAM_state <= S_US_IDLE;
endcase
end
end
end
endmodule