From 34c3b29d2d43ee00fa2fcdb4fea203275e329425 Mon Sep 17 00:00:00 2001
From: tracer-ysyx <tracer@ysyx.org>
Date: Wed, 10 Jan 2024 17:10:11 +0800
Subject: [PATCH] =?UTF-8?q?>=20configure(npc)=20=20ysyx=5F22040000=20?=
 =?UTF-8?q?=E6=9D=8E=E5=BF=83=E6=9D=A8=20=20Linux=20calcite=206.1.69=20#1-?=
 =?UTF-8?q?NixOS=20SMP=20PREEMPT=5FDYNAMIC=20Wed=20Dec=2020=2016:00:29=20U?=
 =?UTF-8?q?TC=202023=20x86=5F64=20GNU/Linux=20=20=2017:10:11=20=20up=202?=
 =?UTF-8?q?=20days=2016:10,=20=202=20users,=20=20load=20average:=200.52,?=
 =?UTF-8?q?=200.61,=200.56?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 npc/CMakeLists.txt                         |  2 +-
 npc/constr/Keyboard.nxdc                   |  3 ++-
 npc/core/src/main/scala/Keyboard.scala     | 15 ++++++++++-----
 npc/core/src/main/scala/Main.scala         |  2 +-
 npc/csrc_nvboard/{ => SegHandler}/main.cpp |  6 +++---
 5 files changed, 17 insertions(+), 11 deletions(-)
 rename npc/csrc_nvboard/{ => SegHandler}/main.cpp (83%)

diff --git a/npc/CMakeLists.txt b/npc/CMakeLists.txt
index 9385fe7..880181b 100644
--- a/npc/CMakeLists.txt
+++ b/npc/CMakeLists.txt
@@ -49,7 +49,7 @@ add_custom_command(
   DEPENDS ${CMAKE_SOURCE_DIR}/constr/${TOPMODULE}.nxdc 
 )
 
-add_executable(V${TOPMODULE}_nvboard csrc_nvboard/main.cpp auto_bind.cpp)
+add_executable(V${TOPMODULE}_nvboard csrc_nvboard/${TOPMODULE}/main.cpp auto_bind.cpp)
 
 verilate(V${TOPMODULE}_nvboard TRACE COVERAGE THREADS
   TOP_MODULE ${TOPMODULE}
diff --git a/npc/constr/Keyboard.nxdc b/npc/constr/Keyboard.nxdc
index 0cac67a..e615b2c 100644
--- a/npc/constr/Keyboard.nxdc
+++ b/npc/constr/Keyboard.nxdc
@@ -1,6 +1,7 @@
 top=Keyboard
 
-io_keycode_bits (SW7, SW6, SW5, SW4, SW3, SW2, SW1, SW0)
+io_ps2_clk PS2_CLK
+io_ps2_data PS2_DAT
 io_segs_0 (SEG0A, SEG0B, SEG0C, SEG0D, SEG0E, SEG0F, SEG0G, DEC0P)
 io_segs_1 (SEG1A, SEG1B, SEG1C, SEG1D, SEG1E, SEG1F, SEG1G, DEC1P)
 io_segs_2 (SEG2A, SEG2B, SEG2C, SEG2D, SEG2E, SEG2F, SEG2G, DEC2P)
diff --git a/npc/core/src/main/scala/Keyboard.scala b/npc/core/src/main/scala/Keyboard.scala
index 7fd22ab..98c0c36 100644
--- a/npc/core/src/main/scala/Keyboard.scala
+++ b/npc/core/src/main/scala/Keyboard.scala
@@ -1,7 +1,7 @@
 package npc.keyboard
 
 import chisel3._
-import chisel3.util.{Counter, Decoupled, Queue, Reverse, MuxLookup}
+import chisel3.util.{Counter, Decoupled, Queue, Reverse, MuxLookup, RegEnable}
 
 import npc.seg._
 import upickle.implicits.key
@@ -62,11 +62,15 @@ class SegGenerator(seg_count: Int) extends Module {
     val keycode = Flipped(Decoupled(UInt(8.W)))
     val segs = Output(Vec(seg_count, UInt(8.W)))
   })
-  io.keycode.ready := DontCare
+  val counter = Counter(0xFF)
+
+  io.keycode.ready := false.B
+  when(io.keycode.valid) {
+    io.keycode.ready := true.B
+  }
 
   val seg_regs = RegInit(VecInit(Seq.fill(seg_count)(0.U(8.W))))
   val last_keycode = RegInit(0.U(8.W))
-  val (counter, _) = Counter(0 to 0xFF, clock.asBool, reset.asBool)
   val digit_to_seg = ((0 until 16).map(_.U)).zip(Seq(
     "b00000011".U, "b10011111".U, "b00100101".U, "b00001101".U,
     "b10011001".U, "b01001001".U, "b01000001".U, "b00011111".U,
@@ -83,13 +87,14 @@ class SegGenerator(seg_count: Int) extends Module {
     0x25.U, 0x2E.U, 0x36.U, 0x3D.U, 0x3E.U, 0x46.U,
   ))
 
-  val keycode = io.keycode.bits
+  // val keycode = Mux(io.keycode.ready && io.keycode.valid, io.keycode.bits, keycode)
+  val keycode = RegEnable(io.keycode.bits, 0.U, io.keycode.ready && io.keycode.valid)
   val keycode_digits = VecInit(keycode(3,0)) ++ VecInit(keycode(7,4))
   val keycode_seg = keycode_digits.map(MuxLookup(_, 0xFF.U)(digit_to_seg))
   val ascii = MuxLookup(keycode, 0.U)(keycode_to_ascii)
   val ascii_digits = VecInit(ascii(3,0)) ++ VecInit(ascii(6,4))
   val ascii_seg = ascii_digits.map(MuxLookup(_, 0xFF.U)(digit_to_seg))
-  val count_digits = VecInit(counter(3,0)) ++ VecInit(counter(7,4))
+  val count_digits = VecInit(counter.value(3,0)) ++ VecInit(counter.value(7,4))
   val count_seg = count_digits.map(MuxLookup(_, 0xFF.U)(digit_to_seg))
 
   seg_regs := keycode_seg ++ ascii_seg ++ count_seg ++ Seq(0xFF.U, 0xFF.U)
diff --git a/npc/core/src/main/scala/Main.scala b/npc/core/src/main/scala/Main.scala
index 3116d12..78f24ae 100644
--- a/npc/core/src/main/scala/Main.scala
+++ b/npc/core/src/main/scala/Main.scala
@@ -88,7 +88,7 @@ class Keyboard extends Module {
     val segs = Output(Vec(3, UInt(8.W)))
   })
 
-  val seg_handler = Module(new SegHandler)
+  val seg_handler = Module(new SegGenerator(seg_count = 8))
   val keyboard_controller = Module(new KeyboardController)
 
   seg_handler.io.keycode <> keyboard_controller.io.out
diff --git a/npc/csrc_nvboard/main.cpp b/npc/csrc_nvboard/SegHandler/main.cpp
similarity index 83%
rename from npc/csrc_nvboard/main.cpp
rename to npc/csrc_nvboard/SegHandler/main.cpp
index d8eb0a2..76ba884 100644
--- a/npc/csrc_nvboard/main.cpp
+++ b/npc/csrc_nvboard/SegHandler/main.cpp
@@ -45,9 +45,9 @@ int main(int argc, char **argv, char **env) {
   while (true) {
     nvboard_update();
     cycle(top, [&] {
-      if (keycode != top->io_keycode_bits) {
-        printf("keycode: 0x%x display: %x %x\n", top->io_keycode_bits, top->io_segs_1, top->io_segs_0);
-        keycode = top->io_keycode_bits;
+      if (keycode != top->io_ps2_data) {
+        keycode = top->io_ps2_data;
+        printf("%d\n", keycode);
       }
     });
   }