-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
108 lines (88 loc) · 1.64 KB
/
mem.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "ram.h"
nibble *MAINMEM = NULL;
nibble *SECONDMEM = NULL;
// Initailize Memory Function
int initMem(void)
{
MAINMEM = (nibble*)calloc(MEMSIZE, sizeof(nibble));
if (MAINMEM != NULL)
{
setBoot(MAINMEM);
printf("Memory Initialized\n");
}
SECONDMEM = (nibble*)calloc(MEMSIZE, sizeof(nibble));
if (SECONDMEM != NULL)
{
setBoot(SECONDMEM);
printf("Secondary Memory Initialized\n");
return 1;
}
return -1;
}
// Print all of Memory
void printMem(uint16_t lower, uint16_t upper, int mem)
{
nibble* memory;
if(mem == 1)
memory = SECONDMEM;
else
memory = MAINMEM;
if(memory == NULL)
{
puts("No allocated memory");
return;
}
puts("Memory contents: ");
for(int i = lower; i < upper ; i++)
{
printf("%s: %s", tobitstr(MEMADDRSIZE, i, PLATEND), tobitstr(MEMMODSIZE, memory[i].data, PLATEND));
if(i < IOMEM && IOMEM > 0)
{
printf(" (I/O)");
}
else if(i >= (IOMEM) && i < (IOMEM + BOOTMEM) && BOOTMEM > 0)
{
printf(" (Boot ROM)");
}
puts("");
}
}
// Writes to Memory
void writeMem(nibble value, uint16_t address)
{
if(MAINMEM == NULL)
{
return;
}
else if(address < 3)
{
return;
}
MAINMEM[address % MEMSIZE].data = value.data;
}
// Reads from Memory
nibble readMem(uint16_t address)
{
if(address < MEMSIZE)
{
return MAINMEM[address];
}
printf("WARNING Address accessed was negative");
nibble n;
return n;
}
// Frees used Memory
void freeMem(void)
{
if(MAINMEM == NULL || MEMSIZE == 0)
{
return;
}
free(MAINMEM);
return;
}
void setBoot(nibble * mem){
for(int i = 0; i < 16; i++){
mem[i+IOMEM].data = i;
}
}