/* pk-copy.pk - `copy' command. */ /* Copyright (C) 2020, 2021 Jose E. Marchesi */ /* 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 . */ pk_help_add_topic :entry Poke_HelpEntry { category = "commands", topic = "copy", summary = "Copy a range of memory.", description = " Copy a range of bytes from a given offset at the given IO space to another given offset in the same or another IO space. Synopsis: copy [:from OFFSET] [:to OFFSET] [:size OFFSET] \\ [:from_ios IOS] [:to_ios IOS] Arguments: :from (offset) Beginning of the range to copy. Defaults to 0#B. :size (offset) Size of the range to copy. The offset provided in this argument is truncated to bytes. :to (offset) Offset where to copy the specified range. Defaults to :from. :from_ios (int) Origin IO space. Defaults to the current IO space. :to_ios (int) Destination IO space. Defaults to the current IO space. If there is not a current IO space available, or any of the specified IO spaces don't exist, `copy' raises an E_no_ios exception. See `.doc copy' for more information." }; fun copy = (int from_ios = get_ios, int to_ios = get_ios, off64 from = 0#B, off64 to = from, off64 size = 0#B) void: { if (size == 0#B || (to == from && to_ios == from_ios)) return; /* Determine the best step size to operate with. */ var step = 1#B; if (size % 8#B == 0#B) step = 8#B; else if (size % 4#B == 0#B) step = 4#B; else if (size % 2#B == 0#B) step = 2#B; /* Change the endianness to big endian, so we get the right ordering of the bytes when operating on steps > 1#B. */ var endian = get_endian; set_endian (ENDIAN_BIG); /* Copy the stuff. */ for (var end = from + size; from < end; from += step, to += step) { if (step == 8#B) uint<64> @ to_ios : to = uint<64> @ from_ios : from; else if (step == 4#B) uint<32> @ to_ios : to = uint<32> @ from_ios : from; else if (step == 2#B) uint<16> @ to_ios : to = uint<16> @ from_ios : from; else byte @ to_ios : to = byte @ from_ios : from; } /* Cleanup. */ set_endian (endian); }