-
Notifications
You must be signed in to change notification settings - Fork 20
/
uh_jls.v
1361 lines (1217 loc) · 43.5 KB
/
uh_jls.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------------------------
// Module : uh_jls
// Type : synthesizable, IP's top
// Standard: Verilog 2001 (IEEE1364-2001)
// Function: UH-JLS, a ultra high performance JPEG-LS image encoder
//--------------------------------------------------------------------------------------------------------
module uh_jls (
input wire clk, // all below signals are sync with clk's posedge
input wire i_sof, // start of image (should set to 1 for at least 50 cycles before inputting an image)
input wire [10:0] i_w, // image width = 5*(rrw+1) , i_w∈[0,2047], i.e., width=5,10,15...10240
input wire [15:0] i_h, // image height= rrh+1 , i_h∈[0,65535], i.e., height∈[1,65536]
output wire i_rdy, // input pixel ready, handshake with i_e
input wire i_e, // input pixel enable, handshake with i_rdy
input wire [ 7:0] i_x0, i_x1, i_x2, i_x3, i_x4, // input 5 neighbor pixels
output wire o_e, // output data enable
output wire [63:0] o_data, // output data
output wire o_last // indicate the last output data of a image
);
reg [10:0] rrw;
reg [15:0] rrh;
always @ (posedge clk)
if (i_sof) begin
rrw <= i_w;
rrh <= i_h;
end
wire [15:0] img_w = 16'd5 * ({5'd0,rrw} + 16'd1);
wire [15:0] img_h = rrh + 16'd1;
wire [63:0] jls_header [0:3];
assign jls_header[0] = 64'hD8FF;
assign jls_header[1] = {img_h[7:0], img_h[15:8], 48'h080B00F7FF00};
assign jls_header[2] = {48'hDAFF00110101, img_w[7:0], img_w[15:8]};
assign jls_header[3] = 64'h01010800;
//---------------------------------------------------------------------------------------------------------------------------
// local parameters // BPP=8 NEAR=0 T1=3 T2=7 T3=21 ALPHA=256 QUANT=1 QBETA=256 QBPP=8 LIMIT=23 A_INIT=4
//---------------------------------------------------------------------------------------------------------------------------
localparam signed [ 8:0] P_T1 = 9'sd3;
localparam signed [ 8:0] P_T2 = 9'sd7;
localparam signed [ 8:0] P_T3 = 9'sd21;
//wire [3:0] J [32] = '{0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,5,5,6,6,7,7,8, 9,10,11,12,13,14,15};
//wire[13:0] J_MASK [32] = '{16383,16383,16383,16383,16382,16382,16382,16382,16380,16380,16380,16380,16376,16376,16376,16376,16368,16368,16352,16352,16320,16320,16256,16256,16128,15872,15360,14336,12288,8192,0,0};
wire [ 3:0] J_PLUS [0:31]; //= '{1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,6,6,7,7,8,8,9,10,11,12,13,14,15,15};
wire [13:0] J_MASK [0:31]; //= '{0,0,0,0,1,1,1,1,3,3,3,3,7,7,7,7,15,15,31,31,63,63,127,127,255,511,1023,2047,4095,8191,16383,16383};
assign J_PLUS[ 0] = 1;
assign J_PLUS[ 1] = 1;
assign J_PLUS[ 2] = 1;
assign J_PLUS[ 3] = 1;
assign J_PLUS[ 4] = 2;
assign J_PLUS[ 5] = 2;
assign J_PLUS[ 6] = 2;
assign J_PLUS[ 7] = 2;
assign J_PLUS[ 8] = 3;
assign J_PLUS[ 9] = 3;
assign J_PLUS[10] = 3;
assign J_PLUS[11] = 3;
assign J_PLUS[12] = 4;
assign J_PLUS[13] = 4;
assign J_PLUS[14] = 4;
assign J_PLUS[15] = 4;
assign J_PLUS[16] = 5;
assign J_PLUS[17] = 5;
assign J_PLUS[18] = 6;
assign J_PLUS[19] = 6;
assign J_PLUS[20] = 7;
assign J_PLUS[21] = 7;
assign J_PLUS[22] = 8;
assign J_PLUS[23] = 8;
assign J_PLUS[24] = 9;
assign J_PLUS[25] = 10;
assign J_PLUS[26] = 11;
assign J_PLUS[27] = 12;
assign J_PLUS[28] = 13;
assign J_PLUS[29] = 14;
assign J_PLUS[30] = 15;
assign J_PLUS[31] = 15;
assign J_MASK[ 0] = 0;
assign J_MASK[ 1] = 0;
assign J_MASK[ 2] = 0;
assign J_MASK[ 3] = 0;
assign J_MASK[ 4] = 1;
assign J_MASK[ 5] = 1;
assign J_MASK[ 6] = 1;
assign J_MASK[ 7] = 1;
assign J_MASK[ 8] = 3;
assign J_MASK[ 9] = 3;
assign J_MASK[10] = 3;
assign J_MASK[11] = 3;
assign J_MASK[12] = 7;
assign J_MASK[13] = 7;
assign J_MASK[14] = 7;
assign J_MASK[15] = 7;
assign J_MASK[16] = 15;
assign J_MASK[17] = 15;
assign J_MASK[18] = 31;
assign J_MASK[19] = 31;
assign J_MASK[20] = 63;
assign J_MASK[21] = 63;
assign J_MASK[22] = 127;
assign J_MASK[23] = 127;
assign J_MASK[24] = 255;
assign J_MASK[25] = 511;
assign J_MASK[26] = 1023;
assign J_MASK[27] = 2047;
assign J_MASK[28] = 4095;
assign J_MASK[29] = 8191;
assign J_MASK[30] = 16383;
assign J_MASK[31] = 16383;
//---------------------------------------------------------------------------------------------------------------------------
// function: predictor (get_px)
//---------------------------------------------------------------------------------------------------------------------------
function [7:0] func_predictor;
input [7:0] a, b, c;
begin
if ( c>=a && c>=b )
func_predictor = (a>b) ? b : a;
else if ( c<=a && c<=b )
func_predictor = (a>b) ? a : b;
else
func_predictor = a - c + b;
end
endfunction
//---------------------------------------------------------------------------------------------------------------------------
// function: q_quantize
//---------------------------------------------------------------------------------------------------------------------------
function signed [3:0] func_q_quantize;
input [7:0] x1, x2;
reg signed [8:0] delta;
begin
delta = $signed({1'b0,x1}) - $signed({1'b0,x2});
if (delta <= -P_T3 )
func_q_quantize = -4'sd4;
else if (delta <= -P_T2 )
func_q_quantize = -4'sd3;
else if (delta <= -P_T1 )
func_q_quantize = -4'sd2;
else if (delta < 9'sd0 )
func_q_quantize = -4'sd1;
else if (delta == 9'sd0 )
func_q_quantize = 4'sd0;
else if (delta < P_T1 )
func_q_quantize = 4'sd1;
else if (delta < P_T2 )
func_q_quantize = 4'sd2;
else if (delta < P_T3 )
func_q_quantize = 4'sd3;
else
func_q_quantize = 4'sd4;
end
endfunction
//---------------------------------------------------------------------------------------------------------------------------
// function: get_q
//---------------------------------------------------------------------------------------------------------------------------
function [9:0] func_get_q;
input [7:0] a, b, c, d;
reg signed [9:0] qs;
reg s;
reg [8:0] q;
begin
qs = 10'sd81 * func_q_quantize(d,b) + 10'sd9 * func_q_quantize(b,c) + func_q_quantize(c,a);
s = qs[9];
q = s ? (~qs[8:0]+9'd1) : qs[8:0];
q = q - 9'd1;
func_get_q = {s, q};
end
endfunction
//---------------------------------------------------------------------------------------------------------------------------
// regs and wires
//---------------------------------------------------------------------------------------------------------------------------
// stage A
reg a_e;
reg [ 7:0] a_x [0:4];
reg [10:0] a_ii;
reg [16:0] a_jj;
//-------------------------------------------------------------------------------------------------------------------
// linebuffer for context pixels
//-------------------------------------------------------------------------------------------------------------------
reg [39:0] linebuffer [0:2047];
reg linebuffer_o_ack;
reg [39:0] linebuffer_o_raw_b;
wire[ 7:0] linebuffer_o_raw [0:4];
reg [ 7:0] linebuffer_o_hold [0:4];
wire[ 7:0] linebuffer_o [0:4];
always @ (posedge clk)
linebuffer_o_ack <= i_rdy & (~i_sof);
always @ (posedge clk)
linebuffer_o_raw_b <= linebuffer[a_ii];
assign linebuffer_o_raw[0] = linebuffer_o_raw_b[ 7: 0];
assign linebuffer_o_raw[1] = linebuffer_o_raw_b[15: 8];
assign linebuffer_o_raw[2] = linebuffer_o_raw_b[23:16];
assign linebuffer_o_raw[3] = linebuffer_o_raw_b[31:24];
assign linebuffer_o_raw[4] = linebuffer_o_raw_b[39:32];
always @ (posedge clk)
if (linebuffer_o_ack) begin
linebuffer_o_hold[0] <= linebuffer_o_raw[0];
linebuffer_o_hold[1] <= linebuffer_o_raw[1];
linebuffer_o_hold[2] <= linebuffer_o_raw[2];
linebuffer_o_hold[3] <= linebuffer_o_raw[3];
linebuffer_o_hold[4] <= linebuffer_o_raw[4];
end
assign linebuffer_o[0] = linebuffer_o_ack ? linebuffer_o_raw[0] : linebuffer_o_hold[0];
assign linebuffer_o[1] = linebuffer_o_ack ? linebuffer_o_raw[1] : linebuffer_o_hold[1];
assign linebuffer_o[2] = linebuffer_o_ack ? linebuffer_o_raw[2] : linebuffer_o_hold[2];
assign linebuffer_o[3] = linebuffer_o_ack ? linebuffer_o_raw[3] : linebuffer_o_hold[3];
assign linebuffer_o[4] = linebuffer_o_ack ? linebuffer_o_raw[4] : linebuffer_o_hold[4];
// line buffer write
always @ (posedge clk)
if (a_e & i_rdy)
linebuffer[a_ii] <= {a_x[4], a_x[3], a_x[2], a_x[1], a_x[0]};
// stage B
reg b_e;
reg b_fc;
reg b_lc;
reg b_fr;
reg b_eof;
reg [ 7:0] b_x [0:4];
// stage C
reg c_e;
reg c_fc;
reg c_lc;
reg c_fr;
reg c_eof;
reg [ 7:0] c_xt [0:4];
reg [ 7:0] c_a;
reg [ 7:0] c_bt [0:4];
reg [ 7:0] c_ct;
reg [ 7:0] c_c;
wire[ 7:0] c_d = c_fr ? 8'd0 : (c_lc ? c_bt[4] : linebuffer_o[0]);
wire[ 7:0] c_b [-1:5]; //= '{c_c, c_bt[0], c_bt[1], c_bt[2], c_bt[3], c_bt[4], c_d};
wire[ 7:0] c_x [-1:4]; //= '{c_a, c_xt[0], c_xt[1], c_xt[2], c_xt[3], c_xt[4]};
assign c_b[-1] = c_c;
assign c_b[ 0] = c_bt[0];
assign c_b[ 1] = c_bt[1];
assign c_b[ 2] = c_bt[2];
assign c_b[ 3] = c_bt[3];
assign c_b[ 4] = c_bt[4];
assign c_b[ 5] = c_d;
assign c_x[-1] = c_a;
assign c_x[ 0] = c_xt[0];
assign c_x[ 1] = c_xt[1];
assign c_x[ 2] = c_xt[2];
assign c_x[ 3] = c_xt[3];
assign c_x[ 4] = c_xt[4];
// stage D
reg d_e;
reg d_lc;
reg d_eof;
reg d_runi;
reg d_rune [0:4];
reg d_rgar [0:4];
reg d_rt [0:4];
reg [ 7:0] d_x [0:4];
reg [ 7:0] d_px [0:4];
reg d_s [0:4];
reg [ 8:0] d_q [0:4];
// stage E
reg e_e;
reg e_eof;
reg [ 7:0] e_x [0:4];
reg [ 7:0] e_px [0:4];
reg e_s [0:4];
reg [ 4:0] e_q [0:4]; //
reg [ 3:0] e_g [0:4]; // q group number
reg e_on [0:4];
reg [13:0] e_rc;
reg [ 4:0] e_ri;
reg [13:0] e_cb [0:4];
reg [ 3:0] e_cn [0:4];
// stage F : double buffer
reg [ 3:0] f_i_ptr;
reg [ 1:0] f_o_ptr;
reg f_eof;
wire f_e = f_o_ptr != f_i_ptr[3:2];
assign i_rdy = f_o_ptr != {~f_i_ptr[3], f_i_ptr[2]}; // double buffer input grant (not full)
reg [ 7:0] f_x [0:7] [0:4];
reg [ 7:0] f_px [0:7] [0:4];
reg f_s [0:7] [0:4];
reg [ 4:0] f_q [0:7] [0:4];
reg [ 3:0] f_g [0:7] [0:4];
reg [13:0] f_cb [0:7] [0:4];
reg [ 3:0] f_cn [0:7] [0:4];
reg f_on [0:7] [0:4];
// stage G
reg g_e;
reg g_eof;
reg [ 7:0] g_x [0:19];
reg [ 7:0] g_px [0:19];
reg g_s [0:19];
reg [ 4:0] g_q [0:19];
reg [ 3:0] g_g [0:19];
reg [13:0] g_cb [0:19];
reg [ 3:0] g_cn [0:19];
reg g_on [0:19];
// stage H
reg h_e;
reg h_eof;
reg [ 7:0] h_x [0:19];
reg [ 7:0] h_px [0:19];
reg h_s [0:19];
reg [ 4:0] h_q [0:19];
reg [ 3:0] h_g [0:19];
reg [13:0] h_cb [0:19];
reg [ 3:0] h_cn [0:19];
reg h_on [0:19];
reg [19:0] h_bitmap [0:19];
// stage J
reg j_e;
reg j_eof;
reg [ 7:0] j_x [0:19];
reg [ 7:0] j_px [0:19];
reg j_s [0:19];
reg [ 4:0] j_q [0:19];
reg [ 3:0] j_g [0:19];
reg [13:0] j_cb [0:19];
reg [ 3:0] j_cn [0:19];
reg j_on [0:19];
reg [ 4:0] j_nptr [0:19];
// stage K
reg k_e;
reg k_eof;
reg [ 7:0] k_x [0:19];
reg [ 7:0] k_px [0:19];
reg k_s [0:19];
reg [ 4:0] k_q [0:19];
reg [ 3:0] k_g [0:19];
reg [13:0] k_cb [0:19];
reg [ 3:0] k_cn [0:19];
reg k_on [0:19];
reg [ 4:0] k_nptr [0:19];
reg [ 4:0] k_nmax;
reg [ 4:0] k_ncnt;
wire k_rdy;
// stage L
reg l_eof;
reg l_st;
reg l_et;
reg [ 7:0] l_x [0:19];
reg [ 7:0] l_px [0:19];
reg l_s [0:19];
reg [ 4:0] l_q [0:19];
reg [ 3:0] l_g [0:19];
reg [13:0] l_cb [0:19];
reg [ 3:0] l_cn [0:19];
reg l_on [0:19];
// stage M
reg m_eof;
reg m_st;
reg m_et;
reg [ 4:0] m_adr[0:13];
reg [ 3:0] m_g [0:19];
reg [ 7:0] m_x [0:19];
reg [ 7:0] m_px [0:19];
reg m_s [0:19];
reg [ 4:0] m_q [0:19];
reg [13:0] m_cb;
reg [ 3:0] m_cn;
reg m_on [0:19];
// stage N
reg n_eof;
reg n_st;
reg n_et;
reg [ 3:0] n_g [0:19];
reg n_vl [0:13];
reg [ 7:0] n_x [0:13];
reg [ 7:0] n_px [0:13];
reg n_s [0:13];
reg [ 4:0] n_q [0:13];
reg [13:0] n_cb;
reg [ 3:0] n_cn;
reg n_on [0:19];
// stage P : multiple stages (for bypassed signals that are not used in regular/run mode pipelines)
reg p_eof[0:10];
reg p_st [0:10];
reg p_et [0:10];
reg [ 3:0] p_g [0:10] [0:19];
reg p_on [0:10] [0:19];
reg [13:0] p_cb [0:10];
reg [ 3:0] p_cn [0:10];
// stage Q : regular/run mode pipelines output signals, and bypassed signals that are not used in regular/run mode pipelines
// bypassed signals that are not used in regular/run mode pipelines
wire q_eof;
wire q_st;
wire q_et;
reg [ 3:0] q_g [0:19]; // not real register
reg q_on [0:19]; // not real register
wire [13:0] q_cb;
wire [ 3:0] q_cn;
wire q_vl [0:13];
wire [ 4:0] q_zc [0:13];
wire [ 8:0] q_bv [0:13];
wire [ 3:0] q_bc [0:13];
// stage R : simple stage (re-ordered signals)
reg r_eof;
reg r_st;
reg r_et;
reg r_vl [0:19];
reg [13:0] r_cb [0:19];
reg [ 3:0] r_cn [0:19];
reg [ 4:0] r_zc [0:19];
reg [ 8:0] r_bv [0:19];
reg [ 3:0] r_bc [0:19];
reg r_on [0:19];
// stage S : simple stage (gathered signals)
reg s_eof;
reg s_et;
reg [13:0] s_cb [0:19];
reg [ 4:0] s_zc [0:19];
reg [ 8:0] s_bv [0:19];
reg [ 3:0] s_bc [0:19];
// stage T : double buffer
reg t_eof;
reg [ 3:0] t_i_ptr;
reg [ 5:0] t_o_ptr;
wire t_empty_n = t_i_ptr != t_o_ptr[5:2];
// stage U : simple stage (output bits and counts (raw))
reg u_eof_n;
reg u_eof;
reg u_e;
reg [13:0] u_cb [0:4];
reg [ 4:0] u_zc [0:4]; // 0~24
reg [ 8:0] u_bv [0:4];
reg [ 3:0] u_bc [0:4]; // 0~13
// stage V : simple stage (output bits and counts (cutted bv))
reg v_eof;
reg v_e;
reg [13:0] v_cb [0:4];
reg [ 4:0] v_zc [0:4]; // 0~24
reg [ 8:0] v_bv [0:4];
reg [ 3:0] v_bc [0:4]; // 0~13
// stage W : simple stage (intra-channel merge)
reg w_eof;
reg [36:0] w_bb [0:4];
reg [ 5:0] w_bn [0:4];
// stage X : simple stage (inter-channel merge)
reg x_eof;
reg[184:0] x_bb;
reg[ 7:0] x_bn;
// stage Y : simple stage
reg y_eof;
reg[407:0] y_bbuf;
reg [ 8:0] y_bcnt;
reg y_e;
reg [63:0] y_data;
// stage Z : simple stage (added .jls header)
reg [ 2:0] z_sof_idx;
reg z_eof_n;
reg z_e;
reg [63:0] z_data;
reg z_last;
integer i, j; // temporary loop variables
//-------------------------------------------------------------------------------------------------------------------
// pipeline stage a: maintain counters: i, j
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk)
if (i_sof) begin
a_e <= 1'b0;
for (i=0; i<5; i=i+1) a_x[i] <= 8'd0;
end else if (i_rdy) begin
a_e <= i_e;
a_x[0] <= i_x0;
a_x[1] <= i_x1;
a_x[2] <= i_x2;
a_x[3] <= i_x3;
a_x[4] <= i_x4;
end
always @ (posedge clk)
if (i_sof) begin
a_ii <= 0;
a_jj <= 0;
end else if (a_e & i_rdy) begin
if (a_ii < rrw)
a_ii <= a_ii + 11'd1;
else begin
a_ii <= 0;
if (a_jj <= {1'b0,rrh})
a_jj <= a_jj + 17'd1;
end
end
//-------------------------------------------------------------------------------------------------------------------
// pipeline stage b: generate fc, lc, fr, eof
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk)
if (i_sof) begin
{b_e, b_fc, b_lc, b_fr, b_eof} <= 0;
for (i=0; i<5; i=i+1) b_x[i] <= 8'd0;
end else if (i_rdy) begin
b_e <= a_e & (a_jj <= {1'b0,rrh});
b_fc <= a_ii == 11'd0;
b_lc <= a_ii == rrw;
b_fr <= a_jj == 17'd0;
b_eof <= a_jj > {1'b0,rrh};
for (i=0; i<5; i=i+1) b_x[i] <= a_x[i];
end
//-------------------------------------------------------------------------------------------------------------------
// pipeline stage c: generate context pixels
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk)
if (i_sof) begin
{c_e, c_fc, c_lc, c_fr, c_eof, c_a, c_ct, c_c} <= 0;
for (i=0; i<5; i=i+1) begin
c_xt[i] <= 0;
c_bt[i] <= 0;
end
end else if (i_rdy) begin
c_e <= b_e;
c_fc <= b_fc;
c_lc <= b_lc;
c_fr <= b_fr;
c_eof <= b_eof;
if (b_e) begin
for (i=0; i<5; i=i+1) begin
c_xt[i] <= b_x[i];
c_bt[i] <= b_fr ? 8'd0 : linebuffer_o[i];
end
c_a <= b_fc ? (b_fr ? 8'd0 : linebuffer_o[0]) : c_xt[4];
if (b_fr) begin
c_ct <= 8'd0;
c_c <= 8'd0;
end else if (b_fc) begin
c_ct <= linebuffer_o[0];
c_c <= c_ct;
end else
c_c <= c_bt[4];
end
end
//-------------------------------------------------------------------------------------------------------------------
// pipeline stage d: generate px, q
//-------------------------------------------------------------------------------------------------------------------
reg runi;
always @ (posedge clk)
if (i_sof) begin
{d_e, d_lc, d_eof, d_runi} <= 0;
for (i=0; i<5; i=i+1) begin
d_rune[i] <= 0;
d_rgar[i] <= 0;
d_rt[i] <= 0;
d_x [i] <= 0;
d_px[i] <= 0;
d_s [i] <= 0;
d_q [i] <= 0;
end
end else if (i_rdy) begin
runi = ~c_fc & d_runi;
d_e <= c_e;
d_lc <= c_lc;
d_eof <= c_eof;
for (i=0; i<5; i=i+1) begin
d_rune[i] <= 0;
d_rt[i] <= 0;
runi = runi | (c_x[i-1] == c_b[i-1] && c_b[i-1] == c_b[i] && c_b[i] == c_b[i+1]);
d_x[i] <= c_x[i];
d_px[i] <= func_predictor(c_x[i-1], c_b[i], c_b[i-1]);
{d_s[i], d_q[i]} <= func_get_q(c_x[i-1], c_b[i], c_b[i-1], c_b[i+1]);
if (runi) begin
runi = c_x[i-1] == c_x[i];
d_rune[i] <= ~runi;
if (~runi) begin
d_rt[i] <= c_x[i-1] == c_b[i];
d_px[i] <= c_b[i];
d_s[i] <= c_x[i-1] > c_b[i];
end
end
d_rgar[i] <= ~runi;
end
if (c_e)
d_runi <= runi;
end
//-------------------------------------------------------------------------------------------------------------------
// pipeline stage e: process run
//-------------------------------------------------------------------------------------------------------------------
reg [13:0] rc; // not real register
reg [ 4:0] ri; // not real register
always @ (posedge clk)
if (i_sof) begin
e_rc <= 0;
e_ri <= 0;
e_e <= 1'b0;
e_eof <= 1'b0;
for (i=0; i<5; i=i+1) begin
e_x [i] <= 0;
e_px[i] <= 0;
e_s [i] <= 0;
e_q [i] <= 0;
e_g [i] <= 4'hF;
e_cb[i] <= 0;
e_cn[i] <= 0;
e_on[i] <= 0;
end
end else if (i_rdy) begin
e_e <= d_e;
e_eof <= d_eof;
for (i=0; i<5; i=i+1) begin
e_x [i] <= d_x [i];
e_px[i] <= d_px[i];
e_s [i] <= d_s [i];
e_on[i] <= 0;
end
if (d_e) begin
rc = e_rc;
ri = e_ri;
for (i=0; i<5; i=i+1) begin
e_cb[i] <= d_rune[i] ? rc : 14'd0;
e_cn[i] <= d_rune[i] ? J_PLUS[ri] : 4'd0;
if (d_rgar[i]) begin
rc = 0;
if (d_rune[i] && ri != 5'd0)
ri = ri - 5'd1;
end else if (rc == J_MASK[ri]) begin
rc = 0;
ri = ri + 5'd1;
e_on[i] <= 1'b1;
end else
rc = rc + 14'd1;
end
if (d_lc && rc != 14'd0)
e_on[4] <= 1'b1;
e_rc <= d_lc ? 14'd0 : rc;
e_ri <= ri;
end
for (i=0; i<5; i=i+1) begin
if (d_eof) begin
e_q[i] <= 0;
e_g[i] <= 4'hF;
end else if (d_rune[i]) begin // case: run mode (end of run)
e_q[i] <= {4'd0, d_rt[i]};
e_g[i] <= 4'd13;
end else if (d_rgar[i]) begin // case: regular mode
e_q[i] <= d_q[i] / 9'd13;
e_g[i] <= d_q[i] % 9'd13;
end else begin // case: not run or regular
e_q[i] <= 0;
e_g[i] <= 4'hF;
end
end
end
//-------------------------------------------------------------------------------------------------------------------
// double buffer write
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk)
if (i_sof) begin
f_eof <= 1'b0;
f_i_ptr <= 0;
end else if (i_rdy) begin
if (e_eof) begin
if (f_i_ptr[1:0] != 2'd0)
f_i_ptr <= f_i_ptr + 4'd1;
else
f_eof <= 1'b1;
end else if (e_e) begin
f_i_ptr <= f_i_ptr + 4'd1;
end
if (e_eof | e_e) begin
for (i=0; i<5; i=i+1) begin
f_x [f_i_ptr[2:0]][i] <= e_x[i];
f_px[f_i_ptr[2:0]][i] <= e_px[i];
f_s [f_i_ptr[2:0]][i] <= e_s[i];
f_q [f_i_ptr[2:0]][i] <= e_q[i];
f_g [f_i_ptr[2:0]][i] <= e_g[i];
f_cb[f_i_ptr[2:0]][i] <= e_cb[i];
f_cn[f_i_ptr[2:0]][i] <= e_cn[i];
f_on[f_i_ptr[2:0]][i] <= e_on[i];
end
end
end
//-------------------------------------------------------------------------------------------------------------------
// double buffer read
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk)
if (i_sof) begin
g_e <= 1'b0;
g_eof <= 1'b0;
f_o_ptr <= 0;
for (i=0; i<20; i=i+1) begin
g_x [i] <= 0;
g_px[i] <= 0;
g_s [i] <= 0;
g_q [i] <= 0;
g_g [i] <= 4'hF;
g_cb[i] <= 0;
g_cn[i] <= 0;
g_on[i] <= 0;
end
end else if (k_rdy) begin
g_e <= f_e;
g_eof <= ~f_e & f_eof;
if (~f_e) begin
for (i=0; i<20; i=i+1) begin
g_x [i] <= 0;
g_px[i] <= 0;
g_s [i] <= 0;
g_q [i] <= 0;
g_g [i] <= 4'hF;
g_cb[i] <= 0;
g_cn[i] <= 0;
g_on[i] <= 0;
end
end else begin
for (i=0; i<4; i=i+1) begin
for (j=0; j<5; j=j+1) begin
g_x [i*5+j] <= f_x [{f_o_ptr[0],i[1:0]}] [j];
g_px[i*5+j] <= f_px[{f_o_ptr[0],i[1:0]}] [j];
g_s [i*5+j] <= f_s [{f_o_ptr[0],i[1:0]}] [j];
g_q [i*5+j] <= f_q [{f_o_ptr[0],i[1:0]}] [j];
g_g [i*5+j] <= f_g [{f_o_ptr[0],i[1:0]}] [j];
g_cb[i*5+j] <= f_cb[{f_o_ptr[0],i[1:0]}] [j];
g_cn[i*5+j] <= f_cn[{f_o_ptr[0],i[1:0]}] [j];
g_on[i*5+j] <= f_on[{f_o_ptr[0],i[1:0]}] [j];
end
end
f_o_ptr <= f_o_ptr + 2'd1;
end
end
//-------------------------------------------------------------------------------------------------------------------
// selecting circuit stage: calculate nptr
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk)
if (i_sof) begin
h_e <= 1'b0;
h_eof <= 1'b0;
for (i=0; i<20; i=i+1) begin
h_x [i] <= 0;
h_px[i] <= 0;
h_s [i] <= 0;
h_q [i] <= 0;
h_g [i] <= 4'hF;
h_cb[i] <= 0;
h_cn[i] <= 0;
h_on[i] <= 0;
h_bitmap[i] <= 0;
end
end else if (k_rdy) begin
h_e <= g_e;
h_eof<= g_eof;
for (i=0; i<20; i=i+1) begin
h_x [i] <= g_x[i];
h_px[i] <= g_px[i];
h_s [i] <= g_s[i];
h_q [i] <= g_q[i];
h_g [i] <= g_g[i];
h_cb[i] <= g_cb[i];
h_cn[i] <= g_cn[i];
h_on[i] <= g_on[i];
for (j=0; j<20; j=j+1)
h_bitmap[i][j] <= ((j<i) && (g_g[i] != 4'hF) && (g_g[i] == g_g[j])) ? 1'd1 : 1'd0;
end
end
//-------------------------------------------------------------------------------------------------------------------
// selecting circuit stage:
//-------------------------------------------------------------------------------------------------------------------
reg [4:0] nptr; // not real register
always @ (posedge clk)
if (i_sof) begin
j_e <= 1'b0;
j_eof <= 1'b0;
for (i=0; i<20; i=i+1) begin
j_x [i] <= 0;
j_px[i] <= 0;
j_s [i] <= 0;
j_q [i] <= 0;
j_g [i] <= 4'hF;
j_cb[i] <= 0;
j_cn[i] <= 0;
j_on[i] <= 0;
j_nptr[i] <= 0;
end
end else if (k_rdy) begin
j_e <= h_e;
j_eof<= h_eof;
for (i=0; i<20; i=i+1) begin
j_x [i] <= h_x[i];
j_px[i] <= h_px[i];
j_s [i] <= h_s[i];
j_q [i] <= h_q[i];
j_g [i] <= h_g[i];
j_cb[i] <= h_cb[i];
j_cn[i] <= h_cn[i];
j_on[i] <= h_on[i];
nptr = 5'd0;
for(j=0; j<20; j=j+1)
nptr = nptr + (h_bitmap[i][j] ? 5'd1 : 5'd0);
j_nptr[i] <= nptr;
end
end
//-------------------------------------------------------------------------------------------------------------------
// selecting circuit stage:
//-------------------------------------------------------------------------------------------------------------------
reg [4:0] nmax; // not real register
always @ (posedge clk)
if (i_sof) begin
k_e <= 1'b0;
k_eof <= 1'b0;
for (i=0; i<20; i=i+1) begin
k_x [i] <= 0;
k_px[i] <= 0;
k_s [i] <= 0;
k_q [i] <= 0;
k_g [i] <= 4'hF;
k_cb[i] <= 0;
k_cn[i] <= 0;
k_on[i] <= 0;
k_nptr[i] <= 0;
end
k_nmax <= 0;
end else if (k_rdy) begin
k_e <= j_e;
k_eof<= j_eof;
nmax = 5'd0;
for (i=0; i<20; i=i+1) begin
k_x [i] <= j_x[i];
k_px[i] <= j_px[i];
k_s [i] <= j_s[i];
k_q [i] <= j_q[i];
k_g [i] <= j_g[i];
k_cb[i] <= j_cb[i];
k_cn[i] <= j_cn[i];
k_on[i] <= j_on[i];
k_nptr[i] <= j_nptr[i];
if (nmax < j_nptr[i])
nmax = j_nptr[i];
end
k_nmax <= nmax;
end
assign k_rdy = k_ncnt >= k_nmax;
always @ (posedge clk) // maintain double buffer output clock counter
if (i_sof)
k_ncnt <= 5'd0;
else if (k_e)
k_ncnt <= k_rdy ? 5'd0 : (k_ncnt + 5'd1);
//-------------------------------------------------------------------------------------------------------------------
// selecting circult output (buffered)
//-------------------------------------------------------------------------------------------------------------------
reg valid_group; // not real register
always @ (posedge clk) begin
l_eof<= k_eof & (~i_sof);
l_st <= k_e & (~i_sof) & (k_ncnt == 5'd0 );
l_et <= k_e & (~i_sof) & (k_ncnt == k_nmax);
for (i=0; i<20; i=i+1) begin
l_x [i] <= k_x[i];
l_px[i] <= k_px[i];
l_s [i] <= k_s[i];
l_q [i] <= k_q[i];
l_on[i] <= i_sof ? 1'b0 : k_on[i];
valid_group = k_e & (k_ncnt == k_nptr[i]) & (~i_sof);
l_g [i] <= valid_group ? k_g[i] : 4'hF;
l_cb[i] <= (valid_group && k_g[i] == 4'd13) ? k_cb[i] : 14'd0;
l_cn[i] <= (valid_group && k_g[i] == 4'd13) ? k_cn[i] : 4'd0;
end
end
//-------------------------------------------------------------------------------------------------------------------
// ordering circult (stage 1: decoders)
//-------------------------------------------------------------------------------------------------------------------
reg [13:0] cb; // not real register
reg [ 3:0] cn; // not real register
reg [ 3:0] gg4; // not real register
always @ (*) begin
cb = 0;
cn = 0;
for (i=0; i<20; i=i+1) begin
cb = (cb | l_cb[i]);
cn = (cn | l_cn[i]);
end
end
always @ (posedge clk) begin
m_eof<= l_eof & (~i_sof);
m_st <= l_st & (~i_sof);
m_et <= l_et & (~i_sof);
m_cb <= cb;
m_cn <= cn;
for (i=0; i<20; i=i+1) begin
m_g [i] <= l_g[i];
m_x [i] <= l_x[i];
m_px[i] <= l_px[i];
m_s [i] <= l_s[i];
m_q [i] <= l_q[i];
m_on[i] <= i_sof ? 1'b0 : l_on[i];
end
for (gg4=4'd0; gg4<=4'd13; gg4=gg4+4'd1) begin // generate 14 decoders
m_adr[gg4] <= 5'h1F;
for (i=19; i>=0; i=i-1)
if (l_g[i] == gg4)
m_adr[gg4] <= i[4:0];
end
end
//-------------------------------------------------------------------------------------------------------------------
// ordering circult (stage 2: muxs)
//-------------------------------------------------------------------------------------------------------------------
always @ (posedge clk) begin
n_eof<= m_eof & (~i_sof);
n_st <= m_st & (~i_sof);
n_et <= m_et & (~i_sof);
for (i=0; i<=13; i=i+1) begin // after adding run mode pipeline, modify to (int i=0; i<=13; i++)
if (m_adr[i] != 5'h1F) begin
n_vl[i] <= 1'b1;
n_x [i] <= m_x [m_adr[i]];
n_px[i] <= m_px[m_adr[i]];
n_s [i] <= m_s [m_adr[i]];
n_q [i] <= m_q [m_adr[i]];
end else begin
n_vl[i] <= 1'b0;
n_x [i] <= 0;
n_px[i] <= 0;