-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* BCST - Introduction to Computer Systems | ||
* Author: [email protected] | ||
* Github: https://github.com/yangminz/bcst_csapp | ||
* Bilibili: https://space.bilibili.com/4564101 | ||
* Zhihu: https://www.zhihu.com/people/zhao-yang-min | ||
* This project (code repository and videos) is exclusively owned by yangminz | ||
* and shall not be used for commercial and profitting purpose | ||
* without yangminz's permission. | ||
*/ | ||
|
||
// Dynamic Random Access Memory | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "headers/cpu.h" | ||
#include "headers/memory.h" | ||
#include "headers/common.h" | ||
#include "headers/address.h" | ||
|
||
// each swap file is swap page | ||
// each line of this swap page is one uint64 | ||
#define SWAP_PAGE_FILE_LINES 512 | ||
|
||
// disk address counter | ||
static uint64_t internal_swap_daddr = 0; | ||
|
||
int swap_in(uint64_t daddr, uint64_t ppn) | ||
{ | ||
FILE *fr = NULL; | ||
char filename[128]; | ||
sprintf(filename, "../files/swap/page-%ld.txt", daddr); | ||
fr = fopen(filename, "r"); | ||
assert(fr != NULL); | ||
|
||
uint64_t ppn_ppo = ppn << PHYSICAL_PAGE_OFFSET_LENGTH; | ||
char buf[64] = {'0'}; | ||
for (int i = 0; i < SWAP_PAGE_FILE_LINES; ++ i) | ||
{ | ||
char *str = fgets(buf, 64, fr); | ||
*((uint64_t *)(&pm[ppn_ppo + i * 8])) = string2uint(str); | ||
} | ||
fclose(fr); | ||
} | ||
|
||
int swap_out(uint64_t daddr, uint64_t ppn) | ||
{ | ||
FILE *fw = NULL; | ||
char filename[128]; | ||
sprintf(filename, "../files/swap/page-%ld.txt", daddr); | ||
fw = fopen(filename, "w"); | ||
assert(fw != NULL); | ||
|
||
uint64_t ppn_ppo = ppn << PHYSICAL_PAGE_OFFSET_LENGTH; | ||
for (int i = 0; i < SWAP_PAGE_FILE_LINES; ++ i) | ||
{ | ||
fprintf(fw, "0x%16lx\n", *((uint64_t *)(&pm[ppn_ppo + i * 8]))); | ||
} | ||
fclose(fw); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters