|
| 1 | +package mvp.buffer |
| 2 | + |
| 3 | +import chisel3._ |
| 4 | +import chisel3.util._ |
| 5 | +import chisel3.experimental._ |
| 6 | + |
| 7 | +import mvp.common._ |
| 8 | +import mvp.polyram._ |
| 9 | + |
| 10 | +class poly_rd_interface(width: Int, nElems: Int, nBanks: Int, readDelay: Int) extends Module { |
| 11 | + override val desiredName = s"poly_rd_interface_${width}_${nElems}_${nBanks}_${readDelay}" |
| 12 | + |
| 13 | + val dataWidth = width |
| 14 | + val hiAddrWidth = utils.log2(nBanks) |
| 15 | + val loAddrWidth = utils.log2(nElems) |
| 16 | + val addrWidth = hiAddrWidth + loAddrWidth |
| 17 | + |
| 18 | + val io = IO(new Bundle { |
| 19 | + val vpu_rd = Flipped(new VpuRdPort(dataWidth, addrWidth, nBanks)) |
| 20 | + val buf_rd = new BufRdPort(dataWidth, loAddrWidth, nBanks) |
| 21 | + }) |
| 22 | + |
| 23 | + val lo_addr = utils.slice(io.vpu_rd.addr, 0, loAddrWidth) |
| 24 | + val hi_addr = utils.slice(io.vpu_rd.addr, loAddrWidth, addrWidth) |
| 25 | + |
| 26 | + io.buf_rd.addr := utils.repeat(nBanks, lo_addr) |
| 27 | + |
| 28 | + var lut = List.tabulate(nBanks) { x => (x.U -> io.buf_rd.data(x)) } |
| 29 | + |
| 30 | + io.vpu_rd.data := MuxLookup(ShiftRegister(hi_addr, readDelay), 0.U, lut) |
| 31 | +} |
| 32 | + |
| 33 | +class poly_wr_interface(width: Int, nElems: Int, nBanks: Int) extends Module { |
| 34 | + override val desiredName = s"poly_wr_interface_${width}_${nElems}_${nBanks}" |
| 35 | + |
| 36 | + val dataWidth = width |
| 37 | + val hiAddrWidth = utils.log2(nBanks) |
| 38 | + val loAddrWidth = utils.log2(nElems) |
| 39 | + val addrWidth = hiAddrWidth + loAddrWidth |
| 40 | + |
| 41 | + val io = IO(new Bundle { |
| 42 | + val vpu_wr = Flipped(new VpuWrPort(dataWidth, addrWidth, nBanks)) |
| 43 | + val buf_wr = new BufWrPort(dataWidth, loAddrWidth, nBanks) |
| 44 | + }) |
| 45 | + |
| 46 | + val lo_addr = utils.slice(io.vpu_wr.addr, 0, loAddrWidth) |
| 47 | + val hi_addr = utils.slice(io.vpu_wr.addr, loAddrWidth, addrWidth) |
| 48 | + |
| 49 | + io.buf_wr.addr := utils.repeat(nBanks, lo_addr) |
| 50 | + io.buf_wr.data := utils.repeat(nBanks, io.vpu_wr.data) |
| 51 | + |
| 52 | + when (io.vpu_wr.en) { |
| 53 | + io.buf_wr.en := UIntToOH(hi_addr).asBools |
| 54 | + } .otherwise { |
| 55 | + io.buf_wr.en := utils.repeat(nBanks, 0.B) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class double_pp_buffer(width: Int, nElems: Int, nBanks: Int) extends Module { |
| 60 | + override val desiredName = s"double_pp_buffer_${width}_${nElems}_${nBanks}" |
| 61 | + |
| 62 | + val io = IO(new Bundle { |
| 63 | + val i_done = Input(Bool()) |
| 64 | + val polyvec0_rd = Flipped(new BufRdPort(width, utils.log2(nElems), nBanks)) |
| 65 | + val polyvec0_wr = Flipped(new BufWrPort(width, utils.log2(nElems), nBanks)) |
| 66 | + val polyvec1_rd = Flipped(new BufRdPort(width, utils.log2(nElems), nBanks)) |
| 67 | + val polyvec1_wr = Flipped(new BufWrPort(width, utils.log2(nElems), nBanks)) |
| 68 | + }) |
| 69 | + |
| 70 | + val done_r = RegNext(io.i_done) |
| 71 | + |
| 72 | + val s01 :: s10 :: Nil = Enum(2) |
| 73 | + val state = RegInit(s01) |
| 74 | + when (io.i_done && !done_r) { |
| 75 | + switch (state) { |
| 76 | + is (s01) { state := s10 } |
| 77 | + is (s10) { state := s01 } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + val u_ram = List.fill(2) { |
| 82 | + Module(new poly_ram_wrapper(width, utils.log2(nElems), nBanks)) |
| 83 | + } |
| 84 | + |
| 85 | + when (state === s01) { |
| 86 | + io.polyvec0_rd <> u_ram(0).io.rd |
| 87 | + io.polyvec0_wr <> u_ram(0).io.wr |
| 88 | + io.polyvec1_rd <> u_ram(1).io.rd |
| 89 | + io.polyvec1_wr <> u_ram(1).io.wr |
| 90 | + } .elsewhen (state === s10) { |
| 91 | + io.polyvec0_rd <> u_ram(1).io.rd |
| 92 | + io.polyvec0_wr <> u_ram(1).io.wr |
| 93 | + io.polyvec1_rd <> u_ram(0).io.rd |
| 94 | + io.polyvec1_wr <> u_ram(0).io.wr |
| 95 | + } .otherwise { |
| 96 | + io := DontCare |
| 97 | + u_ram(0).io := DontCare |
| 98 | + u_ram(1).io := DontCare |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class triple_pp_buffer(width: Int, nElems: Int, nBanks: Int) extends Module { |
| 103 | + override val desiredName = s"triple_pp_buffer_${width}_${nElems}_${nBanks}" |
| 104 | + |
| 105 | + val io = IO(new Bundle { |
| 106 | + val i_done = Input(Bool()) |
| 107 | + val polyvec0_rd = Flipped(new BufRdPort(width, utils.log2(nElems), nBanks)) |
| 108 | + val polyvec0_wr = Flipped(new BufWrPort(width, utils.log2(nElems), nBanks)) |
| 109 | + val polyvec1_rd = Flipped(new BufRdPort(width, utils.log2(nElems), nBanks)) |
| 110 | + val polyvec1_wr = Flipped(new BufWrPort(width, utils.log2(nElems), nBanks)) |
| 111 | + val polyvec2_rd = Flipped(new BufRdPort(width, utils.log2(nElems), nBanks)) |
| 112 | + val polyvec2_wr = Flipped(new BufWrPort(width, utils.log2(nElems), nBanks)) |
| 113 | + }) |
| 114 | + |
| 115 | + val done_r = RegNext(io.i_done) |
| 116 | + |
| 117 | + val s012 :: s201 :: s120 :: Nil = Enum(3) |
| 118 | + val state = RegInit(s012) |
| 119 | + when (io.i_done && !done_r) { |
| 120 | + switch (state) { |
| 121 | + is (s012) { state := s201 } |
| 122 | + is (s201) { state := s120 } |
| 123 | + is (s120) { state := s012 } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + val u_ram = List.fill(3) { |
| 128 | + Module(new poly_ram_wrapper(width, utils.log2(nElems), nBanks)) |
| 129 | + } |
| 130 | + |
| 131 | + when (state === s012) { |
| 132 | + io.polyvec0_rd <> u_ram(0).io.rd |
| 133 | + io.polyvec0_wr <> u_ram(0).io.wr |
| 134 | + io.polyvec1_rd <> u_ram(1).io.rd |
| 135 | + io.polyvec1_wr <> u_ram(1).io.wr |
| 136 | + io.polyvec2_rd <> u_ram(2).io.rd |
| 137 | + io.polyvec2_wr <> u_ram(2).io.wr |
| 138 | + } .elsewhen (state === s201) { |
| 139 | + io.polyvec0_rd <> u_ram(2).io.rd |
| 140 | + io.polyvec0_wr <> u_ram(2).io.wr |
| 141 | + io.polyvec1_rd <> u_ram(0).io.rd |
| 142 | + io.polyvec1_wr <> u_ram(0).io.wr |
| 143 | + io.polyvec2_rd <> u_ram(1).io.rd |
| 144 | + io.polyvec2_wr <> u_ram(1).io.wr |
| 145 | + } .elsewhen (state === s120) { |
| 146 | + io.polyvec0_rd <> u_ram(1).io.rd |
| 147 | + io.polyvec0_wr <> u_ram(1).io.wr |
| 148 | + io.polyvec1_rd <> u_ram(2).io.rd |
| 149 | + io.polyvec1_wr <> u_ram(2).io.wr |
| 150 | + io.polyvec2_rd <> u_ram(0).io.rd |
| 151 | + io.polyvec2_wr <> u_ram(0).io.wr |
| 152 | + } .otherwise { |
| 153 | + io := DontCare |
| 154 | + u_ram(0).io := DontCare |
| 155 | + u_ram(1).io := DontCare |
| 156 | + u_ram(2).io := DontCare |
| 157 | + } |
| 158 | +} |
0 commit comments