-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathysyx_22000000.v
More file actions
4150 lines (3820 loc) · 146 KB
/
ysyx_22000000.v
File metadata and controls
4150 lines (3820 loc) · 146 KB
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 ysyx_22000000_Adder64(//y turn into ~y outside when to sub
output [63:0] result,
output cout,OF,SF,ZF,CF,
input [63:0] x,y,
input sub
);
/*
wire [31:0] res_l, res_h;
wire c_l;
ysyx_22000000_Adder32 adder_low(res_l, c_l, x[31:0], y[31:0], sub);
ysyx_22000000_Adder32 adder_high(res_h, cout, x[63:32], y[63:32], c_l);
assign result = {res_h, res_l};
*/
assign {cout, result} = {1'b0,x} + {1'b0,y} + {64'b0,sub};
//assign {cout, result} = {1'b1,x} + {1'b1,y} + {64'b11111111111111,sub};
assign OF = (~x[63] & ~y[63] & result[63]) | (x[63] & y[63] & ~result[63]);
assign SF = result[63];
assign ZF = (result == 64'h0000000000000000 ? 1 : 0);
assign CF = cout ^ sub;
endmodule
module ysyx_22000000_FA(
output f,cout,
input x,y,cin
);
assign f= x ^ y ^ cin;
assign cout= (x & y) | (x & cin) | (y & cin);
endmodule
module ysyx_22000000_CLU4(
input [3:0] p,g,
input c0,
output [3:0] c
);
wire c1,c2,c3,c4;
assign c1=g[0] | (p[0] & c0);
assign c2=g[1] | (p[1] & c1);
assign c3=g[2] | (p[2] & c2);
assign c4=g[3] | (p[3] & c3);
assign c = {c4,c3,c2,c1};
endmodule
module ysyx_22000000_CLA4(
output [3:0] f,
output cout,
input [3:0] x,y,
input cin
);
wire [4:1] p,g;
wire [4:1] c;
assign p=x|y;
assign g=x&y;
ysyx_22000000_CLU4 gen(
.c0(cin),
.p(p),
.g(g),
.c(c)
);
genvar i;
wire cout_miss_fa;
ysyx_22000000_FA adder(
.x(x[0]),
.y(y[0]),
.cin(cin),
.f(f[0]),
.cout(cout_miss_fa)
);
wire [3:1] cout_miss;
generate
for (i=1;i<=3;i=i+1) begin
ysyx_22000000_FA adder(
.cout(cout_miss[i]),
.x(x[i]),
.y(y[i]),
.cin(c[i]),
.f(f[i])
);
end
endgenerate
assign cout = c[4];
endmodule
module ysyx_22000000_CLA8(
output [7:0] f,
output cout,
input [7:0] x,y,
input cin
);
wire cin2;
wire [3:0] p,g;
assign p=x[3:0] | y[3:0];
assign g=x[3:0] & y[3:0];
assign cin2=g[3] | (p[3]&g[2]) | (p[3]&p[2]&g[1]) | (p[3]&p[2]&p[1]&g[0]) | (p[3]&p[2]&p[1]&p[0]&cin);
wire cout_miss_l;
ysyx_22000000_CLA4 adder_low(
.x(x[3:0]),
.y(y[3:0]),
.cin(cin),
.f(f[3:0]),
.cout(cout_miss_l)
);
ysyx_22000000_CLA4 adder_high(
.x(x[7:4]),
.y(y[7:4]),
.cin(cin2),
.f(f[7:4]),
.cout(cout)
);
endmodule
module ysyx_22000000_Adder32(
output [31:0] result,
output cout,
input [31:0] x,
input [31:0] y,
input sub
);
//parameter t=32;
wire [3:0] Gg,Pg;
wire [7:0] p[3:0],g[3:0];
wire [4:0] c;
wire [3:0] cout_temp;
genvar i;
generate
for (i=0;i<=3;i=i+1) begin
assign p[i]=x[i*8+7:i*8] | y[i*8+7:i*8];
assign g[i]=x[i*8+7:i*8] & y[i*8+7:i*8];
assign Pg[i] = (p[i]==8'hff ? 1 : 0);
assign Gg[i] = g[i][7] | (p[i][7] & g[i][6]) | (p[i][7] & p[i][6] & g[i][5]) | (p[i][7] & p[i][6] & p[i][5] & g[i][4])
| (p[i][7] & p[i][6] & p[i][5] & p[i][4] & g[i][3]) | (p[i][7] & p[i][6] & p[i][5] & p[i][4] & p[i][3] & g[i][2]) |
(p[i][7] & p[i][6] & p[i][5] & p[i][4] & p[i][3] & p[i][2] & g[i][1]) | (p[i][7] & p[i][6] & p[i][5] & p[i][4] & p[i][3] & p[i][2] & p[i][1] & p[i][0] & g[i][0]);
end
endgenerate
ysyx_22000000_CLU4 gen(
.p(Pg),
.g(Gg),
.c0(sub),
.c(c[4:1])
);
assign c[0]=sub;
wire [7:0] ff[3:0];
generate
for (i=0;i<=3;i=i+1) begin
ysyx_22000000_CLA8 adder8(
.x(x[i*8+7:i*8]),
.y(y[i*8+7:i*8]),
.cin(c[i]),
.cout(cout_temp[i]),
.f(ff[i])
);
end
endgenerate
assign result = {ff[3], ff[2], ff[1], ff[0]};
assign cout = cout_temp[3];
endmodule
module ysyx_22000000_ALU(
input clk,
input rst,
input src_valid,
input mwb_block,
output alu_busy,
input [63:0] inputa, inputb,
input [4:0] ALUOp,
input [1:0] MulOp,
output zero,
output reg [63:0] result
);
wire SUBctr, SIGctr, ALctr, SFTctr, Wctr;
wire [3:0] OPctr;
wire [63:0] adderres;
wire [63:0] res0, res1, res2, res3, res4, res5, res6, res8, res9;
reg [63:0] res7;
///adder,and,or,xor,shift,inputb,cmp
wire [63:0] adderb;
assign adderb = inputb ^ {64{SUBctr}};
wire CF, SF, OF, ZF;
wire cout_miss;
ysyx_22000000_ALUSig alusig(.ALUOp(ALUOp), .SUBctr(SUBctr), .SIGctr(SIGctr), .ALctr(ALctr), .SFTctr(SFTctr), .OPctr(OPctr), .Wctr(Wctr));
ysyx_22000000_Adder64 adder(.result(adderres),.x(inputa),.y(adderb),.sub(SUBctr),.CF(CF),.OF(OF),.SF(SF),.ZF(ZF),.cout(cout_miss));
assign res0 = (Wctr == 1'b0) ? adderres : {{32{adderres[31]}}, adderres[31:0]};
assign res1 = inputa & inputb;
assign res2 = inputa | inputb;
assign res3 = inputa ^ inputb;
wire [31:0] sllWres, srWres, lower32;
assign lower32 = inputa[31:0];
wire [5:0] shamt = inputb[5:0];//not consider the valid bit
wire [4:0] shamtW = inputb[4:0];
wire [63:0] srres, shiftL, shiftR;
//shift
assign sllWres = lower32 << shamtW;
assign srWres = (ALctr == 1'b0) ? lower32 >> shamtW : $signed($signed(lower32) >>> shamtW);
assign shiftL = (Wctr == 1'b0) ? (inputa << shamt) : {{32{sllWres[31]}}, sllWres[31:0]};
assign srres = (ALctr == 1'b0) ? inputa >> shamt : $signed($signed(inputa) >>> shamt);
assign shiftR = (Wctr == 1'b0) ? srres : {{32{srWres[31]}}, srWres[31:0]};
assign res4 = (SFTctr == 1'b0) ? shiftL : shiftR;//not finish ysyx_22000000_Shifter shifter(.dout(res4),.din(inputa),.shamt(shamt),.AL(ALctr),.LR(SFTctr));
assign res5 = inputb;
assign res6 = {{63{1'b0}},{(SIGctr == 1'b1) ? OF ^ SF : CF}}; //cmp,not finish
//mul
wire op_mul = src_valid & ~OPctr[3] & OPctr[2] & OPctr[1] & OPctr[0];//res7
wire [64:0] multiplicand, multiplier;
wire [63:0] result_hi, result_lo;
assign multiplicand = (Wctr == 1'b0) ? {MulOp[1] & inputa[63],inputa} : {{33{inputa[31]}},inputa[31:0]};
assign multiplier = (Wctr == 1'b0) ? {MulOp[1] & MulOp[0] & inputb[63],inputb} : {{33{inputb[31]}},inputb[31:0]};
reg [63:0] result_hi_r, result_lo_r;
reg mul_doing;
wire mul_ready, mul_out_valid, mul_valid;
ysyx_22000000_mulu mulu(
.clk(clk),
.rst(rst),
.multiplicand(multiplicand),
.multiplier(multiplier),
.mul_valid(mul_valid),
.mul_ready(mul_ready),
.out_valid(mul_out_valid),
.result({result_hi,result_lo})
);
reg old_mul;
assign mul_valid = op_mul && !mul_doing && !mul_out_valid && !old_mul;
always @(posedge clk) begin
if(rst || !mwb_block) begin
old_mul <= 1'b0;
end
else if(mul_out_valid && mwb_block) begin
old_mul <= 1'b1;
end
end
always @(posedge clk) begin
if(rst) begin
mul_doing <= 1'b0;
result_hi_r <= 64'b0;
result_lo_r <= 64'b0;
end
/*除法结果输出后需要将div_doing置零*/
else if(mul_out_valid) begin
mul_doing <= 1'b0;
result_hi_r <= result_hi;
result_lo_r <= result_lo;
end
/*握手成功后,也就是除法器接受输入的数据后需要把div_doing置高*/
else if(mul_valid && mul_ready) begin
mul_doing <= 1'b1;
end
end
wire [63:0] rhi, rlo;
assign rhi = (!mwb_block && old_mul) ? result_hi_r : result_hi;
assign rlo = (!mwb_block && old_mul) ? result_lo_r : result_lo;
always @(*)begin
if(MulOp == 2'b00) begin
res7 = (Wctr == 1'b0) ? rlo : {{32{rlo[31]}},rlo[31:0]};
end
else res7 = rhi;
end
/*
reg [127:0] mulres;
wire [63:0] mulresW;
assign mulresW = inputa[31:0] * inputb[31:0];
always@(*) begin
case(MulOp)
2'b00: mulres = {{64{1'b0}},inputa} * {{64{1'b0}},inputb};
2'b01: mulres = {{64{1'b0}},inputa} * {{64{1'b0}},inputb};
2'b10: mulres = $signed($signed({{64{inputa[63]}},inputa}) * {{64{1'b0}},inputb});
default: mulres = $signed($signed({{64{inputa[63]}},inputa}) * $signed({{64{inputb[63]}},inputb}));
endcase
end
always@(*) begin
if(MulOp == 2'b00) begin
res7 = (Wctr == 1'b0) ? mulres[63:0] : {{32{mulresW[31]}},mulresW[31:0]};
end
else res7 = mulres[127:64];
end
*/
//div & rem
wire op_div = src_valid & OPctr[3];
wire [63:0] dividend, divisor, quotient, remainder;
assign dividend = (Wctr == 1'b0) ? inputa : {{32{SIGctr & inputa[31]}},inputa[31:0]};
assign divisor = (Wctr == 1'b0) ? inputb : {{32{SIGctr & inputb[31]}},inputb[31:0]};
reg [63:0] quotient_r, remainder_r;
reg div_doing;
wire div_ready, out_valid, div_valid;
ysyx_22000000_divu divu(
.clk(clk),
.rst(rst),
.dividend(dividend),
.divisor(divisor),
.div_valid(div_valid),
.div_signed(SIGctr),
.flush(1'b0),
.div_ready(div_ready),
.out_valid(out_valid),
.quotient(quotient),
.remainder(remainder)
);
//假设新的op_div = 0
// out_valid = 1 -> 则alu_busy = 0,div_valid = 0,准备保存结果,div_doing准备拉低,新的op_div准备进入
// 该周期结束后应当有新的op_div进入,所以若此时m/wb_block = 1,
// 则下个周期alu_busy = 0,div_valid = 0;
//out_valid = 0 -> 新的op_div进来,保存的结果出去,div_ready拉高, alu_busy = 0, div_valid = 0;
// 若M.WB阻塞,则新的op_div进不来, alu_busy = 1, div_valid = 1;
// 也就是在这个周期开始前enable为0
// ->加入m/wb_block控制。
//如果下个周期新指令进不来,就说明下个周期alu_busy,div_valid都是低
reg old_div;
assign div_valid = op_div && !div_doing && !out_valid && !old_div;
assign alu_busy = (op_div && !out_valid && !old_div) | (op_mul && !mul_out_valid && !old_mul);
always @(posedge clk) begin
if(rst || !mwb_block) begin
old_div <= 1'b0;
end
else if(out_valid && mwb_block) begin
old_div <= 1'b1;
end
end
always @(posedge clk) begin
if(rst) begin
div_doing <= 1'b0;
quotient_r <= 64'b0;
remainder_r <= 64'b0;
end
/*除法结果输出后需要将div_doing置零*/
else if(out_valid) begin
div_doing <= 1'b0;
quotient_r <= quotient;
remainder_r <= remainder;
end
/*握手成功后,也就是除法器接受输入的数据后需要把div_doing置高*/
else if(div_valid && div_ready) begin
div_doing <= 1'b1;
end
end
assign res8 = (!mwb_block && old_div) ? quotient_r : quotient;
assign res9 = (!mwb_block && old_div) ? remainder_r : remainder;
/*
wire [63:0] divres;
wire [31:0] divresW;
assign divres =(OPctr == 4'd8) ? ( (SIGctr == 1'b0) ? inputa / inputb : $signed($signed(inputa) / $signed(inputb))) : 0;
assign divresW =(OPctr == 4'd8) ? ( (SIGctr == 1'b0) ? inputa[31:0] / inputb[31:0] : $signed($signed(inputa[31:0]) / $signed(inputb[31:0]))) : 0;
assign res8 = (Wctr == 1'b0) ? divres : {{32{divresW[31]}}, divresW[31:0]};
wire [63:0] remres;
wire [31:0] remresW;
assign remres =(OPctr == 4'd9) ? ( (SIGctr == 1'b0) ? inputa % inputb : $signed($signed(inputa) % $signed(inputb))) : 0;
assign remresW =(OPctr == 4'd9) ? ( (SIGctr == 1'b0) ? inputa[31:0] % inputb[31:0] : $signed($signed(inputa[31:0]) % $signed(inputb[31:0]))) : 0;
assign res9 = (Wctr == 1'b0) ? remres : {{32{remresW[31]}}, remresW[31:0]};
*/
always@(*) begin
case(OPctr)
0: result = res0;
1: result = res1;
2: result = res2;
3: result = res3;
4: result = res4;
5: result = res5;
6: result = res6;
7: result = res7;
8: result = res8;
default: result = res9;
endcase
end
assign zero = ZF;
endmodule
module ysyx_22000000_ALU_lite(
input [63:0] inputa, inputb,
input [4:0] ALUOp,
output zero,
output reg [63:0] result
);
wire SUBctr, SIGctr, ALctr, SFTctr, Wctr;
wire [3:0] OPctr;
wire [63:0] adderres;
wire [63:0] res0, res6;
wire cout_miss;
///adder,and,or,xor,shift,inputb,cmp
wire [63:0] adderb;
assign adderb = inputb ^ {64{SUBctr}};
wire CF, SF, OF, ZF;
ysyx_22000000_ALUSig alusig(.ALUOp(ALUOp), .SUBctr(SUBctr), .SIGctr(SIGctr), .ALctr(ALctr), .SFTctr(SFTctr), .OPctr(OPctr), .Wctr(Wctr));
ysyx_22000000_Adder64 adder(.result(adderres),.x(inputa),.y(adderb),.sub(SUBctr),.CF(CF),.OF(OF),.SF(SF),.ZF(ZF), .cout(cout_miss));
assign res0 = (Wctr == 1'b0) ? adderres : {{32{adderres[31]}}, adderres[31:0]};
assign res6 = {{63{1'b0}},{(SIGctr == 1'b1) ? OF ^ SF : CF}}; //cmp,not finish
always@(*) begin
case(OPctr)
0: result = res0;
default: result = res6;
endcase
end
assign zero = ZF;
endmodule
module ysyx_22000000_ALUSig(
input [4:0] ALUOp,
output reg SUBctr,SIGctr,ALctr,SFTctr,Wctr,
output reg [3:0] OPctr
);
always@(*) begin
case(ALUOp)
5'b00000: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0000; end
5'b10000: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b0000; end
5'b00001: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0100; end
5'b10001: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b0100; end
5'b00010: begin SUBctr = 1; SIGctr = 1; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0110; end
5'b00011: begin SUBctr = 1; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0110; end
5'b00100: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0011; end
5'b00101: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 1; Wctr = 0; OPctr = 4'b0100; end
5'b10101: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 1; Wctr = 1; OPctr = 4'b0100; end
5'b00110: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0010; end
5'b00111: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0001; end
5'b01000: begin SUBctr = 1; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0000; end
5'b11000: begin SUBctr = 1; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b0000; end
5'b01101: begin SUBctr = 0; SIGctr = 0; ALctr = 1; SFTctr = 1; Wctr = 0; OPctr = 4'b0100; end
5'b11101: begin SUBctr = 0; SIGctr = 0; ALctr = 1; SFTctr = 1; Wctr = 1; OPctr = 4'b0100; end
5'b01111: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0101; end
5'b01001: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0111; end
5'b01010: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b1000; end
5'b01011: begin SUBctr = 0; SIGctr = 1; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b1000; end
5'b01100: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b1001; end
5'b01110: begin SUBctr = 0; SIGctr = 1; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b1001; end
5'b11001: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b0111; end
5'b11010: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b1000; end
5'b11011: begin SUBctr = 0; SIGctr = 1; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b1000; end
5'b11100: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b1001; end
5'b11110: begin SUBctr = 0; SIGctr = 1; ALctr = 0; SFTctr = 0; Wctr = 1; OPctr = 4'b1001; end
default: begin SUBctr = 0; SIGctr = 0; ALctr = 0; SFTctr = 0; Wctr = 0; OPctr = 4'b0000; end
endcase
end
endmodule
module ysyx_22000000_arbiter(
input clk,
input rst,
//icache <-> arbiter
input i_acq,
input [63:0] i_rw_addr_i,
input i_rw_req_i,//
input i_rw_valid_i,
output [127:0] i_data_read_o,//finish burst
output reg i_rw_ready_o,//data_read_i in ram
input [7:0] i_rw_size_o,
input i_rw_dev_o,
input [3:0] i_rw_bytes_o,
//dcache <-> arbiter
input d_acq,
input [63:0] d_rw_addr_i,
input d_rw_req_i,//
input d_rw_valid_i,
input [127:0] d_rw_w_data_i,
output [127:0] d_data_read_o,//finish burst
output reg d_rw_ready_o,//ready to give data or fetch data
input [7:0] d_rw_size_o,
input d_rw_dev_o,
input [3:0] d_rw_bytes_o,
//arbiter<->memory
output reg [63:0] rw_addr_o,
output reg rw_req_o,//
output reg rw_valid_o,
output [127:0] rw_w_data_o,
input [127:0] data_read_i,//finish burst
input rw_ready_i,//data_read_i in ram
output reg [7:0] rw_size_o,
output reg rw_dev_o,
output reg [3:0] rw_bytes_o
);
//wire cache_valid = d_rw_valid_i || i_rw_valid_i;
parameter [1:0] IDLE = 2'b00, ICACHE = 2'b01, DCACHE = 2'b10;
reg [1:0] cur_status, next_status;
always @(posedge clk) begin
if(rst) cur_status <= IDLE;
else cur_status <= next_status;
end
always @(*) begin
case (cur_status)
IDLE: begin
if(d_acq) next_status = DCACHE;
else if(i_acq) next_status = ICACHE;
else next_status = IDLE;
end
DCACHE: begin
if(d_acq) next_status = DCACHE;
else next_status = IDLE;
end
ICACHE: begin
if(i_acq) next_status = ICACHE;
else next_status = IDLE;
end
default: next_status = IDLE;
endcase
end
always @(*) begin
case (cur_status)
DCACHE: begin
rw_addr_o = d_rw_addr_i;
rw_req_o = d_rw_req_i;
rw_valid_o = d_rw_valid_i;
i_rw_ready_o = 1'b0;
d_rw_ready_o = rw_ready_i;
rw_size_o = d_rw_size_o;
rw_dev_o = d_rw_dev_o;
rw_bytes_o = d_rw_bytes_o;
end
ICACHE: begin
rw_addr_o = i_rw_addr_i;
rw_req_o = i_rw_req_i;
rw_valid_o = i_rw_valid_i;
i_rw_ready_o = rw_ready_i;
d_rw_ready_o = 1'b0;
rw_size_o = i_rw_size_o;
rw_dev_o = i_rw_dev_o;
rw_bytes_o = i_rw_bytes_o;
end
default: begin
rw_addr_o = 0;
rw_req_o = 0;
rw_valid_o = 0;
i_rw_ready_o = 1'b0;
d_rw_ready_o = 1'b0;
rw_size_o = 0;
rw_dev_o = 0;
rw_bytes_o = 0;
end
endcase
end
assign i_data_read_o = data_read_i;
assign d_data_read_o = data_read_i;
assign rw_w_data_o = d_rw_w_data_i;
endmodule
// Burst types
`define ysyx_22000000_AXI_BURST_TYPE_FIXED 2'b00 //突发类型 FIFO
`define ysyx_22000000_AXI_BURST_TYPE_INCR 2'b01 //ram
`define ysyx_22000000_AXI_BURST_TYPE_WRAP 2'b10
// Access permissions
`define ysyx_22000000_AXI_PROT_UNPRIVILEGED_ACCESS 3'b000
`define ysyx_22000000_AXI_PROT_PRIVILEGED_ACCESS 3'b001
`define ysyx_22000000_AXI_PROT_SECURE_ACCESS 3'b000
`define ysyx_22000000_AXI_PROT_NON_SECURE_ACCESS 3'b010
`define ysyx_22000000_AXI_PROT_DATA_ACCESS 3'b000
`define ysyx_22000000_AXI_PROT_INSTRUCTION_ACCESS 3'b100
// Memory types (AR)
`define ysyx_22000000_AXI_ARCACHE_DEVICE_NON_BUFFERABLE 4'b0000
`define ysyx_22000000_AXI_ARCACHE_DEVICE_BUFFERABLE 4'b0001
`define ysyx_22000000_AXI_ARCACHE_NORMAL_NON_CACHEABLE_NON_BUFFERABLE 4'b0010
`define ysyx_22000000_AXI_ARCACHE_NORMAL_NON_CACHEABLE_BUFFERABLE 4'b0011
`define ysyx_22000000_AXI_ARCACHE_WRITE_THROUGH_NO_ALLOCATE 4'b1010
`define ysyx_22000000_AXI_ARCACHE_WRITE_THROUGH_READ_ALLOCATE 4'b1110
`define ysyx_22000000_AXI_ARCACHE_WRITE_THROUGH_WRITE_ALLOCATE 4'b1010
`define ysyx_22000000_AXI_ARCACHE_WRITE_THROUGH_READ_AND_WRITE_ALLOCATE 4'b1110
`define ysyx_22000000_AXI_ARCACHE_WRITE_BACK_NO_ALLOCATE 4'b1011
`define ysyx_22000000_AXI_ARCACHE_WRITE_BACK_READ_ALLOCATE 4'b1111
`define ysyx_22000000_AXI_ARCACHE_WRITE_BACK_WRITE_ALLOCATE 4'b1011
`define ysyx_22000000_AXI_ARCACHE_WRITE_BACK_READ_AND_WRITE_ALLOCATE 4'b1111
// Memory types (AW)
`define ysyx_22000000_AXI_AWCACHE_DEVICE_NON_BUFFERABLE 4'b0000
`define ysyx_22000000_AXI_AWCACHE_DEVICE_BUFFERABLE 4'b0001
`define ysyx_22000000_AXI_AWCACHE_NORMAL_NON_CACHEABLE_NON_BUFFERABLE 4'b0010
`define ysyx_22000000_AXI_AWCACHE_NORMAL_NON_CACHEABLE_BUFFERABLE 4'b0011
`define ysyx_22000000_AXI_AWCACHE_WRITE_THROUGH_NO_ALLOCATE 4'b0110
`define ysyx_22000000_AXI_AWCACHE_WRITE_THROUGH_READ_ALLOCATE 4'b0110
`define ysyx_22000000_AXI_AWCACHE_WRITE_THROUGH_WRITE_ALLOCATE 4'b1110
`define ysyx_22000000_AXI_AWCACHE_WRITE_THROUGH_READ_AND_WRITE_ALLOCATE 4'b1110
`define ysyx_22000000_AXI_AWCACHE_WRITE_BACK_NO_ALLOCATE 4'b0111
`define ysyx_22000000_AXI_AWCACHE_WRITE_BACK_READ_ALLOCATE 4'b0111
`define ysyx_22000000_AXI_AWCACHE_WRITE_BACK_WRITE_ALLOCATE 4'b1111
`define ysyx_22000000_AXI_AWCACHE_WRITE_BACK_READ_AND_WRITE_ALLOCATE 4'b1111
`define ysyx_22000000_AXI_SIZE_BYTES_1 3'b000 //突发宽度一个数据的宽度
`define ysyx_22000000_AXI_SIZE_BYTES_2 3'b001
`define ysyx_22000000_AXI_SIZE_BYTES_4 3'b010
`define ysyx_22000000_AXI_SIZE_BYTES_8 3'b011
`define ysyx_22000000_AXI_SIZE_BYTES_16 3'b100
`define ysyx_22000000_AXI_SIZE_BYTES_32 3'b101
`define ysyx_22000000_AXI_SIZE_BYTES_64 3'b110
`define ysyx_22000000_AXI_SIZE_BYTES_128 3'b111
module ysyx_22000000_axi_rw # (
parameter RW_DATA_WIDTH = 128,
parameter RW_ADDR_WIDTH = 64,
parameter AXI_DATA_WIDTH = 64,
parameter AXI_ADDR_WIDTH = 32,
parameter AXI_ID_WIDTH = 4,
parameter AXI_STRB_WIDTH = AXI_DATA_WIDTH/8
)(
input clock,
input reset,
input rw_req_i, //IF&MEM输入信号
input rw_valid_i, //IF&MEM输入信号
output rw_ready_o, //IF&MEM输入信号
output reg [RW_DATA_WIDTH-1:0] data_read_o, //IF&MEM输入信号
input [RW_DATA_WIDTH-1:0] rw_w_data_i, //IF&MEM输入信号
input [RW_ADDR_WIDTH-1:0] rw_addr_i, //IF&MEM输入信号
input [AXI_STRB_WIDTH - 1:0] rw_size_i, //IF&MEM输入信号
input rw_dev_i,
input [3:0] rw_bytes_i,
// Advanced eXtensible Interface
input axi_aw_ready_i,
output axi_aw_valid_o,
output [AXI_ADDR_WIDTH-1:0] axi_aw_addr_o,
output [AXI_ID_WIDTH-1:0] axi_aw_id_o,
output [7:0] axi_aw_len_o,
output [2:0] axi_aw_size_o,
output [1:0] axi_aw_burst_o,
input axi_w_ready_i,
output axi_w_valid_o,
output [AXI_DATA_WIDTH-1:0] axi_w_data_o,
output [AXI_DATA_WIDTH/8-1:0] axi_w_strb_o,
output axi_w_last_o,
output axi_b_ready_o,
input axi_b_valid_i,
input [1:0] axi_b_resp_i,
input [AXI_ID_WIDTH-1:0] axi_b_id_i,
input axi_ar_ready_i,
output axi_ar_valid_o,
output [AXI_ADDR_WIDTH-1:0] axi_ar_addr_o,
output [AXI_ID_WIDTH-1:0] axi_ar_id_o,
output [7:0] axi_ar_len_o,
output [2:0] axi_ar_size_o,
output [1:0] axi_ar_burst_o,
output axi_r_ready_o,
input axi_r_valid_i,
input [1:0] axi_r_resp_i,
input [AXI_DATA_WIDTH-1:0] axi_r_data_i,
input axi_r_last_i,
input [AXI_ID_WIDTH-1:0] axi_r_id_i
);
// ------------------State Machine------------------TODO
localparam [2:0] W_IDLE = 3'b000, W_WRITE = 3'b010, W_RESP = 3'b011, W_DONE = 3'b100;
localparam [2:0] R_IDLE = 3'b000, R_ADDR = 3'b001, R_READ = 3'b010, R_DONE = 3'b100;
reg [2:0] w_status, r_status;
wire r_state_idle = (r_status == R_IDLE);
wire r_state_addr = (r_status == R_ADDR);
wire r_state_read = (r_status == R_READ);
wire w_state_idle = (w_status == W_IDLE);
wire w_state_resp = (w_status == W_RESP);
// 写通道状态切换
//握手信号
wire aw_fire = axi_aw_ready_i & axi_aw_valid_o;
wire w_fire = axi_w_ready_i & axi_w_valid_o;
wire b_fire = axi_b_ready_o & axi_b_valid_i;
wire w_last = w_fire & axi_w_last_o;//now is trans the last
reg aw_valid_r, w_valid_r;
always @(posedge clock) begin
if(reset) aw_valid_r <= 1'b0;
else if(aw_fire) aw_valid_r <= 1'b0;
else if(w_state_idle && rw_valid_i && rw_req_i) aw_valid_r <= 1'b1;
end
always @(posedge clock) begin
if(reset) w_valid_r <= 1'b0;
else if(w_last) w_valid_r <= 1'b0;
else if(w_state_idle && rw_valid_i && rw_req_i) w_valid_r <= 1'b1;
end
always @(posedge clock) begin
if(reset) begin
w_status <= W_IDLE;
end
else begin
if((rw_valid_i == 1'b1) && (rw_req_i == 1'b1)) begin//now write
case(w_status)
W_IDLE: w_status <= W_WRITE;
W_WRITE: begin
if(w_last) w_status <= W_RESP;
else w_status <= W_WRITE;
end
W_RESP: begin
if(b_fire) w_status <= W_DONE;//wait valid down
else w_status <= W_RESP;
end
W_DONE: w_status <= W_IDLE;
default: w_status <= W_IDLE;
endcase
end
end
end
// 读通道状态切换
//握手信号
wire ar_fire = axi_ar_ready_i & axi_ar_valid_o;
wire r_fire = axi_r_ready_o & axi_r_valid_i;
wire r_last = r_fire & axi_r_last_i;
always @(posedge clock) begin
if(reset) begin
r_status <= R_IDLE;
end
else begin
if((rw_valid_i == 1'b1) && (rw_req_i == 1'b0)) begin//now read
case(r_status)
R_IDLE: r_status <= R_ADDR;
R_ADDR: begin
if(ar_fire) r_status <= R_READ;
else r_status <= R_ADDR;
end
R_READ: begin
if(r_last) r_status <= R_DONE;//wait valid down
else r_status <= R_READ;
end
R_DONE: r_status <= R_IDLE;
default: r_status <= R_IDLE;
endcase
end
end
end
reg rw_ready_r;
always @(posedge clock) begin
if(reset) begin
rw_ready_r <= 1'b0;
end
else if(((rw_req_i == 1'b1) && b_fire) || ((rw_req_i == 1'b0) && r_last)) begin
rw_ready_r <= 1'b1;
end
else rw_ready_r <= 1'b0;
end
assign rw_ready_o = rw_ready_r;
/////////burst number////////
wire [7:0] axi_len;
reg [7:0] rcnt,wcnt;
always @(posedge clock) begin
if(reset || ((rw_req_i == 1'b0) && (r_state_idle == 1'b1))) begin
rcnt <= 0;
end
else if((rcnt != axi_len) && r_fire) begin
rcnt <= rcnt + 1;
end
end
always @(posedge clock) begin
if(reset || (rw_req_i && w_state_idle)) begin
wcnt <= 0;
end
else if((wcnt != axi_len) && w_fire) begin
wcnt <= wcnt + 1;
end
end
// ------------------Write Transaction------------------
localparam AXI_SIZE = $clog2(AXI_DATA_WIDTH / 8);
reg [2:0] dev_size;
always @(*)begin
case(rw_bytes_i)
4'h1: dev_size = 0;
4'h2: dev_size = 1;
4'h4: dev_size = 2;
default dev_size = 2;
endcase
end
wire [AXI_ID_WIDTH-1:0] axi_id = {AXI_ID_WIDTH{1'b0}};
assign axi_len = (rw_dev_i == 1'b0) ? 1 : 0;
wire [2:0] axi_size = (rw_dev_i == 1'b0) ? AXI_SIZE[2:0] : dev_size;
// 写地址通道 以下没有备注初始化信号的都可能是你需要产生和用到的
assign axi_aw_valid_o = aw_valid_r;//
assign axi_aw_addr_o = rw_addr_i[31:0];//
assign axi_aw_id_o = axi_id; //初始化信号即可
assign axi_aw_len_o = axi_len;//
assign axi_aw_size_o = axi_size;//
assign axi_aw_burst_o = `ysyx_22000000_AXI_BURST_TYPE_INCR;//
// 写数据通道
/*
reg [AXI_DATA_WIDTH - 1: 0] rw_w_data_r;
reg [AXI_DATA_WIDTH/8-1:0] rw_size_r;
always @(posedge clock) begin
if(reset) begin
rw_w_data_r <= 0;
rw_size_r <= 0;
end
else if(w_state_idle && rw_valid_i && rw_req_i) begin//写地址时就开始存待写的值,这样写数据时就赶上了
rw_w_data_r <= rw_w_data_i[wcnt*AXI_DATA_WIDTH +: AXI_DATA_WIDTH];
rw_size_r <= rw_size_i;
end
end
*/
/*
写地址:
如果突发传2个8字节:
握手时的上升沿之后-> wcnt=1,rw_w_data_r写入
如果传1个8字节:
握手时上升沿之后->wcnt=0,rw_w_data_r写入
写数据:
如果突发传2个8字节:
握手时上升沿之后->wcnt=1,第二个rw_w_data_r写入,w_last拉高。
突发传1个8字节:
握手时需要w_last是高的。
*/
/*
reg axi_w_last_r;
always @(posedge clock) begin
if(reset) begin
axi_w_last_r <= 0;
end
else if(w_fire && (wcnt == axi_len)) begin//写数据且达到最后一个
axi_w_last_r <= 1'b1;
end
else if(b_fire) begin
axi_w_last_r <= 0;
end
end
*/
assign axi_w_valid_o = w_valid_r;//
assign axi_w_data_o = rw_w_data_i[wcnt*AXI_DATA_WIDTH +: AXI_DATA_WIDTH];//
assign axi_w_strb_o = rw_size_i;//
assign axi_w_last_o = (wcnt == axi_len);//
// 写应答通道
assign axi_b_ready_o = w_state_resp;//
// ------------------Read Transaction------------------
// Read address channel signals
assign axi_ar_valid_o = r_state_addr;//
assign axi_ar_addr_o = rw_addr_i[31:0];//
assign axi_ar_id_o = axi_id; //初始化信号即可
assign axi_ar_len_o = axi_len;//
assign axi_ar_size_o = axi_size;//
assign axi_ar_burst_o = `ysyx_22000000_AXI_BURST_TYPE_INCR;//
// Read data channel signals
//low 64
always @(posedge clock) begin
if(reset) begin
data_read_o[AXI_DATA_WIDTH - 1:0] <= 0;
end
else if(r_fire && (rcnt == 0)) begin//r_trans0
data_read_o[AXI_DATA_WIDTH - 1:0] <= axi_r_data_i;
end
end
//high 64
always @(posedge clock) begin
if(reset) begin
data_read_o[2*AXI_DATA_WIDTH - 1:AXI_DATA_WIDTH] <= 0;
end
else if(r_fire && (rcnt == 1)) begin//r_trans1
data_read_o[2*AXI_DATA_WIDTH - 1:AXI_DATA_WIDTH] <= axi_r_data_i;
end
end
assign axi_r_ready_o = r_state_read;//
endmodule
module ysyx_22000000_icache(
input clk,
input rst,
//cpu<->cache
input [63:0] cpu_req_addr,
input cpu_req_valid,
output reg [63:0] cpu_data_read,
output reg cpu_ready,
output cache_idle,
//cache<->memory
output reg [63:0] rw_addr_o,
output reg rw_req_o,//
output reg rw_valid_o,
input [127:0] data_read_i,//finish burst
input rw_ready_i,//data_read_i in ram
input cpu_dev,
output[5:0] io_sram0_addr,
output io_sram0_cen,
output io_sram0_wen,
output[127:0] io_sram0_wdata,
input[127:0] io_sram0_rdata,
output[5:0] io_sram1_addr,
output io_sram1_cen,
output io_sram1_wen,
output[127:0] io_sram1_wdata,
input[127:0] io_sram1_rdata,
output[5:0] io_sram2_addr,
output io_sram2_cen,
output io_sram2_wen,
output[127:0] io_sram2_wdata,
input[127:0] io_sram2_rdata,
output[5:0] io_sram3_addr,
output io_sram3_cen,
output io_sram3_wen,
output[127:0] io_sram3_wdata,
input[127:0] io_sram3_rdata
);
parameter nline = 256;
reg V [0:nline - 1];
reg [19:0] tag [0:nline - 1];
wire [7:0] cpu_index;
wire [3:0] cpu_offset;
wire [19:0] cpu_tag;
assign cpu_offset = cpu_req_addr[3:0];
assign cpu_index = cpu_req_addr[11:4];
assign cpu_tag = cpu_req_addr[31:12];
wire hit;
//status transform
parameter [2:0] IDLE = 3'b000, CompareTag = 3'b001, Allocate = 3'b010, Readin = 3'b011, DEV = 3'b100;
reg [2:0] cur_status, next_status;
always @(posedge clk) begin
if(rst) cur_status <= IDLE;
else cur_status <= next_status;
end
assign cache_idle = (cur_status == IDLE);
always @(*) begin
case (cur_status)
IDLE: begin
if(cpu_req_valid) begin
if(cpu_dev) next_status = DEV;
else next_status = CompareTag;
end
else next_status = IDLE;
end
CompareTag: begin
if(hit) next_status = Readin;
else next_status = Allocate;
end
Allocate: begin
if(rw_ready_i) begin
next_status = CompareTag;
end
else next_status = Allocate;
end
Readin: next_status = IDLE;//RETN;
DEV: begin
if(rw_ready_i) begin
next_status = IDLE;
end
else next_status = DEV;
end
default: next_status = IDLE;
endcase
end
//cache line
wire [127:0] line_o [0:3];