# Copyright (c) 2017-2019 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import struct
from spinn_utilities.overrides import overrides
from spinnman.messages.scp.abstract_messages import (
    AbstractSCPRequest, BMPRequest)
from spinnman.messages.scp import SCPRequestHeader
from spinnman.messages.scp.enums import SCPCommand
from .check_ok_response import CheckOKResponse

_ONE_WORD = struct.Struct("<I")


class WriteFPGARegister(BMPRequest):
    """ A request for writing a word to a FPGA (SPI) register.

    See the SpI/O project's spinnaker_fpga design's `README`_ for a listing
    of FPGA registers. The SpI/O project can be found on GitHub at:
    https://github.com/SpiNNakerManchester/spio/

    .. _README: https://github.com/SpiNNakerManchester/spio/\
                blob/master/designs/spinnaker_fpgas/README.md#spi-interface
    """
    __slots__ = []

    def __init__(self, fpga_num, addr, value, board):
        """
        :param int fpga_num: FPGA number (0, 1 or 2) to communicate with.
        :param int addr: Register address to read or write to (will be rounded
            down to the nearest 32-bit word boundary).
        :param int value: A 32-bit int value to write to the register
        """
        super().__init__(
            board,
            SCPRequestHeader(command=SCPCommand.CMD_LINK_WRITE),
            argument_1=addr & (~0x3), argument_2=4, argument_3=fpga_num,
            data=_ONE_WORD.pack(value))

    @overrides(AbstractSCPRequest.get_scp_response)
    def get_scp_response(self):
        return CheckOKResponse("Send FPGA register write", "CMD_LINK_WRITE")
