Permalink
Browse files

Finished memory class

  • Loading branch information...
1 parent b5e64b8 commit 12de8382d09b845bb46f2a7c20cfcd1bbf72c15b @pksunkara committed Aug 8, 2011
Showing with 87 additions and 16 deletions.
  1. +10 −0 INPUT
  2. +14 −2 access.cpp
  3. +13 −1 access.h
  4. +0 −1 cache.cpp
  5. +5 −0 cache.h
  6. +24 −6 main.cpp
  7. +11 −4 memory.cpp
  8. +10 −2 memory.h
View
10 INPUT
@@ -1 +1,11 @@
10
+0 1 2 3 4 5 6 7 8 9
+1 2 3 4 5 6 7 8 9 10
+2 3 4 5 6 7 8 9 10 11
+3 4 5 6 7 8 9 10 11 12
+4 5 6 7 8 9 10 11 12 13
+5 6 7 8 9 10 11 12 13 14
+6 7 8 9 10 11 12 13 14 15
+7 8 9 10 11 12 13 14 15 16
+8 9 10 11 12 13 14 15 16 17
+9 10 11 12 13 14 15 16 17 18
View
@@ -4,6 +4,18 @@
using namespace std;
-Access::Access(void) {}
+Access::Access(int matrix_n) {
+ this->memory = new Memory(matrix_n);
+}
-Access::~Access(void) {}
+void Access::write(int x, int y, int value) {
+ this->memory->set(x, y, value);
+}
+
+int Access::get(int x, int y) {
+ return this->memory->get(x, y);
+}
+
+Access::~Access(void) {
+ delete this->memory;
+}
View
@@ -1,8 +1,20 @@
+#ifndef ACCESS_H
+#define ACCESS_H
+
+#include "memory.h"
+#include "cache.h"
+
class Access {
public:
- Access(void);
+ Access(int matrix_n);
+
+ void write(int x, int y, int value);
+ int get(int x, int y);
~Access(void);
private:
+ Memory* memory;
};
+
+#endif
View
@@ -16,7 +16,6 @@ Cache::Cache(int cache_size, int block_size, int assoc) {
//TODO: Introduce blocks later
this->memory = new int(this->cache_size/4);
- return;
}
int Cache::get_hits(void) {
View
@@ -1,3 +1,6 @@
+#ifndef CACHE_H
+#define CACHE_H
+
class Cache {
public:
Cache(int cache_size, int block_size, int assoc);
@@ -17,3 +20,5 @@ class Cache {
int hits;
int misses;
};
+
+#endif
View
@@ -8,15 +8,21 @@
using namespace std;
+// Global variable declaration
+Access* accessor;
+
+// Global prototype decalaration
+int M(int, int);
+
int main(int argc, char** argv) {
if(argc!=5) {
printf("Usage: %s <cache_size_inKB> <block_size_inB> <associativity> <input_file>\n", argv[0]);
return 1;
}
// Declaring variables
- int n;
- Memory* mem;
+ int i, j, buf;
+ int matrix_n;
ifstream inFile;
// Opening files
@@ -28,14 +34,26 @@ int main(int argc, char** argv) {
return 1;
}
- // Reading Matrix
- inFile >> n;
+ // Reading Matrix Length
+ inFile >> matrix_n;
+
+ // Initalize accessor
+ accessor = new Access(matrix_n);
- // Initalize memory
- mem = new Memory(n);
+ // Reading matrix
+ for(i=0; i<matrix_n; i++) {
+ for(j=0; j<matrix_n; j++) {
+ inFile >> buf;
+ accessor->write(i, j, buf);
+ }
+ }
// Closing files
inFile.close();
return 0;
}
+
+int M(int x, int y) {
+ return accessor->get(x, y);
+}
View
@@ -4,10 +4,17 @@
using namespace std;
-Memory::Memory(int size) {
- this->size = size;
- this->memory = new int(this->size);
- return;
+Memory::Memory(int n) {
+ this->n = n;
+ this->memory = new int[n*n];
+}
+
+void Memory::set(int x, int y, int value) {
+ this->memory[x*n+y] = value;
+}
+
+int Memory::get(int x, int y) {
+ return this->memory[x*n+y];
}
Memory::~Memory(void) {
View
@@ -1,10 +1,18 @@
+#ifndef MEMORY_H
+#define MEMORY_H
+
class Memory {
public:
- Memory(int size);
+ Memory(int n);
+
+ void set(int x, int y, int value);
+ int get(int x, int y);
~Memory(void);
private:
int* memory;
- int size;
+ int n;
};
+
+#endif

0 comments on commit 12de838

Please sign in to comment.