module test_decimal;
    reg clk=0;
    reg [3:0] key=14;
    reg [9:0] switches=0;
    wire [9:0] lamps;
    wire [6:0] h0,h1,h2,h3,h4,h5;
    wire [41:0] digits={h5,h4,h3,h2,h1,h0};
    integer cycles=0, fast_period, slow_period, started;
    decimal_cpu #(.ISSUE_BITS(3)) dut(clk,key,switches,lamps,h0,h1,h2,h3,h4,h5);
    always #1 clk=~clk;
    always @(posedge clk) cycles=cycles+1;
    function [6:0] digit;
        input integer n;
        begin case (n)
            0:digit=64; 1:digit=121; 2:digit=36; 3:digit=48; 4:digit=25;
            5:digit=18; 6:digit=2; 7:digit=120; 8:digit=0; default:digit=16;
        endcase end
    endfunction
    function [39:0] decimal_text;
        input [34:0] pins;
        integer place, value;
        begin
            decimal_text="?????";
            for(place=0;place<5;place=place+1)
                for(value=0;value<10;value=value+1)
                    if(pins[7*place +: 7]===digit(value))
                        decimal_text[8*place +: 8]="0"+value;
        end
    endfunction
    task first_decimal;
        input integer n;
        integer limit, place, remaining;
        reg [39:0] observed, wanted;
        begin
            // The program writes HEX4 first and HEX0 last. Read the first complete result.
            limit=cycles+90000;
            while(h0===7'h7f && cycles<limit) @(negedge clk);
            observed=decimal_text(digits[34:0]);
            remaining=n;
            for(place=0;place<5;place=place+1) begin
                wanted[8*place +: 8]="0"+remaining%10;
                remaining=remaining/10;
            end
            if(h5!==7'h7f || observed!==wanted) begin
                $display("FAIL decimal %0d, first completed display %s (HEX5 pins %b)",n,observed,h5);
                $finish;
            end
        end
    endtask
    task await_decimal;
        input integer n;
        integer i, remaining, until_cycle;
        reg [41:0] wanted;
        begin
            wanted={42{1'b1}}; remaining=n;
            for(i=0;i<5;i=i+1) begin
                wanted[7*i +: 7]=digit(remaining%10);
                remaining=remaining/10;
            end
            until_cycle=cycles+90000;
            while(digits!==wanted && cycles<until_cycle) @(negedge clk);
            if(digits!==wanted) begin $display("FAIL decimal %d, observed pins %h",n,digits); $finish; end
            repeat(50) @(negedge clk);
            if(digits!==wanted) begin $display("FAIL unstable decimal %d",n); $finish; end
        end
    endtask
    task restart;
        input integer options;
        begin
            key=14; switches=options; repeat(12) @(negedge clk);
            if(digits!=={42{1'b1}}) begin $display("FAIL reset"); $finish; end
            key=15; switches=512+options;
        end
    endtask
    initial begin
        restart(0); first_decimal(0); await_decimal(0); await_decimal(1); started=cycles;
        await_decimal(2); fast_period=cycles-started;
        restart(8); first_decimal(9); await_decimal(9); await_decimal(10);
        restart(16); first_decimal(99); await_decimal(99); await_decimal(100);
        restart(31); first_decimal(65534); await_decimal(65534); await_decimal(65535); started=cycles;
        await_decimal(0); slow_period=cycles-started; await_decimal(1);
        if(slow_period <= fast_period*6 || slow_period > fast_period*9) begin $display("FAIL switch-dependent timing"); $finish; end
        $display("PASS decimal 0, 9, 10, 99, 100, 65534, 65535, rollover and variable pacing"); $finish;
    end
endmodule
