-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitgridengine.pas
202 lines (173 loc) · 5.02 KB
/
bitgridengine.pas
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
190
191
192
193
194
195
196
197
198
199
200
201
unit BitGridEngine;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils;
Type
Nibble = 0..15;
TBitCell = record
input, output : nibble;
lookup : Uint64; // 16 nibbles
end;
tBoolFunction = Function : Boolean;
pBoolFunction = ^tBoolFunction;
tBooleanInput = record
X,Y : Integer;
Source : ^Boolean;
end;
TBitGrid = object
Cells : array of array of TBitCell;
Height,Width : integer;
CycleCount : UInt64;
IOtest : tBooleanInput;
constructor Init(Xsize, YSize : integer);
destructor Done;
procedure DoClock;
procedure DoPhaseA;
procedure DoPhaseB;
procedure PutBit(X,Y : Integer; Bit : Boolean);
end;
Const
PassThrough = $FB73EA62D951C840;
LeftBits = $1111111100000000; LeftBit = 8;
RightBits = $1100110011001100; RightBit = 2;
UpBits = $1010101010101010; UpBit = 1;
DownBits = $1111000011110000; DownBit = 4;
AllBits = (LeftBits * RightBit) or (RightBits * LeftBit) OR (UpBits * DownBit) or (DownBits * UpBit);
Reflector = $FEDCBA9876543210;
Counter = $5DA25DA25DA25DA2;
implementation
Function Wrap(N,Bound : Integer):Integer;
begin
While N >= Bound do
N := N - Bound;
While N < 0 do
N := N + Bound;
Wrap := N;
end;
Constructor TBitGrid.Init(Xsize, YSize : integer);
var
x,y : integer;
begin
CycleCount := 0;
SetLength(Cells,Xsize,Ysize);
width := Xsize;
height := Ysize;
for y := 0 to Height-1 do
for x := 0 to Width-1 do
begin
cells[x,y].input:= 0;
cells[x,y].output := 0;
cells[x,y].lookup := PassThrough;
end;
IOtest.X := 3;
IOtest.Y := 7;
IOtest.Source := nil;
end;
Destructor TBitGrid.Done;
begin
SetLength(Cells,0,0);
end;
Procedure TBitGrid.DoClock;
var
x,y,next : integer;
begin
DoPhaseA;
DoPhaseB;
end;
procedure TBitGrid.DoPhaseA;
var
x,y,next : integer;
begin
// process the override list (one item for now)
If IOtest.Source <> nil then
begin
If IOtest.Source^ then
Cells[IOtest.X, IOtest.Y].input := $0f
else
Cells[IOtest.X, IOtest.Y].input := 00; ;
end;
// do phase A, only even cells
for y := 0 to Height-1 do
for x := 0 to Width-1 do
if NOT Odd(x+y) then
with cells[x,y] do
begin
// compute the new output
output:= lookup SHR (input*4) AND $0f;
// distribute the output to the inputs of neigbors
// right
next := wrap((x+1),width);
if (output AND 2) <> 0 then
cells[next,y].input := cells[next,y].input OR $08
else
cells[next,y].input := cells[next,y].input AND $07;
// left
next := wrap(x-1,width);
if (output AND 8) <> 0 then
cells[next,y].input := cells[next,y].input OR $02
else
cells[next,y].input := cells[next,y].input AND $0d;
// down
next := wrap(y+1,height);
if (output AND 4) <> 0 then
cells[x,next].input := cells[x,next].input OR $01
else
cells[x,next].input := cells[x,next].input AND $0e;
// up
next := wrap(y-1,height);
if (output AND 1) <> 0 then
cells[x,next].input := cells[x,next].input OR $04
else
cells[x,next].input := cells[x,next].input AND $0b;
end;
end; // DoPhaseA
procedure TBitGrid.DoPhaseB;
var
x,y,next : integer;
begin
// do phase B, only odd cells
for y := 0 to Height-1 do
for x := 0 to Width-1 do
if Odd(x+y) then
with cells[x,y] do
begin
// compute the new output
output:= lookup SHR (input*4) AND $0f;
// distribute the output to the inputs of neigbors
// right
next := wrap((x+1),width);
if (output AND 2) <> 0 then
cells[next,y].input := cells[next,y].input OR $08
else
cells[next,y].input := cells[next,y].input AND $07;
// left
next := wrap(x-1,width);
if (output AND 8) <> 0 then
cells[next,y].input := cells[next,y].input OR $02
else
cells[next,y].input := cells[next,y].input AND $0d;
// down
next := wrap(y+1,height);
if (output AND 4) <> 0 then
cells[x,next].input := cells[x,next].input OR $01
else
cells[x,next].input := cells[x,next].input AND $0e;
// up
next := wrap(y-1,height);
if (output AND 1) <> 0 then
cells[x,next].input := cells[x,next].input OR $04
else
cells[x,next].input := cells[x,next].input AND $0b;
end;
Inc(CycleCount);
end; // DoPhgaseB
procedure TBitGrid.PutBit(X,Y : Integer; Bit : Boolean);
begin
// put the input to all inputs, for now
if Bit then
cells[x,y].input:= $0f
else
cells[x,y].input:= $00;
end;
end.