-
Notifications
You must be signed in to change notification settings - Fork 389
/
FpuCore.scala
1949 lines (1682 loc) · 68.9 KB
/
FpuCore.scala
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
package vexriscv.ip.fpu
import spinal.core._
import spinal.lib._
import spinal.lib.eda.bench.{Bench, Rtl, XilinxStdTargets}
import spinal.lib.math.UnsignedDivider
import scala.collection.mutable.ArrayBuffer
object FpuDivSqrtIterationState extends SpinalEnum{
val IDLE, YY, XYY, Y2_XYY, DIV, _15_XYY2, Y_15_XYY2, Y_15_XYY2_RESULT, SQRT = newElement()
}
case class FpuCore( portCount : Int, p : FpuParameter) extends Component{
val io = new Bundle {
val port = Vec(slave(FpuPort(p)), portCount)
}
val portCountWidth = log2Up(portCount)
val Source = HardType(UInt(portCountWidth bits))
val exponentOne = (1 << p.internalExponentSize-1) - 1
val exponentF32Subnormal = exponentOne-127
val exponentF64Subnormal = exponentOne-1023
val exponentF32Infinity = exponentOne+127+1
val exponentF64Infinity = exponentOne+1023+1
def whenDouble(format : FpuFormat.C)(yes : => Unit)(no : => Unit): Unit ={
if(p.withDouble) when(format === FpuFormat.DOUBLE) { yes } otherwise{ no }
if(!p.withDouble) no
}
def muxDouble[T <: Data](format : FpuFormat.C)(yes : => T)(no : => T): T ={
if(p.withDouble) ((format === FpuFormat.DOUBLE) ? { yes } | { no })
else no
}
case class RfReadInput() extends Bundle{
val source = Source()
val opcode = p.Opcode()
val rs1, rs2, rs3 = p.rfAddress()
val rd = p.rfAddress()
val arg = p.Arg()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
case class RfReadOutput() extends Bundle{
val source = Source()
val opcode = p.Opcode()
val rs1, rs2, rs3 = p.internalFloating()
val rd = p.rfAddress()
val arg = p.Arg()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
val rs1Boxed, rs2Boxed = p.withDouble generate Bool()
}
case class LoadInput() extends Bundle{
val source = Source()
val rd = p.rfAddress()
val i2f = Bool()
val arg = Bits(2 bits)
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
case class ShortPipInput() extends Bundle{
val source = Source()
val opcode = p.Opcode()
val rs1, rs2 = p.internalFloating()
val rd = p.rfAddress()
val value = Bits(32 bits)
val arg = Bits(2 bits)
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
val rs1Boxed, rs2Boxed = p.withDouble generate Bool()
}
class MulInput() extends Bundle{
val source = Source()
val rs1, rs2, rs3 = p.internalFloating()
val rd = p.rfAddress()
val add = Bool()
val divSqrt = Bool()
val msb1, msb2 = Bool() //allow usage of msb bits of mul
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
case class DivSqrtInput() extends Bundle{
val source = Source()
val rs1, rs2 = p.internalFloating()
val rd = p.rfAddress()
val div = Bool()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
case class DivInput() extends Bundle{
val source = Source()
val rs1, rs2 = p.internalFloating()
val rd = p.rfAddress()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
case class SqrtInput() extends Bundle{
val source = Source()
val rs1 = p.internalFloating()
val rd = p.rfAddress()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
val addExtraBits = 2
case class AddInput() extends Bundle{
val source = Source()
val rs1, rs2 = FpuFloat(exponentSize = p.internalExponentSize, mantissaSize = p.internalMantissaSize+addExtraBits)
val rd = p.rfAddress()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
val needCommit = Bool()
}
class MergeInput() extends Bundle{
val source = Source()
val rd = p.rfAddress()
val value = p.writeFloating()
val scrap = Bool()
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
val NV = Bool()
val DZ = Bool()
}
case class RoundOutput() extends Bundle{
val source = Source()
val rd = p.rfAddress()
val value = p.internalFloating()
val format = p.withDouble generate FpuFormat()
val NV, NX, OF, UF, DZ = Bool()
val write = Bool()
}
val rf = new Area{
case class Entry() extends Bundle{
val value = p.internalFloating()
val boxed = p.withDouble generate Bool()
}
val ram = Mem(Entry(), 32*portCount)
val init = new Area{
val counter = Reg(UInt(6 bits)) init(0)
val done = CombInit(counter.msb)
when(!done){
counter := counter + 1
}
def apply(port : Flow[MemWriteCmd[Bool]]) = {
port.valid := !done
port.address := counter.resized
port.data := False
port
}
}
val scoreboards = Array.fill(portCount)(new Area{
val target, hit = Mem(Bool, 32) // XOR
val writes = Mem(Bool, 32)
val targetWrite = init(target.writePort)
val hitWrite = init(hit.writePort)
})
}
val commitFork = new Area{
val load, commit = Vec(Stream(FpuCommit(p)), portCount)
for(i <- 0 until portCount){
val fork = new StreamFork(FpuCommit(p), 2, synchronous = true)
fork.io.input << io.port(i).commit
fork.io.outputs(0) >> load(i)
fork.io.outputs(1).pipelined(m2s = false, s2m = true) >> commit(i) //Pipelining here is light, as it only use the flags of the payload
}
}
class Tracker(width : Int) extends Area{
val counter = Reg(UInt(width bits)) init(0)
val full = counter.andR
val notEmpty = counter.orR
val inc = False
val dec = False
counter := counter + U(inc) - U(dec)
}
class CommitArea(source : Int) extends Area{
val pending = new Tracker(4)
val add, mul, div, sqrt, short = new Tracker(4)
val input = commitFork.commit(source).haltWhen(List(add, mul, div, sqrt, short).map(_.full).orR || !pending.notEmpty).toFlow
when(input.fire){
add.inc setWhen(List(FpuOpcode.ADD).map(input.opcode === _).orR)
mul.inc setWhen(List(FpuOpcode.MUL, FpuOpcode.FMA).map(input.opcode === _).orR)
div.inc setWhen(List(FpuOpcode.DIV).map(input.opcode === _).orR)
sqrt.inc setWhen(List(FpuOpcode.SQRT).map(input.opcode === _).orR)
short.inc setWhen(List(FpuOpcode.SGNJ, FpuOpcode.MIN_MAX, FpuOpcode.FCVT_X_X).map(input.opcode === _).orR)
rf.scoreboards(source).writes(input.rd) := input.write
pending.dec := True
}
}
val commitLogic = for(source <- 0 until portCount) yield new CommitArea(source)
def commitConsume(what : CommitArea => Tracker, source : UInt, fire : Bool) : Bool = {
for(i <- 0 until portCount) what(commitLogic(i)).dec setWhen(fire && source === i)
commitLogic.map(what(_).notEmpty).read(source)
}
val scheduler = for(portId <- 0 until portCount;
scoreboard = rf.scoreboards(portId)) yield new Area{
val input = io.port(portId).cmd.pipelined(s2m = true)
val useRs1, useRs2, useRs3, useRd = False
switch(input.opcode){
is(p.Opcode.LOAD) { useRd := True }
is(p.Opcode.STORE) { useRs2 := True }
is(p.Opcode.ADD) { useRd := True; useRs1 := True; useRs2 := True }
is(p.Opcode.MUL) { useRd := True; useRs1 := True; useRs2 := True }
is(p.Opcode.DIV) { useRd := True; useRs1 := True; useRs2 := True }
is(p.Opcode.SQRT) { useRd := True; useRs1 := True }
is(p.Opcode.FMA) { useRd := True; useRs1 := True; useRs2 := True; useRs3 := True }
is(p.Opcode.I2F) { useRd := True }
is(p.Opcode.F2I) { useRs1 := True }
is(p.Opcode.MIN_MAX) { useRd := True; useRs1 := True; useRs2 := True }
is(p.Opcode.CMP) { useRs1 := True; useRs2 := True }
is(p.Opcode.SGNJ) { useRd := True; useRs1 := True; useRs2 := True }
is(p.Opcode.FMV_X_W) { useRs1 := True }
is(p.Opcode.FMV_W_X) { useRd := True }
is(p.Opcode.FCLASS ) { useRs1 := True }
is(p.Opcode.FCVT_X_X ) { useRd := True; useRs1 := True }
}
val uses = List(useRs1, useRs2, useRs3, useRd)
val regs = List(input.rs1, input.rs2, input.rs3, input.rd)
val rfHits = regs.map(scoreboard.hit.readAsync(_))
val rfTargets = regs.map(scoreboard.target.readAsync(_))
val rfBusy = (rfHits, rfTargets).zipped.map(_ ^ _)
val hits = (0 to 3).map(id => uses(id) && rfBusy(id))
val hazard = hits.orR || !rf.init.done || commitLogic(portId).pending.full
val output = input.haltWhen(hazard)
when(input.opcode === p.Opcode.STORE){
output.rs1 := input.rs2 //Datapath optimisation to unify rs source in the store pipeline
}
when(input.valid && rf.init.done){
scoreboard.targetWrite.address := input.rd
scoreboard.targetWrite.data := !rfTargets.last
}
when(output.fire && useRd){
scoreboard.targetWrite.valid := True
commitLogic(portId).pending.inc := True
}
}
val cmdArbiter = new Area{
val arbiter = StreamArbiterFactory.noLock.roundRobin.build(FpuCmd(p), portCount)
arbiter.io.inputs <> Vec(scheduler.map(_.output.pipelined(m2s = p.schedulerM2sPipe)))
val output = arbiter.io.output.swapPayload(RfReadInput())
output.source := arbiter.io.chosen
output.payload.assignSomeByName(arbiter.io.output.payload)
}
val read = new Area{
val s0 = cmdArbiter.output.pipelined()
val s1 = s0.m2sPipe()
val output = s1.swapPayload(RfReadOutput())
val rs = if(p.asyncRegFile){
List(s1.rs1, s1.rs2, s1.rs3).map(a => rf.ram.readAsync(s1.source @@ a))
} else {
List(s0.rs1, s0.rs2, s0.rs3).map(a => rf.ram.readSync(s0.source @@ a, enable = !output.isStall))
}
output.source := s1.source
output.opcode := s1.opcode
output.arg := s1.arg
output.roundMode := s1.roundMode
output.rd := s1.rd
output.rs1 := rs(0).value
output.rs2 := rs(1).value
output.rs3 := rs(2).value
if(p.withDouble){
output.rs1Boxed := rs(0).boxed
output.rs2Boxed := rs(1).boxed
output.format := s1.format
val store = s1.opcode === FpuOpcode.STORE ||s1.opcode === FpuOpcode.FMV_X_W
val sgnjBypass = s1.opcode === FpuOpcode.SGNJ && s1.format === FpuFormat.DOUBLE
when(!sgnjBypass) {
when(store) { //Pass through
output.format := rs(0).boxed ? FpuFormat.FLOAT | FpuFormat.DOUBLE
} elsewhen (s1.format === FpuFormat.FLOAT =/= rs(0).boxed) {
output.rs1.setNanQuiet
output.rs1.sign := False
}
}
when(s1.format === FpuFormat.FLOAT =/= rs(1).boxed) {
output.rs2.setNanQuiet
output.rs2.sign := False
}
when(s1.format === FpuFormat.FLOAT =/= rs(2).boxed) {
output.rs3.setNanQuiet
}
}
}
val decode = new Area{
val input = read.output/*.s2mPipe()*/.combStage()
input.ready := False
val loadHit = List(FpuOpcode.LOAD, FpuOpcode.FMV_W_X, FpuOpcode.I2F).map(input.opcode === _).orR
val load = Stream(LoadInput())
load.valid := input.valid && loadHit
input.ready setWhen(loadHit && load.ready)
load.payload.assignSomeByName(input.payload)
load.i2f := input.opcode === FpuOpcode.I2F
val shortPipHit = List(FpuOpcode.STORE, FpuOpcode.F2I, FpuOpcode.CMP, FpuOpcode.MIN_MAX, FpuOpcode.SGNJ, FpuOpcode.FMV_X_W, FpuOpcode.FCLASS, FpuOpcode.FCVT_X_X).map(input.opcode === _).orR
val shortPip = Stream(ShortPipInput())
input.ready setWhen(shortPipHit && shortPip.ready)
shortPip.valid := input.valid && shortPipHit
shortPip.payload.assignSomeByName(input.payload)
val divSqrtHit = input.opcode === p.Opcode.DIV || input.opcode === p.Opcode.SQRT
val divSqrt = Stream(DivSqrtInput())
if(p.withDivSqrt) {
input.ready setWhen (divSqrtHit && divSqrt.ready)
divSqrt.valid := input.valid && divSqrtHit
divSqrt.payload.assignSomeByName(input.payload)
divSqrt.div := input.opcode === p.Opcode.DIV
}
val divHit = input.opcode === p.Opcode.DIV
val div = Stream(DivInput())
if(p.withDiv) {
input.ready setWhen (divHit && div.ready)
div.valid := input.valid && divHit
div.payload.assignSomeByName(input.payload)
}
val sqrtHit = input.opcode === p.Opcode.SQRT
val sqrt = Stream(SqrtInput())
if(p.withSqrt) {
input.ready setWhen (sqrtHit && sqrt.ready)
sqrt.valid := input.valid && sqrtHit
sqrt.payload.assignSomeByName(input.payload)
}
val fmaHit = input.opcode === p.Opcode.FMA
val mulHit = input.opcode === p.Opcode.MUL || fmaHit
val mul = Stream(new MulInput())
val divSqrtToMul = Stream(new MulInput())
if(!p.withDivSqrt){
divSqrtToMul.valid := False
divSqrtToMul.payload.assignDontCare()
}
if(p.withMul) {
input.ready setWhen (mulHit && mul.ready && !divSqrtToMul.valid)
mul.valid := input.valid && mulHit || divSqrtToMul.valid
divSqrtToMul.ready := mul.ready
mul.payload := divSqrtToMul.payload
when(!divSqrtToMul.valid) {
mul.payload.assignSomeByName(input.payload)
mul.add := fmaHit
mul.divSqrt := False
mul.msb1 := True
mul.msb2 := True
mul.rs2.sign.allowOverride();
mul.rs2.sign := input.rs2.sign ^ input.arg(0)
mul.rs3.sign.allowOverride();
mul.rs3.sign := input.rs3.sign ^ input.arg(1)
}
}
val addHit = input.opcode === p.Opcode.ADD
val add = Stream(AddInput())
val mulToAdd = Stream(AddInput())
if(p.withAdd) {
input.ready setWhen (addHit && add.ready && !mulToAdd.valid)
add.valid := input.valid && addHit || mulToAdd.valid
mulToAdd.ready := add.ready
add.payload := mulToAdd.payload
when(!mulToAdd.valid) {
add.source := input.source
add.rd := input.rd
add.roundMode := input.roundMode
if(p.withDouble) add.format := input.format
add.needCommit := True
add.rs1.special := input.rs1.special
add.rs2.special := input.rs2.special
add.rs1.exponent := input.rs1.exponent
add.rs2.exponent := input.rs2.exponent
add.rs1.sign := input.rs1.sign
add.rs2.sign := input.rs2.sign ^ input.arg(0)
add.rs1.mantissa := input.rs1.mantissa << addExtraBits
add.rs2.mantissa := input.rs2.mantissa << addExtraBits
}
}
}
val load = new Area{
case class S0() extends Bundle{
val source = Source()
val rd = p.rfAddress()
val value = p.storeLoadType()
val i2f = Bool()
val arg = Bits(2 bits)
val roundMode = FpuRoundMode()
val format = p.withDouble generate FpuFormat()
}
val s0 = new Area{
val input = decode.load.pipelined(m2s = true, s2m = true).stage()
val filtred = commitFork.load.map(port => port.takeWhen(List(FpuOpcode.LOAD, FpuOpcode.FMV_W_X, FpuOpcode.I2F).map(_ === port.opcode).orR))
def feed = filtred(input.source)
val hazard = !feed.valid
val output = input.haltWhen(hazard).swapPayload(S0())
filtred.foreach(_.ready := False)
feed.ready := input.valid && output.ready
output.source := input.source
output.rd := input.rd
output.value := feed.value
output.i2f := input.i2f
output.arg := input.arg
output.roundMode := input.roundMode
if(p.withDouble) {
output.format := input.format
when(!input.i2f && input.format === FpuFormat.DOUBLE && output.value(63 downto 32).andR){ //Detect boxing
output.format := FpuFormat.FLOAT
}
}
}
val s1 = new Area{
val input = s0.output.stage()
val busy = False
val f32 = new Area{
val mantissa = input.value(0, 23 bits).asUInt
val exponent = input.value(23, 8 bits).asUInt
val sign = input.value(31)
}
val f64 = p.withDouble generate new Area{
val mantissa = input.value(0, 52 bits).asUInt
val exponent = input.value(52, 11 bits).asUInt
val sign = input.value(63)
}
val recodedExpOffset = UInt(p.internalExponentSize bits)
val passThroughFloat = p.internalFloating()
passThroughFloat.special := False
whenDouble(input.format){
passThroughFloat.sign := f64.sign
passThroughFloat.exponent := f64.exponent.resized
passThroughFloat.mantissa := f64.mantissa
recodedExpOffset := exponentF64Subnormal
} {
passThroughFloat.sign := f32.sign
passThroughFloat.exponent := f32.exponent.resized
passThroughFloat.mantissa := f32.mantissa << (if (p.withDouble) 29 else 0)
recodedExpOffset := exponentF32Subnormal
}
val manZero = passThroughFloat.mantissa === 0
val expZero = passThroughFloat.exponent === 0
val expOne = passThroughFloat.exponent(7 downto 0).andR
if(p.withDouble) {
expZero.clearWhen(input.format === FpuFormat.DOUBLE && input.value(62 downto 60) =/= 0)
expOne.clearWhen(input.format === FpuFormat.DOUBLE && input.value(62 downto 60) =/= 7)
}
val isZero = expZero && manZero
val isSubnormal = expZero && !manZero
val isInfinity = expOne && manZero
val isNan = expOne && !manZero
val fsm = new Area{
val done, boot, patched = Reg(Bool())
val ohInputWidth = 32 max p.internalMantissaSize
val ohInput = Bits(ohInputWidth bits).assignDontCare()
when(!input.i2f) {
if(!p.withDouble) ohInput := input.value(0, 23 bits) << 9
if( p.withDouble) ohInput := passThroughFloat.mantissa.asBits
} otherwise {
ohInput(ohInputWidth-32-1 downto 0) := 0
ohInput(ohInputWidth-32, 32 bits) := input.value(31 downto 0)
}
val i2fZero = Reg(Bool)
val shift = new Area{
val by = Reg(UInt(log2Up(ohInputWidth) bits))
val input = UInt(ohInputWidth bits).assignDontCare()
var logic = input
for(i <- by.range){
logic \= by(i) ? (logic |<< (BigInt(1) << i)) | logic
}
val output = RegNextWhen(logic, !done)
}
shift.input := (ohInput.asUInt |<< 1).resized
when(input.valid && (input.i2f || isSubnormal) && !done){
busy := True
when(boot){
when(input.i2f && !patched && input.value(31) && input.arg(0)){
input.value.getDrivingReg()(0, 32 bits) := B(input.value.asUInt.twoComplement(True).resize(32 bits))
patched := True
} otherwise {
shift.by := OHToUInt(OHMasking.first((ohInput).reversed))
boot := False
i2fZero := input.value(31 downto 0) === 0
}
} otherwise {
done := True
}
}
val expOffset = (UInt(p.internalExponentSize bits))
expOffset := 0
when(isSubnormal){
expOffset := shift.by.resized
}
when(!input.isStall){
done := False
boot := True
patched := False
}
}
val i2fSign = fsm.patched
val (i2fHigh, i2fLow) = fsm.shift.output.splitAt(if(p.withDouble) 0 else widthOf(input.value)-24)
val scrap = i2fLow =/= 0
val recoded = p.internalFloating()
recoded.mantissa := passThroughFloat.mantissa
recoded.exponent := (passThroughFloat.exponent -^ fsm.expOffset + recodedExpOffset).resized
recoded.sign := passThroughFloat.sign
recoded.setNormal
when(isZero){recoded.setZero}
when(isInfinity){recoded.setInfinity}
when(isNan){recoded.setNan}
val output = input.haltWhen(busy).swapPayload(new MergeInput())
output.source := input.source
output.roundMode := input.roundMode
if(p.withDouble) {
output.format := input.format
}
output.rd := input.rd
output.value.sign := recoded.sign
output.value.exponent := recoded.exponent
output.value.mantissa := recoded.mantissa @@ U"0"
output.value.special := recoded.special
output.scrap := False
output.NV := False
output.DZ := False
when(input.i2f){
output.value.sign := i2fSign
output.value.exponent := (U(exponentOne+31) - fsm.shift.by).resized
output.value.setNormal
output.scrap := scrap
when(fsm.i2fZero) { output.value.setZero }
}
when(input.i2f || isSubnormal){
output.value.mantissa := U(i2fHigh) @@ (if(p.withDouble) U"0" else U"")
}
}
}
val shortPip = new Area{
val input = decode.shortPip.stage()
val toFpuRf = List(FpuOpcode.MIN_MAX, FpuOpcode.SGNJ, FpuOpcode.FCVT_X_X).map(input.opcode === _).orR
val rfOutput = Stream(new MergeInput())
val isCommited = commitConsume(_.short, input.source, input.fire && toFpuRf)
val output = rfOutput.haltWhen(!isCommited)
val result = p.storeLoadType().assignDontCare()
val halt = False
val recodedResult = p.storeLoadType()
val f32 = new Area{
val exp = (input.rs1.exponent - (exponentOne-127)).resize(8 bits)
val man = CombInit(input.rs1.mantissa(if(p.withDouble) 51 downto 29 else 22 downto 0))
}
val f64 = p.withDouble generate new Area{
val exp = (input.rs1.exponent - (exponentOne-1023)).resize(11 bits)
val man = CombInit(input.rs1.mantissa)
}
whenDouble(input.format){
recodedResult := input.rs1.sign ## f64.exp ## f64.man
} {
recodedResult := (if(p.withDouble) B"xFFFFFFFF" else B"") ## input.rs1.sign ## f32.exp ## f32.man
}
val expSubnormalThreshold = muxDouble[UInt](input.format)(exponentF64Subnormal)(exponentF32Subnormal)
val expInSubnormalRange = input.rs1.exponent <= expSubnormalThreshold
val isSubnormal = !input.rs1.special && expInSubnormalRange
val isNormal = !input.rs1.special && !expInSubnormalRange
val fsm = new Area{
val f2iShift = input.rs1.exponent - U(exponentOne)
val isF2i = input.opcode === FpuOpcode.F2I
val needRecoding = List(FpuOpcode.FMV_X_W, FpuOpcode.STORE).map(_ === input.opcode).orR && isSubnormal
val done, boot = Reg(Bool())
val isZero = input.rs1.isZero// || input.rs1.exponent < exponentOne-1
val shift = new Area{
val by = Reg(UInt(log2Up(p.internalMantissaSize+1 max 33) bits))
val input = UInt(p.internalMantissaSize+1 max 33 bits).assignDontCare()
var logic = input
val scrap = Reg(Bool)
for(i <- by.range.reverse){
scrap setWhen(by(i) && logic(0, 1 << i bits) =/= 0)
logic \= by(i) ? (logic |>> (BigInt(1) << i)) | logic
}
when(boot){
scrap := False
}
val output = RegNextWhen(logic, !done)
}
shift.input := (U(!isZero) @@ input.rs1.mantissa) << (if(p.withDouble) 0 else 9)
val formatShiftOffset = muxDouble[UInt](input.format)(exponentOne-1023+1)(exponentOne - (if(p.withDouble) (127+34) else (127-10)))
when(input.valid && (needRecoding || isF2i) && !done){
halt := True
when(boot){
when(isF2i){
shift.by := ((U(exponentOne + 31) - input.rs1.exponent).min(U(33)) + (if(p.withDouble) 20 else 0)).resized //TODO merge
} otherwise {
shift.by := (formatShiftOffset - input.rs1.exponent).resized
}
boot := False
} otherwise {
done := True
}
}
when(!input.isStall){
done := False
boot := True
}
}
val mantissaForced = False
val exponentForced = False
val mantissaForcedValue = Bool().assignDontCare()
val exponentForcedValue = Bool().assignDontCare()
val cononicalForced = False
when(input.rs1.special){
switch(input.rs1.exponent(1 downto 0)){
is(FpuFloat.ZERO){
mantissaForced := True
exponentForced := True
mantissaForcedValue := False
exponentForcedValue := False
}
is(FpuFloat.INFINITY){
mantissaForced := True
exponentForced := True
mantissaForcedValue := False
exponentForcedValue := True
}
is(FpuFloat.NAN){
exponentForced := True
exponentForcedValue := True
when(input.rs1.isCanonical){
cononicalForced := True
mantissaForced := True
mantissaForcedValue := False
}
}
}
}
when(isSubnormal){
exponentForced := True
exponentForcedValue := False
recodedResult(0,23 bits) := fsm.shift.output(22 downto 0).asBits
whenDouble(input.format){
recodedResult(51 downto 23) := fsm.shift.output(51 downto 23).asBits
}{}
}
when(mantissaForced){
recodedResult(0,23 bits) := (default -> mantissaForcedValue)
whenDouble(input.format){
recodedResult(23, 52-23 bits) := (default -> mantissaForcedValue)
}{}
}
when(exponentForced){
whenDouble(input.format){
recodedResult(52, 11 bits) := (default -> exponentForcedValue)
} {
recodedResult(23, 8 bits) := (default -> exponentForcedValue)
}
}
when(cononicalForced){
whenDouble(input.format){
recodedResult(63) := False
recodedResult(51) := True
} {
recodedResult(31) := False
recodedResult(22) := True
}
}
val rspNv = False
val rspNx = False
val f2i = new Area{ //Will not work for 64 bits float max value rounding
val unsigned = fsm.shift.output(32 downto 0) >> 1
val resign = input.arg(0) && input.rs1.sign
val round = fsm.shift.output(0) ## fsm.shift.scrap
val increment = input.roundMode.mux(
FpuRoundMode.RNE -> (round(1) && (round(0) || unsigned(0))),
FpuRoundMode.RTZ -> False,
FpuRoundMode.RDN -> (round =/= 0 && input.rs1.sign),
FpuRoundMode.RUP -> (round =/= 0 && !input.rs1.sign),
FpuRoundMode.RMM -> (round(1))
)
val result = (Mux(resign, ~unsigned, unsigned) + (resign ^ increment).asUInt)
val overflow = (input.rs1.exponent > (input.arg(0) ? U(exponentOne+30) | U(exponentOne+31)) || input.rs1.isInfinity) && !input.rs1.sign || input.rs1.isNan
val underflow = (input.rs1.exponent > U(exponentOne+31) || input.arg(0) && unsigned.msb && (unsigned(30 downto 0) =/= 0 || increment) || !input.arg(0) && (unsigned =/= 0 || increment) || input.rs1.isInfinity) && input.rs1.sign
val isZero = input.rs1.isZero
if(p.withDouble){
overflow setWhen(!input.rs1.sign && increment && unsigned(30 downto 0).andR && (input.arg(0) || unsigned(31)))
}
when(isZero){
result := 0
} elsewhen(underflow || overflow) {
val low = overflow
val high = input.arg(0) ^ overflow
result := (31 -> high, default -> low)
rspNv := input.valid && input.opcode === FpuOpcode.F2I && fsm.done && !isZero
} otherwise {
rspNx := input.valid && input.opcode === FpuOpcode.F2I && fsm.done && round =/= 0
}
}
val bothZero = input.rs1.isZero && input.rs2.isZero
val rs1Equal = input.rs1 === input.rs2
val rs1AbsSmaller = (input.rs1.exponent @@ input.rs1.mantissa) < (input.rs2.exponent @@ input.rs2.mantissa)
rs1AbsSmaller.setWhen(input.rs2.isInfinity)
rs1AbsSmaller.setWhen(input.rs1.isZero)
rs1AbsSmaller.clearWhen(input.rs2.isZero)
rs1AbsSmaller.clearWhen(input.rs1.isInfinity)
rs1Equal setWhen(input.rs1.sign === input.rs2.sign && input.rs1.isInfinity && input.rs2.isInfinity)
val rs1Smaller = (input.rs1.sign ## input.rs2.sign).mux(
0 -> rs1AbsSmaller,
1 -> False,
2 -> True,
3 -> (!rs1AbsSmaller && !rs1Equal)
)
val minMaxSelectRs2 = !(((rs1Smaller ^ input.arg(0)) && !input.rs1.isNan || input.rs2.isNan))
val minMaxSelectNanQuiet = input.rs1.isNan && input.rs2.isNan
val cmpResult = B(rs1Smaller && !bothZero && !input.arg(1) || (rs1Equal || bothZero) && !input.arg(0))
when(input.rs1.isNan || input.rs2.isNan) { cmpResult := 0 }
val sgnjRs1Sign = CombInit(input.rs1.sign)
val sgnjRs2Sign = CombInit(input.rs2.sign)
if(p.withDouble){
sgnjRs2Sign setWhen(input.rs2Boxed && input.format === FpuFormat.DOUBLE)
}
val sgnjResult = (sgnjRs1Sign && input.arg(1)) ^ sgnjRs2Sign ^ input.arg(0)
val fclassResult = B(0, 32 bits)
val decoded = input.rs1.decode()
fclassResult(0) := input.rs1.sign && decoded.isInfinity
fclassResult(1) := input.rs1.sign && isNormal
fclassResult(2) := input.rs1.sign && isSubnormal
fclassResult(3) := input.rs1.sign && decoded.isZero
fclassResult(4) := !input.rs1.sign && decoded.isZero
fclassResult(5) := !input.rs1.sign && isSubnormal
fclassResult(6) := !input.rs1.sign && isNormal
fclassResult(7) := !input.rs1.sign && decoded.isInfinity
fclassResult(8) := decoded.isNan && !decoded.isQuiet
fclassResult(9) := decoded.isNan && decoded.isQuiet
switch(input.opcode){
is(FpuOpcode.STORE) { result := recodedResult }
is(FpuOpcode.FMV_X_W) { result := recodedResult }
is(FpuOpcode.F2I) { result(31 downto 0) := f2i.result.asBits }
is(FpuOpcode.CMP) { result(31 downto 0) := cmpResult.resized }
is(FpuOpcode.FCLASS) { result(31 downto 0) := fclassResult.resized }
}
rfOutput.valid := input.valid && toFpuRf && !halt
rfOutput.source := input.source
rfOutput.rd := input.rd
rfOutput.roundMode := input.roundMode
if(p.withDouble) rfOutput.format := input.format
rfOutput.scrap := False
rfOutput.value.sign := input.rs1.sign
rfOutput.value.exponent := input.rs1.exponent
rfOutput.value.mantissa := input.rs1.mantissa @@ U"0"
rfOutput.value.special := input.rs1.special
switch(input.opcode){
is(FpuOpcode.MIN_MAX){
when(minMaxSelectRs2) {
rfOutput.value.sign := input.rs2.sign
rfOutput.value.exponent := input.rs2.exponent
rfOutput.value.mantissa := input.rs2.mantissa @@ U"0"
rfOutput.value.special := input.rs2.special
}
when(minMaxSelectNanQuiet){
rfOutput.value.setNanQuiet
}
}
is(FpuOpcode.SGNJ){
when(!input.rs1.isNan) {
rfOutput.value.sign := sgnjResult
}
if(p.withDouble) when(input.rs1Boxed && input.format === FpuFormat.DOUBLE){
rfOutput.value.sign := input.rs1.sign
rfOutput.format := FpuFormat.FLOAT
}
}
if(p.withDouble) is(FpuOpcode.FCVT_X_X){
rfOutput.format := ((input.format === FpuFormat.FLOAT) ? FpuFormat.DOUBLE | FpuFormat.FLOAT)
when(input.rs1.isNan){
rfOutput.value.setNanQuiet
}
}
}
val signalQuiet = input.opcode === FpuOpcode.CMP && input.arg =/= 2
val rs1Nan = input.rs1.isNan
val rs2Nan = input.rs2.isNan
val rs1NanNv = input.rs1.isNan && (!input.rs1.isQuiet || signalQuiet)
val rs2NanNv = input.rs2.isNan && (!input.rs2.isQuiet || signalQuiet)
val NV = List(FpuOpcode.CMP, FpuOpcode.MIN_MAX, FpuOpcode.FCVT_X_X).map(input.opcode === _).orR && rs1NanNv ||
List(FpuOpcode.CMP, FpuOpcode.MIN_MAX).map(input.opcode === _).orR && rs2NanNv
rspNv setWhen(NV)
val rspStreams = Vec(Stream(FpuRsp(p)), portCount)
input.ready := !halt && (toFpuRf ? rfOutput.ready | rspStreams.map(_.ready).read(input.source))
for(i <- 0 until portCount){
def rsp = rspStreams(i)
rsp.valid := input.valid && input.source === i && !toFpuRf && !halt
rsp.value := result
rsp.NV := rspNv
rsp.NX := rspNx
io.port(i).rsp << rsp.stage()
}
rfOutput.NV := NV
rfOutput.DZ := False
}
val mul = p.withMul generate new Area{
val inWidthA = p.internalMantissaSize+1
val inWidthB = p.internalMantissaSize+1
val outWidth = p.internalMantissaSize*2+2
case class MulSplit(offsetA : Int, offsetB : Int, widthA : Int, widthB : Int, id : Int){
val offsetC = offsetA+offsetB
val widthC = widthA + widthB
val endC = offsetC+widthC
}
val splitsUnordered = for(offsetA <- 0 until inWidthA by p.mulWidthA;
offsetB <- 0 until inWidthB by p.mulWidthB;
widthA = (inWidthA - offsetA) min p.mulWidthA;
widthB = (inWidthB - offsetB) min p.mulWidthB) yield {
MulSplit(offsetA, offsetB, widthA, widthB, -1)
}
val splits = splitsUnordered.sortWith(_.endC < _.endC).zipWithIndex.map(e => e._1.copy(id=e._2))
class MathWithExp extends MulInput{
val exp = UInt(p.internalExponentSize+1 bits)
}
val preMul = new Area{
val input = decode.mul.stage()
val output = input.swapPayload(new MathWithExp())
output.payload.assignSomeByName(input.payload)
output.exp := input.rs1.exponent +^ input.rs2.exponent
}
class MathWithMul extends MathWithExp{
val muls = Vec(splits.map(e => UInt(e.widthA + e.widthB bits)))
}
val mul = new Area{
val input = preMul.output.stage()
val output = input.swapPayload(new MathWithMul())
val mulA = U(input.msb1) @@ input.rs1.mantissa
val mulB = U(input.msb2) @@ input.rs2.mantissa
output.payload.assignSomeByName(input.payload)
splits.foreach(e => output.muls(e.id) := mulA(e.offsetA, e.widthA bits) * mulB(e.offsetB, e.widthB bits))
}
val sumSplitAt = splits.size/2//splits.filter(e => e.endC <= p.internalMantissaSize).size
class Sum1Output extends MathWithExp{
val muls2 = Vec(splits.drop(sumSplitAt).map(e => UInt(e.widthA + e.widthB bits)))
val mulC2 = UInt(p.internalMantissaSize*2+2 bits)
}
class Sum2Output extends MathWithExp{
val mulC = UInt(p.internalMantissaSize*2+2 bits)
}
val sum1 = new Area {
val input = mul.output.stage()
val sum = splits.take(sumSplitAt).map(e => (input.muls(e.id) << e.offsetC).resize(outWidth)).reduceBalancedTree(_ + _)
val output = input.swapPayload(new Sum1Output())
output.payload.assignSomeByName(input.payload)
output.mulC2 := sum.resized
output.muls2 := Vec(input.muls.drop(sumSplitAt))
}
val sum2 = new Area {
val input = sum1.output.stage()
val sum = input.mulC2 + splits.drop(sumSplitAt).map(e => (input.muls2(e.id-sumSplitAt) << e.offsetC).resize(outWidth)).reduceBalancedTree(_ + _)
val isCommited = commitConsume(_.mul, input.source, input.fire)
val output = input.haltWhen(!isCommited).swapPayload(new Sum2Output())
output.payload.assignSomeByName(input.payload)
output.mulC := sum
}
val norm = new Area{
val input = sum2.output.stage()
val (mulHigh, mulLow) = input.mulC.splitAt(p.internalMantissaSize-1)
val scrap = mulLow =/= 0
val needShift = mulHigh.msb
val exp = input.exp + U(needShift)
val man = needShift ? mulHigh(1, p.internalMantissaSize+1 bits) | mulHigh(0, p.internalMantissaSize+1 bits)
scrap setWhen(needShift && mulHigh(0))
val forceZero = input.rs1.isZero || input.rs2.isZero
val underflowThreshold = muxDouble[UInt](input.format)(exponentOne + exponentOne - 1023 - 53) (exponentOne + exponentOne - 127 - 24)
val underflowExp = muxDouble[UInt](input.format)(exponentOne - 1023 - 54) (exponentOne - 127 - 25)
val forceUnderflow = exp < underflowThreshold
val forceOverflow = input.rs1.isInfinity || input.rs2.isInfinity
val infinitynan = ((input.rs1.isInfinity || input.rs2.isInfinity) && (input.rs1.isZero || input.rs2.isZero))
val forceNan = input.rs1.isNan || input.rs2.isNan || infinitynan
val output = p.writeFloating()
output.sign := input.rs1.sign ^ input.rs2.sign
output.exponent := (exp - exponentOne).resized
output.mantissa := man.asUInt
output.setNormal
val NV = False
when(exp(exp.getWidth-3, 3 bits) >= 5) { output.exponent(p.internalExponentSize-2, 2 bits) := 3 }
when(forceNan) {
output.setNanQuiet
NV setWhen(infinitynan || input.rs1.isNanSignaling || input.rs2.isNanSignaling)
} elsewhen(forceOverflow) {
output.setInfinity
} elsewhen(forceZero) {
output.setZero
} elsewhen(forceUnderflow) {
output.exponent := underflowExp.resized
}
}
val result = new Area {
def input = norm.input
def NV = norm.NV