-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgodson_alu_module.v
3157 lines (2586 loc) · 122 KB
/
godson_alu_module.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
/*------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Copyright (c) 2016, Loongson Technology Corporation Limited.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of Loongson Technology Corporation Limited nor the names of
its contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL LOONGSON TECHNOLOGY CORPORATION LIMITED BE LIABLE
TO ANY PARTY FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
`include "global.h"
`include "bus.h"
`include "reg.h"
module godson_alu_module_nomul(
clock,reset,commitbus_ex,
alurs_to_alu,
// HAVE_DSP_UNIT
DSPCtl_value,
src3_to_alu,
alures,
alures_jalr_target,
brbus_to_fu
);
input clock;
input reset;
input commitbus_ex;
input [84:0] alurs_to_alu;
input [10:0] src3_to_alu;
input [6:0] brbus_to_fu;
//HAVE_DSP_UNIT
input [31:0] DSPCtl_value;
output [121:0] alures;
output [31:0] alures_jalr_target;
//inputs from alurs
wire [2:0] op_ac =alurs_to_alu[84:82];
wire [2:0] brqid =alurs_to_alu[81:79];
wire valid =alurs_to_alu[78];
wire [7:0] op =alurs_to_alu[77:70];
wire [1:0] ac =alurs_to_alu[69:68];
wire [3:0] qid =alurs_to_alu[67:64];
wire [31:0] vj =alurs_to_alu[63:32];
wire [31:0] vk =alurs_to_alu[31:0];
//HAVE_DSP_UNIT
wire write_dspctl = op==`OP_EXTPDP || op==`OP_MTHLIP || op==`OP_ADDSC || op==`OP_EXTP ||
op==`OP_ADDQ || op==`OP_ADDQ_S || op==`OP_SUBQ || op==`OP_SUBQ_S || op==`OP_ABSQ_S ||
op==`OP_PRECRQU_S_QB_PH || op==`OP_ADDWC || op==`OP_PRECRQ_RS_PH_W || op==`OP_SHLLV ||
op==`OP_SHLLVS || op==`OP_EXTR_W || op==`OP_EXTR_R_W || op==`OP_EXTR_RS_W ||
op==`OP_EXTR_S_H ||
op==`OP_DPAU_H_QBL || op==`OP_DPAQ_S_W_PH || op==`OP_DPAU_H_QBR || op==`OP_DPAQ_SA_L_W ||
op==`OP_DPSQ_S_W_PH || op==`OP_MAQ_S_W_PHL || op==`OP_DPSQ_SA_L_W || op==`OP_MAQ_S_W_PHR ||
op==`OP_MAQ_SA_W_PHL || op==`OP_MULEU_S_PH_QBL || op==`OP_MAQ_SA_W_PHR ||
op==`OP_MULEU_S_PH_QBR || op==`OP_MULEQ_S_W_PHL ||op==`OP_MULSAQ_S_W_PH ||
op==`OP_MULEQ_S_W_PHR || op==`OP_MULQ_RS_PH ||
op==`OP_CMP_EQ || op==`OP_CMP_LT || op==`OP_CMP_LE || op==`OP_WRDSP;
wire brbus_brerr;
wire [5:0] brbus_brmask;
assign brbus_brerr = brbus_to_fu[0];
assign brbus_brmask = brbus_to_fu[6:1];
//internal wires
wire alures_valid;
wire [3:0] alures_qid;
wire [31:0] alures_value;
wire alures_ov;
wire alures_trap;
wire alures_bnt;
wire alures_con_true;
wire alu_valid;
assign alu_valid = valid ;
wire [31:0] alu_res;
reg [9:0] src3;
always @(posedge clock)
if (reset)
src3 <= 10'b0;
else if (src3_to_alu[10])
src3 <= src3_to_alu[9:0];
godson_alu alu_0(
.a(vj[31:0]), .b(vk), .c(src3),.op(op[7:0]),
.out(alu_res),.jalr_target(alures_jalr_target),
.ov(alures_ov), .trap(alures_trap), .bnt(alures_bnt), .condition_true(alures_con_true)) ;
/////dsp///////////////
wire [31:0] dsp_result;
wire [5:0] pos_out_dsp;
wire [5:0] scount_out;
wire carry_out;
wire efi_out_dsp;
wire [7:0] outflag_out_dsp;
wire [3:0] ccond_out;
wire dsp_bnt;
wire[31:0] dspres_DSPCtl;
wire [7:0] outflag_out;
wire [5:0] pos_out;
wire efi_out;
//HAVE_DSP_UNIT
godson_dsp dsp_alu_0 (.a(vj), .b(vk), .ac(ac),.op(op), .op_ac(op_ac),
.pos_in(DSPCtl_value[5:0]),.scount_in(DSPCtl_value[12:7]),.carry_in(DSPCtl_value[13]),
.efi_in(DSPCtl_value[14]),
.outflag_in(DSPCtl_value[23:16]),.ccond_in(DSPCtl_value[27:24]),
.result(dsp_result), .pos_out(pos_out_dsp),.scount_out(scount_out),
.carry_out(carry_out),.efi_out(efi_out_dsp), .outflag_out(outflag_out_dsp),
.ccond_out(ccond_out),.dsp_bnt(dsp_bnt));
assign outflag_out = {8{alu_valid}} & outflag_out_dsp;
assign dspres_DSPCtl = {4'b0,ccond_out,outflag_out,1'b0,efi_out_dsp,carry_out,scount_out,1'b0,pos_out_dsp};
assign alures_value = alu_valid &~(op[7:6]==2'b11)? alu_res : dsp_result;
assign alures_valid = alu_valid;
assign alures_qid = qid;
wire block_end;
BLOCK_END block_end_0 (.op(op), .end_op(block_end));
assign alures[121] = write_dspctl;
assign alures[120] = block_end;
assign alures[119:88] = dspres_DSPCtl;
//HAVE_DSP_UNIT
assign alures[86] = alu_valid && (alures_bnt || dsp_bnt);
assign alures[87] = alures_con_true &alu_valid;
assign alures[85] = alures_valid;
assign alures[84:81] = alures_qid;
assign alures[80:17] = {32'b0, alures_value};
assign alures[16] = alu_valid && alures_ov;
assign alures[15] = alu_valid && alures_trap;
assign alures[14] = 1'b0;
assign alures[13] = 1'b0;
assign alures[12] = 1'b0;
assign alures[11] = 1'b0;
assign alures[10] = 1'b0;
assign alures[9] = 1'b0;
assign alures[8] = 1'b0;
assign alures[7] = 1'b0;
assign alures[6] = 1'b0;
assign alures[5:0] = 6'b0;
endmodule
module godson_alu_module(clock,reset,commitbus_ex,
alurs_to_alu,
//HAVE_DSP_UNIT
DSPCtl_value,
src3_to_alu,
alures,
allow,
brbus_to_fu,acc_write_ok,acc_qid
,alures_jalr_target, alu_res
,alu2rsfull_tmp
);
input clock;
input reset;
input commitbus_ex;
input [87:0] alurs_to_alu;
input [10:0] src3_to_alu;
input [6:0] brbus_to_fu;
input acc_write_ok;
//HAVE_DSP_UNIT
input [31:0] DSPCtl_value;
output [122:0] alures;
output [31:0] alu_res;
output [31:0] alures_jalr_target;
output allow;
output [3:0] acc_qid;
output alu2rsfull_tmp;
//inputs from alurs
wire [2:0] op_ac =alurs_to_alu[87:85];
wire [2:0] opreg =alurs_to_alu[84:82];
wire [2:0] brqid =alurs_to_alu[81:79];
wire valid =alurs_to_alu[78];
wire [7:0] op =alurs_to_alu[77:70];
wire [1:0] ac =alurs_to_alu[69:68];
wire [3:0] qid =alurs_to_alu[67:64];
wire [31:0] vj =alurs_to_alu[63:32];
wire [31:0] vk =alurs_to_alu[31:0];
wire brbus_brerr;
wire [5:0] brbus_brmask;
assign brbus_brerr = brbus_to_fu[0];
assign brbus_brmask = brbus_to_fu[6:1];
//internal wires
wire alures_valid;
wire [3:0] alures_qid;
wire [31:0] alures_value;
wire alures_ov;
wire alures_trap;
wire alures_bnt;
wire alures_con_true;
wire alu_valid,mul_valid,div_valid ;
assign mul_valid = valid & ((op==`OP_MULT)||(op==`OP_MULTU)||(op==`OP_MUL)||
(op==`OP_MADD)||(op==`OP_MADDU)||(op==`OP_MSUB)||(op==`OP_MSUBU) ||
//HAVE_DSP_UNIT
(op==`OP_MULEU_S_PH_QBL) || (op == `OP_MULEU_S_PH_QBR)|| (op == `OP_MULQ_RS_PH)||
(op==`OP_MULEQ_S_W_PHL) || (op == `OP_MULEQ_S_W_PHR)|| (op == `OP_MULSAQ_S_W_PH)||
(op==`OP_MAQ_S_W_PHL) || (op == `OP_MAQ_S_W_PHR)|| (op == `OP_MAQ_SA_W_PHL)||
(op==`OP_MAQ_SA_W_PHR) || (op == `OP_DPAU_H_QBL)|| (op == `OP_DPAU_H_QBR)||
(op==`OP_DPAQ_S_W_PH) || (op == `OP_DPAQ_SA_L_W)|| (op == `OP_DPSU_H_QBL)||
(op==`OP_DPSU_H_QBR) || (op == `OP_DPSQ_S_W_PH)|| (op == `OP_DPSQ_SA_L_W) ||
(op==`OP_SHILO) || (op==`OP_MTHLIP)||
(op==`OP_EXTP) || (op==`OP_EXTPDP) || (op==`OP_EXTR_W) || (op==`OP_EXTR_R_W) ||
(op==`OP_EXTR_RS_W) || (op==`OP_EXTR_S_H)||
(op==`OP_MFHI) || (op==`OP_MFLO) || (op==`OP_MTHI) || (op==`OP_MTLO));
assign div_valid = valid & ((op==`OP_DIV)||(op==`OP_DIVU));
assign alu_valid = valid & !mul_valid & !div_valid;
wire [3:0]acc_qid_mul, acc_qid_div;
wire mul_op_valid;
assign acc_qid = mul_op_valid ? acc_qid_mul : acc_qid_div;
reg [9:0] src3;
always @(posedge clock)
if (reset)
src3 <= 10'b0;
else if (src3_to_alu[10])
src3 <= src3_to_alu[9:0];
wire [31:0]alu_res_tmp;
godson_alu alu_1(
.a(vj[31:0]), .b(vk), .c(src3),.op(op[7:0]),
.out(alu_res_tmp), .jalr_target(alures_jalr_target),
.ov(alures_ov), .trap(alures_trap), .bnt(alures_bnt), .condition_true(alures_con_true)) ;
/////dsp///////////////
wire [31:0] dsp_result;
wire [5:0] pos_out_dsp;
wire [5:0] pos_out;
wire [5:0] scount_out;
wire carry_out;
wire efi_out_dsp;
wire efi_out;
wire [7:0] outflag_out_dsp;
wire [3:0] ccond_out;
wire dsp_bnt;
wire[31:0] dspres_DSPCtl;
wire [7:0] outflag_out;
//HAVE_DSP_UNIT
godson_dsp dsp_alu_1 (.a(vj), .b(vk), .ac(ac),.op(op), .op_ac(op_ac),
.pos_in(DSPCtl_value[5:0]),.scount_in(DSPCtl_value[12:7]),.carry_in(DSPCtl_value[13]),
.efi_in(DSPCtl_value[14]),
.outflag_in(DSPCtl_value[23:16]),.ccond_in(DSPCtl_value[27:24]),
.result(dsp_result), .pos_out(pos_out_dsp),.scount_out(scount_out),
.carry_out(carry_out),.efi_out(efi_out_dsp), .outflag_out(outflag_out_dsp),
.ccond_out(ccond_out),.dsp_bnt(dsp_bnt));
/***************mul*************/
wire [63:0] mul_res;
wire [7:0] mulres_op;
wire [7:0] outflag_out_mul;
wire [5:0] pos_out_mul;
wire efi_out_mul;
wire div_allow;
wire [66:0] div_res;
wire [55:0] div_res_h;
//HAVE_DSP_UNIT
wire op_in = ((op==`OP_DPAQ_SA_L_W) | (op==`OP_DPSQ_SA_L_W)) ? 1'b0 : op[0];
wire valid_p1, validreg0, not_write_acc_op;
godson_mul mul_0( .clock(clock), .reset(reset), .commitbus_ex(commitbus_ex),.opreg(opreg),
.vj(vj[31:0]), .vk(vk), .ac(ac), .qid(qid), .op(op_in), .mulin_op(op), .valid(mul_valid),
.mulres(mul_res), .mulres_op(mulres_op),
.mulbusrep_h(!alu_valid), .mulbusrep(!alu_valid),
.brbus_brerr(brbus_brerr), .brbus_brmask(brbus_brmask), .acc_write_ok(acc_write_ok), .acc_qid(acc_qid_mul),
.divres(div_res), .divres_h(div_res_h),
//HAVE_DSP_UNIT
.pos_in(DSPCtl_value[5:0]), .efi_in(DSPCtl_value[14]),
.pos_out(pos_out_mul), .efi_out(efi_out_mul), .outflag_in(DSPCtl_value[23:16]), .outflag_out(outflag_out_mul),
.mul_op_valid(mul_op_valid),.brqid(brqid),
.valid_p1_tmp(valid_p1), .validreg0_tmp(validreg0), .not_write_acc_op(not_write_acc_op));
godson_div div_1(.clock(clock), .reset(reset), .commitbus_ex(commitbus_ex), .vj(vj[31:0]),
.vk(vk), .qid(qid), .op(op), .valid(div_valid),
.divres_h(div_res_h), .divres(div_res), .divbusrep_h(!(alu_valid|mul_res[54])), .divbusrep(!(alu_valid|mul_res[54])),
.allow(div_allow), .brbus_brerr(brbus_brerr), .brbus_brmask(brbus_brmask),.brqid(brqid),.acc_write_ok(acc_write_ok), .acc_qid(acc_qid_div));
//HAVE_DSP_UNIT
assign outflag_out = alu_valid ? outflag_out_dsp : outflag_out_mul;
assign pos_out = alu_valid ? pos_out_dsp : pos_out_mul;
assign efi_out = alu_valid ? efi_out_dsp : efi_out_mul;
assign dspres_DSPCtl = {4'b0,ccond_out,outflag_out,1'b0,efi_out,carry_out,scount_out,1'b0,pos_out};
assign alures_value = alu_valid &~(op[7:6]==2'b11)? alu_res_tmp:
alu_valid ? dsp_result: mul_res[48:17];
assign alu_res = (op[7:6]==2'b00) ? alu_res_tmp : dsp_result;
assign alures_valid = alu_valid | mul_res[54] & not_write_acc_op | (mul_res[54] | div_res[54]) & acc_write_ok;
assign alures_qid = alu_valid? qid : mul_res[55]? mul_res[53:50] : div_res[53:50];
wire [7:0] alures_op = alu_valid? op : mul_res[55]? mul_res[63:56] : div_res[63:56];
// HAVE_DSP_UNIT
wire dsp_acc_op = (alures_op==`OP_MAQ_SA_W_PHL) ||
(alures_op==`OP_MAQ_SA_W_PHR) ||(alures_op==`OP_MAQ_S_W_PHL)||(alures_op==`OP_MAQ_S_W_PHR)||
(alures_op==`OP_MULSAQ_S_W_PH)||(alures_op==`OP_DPAU_H_QBL) ||(alures_op==`OP_DPAU_H_QBR) ||
(alures_op==`OP_DPAQ_S_W_PH) ||(alures_op==`OP_DPAQ_SA_L_W)||(alures_op==`OP_DPSU_H_QBL) ||
(alures_op==`OP_DPSU_H_QBR) ||(alures_op==`OP_DPSQ_S_W_PH)||(alures_op==`OP_DPSQ_SA_L_W)||
(alures_op==`OP_SHILO) ||(alures_op==`OP_MTHLIP);
wire fix_acc_op =(alures_op==`OP_MULT)||(alures_op==`OP_MULTU)||(alures_op==`OP_MADD)||(alures_op==`OP_MADDU)||
(alures_op==`OP_MSUB)||(alures_op==`OP_MSUBU)||(alures_op==`OP_DIV) ||(alures_op==`OP_DIVU) ||
(alures_op==`OP_MTHI)||(alures_op==`OP_MTLO);
wire alures_acc_op = dsp_acc_op || fix_acc_op;
wire block_end;
BLOCK_END block_end_0 (.op(alures_op), .end_op(block_end));
// HAVE_DSP_UNIT
wire write_dspctl = alures_op==`OP_EXTPDP || alures_op==`OP_MTHLIP || alures_op==`OP_ADDSC || alures_op==`OP_EXTP ||
alures_op==`OP_EXTPDP || alures_op==`OP_ADDQ || alures_op==`OP_ADDQ_S || alures_op==`OP_SUBQ ||
alures_op==`OP_SUBQ_S || alures_op==`OP_ABSQ_S || alures_op==`OP_PRECRQU_S_QB_PH ||
alures_op==`OP_ADDWC || alures_op==`OP_PRECRQ_RS_PH_W || alures_op==`OP_SHLLV ||
alures_op==`OP_SHLLVS || alures_op==`OP_EXTR_W || alures_op==`OP_EXTR_R_W ||
alures_op==`OP_EXTR_RS_W || alures_op==`OP_EXTR_S_H || alures_op==`OP_DPAU_H_QBL ||
alures_op==`OP_DPAQ_S_W_PH || alures_op==`OP_DPAU_H_QBR || alures_op==`OP_DPAQ_SA_L_W ||
alures_op==`OP_DPSQ_S_W_PH || alures_op==`OP_MAQ_S_W_PHL || alures_op==`OP_DPSQ_SA_L_W ||
alures_op==`OP_MAQ_S_W_PHR || alures_op==`OP_MAQ_SA_W_PHL || alures_op==`OP_MULEU_S_PH_QBL ||
alures_op==`OP_MAQ_SA_W_PHR || alures_op==`OP_MULEU_S_PH_QBR || alures_op==`OP_MULEQ_S_W_PHL ||
alures_op==`OP_MULSAQ_S_W_PH || alures_op==`OP_MULEQ_S_W_PHR || alures_op==`OP_WRDSP ||
alures_op==`OP_MULQ_RS_PH || alures_op==`OP_CMP_EQ ||
alures_op==`OP_CMP_LT || alures_op==`OP_CMP_LE ;
assign alures[122] = write_dspctl;
assign alures[121] = block_end;
assign alures[120] = alures_acc_op;
assign alures[119:88] = dspres_DSPCtl;
assign alures[16] = alu_valid && alures_ov;
assign alures[15] = alu_valid && alures_trap;
assign alures[87] = alures_con_true &alu_valid;
//HAVE_DSP_UNIT
assign alures[86] = alu_valid && (alures_bnt ||dsp_bnt);
assign alures[85] = alures_valid;
assign alures[84:81] = alures_qid;
assign alures[80:17] = {32'b0,alures_value};
assign alures[14] = 1'b0;
assign alures[13] = 1'b0;
assign alures[12] = 1'b0;
assign alures[11] = 1'b0;
assign alures[10] = 1'b0;
assign alures[9] = 1'b0;
assign alures[8] = 1'b0;
assign alures[7] = 1'b0;
assign alures[6] = 1'b0;
assign alures[5:0] = 6'b0;
assign allow = ~mul_valid | ~valid_p1 | ~validreg0 | not_write_acc_op | acc_write_ok;
assign alu2rsfull_tmp = mul_valid && valid_p1 & validreg0 & ~not_write_acc_op /*& ~acc_write_ok*/;//~allow
endmodule
//HAVE_DSP_UNIT
module godson_dsp(a, b, ac, op, op_ac, pos_in, scount_in, carry_in,
efi_in, outflag_in, ccond_in,
result, pos_out, scount_out, carry_out, efi_out, outflag_out, ccond_out, dsp_bnt);
input [31:0] a;
input [31:0] b;
input [1:0] ac;
input [7:0] op;
input [2:0] op_ac;
input [5:0] pos_in;
input [5:0] scount_in;
input carry_in;
input efi_in;
input [7:0]outflag_in;
input [3:0]ccond_in;
//output [64:0] result;
output [31:0] result;
output [5:0] pos_out;
output [5:0] scount_out;
output carry_out;
output efi_out;
output [7:0] outflag_out;
output [3:0] ccond_out;
output dsp_bnt;
/*************first kind: sub class*************************/
assign dsp_bnt = (op==`OP_BPOSGE32) & (~pos_in[5]);
//Byte operate
wire sub_op;
wire op_of_qb = (op_ac[1:0]==2'b01);
assign sub_op = (op_ac[2]==1'b1);
wire [8:0]bv1_0, bv1_1, bv1_2, bv1_3;
wire [8:0]bv2_0, bv2_1, bv2_2, bv2_3;
assign bv1_0 = op_of_qb ? {1'b0, a[7:0]} : 9'b0;
assign bv1_1 = op_of_qb ? {1'b0, a[15:8]} : 9'b0;
assign bv1_2 = op_of_qb ? {1'b0, a[23:16]} : 9'b0;
assign bv1_3 = op_of_qb ? {1'b0, a[31:24]} : 9'b0;
assign bv2_0 = op_of_qb ? (sub_op ? ~{1'b0, b[7:0]} : {1'b0, b[7:0]}) : 9'b0;
assign bv2_1 = op_of_qb ? (sub_op ? ~{1'b0, b[15:8]} : {1'b0, b[15:8]}) : 9'b0;
assign bv2_2 = op_of_qb ? (sub_op ? ~{1'b0, b[23:16]} : {1'b0, b[23:16]}) : 9'b0;
assign bv2_3 = op_of_qb ? (sub_op ? ~{1'b0, b[31:24]} : {1'b0, b[31:24]}) : 9'b0;
wire bcin = sub_op;
//SUBU.QB, SUBU_S.QB, ADDU.QB, ADDU_S.QB
wire [8:0] bsum_0, bsum_1, bsum_2, bsum_3;
assign bsum_0 = bv1_0 + bv2_0 + bcin;
assign bsum_1 = bv1_1 + bv2_1 + bcin;
assign bsum_2 = bv1_2 + bv2_2 + bcin;
assign bsum_3 = bv1_3 + bv2_3 + bcin;
//RADDU.W.QB
//wire [9:0]braddu_temp; //nomatch
wire [10:0]braddu_temp;
assign braddu_temp = {2'b0, bv1_0} + {2'b0, bv1_1} + {2'b0, bv1_2} + {2'b0, bv1_3};
wire [3:0] eq_4;
assign eq_4[0] = (a[ 7: 0]==b[ 7: 0]);
assign eq_4[1] = (a[15: 8]==b[15: 8]);
assign eq_4[2] = (a[23:16]==b[23:16]);
assign eq_4[3] = (a[31:24]==b[31:24]);
wire [3:0] blt;
assign blt[0] = bsum_0[8];
assign blt[1] = bsum_1[8];
assign blt[2] = bsum_2[8];
assign blt[3] = bsum_3[8];
//PICK.QB
wire [7:0]pick_0, pick_1, pick_2, pick_3;
assign pick_3 = ccond_in[3] ? bv1_3[7:0] : bv2_3[7:0];
assign pick_2 = ccond_in[2] ? bv1_2[7:0] : bv2_2[7:0];
assign pick_1 = ccond_in[1] ? bv1_1[7:0] : bv2_1[7:0];
assign pick_0 = ccond_in[0] ? bv1_0[7:0] : bv2_0[7:0];
wire [7:0]boutflag;
assign boutflag = (op==`OP_ADDQ)&(ac==2'b00) ?
{3'b0,((bsum_3[8]|bsum_2[8]|bsum_1[8]|bsum_0[8])?1'b1:1'b0),4'b0} :
(op==`OP_ADDQ_S)&(ac==2'b00) ?
{3'b0,((bsum_3[8]|bsum_2[8]|bsum_1[8]|bsum_0[8])?1'b1:1'b0),4'b0} :
(op==`OP_SUBQ)&(ac==2'b00) ?
{3'b0, ((bsum_3[8]|bsum_2[8]|bsum_1[8]|bsum_0[8])?1'b1:1'b0),4'b0} :
(op==`OP_SUBQ_S)&(ac==2'b00) ?
{3'b0,((bsum_3[8]|bsum_2[8]|bsum_1[8]|bsum_0[8])?1'b1:1'b0),4'b0}:8'b0;
reg [31:0]bresult;
always @(sub_op or bsum_0 or bsum_1 or bsum_2 or bsum_3 or a or b or blt or eq_4 or op
or braddu_temp or pick_0 or pick_1 or pick_2 or pick_3) begin
case(op) // synopsys full_case parallel_case
/*ADDU.QB*/
`OP_ADDQ :
bresult = {bsum_3[7:0],bsum_2[7:0],bsum_1[7:0],bsum_0[7:0]};
/*ADDU_S.QB*/
`OP_ADDQ_S :
bresult = {bsum_3[8] ? 8'hff : bsum_3[7:0], bsum_2[8] ? 8'hff : bsum_2[7:0],
bsum_1[8] ? 8'hff : bsum_1[7:0], bsum_0[8] ? 8'hff : bsum_0[7:0]};
`OP_SUBQ :
/*SUBU.QB*/
bresult = {bsum_3[7:0],bsum_2[7:0],bsum_1[7:0],bsum_0[7:0]};
`OP_SUBQ_S :
/*SUBU_S.QB*/
bresult = {bsum_3[8] ? 8'h00 : bsum_3[7:0],
bsum_2[8] ? 8'h00 : bsum_2[7:0],
bsum_1[8] ? 8'h00 : bsum_1[7:0],
bsum_0[8] ? 8'h00 : bsum_0[7:0]};
`OP_RADDU :
//bresult = {22'b0, braddu_temp}; nomatch
bresult = {21'b0, braddu_temp};
`OP_CMP_EQ :
/*CMPGU.EQ.QB , CMPU.EQ.QB*/
bresult = {28'h0, eq_4[3], eq_4[2],eq_4[1],eq_4[0]};
`OP_CMP_LT :
/*CMPGU.LT.QB , CMPU.LT.QB*/
bresult = {28'h0, blt[3], blt[2], blt[1], blt[0]};
//8'hde :
`OP_CMP_LE :
/*CMPGU.LE.QB , CMPU.LE.QB*/
bresult = {28'h0, (blt[3]|eq_4[3]), (blt[2]|eq_4[2]), (blt[1]|eq_4[1]), (blt[0]|eq_4[0])};
`OP_PICK :
bresult = {pick_3, pick_2, pick_1, pick_0};
`OP_PRECEQU_PH_QBL :
/*PRECEQU_PH_QBL*/
bresult = {{1'b0, a[31:24], 7'b0}, {1'b0, a[23:16], 7'b0}};
`OP_PRECEQU_PH_QBR :
/*PRECEQU_PH_QBR*/
bresult = {{1'b0, a[15:8], 7'b0}, {1'b0, a[7:0], 7'b0}};
`OP_PRECEQU_PH_QBLA :
/*PRECEQU_PH_QBLA*/
bresult = {{1'b0, a[31:24], 7'b0}, {1'b0, a[15:8], 7'b0}};
`OP_PRECEQU_PH_QBRA :
/*PRECEQU_PH_QBRA*/
bresult = {{1'b0, a[23:16], 7'b0}, {1'b0, a[7:0], 7'b0}};
`OP_PRECEU_PH_QBL :
/*PRECEU_PH_QBL*/
bresult = {{8'b0, a[31:24]}, {8'b0, a[23:16]}};
`OP_PRECEU_PH_QBR :
/*PRECEU_PH_QBR*/
bresult = {{8'b0, a[15:8]}, {8'b0, a[7:0]}};
`OP_PRECEU_PH_QBLA :
/*PRECEU_PH_QBLA*/
bresult = {{8'b0, a[31:24]}, {8'b0, a[15:8]}};
`OP_PRECEU_PH_QBRA :
/*PRECEU_PH_QBRA*/
bresult = {{8'b0, a[23:16]}, {8'b0, a[7:0]}};
// `OP_REPL :
/*REPL(V).QB*/
default :
bresult = {a[7:0], a[7:0], a[7:0], a[7:0]};
endcase
end //end always
/*********************halfword operate*****************************/
wire op_of_ph = (op_ac[1:0]==2'b10);
wire [16:0] hv1_0, hv1_1, hv2_0, hv2_1;
assign hv1_0 = op_of_ph ? {a[15], a[15:0]} : 16'b0;
assign hv1_1 = op_of_ph ? {a[31], a[31:16]} : 16'b0;
assign hv2_0 = op_of_ph ? (sub_op ? ~{b[15], b[15:0]} : {b[15], b[15:0]}) : 16'b0;
assign hv2_1 = op_of_ph ? (sub_op ? ~{b[31], b[31:16]} : {b[31], b[31:16]}) : 16'b0;
wire hcin;
wire [16:0] hsum_0, hsum_1;
assign hcin = sub_op;
assign hsum_0 = hv1_0 + hv2_0 + hcin;
assign hsum_1 = hv1_1 + hv2_1 + hcin;
wire [1:0]hlt;
assign hlt[0] = hsum_0[16];
assign hlt[1] = hsum_1[16];
/********for PRECRQU_S.QB.PH*************/
wire [16:0]hsum0, hsum1, hsum2, hsum3;
assign hsum0 = (op==`OP_PRECRQU_S_QB_PH) ? ({1'b0, a[31:16]} + (~{1'b0, 16'h7f80}) + 1'b1) : 17'b0;
assign hsum1 = (op==`OP_PRECRQU_S_QB_PH) ? ({1'b0, a[15:0]} + (~{1'b0, 16'h7f80}) + 1'b1) : 17'b0;
assign hsum2 = (op==`OP_PRECRQU_S_QB_PH) ? ({1'b0, b[31:16]} + (~{1'b0, 16'h7f80}) + 1'b1) : 17'b0;
assign hsum3 = (op==`OP_PRECRQU_S_QB_PH) ? ({1'b0, b[15:0]} + (~{1'b0, 16'h7f80}) + 1'b1) : 17'b0;
wire [3:0]heq_4;
assign heq_4[3] = (a[31:16]==16'h7f80);
assign heq_4[2] = (a[15:0] ==16'h7f80);
assign heq_4[1] = (b[31:16]==16'h7f80);
assign heq_4[0] = (b[15:0] ==16'h7f80);
wire hcond1, hcond2, hcond3, hcond4, hcond5, hcond6, hcond7, hcond8;
assign hcond1 = a[31];
assign hcond2 = a[15];
assign hcond3 = b[31];
assign hcond4 = b[15];
assign hcond5 = !a[31] & !hsum0[16] & !heq_4[3];
assign hcond6 = !a[15] & !hsum1[16] & !heq_4[2];
assign hcond7 = !b[31] & !hsum2[16] & !heq_4[1];
assign hcond8 = !b[15] & !hsum3[16] & !heq_4[0];
wire [7:0]houtflag;
assign houtflag = (op==`OP_ADDQ)&(ac==2'b01) ?
{3'b0,(((hsum_0[16]!=hsum_0[15])|(hsum_1[16]!=hsum_1[15]))?1'b1:1'b0),4'b0} :
(op==`OP_ADDQ_S)&(ac==2'b01) ?
{3'b0,(((hsum_0[16]!=hsum_0[15])|(hsum_1[16]!=hsum_1[15]))?1'b1:1'b0),4'b0} :
(op==`OP_SUBQ)&(ac==2'b01) ?
{3'b0,(((hsum_0[16]!=hsum_0[15])|(hsum_1[16]!=hsum_1[15]))?1'b1:1'b0),4'b0} :
(op==`OP_ABSQ_S)&(ac==2'b01) ?
{3'b0,(((a[31:16]==16'h8000)|(a[15:0]==16'h8000))?1'b1:1'b0),4'b0} :
(op==`OP_SUBQ_S)&(ac==2'b01) ?
{3'b0,(((hsum_0[16]!=hsum_0[15])|(hsum_1[16]!=hsum_1[15]))?1'b1:1'b0),4'b0} :
(op==`OP_PRECRQU_S_QB_PH)&(ac==2'b01) ?
{1'b0,((hcond1|hcond2|hcond3|hcond4|hcond5|hcond6|hcond7|hcond8)?
1'b1:1'b0),6'b0}:8'b0;
reg [31:0]hresult;
always @(sub_op or hsum_0 or hsum_1 or a or b or hlt or eq_4 or op or ccond_in or hcond1
or hcond2 or hcond3 or hcond4 or hcond5 or hcond6 or hcond7 or hcond8) begin
case(op) // synopsys full_case parallel_case
`OP_ADDQ :
/*ADDQ.PH*/
hresult = {hsum_1[15:0],hsum_0[15:0]};
`OP_ADDQ_S :
/*ADDQ_S.PH*/
hresult = {((hsum_1[16]!=hsum_1[15]) & (!hsum_1[16])) ? 16'h7fff :
((hsum_1[16]!=hsum_1[15]) & hsum_1[16]) ? 16'h8000 : hsum_1[15:0],
((hsum_0[16]!=hsum_0[15]) & (!hsum_0[16])) ? 16'h7fff :
((hsum_0[16]!=hsum_0[15]) & hsum_0[16]) ? 16'h8000 : hsum_0[15:0]};
`OP_SUBQ :
/*SUBQ.PH*/
hresult = {hsum_1[15:0],hsum_0[15:0]};
`OP_SUBQ_S :
/*SUBQ_S.PH*/
hresult = {((hsum_1[16]!=hsum_1[15]) & (!hsum_1[16])) ? 16'h7fff :
((hsum_1[16]!=hsum_1[15]) & hsum_1[16]) ? 16'h8000 : hsum_1[15:0],
((hsum_0[16]!=hsum_0[15]) & (!hsum_0[16])) ? 16'h7fff :
((hsum_0[16]!=hsum_0[15]) & hsum_0[16]) ? 16'h8000 : hsum_0[15:0]};
/*CMP.EQ.PH*/
`OP_CMP_EQ :
hresult = {30'b0, (eq_4[3]&eq_4[2]), (eq_4[1]&eq_4[0])};
/*CMP.LT.PH*/
`OP_CMP_LT :
hresult = {30'b0, hlt[1], hlt[0]};
/*CMP.LE.PH*/
`OP_CMP_LE :
hresult = {30'b0, (hlt[1]|eq_4[3]&eq_4[2]), (hlt[0]|eq_4[1]&eq_4[0])};
/*PICK.PH*/
`OP_PICK :
hresult = {ccond_in[1] ? a[31:16] : b[31:16], ccond_in[0] ? a[15:0] : b[15:0]};
/*PACKRL.PH*/
`OP_PACKRL_PH :
hresult = {a[15:0], b[31:16]};
/*ABSQ_S.PH*/
`OP_ABSQ_S :
hresult = {(a[31:16]==16'h8000) ? 16'h7fff : a[31] ? (~a[31:16] + 1'b1) : a[31:16],
(a[15:0] ==16'h8000) ? 16'h7fff : a[15] ? (~a[15:0] + 1'b1) : a[15:0] };
/*PRECRQ.QB.PH*/
`OP_PRECRQ_QB_PH :
hresult = {a[31:24], a[15:8], b[31:24], b[15:8]};
/*PRECRQU_S.QB.PH*/
`OP_PRECRQU_S_QB_PH :
hresult = {hcond1 ? 8'h0 : hcond5 ? 8'hff : a[30:23],
hcond2 ? 8'h0 : hcond6 ? 8'hff : a[14:7],
hcond3 ? 8'h0 : hcond7 ? 8'hff : b[30:23],
hcond4 ? 8'h0 : hcond8 ? 8'hff : b[14:7]};
/*PRECEQ.W.PHL*/
`OP_PRECEQ_W_PHL :
hresult = {a[31:16], 16'h0};
/*PRECEQ.W.PHR*/
`OP_PRECEQ_W_PHR :
hresult = {a[15:0], 16'h0};
/*REPL(V).PH*/
//`OP_REPL :
default : hresult = {a[15:0], a[15:0]};
endcase
end //end always
/****************************word operate*****************************************/
wire op_of_w = (op_ac[1:0] == 2'b11);
wire [32:0] wv1, wv2;
assign wv1 = ((op==`OP_ADDQ_S)|(op==`OP_SUBQ_S)|(op==`OP_ADDWC)) ? {a[31], a[31:0]} : {1'b0, a[31:0]};
assign wv2 = sub_op ? ~{b[31], b} : ((op==`OP_ADDQ_S)|(op==`OP_ADDWC)) ? {b[31], b} :
(op==`OP_MODSUB) ? (~{25'b0, b[7:0]}) : {1'b0, b};
wire wcin;
assign wcin = sub_op | ((op==`OP_ADDWC) & carry_in) | (op==`OP_MODSUB);
wire [32:0] wsum;
assign wsum = op_of_w ? (wv1 +wv2 + wcin) : 33'b0;
/*****************for PRECRQ_RS.PH.W*********************/
wire [32:0] wsum1, wsum2;
assign wsum1 = (op==`OP_PRECRQ_RS_PH_W) ? ({a[31], a[31:0]} + 16'h8000) : 33'b0;
assign wsum2 = (op==`OP_PRECRQ_RS_PH_W) ? ({b[31], b[31:0]} + 16'h8000) : 33'b0;
/*****************for INSV********************************/
wire [31:0] temp = (op==`OP_INSV) ? (({32{1'b1}} << scount_in) ^ {32{1'b1}}) : 32'b0;
wire [31:0] a_0 = a & temp;
wire [31:0] temp_1 = temp << pos_in;
wire [31:0] a_1 = a_0 << pos_in;
wire [31:0] b_1 = b & ~temp_1;
wire [31:0] res1 = (scount_in == 6'b0) ? 32'b0 : (b_1 | a_1);
wire [7:0]woutflag;
assign woutflag = (op==`OP_ADDQ_S) ? {3'b0,((wsum[32]!=wsum[31])?1'b1:1'b0),4'b0} :
(op==`OP_SUBQ_S) ? {3'b0,((wsum[32]!=wsum[31])?1'b1:1'b0),4'b0} :
(op==`OP_ADDWC) ? {3'b0,((wsum[32]!=wsum[31])?1'b1:1'b0),4'b0} :
(op==`OP_ABSQ_S) ? {3'b0,((a[31:0]==32'h80000000)?1'b1:1'b0),4'b0} :
(op==`OP_PRECRQ_RS_PH_W) ?
{1'b0,(((wsum1[32]!=wsum1[31])|(wsum2[32]!=wsum2[31]))?1'b1:1'b0),6'b0}:8'b0;
wire wcarry;
assign wcarry = (op==`OP_ADDSC) & wsum[32];
reg [31:0]wresult;
always @ (sub_op or op or a or b or wsum or wsum1 or wsum2 or res1) begin
case(op)
`OP_ADDQ_S : //ADDQ_S.W
wresult = ((wsum[32]!=wsum[31])&!wsum[32]) ? 32'h7fffffff :
((wsum[32]!=wsum[31])&wsum[32]) ? 32'h80000000 : wsum[31:0];
`OP_SUBQ_S : //SUBQ_S.W
wresult = ((wsum[32]!=wsum[31])&!wsum[32]) ? 32'h7fffffff :
((wsum[32]!=wsum[31])&wsum[32]) ? 32'h80000000 : wsum[31:0];
`OP_ADDSC : //ADDSC
wresult = wsum[31:0];
`OP_ADDWC : //ADDWC
wresult = wsum[31:0];
`OP_MODSUB : //MODSUB
//wresult = (a[31:0]==32'b0) ? b[23:8] : wsum[31:0];
wresult = (a[31:0]==32'b0) ? {16'b0,b[23:8]} : wsum[31:0];
`OP_ABSQ_S :
wresult = (a[31:0] == 32'h80000000) ? 32'h7fffffff : a[31] ? (~a[31:0] + 1'b1) : a[31:0];
`OP_PRECRQ_PH_W : //PRECRQ.PH.W
wresult = {a[31:16], b[31:16]};
`OP_PRECRQ_RS_PH_W : //PRECRQ_RS.PH.W
wresult = {(wsum1[32]!=wsum1[31]) ? 16'h7fff : wsum1[31:16],
(wsum2[32]!=wsum2[31]) ? 16'h7fff : wsum2[31:16]};
`OP_BITREV :
wresult = {16'h0, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8],
a[9],a[10], a[11], a[12], a[13], a[14],a[15]};
// `OP_INSV :
default : wresult = res1;
endcase
end
wire [31:0]sub_result;
assign sub_result = (ac==2'b00) ? bresult : (ac==2'b01) ? hresult : wresult;
wire [7:0] sub_outflag;
assign sub_outflag = (ac==2'b00) ? boutflag : (ac==2'b01) ? houtflag : woutflag;
/**********************the second kind: shift*******************/
wire [31:0] shift_result;
//SHLL.QB or SHLLV.QB, SHRL.QB or SHRLV.QB
wire shift_qb = ((op==`OP_SHRAV) | (op==`OP_SHLLV)) & (ac==2'b00);
wire [2:0] bsa;
assign bsa = shift_qb ? b[2:0] : 3'b0;
wire[7:0]ba1,ba2,ba3,ba4;
assign ba1 = a[7:0];
assign ba2 = a[15:8];
assign ba3 = a[23:16];
assign ba4 = a[31:24];
wire[15:0]bsa1_ll, bsa2_ll, bsa3_ll, bsa4_ll;
assign bsa1_ll = {8'b0, ba1} << bsa;
assign bsa2_ll = {8'b0, ba2} << bsa;
assign bsa3_ll = {8'b0, ba3} << bsa;
assign bsa4_ll = {8'b0, ba4} << bsa;
wire[7:0]bsa1_rl, bsa2_rl, bsa3_rl, bsa4_rl;
assign bsa1_rl = ba1 >> bsa;
assign bsa2_rl = ba2 >> bsa;
assign bsa3_rl = ba3 >> bsa;
assign bsa4_rl = ba4 >> bsa;
wire bsetcond1, bsetcond2, bsetcond3, bsetcond4;
assign bsetcond1 = |(bsa1_ll[15:8]);
assign bsetcond2 = |(bsa2_ll[15:8]);
assign bsetcond3 = |(bsa3_ll[15:8]);
assign bsetcond4 = |(bsa4_ll[15:8]);
//SHLL.PH or SHLLV.PH
wire shift_ph = ((op==`OP_SHLLV) | (op==`OP_SHLLVS) | (op==`OP_SHRAV) | (op==`OP_SHRAVR)) & (ac==2'b01);
wire [3:0] hsa;
assign hsa = shift_ph ? b[3:0] : 4'b0;
wire[15:0]ha1,ha2;
assign ha1 = a[31:16];
assign ha2 = a[15:0];
//wire[15:0]hsa1_ll, hsa2_ll;
wire[31:0]hsa1_la, hsa2_la;
assign hsa1_la = {{16{a[31]}}, ha1} << hsa;
assign hsa2_la = {{16{a[15]}}, ha2} << hsa;
wire hsetcond1, hsetcond2, hsetcond3, hsetcond4;
assign hsetcond1 = (hsa!=4'b0) & (~a[31]) & (|hsa1_la[31:15]);
assign hsetcond2 = (hsa!=4'b0) & a[31] & ~(&hsa1_la[31:15]);
assign hsetcond3 = (hsa!=4'b0) & (~a[15]) & (|hsa2_la[31:15]);
assign hsetcond4 = (hsa!=4'b0) & a[15] & ~(&hsa2_la[31:15]);
wire [15:0]shllvs_high = (hsa==4'b0) ? a[31:16] : (~a[31] & (|hsa1_la[30:15])) ? 16'h7fff :
(a[31] & ~(&hsa1_la[30:15])) ? 16'h8000 : hsa1_la[15:0];
wire [15:0]shllvs_low = (hsa==4'b0) ? a[15:0] : (~a[15] & (|hsa2_la[30:15])) ? 16'h7fff :
(a[15] & ~(&hsa2_la[30:15])) ? 16'h8000 : hsa2_la[15:0];
//SHRA.PH or SHRAV.PH or SHRA_R.PH or SHRAV_R.PH
wire[15:0]hsa1_ra_tmp, hsa2_ra_tmp;
wire [3:0] hsa_r;
assign hsa_r = hsa + 4'b1111; //hsa_r=hsa-1
wire [3:0]hsa_tmp = (op==`OP_SHRAV) ? hsa : hsa_r;
sra16 sra16_0(.sign(a[31]), .in(ha1), .shift(hsa_tmp), .out(hsa1_ra_tmp));
sra16 sra16_1(.sign(a[15]), .in(ha2), .shift(hsa_tmp), .out(hsa2_ra_tmp));
wire[16:0]hsa1_ra_r, hsa2_ra_r;
assign hsa1_ra_r = (hsa==4'b0) ? {a[31:16], 1'b0} : ({a[31], hsa1_ra_tmp} + 1'b1);
assign hsa2_ra_r = (hsa==4'b0) ? {a[15:0], 1'b0} : ({a[15], hsa2_ra_tmp} + 1'b1);
/*******************SHLL_S.W or SHLLV_S.W******************/
wire shift_w = ((op==`OP_SHLLVS) | (op==`OP_SHRAVR)) & (ac==2'b10);
wire [4:0] wsa;
assign wsa = shift_w ? b[4:0] : 5'b0;
wire [63:0]wsa_la;
assign wsa_la = {{32{a[31]}}, a[31:0]} << wsa;
wire wsetcond1, wsetcond2;
assign wsetcond1 = (wsa!=5'b0) & (~a[31]) & (|wsa_la[63:31]);
assign wsetcond2 = (wsa!=5'b0) & a[31] & ~(&wsa_la[63:31]);
wire [31:0]shllvs_w = (wsa==5'b0) ? a : (~a[31] & (|wsa_la[62:31])) ? 32'h7fffffff :
(a[31] & ~(&wsa_la[62:31])) ? 32'h80000000 : wsa_la[31:0];
/********************SHRA_R.W or SHRAV_R.W******************************/
wire [4:0] wsa_r;
assign wsa_r = wsa + 5'b11111;
wire [31:0] wsa_ra_r0;
wire [32:0] wsa_ra_r;
assign wsa_ra_r = (wsa ==5'b0) ? {a, 1'b0} : {a[31], wsa_ra_r0} + 1'b1;
sra32 sra32_2(.sign(a[31]), .in(a), .shift(wsa_r), .out(wsa_ra_r0));
assign shift_result =(op==`OP_SHRAV) ?
((ac==2'b00)?{bsa4_rl, bsa3_rl, bsa2_rl, bsa1_rl} : {hsa1_ra_tmp, hsa2_ra_tmp}) :
(op==`OP_SHRAVR) ?
((ac==2'b01) ? {hsa1_ra_r[16:1], hsa2_ra_r[16:1]} : wsa_ra_r[32:1]) :
(op==`OP_SHLLVS) ?
((ac==2'b01)?{shllvs_high, shllvs_low} : shllvs_w) :
/**********the last is SHLLV***************************/
(ac==2'b00) ? {bsa4_ll[7:0],bsa3_ll[7:0],bsa2_ll[7:0],bsa1_ll[7:0]} : {hsa1_la[15:0], hsa2_la[15:0]};
wire [7:0]shift_outflag;
assign shift_outflag = (op==`OP_SHLLV)&(ac==2'b00)?
{1'b0,((bsetcond1|bsetcond2|bsetcond3|bsetcond4)?1'b1:1'b0),6'b0} :
((op==`OP_SHLLV)|(op==`OP_SHLLVS))&(ac==2'b01) ?
{1'b0,((hsetcond1|hsetcond2|hsetcond3|hsetcond4)?1'b1:1'b0),6'b0} :
(op==`OP_SHLLVS)&(ac==2'b10) ?
{1'b0,((wsetcond1|wsetcond2)?1'b1:1'b0),6'b0}: 8'b0;
/***********************ac and DSPcontrol sub-class*******************************/
// WRDSP, RDDSP
wire [5:0]ac_sa_0, mask;
assign ac_sa_0 = b[5:0];
assign mask = b[5:0];
wire [5:0]ac_sa_1;
assign ac_sa_1 = (~ac_sa_0) + 1'b1;
assign pos_out = ((op==`OP_WRDSP) & mask[0]) ? a[5:0] : pos_in;
assign scount_out = ((op==`OP_WRDSP) & mask[1]) ? a[12:7] : scount_in;
wire ac_carry;
assign ac_carry = ((op==`OP_WRDSP) & mask[2]) ? a[13] : carry_in;
wire [7:0]ac_outflag;
assign ac_outflag = (op==`OP_WRDSP) & mask[3] ? a[23:16] : outflag_in;
assign ccond_out = ((op==`OP_WRDSP) & mask[4]) ? a[27:24] :
(((op==`OP_CMP_EQ) | (op==`OP_CMP_LT)| (op==`OP_CMP_LE)) & (ac==2'b00)) ? bresult[3:0] :
(((op==`OP_CMP_EQ) | (op==`OP_CMP_LT)| (op==`OP_CMP_LE)) & (ac==2'b01)) ?
{ccond_in[3:2],hresult[1:0]} : ccond_in;
assign efi_out = ((op==`OP_WRDSP) & mask[5]) ? a[14] : efi_in;
wire [31:0] ac_result;
wire [3:0] ccond_tmp_r = mask[4] ? ccond_in : 4'b0;
wire [3:0] ccond_tmp_w = mask[4] ? a[27:24] : ccond_in;
wire [7:0] outflag_tmp_r = mask[3] ? outflag_in : 8'b0;
wire [7:0] outflag_tmp_w = mask[3] ? a[23:16] : outflag_in;
wire efi_tmp_r = mask[5] ? efi_in : 1'b0;
wire efi_tmp_w = mask[5] ? a[14] : efi_in;
wire carry_tmp_r = mask[2] ? carry_in : 1'b0;
wire carry_tmp_w = mask[2] ? a[13] : carry_in;
wire [5:0] scount_tmp_r = mask[1] ? scount_in : 6'b0;
wire [5:0] scount_tmp_w = mask[1] ? a[12:7] : scount_in;
wire [5:0] pos_tmp_r = mask[0] ? pos_in : 6'b0;
wire [5:0] pos_tmp_w = mask[0] ? a[5:0] : pos_in;
assign ac_result = (op==`OP_RDDSP) ?
{4'b0, ccond_tmp_r, outflag_tmp_r, 1'b0, efi_tmp_r,
carry_tmp_r, scount_tmp_r, 1'b0, pos_tmp_r} :
{4'b0, ccond_tmp_w, outflag_tmp_w, 1'b0, efi_tmp_w,
carry_tmp_w, scount_tmp_w, 1'b0, pos_tmp_w};//OP_RDDSP & OP_WRDSP
/********************************************************************/
wire sub_class;
assign sub_class = (op[7:4]==4'hc)|((op[7:4]==4'hd)&(op!=`OP_SHLLV)&(op!=`OP_SHLLVS)&(op!=`OP_SHRAV)&(op!=`OP_SHRAVR))|(op==`OP_BITREV)|(op==`OP_INSV)|(op==`OP_REPL);
wire shift_class;
assign shift_class = (op==`OP_SHLLV) | (op==`OP_SHLLVS) | (op==`OP_SHRAV) | (op==`OP_SHRAVR);
wire ac_class;
assign ac_class = (op==`OP_RDDSP) |(op==`OP_WRDSP);
assign result = sub_class ? sub_result : shift_class ? shift_result: ac_result;//last is ac_class
assign outflag_out = sub_class ? sub_outflag : shift_class ? shift_outflag : ac_outflag;
assign carry_out = (op==`OP_ADDSC) ? wcarry : ac_carry;
endmodule
module godson_alu(a, b,c, op, out, ov, trap, bnt, condition_true,jalr_target) ;
input [31:0] a, b;
input [9 :0] c;
input [7:0] op;
output [31:0] out,jalr_target;
output trap,ov,bnt;
output condition_true;
wire sel_slt,sel_shift,sel_cloz,sel_gate,sel_movc,sel_r2,sel_default;
wire [31:0] slt_out,shift_out,cloz_out,gate_out,movc_out,r2_out,out1;
//-----ADD,ADDU,SUB,SUBU,LUI,TEQ,TNE,TLT,TLTU,TGE,TGEU,SLT,SLTU-----//
wire [31:0] a_tmp;
wire [31:0] b_tmp;
wire cin,cout;
wire alu_op = (op==`OP_TEQ)||(op==`OP_TNE)||(op==`OP_TLT)||(op==`OP_TLTU)
||(op==`OP_TGE)||(op==`OP_TGEU)||(op==`OP_SLT)||(op==`OP_SLTU)
||(op==`OP_SUB)||(op==`OP_SUBU)||(op==`OP_ADD)||(op==`OP_ADDU);
assign cin = (op==`OP_TEQ)||(op==`OP_TNE)||(op==`OP_TLT)||(op==`OP_TLTU)
||(op==`OP_TGE)||(op==`OP_TGEU)||(op==`OP_SLT)||(op==`OP_SLTU)
||(op==`OP_SUB)||(op==`OP_SUBU);
assign a_tmp = alu_op ? a : 32'b0;
assign b_tmp = cin ? ~b : alu_op ? b : 32'b0;
assign {cout,out1} = {1'b0, a_tmp} + {1'b0, b_tmp} + cin;
wire equ = (a==b);
wire lt = (a[31]&out1[31])|(a[31]&(~b[31]))|(out1[31]&(~b[31]));
assign trap = (equ&(op==`OP_TEQ)) | ((!equ)&(op==`OP_TNE)) |
(lt&(op==`OP_TLT)) | ((!cout)&(op==`OP_TLTU)) |
((!lt)&(op==`OP_TGE)) | (cout&(op==`OP_TGEU));
assign ov = (((a[31]& b[31]&(~out1[31])) | ((~a[31])&(~b[31])&out1[31]))&(op==`OP_ADD)) |
(((a[31]&(~b[31])&(~out1[31])) | ((~a[31])&b[31]&out1[31]))&(op==`OP_SUB));
assign sel_slt = (op==`OP_SLT)||(op==`OP_SLTU);
assign slt_out = (op==`OP_SLTU)? {31'b0, ~cout} : {31'b0, lt};
//--------------------SLL,SRL,SRA--------------------------//
wire [31:0] sll_out,sr_out,shift_temp;
assign sel_shift = (op==`OP_SLL)||(op==`OP_SRL)||(op==`OP_SRA);
assign sll_out = a << b[4:0];
wire [4:0] shift_offset = sel_shift ? b[4:0] : 5'b0;
sra32 sra32_0(.sign(op[0]), .in(a), .shift(shift_offset), .out(sr_out));
assign shift_out = (sr_out & {32{op[1]}}) | (sll_out & {32{~op[1]}});
//--------------------CLO,CLZ--------------------------//
wire [31:0] cloz_in1,cloz_in2;
wire [4:0] cloz_out_t;
wire nonzero;