1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* 4* XiangShan is licensed under Mulan PSL v2. 5* You can use this software according to the terms and conditions of the Mulan PSL v2. 6* You may obtain a copy of Mulan PSL v2 at: 7* http://license.coscl.org.cn/MulanPSL2 8* 9* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12* 13* See the Mulan PSL v2 for more details. 14***************************************************************************************/ 15 16package xiangshan.backend.fu.util 17 18import chisel3._ 19import chisel3.util._ 20 21abstract class CarrySaveAdderMToN(m: Int, n: Int)(len: Int) extends Module{ 22 val io = IO(new Bundle() { 23 val in = Input(Vec(m, UInt(len.W))) 24 val out = Output(Vec(n, UInt(len.W))) 25 }) 26} 27 28class CSA2_2(len: Int) extends CarrySaveAdderMToN(2, 2)(len) { 29 val temp = Wire(Vec(len, UInt(2.W))) 30 for((t, i) <- temp.zipWithIndex){ 31 val (a, b) = (io.in(0)(i), io.in(1)(i)) 32 val sum = a ^ b 33 val cout = a & b 34 t := Cat(cout, sum) 35 } 36 io.out.zipWithIndex.foreach({case(x, i) => x := Cat(temp.reverse map(_(i)))}) 37} 38 39class CSA3_2(len: Int) extends CarrySaveAdderMToN(3, 2)(len){ 40 val temp = Wire(Vec(len, UInt(2.W))) 41 for((t, i) <- temp.zipWithIndex){ 42 val (a, b, cin) = (io.in(0)(i), io.in(1)(i), io.in(2)(i)) 43 val a_xor_b = a ^ b 44 val a_and_b = a & b 45 val sum = a_xor_b ^ cin 46 val cout = a_and_b | (a_xor_b & cin) 47 t := Cat(cout, sum) 48 } 49 io.out.zipWithIndex.foreach({case(x, i) => x := Cat(temp.reverse map(_(i)))}) 50} 51 52class CSA5_3(len: Int)extends CarrySaveAdderMToN(5, 3)(len){ 53 val FAs = Array.fill(2)(Module(new CSA3_2(len))) 54 FAs(0).io.in := io.in.take(3) 55 FAs(1).io.in := VecInit(FAs(0).io.out(0), io.in(3), io.in(4)) 56 io.out := VecInit(FAs(1).io.out(0), FAs(0).io.out(1), FAs(1).io.out(1)) 57} 58 59class C22 extends CSA2_2(1) 60class C32 extends CSA3_2(1) 61class C53 extends CSA5_3(1) 62 63