#include "common.h"
#include "io.h"
#include "abcio.h"
#include "sys.h"
#include "console.h"
#include "config.h"
#include "boardinfo_fpga.h"

void __hot main(void)
{
    unsigned int max_abc_latency = 0;
    unsigned int abc_latency;

    init();

    while (1) {
	wait_for_irq();

	if (unlikely(do_write_rtc))
	    write_rtc();

	if (unlikely(do_update_boardinfo))
	    rom_update_boardinfo();

	if (unlikely(do_update_config)) {
	    update_config();
	    abc_init_memmap();
	    rtc_abc_config();
	    abcdisk_config();
	    pun80_config();
	}

	abcdisk_io_poll();

	abc_latency = ABC_LATENCY;
	if (abc_latency > max_abc_latency) {
	    max_abc_latency = abc_latency;
	    con_printf("\nWorst ABC-bus latency: %u SDRAM cycles%s\n",
		       abc_latency & 255,
		       (abc_latency >> 8) ? " UNDERRUN" : "");
	}
    }
}
