1 /* 2 * A memory card region accessor. 3 * 4 * Copyright (C) 2021, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <string.h> 23 24 #include <algorithm> 25 26 #include "msc_region_accessor.h" 27 28 29 30 MscRegionAccessor::MscRegionAccessor(MscRegionOperations *ops, 31 uint32_t start_block, uint32_t num_blocks, 32 fileid_t fileid) 33 : Accessor(fileid), _ops(ops) 34 { 35 // NOTE: Block size assumption employed. 36 37 _start = start_block * 512; 38 _size = num_blocks * 512; 39 } 40 41 /* Return the size of the file. */ 42 43 offset_t MscRegionAccessor::get_size() 44 { 45 return _size; 46 } 47 48 /* Update the size of the file. */ 49 50 void MscRegionAccessor::set_size(offset_t size) 51 { 52 (void) size; 53 } 54 55 /* Data transfer helper methods. */ 56 57 /* Fill the populated portion of a flexpage. */ 58 59 void MscRegionAccessor::fill_populated(Flexpage *flexpage) 60 { 61 offset_t filepos = flexpage->base_offset; 62 offset_t populated_size = std::min(flexpage->size, _size - filepos); 63 64 /* Tag the region with file state. */ 65 66 flexpage->region->fill(fileid, filepos); 67 68 /* Fill the region with file content. */ 69 70 _ops->read(flexpage->base_addr, flexpage->base_addr_physical, 71 _start + filepos, populated_size); 72 73 /* Pad the flexpage with zero. */ 74 75 if (populated_size < flexpage->size) 76 memset((void *) (flexpage->base_addr + populated_size), 0, 77 flexpage->size - populated_size); 78 } 79 80 /* Flush the populated portion of a flexpage. */ 81 82 void MscRegionAccessor::flush_populated(Flexpage *flexpage) 83 { 84 offset_t filepos = flexpage->base_offset; 85 offset_t populated_size = std::min(flexpage->size, _size - filepos); 86 87 /* Remove the file state tag from the region. */ 88 89 flexpage->region->flush(); 90 91 /* Flush the file content. */ 92 93 _ops->write(flexpage->base_addr, flexpage->base_addr_physical, 94 _start + filepos, populated_size); 95 } 96 97 // vim: tabstop=4 expandtab shiftwidth=4