-
Notifications
You must be signed in to change notification settings - Fork 0
/
Milestone_1.v
559 lines (405 loc) · 12.3 KB
/
Milestone_1.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
Copyright by 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"
//Module responsible for coordinating the FIR and RGB Converter
module Milestone_1 (
/////// board clocks ////////////
input logic CLOCK_50_I, // 50 MHz clock
input logic Resetn,
output logic [17:0] SRAM_address,
output logic [15:0] SRAM_write_data,
output logic SRAM_we_n,
input logic [15:0] SRAM_read_data,
output logic M1_done,
input logic M1_start
);
M1_state_type state;
///////FIR
logic load_U_buffer;
logic load_V_buffer;
logic enable_U;
logic enable_V;
logic read_U_0;
logic read_V_0;
logic line_start;
logic line_end;
logic read_end_Y;
//output logic FIR_enable;
logic cycle;
logic common_case;
logic common_U;
////////RGB Converter
logic [31:0] U_RGB;
logic [31:0] V_RGB;
logic [31:0] Y_RGB;
logic enable_RGB;
//For FIR
logic [17:0] Y_address, Y_compare_address;
logic [17:0] U_address;
logic [17:0] V_address;
logic [31:0] even_U;
logic [31:0] even_V;
logic [31:0] FIR_BUFF_U;
logic [31:0] FIR_BUFF_V;
logic clear_SReg;
//For RGB Conversion
//logic enable_RGB;
logic [17:0] RGB_address;
logic [31:0] Y_buff;
logic [7:0] R,G,B;
logic [7:0] R2,G2,B2;
logic [7:0] B_out_buffer;
//Finite Impulse Response (FIR) unit
FIR FIR_unit (
.CLOCK_50_I(CLOCK_50_I),
.resetn(Resetn),
.enable_U(enable_U),
.enable_V(enable_V),
.load_U_buffer(load_U_buffer),
.load_V_buffer(load_V_buffer),
.read_U_0(read_U_0),
.read_V_0(read_V_0),
.cycle(cycle),
.clear_SReg(clear_SReg),
.FIR_BUFF_U(FIR_BUFF_U),
.FIR_BUFF_V(FIR_BUFF_V),
.even_U(even_U),
.even_V(even_V),
.line_end(line_end),
.line_start(line_start),
.SRAM_read_data(SRAM_read_data)
);
//RGB Conversion unit
RGB_Converter RGB_unit(
//To RGB Converter
.CLOCK_50_I(CLOCK_50_I),
.resetn(Resetn),
.enable_RGB(enable_RGB),
.U_in_RGB(U_RGB),
.V_in_RGB(V_RGB),
.Y_in_RGB(Y_RGB),
//From RGB Converter
.R_buff(R),
.G_buff(G),
.B_buff(B)
);
always_ff @ (posedge CLOCK_50_I or negedge Resetn) begin
if (~Resetn) begin
state <= S_M1_IDLE;
SRAM_we_n <= 1'b1;
SRAM_write_data <= 16'd0;
line_start <= 1'b1;
line_end <= 1'b0;
read_end_Y <= 1'b0;
cycle <= 1'b1;
common_case <= 1'b0;
clear_SReg <= 1'b0;
enable_U <= 1'b0;
enable_V <= 1'b0;
enable_RGB <= 1'b0;
load_U_buffer <= 1'b0;
load_V_buffer <= 1'b0;
M1_done <= 1'b0;
Y_address <= 18'd38400;
V_address <= 18'd0;
Y_compare_address <= 18'd0;
U_address <= 18'd57600;
RGB_address <= 18'd146944;
B_out_buffer <= 8'd0;
SRAM_address <= 18'd38400;//Start by reading first U value U0/1
end else begin
////$write("\n\n\n\nState %s\n", state);
////$write("\t Y Location %d\n", Y_address);
////$write("\t Write enable %d\n", SRAM_we_n);
////$write("\t Write data %h\n", SRAM_write_data);
////$write("\t SRAM Read %h\n\n", SRAM_read_data);
////$write("\t SRAM address %d\n\n", SRAM_address);
/* ////$write("\t cycle %d\n\n", cycle);
////$write("\t common_case %d\n\n", common_case);
////$write("\t R %h \n", R);
////$write("\t G %h \n", G);
////$write("\t B %h \n", B); */
case (state)
S_M1_IDLE : begin
if (M1_start) begin
state <= S_M1_START;
SRAM_we_n <= 1'b1;
SRAM_write_data <= 16'd0;
line_start <= 1'b1;
line_end <= 1'b0;
read_end_Y <= 1'b0;
enable_U <= 1'b0;
enable_V <= 1'b0;
enable_RGB <= 1'b0;
load_U_buffer <= 1'b0;
load_V_buffer <= 1'b0;
cycle <= 1'b1;
common_case <= 1'b0;
clear_SReg <= 1'b0;
M1_done <= 1'b0;
B_out_buffer <= 8'd0;
SRAM_address <= 18'd38400;
Y_address <= 18'd0;
Y_compare_address <= 18'd0;
U_address <= 18'd38400;
V_address <= 18'd57600;
RGB_address <= 18'd146944;
end
end
S_M1_START: begin
SRAM_we_n <= 1'b1; //Disable writing to SRAM
clear_SReg <= 1'b0;
SRAM_address <= V_address; //Set address to first V value V0/1
U_address <= U_address + 18'd1;
V_address <= V_address + 18'd1;
state <= S_START_LINE_0;
end
S_START_LINE_0: begin
SRAM_address <= U_address; //U2/3
U_address <= U_address + 18'd1;
read_U_0 <= 1'b1; //Load U0 three times into U_SReg in the next clock cycle and U1 once
state <= S_START_LINE_1;
end
S_START_LINE_1: begin
SRAM_address <= V_address;//V2/3
V_address <= V_address + 18'd1;
read_U_0 <= 1'b0; //Load U0 three times into U_SReg in the next clock cycle and U1 once
read_V_0 <= 1'b1;
state <= S_START_LINE_2;
end
S_START_LINE_2: begin
//Do not send address to read from for delay3
//read_U_0 <= 1'b0;
read_V_0 <= 1'b0; // Load V0 three times into V_SReg in the next clock cycle and V1 once
enable_U <= 1'b1; // Load U2/3 into U_SReg in the next clock cycle
state <= S_START_LINE_3;
end
S_START_LINE_3: begin
//send address to read from for delay4
SRAM_address <= U_address; //U4/5
U_address <= U_address + 18'd1;
enable_U <= 1'b0; // Load U2/3 into U_SReg in the next clock cycle
//read_V_0 <= 1'b0;
state <= S_START_LINE_4;
end
S_START_LINE_4: begin
SRAM_address <= Y_address; //Y0/1
Y_address <= Y_address + 18'd1;
//enable_U <= 1'b0;
enable_V <= 1'b1;// Load V2/3 into V_SReg in the next clock cycle
state <= S_START_LINE_5;
end
S_START_LINE_5: begin
//SRAM_address <= Y_address; //Y0/1
//Y_address <= Y_address + 18'd1;
enable_V <= 1'b0;
line_start <= 1'b0; // Signal to begin FIR calculations
load_U_buffer <= 1'b1;
state <= S_START_LINE_6;
end
S_START_LINE_6: begin
SRAM_address <= V_address; // V4/5
V_address <= V_address + 18'd1;
load_U_buffer <= 1'b0;
line_start <= 1'b0; // Signal to begin FIR calculations
state <= S_START_LINE_7;
end
S_START_LINE_7: begin
enable_U <= 1'b1; // Read U4/5 next cycle, Load U4 into SReg and buffer U5
state <= S_START_LINE_8;
end
S_START_LINE_8: begin
Y_RGB <= {24'd0,SRAM_read_data[15:8]};
Y_buff <= {24'd0,SRAM_read_data[7:0]};
enable_U <= 1'b0;
load_V_buffer <= 1'b1;
state <= S_START_LINE_9;
end
S_START_LINE_9: begin
cycle <= 1'b0;
//Y_RGB <= {24'd0,SRAM_read_data[15:8]};
//Y_buff <= {24'd0,SRAM_read_data[7:0]};
U_RGB <= even_U;
V_RGB <= even_V;
load_V_buffer <= 1'b0;
//enable_V <= 1'b1; //Load next V value into shift register
enable_RGB <= 1'b1;
SRAM_we_n <= 1'b1; //Don't write on first RUN_0 of the line
state <= S_RUN_0;
/* ////$write("##################################################################################################################################################\n\n");
////$write(" END OF LEAD IN \n");
////$write("##################################################################################################################################################\n\n"); */
end
////////////////////////////////////////////////////////////
S_RUN_0: begin
if (common_case) begin
SRAM_address <= RGB_address; //Assert RGB address to write to
RGB_address <= RGB_address + 18'd1; //Increment RGB address for the next write
SRAM_write_data <= {G, B};
end else begin
common_case <= 1'b1;
end
if ( cycle ) begin
load_U_buffer <= 1'b1;
end
load_V_buffer <= 1'b0;
enable_V <= 1'b1; //Load next V value into shift register
state <= S_RUN_1;
end
S_RUN_1: begin
SRAM_we_n <= 1'b1;
SRAM_address <= Y_address; //Assert Read Address for next Y values
Y_address <= Y_address + 18'd1;
load_U_buffer <= 1'b0;
enable_V <= 1'b0; //Load next V value into shift register
state <= S_RUN_2;
end
S_RUN_2: begin
if (cycle) begin
SRAM_address <= V_address; //Assert Read Address for next Y values
V_address <= V_address + 18'd1;
end
Y_RGB <= Y_buff;
U_RGB <= FIR_BUFF_U;
V_RGB <= FIR_BUFF_V;
state <= S_RUN_3;
end
S_RUN_3: begin
if (cycle) load_U_buffer <= 1'b0;
SRAM_address <= RGB_address; //Set RGB address to write to
RGB_address <= RGB_address + 18'd1; //Increment RGB_address for next write
SRAM_we_n <= 1'b0; //enable writing for next cycle
SRAM_write_data <= {R,G};
B_out_buffer <= B; //Save B value to write along with the next R value
enable_U <= 1'b1;
state <= S_RUN_4;
end
S_RUN_4: begin
SRAM_we_n <= 1'b1;
if(~cycle) begin
SRAM_address <= U_address;
U_address <= U_address + 18'd1;
end else begin
load_V_buffer <= 1'b1;
end
Y_RGB <= {24'd0,SRAM_read_data[15:8]};
Y_buff <= {24'd0, SRAM_read_data[7:0]};
enable_U <= 1'b0;
state <= S_RUN_5;
end
S_RUN_5: begin
load_V_buffer <= 1'b0;
SRAM_we_n <= 1'b0; //Enable writing for the next cycle
SRAM_address <= RGB_address; //Assert RGB address to write to
RGB_address <= RGB_address + 18'd1; //Increment RGB address for the next write
SRAM_write_data <= {B_out_buffer, R};
U_RGB <= even_U;
V_RGB <= even_V;
cycle <= ~cycle;
//Going to end of line
if ( Y_address - Y_compare_address == 18'd156 ) begin
state <= S_END_LINE_0;
U_address <= U_address - 18'd1;
//line_end <= 1'b1;
end else begin
state <= S_RUN_0;
end
//////$write("###########################################################################################################################################################################################################################\n\n");
end
////////////////////////////////////////////////////////////
//End line cases are similar to common cases but not reading
S_END_LINE_0: begin
SRAM_address <= RGB_address; //Assert RGB address to write to
RGB_address <= RGB_address + 18'd1; //Increment RGB address for the next write
SRAM_write_data <= {G, B};
if (~read_end_Y) enable_V <= 1'b1;
state <= S_END_LINE_1;
end
S_END_LINE_1: begin
SRAM_we_n <= 1'b1;
if (~read_end_Y) begin
SRAM_address <= Y_address;
Y_address <= Y_address + 18'd1;
end
line_end <= 1'b1;
enable_V <= 1'b0;
state <= S_END_LINE_2;
end
S_END_LINE_2: begin
Y_RGB <= Y_buff;
U_RGB <= FIR_BUFF_U;
V_RGB <= FIR_BUFF_V;
state <= S_END_LINE_3;
end
S_END_LINE_3: begin
SRAM_address <= RGB_address;
RGB_address <= RGB_address + 18'd1;
SRAM_we_n <= 1'b0;
SRAM_write_data <= {R, G};
B_out_buffer <= B; //Save B value to write along with the next R value
if (~read_end_Y) enable_U <= 1'b1;
state <= S_END_LINE_4;
end
S_END_LINE_4: begin
SRAM_we_n <= 1'b1; //Enable writing
if (~read_end_Y)Y_RGB <= {24'd0,SRAM_read_data[15:8]};
Y_buff <= {24'd0, SRAM_read_data[7:0]};
enable_U <= 1'b0;
state <= S_END_LINE_5;
end
S_END_LINE_5: begin
SRAM_we_n <= 1'b0; //Enable writing
SRAM_address <= RGB_address;
RGB_address <= RGB_address + 18'd1;
SRAM_write_data <= {B_out_buffer, R};
if (~read_end_Y) U_RGB <= even_U;
if (~read_end_Y) V_RGB <= even_V;
if ( Y_address - Y_compare_address == 18'd160 ) begin
if (read_end_Y) begin
Y_compare_address <= Y_compare_address + 18'd160;
state <= S_END_LINE_6;
end else begin
read_end_Y <= 1'b1;
state <= S_END_LINE_0;
end
end else begin
state <= S_END_LINE_0;
end
end
S_END_LINE_6: begin
SRAM_we_n <= 1'b0;
SRAM_address <= RGB_address; //Assert RGB address to write to
RGB_address <= RGB_address + 18'd1; //Increment RGB address for the next write
SRAM_write_data <= {G, B};
state <= S_END_LINE_7;
end
S_END_LINE_7: begin
SRAM_we_n <= 1'b1;
SRAM_address <= U_address;
//U_address <= U_address + 18'd1;
common_case <= 1'b0;
line_end <= 1'b0;
line_start <= 1'b1;
clear_SReg <= 1'b1;
enable_RGB <= 1'b0;
read_end_Y <= 1'b0;
if ( Y_address == 18'd38400) begin
state <= S_M1_IDLE;
M1_done <= 1'b1;
////$write("WE DID IT");
end else begin
state <= S_M1_START;
end
end
default: state <= S_M1_IDLE;
endcase
end
end
endmodule