#!/usr/bin/env python3
"""Script for generating joystick response curve tables.

 --Jacques Gagnon <darthcloud@gmail.com>
"""

import os
from js_scale import get_joysticks
from js_curves import get_gc_js_curve_tables

if __name__ == "__main__":
    scale_tables = get_gc_js_curve_tables(get_joysticks())

    tableasm = "../firmware/js_curve.inc"

    if os.path.exists(tableasm):
        os.unlink(tableasm)

    FILE = open(tableasm, "wb")

    FILE.write(b"    ;; 256 bytes tables for GC joysticks response curve.\n")
    FILE.write(b"    ;; Generated by Jacques Gagnon <darthcloud@gmail.com>.\n")
    FILE.write(b"    ;; See notes/gen_asm_js_curve.py\n")
    FILE.write(b"\n")
    FILE.write(b"gamecube_js_curves\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[0][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[2][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[4][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[6][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"gamecube_cs_curves\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[1][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[3][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[5][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.write(b"    db  ")

    for byte in range(256):
        FILE.write(b"0x%02X" % (scale_tables[7][byte] & 0xFF))
        if byte != 255:
            FILE.write(b", ")

    FILE.write(b"\n\n")
    FILE.close()

    print("{} generated.".format(tableasm))

### The End ###
