OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [rtl/] [soc/] [ps2/] [ps2.v] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/*
2
 * Copyright (c) 2014, Aleksander Osman
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * * Redistributions of source code must retain the above copyright notice, this
9
 *   list of conditions and the following disclaimer.
10
 *
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 *   this list of conditions and the following disclaimer in the documentation
13
 *   and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
 
27
module ps2(
28
    input                   clk,
29
    input                   rst_n,
30
 
31
    output reg              irq_keyb,
32
    output reg              irq_mouse,
33
 
34
    //io slave 0x60-0x67
35
    input       [2:0]       io_address,
36
    input                   io_read,
37
    output reg  [7:0]       io_readdata,
38
    input                   io_write,
39
    input       [7:0]       io_writedata,
40
 
41
    //io slave 0x90-0x9F
42
    input       [3:0]       sysctl_address,
43
    input                   sysctl_read,
44
    output reg  [7:0]       sysctl_readdata,
45
    input                   sysctl_write,
46
    input       [7:0]       sysctl_writedata,
47
 
48
    //speaker port 61h
49
    output                  speaker_61h_read,
50
    input       [7:0]       speaker_61h_readdata,
51
    output                  speaker_61h_write,
52
    output      [7:0]       speaker_61h_writedata,
53
 
54
    //output port
55
    output reg              output_a20_enable,
56
    output reg              output_reset_n,
57
 
58
    //ps2 keyboard
59
    inout                   ps2_kbclk,
60
    inout                   ps2_kbdat,
61
 
62
    //ps2 mouse
63
    inout                   ps2_mouseclk,
64
    inout                   ps2_mousedat
65
);
66
 
67
//------------------------------------------------------------------------------
68
 
69
reg io_read_last;
70
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) io_read_last <= 1'b0; else if(io_read_last) io_read_last <= 1'b0; else io_read_last <= io_read; end
71
wire io_read_valid = io_read && io_read_last == 1'b0;
72
 
73
//------------------------------------------------------------------------------
74
 
75
assign speaker_61h_read         = io_read_valid && io_address == 3'd1;
76
assign speaker_61h_write        = io_write && io_address == 3'd1;
77
assign speaker_61h_writedata    = io_writedata;
78
 
79
//------------------------------------------------------------------------------ io read
80
 
81
wire [7:0] io_readdata_next =
82
    (io_read_valid && io_address == 3'd1)?        speaker_61h_readdata :
83
    (io_read_valid && io_address == 3'd4)? {
84
        status_keyboardparityerror,
85
        status_timeout,
86
        status_mousebufferfull,
87
        1'b1, //keyboard inhibit
88
        status_lastcommand,
89
        status_system,
90
        status_inputbufferfull,
91
        status_outputbufferfull
92
    } :
93
    (status_mousebufferfull)?               mouse_fifo_q :
94
                                            keyb_fifo_q_final;
95
 
96
always @(posedge clk or negedge rst_n) begin
97
    if(rst_n == 1'b0)   io_readdata <= 8'd0;
98
    else                io_readdata <= io_readdata_next;
99
end
100
 
101
//------------------------------------------------------------------------------ sysctl read
102
 
103
wire [7:0] sysctl_readdata_next =
104
    (sysctl_address == 4'h2)?       { 6'd0, output_a20_enable, 1'b0 } :
105
                                    8'hFF;
106
 
107
always @(posedge clk or negedge rst_n) begin
108
    if(rst_n == 1'b0)   sysctl_readdata <= 8'd0;
109
    else                sysctl_readdata <= sysctl_readdata_next;
110
end
111
 
112
//------------------------------------------------------------------------------ output
113
 
114
always @(posedge clk or negedge rst_n) begin
115
    if(rst_n == 1'b0)                                   output_a20_enable <= 1'b1;
116
    else if(cmd_write_output_port)                      output_a20_enable <= io_writedata[1];
117
    else if(cmd_disable_a20)                            output_a20_enable <= 1'b0;
118
    else if(cmd_enable_a20)                             output_a20_enable <= 1'b1;
119
    else if(sysctl_write && sysctl_address == 4'h2)     output_a20_enable <= sysctl_writedata[1];
120
end
121
 
122
always @(posedge clk or negedge rst_n) begin
123
    if(rst_n == 1'b0)                                                       output_reset_n <= 1'b1;
124
    else if(cmd_write_output_port)                                          output_reset_n <= io_writedata[0];
125
    else if(cmd_reset)                                                      output_reset_n <= 1'b0;
126
    else if(sysctl_write && sysctl_address == 4'h2 && sysctl_writedata[0])  output_reset_n <= 1'b0;
127
    else                                                                    output_reset_n <= 1'b1;
128
end
129
 
130
//------------------------------------------------------------------------------
131
 
132
reg status_keyboardparityerror;
133
always @(posedge clk or negedge rst_n) begin
134
    if(rst_n == 1'b0)                                       status_keyboardparityerror <= 1'b0;
135
    else if(keyb_recv_parity_err || mouse_recv_parity_err)  status_keyboardparityerror <= 1'b1;
136
    else if(io_read_valid && io_address == 3'd4)            status_keyboardparityerror <= 1'b0;
137
end
138
 
139
reg status_timeout;
140
always @(posedge clk or negedge rst_n) begin
141
    if(rst_n == 1'b0)                                   status_timeout <= 1'b0;
142
    else if(keyb_timeout_reset || mouse_timeout_reset)  status_timeout <= 1'b1;
143
    else if(io_read_valid && io_address == 3'd4)        status_timeout <= 1'b0;
144
end
145
 
146
reg status_lastcommand;
147
always @(posedge clk or negedge rst_n) begin
148
    if(rst_n == 1'b0)                           status_lastcommand <= 1'b1;
149
    else if(io_write && io_address == 3'd0)     status_lastcommand <= 1'b0;
150
    else if(io_write && io_address == 3'd4)     status_lastcommand <= 1'b1;
151
end
152
 
153
reg translate;
154
always @(posedge clk or negedge rst_n) begin
155
    if(rst_n == 1'b0)                   translate <= 1'b1;
156
    else if(cmd_write_command_byte)     translate <= io_writedata[6];
157
end
158
 
159
reg disable_mouse;
160
always @(posedge clk or negedge rst_n) begin
161
    if(rst_n == 1'b0)                   disable_mouse <= 1'b1;
162
    else if(cmd_write_command_byte)     disable_mouse <= io_writedata[5];
163
    else if(cmd_disable_mouse)          disable_mouse <= 1'b1;
164
    else if(cmd_enable_mouse)           disable_mouse <= 1'b0;
165
    else if(write_to_mouse)             disable_mouse <= 1'b0;
166
end
167
 
168
reg disable_mouse_visible;
169
always @(posedge clk or negedge rst_n) begin
170
    if(rst_n == 1'b0)                   disable_mouse_visible <= 1'b1;
171
    else if(cmd_write_command_byte)     disable_mouse_visible <= io_writedata[5];
172
    else if(cmd_disable_mouse)          disable_mouse_visible <= 1'b1;
173
    else if(cmd_enable_mouse)           disable_mouse_visible <= 1'b0;
174
end
175
 
176
reg disable_keyboard;
177
always @(posedge clk or negedge rst_n) begin
178
    if(rst_n == 1'b0)                   disable_keyboard <= 1'b0;
179
    else if(cmd_write_command_byte)     disable_keyboard <= io_writedata[4];
180
    else if(cmd_disable_keyb)           disable_keyboard <= 1'b1;
181
    else if(cmd_enable_keyb)            disable_keyboard <= 1'b0;
182
    else if(write_to_keyb)              disable_keyboard <= 1'b0;
183
end
184
 
185
reg status_system;
186
always @(posedge clk or negedge rst_n) begin
187
    if(rst_n == 1'b0)                   status_system <= 1'b0;
188
    else if(cmd_write_command_byte)     status_system <= io_writedata[2];
189
    else if(cmd_self_test)              status_system <= 1'b1;
190
end
191
 
192
reg allow_irq_mouse;
193
always @(posedge clk or negedge rst_n) begin
194
    if(rst_n == 1'b0)                   allow_irq_mouse <= 1'b1;
195
    else if(cmd_write_command_byte)     allow_irq_mouse <= io_writedata[1];
196
end
197
 
198
reg allow_irq_keyb;
199
always @(posedge clk or negedge rst_n) begin
200
    if(rst_n == 1'b0)                   allow_irq_keyb <= 1'b1;
201
    else if(cmd_write_command_byte)     allow_irq_keyb <= io_writedata[0];
202
end
203
 
204
//------------------------------------------------------------------------------ interrupts
205
 
206
always @(posedge clk or negedge rst_n) begin
207
    if(rst_n == 1'b0)                                                                                       irq_keyb <= 1'b0;
208
    else if(io_read_valid && io_address == 3'd0 && status_outputbufferfull && ~(status_mousebufferfull))    irq_keyb <= 1'b0;
209
    else if(allow_irq_keyb && status_outputbufferfull && ~(status_mousebufferfull))                         irq_keyb <= 1'b1;
210
end
211
 
212
always @(posedge clk or negedge rst_n) begin
213
    if(rst_n == 1'b0)                                                       irq_mouse <= 1'b0;
214
    else if(io_read_valid && io_address == 3'd0 && status_mousebufferfull)  irq_mouse <= 1'b0;
215
    else if(allow_irq_mouse && status_mousebufferfull)                      irq_mouse <= 1'b1;
216
end
217
 
218
wire outputbuffer_idle = ~(status_mousebufferfull) && ~(status_outputbufferfull);
219
 
220
reg status_mousebufferfull;
221
always @(posedge clk or negedge rst_n) begin
222
    if(rst_n == 1'b0)                                   status_mousebufferfull <= 1'b0;
223
    else if(io_read_valid && io_address == 3'd0)        status_mousebufferfull <= 1'b0;
224
    else if(outputbuffer_idle && ~(mouse_fifo_empty))   status_mousebufferfull <= 1'b1;
225
end
226
 
227
reg status_outputbufferfull;
228
always @(posedge clk or negedge rst_n) begin
229
    if(rst_n == 1'b0)                                                           status_outputbufferfull <= 1'b0;
230
    else if(io_read_valid && io_address == 3'd0)                                status_outputbufferfull <= 1'b0;
231
    else if(outputbuffer_idle && (~(mouse_fifo_empty) || ~(keyb_fifo_empty)))   status_outputbufferfull <= 1'b1;
232
end
233
 
234
//------------------------------------------------------------------------------ io write / controller commands
235
 
236
reg expecting_port_60h;
237
always @(posedge clk or negedge rst_n) begin
238
    if(rst_n == 1'b0)                           expecting_port_60h <= 1'b0;
239
    else if(io_write && io_address == 3'd0)     expecting_port_60h <= 1'b0;
240
    else if(cmd_with_param_first_byte)          expecting_port_60h <= 1'b1;
241
    else if(io_write && io_address == 3'd4)     expecting_port_60h <= 1'b0;
242
end
243
 
244
reg [7:0] last_command;
245
always @(posedge clk or negedge rst_n) begin
246
    if(rst_n == 1'b0)                           last_command <= 8'h00;
247
    else if(io_write && io_address == 3'd4)     last_command <= io_writedata;
248
end
249
 
250
wire cmd_with_param_first_byte  = io_write && io_address == 3'd4 && (
251
    io_writedata == 8'h60 || //write command byte
252
    io_writedata == 8'hCB || //write keyboard controller mode
253
    io_writedata == 8'hD1 || //write output port
254
    io_writedata == 8'hD3 || //write mouse output port
255
    io_writedata == 8'hD4 || //write to mouse
256
    io_writedata == 8'hD2    //write keyboard output buffer
257
);
258
 
259
wire cmd_with_param             = io_write && io_address == 3'd0 && expecting_port_60h && ~(status_inputbufferfull);
260
wire cmd_without_param          = io_write && io_address == 3'd4 && ~(cmd_with_param_first_byte);
261
 
262
wire cmd_write_command_byte     = cmd_with_param && last_command == 8'h60;
263
wire cmd_write_output_port      = cmd_with_param && last_command == 8'hD1;
264
wire cmd_write_to_keyb_output   = cmd_with_param && last_command == 8'hD2;
265
wire cmd_write_to_mouse_output  = cmd_with_param && last_command == 8'hD3;
266
wire cmd_write_to_mouse         = cmd_with_param && last_command == 8'hD4;
267
 
268
wire cmd_read_command_byte      = cmd_without_param && io_writedata == 8'h20;
269
wire cmd_disable_mouse          = cmd_without_param && io_writedata == 8'hA7;
270
wire cmd_enable_mouse           = cmd_without_param && io_writedata == 8'hA8;
271
wire cmd_test_mouse_port        = cmd_without_param && io_writedata == 8'hA9;
272
wire cmd_self_test              = cmd_without_param && io_writedata == 8'hAA;
273
wire cmd_interface_test         = cmd_without_param && io_writedata == 8'hAB;
274
wire cmd_disable_keyb           = cmd_without_param && io_writedata == 8'hAD;
275
wire cmd_enable_keyb            = cmd_without_param && io_writedata == 8'hAE;
276
wire cmd_read_input_port        = cmd_without_param && io_writedata == 8'hC0;
277
wire cmd_read_controller_mode   = cmd_without_param && io_writedata == 8'hCA;
278
wire cmd_read_output_port       = cmd_without_param && io_writedata == 8'hD0;
279
wire cmd_disable_a20            = cmd_without_param && io_writedata == 8'hDD;
280
wire cmd_enable_a20             = cmd_without_param && io_writedata == 8'hDF;
281
wire cmd_reset                  = cmd_without_param && io_writedata == 8'hFE;
282
 
283
//------------------------------------------------------------------------------ controller reply - not device
284
 
285
wire [7:0] command_byte = {
286
    1'b0,
287
    translate,
288
    disable_mouse_visible,
289
    disable_keyboard,
290
    1'b0,
291
    status_system,
292
    allow_irq_mouse,
293
    allow_irq_keyb
294
};
295
 
296
wire [7:0] output_port = {
297
    1'b0,
298
    1'b0,
299
    irq_mouse,
300
    irq_keyb,
301
    1'b0,
302
    1'b0,
303
    output_a20_enable,
304
    1'b1
305
};
306
 
307
reg [7:0] keyb_reply;
308
always @(posedge clk or negedge rst_n) begin
309
    if(rst_n == 1'b0)                   keyb_reply <= 8'h00;
310
    else if(cmd_write_to_keyb_output)   keyb_reply <= io_writedata;
311
    else if(cmd_read_command_byte)      keyb_reply <= command_byte;
312
    else if(cmd_test_mouse_port)        keyb_reply <= 8'h00;
313
    else if(cmd_self_test)              keyb_reply <= 8'h55;
314
    else if(cmd_interface_test)         keyb_reply <= 8'h00;
315
    else if(cmd_read_input_port)        keyb_reply <= 8'h80;
316
    else if(cmd_read_controller_mode)   keyb_reply <= 8'h01;
317
    else if(cmd_read_output_port)       keyb_reply <= output_port;
318
end
319
 
320
reg keyb_reply_valid;
321
always @(posedge clk or negedge rst_n) begin
322
    if(rst_n == 1'b0)                   keyb_reply_valid <= 1'b0;
323
    else if(cmd_write_to_keyb_output)   keyb_reply_valid <= 1'b1;
324
    else if(cmd_read_command_byte)      keyb_reply_valid <= 1'b1;
325
    else if(cmd_test_mouse_port)        keyb_reply_valid <= 1'b1;
326
    else if(cmd_self_test)              keyb_reply_valid <= 1'b1;
327
    else if(cmd_interface_test)         keyb_reply_valid <= 1'b1;
328
    else if(cmd_read_input_port)        keyb_reply_valid <= 1'b1;
329
    else if(cmd_read_controller_mode)   keyb_reply_valid <= 1'b1;
330
    else if(cmd_read_output_port)       keyb_reply_valid <= 1'b1;
331
    else if(ps2_kb_reply_done)          keyb_reply_valid <= 1'b0;
332
end
333
 
334
reg [7:0] mouse_reply;
335
always @(posedge clk or negedge rst_n) begin
336
    if(rst_n == 1'b0)                   mouse_reply <= 8'd0;
337
    else if(cmd_write_to_mouse_output)  mouse_reply <= io_writedata;
338
end
339
 
340
reg mouse_reply_valid;
341
always @(posedge clk or negedge rst_n) begin
342
    if(rst_n == 1'b0)                   mouse_reply_valid <= 1'b0;
343
    else if(cmd_write_to_mouse_output)  mouse_reply_valid <= 1'b1;
344
    else if(ps2_mouse_reply_done)       mouse_reply_valid <= 1'b0;
345
end
346
 
347
//------------------------------------------------------------------------------ write to device
348
 
349
wire write_to_keyb  = io_write && io_address == 3'd0 && ~(expecting_port_60h) && ~(status_inputbufferfull);
350
wire write_to_mouse = cmd_write_to_mouse;
351
 
352
reg status_inputbufferfull;
353
always @(posedge clk or negedge rst_n) begin
354
    if(rst_n == 1'b0)                                       status_inputbufferfull <= 1'b0;
355
    else if(write_to_keyb || write_to_mouse)                status_inputbufferfull <= 1'b1;
356
    else if(input_write_done && status_outputbufferfull)    status_inputbufferfull <= 1'b0;
357
end
358
 
359
reg input_write_done;
360
always @(posedge clk or negedge rst_n) begin
361
    if(rst_n == 1'b0)                                   input_write_done <= 1'b0;
362
    else if(write_to_keyb || write_to_mouse)            input_write_done <= 1'b0;
363
    else if(ps2_kb_write_done || ps2_mouse_write_done)  input_write_done <= 1'b1;
364
end
365
 
366
reg input_for_mouse;
367
always @(posedge clk or negedge rst_n) begin
368
    if(rst_n == 1'b0)       input_for_mouse <= 1'b0;
369
    else if(write_to_keyb)  input_for_mouse <= 1'b0;
370
    else if(write_to_mouse) input_for_mouse <= 1'b1;
371
end
372
 
373
reg [7:0] inputbuffer;
374
always @(posedge clk or negedge rst_n) begin
375
    if(rst_n == 1'b0)                                       inputbuffer <= 8'd0;
376
    else if(write_to_keyb || write_to_mouse)                inputbuffer <= io_writedata;
377
    else if(ps2_kb_write_shift || ps2_mouse_write_shift)    inputbuffer <= { 1'b0, inputbuffer[7:1] };
378
end
379
 
380
//------------------------------------------------------------------------------ ps/2 for keyboard
381
 
382
assign ps2_kbclk    = (ps2_kbclk_ena)?      ps2_kbclk_out       : 1'bZ;
383
assign ps2_kbdat    = (ps2_kbdat_ena)?      ps2_kbdat_out       : 1'bZ;
384
 
385
reg ps2_kbclk_ena;
386
always @(posedge clk or negedge rst_n) begin
387
    if(rst_n == 1'b0)                                                               ps2_kbclk_ena <= 1'b0;
388
    else if(keyb_timeout_reset)                                                     ps2_kbclk_ena <= 1'b0;
389
    else if(keyb_state == PS2_SEND_INHIBIT || keyb_state == PS2_WAIT_START)         ps2_kbclk_ena <= 1'b1;
390
    else if(keyb_state == PS2_SEND_CLOCK_RELEASE || keyb_state == PS2_WAIT_FINISH)  ps2_kbclk_ena <= 1'b0;
391
end
392
 
393
reg ps2_kbclk_out;
394
always @(posedge clk or negedge rst_n) begin
395
    if(rst_n == 1'b0)                                                           ps2_kbclk_out <= 1'b0;
396
    else if(keyb_state == PS2_SEND_INHIBIT || keyb_state == PS2_WAIT_START)     ps2_kbclk_out <= 1'b0;
397
end
398
 
399
reg ps2_kbdat_ena;
400
always @(posedge clk or negedge rst_n) begin
401
    if(rst_n == 1'b0)                                           ps2_kbdat_ena <= 1'b0;
402
    else if(keyb_timeout_reset)                                 ps2_kbdat_ena <= 1'b0;
403
    else if(keyb_state == PS2_SEND_DATA_LOW)                    ps2_kbdat_ena <= 1'b1;
404
    else if(ps2_kb_write_shift && keyb_bit_counter == 4'd9)     ps2_kbdat_ena <= 1'b0;  //stop bit
405
end
406
 
407
reg ps2_kbdat_out;
408
always @(posedge clk or negedge rst_n) begin
409
    if(rst_n == 1'b0)                                       ps2_kbdat_out <= 1'b0;
410
    else if(keyb_state == PS2_SEND_DATA_LOW)                ps2_kbdat_out <= 1'b0;              //start bit
411
    else if(ps2_kb_write_shift && keyb_bit_counter < 4'd8)  ps2_kbdat_out <= inputbuffer[0];    //data bits
412
    else if(ps2_kb_write_shift && keyb_bit_counter == 4'd8) ps2_kbdat_out <= ~(keyb_parity);    //parity bit
413
end
414
 
415
reg [15:0] keyb_clk_mv;
416
reg keyb_clk_mv_wait;
417
reg was_ps2_kbclk;
418
always @(posedge clk or negedge rst_n) begin
419
    if(rst_n == 1'b0) begin
420
        keyb_clk_mv         <= 16'd0;
421
        keyb_clk_mv_wait    <= 1'b0;
422
        was_ps2_kbclk       <= 1'b0;
423
    end
424
    else begin
425
        keyb_clk_mv <= { keyb_clk_mv[14:0], keyb_kbclk };
426
 
427
        if(keyb_clk_mv_wait == 1'b0 && keyb_clk_mv[15:12] == 4'b1111 && keyb_clk_mv[3:0] == 4'b0000) begin
428
            was_ps2_kbclk <= 1'b1;
429
            keyb_clk_mv_wait <= 1'b1;
430
        end
431
        else if(keyb_clk_mv_wait == 1'b1 && keyb_clk_mv[15:0] == 16'h0000) begin
432
            keyb_clk_mv_wait <= 1'b0;
433
            was_ps2_kbclk <= 1'b0;
434
        end
435
        else begin
436
            was_ps2_kbclk <= 1'b0;
437
        end
438
    end
439
end
440
 
441
reg keyb_kbclk;
442
always @(posedge clk or negedge rst_n) begin
443
    if(rst_n == 1'b0)   keyb_kbclk <= 1'b1;
444
    else                keyb_kbclk <= ps2_kbclk;
445
end
446
 
447
reg keyb_kbdat;
448
always @(posedge clk or negedge rst_n) begin
449
    if(rst_n == 1'b0)   keyb_kbdat <= 1'b1;
450
    else                keyb_kbdat <= ps2_kbdat;
451
end
452
 
453
reg [25:0] keyb_timeout;
454
always @(posedge clk or negedge rst_n) begin
455
    if(rst_n == 1'b0)                                                       keyb_timeout <= 26'h0;
456
    else if(keyb_state == PS2_SEND_INHIBIT || keyb_state == PS2_RECV_START) keyb_timeout <= 26'h3FFFFFF;
457
    else if(keyb_state == PS2_IDLE)                                         keyb_timeout <= 26'h0;
458
    else if(keyb_timeout > 26'd0)                                           keyb_timeout <= keyb_timeout - 26'd1;
459
end
460
 
461
wire keyb_timeout_reset = keyb_timeout == 26'd1;
462
 
463
reg [12:0] keyb_timer;
464
always @(posedge clk or negedge rst_n) begin
465
    if(rst_n == 1'b0)                           keyb_timer <= 13'd0;
466
    else if(keyb_timeout_reset)                 keyb_timer <= 13'd0;
467
 
468
    else if(keyb_state == PS2_SEND_INHIBIT)     keyb_timer <= 13'd8191;
469
    else if(keyb_state == PS2_WAIT_START)       keyb_timer <= 13'd8191;
470
 
471
    else if(keyb_timer > 13'd0)                 keyb_timer <= keyb_timer - 13'd1;
472
end
473
 
474
reg [3:0] keyb_bit_counter;
475
always @(posedge clk or negedge rst_n) begin
476
    if(rst_n == 1'b0)                               keyb_bit_counter <= 4'd0;
477
    else if(keyb_timeout_reset)                     keyb_bit_counter <= 4'd0;
478
 
479
    else if(keyb_state == PS2_SEND_CLOCK_RELEASE)   keyb_bit_counter <= 4'd0;
480
    else if(ps2_kb_write_shift)                     keyb_bit_counter <= keyb_bit_counter + 4'd1;
481
 
482
    else if(keyb_state == PS2_RECV_START)           keyb_bit_counter <= 4'd0;
483
    else if(keyb_recv)                              keyb_bit_counter <= keyb_bit_counter + 4'd1;
484
end
485
 
486
reg keyb_parity;
487
always @(posedge clk or negedge rst_n) begin
488
    if(rst_n == 1'b0)                               keyb_parity <= 1'b0;
489
 
490
    else if(keyb_state == PS2_SEND_CLOCK_RELEASE)   keyb_parity <= 1'b0;
491
    else if(ps2_kb_write_shift)                     keyb_parity <= keyb_parity ^ inputbuffer[0];
492
 
493
    else if(keyb_state == PS2_RECV_START)           keyb_parity <= 1'b0;
494
    else if(keyb_recv)                              keyb_parity <= keyb_parity ^ keyb_kbdat;
495
end
496
 
497
reg [7:0] keyb_recv_buffer;
498
always @(posedge clk or negedge rst_n) begin
499
    if(rst_n == 1'b0)                               keyb_recv_buffer <= 8'd0;
500
    else if(keyb_recv && keyb_bit_counter < 4'd8)   keyb_recv_buffer <= { keyb_kbdat, keyb_recv_buffer[7:1] };
501
end
502
 
503
wire keyb_recv              = keyb_state == PS2_RECV_BITS && was_ps2_kbclk;
504
wire keyb_recv_ok           = keyb_recv && keyb_bit_counter == 4'd8 && ~(keyb_parity) == keyb_kbdat;
505
wire keyb_recv_parity_err   = keyb_recv && keyb_bit_counter == 4'd8 && ~(keyb_parity) != keyb_kbdat;
506
 
507
reg keyb_recv_result;
508
always @(posedge clk or negedge rst_n) begin
509
    if(rst_n == 1'b0)                      keyb_recv_result <= 1'b0;
510
    else if(keyb_state == PS2_RECV_BITS)   keyb_recv_result <= keyb_recv_ok;
511
end
512
wire keyb_recv_final = keyb_state == PS2_RECV_WAIT_FOR_IDLE && keyb_kbclk == 1'b1 && keyb_kbdat == 1'b1 && keyb_recv_result;
513
 
514
reg keyb_translate_escape;
515
always @(posedge clk or negedge rst_n) begin
516
    if(rst_n == 1'b0)                                       keyb_translate_escape <= 1'b0;
517
    else if(keyb_timeout_reset)                             keyb_translate_escape <= 1'b0;
518
    else if(keyb_recv_ok && keyb_translate_escape == 1'b0)  keyb_translate_escape <= translate && keyb_recv_buffer == 8'hF0;
519
    else if(keyb_recv_final && keyb_recv_buffer != 8'hF0)   keyb_translate_escape <= 1'b0;
520
end
521
 
522
reg [7:0] trans;
523
always @(posedge clk or negedge rst_n) begin
524
    if(rst_n == 1'b0)   trans <= 8'd0;
525
    else begin
526
        case(keyb_recv_buffer)
527
            8'h00: trans <= 8'hff; 8'h01: trans <= 8'h43; 8'h02: trans <= 8'h41; 8'h03: trans <= 8'h3f; 8'h04: trans <= 8'h3d; 8'h05: trans <= 8'h3b; 8'h06: trans <= 8'h3c; 8'h07: trans <= 8'h58;
528
            8'h08: trans <= 8'h64; 8'h09: trans <= 8'h44; 8'h0A: trans <= 8'h42; 8'h0B: trans <= 8'h40; 8'h0C: trans <= 8'h3e; 8'h0D: trans <= 8'h0f; 8'h0E: trans <= 8'h29; 8'h0F: trans <= 8'h59;
529
            8'h10: trans <= 8'h65; 8'h11: trans <= 8'h38; 8'h12: trans <= 8'h2a; 8'h13: trans <= 8'h70; 8'h14: trans <= 8'h1d; 8'h15: trans <= 8'h10; 8'h16: trans <= 8'h02; 8'h17: trans <= 8'h5a;
530
            8'h18: trans <= 8'h66; 8'h19: trans <= 8'h71; 8'h1A: trans <= 8'h2c; 8'h1B: trans <= 8'h1f; 8'h1C: trans <= 8'h1e; 8'h1D: trans <= 8'h11; 8'h1E: trans <= 8'h03; 8'h1F: trans <= 8'h5b;
531
            8'h20: trans <= 8'h67; 8'h21: trans <= 8'h2e; 8'h22: trans <= 8'h2d; 8'h23: trans <= 8'h20; 8'h24: trans <= 8'h12; 8'h25: trans <= 8'h05; 8'h26: trans <= 8'h04; 8'h27: trans <= 8'h5c;
532
            8'h28: trans <= 8'h68; 8'h29: trans <= 8'h39; 8'h2A: trans <= 8'h2f; 8'h2B: trans <= 8'h21; 8'h2C: trans <= 8'h14; 8'h2D: trans <= 8'h13; 8'h2E: trans <= 8'h06; 8'h2F: trans <= 8'h5d;
533
            8'h30: trans <= 8'h69; 8'h31: trans <= 8'h31; 8'h32: trans <= 8'h30; 8'h33: trans <= 8'h23; 8'h34: trans <= 8'h22; 8'h35: trans <= 8'h15; 8'h36: trans <= 8'h07; 8'h37: trans <= 8'h5e;
534
            8'h38: trans <= 8'h6a; 8'h39: trans <= 8'h72; 8'h3A: trans <= 8'h32; 8'h3B: trans <= 8'h24; 8'h3C: trans <= 8'h16; 8'h3D: trans <= 8'h08; 8'h3E: trans <= 8'h09; 8'h3F: trans <= 8'h5f;
535
            8'h40: trans <= 8'h6b; 8'h41: trans <= 8'h33; 8'h42: trans <= 8'h25; 8'h43: trans <= 8'h17; 8'h44: trans <= 8'h18; 8'h45: trans <= 8'h0b; 8'h46: trans <= 8'h0a; 8'h47: trans <= 8'h60;
536
            8'h48: trans <= 8'h6c; 8'h49: trans <= 8'h34; 8'h4A: trans <= 8'h35; 8'h4B: trans <= 8'h26; 8'h4C: trans <= 8'h27; 8'h4D: trans <= 8'h19; 8'h4E: trans <= 8'h0c; 8'h4F: trans <= 8'h61;
537
            8'h50: trans <= 8'h6d; 8'h51: trans <= 8'h73; 8'h52: trans <= 8'h28; 8'h53: trans <= 8'h74; 8'h54: trans <= 8'h1a; 8'h55: trans <= 8'h0d; 8'h56: trans <= 8'h62; 8'h57: trans <= 8'h6e;
538
            8'h58: trans <= 8'h3a; 8'h59: trans <= 8'h36; 8'h5A: trans <= 8'h1c; 8'h5B: trans <= 8'h1b; 8'h5C: trans <= 8'h75; 8'h5D: trans <= 8'h2b; 8'h5E: trans <= 8'h63; 8'h5F: trans <= 8'h76;
539
            8'h60: trans <= 8'h55; 8'h61: trans <= 8'h56; 8'h62: trans <= 8'h77; 8'h63: trans <= 8'h78; 8'h64: trans <= 8'h79; 8'h65: trans <= 8'h7a; 8'h66: trans <= 8'h0e; 8'h67: trans <= 8'h7b;
540
            8'h68: trans <= 8'h7c; 8'h69: trans <= 8'h4f; 8'h6A: trans <= 8'h7d; 8'h6B: trans <= 8'h4b; 8'h6C: trans <= 8'h47; 8'h6D: trans <= 8'h7e; 8'h6E: trans <= 8'h7f; 8'h6F: trans <= 8'h6f;
541
            8'h70: trans <= 8'h52; 8'h71: trans <= 8'h53; 8'h72: trans <= 8'h50; 8'h73: trans <= 8'h4c; 8'h74: trans <= 8'h4d; 8'h75: trans <= 8'h48; 8'h76: trans <= 8'h01; 8'h77: trans <= 8'h45;
542
            8'h78: trans <= 8'h57; 8'h79: trans <= 8'h4e; 8'h7A: trans <= 8'h51; 8'h7B: trans <= 8'h4a; 8'h7C: trans <= 8'h37; 8'h7D: trans <= 8'h49; 8'h7E: trans <= 8'h46; 8'h7F: trans <= 8'h54;
543
            8'h80: trans <= 8'h80; 8'h81: trans <= 8'h81; 8'h82: trans <= 8'h82; 8'h83: trans <= 8'h41; 8'h84: trans <= 8'h54; 8'h85: trans <= 8'h85; 8'h86: trans <= 8'h86; 8'h87: trans <= 8'h87;
544
            8'h88: trans <= 8'h88; 8'h89: trans <= 8'h89; 8'h8A: trans <= 8'h8a; 8'h8B: trans <= 8'h8b; 8'h8C: trans <= 8'h8c; 8'h8D: trans <= 8'h8d; 8'h8E: trans <= 8'h8e; 8'h8F: trans <= 8'h8f;
545
            default: trans <= keyb_recv_buffer;
546
        endcase
547
    end
548
end
549
 
550
localparam [3:0] PS2_IDLE                   = 4'd0;
551
 
552
localparam [3:0] PS2_SEND_INHIBIT           = 4'd1;
553
localparam [3:0] PS2_SEND_INHIBIT_WAIT      = 4'd2;
554
localparam [3:0] PS2_SEND_DATA_LOW          = 4'd3;
555
localparam [3:0] PS2_SEND_CLOCK_RELEASE     = 4'd4;
556
localparam [3:0] PS2_SEND_BITS              = 4'd5;
557
localparam [3:0] PS2_SEND_WAIT_FOR_ACK      = 4'd6;
558
localparam [3:0] PS2_SEND_WAIT_FOR_IDLE     = 4'd7;
559
localparam [3:0] PS2_SEND_FINISHED          = 4'd8;
560
 
561
localparam [3:0] PS2_RECV_START             = 4'd9;
562
localparam [3:0] PS2_RECV_BITS              = 4'd10;
563
localparam [3:0] PS2_RECV_WAIT_FOR_STOP     = 4'd11;
564
localparam [3:0] PS2_RECV_WAIT_FOR_IDLE     = 4'd12;
565
 
566
localparam [3:0] PS2_WAIT_START             = 4'd13;
567
localparam [3:0] PS2_WAIT                   = 4'd14;
568
localparam [3:0] PS2_WAIT_FINISH            = 4'd15;
569
 
570
reg [3:0] keyb_state;
571
 
572
always @(posedge clk or negedge rst_n) begin
573
    if(rst_n == 1'b0)                                                                                               keyb_state <= PS2_IDLE;
574
    else if(keyb_timeout_reset)                                                                                     keyb_state <= PS2_IDLE;
575
 
576
    //buffer full
577
    else if(keyb_state == PS2_IDLE && (keyb_fifo_counter >= 7'd60 || disable_keyboard))                                                                     keyb_state <= PS2_WAIT_START;
578
    else if(keyb_state == PS2_WAIT_START)                                                                                                                   keyb_state <= PS2_WAIT;
579
    else if(keyb_state == PS2_WAIT && keyb_timer == 13'd1 && status_inputbufferfull && ~(input_write_done) && ~(input_for_mouse) && ~(disable_keyboard))    keyb_state <= PS2_SEND_INHIBIT;
580
    else if(keyb_state == PS2_WAIT && keyb_timer == 13'd1 && (keyb_fifo_counter >= 7'd60 || disable_keyboard))                                              keyb_state <= PS2_WAIT_START;
581
    else if(keyb_state == PS2_WAIT && keyb_timer == 13'd1)                                                                                                  keyb_state <= PS2_WAIT_FINISH;
582
    else if(keyb_state == PS2_WAIT_FINISH)                                                                                                                  keyb_state <= PS2_IDLE;
583
 
584
    //send
585
    else if(keyb_state == PS2_IDLE && status_inputbufferfull && ~(input_write_done) && ~(input_for_mouse))  keyb_state <= PS2_SEND_INHIBIT;
586
    else if(keyb_state == PS2_SEND_INHIBIT)                                                                 keyb_state <= PS2_SEND_INHIBIT_WAIT;
587
    else if(keyb_state == PS2_SEND_INHIBIT_WAIT && keyb_timer == 13'd8)                                     keyb_state <= PS2_SEND_DATA_LOW;
588
    else if(keyb_state == PS2_SEND_DATA_LOW)                                                                keyb_state <= PS2_SEND_INHIBIT_WAIT;
589
    else if(keyb_state == PS2_SEND_INHIBIT_WAIT && keyb_timer == 13'd1)                                     keyb_state <= PS2_SEND_CLOCK_RELEASE;
590
    else if(keyb_state == PS2_SEND_CLOCK_RELEASE)                                                           keyb_state <= PS2_SEND_BITS;
591
    else if(keyb_state == PS2_SEND_BITS && ps2_kb_write_shift && keyb_bit_counter == 4'd9)                  keyb_state <= PS2_SEND_WAIT_FOR_ACK;
592
    else if(keyb_state == PS2_SEND_WAIT_FOR_ACK && was_ps2_kbclk && keyb_kbdat == 1'b0)                     keyb_state <= PS2_SEND_WAIT_FOR_IDLE;
593
    else if(keyb_state == PS2_SEND_WAIT_FOR_IDLE && keyb_kbclk == 1'b1 && keyb_kbdat == 1'b1)               keyb_state <= PS2_SEND_FINISHED;
594
    else if(keyb_state == PS2_SEND_FINISHED)                                                                keyb_state <= PS2_IDLE;
595
 
596
    //recv
597
    else if(keyb_state == PS2_IDLE && was_ps2_kbclk && keyb_kbdat == 1'b0)                          keyb_state <= PS2_RECV_START;
598
    else if(keyb_state == PS2_RECV_START)                                                           keyb_state <= PS2_RECV_BITS;
599
    else if(keyb_state == PS2_RECV_BITS && (keyb_recv_ok || keyb_recv_parity_err))                  keyb_state <= PS2_RECV_WAIT_FOR_STOP;
600
    else if(keyb_state == PS2_RECV_WAIT_FOR_STOP && was_ps2_kbclk && keyb_kbdat == 1'b1)            keyb_state <= PS2_RECV_WAIT_FOR_IDLE;
601
    else if(keyb_state == PS2_RECV_WAIT_FOR_IDLE && keyb_kbclk == 1'b1 && keyb_kbdat == 1'b1)       keyb_state <= PS2_IDLE;
602
end
603
 
604
wire [6:0]  keyb_fifo_counter = { keyb_fifo_full, keyb_fifo_usedw };
605
wire        keyb_fifo_full;
606
wire [5:0]  keyb_fifo_usedw;
607
 
608
wire [7:0]  keyb_fifo_q;
609
wire        keyb_fifo_empty;
610
 
611
wire [7:0] keyb_fifo_q_final = (keyb_fifo_empty)? keyb_fifo_q_last : keyb_fifo_q;
612
 
613
reg [7:0] keyb_fifo_q_last;
614
always @(posedge clk or negedge rst_n) begin
615
    if(rst_n == 1'b0)           keyb_fifo_q_last <= 8'd0;
616
    else if(~(keyb_fifo_empty)) keyb_fifo_q_last <= keyb_fifo_q;
617
end
618
 
619
wire ps2_kb_write_shift = keyb_state == PS2_SEND_BITS && was_ps2_kbclk;
620
 
621
wire ps2_kb_write_done = keyb_state == PS2_SEND_FINISHED;
622
 
623
wire ps2_kb_reply_done = keyb_reply_valid && keyb_fifo_counter < 7'd60 && ~(keyb_recv_final);
624
 
625
simple_fifo #(
626
    .width      (8),
627
    .widthu     (6)
628
)
629
keyb_fifo(
630
    .clk        (clk),
631
    .rst_n      (rst_n),
632
 
633
    .sclr       (cmd_self_test),                                                                                                //input
634
 
635
    .wrreq      (ps2_kb_reply_done || (keyb_recv_final && (~(translate) || keyb_recv_buffer != 8'hF0))),                        //input
636
    .data       ((ps2_kb_reply_done)? keyb_reply : (translate)? ({ keyb_translate_escape, 7'd0 } | trans) : keyb_recv_buffer),  //input [7:0]
637
    .full       (keyb_fifo_full),                                                                                               //output
638
    .usedw      (keyb_fifo_usedw),                                                                                              //output [5:0]
639
 
640
    .rdreq      (io_read_valid && io_address == 3'd0 && status_outputbufferfull && ~(status_mousebufferfull)),                  //input
641
    .q          (keyb_fifo_q),                                                                                                  //output [7:0]
642
    .empty      (keyb_fifo_empty)                                                                                               //output
643
);
644
 
645
//------------------------------------------------------------------------------ ps/2 for mouse
646
 
647
assign ps2_mouseclk    = (ps2_mouseclk_ena)?      ps2_mouseclk_out       : 1'bZ;
648
assign ps2_mousedat    = (ps2_mousedat_ena)?      ps2_mousedat_out       : 1'bZ;
649
 
650
reg ps2_mouseclk_ena;
651
always @(posedge clk or negedge rst_n) begin
652
    if(rst_n == 1'b0)                                                                   ps2_mouseclk_ena <= 1'b0;
653
    else if(mouse_timeout_reset)                                                        ps2_mouseclk_ena <= 1'b0;
654
    else if(mouse_state == PS2_SEND_INHIBIT || mouse_state == PS2_WAIT_START)           ps2_mouseclk_ena <= 1'b1;
655
    else if(mouse_state == PS2_SEND_CLOCK_RELEASE || mouse_state == PS2_WAIT_FINISH)    ps2_mouseclk_ena <= 1'b0;
656
end
657
 
658
reg ps2_mouseclk_out;
659
always @(posedge clk or negedge rst_n) begin
660
    if(rst_n == 1'b0)                                                           ps2_mouseclk_out <= 1'b0;
661
    else if(mouse_state == PS2_SEND_INHIBIT || mouse_state == PS2_WAIT_START)   ps2_mouseclk_out <= 1'b0;
662
end
663
 
664
reg ps2_mousedat_ena;
665
always @(posedge clk or negedge rst_n) begin
666
    if(rst_n == 1'b0)                                               ps2_mousedat_ena <= 1'b0;
667
    else if(mouse_timeout_reset)                                    ps2_mousedat_ena <= 1'b0;
668
    else if(mouse_state == PS2_SEND_DATA_LOW)                       ps2_mousedat_ena <= 1'b1;
669
    else if(ps2_mouse_write_shift && mouse_bit_counter == 4'd9)     ps2_mousedat_ena <= 1'b0;  //stop bit
670
end
671
 
672
reg ps2_mousedat_out;
673
always @(posedge clk or negedge rst_n) begin
674
    if(rst_n == 1'b0)                                           ps2_mousedat_out <= 1'b0;
675
    else if(mouse_state == PS2_SEND_DATA_LOW)                   ps2_mousedat_out <= 1'b0;               //start bit
676
    else if(ps2_mouse_write_shift && mouse_bit_counter < 4'd8)  ps2_mousedat_out <= inputbuffer[0];     //data bits
677
    else if(ps2_mouse_write_shift && mouse_bit_counter == 4'd8) ps2_mousedat_out <= ~(mouse_parity);    //parity bit
678
end
679
 
680
reg [15:0] mouse_clk_mv;
681
reg mouse_clk_mv_wait;
682
reg was_ps2_mouseclk;
683
always @(posedge clk or negedge rst_n) begin
684
    if(rst_n == 1'b0) begin
685
        mouse_clk_mv         <= 16'd0;
686
        mouse_clk_mv_wait    <= 1'b0;
687
        was_ps2_mouseclk       <= 1'b0;
688
    end
689
    else begin
690
        mouse_clk_mv <= { mouse_clk_mv[14:0], mouse_mouseclk };
691
 
692
        if(mouse_clk_mv_wait == 1'b0 && mouse_clk_mv[15:12] == 4'b1111 && mouse_clk_mv[3:0] == 4'b0000) begin
693
            was_ps2_mouseclk <= 1'b1;
694
            mouse_clk_mv_wait <= 1'b1;
695
        end
696
        else if(mouse_clk_mv_wait == 1'b1 && mouse_clk_mv[15:0] == 16'h0000) begin
697
            mouse_clk_mv_wait <= 1'b0;
698
            was_ps2_mouseclk <= 1'b0;
699
        end
700
        else begin
701
            was_ps2_mouseclk <= 1'b0;
702
        end
703
    end
704
end
705
 
706
reg mouse_mouseclk;
707
always @(posedge clk or negedge rst_n) begin
708
    if(rst_n == 1'b0)   mouse_mouseclk <= 1'b1;
709
    else                mouse_mouseclk <= ps2_mouseclk;
710
end
711
 
712
reg mouse_mousedat;
713
always @(posedge clk or negedge rst_n) begin
714
    if(rst_n == 1'b0)   mouse_mousedat <= 1'b1;
715
    else                mouse_mousedat <= ps2_mousedat;
716
end
717
 
718
reg [25:0] mouse_timeout;
719
always @(posedge clk or negedge rst_n) begin
720
    if(rst_n == 1'b0)                                                           mouse_timeout <= 26'h0;
721
    else if(mouse_state == PS2_SEND_INHIBIT || mouse_state == PS2_RECV_START)   mouse_timeout <= 26'h3FFFFFF;
722
    else if(mouse_state == PS2_IDLE)                                            mouse_timeout <= 26'h0;
723
    else if(mouse_timeout > 26'd0)                                              mouse_timeout <= mouse_timeout - 26'd1;
724
end
725
 
726
wire mouse_timeout_reset = mouse_timeout == 26'd1;
727
 
728
reg [12:0] mouse_timer;
729
always @(posedge clk or negedge rst_n) begin
730
    if(rst_n == 1'b0)                           mouse_timer <= 13'd0;
731
    else if(mouse_timeout_reset)                mouse_timer <= 13'd0;
732
    else if(mouse_state == PS2_SEND_INHIBIT)    mouse_timer <= 13'd8191;
733
    else if(mouse_state == PS2_WAIT_START)      mouse_timer <= 13'd8191;
734
 
735
    else if(mouse_timer > 13'd0)                mouse_timer <= mouse_timer - 13'd1;
736
end
737
 
738
reg [3:0] mouse_bit_counter;
739
always @(posedge clk or negedge rst_n) begin
740
    if(rst_n == 1'b0)                               mouse_bit_counter <= 4'd0;
741
    else if(mouse_timeout_reset)                    mouse_bit_counter <= 4'd0;
742
 
743
    else if(mouse_state == PS2_SEND_CLOCK_RELEASE)  mouse_bit_counter <= 4'd0;
744
    else if(ps2_mouse_write_shift)                  mouse_bit_counter <= mouse_bit_counter + 4'd1;
745
 
746
    else if(mouse_state == PS2_RECV_START)          mouse_bit_counter <= 4'd0;
747
    else if(mouse_recv)                             mouse_bit_counter <= mouse_bit_counter + 4'd1;
748
end
749
 
750
reg mouse_parity;
751
always @(posedge clk or negedge rst_n) begin
752
    if(rst_n == 1'b0)                               mouse_parity <= 1'b0;
753
 
754
    else if(mouse_state == PS2_SEND_CLOCK_RELEASE)  mouse_parity <= 1'b0;
755
    else if(ps2_mouse_write_shift)                  mouse_parity <= mouse_parity ^ inputbuffer[0];
756
 
757
    else if(mouse_state == PS2_RECV_START)          mouse_parity <= 1'b0;
758
    else if(mouse_recv)                             mouse_parity <= mouse_parity ^ mouse_mousedat;
759
end
760
 
761
reg [7:0] mouse_recv_buffer;
762
always @(posedge clk or negedge rst_n) begin
763
    if(rst_n == 1'b0)                               mouse_recv_buffer <= 8'd0;
764
    else if(mouse_recv && mouse_bit_counter < 4'd8) mouse_recv_buffer <= { mouse_mousedat, mouse_recv_buffer[7:1] };
765
end
766
 
767
wire mouse_recv              = mouse_state == PS2_RECV_BITS && was_ps2_mouseclk;
768
wire mouse_recv_ok           = mouse_recv && mouse_bit_counter == 4'd8 && ~(mouse_parity) == mouse_mousedat;
769
wire mouse_recv_parity_err   = mouse_recv && mouse_bit_counter == 4'd8 && ~(mouse_parity) != mouse_mousedat;
770
 
771
reg mouse_recv_result;
772
always @(posedge clk or negedge rst_n) begin
773
    if(rst_n == 1'b0)                       mouse_recv_result <= 1'b0;
774
    else if(mouse_state == PS2_RECV_BITS)   mouse_recv_result <= mouse_recv_ok;
775
end
776
wire mouse_recv_final = mouse_state == PS2_RECV_WAIT_FOR_IDLE && mouse_mouseclk == 1'b1 && mouse_mousedat == 1'b1 && mouse_recv_result;
777
 
778
reg [3:0] mouse_state;
779
 
780
always @(posedge clk or negedge rst_n) begin
781
    if(rst_n == 1'b0)                                                                                                       mouse_state <= PS2_IDLE;
782
    else if(mouse_timeout_reset)                                                                                            mouse_state <= PS2_IDLE;
783
 
784
    //buffer full
785
    else if(mouse_state == PS2_IDLE && (mouse_fifo_counter >= 7'd60 || disable_mouse))                                                                  mouse_state <= PS2_WAIT_START;
786
    else if(mouse_state == PS2_WAIT_START)                                                                                                              mouse_state <= PS2_WAIT;
787
    else if(mouse_state == PS2_WAIT && mouse_timer == 13'd1 && status_inputbufferfull && ~(input_write_done) && input_for_mouse && ~(disable_mouse))    mouse_state <= PS2_SEND_INHIBIT;
788
    else if(mouse_state == PS2_WAIT && mouse_timer == 13'd1 && (mouse_fifo_counter >= 7'd60 || disable_mouse))                                          mouse_state <= PS2_WAIT_START;
789
    else if(mouse_state == PS2_WAIT && mouse_timer == 13'd1)                                                                                            mouse_state <= PS2_WAIT_FINISH;
790
    else if(mouse_state == PS2_WAIT_FINISH)                                                                                                             mouse_state <= PS2_IDLE;
791
 
792
    //send
793
    else if(mouse_state == PS2_IDLE && status_inputbufferfull && ~(input_write_done) && input_for_mouse)    mouse_state <= PS2_SEND_INHIBIT;
794
    else if(mouse_state == PS2_SEND_INHIBIT)                                                                mouse_state <= PS2_SEND_INHIBIT_WAIT;
795
    else if(mouse_state == PS2_SEND_INHIBIT_WAIT && mouse_timer == 13'd8)                                   mouse_state <= PS2_SEND_DATA_LOW;
796
    else if(mouse_state == PS2_SEND_DATA_LOW)                                                               mouse_state <= PS2_SEND_INHIBIT_WAIT;
797
    else if(mouse_state == PS2_SEND_INHIBIT_WAIT && mouse_timer == 13'd1)                                   mouse_state <= PS2_SEND_CLOCK_RELEASE;
798
    else if(mouse_state == PS2_SEND_CLOCK_RELEASE)                                                          mouse_state <= PS2_SEND_BITS;
799
    else if(mouse_state == PS2_SEND_BITS && ps2_mouse_write_shift && mouse_bit_counter == 4'd9)             mouse_state <= PS2_SEND_WAIT_FOR_ACK;
800
    else if(mouse_state == PS2_SEND_WAIT_FOR_ACK && was_ps2_mouseclk && mouse_mousedat == 1'b0)             mouse_state <= PS2_SEND_WAIT_FOR_IDLE;
801
    else if(mouse_state == PS2_SEND_WAIT_FOR_IDLE && mouse_mouseclk == 1'b1 && mouse_mousedat == 1'b1)      mouse_state <= PS2_SEND_FINISHED;
802
    else if(mouse_state == PS2_SEND_FINISHED)                                                               mouse_state <= PS2_IDLE;
803
 
804
    //recv
805
    else if(mouse_state == PS2_IDLE && was_ps2_mouseclk && mouse_mousedat == 1'b0)                      mouse_state <= PS2_RECV_START;
806
    else if(mouse_state == PS2_RECV_START)                                                              mouse_state <= PS2_RECV_BITS;
807
    else if(mouse_state == PS2_RECV_BITS && (mouse_recv_ok || mouse_recv_parity_err))                   mouse_state <= PS2_RECV_WAIT_FOR_STOP;
808
    else if(mouse_state == PS2_RECV_WAIT_FOR_STOP && was_ps2_mouseclk && mouse_mousedat == 1'b1)        mouse_state <= PS2_RECV_WAIT_FOR_IDLE;
809
    else if(mouse_state == PS2_RECV_WAIT_FOR_IDLE && mouse_mouseclk == 1'b1 && mouse_mousedat == 1'b1)  mouse_state <= PS2_IDLE;
810
end
811
 
812
wire [6:0]  mouse_fifo_counter = { mouse_fifo_full, mouse_fifo_usedw };
813
wire        mouse_fifo_full;
814
wire [5:0]  mouse_fifo_usedw;
815
 
816
wire [7:0]  mouse_fifo_q;
817
wire        mouse_fifo_empty;
818
 
819
wire ps2_mouse_write_shift = mouse_state == PS2_SEND_BITS && was_ps2_mouseclk;
820
 
821
wire ps2_mouse_write_done = mouse_state == PS2_SEND_FINISHED;
822
 
823
wire ps2_mouse_reply_done = mouse_reply_valid && mouse_fifo_counter < 7'd60 && ~(mouse_recv_final);
824
 
825
simple_fifo #(
826
    .width      (8),
827
    .widthu     (6)
828
)
829
mouse_fifo(
830
    .clk        (clk),
831
    .rst_n      (rst_n),
832
 
833
    .sclr       (cmd_self_test),                                                //input
834
 
835
    .wrreq      (ps2_mouse_reply_done || mouse_recv_final),                     //input
836
    .data       ((ps2_mouse_reply_done)? mouse_reply : mouse_recv_buffer),      //input [7:0]
837
    .full       (mouse_fifo_full),                                              //output
838
    .usedw      (mouse_fifo_usedw),                                             //output [5:0]
839
 
840
    .rdreq      (io_read_valid && io_address == 3'd0 && status_mousebufferfull),//input
841
    .q          (mouse_fifo_q),                                                 //output [7:0]
842
    .empty      (mouse_fifo_empty)                                              //output
843
);
844
 
845
//------------------------------------------------------------------------------
846
 
847
// synthesis translate_off
848
wire _unused_ok = &{ 1'b0, sysctl_read, sysctl_writedata[7:2], 1'b0 };
849
// synthesis translate_on
850
 
851
//------------------------------------------------------------------------------
852
 
853
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.