# -*- coding: utf-8 -*-

"""Provide standard and vendor specific SCSI commands.

Every command is a tuple (command, expected response length).
Commands that contain fileds that are typically altered during use
(e.g. read commands need to specify an address) are presented as a list of byte 
values. Other commands are presented as an immutable sequence of byte values.
"""

# static commands are tuples
# commands with variables are lists
# format: (CMD, EXPECTED_RETURN_LENGTH)
GET_VENDOR_INFO = (
    (0x06, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01),
    512+16
)

GET_CHIP_ID = (
    (
        0x06, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00
    ),
    512
)

# not a vendor specific cmd
GET_NUM_LBAS = (
    (
        0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00
    ),
    8
)

JUMP_TO_PRAM = (
    (
        0x06, 0xB3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ),
    0
)

JUMP_TO_BOOTMODE = (
    (
        0x06, 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ),
    0
)

LOAD_HEADER = (
    [
        0x06, 0xB1, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ],
    0
)

# Load code body at address = page*512
# CBWCB: 06 B1 02 page_h page_l 00 00 count_h count_l
# DATA OUT: data[count*512]
LOAD_BODY = (
    [
        0x06, 0xB1, 0x02, 0xaa, 0xaa, 0x00, 0x00, 0xcc, 
        0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ],
    0
)

# Status[0] must be 55 for header load, A5 for body load.
GET_STATUS = (
    (
        0x06, 0xB0, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ),
    8
)

# Read 512 bytes of XDATA at given address
# CBWCB: 06 05 'R' 'A' addr_h addr_l
# DATA IN: xdata_at_addr[512], junk[16]
READ_MEMORY = (
    [
        0x06, 0x05, 0x52, 0x41, 0xaa, 0xaa, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ],
    512+16
)

# Write 1 byte to a given XDATA address
# CBWCB: 06 0C 00 'P' 'h' 'I' addr_h addr_l data
WRITE_MEMORY = (
    [
        0x06, 0x0C, 0x00, 0x50, 0x68, 0x49, 0xaa, 0xaa, 
        0xdd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ], 
    0
)

READ_BODY = [
    [
        0x06, 0xB2, 0x10, 0xaa, 0xaa, 0x00, 0x00, 0xcc, 
        0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ],
    0
]

SEND_PASSWORD = (
    (
        0x0E, 0x00, 0x01, 0x55, 0xAA, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ),
    0
)

SCARY_B7 = (
    (
        0x06, 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00
    ),
    0
)

FIRMWARE_UPDATE = (
    (
        0x06, 0xEE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00
    ),
    64+8
)

READ_XRAM = (
    [
        0x06, 0x06, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ],
    1
)

# 0xF000 upwards seem to be (external) function registers
WRITE_XRAM = (
    [0x06, 0x07, 0xaa, 0xaa, 0xee, 0x00, 0x00],
    1
)

READ_IRAM = (
    [0x06, 0x08, 0xaa, 0x00, 0x00, 0x00, 0x00],
    1
)

WRITE_IRAM = (
    [0x06, 0x09, 0xaa, 0xee, 0x00, 0x00],
    1
)

