xref: /XiangShan/src/main/scala/device/AXI4Keyboard.scala (revision 8891a219bbc84f568e1d134854d8d5ed86d6d560)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
4c6d43980SLemover*
5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2.
6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at:
8c6d43980SLemover*          http://license.coscl.org.cn/MulanPSL2
9c6d43980SLemover*
10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13c6d43980SLemover*
14c6d43980SLemover* See the Mulan PSL v2 for more details.
15c6d43980SLemover***************************************************************************************/
16c6d43980SLemover
17ac67b1cbSZihao Yupackage device
18ac67b1cbSZihao Yu
19ac67b1cbSZihao Yuimport chisel3._
20ac67b1cbSZihao Yuimport chisel3.util._
21*8891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
22956d83c0Slinjiaweiimport freechips.rocketchip.diplomacy.AddressSet
23ac67b1cbSZihao Yuimport utils._
243c02ee8fSwakafaimport utility._
25ac67b1cbSZihao Yu
26ac67b1cbSZihao Yuclass KeyboardIO extends Bundle {
27ac67b1cbSZihao Yu  val ps2Clk = Input(Bool())
28ac67b1cbSZihao Yu  val ps2Data = Input(Bool())
29ac67b1cbSZihao Yu}
30ac67b1cbSZihao Yu
31ac67b1cbSZihao Yu// this Module is not tested
32956d83c0Slinjiaweiclass AXI4Keyboard
33956d83c0Slinjiawei(
34a2e9bde6SAllen  address: Seq[AddressSet]
35956d83c0Slinjiawei)(implicit p: Parameters)
36956d83c0Slinjiawei  extends AXI4SlaveModule(address, executable = false, _extra = new KeyboardIO)
37956d83c0Slinjiawei{
38956d83c0Slinjiawei  override lazy val module = new AXI4SlaveModuleImp[KeyboardIO](this){
39ac67b1cbSZihao Yu    val buf = Reg(UInt(10.W))
40ac67b1cbSZihao Yu    val ps2ClkLatch = RegNext(io.extra.get.ps2Clk)
41ac67b1cbSZihao Yu    val negedge = RegNext(ps2ClkLatch) && ~ps2ClkLatch
42ac67b1cbSZihao Yu    when (negedge) { buf := Cat(io.extra.get.ps2Data, buf(9,1)) }
43ac67b1cbSZihao Yu
44ac67b1cbSZihao Yu    val cnt = Counter(negedge, 10)
45ac67b1cbSZihao Yu    val queue = Module(new Queue(UInt(8.W), 8))
46ac67b1cbSZihao Yu    queue.io.enq.valid := cnt._2 && !buf(0) && io.extra.get.ps2Data && buf(9,1).xorR
47ac67b1cbSZihao Yu    queue.io.enq.bits := buf(8,1)
48ac67b1cbSZihao Yu    queue.io.deq.ready := in.r.ready
49ac67b1cbSZihao Yu
50ac67b1cbSZihao Yu    in.r.bits.data := Mux(queue.io.deq.valid, queue.io.deq.bits, 0.U)
51ac67b1cbSZihao Yu  }
52956d83c0Slinjiawei
53956d83c0Slinjiawei}
54