source: trunk/MultiChannelUSB/control.v@ 70

Last change on this file since 70 was 69, checked in by demin, 15 years ago

add I2C master

File size: 6.5 KB
Line 
1module control
2 (
3 input wire clk,
4 input wire rx_empty, tx_full,
5 input wire [7:0] rx_data,
6
7 input wire [1:0] mux_max_byte,
8 input wire [15:0] mux_min_addr, mux_max_addr,
9 input wire [7:0] mux_q,
10
11 output wire mux_reset,
12 output wire mux_type,
13 output wire [1:0] mux_chan,
14 output wire [1:0] mux_byte,
15 output wire [15:0] mux_addr,
16
17 output wire rx_rdreq,
18 output wire tx_wrreq,
19 output wire [7:0] tx_data,
20
21 output wire ram_we,
22 output wire [19:0] ram_addr,
23 inout wire [17:0] ram_data,
24
25 output wire i2c_wrreq,
26 output wire [15:0] i2c_data,
27 input wire i2c_full,
28
29 output wire led
30 );
31
32 reg [23:0] led_counter;
33 reg [18:0] ram_counter;
34 reg [10:0] tst_counter;
35 reg [15:0] int_addr, int_max_addr;
36
37 reg int_rdreq, int_wrreq;
38 reg int_type, int_reset;
39 reg [1:0] int_chan, int_byte, int_max_byte;
40 reg [7:0] int_data;
41 reg int_led;
42
43 reg [15:0] int_i2c_data;
44 reg int_i2c_wrreq;
45
46
47 wire crc_error = 1'b0;
48 reg crc_reset;
49 reg [2:0] byte_counter;
50 reg [4:0] idle_counter;
51
52 reg [4:0] state;
53
54 wire [15:0] src, dst;
55
56 reg [15:0] memory [15:0];
57 reg [7:0] buffer [7:0];
58
59 assign src = (buffer[0][7]) ? memory[buffer[3][3:0]] : {buffer[2], buffer[3]};
60 assign dst = {1'b0, buffer[0][6:0], buffer[1]};
61
62 reg int_ram_we;
63 reg [17:0] int_ram_data;
64 wire [17:0] int_ram_q;
65 wire [17:0] opt_ram_we;
66 assign ram_we = ~int_ram_we;
67 assign int_ram_q = ram_data;
68// assign ram_data = int_ram_we ? int_ram_data : 18'bz;
69// assign ram_addr = {ram_counter[18:5],1'd0,ram_counter[4:0]};
70 assign ram_addr = {1'd0, ram_counter[18:0]};
71
72 genvar j;
73 generate
74 for (j = 0; j < 18; j = j + 1)
75 begin : SRAM_WE
76 assign opt_ram_we[j] = int_ram_we;
77 assign ram_data[j] = opt_ram_we[j] ? int_ram_data[j] : 1'bz;
78 end
79 endgenerate
80
81 always @(posedge clk)
82 begin
83 if (~rx_empty)
84 begin
85 int_led <= 1'b0;
86 led_counter <= 24'd0;
87 end
88 else
89 begin
90 if (&led_counter)
91 begin
92 int_led <= 1'b1;
93 end
94 else
95 begin
96 led_counter <= led_counter + 24'd1;
97 end
98 end
99
100 case(state)
101 0:
102 begin
103 int_rdreq <= 1'b1;
104 int_wrreq <= 1'b0;
105 int_type <= 1'b0;
106 int_chan <= 2'd0;
107 int_byte <= 2'd0;
108 int_reset <= 1'b0;
109 crc_reset <= 1'b0;
110 int_ram_we <= 1'b0;
111 int_ram_data <= 16'd0;
112 ram_counter <= 19'd0;
113 idle_counter <= 5'd0;
114 byte_counter <= 3'd0;
115 state <= 5'd1;
116 end
117
118 1:
119 begin
120 // read 8 bytes
121 if (~rx_empty)
122 begin
123 idle_counter <= 5'd0;
124 byte_counter <= byte_counter + 3'd1;
125 buffer[byte_counter] <= rx_data;
126 if (&byte_counter)
127 begin
128 int_rdreq <= 1'b0;
129 state <= 5'd2;
130 end
131 end
132 else if(|byte_counter)
133 begin
134 idle_counter <= idle_counter + 5'd1;
135 if (&idle_counter)
136 begin
137 int_rdreq <= 1'b0;
138 crc_reset <= 1'b1;
139 state <= 5'd0;
140 end
141 end
142 end
143
144 2:
145 begin
146 crc_reset <= 1'b1;
147 if (~crc_error)
148 begin
149 memory[dst[3:0]] <= src;
150
151 case (dst)
152 16'h0000:
153 begin
154 state <= 5'd0;
155 end
156
157 16'h0001:
158 begin
159 int_type <= src[4];
160 int_chan <= src[1:0];
161 int_reset <= 1'b1;
162 state <= 5'd0;
163 end
164
165 16'h0002:
166 begin
167 int_type <= src[4];
168 int_chan <= src[1:0];
169 state <= 5'd3;
170 end
171
172 16'h0003:
173 begin
174 tst_counter <= 11'd0;
175 state <= 5'd6;
176 end
177 16'h0004:
178 begin
179 int_ram_we <= 1'b1;
180 int_ram_data <= 18'd0;
181 ram_counter <= 19'd0;
182 state <= 5'd9;
183 end
184 16'h0005:
185 begin
186 int_i2c_data <= src;
187 int_i2c_wrreq <= 1'b1;
188 state <= 5'd15;
189 end
190 endcase
191 end
192 end
193
194 // mux transfer
195 3:
196 begin
197 crc_reset <= 1'b0;
198 int_addr <= mux_min_addr;
199 int_max_addr <= mux_min_addr + mux_max_addr;
200 int_max_byte <= mux_max_byte;
201 int_byte <= 2'd0;
202 state <= 5'd4;
203 end
204
205 4:
206 begin
207 int_wrreq <= 1'b0;
208 state <= 5'd5;
209 end
210
211 5:
212 begin
213 if (~tx_full)
214 begin
215 int_data <= mux_q;
216 int_wrreq <= 1'b1;
217 if ((int_byte == int_max_byte) && (int_addr == int_max_addr))
218 begin
219 state <= 5'd0;
220 end
221 else
222 begin
223 state <= 5'd4;
224 if (int_byte == int_max_byte)
225 begin
226 int_addr <= int_addr + 16'd1;
227 int_byte <= 2'd0;
228 end
229 else
230 begin
231 int_byte <= int_byte + 2'd1;
232 end
233 end
234 end
235 end
236
237 // tst transfer
238 6:
239 begin
240 crc_reset <= 1'b0;
241 int_data <= tst_counter;
242 int_wrreq <= 1'b1;
243 tst_counter <= tst_counter + 11'd1;
244 state <= 5'd7;
245 end
246 7:
247 begin
248 if (~tx_full)
249 begin
250 int_data <= tst_counter;
251 if (&tst_counter)
252 begin
253 state <= 5'd8;
254 end
255 else
256 begin
257 tst_counter <= tst_counter + 11'd1;
258 end
259 end
260 end
261 8:
262 begin
263 if (~tx_full)
264 begin
265 int_wrreq <= 1'b0;
266 state <= 5'd0;
267 end
268 end
269 // ram transfer
270 9:
271 begin
272 crc_reset <= 1'b0;
273 state <= 5'd10;
274 end
275 10:
276 begin
277 int_ram_data[8:1] <= ram_counter[7:0];
278// int_ram_data[8:1] <= 8'd0;
279 if (&ram_counter)
280 begin
281 state <= 5'd11;
282 end
283 else
284 begin
285 state <= 5'd9;
286 ram_counter <= ram_counter + 19'd1;
287 end
288 end
289 11:
290 begin
291 int_ram_we <= 1'b0;
292 int_ram_data <= 18'd0;
293 ram_counter <= 19'd0;
294 state <= 5'd12;
295 end
296 12:
297 begin
298 int_wrreq <= 1'b0;
299 state <= 5'd13;
300 end
301 13:
302 begin
303 state <= 5'd14;
304 end
305 14:
306 begin
307 if (~tx_full)
308 begin
309 int_data <= int_ram_q[8:1];
310 int_wrreq <= 1'b1;
311 if (&ram_counter)
312 begin
313 state <= 5'd0;
314 end
315 else
316 begin
317 state <= 5'd12;
318 ram_counter <= ram_counter + 19'd1;
319 end
320 end
321 end
322
323 // i2c write
324 15:
325 begin
326 crc_reset <= 1'b0;
327 if (~i2c_full)
328 begin
329 int_i2c_wrreq <= 1'b0;
330 state <= 5'd0;
331 end
332 end
333
334 default:
335 begin
336 state <= 5'd0;
337 end
338 endcase
339 end
340
341 assign mux_reset = int_reset;
342 assign mux_type = int_type;
343 assign mux_chan = int_chan;
344 assign mux_byte = int_byte;
345 assign mux_addr = int_addr;
346 assign rx_rdreq = int_rdreq & (~rx_empty);
347 assign tx_wrreq = int_wrreq & (~tx_full);
348 assign tx_data = int_data;
349 assign i2c_wrreq = int_i2c_wrreq;
350 assign i2c_data = int_i2c_data;
351 assign led = int_led;
352
353endmodule
Note: See TracBrowser for help on using the repository browser.