-
Notifications
You must be signed in to change notification settings - Fork 1
/
PS2.scala
225 lines (192 loc) · 6.71 KB
/
PS2.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package MySpinalHardware
import spinal.core._
import spinal.lib._
import spinal.lib.fsm._
import spinal.core.sim._
import java.io.FileReader
import scala.util.control.Breaks
//import _root_.com.opencsv._
import spinal.lib.misc.Timer
class PS2_Keyboard(Timeout: BigInt) extends Component
{
val io = new Bundle {
val clock_in = in Bool()
val data_in = in Bool()
val output = master Stream(Bits(8 bits))
}
val data_out = Stream(Bits(8 bits))
val outFIFO = StreamFifo(
dataType = Bits(8 bits),
depth = 16
)
val data = Reg(Bits(11 bits)) init(0)
val dataLatch = Reg(Bits(8 bits)) init(0)
val bitCount = Reg(UInt(4 bits)) init(0)
val bitTimeOut = Counter(Timeout+10)
val parity = Reg(Bool()) init(False)
outFIFO.io.push << data_out
outFIFO.io.pop >> io.output
data_out.payload := data(8 downto 1)
data_out.valid := False
when(bitCount > 0){
bitTimeOut.increment()
}
when(io.clock_in.rise()){
data := io.data_in ## data(10 downto 1)
bitCount := bitCount + 1
bitTimeOut.clear()
when(io.data_in && bitCount <= 0x8){
parity := !parity
}
}
when(bitCount === 0xB || bitTimeOut.willOverflow){
when(data(10) && !data(0) && !parity === data(9)){
dataLatch := data(8 downto 1)
data_out.valid := !bitTimeOut.willOverflow && data_out.ready
}
data := 0
bitTimeOut.clear()
bitCount := 0
parity := False
}
}
class PS2_Keyboard_Decoder(TimeoutAmt: BigInt) extends Component {
val io = new Bundle {
val clock_in = in Bool()
val data_in = in Bool()
val keyCode = master Stream(Bits(8 bits))
}
val ps2Key = new PS2_Keyboard(100)
ps2Key.io.clock_in := io.clock_in
ps2Key.io.data_in := io.data_in
ps2Key.io.output.ready := False
val keyCode_out = Stream(Bits(8 bits))
val keyCodeFIFO = StreamFifo(
dataType = Bits(8 bits),
depth = 16
)
keyCodeFIFO.io.push << keyCode_out
keyCodeFIFO.io.pop >> io.keyCode
keyCode_out.valid := False
val Shift = Reg(Bool()) init(False)
val Ctrl = Reg(Bool()) init(False)
val DecoderAddr = Reg(Bits(9 bits)) init(0)
val DecoderAddrNext = Ctrl ## Shift ## ps2Key.io.output.payload(6 downto 0);
val keyDecoderRom = new RamInit("./data/keys.bin", 9)
keyDecoderRom.io.addra := DecoderAddr
keyDecoderRom.io.dina := 0x00
keyDecoderRom.io.ena := True
keyDecoderRom.io.wea := 0
keyCode_out.payload := keyDecoderRom.io.douta
val fsm = new StateMachine {
val Init: State = new State with EntryPoint {
whenIsActive {
goto(WaitForData)
}
}
val WaitForData: State = new State {
whenIsActive {
when(ps2Key.io.output.valid){
when(ps2Key.io.output.payload === 0xF0){
goto(KeyEnd)
}elsewhen(ps2Key.io.output.payload === 0xE0){
goto(ExtendedKey)
}elsewhen(ps2Key.io.output.payload === 0x12 || ps2Key.io.output.payload === 0x59){
Shift := True
}elsewhen(ps2Key.io.output.payload === 0x14){
Ctrl := True
}otherwise{
DecoderAddr := DecoderAddrNext
goto(KeyPressed)
}
ps2Key.io.output.ready := True
}
}
}
val KeyPressed: State = new StateDelay(2) {
whenCompleted {
when(keyDecoderRom.io.douta.asUInt >= 0x08){
keyCode_out.valid := keyCode_out.ready
}
goto(WaitForData)
}
}
val ExtendedKey: State = new State {
whenIsActive {
when(ps2Key.io.output.valid){
when(ps2Key.io.output.payload === 0xF0){
goto(KeyEnd)
}otherwise{
when(ps2Key.io.output.payload === 0x14){
Ctrl := True
goto(WaitForData)
} otherwise {
DecoderAddr := DecoderAddrNext
goto(ExtendedKeyPressed)
}
}
ps2Key.io.output.ready := True
}
}
}
val ExtendedKeyPressed: State = new StateDelay(2) {
whenCompleted {
when(keyDecoderRom.io.douta.asUInt =/= 0x00){
keyCode_out.valid := keyCode_out.ready
}
goto(WaitForData)
}
}
val KeyEnd: State = new State {
whenIsActive {
when(ps2Key.io.output.valid){
when(ps2Key.io.output.payload === 0x12 || ps2Key.io.output.payload === 0x59){
Shift := False
}elsewhen(ps2Key.io.output.payload === 0x14){
Ctrl := False
}
ps2Key.io.output.ready := True
goto(WaitForData)
}
}
}
}
}
// object PS2_Keyboard_Test {
// def main(args: Array[String]) {
// SimConfig.withFstWave.compile{
// val dut = new PS2_Keyboard_Decoder(100)
// dut
// }.doSim { dut =>
// //Fork a process to generate the reset and the clock on the dut
// dut.io.clock_in #= true
// dut.io.data_in #= true
// dut.clockDomain.forkStimulus(period = 10)
// val pathToFile = "data/PS2_abcdef.csv"
// val reader = new FileReader(pathToFile)
// val csvReader = new CSVReader(reader)
// var row: Array[String] = csvReader.readNext()
// dut.clockDomain.waitRisingEdge()
// var c = 0;
// val loop = new Breaks;
// loop.breakable {
// while (true) {
// if(row(1) == "1"){
// dut.io.clock_in #= true
// }else{
// dut.io.clock_in #= false
// }
// if(row(2) == "1"){
// dut.io.data_in #= true
// }else{
// dut.io.data_in #= false
// }
// row = csvReader.readNext()
// if(row == null) loop.break()
// dut.clockDomain.waitRisingEdge()
// }
// }
// reader.close()
// }
// }
// }