@@ -19,14 +19,14 @@ WriteResult
1919MemorySection::write (Offset dst_offset, const void *source, size_t size, WriteOptions options) {
2020 UNUSED (options)
2121
22- auto destination = static_cast <size_t >(dst_offset);
23-
24- if (destination >= length ()) {
22+ if (dst_offset >= length ()) {
2523 throw SIMULATOR_EXCEPTION (
2624 OutOfMemoryAccess, " Trying to write outside of the memory section" ,
27- QString (" Accessing using offset: " ) + QString::number (destination ));
25+ QString (" Accessing using offset: " ) + QString::number (dst_offset ));
2826 }
2927
28+ size_t destination = static_cast <size_t >(dst_offset);
29+
3030 // Size the can be read from this section
3131 const size_t available_size = std::min (destination + size, length ()) - destination;
3232
@@ -41,16 +41,16 @@ ReadResult
4141MemorySection::read (void *destination, Offset src_offset, size_t size, ReadOptions options) const {
4242 UNUSED (options)
4343
44- auto source = static_cast <size_t >(src_offset);
45-
46- size = std::min (source + size, length ()) - source;
47-
48- if (source >= length ()) {
44+ if (src_offset >= length ()) {
4945 throw SIMULATOR_EXCEPTION (
5046 OutOfMemoryAccess, " Trying to read outside of the memory section" ,
51- QString (" Accessing using offset: " ) + QString::number (source ));
47+ QString (" Accessing using offset: " ) + QString::number (src_offset ));
5248 }
5349
50+ size_t source = static_cast <size_t >(src_offset);
51+
52+ size = std::min (source + size, length ()) - source;
53+
5454 memcpy (destination, &dt[source], size);
5555
5656 return { .n_bytes = size };
@@ -80,10 +80,6 @@ bool MemorySection::operator!=(const MemorySection &ms) const {
8080// Settings sanity checks
8181static_assert (MEMORY_SECTION_SIZE != 0 , " Nonzero memory section size is required." );
8282static_assert (MEMORY_TREE_ROW_SIZE != 0 , " Nonzero memory tree row size is required." );
83- static_assert (
84- ((32 - MEMORY_SECTION_BITS ) % MEMORY_TREE_BITS ) == 0 ,
85- " Number of bits in tree row has to be exact division of available number "
86- " of bits." );
8783
8884/* *
8985 * Generate mask to get memory section index from address.
@@ -96,14 +92,15 @@ static_assert(
9692 * ```address & generate_mask(8, 0)```
9793 */
9894constexpr uint64_t generate_mask (size_t section_size, size_t unit_size) {
99- return ((1U << section_size) - 1 ) << unit_size;
95+ return ((1LLU << section_size) - 1 ) << unit_size;
10096}
10197
10298/* *
10399 * Get index in row for given offset and row number i
104100 */
105101constexpr size_t tree_row_bit_offset (size_t i) {
106- return 32 - MEMORY_TREE_BITS - i * MEMORY_TREE_BITS ;
102+ return (MEMORY_TREE_BITS * MEMORY_TREE_DEPTH + MEMORY_SECTION_BITS )
103+ - MEMORY_TREE_BITS * (i + 1 );
107104}
108105
109106/*
@@ -143,7 +140,7 @@ void Memory::reset(const Memory &m) {
143140 this ->mt_root = copy_section_tree (m.get_memory_tree_root (), 0 );
144141}
145142
146- MemorySection *Memory::get_section (size_t offset, bool create) const {
143+ MemorySection *Memory::get_section (Offset offset, bool create) const {
147144 union MemoryTree *w = this ->mt_root ;
148145 size_t row_num;
149146 // Walk memory tree branch from root to leaf and create new nodes when
@@ -221,7 +218,7 @@ union machine::MemoryTree *Memory::allocate_section_tree() {
221218 return mt;
222219}
223220
224- void Memory::free_section_tree (union MemoryTree *mt, size_t depth) {
221+ void Memory::free_section_tree (union MemoryTree *mt, unsigned depth) {
225222 if (depth < (MEMORY_TREE_DEPTH - 1 )) { // Following level is memory tree
226223 for (size_t i = 0 ; i < MEMORY_TREE_ROW_SIZE ; i++) {
227224 if (mt[i].subtree != nullptr ) {
@@ -239,7 +236,7 @@ void Memory::free_section_tree(union MemoryTree *mt, size_t depth) {
239236bool Memory::compare_section_tree (
240237 const union MemoryTree *mt1,
241238 const union MemoryTree *mt2,
242- size_t depth) {
239+ unsigned depth) {
243240 if (depth < (MEMORY_TREE_DEPTH - 1 )) { // Following level is memory tree
244241 for (size_t i = 0 ; i < MEMORY_TREE_ROW_SIZE ; i++) {
245242 if (((mt1[i].subtree == nullptr || mt2[i].subtree == nullptr )
@@ -260,7 +257,7 @@ bool Memory::compare_section_tree(
260257 return true ;
261258}
262259
263- union machine::MemoryTree *Memory::copy_section_tree (const union MemoryTree *mt, size_t depth) {
260+ union machine::MemoryTree *Memory::copy_section_tree (const union MemoryTree *mt, unsigned depth) {
264261 union MemoryTree *nmt = allocate_section_tree ();
265262 if (depth < (MEMORY_TREE_DEPTH - 1 )) { // Following level is memory tree
266263 for (size_t i = 0 ; i < MEMORY_TREE_ROW_SIZE ; i++) {
0 commit comments