#include "common.h"
#include "io.h"
#include "sys.h"
#include "console.h"
#include "zlib.h"

#define VJTAG_FLASH_CMD	0xabc80046

IRQHANDLER(abc,0)
{
    irqhandler_spurious_0(vector);
}
IRQHANDLER(tty,1)
{
    irqhandler_spurious_0(vector);
}

/*
 * SDRAM upload buffer - always first in SDRAM
 */
static uint8_t __attribute__((section(".jtag_flash_buffer")))
jtag_flash_buf[17 << 20];	/* 16 MB + some slack */

void main(void)
{
    static const char hello[] =
	"\n\nMAX80 JTAG update firmware compiled on: ";

    unmask_irqs((1U << ROMCOPY_IRQ)|(1U << EBREAK_IRQ)|(1U << BUSERR_IRQ));

    con_puts(hello);
    con_puts(__datestamp);
    con_putc('\n');

    wait_romcopy_done();

    while (1) {
	uint32_t cmd;

	waitfor(VJTAG_IRQ);
	cmd = VJTAG_CPUCMD;

	if (cmd == VJTAG_FLASH_CMD) {
	    con_puts("JTAG: starting firmware flash process\n");
	    rom_flash_from_memory(jtag_flash_buf, sizeof jtag_flash_buf);
	    con_puts("JTAG: firmware flash process failed\n");
	} else {
	    con_printf("JTAG: unknown command: 0x%08x\n", cmd);
	}
    }
}
