-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cc
40 lines (35 loc) · 1.03 KB
/
memory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <iostream>
#include "memory.h"
void Memory::init(unsigned int size, unsigned int block_size, bus_t* bus){
mem = new uint8_t[size];
for (unsigned int i = 0; i < size; i++) mem[i] = 0;
this->size = size;
this->block_size = block_size;
this->bus = bus;
writebacks = 0;
data_reqs = 0;
}
void Memory::access(addr_t physical_addr, access_t access_type){
uint8_t* mem_block = mem + physical_addr;
if (access_type == STORE) {
memcpy(mem_block, bus->data, sizeof(uint8_t) * block_size);
if (verbose) std::cout << " WRITEBACK TO MEM\n";
writebacks++;
} else {
memcpy(bus->data, mem_block, sizeof(uint8_t) * block_size);
if (verbose) std::cout << " DATA REQ FROM MEM\n";
data_reqs++;
}
}
void Memory::print_stats() {
printf("Writebacks: %llu\n"
"Data requests from memory: %llu\n",
writebacks, data_reqs);
}
Memory::~Memory(){
delete [] mem;
}