// look in pins.pcf for all the pin names on the TinyFPGA BX board
//
// apio build
// tinyprog -p hardware.bin
//
module top (
    input CLK,    // 16MHz clock
    output LED,   // User/boot LED next to power LED
    output USBPU
);
    // drive USB pull-up resistor to '0' to disable USB
    assign USBPU = 0;

    ////////
    // flash the user LED once per second
    ////////

    // Second counter which takes 16MHz clock and divides it down
    // to a 1s output clock.  Wire the output directly to the board LED
    secondcounter cntsec(CLK, LED);

endmodule

//
// Brute-force counter to turn 16 MHz system clock into 1s clock
module secondcounter (
    input clock,
    output second
);

    reg second;

    // Want to produce output that toggles every 0.5 s
    // That means counting to 8M at 16 MHz, which needs 24 bits
    parameter countstart = 24'h7A1200;

    reg [23:0] count24;

    // Initialize to 8M
    initial begin
        count24 <= countstart;
    end

    // Decrement on every clock, reset when it gets to zero
    always @ (posedge clock) begin
        if (count24 == 24'h000000) begin
	    count24 <= countstart;  // Reset to 8M
	    second <= !second;      // Toggle output
	end
        else
	    count24 = count24 - 1;
    end

endmodule