}
#endif
-void swap_read(int swap_base, int core_base, int size) {
- printf("swap_read swap [%d,%d) to core [%d,%d)\n", swap_base, swap_base + size, core_base, core_base + size);
+// bases are in blocks, size is in bytes (allows incomplete last block)
+void swap_read(int swap_base, int core_base, long size) {
+ int blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
+ printf("swap_read swap [%d,%d) to core [%d,%d)\n", swap_base, swap_base + blocks, core_base, core_base + blocks);
#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("blocks");
#endif
for (int i = 0; i < size; ++i) {
+ int swap_block = swap_base + i;
#ifdef INDIRECT_SWAP
- int swap_block = swap_table_mem[swap_base + i];
+ swap_block = swap_table_mem[swap_block];
printf(" %d", swap_block);
-#else
- int swap_block = swap_base + i;
#endif
+ long swap_addr = (long)swap_block << BLOCK_SHIFT;
+ int core_block = core_base + i;
#ifdef INDIRECT_CORE
- int core_block = core_table_mem[core_base + i];
+ core_block = core_table_mem[core_block];
#ifdef INDIRECT_SWAP
printf(",%d", core_block);
#else
printf(" %d", core_block);
#endif
-#else
- int core_block = core_base + i;
#endif
- for (int j = 0; j < BLOCK_SIZE; ++j)
- assert(core_block_mem[core_block * BLOCK_SIZE + j] == 0xaa);
- memcpy(
- core_block_mem + core_block * BLOCK_SIZE,
- swap_block_mem + swap_block * BLOCK_SIZE,
- BLOCK_SIZE
- );
- memset(
- swap_block_mem + swap_block * BLOCK_SIZE,
- 0xaa,
- BLOCK_SIZE
- );
+ long core_addr = (long)core_block << BLOCK_SHIFT;
+ int count = size < BLOCK_SIZE ? (int)size : BLOCK_SIZE;
+ size -= count;
+ for (int j = 0; j < count; ++j)
+ assert(core_block_mem[core_addr + j] == 0xaa);
+ memcpy(core_block_mem + core_addr, swap_block_mem + swap_addr, count);
+ memset(swap_block_mem + swap_addr, 0xaa, count);
}
#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("\n");
#endif
}
-void swap_write(int swap_base, int core_base, int size) {
- printf("swap_write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + size, swap_base, swap_base + size);
+void swap_write(int swap_base, int core_base, long size) {
+ int blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
+ printf("swap_write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, swap_base, swap_base + blocks);
#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("blocks");
#endif
for (int i = 0; i < size; ++i) {
+ int core_block = core_base + i;
#ifdef INDIRECT_CORE
- int core_block = core_table_mem[core_base + i];
+ core_block = core_table_mem[core_block];
printf(" %d", core_block);
-#else
- int core_block = core_base + i;
#endif
+ long core_addr = (long)core_block << BLOCK_SHIFT;
+ int swap_block = swap_base + i;
#ifdef INDIRECT_SWAP
- int swap_block = swap_table_mem[swap_base + i];
+ swap_block = swap_table_mem[swap_block];
#ifdef INDIRECT_CORE
printf(",%d", swap_block);
#else
printf(" %d", swap_block);
#endif
-#else
- int swap_block = swap_base + i;
#endif
- for (int j = 0; j < BLOCK_SIZE; ++j)
- assert(swap_block_mem[swap_block * BLOCK_SIZE + j] == 0xaa);
- memcpy(
- swap_block_mem + swap_block * BLOCK_SIZE,
- core_block_mem + core_block * BLOCK_SIZE,
- BLOCK_SIZE
- );
- memset(
- core_block_mem + core_block * BLOCK_SIZE,
- 0xaa,
- BLOCK_SIZE
- );
+ long swap_addr = (long)swap_block << BLOCK_SHIFT;
+ int count = size < BLOCK_SIZE ? (int)size : BLOCK_SIZE;
+ size -= count;
+ for (int j = 0; j < count; ++j)
+ assert(swap_block_mem[swap_addr + j] == 0xaa);
+ memcpy(swap_block_mem + swap_addr, core_block_mem + core_addr, count);
+ memset(core_block_mem + core_addr, 0xaa, count);
}
#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("\n");