> configure(npc)
ysyx_22040000 李心杨 Linux calcite 6.1.69 #1-NixOS SMP PREEMPT_DYNAMIC Wed Dec 20 16:00:29 UTC 2023 x86_64 GNU/Linux 20:30:51 up 1 day 19:31, 2 users, load average: 0.75, 0.84, 0.73
This commit is contained in:
parent
d02c8f5681
commit
45b0983c4a
21 changed files with 290 additions and 780 deletions
110
npc/core/src/main/scala/Keyboard.scala
Normal file
110
npc/core/src/main/scala/Keyboard.scala
Normal file
|
@ -0,0 +1,110 @@
|
|||
package npc.keyboard
|
||||
|
||||
import chisel3._
|
||||
import chisel3.util.{Counter, Decoupled, Queue, Reverse, MuxLookup}
|
||||
|
||||
import npc.seg._
|
||||
|
||||
class PS2Port extends Bundle {
|
||||
val clk = Input(Bool())
|
||||
val data = Input(UInt(1.W))
|
||||
}
|
||||
|
||||
object PS2Port {
|
||||
def apply(): PS2Port = {
|
||||
new PS2Port
|
||||
}
|
||||
}
|
||||
|
||||
class KeyboardController extends Module {
|
||||
val io = IO(new Bundle {
|
||||
val ps2 = PS2Port()
|
||||
val out = Decoupled(UInt(8.W))
|
||||
})
|
||||
// valid only on the clock negedge of ps2_clk
|
||||
val ps2_clk_valid = RegNext(io.ps2.clk, false.B) & ~io.ps2.clk
|
||||
val cycle_counter = Counter(11)
|
||||
val concated_data = RegInit(0.U(8.W))
|
||||
|
||||
val queue_io = Wire(Flipped(Decoupled(UInt(8.W))))
|
||||
val queue = Queue(queue_io, entries = 8)
|
||||
val received = RegInit(Bool(), false.B)
|
||||
val pushed = RegNext(queue_io.valid && queue_io.ready, false.B)
|
||||
queue_io.valid := false.B
|
||||
queue_io.bits := Reverse(concated_data)
|
||||
io.out <> queue
|
||||
|
||||
when(cycle_counter.value === 0.U) {
|
||||
concated_data := 0.U
|
||||
received := false.B
|
||||
}
|
||||
|
||||
when(ps2_clk_valid) {
|
||||
when(cycle_counter.value < 9.U && cycle_counter.value >= 1.U) {
|
||||
concated_data := (concated_data << 1) | io.ps2.data
|
||||
}.elsewhen(cycle_counter.value === 9.U) {
|
||||
received := true.B
|
||||
}
|
||||
cycle_counter.inc()
|
||||
}
|
||||
|
||||
when(!pushed && received) {
|
||||
queue_io.valid := true.B
|
||||
}.elsewhen(pushed && received) {
|
||||
queue_io.valid := false.B
|
||||
received := false.B
|
||||
}
|
||||
}
|
||||
|
||||
class SegHandler(seg_count: Int) extends Module {
|
||||
val io = IO(new Bundle {
|
||||
val keycode = Flipped(Decoupled(UInt(8.W)))
|
||||
val segs = Output(Vec(seg_count, UInt(4.W)))
|
||||
})
|
||||
|
||||
|
||||
val seg_regs = RegInit(VecInit(Seq.fill(seg_count)(0.U(4.W))))
|
||||
val last_keycode = RegInit(0.U(8.W))
|
||||
val counter = Counter(0xFF)
|
||||
val digit_to_seg = Seq(
|
||||
0.U -> "b0111111".U, // 0
|
||||
1.U ->"b0000110".U, // 1
|
||||
2.U -> "b1011011".U, // 2
|
||||
3.U -> "b1001111".U, // 3
|
||||
4.U -> "b1100110".U, // 4
|
||||
5.U -> "b1101101".U, // 5
|
||||
6.U -> "b1111101".U, // 6
|
||||
7.U -> "b0000111".U, // 7
|
||||
8.U -> "b1111111".U, // 8
|
||||
9.U -> "b1101111".U, // 9
|
||||
10.U -> "b1110111".U, // A
|
||||
11.U -> "b1111100".U, // B
|
||||
12.U -> "b0111001".U, // C
|
||||
13.U -> "b1011110".U, // D
|
||||
14.U -> "b1111001".U, // E
|
||||
15.U -> "b1110001".U // F
|
||||
)
|
||||
io.segs := seg_regs
|
||||
|
||||
when(io.keycode.valid) {
|
||||
val data = io.keycode.bits
|
||||
val state_f0_received = RegNext(data === 0xF0.U, false.B)
|
||||
io.keycode.ready := true.B
|
||||
// Handle keycode based on current state
|
||||
// (keyboard press counter) :: (ASCII code) :: (Keycode)
|
||||
when(state_f0_received) {
|
||||
// Release code
|
||||
}.otherwise{
|
||||
counter.inc()
|
||||
last_keycode := io.keycode.bits
|
||||
}
|
||||
}.otherwise {
|
||||
io.keycode.ready := false.B
|
||||
}
|
||||
|
||||
seg_regs := Seq(counter.value, last_keycode, last_keycode).map(d => {
|
||||
MuxLookup(d & 0xF.U, 0.U)(digit_to_seg) | (MuxLookup((d >> 4.U) & 0xF.U, 0.U)(digit_to_seg) << 4.U)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
package npc
|
||||
|
||||
import chisel3._
|
||||
import chisel3.util.{MuxLookup, Fill}
|
||||
import chisel3.util.{MuxLookup, Fill, Decoupled, Counter, Queue, Reverse}
|
||||
import chisel3.stage.ChiselOption
|
||||
|
||||
class RegisterFile(readPorts: Int) extends Module {
|
||||
|
@ -56,20 +56,6 @@ class ALUGenerator(width: Int) extends Module {
|
|||
))
|
||||
}
|
||||
|
||||
class MuxGenerator(width: Int, nInput: Int) extends Module {
|
||||
require(width >= 0)
|
||||
require(nInput >= 1)
|
||||
require(nInput.toBinaryString.map(_ - '0').sum == 1)
|
||||
|
||||
val io = IO(new Bundle {
|
||||
val in = Input(Vec(nInput, UInt(width.W)))
|
||||
val sel = Input(UInt(nInput.toBinaryString.reverse.indexOf('1').W))
|
||||
val out = Output(UInt(width.W))
|
||||
})
|
||||
|
||||
io.out := io.in(io.sel)
|
||||
}
|
||||
|
||||
class Test extends Module {
|
||||
val io = IO(new Bundle {
|
||||
val in = Input(UInt(32.W))
|
||||
|
@ -93,3 +79,20 @@ class Switch extends Module {
|
|||
|
||||
io.out := io.sw(0) ^ io.sw(1)
|
||||
}
|
||||
|
||||
import npc.keyboard._
|
||||
|
||||
class Keyboard extends Module {
|
||||
val io = IO(new Bundle {
|
||||
val ps2 = PS2Port()
|
||||
val segs = Output(Vec(6, UInt(4.W)))
|
||||
})
|
||||
|
||||
val keyboard_controller = new KeyboardController
|
||||
val seg_handler = new SegHandler(6)
|
||||
|
||||
seg_handler.io.keycode <> keyboard_controller.io.out
|
||||
|
||||
io <> keyboard_controller.io
|
||||
io <> seg_handler.io
|
||||
}
|
35
npc/core/src/main/scala/SegGenerator.scala
Normal file
35
npc/core/src/main/scala/SegGenerator.scala
Normal file
|
@ -0,0 +1,35 @@
|
|||
package npc.seg
|
||||
|
||||
import chisel3._
|
||||
import chisel3.util.{Decoupled}
|
||||
import chisel3.util.log2Ceil
|
||||
|
||||
class SegInput(width: Int) extends Bundle {
|
||||
require(width > 0)
|
||||
val addr = UInt(width.W)
|
||||
val value = UInt(log2Ceil(width).W)
|
||||
}
|
||||
|
||||
object SegInput {
|
||||
def apply(width: Int): SegInput = {
|
||||
return new SegInput(width)
|
||||
}
|
||||
}
|
||||
|
||||
class SegGenerator(width: Int) extends {
|
||||
val io = IO(new Bundle {
|
||||
val write = Flipped(Decoupled(SegInput(8)))
|
||||
val segs = Output(Vec(width, UInt(8.W)))
|
||||
})
|
||||
|
||||
val seg_regs = RegInit(VecInit(Seq.fill(width)(0.U(8.W))))
|
||||
io.segs := seg_regs
|
||||
|
||||
when(io.write.valid) {
|
||||
val data = io.write.bits
|
||||
seg_regs(data.addr) := data.value
|
||||
io.write.ready := true.B
|
||||
}.otherwise {
|
||||
io.write.ready := false.B
|
||||
}
|
||||
}
|
64
npc/core/src/test/scala/Keyboard.scala
Normal file
64
npc/core/src/test/scala/Keyboard.scala
Normal file
|
@ -0,0 +1,64 @@
|
|||
package npc.keyboard
|
||||
|
||||
import chisel3._
|
||||
import chiseltest._
|
||||
import org.scalatest.freespec.AnyFreeSpec
|
||||
import chiseltest.simulator.WriteVcdAnnotation
|
||||
|
||||
class KeyboardControllerSpec extends AnyFreeSpec with ChiselScalatestTester {
|
||||
def transfer(keycode: Int, clock: Clock, ps2: PS2Port) : Unit = {
|
||||
require(keycode >= 0 && keycode < 0xFF)
|
||||
var cycle = 0
|
||||
var keycode_remain = keycode << 1 // Shift 1 to do nothing at cycle 0
|
||||
var keycode_collect = 0
|
||||
|
||||
ps2.data.poke(1)
|
||||
ps2.clk.poke(true)
|
||||
clock.step(1)
|
||||
for (cycle <- 0 until 9) {
|
||||
val last_digit = keycode_remain & 1
|
||||
ps2.clk.poke(true)
|
||||
ps2.data.poke(last_digit)
|
||||
clock.step(32)
|
||||
keycode_collect = keycode_collect | (last_digit << cycle)
|
||||
keycode_remain = keycode_remain >> 1
|
||||
ps2.clk.poke(false)
|
||||
clock.step(32)
|
||||
}
|
||||
for (_ <- 9 until 11) {
|
||||
ps2.clk.poke(true)
|
||||
clock.step(32)
|
||||
ps2.clk.poke(false)
|
||||
clock.step(32)
|
||||
}
|
||||
assert(keycode_collect >> 1 == keycode)
|
||||
clock.step(32)
|
||||
}
|
||||
"Simple test" in {
|
||||
test(new KeyboardController).withAnnotations(Seq(WriteVcdAnnotation)) { c =>
|
||||
val data = Array(0xE4, 0xD4, 0xC4, 0xA9)
|
||||
data.foreach(d => {
|
||||
transfer(d, c.clock, c.io.ps2)
|
||||
c.io.out.valid.expect(1.U)
|
||||
c.io.out.bits.expect(d)
|
||||
c.io.out.ready.poke(1)
|
||||
c.clock.step(1)
|
||||
c.io.out.ready.poke(0)
|
||||
})
|
||||
data.foreach(d => {
|
||||
transfer(d, c.clock, c.io.ps2)
|
||||
})
|
||||
data.foreach(d => {
|
||||
c.io.out.valid.expect(1.U)
|
||||
c.io.out.bits.expect(d)
|
||||
c.io.out.ready.poke(1)
|
||||
c.clock.step(1)
|
||||
c.io.out.ready.poke(0)
|
||||
})
|
||||
}
|
||||
}
|
||||
"Keyboard Simulation" in {
|
||||
test(new Keyboard) { c =>
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package npc
|
|||
import chisel3._
|
||||
import chiseltest._
|
||||
import org.scalatest.freespec.AnyFreeSpec
|
||||
import chiseltest.simulator.WriteVcdAnnotation
|
||||
|
||||
class RegisterFileSpec extends AnyFreeSpec with ChiselScalatestTester {
|
||||
"RegisterFile should work" - {
|
||||
|
@ -41,97 +42,62 @@ class RegisterFileSpec extends AnyFreeSpec with ChiselScalatestTester {
|
|||
|
||||
class ALUGeneratorSpec extends AnyFreeSpec with ChiselScalatestTester {
|
||||
"With 32 width, " - {
|
||||
val neg = (x: BigInt) => BigInt("FFFFFFFF", 16) - x + 1
|
||||
val not = (x: BigInt) => x ^ BigInt("FFFFFFFF", 16)
|
||||
val mask = BigInt("FFFFFFFF", 16)
|
||||
val oprands: List[(BigInt, BigInt)] = List(
|
||||
(5, 3), (101010, 101010), (0xFFFFFFFCL, 0xFFFFFFFFL), (4264115, 2)
|
||||
)
|
||||
val operations: Map[Int, (BigInt, BigInt) => BigInt] = Map(
|
||||
0 -> ((a: BigInt, b: BigInt) => (a + b) & mask),
|
||||
1 -> ((a: BigInt, b: BigInt) => (a + neg(b)) & mask),
|
||||
2 -> ((a, _) => not(a)),
|
||||
3 -> (_ & _),
|
||||
4 -> (_ | _),
|
||||
5 -> (_ ^ _),
|
||||
6 -> ((a, b) => if (a < b) 1 else 0),
|
||||
7 -> ((a, b) => if (a == b) 1 else 0),
|
||||
)
|
||||
val validate = (c: ALUGenerator,op: Int, oprands: List[(BigInt, BigInt)]) => {
|
||||
c.io.op.poke(op.U)
|
||||
oprands.foreach({ case (a, b) =>
|
||||
c.io.a.poke(a.U)
|
||||
c.io.b.poke(b.U)
|
||||
c.io.out.expect(operations(op)(a, b))
|
||||
})
|
||||
}
|
||||
"add should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(0.U)
|
||||
c.io.a.poke(5.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(8.U)
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 0, oprands) }
|
||||
}
|
||||
"sub should work" - {
|
||||
"with positive result" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(1.U)
|
||||
c.io.a.poke(5.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(2)
|
||||
validate(c, 1, oprands.filter({case (a, b) => a >= b}))
|
||||
}
|
||||
}
|
||||
"with negative result" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(1.U)
|
||||
c.io.a.poke(3.U)
|
||||
c.io.b.poke(5.U)
|
||||
c.io.out.expect(BigInt("FFFFFFFF", 16) - 1)
|
||||
validate(c, 1, oprands.filter({case (a, b) => a < b}))
|
||||
}
|
||||
}
|
||||
}
|
||||
"not should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(2.U)
|
||||
c.io.a.poke(5.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(BigInt("FFFFFFFA", 16))
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 2, oprands) }
|
||||
}
|
||||
"and should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(3.U)
|
||||
c.io.a.poke(5.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(1.U)
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 3, oprands) }
|
||||
}
|
||||
"or should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(4.U)
|
||||
c.io.a.poke(5.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(7.U)
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 4, oprands) }
|
||||
}
|
||||
"xor should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(5.U)
|
||||
c.io.a.poke(5.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(6.U)
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 5, oprands) }
|
||||
}
|
||||
"compare should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(6)
|
||||
|
||||
c.io.a.poke(62.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(0.U)
|
||||
|
||||
c.io.a.poke(2.U)
|
||||
c.io.b.poke(103.U)
|
||||
c.io.out.expect(1.U)
|
||||
|
||||
c.io.a.poke(16.U)
|
||||
c.io.b.poke(16.U)
|
||||
c.io.out.expect(0.U)
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 6, oprands) }
|
||||
}
|
||||
"equal should work" in {
|
||||
test(new ALUGenerator(32)) { c =>
|
||||
c.io.op.poke(7)
|
||||
|
||||
c.io.a.poke(62.U)
|
||||
c.io.b.poke(3.U)
|
||||
c.io.out.expect(0.U)
|
||||
|
||||
c.io.a.poke(2.U)
|
||||
c.io.b.poke(103.U)
|
||||
c.io.out.expect(0.U)
|
||||
|
||||
c.io.a.poke(16.U)
|
||||
c.io.b.poke(16.U)
|
||||
c.io.out.expect(1.U)
|
||||
}
|
||||
test(new ALUGenerator(32)) { c => validate(c, 7, oprands) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue