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

Subversion Repositories ps2

[/] [ps2/] [tags/] [rel_6/] [bench/] [verilog/] [ps2_test_bench.v] - Blame information for rev 5

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

Line No. Rev Author Line
1 2 mihad
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  ps2_test_bench.v                                            ////
4
////                                                              ////
5
////  This file is part of the "ps2" project                      ////
6
////  http://www.opencores.org/cores/ps2/                         ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - mihad@opencores.org                                   ////
10
////      - Miha Dolenc                                           ////
11
////                                                              ////
12
////  All additional information is avaliable in the README.txt   ////
13
////  file.                                                       ////
14
////                                                              ////
15
////                                                              ////
16
//////////////////////////////////////////////////////////////////////
17
////                                                              ////
18
//// Copyright (C) 2000 Miha Dolenc, mihad@opencores.org          ////
19
////                                                              ////
20
//// This source file may be used and distributed without         ////
21
//// restriction provided that this copyright statement is not    ////
22
//// removed from the file and that any derivative work contains  ////
23
//// the original copyright notice and the associated disclaimer. ////
24
////                                                              ////
25
//// This source file is free software; you can redistribute it   ////
26
//// and/or modify it under the terms of the GNU Lesser General   ////
27
//// Public License as published by the Free Software Foundation; ////
28
//// either version 2.1 of the License, or (at your option) any   ////
29
//// later version.                                               ////
30
////                                                              ////
31
//// This source is distributed in the hope that it will be       ////
32
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
33
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
34
//// PURPOSE.  See the GNU Lesser General Public License for more ////
35
//// details.                                                     ////
36
////                                                              ////
37
//// You should have received a copy of the GNU Lesser General    ////
38
//// Public License along with this source; if not, download it   ////
39
//// from http://www.opencores.org/lgpl.shtml                     ////
40
////                                                              ////
41
//////////////////////////////////////////////////////////////////////
42
//
43
// CVS Revision History
44
//
45
// $Log: not supported by cvs2svn $
46 5 mihad
// Revision 1.1.1.1  2002/02/18 16:16:55  mihad
47
// Initial project import - working
48 2 mihad
//
49 5 mihad
//
50 2 mihad
 
51
`include "timescale.v"
52
`include "ps2_testbench_defines.v"
53
`include "ps2_defines.v"
54
 
55
`define KBD_STATUS_REG      32'h64
56
`define KBD_CNTL_REG        32'h64
57
`define KBD_DATA_REG        32'h60
58
/*
59
 * controller commands
60
 */
61
`define KBD_READ_MODE       32'h20
62
`define KBD_WRITE_MODE      32'h60
63
`define KBD_SELF_TEST       32'hAA
64
`define KBD_SELF_TEST2      32'hAB
65
`define KBD_CNTL_ENABLE     32'hAE
66
/*
67
 * keyboard commands
68
 */
69
`define KBD_ENABLE          32'hF4
70
`define KBD_DISABLE         32'hF5
71
`define KBD_RESET           32'hFF
72
/*
73
 * keyboard replies
74
 */
75
`define KBD_ACK             32'hFA
76
`define KBD_POR             32'hAA
77
/*
78
 * status register bits
79
 */
80
`define KBD_OBF             32'h01
81
`define KBD_IBF             32'h02
82
`define KBD_GTO             32'h40
83
`define KBD_PERR            32'h80
84
/*
85
 * keyboard controller mode register bits
86
 */
87
`define KBD_EKI             32'h01
88
`define KBD_SYS             32'h04
89
`define KBD_DMS             32'h20
90
`define KBD_KCC             32'h40
91
`define KBD_DISABLE_COMMAND 32'h10
92
 
93
module ps2_test_bench() ;
94
 
95
parameter [31:0] MAX_SEQUENCE_LENGTH = 10 ;
96
wire kbd_clk_cable  ;
97
wire kbd_data_cable ;
98
 
99
pullup(kbd_clk_cable)  ;
100
pullup(kbd_data_cable) ;
101
 
102
reg wb_clock ;
103
reg wb_reset ;
104
 
105
wire [7:0] received_char ;
106
wire       char_valid ;
107
 
108
`ifdef XILINX
109
    assign glbl.GSR = wb_reset ;
110
`endif
111
ps2_keyboard_model i_ps2_keyboard_model
112
(
113
    .kbd_clk_io  (kbd_clk_cable),
114
    .kbd_data_io (kbd_data_cable),
115
    .last_char_received_o (received_char),
116
    .char_valid_o (char_valid)
117
) ;
118
 
119
reg ok ;
120
reg error ;
121
 
122
integer watchdog_timer ;
123
reg     watchdog_reset ;
124
reg     watchdog_reset_previous ;
125
 
126
reg [7:0] normal_scancode_set2_mem [0:`PS2_NUM_OF_NORMAL_SCANCODES - 1] ;
127
reg [7:0] normal_scancode_set1_mem [0:`PS2_NUM_OF_NORMAL_SCANCODES - 1] ;
128
reg [7:0] extended_scancode_set2_mem [0:`PS2_NUM_OF_EXTENDED_SCANCODES - 1] ;
129
reg [7:0] extended_scancode_set1_mem [0:`PS2_NUM_OF_EXTENDED_SCANCODES - 1] ;
130
 
131
`define WB_PERIOD (1/`WB_FREQ)
132
initial
133
begin
134
 
135
    $readmemh("../../../bench/data/normal_scancodes_set2.hex", normal_scancode_set2_mem) ;
136
    $readmemh("../../../bench/data/normal_scancodes_set1.hex", normal_scancode_set1_mem) ;
137
    $readmemh("../../../bench/data/extended_scancodes_set2.hex", extended_scancode_set2_mem) ;
138
    $readmemh("../../../bench/data/extended_scancodes_set1.hex", extended_scancode_set1_mem) ;
139
 
140
    if ( ((`PS2_TIMER_5USEC_VALUE_PP * `WB_PERIOD) < 5000) || ((`PS2_TIMER_5USEC_VALUE_PP * `WB_PERIOD) > 6000) )
141
    begin
142
        $display("Warning! 5us timer max value is not defined correctly regarding to WISHBONE bus clock!") ;
143
        $stop ;
144
    end
145
 
146
    if ( ((`PS2_TIMER_60USEC_VALUE_PP * `WB_PERIOD) < 60000) || ((`PS2_TIMER_60USEC_VALUE_PP * `WB_PERIOD) > 61000) )
147
    begin
148
        $display("Warning! 60us timer max value is not defined correctly regarding to WISHBONE bus clock!") ;
149
        $stop ;
150
    end
151
 
152
    if ( (1 << `PS2_TIMER_5USEC_BITS_PP) < `PS2_TIMER_5USEC_VALUE_PP )
153
    begin
154
        $display("Warning! 5us timer max value is not defined correctly regarding to the length in bits of 5us timer!") ;
155
        $stop ;
156
    end
157
 
158
    if ( (1 << `PS2_TIMER_60USEC_BITS_PP) < `PS2_TIMER_60USEC_VALUE_PP )
159
    begin
160
        $display("Warning! 60us timer max value is not defined correctly regarding to the length in bits of 60us timer!") ;
161
        $stop ;
162
    end
163
 
164
    watchdog_timer = 32'h1000_0000 ;
165
    watchdog_reset = 0 ;
166
    watchdog_reset_previous = 0 ;
167
 
168
    wb_clock = 1'b1 ;
169
    wb_reset = 1'b1 ;
170
 
171
    #100 ;
172
 
173
    repeat ( 10 )
174
        @(posedge wb_clock) ;
175
    wb_reset <= 1'b0 ;
176
 
177
    @(posedge wb_clock) ;
178
    #1 initialize_controler ;
179
 
180
    test_scan_code_receiving ;
181
 
182
    test_normal_scancodes ;
183
 
184
    test_extended_scancodes ;
185
 
186
    test_print_screen_and_pause_scancodes ;
187
 
188
    test_keyboard_inhibit ;
189
 
190
    #400 $stop ;
191
end
192
 
193
always
194
    #(`WB_PERIOD / 2) wb_clock = !wb_clock ;
195
 
196
wire wb_cyc,
197
     wb_stb,
198
     wb_we,
199
     wb_ack,
200
     wb_rty,
201
     wb_int ;
202
 
203
wire [3:0] wb_sel ;
204
 
205
wire [31:0] wb_adr, wb_dat_m_s, wb_dat_s_m ;
206
 
207
ps2_sim_top
208
i_ps2_top
209
(
210
    .wb_clk_i        (wb_clock),
211
    .wb_rst_i        (wb_reset),
212
    .wb_cyc_i        (wb_cyc),
213
    .wb_stb_i        (wb_stb),
214
    .wb_we_i         (wb_we),
215
    .wb_sel_i        (wb_sel),
216
    .wb_adr_i        (wb_adr),
217
    .wb_dat_i        (wb_dat_m_s),
218
    .wb_dat_o        (wb_dat_s_m),
219
    .wb_ack_o        (wb_ack),
220
 
221
    .wb_int_o        (wb_int),
222
 
223
    .ps2_kbd_clk_io  (kbd_clk_cable),
224
    .ps2_kbd_data_io (kbd_data_cable)
225
) ;
226
 
227
WB_MASTER_BEHAVIORAL i_wb_master
228
(
229
    .CLK_I    (wb_clock),
230
    .RST_I    (wb_reset),
231
    .TAG_I    (1'b0),
232
    .TAG_O    (),
233
    .ACK_I    (wb_ack),
234
    .ADR_O    (wb_adr),
235
    .CYC_O    (wb_cyc),
236
    .DAT_I    (wb_dat_s_m),
237
    .DAT_O    (wb_dat_m_s),
238
    .ERR_I    (1'b0),
239
    .RTY_I    (1'b0),
240
    .SEL_O    (wb_sel),
241
    .STB_O    (wb_stb),
242
    .WE_O     (wb_we),
243
    .CAB_O    ()
244
);
245
 
246
always@(posedge wb_clock)
247
begin
248
    if ( watchdog_timer === 0 )
249
    begin
250
        $display("Warning! Simulation watchdog timer has expired!") ;
251
        watchdog_timer = 32'hFFFF_FFFF ;
252
    end
253
    else if ( watchdog_reset !== watchdog_reset_previous )
254
        watchdog_timer = 32'hFFFF_FFFF ;
255
 
256
    watchdog_reset_previous = watchdog_reset ;
257
 
258
end
259
 
260
task initialize_controler ;
261
    reg [7:0] data ;
262
    reg status ;
263
begin:main
264
 
265
    // simulate keyboard driver's behaviour 
266
    read_status_reg(data, status) ;
267
    if ( status !== 1 )
268
        disable main ;
269
 
270
    if ( data & `KBD_OBF )
271
        read_data_reg(data, status) ;
272
 
273
    if ( status !== 1 )
274
        disable main ;
275
 
276
    kbd_write(`KBD_CNTL_REG, `KBD_SELF_TEST, status) ;
277
 
278
    if ( status !== 1 )
279
        disable main ;
280
 
281
    // command sent - wait for commands output to be ready
282
    data = 0 ;
283
    while( status && !( data & `KBD_OBF ) )
284
        read_status_reg(data, status) ;
285
 
286
    if ( status !== 1 )
287
        disable main ;
288
 
289
    read_data_reg( data, status ) ;
290
 
291
    if ( status !== 1 )
292
        disable main ;
293
 
294
    if ( data !== 8'h55 )
295
    begin
296
        $display("Error! Keyboard controler should respond to self test command with hard coded value 0x55! ") ;
297
        #400 $stop ;
298
    end
299
 
300
    // perform self test 2
301
    kbd_write(`KBD_CNTL_REG, `KBD_SELF_TEST2, status) ;
302
 
303
    if ( status !== 1 )
304
        disable main ;
305
 
306
    // command sent - wait for commands output to be ready
307
    data = 0 ;
308
    while( status && !( data & `KBD_OBF ) )
309
        read_status_reg(data, status) ;
310
 
311
    if ( status !== 1 )
312
        disable main ;
313
 
314
    read_data_reg( data, status ) ;
315
 
316
    if ( status !== 1 )
317
        disable main ;
318
 
319
    if ( data !== 8'h00 )
320
    begin
321
        $display("Error! Keyboard controler should respond to self test command 2 with hard coded value 0x00! ") ;
322
        #400 $stop ;
323
    end
324
 
325
    kbd_write(`KBD_CNTL_REG, `KBD_CNTL_ENABLE, status);
326
 
327
    if ( status !== 1 )
328
        disable main ;
329
 
330
    // send reset command to keyboard
331
    kbd_write(`KBD_DATA_REG, `KBD_RESET, status) ;
332
 
333
    if ( status !== 1 )
334
        disable main ;
335
 
336
    fork
337
    begin
338
        // wait for keyboard to respond with acknowledge
339
        data = 0 ;
340
        while( status && !( data & `KBD_OBF ) )
341
            read_status_reg(data, status) ;
342
 
343
        if ( status !== 1 )
344
            disable main ;
345
 
346
        read_data_reg( data, status ) ;
347
 
348
        if ( status !== 1 )
349
            disable main ;
350
 
351
        if ( data !== `KBD_ACK )
352
        begin
353
            $display("Error! Expected character from keyboard was 0x%h, actualy received 0x%h!", `KBD_ACK, data ) ;
354
            #400 $stop ;
355
        end
356
 
357
        // wait for keyboard to respond with BAT status
358
        data = 0 ;
359
        while( status && !( data & `KBD_OBF ) )
360
            read_status_reg(data, status) ;
361
 
362
        if ( status !== 1 )
363
            disable main ;
364
 
365
        read_data_reg( data, status ) ;
366
 
367
        if ( status !== 1 )
368
            disable main ;
369
 
370
        if ( data !== `KBD_POR )
371
        begin
372
            $display("Error! Expected character from keyboard was 0x%h, actualy received 0x%h!", `KBD_POR, data ) ;
373
            #400 $stop ;
374
        end
375
 
376
        // send disable command to keyboard
377
        kbd_write(`KBD_DATA_REG, `KBD_DISABLE, status) ;
378
 
379
        if ( status !== 1 )
380
            disable main ;
381
 
382
        // wait for keyboard to respond with acknowledge
383
        data = 0 ;
384
        while( status && !( data & `KBD_OBF ) )
385
            read_status_reg(data, status) ;
386
 
387
        if ( status !== 1 )
388
            disable main ;
389
 
390
        read_data_reg( data, status ) ;
391
 
392
        if ( status !== 1 )
393
            disable main ;
394
 
395
        if ( data !== `KBD_ACK )
396
        begin
397
            $display("Error! Expected character from keyboard was 0x%h, actualy received 0x%h!", `KBD_ACK, data ) ;
398
            #400 $stop ;
399
        end
400
 
401
        kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, status);
402
        if ( status !== 1 )
403
            disable main ;
404
 
405
        kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC, status);
406
        if ( status !== 1 )
407
            disable main ;
408
 
409
        // send disable command to keyboard
410
        kbd_write(`KBD_DATA_REG, `KBD_ENABLE, status) ;
411
 
412
        if ( status !== 1 )
413
            disable main ;
414
 
415
        // wait for keyboard to respond with acknowledge
416
        data = 0 ;
417
        while( status && !( data & `KBD_OBF ) )
418
            read_status_reg(data, status) ;
419
 
420
        if ( status !== 1 )
421
            disable main ;
422
 
423
        read_data_reg( data, status ) ;
424
 
425
        if ( status !== 1 )
426
            disable main ;
427
 
428
        if ( data !== `KBD_ACK )
429
        begin
430
            $display("Error! Expected character from keyboard was 0x%h, actualy received 0x%h!", `KBD_ACK, data ) ;
431
            #400 $stop ;
432
        end
433
 
434
        // now check if command byte is as expected
435
        kbd_write(`KBD_CNTL_REG, `KBD_READ_MODE, status);
436
        if ( status !== 1 )
437
            disable main ;
438
 
439
        data = 0 ;
440
        while( status && !( data & `KBD_OBF ) )
441
            read_status_reg(data, status) ;
442
 
443
        if ( status !== 1 )
444
            disable main ;
445
 
446
        read_data_reg(data, status) ;
447
 
448
        if ( status !== 1 )
449
            disable main ;
450
 
451
        if ( (data & (`KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC)) !== (`KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC)  )
452
        begin
453
            $display("Error! Read command byte returned wrong value!") ;
454
            #400 $stop ;
455
        end
456
    end
457
    begin
458
        @(char_valid) ;
459
        if ( received_char !== `KBD_RESET )
460
        begin
461
            $display("Error! Keyboard received invalid character/command") ;
462
            #400 $stop ;
463
        end
464
 
465
        i_ps2_keyboard_model.kbd_send_char
466
        (
467
            `KBD_ACK,
468
            ok,
469
            error
470
        ) ;
471
 
472
        i_ps2_keyboard_model.kbd_send_char
473
        (
474
            `KBD_POR,
475
            ok,
476
            error
477
        ) ;
478
 
479
         @(char_valid) ;
480
        if ( received_char !== `KBD_DISABLE )
481
        begin
482
            $display("Error! Keyboard received invalid character/command") ;
483
            #400 $stop ;
484
        end
485
 
486
        i_ps2_keyboard_model.kbd_send_char
487
        (
488
            `KBD_ACK,
489
            ok,
490
            error
491
        ) ;
492
 
493
        @(char_valid) ;
494
        if ( received_char !== `KBD_ENABLE )
495
        begin
496
            $display("Error! Keyboard received invalid character/command") ;
497
            #400 $stop ;
498
        end
499
 
500
        i_ps2_keyboard_model.kbd_send_char
501
        (
502
            `KBD_ACK,
503
            ok,
504
            error
505
        ) ;
506
 
507
    end
508
    join
509
 
510
    watchdog_reset = !watchdog_reset ;
511
 
512
end
513
endtask // initialize_controler 
514
 
515
task read_data_reg ;
516
    output [7:0] return_byte_o ;
517
    output ok_o ;
518
    reg `READ_STIM_TYPE    read_data ;
519
    reg `READ_RETURN_TYPE  read_status ;
520
    reg `WB_TRANSFER_FLAGS flags ;
521
begin
522
    ok_o = 1 ;
523
    flags`WB_TRANSFER_SIZE     = 1 ;
524
    flags`WB_TRANSFER_AUTO_RTY = 0 ;
525
    flags`WB_TRANSFER_CAB      = 0 ;
526
    flags`INIT_WAITS           = 0 ;
527
    flags`SUBSEQ_WAITS         = 0 ;
528
 
529
    read_data`READ_ADDRESS = `KBD_DATA_REG ;
530
    read_data`READ_SEL     = 4'h1 ;
531
 
532
    read_status = 0 ;
533
 
534
    i_wb_master.wb_single_read( read_data, flags, read_status ) ;
535
 
536
    if ( read_status`CYC_ACK !== 1'b1 )
537
    begin
538
        $display("Error! Keyboard controler didn't acknowledge single read access!") ;
539
        #400 $stop ;
540
        ok_o = 0 ;
541
    end
542
    else
543
        return_byte_o = read_status`READ_DATA ;
544
 
545
end
546
endtask //read_data_reg
547
 
548
task read_status_reg ;
549
    output [7:0] return_byte_o ;
550
    output ok_o ;
551
    reg `READ_STIM_TYPE    read_data ;
552
    reg `READ_RETURN_TYPE  read_status ;
553
    reg `WB_TRANSFER_FLAGS flags ;
554
begin
555
    ok_o = 1 ;
556
    flags`WB_TRANSFER_SIZE     = 1 ;
557
    flags`WB_TRANSFER_AUTO_RTY = 0 ;
558
    flags`WB_TRANSFER_CAB      = 0 ;
559
    flags`INIT_WAITS           = 0 ;
560
    flags`SUBSEQ_WAITS         = 0 ;
561
 
562
    read_data`READ_ADDRESS = `KBD_STATUS_REG ;
563
    read_data`READ_SEL     = 4'h1 ;
564
 
565
    read_status = 0 ;
566
 
567
    i_wb_master.wb_single_read( read_data, flags, read_status ) ;
568
 
569
    if ( read_status`CYC_ACK !== 1'b1 )
570
    begin
571
        $display("Error! Keyboard controler didn't acknowledge single read access!") ;
572
        #400 $stop ;
573
        ok_o = 0 ;
574
    end
575
    else
576
        return_byte_o = read_status`READ_DATA ;
577
 
578
end
579
endtask // read_status_reg
580
 
581
task kbd_write ;
582
    input [31:0] address_i ;
583
    input [31:0] data_i ;
584
    output ok_o ;
585
 
586
    reg `WRITE_STIM_TYPE   write_data ;
587
    reg `WRITE_RETURN_TYPE write_status ;
588
    reg `WB_TRANSFER_FLAGS flags ;
589
    reg [7:0] kbd_status ;
590
begin:main
591
    ok_o = 1 ;
592
    flags`WB_TRANSFER_SIZE     = 1 ;
593
    flags`WB_TRANSFER_AUTO_RTY = 0 ;
594
    flags`WB_TRANSFER_CAB      = 0 ;
595
    flags`INIT_WAITS           = 0 ;
596
    flags`SUBSEQ_WAITS         = 0 ;
597
 
598
    write_data`WRITE_ADDRESS = address_i ;
599
    write_data`WRITE_DATA    = data_i ;
600
    write_data`WRITE_SEL     = 4'h1 ;
601
 
602
    read_status_reg(kbd_status, ok_o) ;
603
 
604
    while( ok_o && ( kbd_status & `KBD_IBF ))
605
    begin
606
        read_status_reg(kbd_status, ok_o) ;
607
    end
608
 
609
    if ( ok_o !== 1 )
610
        disable main ;
611
 
612
    i_wb_master.wb_single_write( write_data, flags, write_status ) ;
613
 
614
    if ( write_status`CYC_ACK !== 1 )
615
    begin
616
        $display("Error! Keyboard controller didn't acknowledge single write access") ;
617
        #400 $stop ;
618
        ok_o = 0 ;
619
    end
620
end
621
endtask // kbd_write
622
 
623
task test_scan_code_receiving ;
624
    reg ok_keyboard ;
625
    reg ok_controler ;
626
    reg ok ;
627
    reg [7:0] data ;
628
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] keyboard_sequence ;
629
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] controler_sequence ;
630
begin:main
631
    // prepare character sequence to send from keyboard to controler
632
    // L SHIFT make
633
    keyboard_sequence[7:0]   = 8'h12 ;
634
    // A make
635
    keyboard_sequence[15:8]  = 8'h1C ;
636
    // A break
637
    keyboard_sequence[23:16] = 8'hF0 ;
638
    keyboard_sequence[31:24] = 8'h1C ;
639
    // L SHIFT break
640
    keyboard_sequence[39:32] = 8'hF0 ;
641
    keyboard_sequence[47:40] = 8'h12 ;
642
 
643
    // prepare character sequence as it is received in scan code set 1 through the controler
644
    // L SHIFT make
645
    controler_sequence[7:0]   = 8'h2A ;
646
    // A make
647
    controler_sequence[15:8]  = 8'h1E ;
648
    // A break
649
    controler_sequence[23:16] = 8'h9E ;
650
    // L SHIFT break
651
    controler_sequence[31:24] = 8'hAA ;
652
 
653
    fork
654
    begin
655
        send_sequence( keyboard_sequence, 6, ok_keyboard ) ;
656
        if ( ok_keyboard !== 1 )
657
            disable main ;
658
    end
659
    begin
660
        receive_sequence( controler_sequence, 4, ok_controler ) ;
661
 
662
        if ( ok_controler !== 1 )
663
            disable main ;
664
    end
665
    join
666
 
667
    // test same thing with translation disabled!
668
    kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, ok);
669
    if ( ok !== 1 )
670
        disable main ;
671
 
672
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS, ok);
673
    if ( ok !== 1 )
674
        disable main ;
675
 
676
    // since translation is disabled, controler sequence is the same as keyboard sequence
677
    controler_sequence = keyboard_sequence ;
678
 
679
    fork
680
    begin
681
 
682
        send_sequence( keyboard_sequence, 6, ok_keyboard ) ;
683
        if ( ok_keyboard !== 1 )
684
            disable main ;
685
 
686
    end
687
    begin
688
        receive_sequence( controler_sequence, 6, ok_controler ) ;
689
        if ( ok_controler !== 1 )
690
            disable main ;
691
    end
692
    join
693
 
694
    // turn translation on again
695
    kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, ok);
696
    if ( ok !== 1 )
697
        disable main ;
698
 
699
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC, ok);
700
    if ( ok !== 1 )
701
        disable main ;
702
 
703
    // test extended character receiving - rctrl + s combination
704
    // prepare sequence to send from keyboard to controler
705
    // R CTRL make
706
    keyboard_sequence[7:0]   = 8'hE0 ;
707
    keyboard_sequence[15:8]  = 8'h14 ;
708
    // S make
709
    keyboard_sequence[23:16] = 8'h1B ;
710
    // S break
711
    keyboard_sequence[31:24] = 8'hF0 ;
712
    keyboard_sequence[39:32] = 8'h1B ;
713
    // R CTRL break
714
    keyboard_sequence[47:40] = 8'hE0 ;
715
    keyboard_sequence[55:48] = 8'hF0 ;
716
    keyboard_sequence[63:56] = 8'h14 ;
717
 
718
    // prepare sequence that should be received from the controler
719
    // R CTRL make
720
    controler_sequence[7:0]   = 8'hE0 ;
721
    controler_sequence[15:8]  = 8'h1D ;
722
    // S make
723
    controler_sequence[23:16] = 8'h1F ;
724
    // S break
725
    controler_sequence[31:24] = 8'h9F ;
726
    // R CTRL break
727
    controler_sequence[39:32] = 8'hE0 ;
728
    controler_sequence[47:40] = 8'h9D ;
729
 
730
    fork
731
    begin
732
        send_sequence( keyboard_sequence, 8, ok_keyboard ) ;
733
        if ( ok_keyboard !== 1 )
734
            disable main ;
735
    end
736
    begin
737
 
738
        receive_sequence( controler_sequence, 6, ok_controler ) ;
739
 
740
        if ( ok_controler !== 1 )
741
            disable main ;
742
    end
743
    join
744
 
745
     // test same thing with translation disabled!
746
    kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, ok);
747
    if ( ok !== 1 )
748
        disable main ;
749
 
750
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS, ok);
751
    if ( ok !== 1 )
752
        disable main ;
753
 
754
    // since translation is disabled, controler sequence is the same as keyboard sequence
755
    controler_sequence = keyboard_sequence ;
756
 
757
    fork
758
    begin
759
        send_sequence( keyboard_sequence, 8, ok_keyboard ) ;
760
        if ( ok_keyboard !== 1 )
761
            disable main ;
762
    end
763
    begin
764
 
765
        receive_sequence( controler_sequence, 8, ok_controler ) ;
766
 
767
        if ( ok_controler !== 1 )
768
            disable main ;
769
    end
770
    join
771
 
772
    watchdog_reset = !watchdog_reset ;
773
end
774
endtask // test_scan_code_receiving
775
 
776
task test_normal_scancodes ;
777
    reg ok ;
778
    reg ok_keyboard ;
779
    reg ok_controler ;
780
    integer i ;
781
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] keyboard_sequence ;
782
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] controler_sequence ;
783
begin:main
784
    // turn translation on
785
    kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, ok);
786
    if ( ok !== 1 )
787
        disable main ;
788
 
789
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC, ok);
790
    if ( ok !== 1 )
791
        disable main ;
792
 
793
    for ( i = 0 ; i < `PS2_NUM_OF_NORMAL_SCANCODES ; i = i + 1 )
794
    begin
795
        keyboard_sequence[7:0]   = normal_scancode_set2_mem[i] ;
796
        keyboard_sequence[15:8]  = 8'hF0 ;
797
        keyboard_sequence[23:16] = normal_scancode_set2_mem[i] ;
798
 
799
        controler_sequence[7:0]  = normal_scancode_set1_mem[i] ;
800
        controler_sequence[15:8] = normal_scancode_set1_mem[i] | 8'h80 ;
801
        fork
802
        begin
803
            send_sequence( keyboard_sequence, 3, ok_keyboard ) ;
804
            if ( ok_keyboard !== 1 )
805
                disable main ;
806
        end
807
        begin
808
            receive_sequence( controler_sequence, 2, ok_controler ) ;
809
            if ( ok_controler !== 1 )
810
                disable main ;
811
        end
812
        join
813
    end
814
 
815
    watchdog_reset = !watchdog_reset ;
816
 
817
end
818
endtask // test_normal_scancodes
819
 
820
task test_extended_scancodes ;
821
    reg ok ;
822
    reg ok_keyboard ;
823
    reg ok_controler ;
824
    integer i ;
825
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] keyboard_sequence ;
826
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] controler_sequence ;
827
begin:main
828
    // turn translation on
829
    kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, ok);
830
    if ( ok !== 1 )
831
        disable main ;
832
 
833
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC, ok);
834
    if ( ok !== 1 )
835
        disable main ;
836
 
837
    for ( i = 0 ; i < `PS2_NUM_OF_EXTENDED_SCANCODES ; i = i + 1 )
838
    begin
839
        keyboard_sequence[7:0]   = 8'hE0 ;
840
        keyboard_sequence[15:8]  = extended_scancode_set2_mem[i] ;
841
        keyboard_sequence[23:16] = 8'hE0 ;
842
        keyboard_sequence[31:24] = 8'hF0 ;
843
        keyboard_sequence[39:32] = extended_scancode_set2_mem[i] ;
844
 
845
        controler_sequence[7:0]   = 8'hE0 ;
846
        controler_sequence[15:8]  = extended_scancode_set1_mem[i] ;
847
        controler_sequence[23:16] = 8'hE0 ;
848
        controler_sequence[31:24] = extended_scancode_set1_mem[i] | 8'h80 ;
849
        fork
850
        begin
851
            send_sequence( keyboard_sequence, 5, ok_keyboard ) ;
852
            if ( ok_keyboard !== 1 )
853
                disable main ;
854
        end
855
        begin
856
            receive_sequence( controler_sequence, 4, ok_controler ) ;
857
            if ( ok_controler !== 1 )
858
                disable main ;
859
        end
860
        join
861
    end
862
 
863
    watchdog_reset = !watchdog_reset ;
864
 
865
end
866
endtask // test_extended_scancodes
867
 
868
task return_scan_code_on_irq ;
869
    output [7:0] scan_code_o ;
870
    output       ok_o ;
871
    reg    [7:0] temp_data ;
872
begin:main
873
    wait ( wb_int === 1 ) ;
874
    read_status_reg( temp_data, ok_o ) ;
875
 
876
    if ( ok_o !== 1'b1 )
877
        disable main ;
878
 
879
    if ( !( temp_data & `KBD_OBF ) )
880
    begin
881
        $display("Error! Interrupt received from keyboard controler when OBF status not set!") ;
882
        #400 $stop ;
883
    end
884
 
885
    read_data_reg( temp_data, ok_o ) ;
886
 
887
    if ( ok_o !== 1'b1 )
888
        disable main ;
889
 
890
    scan_code_o = temp_data ;
891
end
892
endtask // return_scan_code_on_irq
893
 
894
task send_sequence ;
895
    input [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] sequence_i ;
896
    input [31:0] num_of_chars_i ;
897
    output ok_o ;
898
    reg [7:0] current_char ;
899
    integer i ;
900
    reg ok ;
901
    reg error ;
902
begin:main
903
 
904
    error = 0 ;
905
    ok_o  = 1 ;
906
    ok    = 0 ;
907
 
908
    for( i = 0 ; i < num_of_chars_i ; i = i + 1 )
909
    begin
910
        current_char = sequence_i[7:0] ;
911
 
912
        sequence_i = sequence_i >> 8 ;
913
        ok = 0 ;
914
        error = 0 ;
915
        while ( (ok !== 1) && (error === 0) )
916
        begin
917
            i_ps2_keyboard_model.kbd_send_char
918
            (
919
                current_char,
920
                ok,
921
                error
922
            ) ;
923
        end
924
 
925
        if ( error )
926
        begin
927
            $display("Time %t", $time) ;
928
            $display("Keyboard model signaled an error!") ;
929
            ok_o = 0 ;
930
            disable main ;
931
        end
932
    end
933
end
934
endtask // send_sequence
935
 
936
task receive_sequence ;
937
    input [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] sequence_i ;
938
    input [31:0] num_of_chars_i ;
939
    output ok_o ;
940
    reg [7:0] current_char ;
941
    reg [7:0] data ;
942
    integer i ;
943
begin:main
944
 
945
    ok_o  = 1 ;
946
 
947
    for( i = 0 ; i < num_of_chars_i ; i = i + 1 )
948
    begin
949
        current_char = sequence_i[7:0] ;
950
 
951
        sequence_i = sequence_i >> 8 ;
952
 
953
        return_scan_code_on_irq( data, ok_o ) ;
954
 
955
        if ( ok_o !== 1 )
956
            disable main ;
957
 
958
        if ( data !== current_char )
959
        begin
960
            $display("Time %t", $time) ;
961
            $display("Error! Character received was wrong!") ;
962
            $display("Expected character: %h, received %h ", current_char, data ) ;
963
        end
964
    end
965
end
966
endtask // receive_seqence
967
 
968
task test_keyboard_inhibit ;
969
    reg ok_controler ;
970
    reg ok_keyboard ;
971
    reg error ;
972
    reg [7:0] data ;
973
begin:main
974
    // first test, if keyboard stays inhibited after character is received, but not read from the controler
975
    i_ps2_keyboard_model.kbd_send_char
976
    (
977
        8'hE0,
978
        ok_keyboard,
979
        error
980
    ) ;
981
 
982
    if ( error )
983
    begin
984
        $display("Error! Keyboard signaled an error while sending character!") ;
985
        disable main ;
986
    end
987
 
988
    if ( !ok_keyboard )
989
    begin
990
        $display("Something is wrong! Keyboard wasn't able to send a character!") ;
991
        disable main ;
992
    end
993
 
994
    // wait 5 us to see, if keyboard is inhibited
995
    #5000 ;
996
 
997
    // now check, if clock line is low!
998
    if ( kbd_clk_cable !== 0 )
999
    begin
1000
        $display("Error! Keyboard wasn't inhibited when output buffer was filled!") ;
1001
        disable main ;
1002
    end
1003
 
1004
    // now read the character from input buffer and check if clock was released
1005
    return_scan_code_on_irq( data, ok_controler ) ;
1006
    if ( ok_controler !== 1'b1 )
1007
        disable main ;
1008
 
1009
    if ( data !== 8'hE0 )
1010
    begin
1011
        $display("Time %t", $time) ;
1012
        $display("Error! Character read from controler not as expected!") ;
1013
    end
1014
 
1015
    fork
1016
    begin
1017
        repeat(10)
1018
            @(posedge wb_clock) ;
1019
 
1020
        if ( kbd_clk_cable !== 1 )
1021
        begin
1022
            $display("Error! Keyboard wasn't released from inhibited state when output buffer was read!") ;
1023
            disable main ;
1024
        end
1025
    end
1026
    begin
1027
        i_ps2_keyboard_model.kbd_send_char
1028
        (
1029
            8'h1C,
1030
            ok_keyboard,
1031
            error
1032
        ) ;
1033
        if ( !ok_keyboard )
1034
        begin
1035
            $display("Something is wrong! Keyboard wasn't able to send a character!") ;
1036
            disable main ;
1037
        end
1038
    end
1039
    begin
1040
        return_scan_code_on_irq( data, ok_controler ) ;
1041
        if ( ok_controler !== 1'b1 )
1042
            disable main ;
1043
 
1044
        if ( data !== 8'h1E )
1045
        begin
1046
            $display("Time %t", $time) ;
1047
            $display("Error! Character read from controler not as expected!") ;
1048
        end
1049
    end
1050
    join
1051
 
1052
    // disable keyboard controler
1053
    kbd_write( `KBD_CNTL_REG, `KBD_WRITE_MODE, ok_controler ) ;
1054
    if ( ok_controler !== 1 )
1055
        disable main ;
1056
 
1057
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC | `KBD_DISABLE_COMMAND, ok_controler);
1058
 
1059
    if ( ok_controler !== 1 )
1060
        disable main ;
1061
 
1062
    repeat( 5 )
1063
        @(posedge wb_clock) ;
1064
 
1065
    // now check, if clock line is low!
1066
    if ( kbd_clk_cable !== 0 )
1067
    begin
1068
        $display("Error! Keyboard wasn't inhibited when keyboard controler was disabled!") ;
1069
        disable main ;
1070
    end
1071
 
1072
    // send character and enable keyboard controler at the same time
1073
    fork
1074
    begin
1075
        i_ps2_keyboard_model.kbd_send_char
1076
        (
1077
            8'hE0,
1078
            ok_keyboard,
1079
            error
1080
        ) ;
1081
 
1082
        if ( !ok_keyboard )
1083
        begin
1084
            $display("Something is wrong! Keyboard wasn't able to send a character!") ;
1085
            disable main ;
1086
        end
1087
    end
1088
    begin
1089
        // enable keyboard controler
1090
        kbd_write( `KBD_CNTL_REG, `KBD_WRITE_MODE, ok_controler ) ;
1091
        if ( ok_controler !== 1 )
1092
            disable main ;
1093
 
1094
        kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC, ok_controler);
1095
        if ( ok_controler !== 1 )
1096
            disable main ;
1097
    end
1098
    begin
1099
        return_scan_code_on_irq( data, ok_controler ) ;
1100
        if ( ok_controler !== 1'b1 )
1101
            disable main ;
1102
 
1103
        if ( data !== 8'hE0 )
1104
        begin
1105
            $display("Time %t", $time) ;
1106
            $display("Error! Character read from controler not as expected!") ;
1107
        end
1108
    end
1109
    join
1110 5 mihad
 
1111
    // do D2 command, that copies parameter in input buffer to output buffer
1112
    kbd_write( `KBD_CNTL_REG, 32'hD2, ok_controler ) ;
1113
    if ( ok_controler !== 1 )
1114
        disable main ;
1115
 
1116
    kbd_write(`KBD_DATA_REG, 32'h5555_5555, ok_controler) ;
1117
 
1118
    if ( ok_controler !== 1 )
1119
        disable main ;
1120
 
1121
    return_scan_code_on_irq( data, ok_controler ) ;
1122
    if ( ok_controler !== 1 )
1123
        disable main ;
1124
 
1125
    if ( data !== 8'h55 )
1126
    begin
1127
        $display("Error! D2 command doesn't work properly") ;
1128
    end
1129
 
1130 2 mihad
end
1131
endtask // test_keyboard_inhibit
1132
 
1133
task test_print_screen_and_pause_scancodes ;
1134
    reg ok ;
1135
    reg ok_keyboard ;
1136
    reg ok_controler ;
1137
    integer i ;
1138
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] keyboard_sequence ;
1139
    reg [(MAX_SEQUENCE_LENGTH*8 - 1) : 0] controler_sequence ;
1140
begin:main
1141
    // turn translation on
1142
    kbd_write(`KBD_CNTL_REG, `KBD_WRITE_MODE, ok);
1143
    if ( ok !== 1 )
1144
        disable main ;
1145
 
1146
    kbd_write(`KBD_DATA_REG, `KBD_EKI|`KBD_SYS|`KBD_DMS|`KBD_KCC, ok);
1147
    if ( ok !== 1 )
1148
        disable main ;
1149
 
1150
    // prepare character sequence to send from keyboard to controler - pause
1151
    keyboard_sequence[7:0]   = 8'hE1 ;
1152
    keyboard_sequence[15:8]  = 8'h14 ;
1153
    keyboard_sequence[23:16] = 8'h77 ;
1154
    keyboard_sequence[31:24] = 8'hE1 ;
1155
    keyboard_sequence[39:32] = 8'hF0 ;
1156
    keyboard_sequence[47:40] = 8'h14 ;
1157
    keyboard_sequence[55:48] = 8'hF0 ;
1158
    keyboard_sequence[63:56] = 8'h77 ;
1159
 
1160
    // prepare character sequence as it is received in scan code set 1 through the controler
1161
    controler_sequence[7:0]   = 8'hE1 ;
1162
    controler_sequence[15:8]  = 8'h1D ;
1163
    controler_sequence[23:16] = 8'h45 ;
1164
    controler_sequence[31:24] = 8'hE1 ;
1165
    controler_sequence[39:32] = 8'h9D ;
1166
    controler_sequence[47:40] = 8'hC5 ;
1167
 
1168
    fork
1169
    begin
1170
        send_sequence( keyboard_sequence, 8, ok_keyboard ) ;
1171
        if ( ok_keyboard !== 1 )
1172
            disable main ;
1173
    end
1174
    begin
1175
        receive_sequence( controler_sequence, 6, ok_controler ) ;
1176
        if ( ok_controler !== 1 )
1177
            disable main ;
1178
    end
1179
    join
1180
 
1181
    // prepare character sequence to send from keyboard to controler - make print screen
1182
    keyboard_sequence[7:0]   = 8'hE0 ;
1183
    keyboard_sequence[15:8]  = 8'h12 ;
1184
    keyboard_sequence[23:16] = 8'hE0 ;
1185
    keyboard_sequence[31:24] = 8'h7C ;
1186
 
1187
    // prepare character sequence as it is received in scan code set 1 through the controler
1188
    controler_sequence[7:0]   = 8'hE0 ;
1189
    controler_sequence[15:8]  = 8'h2A ;
1190
    controler_sequence[23:16] = 8'hE0 ;
1191
    controler_sequence[31:24] = 8'h37 ;
1192
 
1193
    fork
1194
    begin
1195
        send_sequence( keyboard_sequence, 4, ok_keyboard ) ;
1196
        if ( ok_keyboard !== 1 )
1197
            disable main ;
1198
    end
1199
    begin
1200
        receive_sequence( controler_sequence, 4, ok_controler ) ;
1201
        if ( ok_controler !== 1 )
1202
            disable main ;
1203
    end
1204
    join
1205
 
1206
    // prepare character sequence to send from keyboard to controler - break print screen
1207
    keyboard_sequence[7:0]   = 8'hE0 ;
1208
    keyboard_sequence[15:8]  = 8'hF0 ;
1209
    keyboard_sequence[23:16] = 8'h7C ;
1210
    keyboard_sequence[31:24] = 8'hE0 ;
1211
    keyboard_sequence[39:32] = 8'hF0 ;
1212
    keyboard_sequence[47:40] = 8'h12 ;
1213
 
1214
    // prepare character sequence as it is received in scan code set 1 through the controler
1215
    controler_sequence[7:0]   = 8'hE0 ;
1216
    controler_sequence[15:8]  = 8'hB7 ;
1217
    controler_sequence[23:16] = 8'hE0 ;
1218
    controler_sequence[31:24] = 8'hAA ;
1219
 
1220
    fork
1221
    begin
1222
        send_sequence( keyboard_sequence, 6, ok_keyboard ) ;
1223
        if ( ok_keyboard !== 1 )
1224
            disable main ;
1225
    end
1226
    begin
1227
        receive_sequence( controler_sequence, 4, ok_controler ) ;
1228
        if ( ok_controler !== 1 )
1229
            disable main ;
1230
    end
1231
    join
1232
end
1233
endtask // test_print_screen_and_pause_scancodes
1234
 
1235
endmodule // ps2_test_bench

powered by: WebSVN 2.1.0

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