# Source code for spinnman.processes.write_memory_process

# Copyright (c) 2017-2019 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# 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 functools
from .abstract_multi_connection_process import AbstractMultiConnectionProcess
from spinnman.constants import UDP_MESSAGE_MAX_SIZE

[docs]class WriteMemoryProcess(AbstractMultiConnectionProcess):
""" A process for writing memory on a SpiNNaker chip.
"""
__slots__ = []
# pylint: disable=too-many-arguments

[docs]    def write_memory_from_bytearray(
self, x, y, p, base_address, data, offset, n_bytes):
""" Writes memory onto a SpiNNaker chip from a bytearray.

:param int x:
The x-coordinate of the chip where the memory is to be written to
:param int y:
The y-coordinate of the chip where the memory is to be written to
:param int p:
The processor of the chip where the memory is to be written to
:param data: the data to write
:type data: bytearray or bytes
:param int offset: where in the data to start writing from
:param int n_bytes: how much data to write
"""
self._write_memory_from_bytearray(
functools.partial(WriteMemory, x=x, y=y, cpu=p))

""" Writes memory onto a neighbour of a SpiNNaker chip from a\
bytearray.

:param int x:
The x-coordinate of the chip where the memory is to be written to
:param int y:
The y-coordinate of the chip where the memory is to be written to
:param int p:
The processor of the chip where the memory is to be written to
Along which link is the neighbour.
:param data: the data to write
:type data: bytearray or bytes
:param int offset: where in the data to start writing from
:param int n_bytes: how much data to write
"""
self._write_memory_from_bytearray(

""" Writes memory onto a SpiNNaker chip from a reader.

:param int x:
The x-coordinate of the chip where the memory is to be written to
:param int y:
The y-coordinate of the chip where the memory is to be written to
:param int p:
The processor of the chip where the memory is to be written to
:param int n_bytes: how much data to write
"""
functools.partial(WriteMemory, x=x, y=y, cpu=p))

""" Writes memory onto a neighbour of a SpiNNaker chip from a reader.

:param int x:
The x-coordinate of the chip where the memory is to be written to
:param int y:
The y-coordinate of the chip where the memory is to be written to
:param int p:
The processor of the chip where the memory is to be written to
Along which link is the neighbour.
:param int n_bytes: how much data to write
"""

def _write_memory_from_bytearray(
self, base_address, data, data_offset, n_bytes, packet_class):
offset = 0
n_bytes_to_write = int(n_bytes)
while n_bytes_to_write > 0:
bytes_to_send = min((n_bytes_to_write, UDP_MESSAGE_MAX_SIZE))
data_array = data[data_offset:data_offset + bytes_to_send]

self._send_request(packet_class(

n_bytes_to_write -= bytes_to_send
offset += bytes_to_send
data_offset += bytes_to_send
self._finish()
self.check_for_error()

offset = 0
n_bytes_to_write = int(n_bytes)
while n_bytes_to_write > 0: