xref: /XiangShan/src/main/scala/device/AXI4Keyboard.scala (revision c6d439803a044ea209139672b25e35fe8d7f4aa0)
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 device
17
18import chisel3._
19import chisel3.util._
20import chipsalliance.rocketchip.config.Parameters
21import freechips.rocketchip.diplomacy.AddressSet
22import utils._
23
24class KeyboardIO extends Bundle {
25  val ps2Clk = Input(Bool())
26  val ps2Data = Input(Bool())
27}
28
29// this Module is not tested
30class AXI4Keyboard
31(
32  address: Seq[AddressSet]
33)(implicit p: Parameters)
34  extends AXI4SlaveModule(address, executable = false, _extra = new KeyboardIO)
35{
36  override lazy val module = new AXI4SlaveModuleImp[KeyboardIO](this){
37    val buf = Reg(UInt(10.W))
38    val ps2ClkLatch = RegNext(io.extra.get.ps2Clk)
39    val negedge = RegNext(ps2ClkLatch) && ~ps2ClkLatch
40    when (negedge) { buf := Cat(io.extra.get.ps2Data, buf(9,1)) }
41
42    val cnt = Counter(negedge, 10)
43    val queue = Module(new Queue(UInt(8.W), 8))
44    queue.io.enq.valid := cnt._2 && !buf(0) && io.extra.get.ps2Data && buf(9,1).xorR
45    queue.io.enq.bits := buf(8,1)
46    queue.io.deq.ready := in.r.ready
47
48    in.r.bits.data := Mux(queue.io.deq.valid, queue.io.deq.bits, 0.U)
49  }
50
51}
52