#include "common.h"
#include "jtag.h"

const uint8_t tap_state_next[16][2] = {
    [TAP_TEST_LOGIC_RESET] = { TAP_RUN_TEST_IDLE,  TAP_TEST_LOGIC_RESET },
    [TAP_RUN_TEST_IDLE   ] = { TAP_RUN_TEST_IDLE,  TAP_SELECT_DR_SCAN   },
    [TAP_SELECT_DR_SCAN  ] = { TAP_CAPTURE_DR,     TAP_SELECT_IR_SCAN   },
    [TAP_CAPTURE_DR      ] = { TAP_SHIFT_DR,       TAP_EXIT1_DR         },
    [TAP_SHIFT_DR        ] = { TAP_SHIFT_DR,       TAP_EXIT1_DR         },
    [TAP_EXIT1_DR        ] = { TAP_PAUSE_DR,       TAP_UPDATE_DR        },
    [TAP_PAUSE_DR        ] = { TAP_PAUSE_DR,       TAP_EXIT2_DR         },
    [TAP_EXIT2_DR        ] = { TAP_SHIFT_DR,       TAP_UPDATE_DR        },
    [TAP_UPDATE_DR       ] = { TAP_RUN_TEST_IDLE,  TAP_SELECT_DR_SCAN   },
    [TAP_SELECT_IR_SCAN  ] = { TAP_CAPTURE_IR,     TAP_TEST_LOGIC_RESET },
    [TAP_CAPTURE_IR      ] = { TAP_SHIFT_IR,       TAP_EXIT1_IR         },
    [TAP_SHIFT_IR        ] = { TAP_SHIFT_IR,       TAP_EXIT1_IR         },
    [TAP_EXIT1_IR        ] = { TAP_PAUSE_IR,       TAP_UPDATE_IR        },
    [TAP_PAUSE_IR        ] = { TAP_PAUSE_IR,       TAP_EXIT2_IR         },
    [TAP_EXIT2_IR        ] = { TAP_SHIFT_IR,       TAP_UPDATE_IR        },
    [TAP_UPDATE_IR       ] = { TAP_RUN_TEST_IDLE,  TAP_SELECT_DR_SCAN   }
};

const char tap_state_names[16][4] =
{
    "TLR", "RTI", "SDS", "CDR", "SDR", "EDR", "PDR", "ED2",
    "UDR", "SIS", "CIR", "SIR", "EIR", "PIR", "EI2", "UIR"
};

#ifdef MAKE_TAPROUTE_C /* Generate taproute.c */

#include <stdio.h>
#include <limits.h>

int main(void)
{
    int dir[16][16];
    unsigned int distance[16][16];
    memset(dir, 0, sizeof dir);

    memset(distance, 0x55, sizeof distance); /* Just don't wrap */
    for (unsigned int fs = 0; fs < 16; fs++) {
	distance[fs][fs] = 0;
	dir[fs][fs] = tap_state_next[fs][0] != fs;
    }

    bool better;
    do {
	better = false;
	for (unsigned int fs = 0; fs < 16; fs++) {
	    for (unsigned int ts = 0; ts < 16; ts++) {
		for (unsigned int d = 0; d < 2; d++) {
		    unsigned int next = tap_state_next[fs][d];
		    unsigned int next_cost = distance[next][ts]+1;
		    if (distance[fs][ts] > next_cost) {
			distance[fs][ts] = next_cost;
			dir[fs][ts] = d;
			better = true;
		    }
		}
	    }
	}
    } while (better);


    printf("/*\n"
	   " * This is a generated file!\n"
	   " *\n"
	   " * JTAG state routing table: to get from the horizontal\n"
	   " * axis state to the vertical axis state, go in the given\n"
	   " * direction. The table in this comment also shows the number\n"
	   " * of transitions needed.\n"
	   " *\n");

    printf(" * from:");
    for (unsigned int fs = 0; fs < 16; fs++)
	printf(" %3s", tap_state_names[fs]);
    printf("\n *\n");

    for (unsigned int ts = 0; ts < 16; ts++) {
	printf(" * %3s: ", tap_state_names[ts]);
	for (unsigned int fs = 0; fs < 16; fs++) {
	    printf(" %d/%d", dir[fs][ts], distance[fs][ts]);
	}
	printf("\n");
    }

    printf(" *\n");
    for (unsigned int d = 0; d < 2; d++) {
	printf(" *  [%u] ", d);
	for (unsigned int fs = 0; fs < 16; fs++)
	    printf(" %3s", tap_state_names[tap_state_next[fs][d]]);
	printf("\n");
    }

    printf(" */\n\n");

    printf("#include \"jtag.h\"\n\n");
    printf("const uint16_t tap_state_route[16] = {\n");

    for (unsigned int ts = 0; ts < 16; ts++) {
	unsigned int dirmask = 0;
	for (unsigned int fs = 0; fs < 16; fs++) {
	    dirmask |= dir[fs][ts] << fs;
	}
	printf("\t0x%04x,\n", dirmask);
    }
    printf("};\n");

    return 0;
}

#endif /* MAKE_TAPROUTE_C */
