`timescale 1ns/1ps
module test_clock;
    reg clk = 0;
    reg [3:0] keys = 15;
    reg [9:0] sw = 0;
    wire [6:0] h5, h4, h3, h2, h1, h0;
    hundredth_clock #(.TICKS(4)) dut(clk, sw, keys, h5, h4, h3, h2, h1, h0);
    always #1 clk = ~clk;
    function [6:0] pattern;
        input integer n;
        begin case(n)
            0:pattern=64; 1:pattern=121; 2:pattern=36; 3:pattern=48; 4:pattern=25;
            5:pattern=18; 6:pattern=2; 7:pattern=120; 8:pattern=0; 9:pattern=16;
            default:pattern=127;
        endcase end
    endfunction
    task cycles;
        input integer n;
        begin repeat(n) @(negedge clk); end
    endtask
    task check;
        input integer minutes, seconds, cs;
        begin
            if ({h5,h4,h3,h2,h1,h0} !== {pattern(minutes/10),pattern(minutes%10),
                    pattern(seconds/10),pattern(seconds%10),pattern(cs/10),pattern(cs%10)}) begin
                $display("FAIL expected %02d:%02d.%02d",minutes,seconds,cs);
                $finish;
            end
        end
    endtask
    task load;
        input [9:0] word_to_load;
        begin sw = word_to_load; cycles(5); keys[1]=0; cycles(6); keys[1]=1; cycles(6); end
    endtask
    integer step;
    initial begin
        sw=10'h359; cycles(8); load(10'h359); check(59,59,98);
        load(10'h142); check(42,59,98);
        load(10'h162); check(42,59,98);
        load(10'h14a); check(42,59,98);
        load(10'h359); check(59,59,98);
        sw[9:8]=0;
        cycles(4); check(59,59,99);
        cycles(4); check(0,0,0);
        for (step=1;step<=6205;step=step+1) begin
            cycles(4); check(step/6000,(step/100)%60,step%100);
        end
        keys[0]=0; cycles(8); check(1,2,5);
        cycles(50); check(1,2,5); keys[0]=1; cycles(8); check(1,2,7);
        sw[8]=1; cycles(48); check(1,2,7);
        sw[8]=0; cycles(8); check(1,2,9);
        $display("PASS minute preset, invalid input, 59:59.99 wrap, 6205 ticks, pause and resume");
        $finish;
    end
endmodule
