-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
189 lines (162 loc) · 3.94 KB
/
main.c
File metadata and controls
189 lines (162 loc) · 3.94 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#pragma region : Custom definitions (You can change anything you want here easily)
typedef long word_type;
// If you want to use records for example you can do:
// typedef struct { long a; long b; } word_type;
// And then change the functions below to work with this structure
// How many word rows is there in memory
#define wordsCount 5
// Memory blocks (cache memory in theory) you need to set data here
// Or you can just set them one-by-one in `main`
word_type M[wordsCount] = {1, 9999999, 64, 127, 65536};
word_type wordZero = 0;
bool isMatch(word_type key, word_type mask, word_type mem)
{
return (key & mask) == (mem & mask);
}
word_type sum(word_type total, word_type mem)
{
return total | mem;
}
word_type resultingWriteWord(word_type completeWord, word_type key, word_type mask)
{
return (completeWord & (-1 - mask)) | (key & mask);
}
#pragma endregion
#pragma region : Flip-Flops & Command signals
word_type Key;
word_type Mask;
bool AP[wordsCount]; // 'Already Processed' (aka. BT) flip-flops
bool Indicators[wordsCount];
word_type Output;
bool S_I; // Selection/Inhiboolion
bool S_N; // Some/None
#pragma endregion
#pragma region : Internal Functions
void UpdateIndicator(int i)
{
if (AP[i] || (S_I && i>0 && S_N) || !isMatch(Key, Mask, M[i]) )
Indicators[i] = 0;
}
#pragma endregion
#pragma region : Given Functions
bool GetBit(long num, int at)
{
return (num & (1<<at))? 1: 0;
}
void SetBit(long *num, int at, bool val)
{
long positionedbool = 1<<at;
*num = ((*num) & (-1 - positionedbool)) | (val * positionedbool);
}
void Set()
{
for (int i = 0; i < wordsCount; i++)
{
Indicators[i] = 1;
}
}
void Reset()
{
for (int i = 0; i < wordsCount; i++)
{
AP[i] = 0;
}
}
void SetAP(int word)
{
AP[word] = 1;
Indicators[word] = 0;
}
void Search()
{
S_N = 0;
for (int i = 0; i < wordsCount; i++)
{
UpdateIndicator(i);
if (Indicators[i] == 1)
S_N = 1;
}
}
void Write()
{
for (int i = 0; i < wordsCount; i++)
{
if (!Indicators[i])
continue;
M[i] = resultingWriteWord(M[i], Key, Mask);
}
}
void Read()
{
Output = wordZero;
for (int i=0; i < wordsCount; i++)
{
if (!Indicators[i] || !isMatch(Key, Mask, M[i]))
continue;
Output = sum(Output, M[i]);
}
}
#pragma endregion
#pragma region : Extra Functions
void displayNums(long M[], int n)
{
for (int i=0; i < n; i++)
{
printf("%ld\n", M[i]);
}
printf("\n");
}
void displayBinaryNums(long M[], int n, bool displayDecAlong)
{
for (int i = 0; i < n; i++)
{
for (int b=31; b >= 0; b--)
{
printf("%d", GetBit(M[i], b));
}
if (displayDecAlong) printf(" (%ld)", M[i]);
printf("\n");
}
printf("\n");
}
#pragma endregion
int main()
{
displayBinaryNums(M, wordsCount, true);
// This Algorithm will increment every number in memory by 1
// Assuming they are positive numbers (64-bit signed integers)
// [To change the word type, default words or words count modify code on top]
init:
int cpt;
S_I = 0; Reset();
cpt = 0;
SetBit(&Key, 63, 1);
Mask = 0; SetBit(&Mask, 63, 1);
Set();
Write();
search0:
SetBit(&Key, cpt, 0); SetBit(&Key, 63, 1);
SetBit(&Mask, cpt, 1); SetBit(&Mask, 63, 1);
Set();
Search();
if (S_N == 0) goto search1;
SetBit(&Key, cpt, 1); SetBit(&Key, 63, 0);
Write();
search1:
SetBit(&Key, cpt, 1); SetBit(&Key, 63, 1);
Set();
Search();
if (S_N == 0) goto end;
SetBit(&Key, cpt, 0); SetBit(&Key, 63, 1);
Write();
SetBit(&Mask, cpt, 0);
cpt += 1;
if (cpt == 63) goto end;
goto search0;
end:
displayBinaryNums(M, wordsCount, true);
return 0;
}