/
erdsir_wormversion.c
2123 lines (1814 loc) · 52.5 KB
/
erdsir_wormversion.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
- THE PLAGUE INTERPRETER
... to bring back to all of us a natural, occult equivalent of the
dogma we no longer believe. [Antonin Artaud]
Returning the body, electronics, and dystopic code to the earth,
revived and decoded years later as "yersinia pestis".
///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// SIR 2016 re-issue with some tweaks and new Turing Machine implementation.
////////////////////////////////////////////////////////////////////////////
LAYOUT:
0 // PLAGUE/CPU ADC:X+2
0 // INSTR ADC:X+1
0 // SPEED ADC: X+0
0 0 0 // CPU/SPEED/INST
*/
#define F_CPU 16000000UL
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#ifdef AVR_IS
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#define randi() (adcread(1)+adcread(5)) // INSTRUCTION
#else
#include <time.h>
#define randi() (rand()%255)
#endif
typedef int u16;
typedef unsigned char u8;
#define trand ((rand()%255)+randi()+(adcread(7)&15))
//#define randi() (incincom())
#define CELLLEN 16
#define floor(x) ((int)(x))
#define MAX_SAM 255
#define MAX_SAM2 127
#define BV(bit) (1<<(bit)) // Byte Value => converts bit into a byte value. One at bit location.
#define cbi(reg, bit) reg &= ~(BV(bit)) // Clears the corresponding bit in register reg
#define sbi(reg, bit) reg |= (BV(bit)) // Sets the corresponding bit in register reg
#define HEX__(n) 0x##n##UL
#define recovered 129
#define dead 255
#define susceptible 0
signed char insdir,dir;
unsigned char modifier,flaggy;
unsigned char xxx[MAX_SAM+12]; // for tm this is our tape
unsigned char desc[64]; // read into desc from randi and check for bounds
//unsigned char incom[255];
signed char m_stack_pos;
u8 m_stack[16];
u8 m_reg8bit1,m_reg8bit2;
#define NOP 0
#define ORG 1
#define EQU 2
#define JMP 3
#define JMPZ 4
#define PSHL 5
#define PSH 6
#define PSHI 7
#define POP 8
#define POPI 9
#define ADD 10
#define SUB 11
#define INC 12
#define DEC 13
#define AND 14
#define OR 15
#define XOR 16
#define NOT 17
#define ROR 18
#define ROL 19
#define PIP 20
#define PDP 21
#define DUP 22
#define SAY 23
#define INP 24
//unsigned char countt=0;
#ifdef AVR_IS
unsigned char adcread(unsigned char channel){
unsigned char result, high;
ADMUX &= 0xF8; // clear existing channel selection
ADMUX |=(channel & 0x07); // set channel/pin
ADCSRA |= (1 << ADSC); // Start A2D Conversions
loop_until_bit_is_set(ADCSRA, ADIF); /* Wait for ADIF, will happen soon */
result=ADCH;
return result;
}
#else
unsigned char adcread(unsigned char channel){
unsigned char result, high;
if (channel==0 ||channel==1 || channel==7) result=rand()%255;
else result=10;
return result;
}
#endif
unsigned char oldone;
/*ISR(TIMER2_COMPA_vect){//timer2 interrupt 200 Hz
// incom[countt]=(adcread(0)+adcread(5));
// countt++;
// divider
static unsigned char speedy=0;
if (++speedy>=speed) { //once per sec test was passed
speedy=0;
oldone++;
if (flaggy==1) {
flaggy=0;
}
else modifier=xxx[oldone];
/// invert or?
OCR0A=255-modifier;
// OCR0A=255-(xxx[oldone]);
}
}*/
/*inline u8 incincom(){
static unsigned char counttt=0;
unsigned char returnee=incom[counttt];
counttt++;
}*/
u8 thread_stack_count(u8 c) {
return (c-1)<=m_stack_pos;
}
void thread_push(u8 data) {
if (m_stack_pos<14 && m_stack_pos>-1) // STACK_SIZEE-2
{
m_stack[++m_stack_pos]=data;
}
}
u8 thread_pop() {
if (m_stack_pos>=0 && m_stack_pos<16)
{
u8 ret=m_stack[m_stack_pos];
m_stack_pos--;
return ret;
// return 0;
}
// printf("errorr\n");
return 0;
}
u8 thread_top() {
if (m_stack_pos>=0 && m_stack_pos<16)
{
return m_stack[m_stack_pos];
}
return 0;
}
inline u8 machine_p88k(u8 addr) {
// return m_heap[addr%HEAP_SIZE];
return xxx[addr];
}
inline void machine_poke(u8 addr, u8 data) {
// m_heap[addr%HEAP_SIZE]=data;
xxx[addr]=data;
// printf("%d\n",&m_memory[addr]);
}
u8 antrulee(u8 dir,u8 inst, u8 rule){
u8 index;
// process state from rule
// rule in binary
// inst is index into binary array - so inst must be lower than 0->7 - it is
index=(rule>>inst)&1;
if (index==0) return dir-1;
else return dir+1;
}
// above ported from cpuintrev
#ifdef AVR_IS
void adc_init(void)
{
cbi(ADMUX, REFS1);
sbi(ADMUX, REFS0); // AVCC
sbi(ADMUX, ADLAR); //8 bits
sbi(ADCSRA, ADPS2);
// sbi(ADCSRA, ADPS1); // change speed here! now div by 64
// sbi(ADCSRA, ADPS0); // change speed here!
sbi(ADCSRA, ADEN);
// sbi(ADCSRA, ADIE); // not needed
DDRC = 0x00;
PORTC = 0x00;
}
#endif
void initcell(unsigned char* cells){
unsigned char x;
for (x=0;x<MAX_SAM;x++){
// cells[x]=randi();
cells[x]=((rand()%255)+randi()+(adcread(7)&15)); // added bracket for AND!
// printf("%d %d\n", cells[x],xxx[x]);
}
}
void mutate(unsigned char* cells){
unsigned char x,y;
for (y=0;y<cells[0];y++){// try this?
x=randi();
// printf("plague %d %d ", x,cells[0]);
cells[x]^=(trand&0x0f);
// cells[x]^=(trand);
// printf("%d xxx:%d\n",y, cells[x]);
}
}
void inc(unsigned char* cells){
unsigned char x,y;
x=randi();
for (y=0;y<cells[0];y++){
cells[x++]++;
}
}
void shift(unsigned char* cells){
unsigned char x,y;
x=randi();
for (y=0;y<cells[0];y++){
cells[x++]=cells[x++];
}
}
void chunk(unsigned char* cells){
unsigned char x,y,tmp;
x=randi();
for (y=0;y<cells[0];y++){
tmp=cells[1] + ++x;
cells[x]=cells[tmp];
}
}
u8 headcount(u8 *datagenbuffer,u8 place){
u8 counter=0;
u8 n=randi();
place-=n-1;
if (datagenbuffer[place]==1) counter++;
place+=1;
if (datagenbuffer[place]==1) counter++;
place+=1;
if (datagenbuffer[place]==1) counter++;
place+=n-2;
if (datagenbuffer[place]==1) counter++;
place+=2;
if (datagenbuffer[place]==1) counter++;
place+=n-2; // was -1
if (datagenbuffer[place]==1) counter++;
place+=1;
if (datagenbuffer[place]==1) counter++;
place+=1;
if (datagenbuffer[place]==1) counter++;
if (counter<3 && counter!=0) return 1;
else return 0;
}
void runwire(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
n=randi();
if (n==0) n=1;
for (x=0;x<MAX_SAM2; x++){
if (cells[x]==0) newcells[x]=0; //blank
else if (cells[x]==1) newcells[x]=255; // head to tail
else if (cells[x]==255) newcells[x]=129; // tail to copper
else if (cells[x]>128 && headcount(cells,x)==1) newcells[x]=1;
else newcells[x]=cells[x];
// printf("%c", cells[x]);
}
// swapping
flag^=0x01;
}
void runhodgenet(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
n=randi();
u8 sum, numill, numinf,y;
u8 k1=cellies[1];
u8 k2=cellies[2];
if (k1==0) k1=1;
if (k2==0) k2=1;
for (x=1;x<MAX_SAM2; x++){
sum=0; numill=0; numinf=0;
place=x-cellies[4]-1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=cellies[4]-2;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=cellies[4]-2;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
place+=1;
place=place&127;
if (cells[place]==cellies[0]) numill++; if (cells[place]>0) numinf++;
sum+=cells[place];
// y=x+32768; UPDATE!
if(cells[x] == 0)
newcells[x] = floor(numinf / k1) + floor(numill / k2);
else if(cells[x] < cellies[0])
{ if ((numinf+1)!=0) newcells[x] = floor(sum / (numinf + 1)) + cellies[3];}
else
newcells[x] = 0;
if(newcells[x] > cellies[0]) newcells[x] = cellies[0];
// printf("numinf%d", numinf);
}
// swapping
flag^=0x01;
}
void runhodge(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
n=randi();
u8 sum, numill, numinf,y;
u8 k1=cellies[1];
u8 k2=cellies[2];
if (k1==0) k1=1;
if (k2==0) k2=1;
for (x=0;x<MAX_SAM2; x++){
sum=0; numill=0; numinf=0;
y=x-n-1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=cellies[4]-2;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=cellies[4]-2;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
y+=1;
sum+=cellies[y];
if (cellies[y]==(cellies[0]-1)) numill++; else if (cellies[y]>0) numinf++;
// y=x+wrap; UPDATE!
if(cells[x] == 0)
newcells[x] = floor(numinf / k1) + floor(numill / k2);
else if(cells[x] < cellies[0] - 1)
newcells[x] = floor(sum / (numinf + 1)) + cellies[3];
else
newcells[x] = 0;
if(newcells[x] > cellies[0] - 1)
newcells[x] = cellies[0] - 1;
// printf("%c", cells[x]);
}
// swapping
flag^=0x01;
}
void runfire(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
u8 sum;
u8 y;
u8 probB=cellies[0]/32;
u8 probI=cellies[1]/10;
for (x=0;x<MAX_SAM2; x++){
n=randi();
y=x-n-1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=n-2;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=n-2;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
// sum=(datagenbuffer[x-1]&1)+(datagenbuffer[x+1]&1)+(datagenbuffer[x-n]&1)+(datagenbuffer[x+n]&1)+(datagenbuffer[x-n-1]&1)+(datagenbuffer[x-n+1]&1)+(datagenbuffer[x+n-1]&1)+(datagenbuffer[x+n+1]&1);
// y=x+32768; // now update
if (cells[x]==0 || cells[x]==254) newcells[x]=cells[x]; //empty or burnt
// now deal with vegetation(bit1 empty) and burning(&1)
else if ((cells[x]&1)==0 && randi()<=(sum*probI)) newcells[x]=cells[x]|1; //veg->burning
else if ((cells[x]&1)==1 && randi()<=probB) newcells[x]=254; // burning->burnt
else newcells[x]=cells[x];
// printf("%c", cells[x]);
}
// swapping
flag^=0x01;
}
void runkrum(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
n=randi()&127;
if (n==0) n=1;
for (x=0;x<MAX_SAM2; x++){
place=x-cellies[0];
if (cells[place]==(cells[x]+1)%n) newcells[x]=cells[place];
place+=cellies[0]-1;
if (cells[place]==(cells[x]+1)%n) newcells[x]=cells[place];
place+=2;
if (cells[place]==(cells[x]+1)%n) newcells[x]=cells[place];
place+=cellies[0]-1;
if (cells[place]==(cells[x]+1)%n) newcells[x]=cells[place];
// printf("%c", cells[x]);
}
// swapping
flag^=0x01;
}
void runlife(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n,sum,y;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
n=randi();
for (x=0;x<MAX_SAM2; x++){
sum=0;
y=x-n-1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=n-2;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=n-2;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
y+=1;
sum+=cellies[y]&1;
/* if (sum==3 || (sum+(datagenbuffer[x]&0x01)==3)) newdatagenbuffer[x]=255; ///
else newdatagenbuffer[x]=0;*/
// y=x+32768; update
if ((cells[x]&1)==1 && sum<2) newcells[x]=0;
else if ((cells[x]&1)==1 && sum>3) newcells[x]=0;
else if ((cells[x]&1)==0 && sum==3) newcells[x]=255;
else newcells[x]=cells[x];
// printf("%c", cells[x]);
}
// swapping
flag^=0x01;
}
void runcel(unsigned char* cellies){
// static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n,sum,state,y;
/* if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
} */
n=randi();
for (x=0;x<MAX_SAM; x++){
state = 0;
if (cellies[(x +1+ (n))&255]>128)
state |= 0x4;
if (cellies[(x+n)&255]>128)
state |= 0x2;
y=x-1;
if (cellies[(y + n)&255]>128)
state |= 0x1;
y=(x+(n*2));///&255;// next row but one!
if ((cellies[0] >> state) & 1){
cellies[y] = 0;
}
else{
cellies[y] = 255;
}
// printf("%c", cellies[x]);
}
// swapping
// flag^=0x01;
}
//u8 table[21];
/*void inittable(u8 r, u8 k, int rule){
u8 max; int z; u8 summ;
// free(table);
max = (k-1)*((r*2)+1);
// // printf("ttt %d\n",max);
// table= malloc(max+1);
for (z=max;z>=0;z--){
summ=0;
while ((rule-pow(k,z))>=0) {
summ++;
rule=rule-pow(k,z);
}
if (summ>=1) {
table[z]=summ;
}
else table[z]=0;
// printf("%d\n",table[z]);
}
}*/
/*void runcel1d(unsigned char* cellies){
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char place,x,n,sum,y,z,zz;
n=randi();
for (x=0;x<MAX_SAM; x++){
sum=0;
// sum of datagenbuffer in radius - not looping!
for (z=-3;z<3;z++){ // hardcode sans radius...
zz=x+z;
if (zz>=n) zz=zz-n;
// if (zz<0) zz=n+zz;
sum+=(cellies[zz])&3; // crash here? and it did/// not now !
}
y=x+n;
cellies[y]=table[sum]<<5;
// printf("%c", cellies[x]);
}
}*/
void hodge(unsigned char* cellies){
int sum=0, numill=0, numinf=0;
unsigned char q,k1,k2,g;
static unsigned char x=CELLLEN+1;
static unsigned char flag=0;
unsigned char *newcells, *cells;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
q=cells[0];k1=cells[1];k2=cells[2];g=cells[3];
if (k1==0) k1=1;
if (k2==0) k2=1;
sum=cells[x]+cells[x-1]+cells[x+1]+cells[x-CELLLEN]+cells[x+CELLLEN]+cells[x-CELLLEN-1]+cells[x-CELLLEN+1]+cells[x+CELLLEN-1]+cells[x+CELLLEN+1];
if (cells[x-1]==(q-1)) numill++; else if (cells[x-1]>0) numinf++;
if (cells[x+1]==(q-1)) numill++; else if (cells[x+1]>0) numinf++;
if (cells[x-CELLLEN]==(q-1)) numill++; else if (cells[x-CELLLEN]>0) numinf++;
if (cells[x+CELLLEN]==(q-1)) numill++; else if (cells[x+CELLLEN]>0) numinf++;
if (cells[x-CELLLEN-1]==q) numill++; else if (cells[x-CELLLEN-1]>0) numinf++;
if (cells[x-CELLLEN+1]==q) numill++; else if (cells[x-CELLLEN+1]>0) numinf++;
if (cells[x+CELLLEN-1]==q) numill++; else if (cells[x+CELLLEN-1]>0) numinf++;
if (cells[x+CELLLEN+1]==q) numill++; else if (cells[x+CELLLEN+1]>0) numinf++;
if(cells[x] == 0)
newcells[x&127] = floor(numinf / k1) + floor(numill / k2);
else if(cells[x] < q - 1)
newcells[x&127] = floor(sum / (numinf + 1)) + g;
else
newcells[x&127] = 0;
if(newcells[x&127] > q - 1)
newcells[x&127] = q - 1;
x++;
if (x>((MAX_SAM2)-CELLLEN-1)) {
x=CELLLEN+1;
flag^=0x01;
}
}
void cel(unsigned char* cells){
static unsigned char l=0; unsigned char cell, state, res;
unsigned char rule=cells[0];
unsigned char tmp;
res=0;
l++;
l&=15;
for (cell = 1; cell < CELLLEN; cell++){
state = 0;
tmp=cell + 1+ (l*CELLLEN); // changed
if (cells[tmp]>128) // max 256
state |= 0x4;
if (cells[cell+(CELLLEN*l)]>128)
state |= 0x2;
if (cells[cell - 1 +(CELLLEN*l)]>128)
state |= 0x1;
if ((rule >> state) & 1){
res += 1;
tmp=cell+(((l+1)%CELLLEN)*CELLLEN);
cells[tmp] = 255;
}
else{
tmp=cell+(((l+1)%CELLLEN)*CELLLEN);
cells[tmp] = 0;
}
}
}
void SIR(unsigned char* cellies){
unsigned char cell,x,sum=0,tmp,tmpp,tmppp,tmpppp;
static unsigned char flag=0;
unsigned char *newcells, *cells;
unsigned char kk=cellies[0], p=cellies[1];
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
// printf("SWOP %d %d\n", cells[11], newcells[11]);
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
// printf("UN_SWOP %d %d\n", cells[11], newcells[11]);
}
for (x=0;x<127;x++){
cell=cells[x];
newcells[x]=cell; // swop
if (cell >= kk) newcells[x] += recovered;
else if ((cell>0 && cell<kk)){
newcells[x]++;
}
else if (cell == susceptible) {
tmp=(x-16)&127;
tmpp=(x+16)&127;
tmppp=(x-1)&127;
tmpppp=(x+1)&127;
if ( (cells[tmp]>0 && cells[tmp]<kk) ||
(cells[tmpp]>0 && cells[tmpp]<kk) ||
(cells[tmppp]>0 && cells[tmppp]<kk) ||
(cells[tmpppp]>0 && cells[tmpppp]<kk))
{
if (randi()%10 < p) newcells[x] = 1;
}
}
}
/* for (x=CELLLEN;x<((MAX_SAM2)-CELLLEN);x++){
cell = cells[x];
newcells[x]=cell;
// newcells[x]=rand()%255;
if (cell >= kk) newcells[x] = recovered;
else if ((cell>0 && cell<kk)){
newcells[x]++;
}
else if (cell == susceptible) {
if ( (cells[x-CELLLEN]>0 && cells[x-CELLLEN]<kk) ||
(cells[x+CELLLEN]>0 && cells[x+CELLLEN]<kk) ||
(cells[x-1]>0 && cells[x-1]<kk) ||
(cells[x+1]>0 && cells[x+1]<kk))
{
if (randi()%10 < p) newcells[x] = 1;
}
}
}*/
flag^=0x01;
}
void life(unsigned char* cellies){
unsigned char x, sum;
static unsigned char flag=0;
unsigned char *newcells, *cells;
if (flag==0) {
cells=cellies; newcells=&cellies[MAX_SAM2];
}
else {
cells=&cellies[MAX_SAM2]; newcells=cellies;
}
for (x=CELLLEN+1;x<((MAX_SAM2)-CELLLEN-1);x++){
sum=cells[x]%2+cells[x-1]%2+cells[x+1]%2+cells[x-CELLLEN]%2+cells[x+CELLLEN]%2+cells[x-CELLLEN-1]%2+cells[x-CELLLEN+1]%2+cells[x+CELLLEN-1]%2+cells[x+CELLLEN+1]%2;
sum=sum-cells[x]%2;
if (sum==3 || (sum+(cells[x]%2)==3)) newcells[x]=255;
else newcells[x]=0;
}
// swapping
flag^=0x01;
}
// instructions for plague CPUS!
unsigned char omem;
/* BIOTA: two dimensional memory map - add/replace with wormings */
unsigned char btdir,dcdir;
unsigned char btempty(unsigned char* cells, unsigned char IP){
// turn around
if (btdir==0) btdir=1;
else if (btdir==1) btdir=0;
else if (btdir==2) btdir=3;
else if (btdir==3) btdir=2;
return IP;
}
unsigned char btoutp(unsigned char* cells, unsigned char IP){
modifier=cells[omem];
flaggy=1;
return IP;
}
unsigned char btstraight(unsigned char* cells, unsigned char IP){
if (dcdir==0) omem+=1;
else if (dcdir==1) omem-=1;
else if (dcdir==2) omem+=16;
else if (dcdir==3) omem-=16;
if (cells[omem]==0)
{ // change dir
if (btdir==0) btdir=1;
else if (btdir==1) btdir=0;
else if (btdir==2) btdir=3;
else if (btdir==3) btdir=2;
}
return IP;
}
unsigned char btbackup(unsigned char* cells, unsigned char IP){
if (dcdir==0) omem-=1;
else if (dcdir==1) omem+=1;
else if (dcdir==2) omem-=16;
else if (dcdir==3) omem+=16;
if (cells[omem]==0)
{
if (btdir==0) btdir=1;
else if (btdir==1) btdir=0;
else if (btdir==2) btdir=3;
else if (btdir==3) btdir=2;
}
return IP;
}
unsigned char btturn(unsigned char* cells, unsigned char IP){
if (dcdir==0) omem+=16;
else if (dcdir==1) omem-=16;
else if (dcdir==2) omem+=1;
else if (dcdir==3) omem-=1;
return IP;
}
unsigned char btunturn(unsigned char* cells, unsigned char IP){
if (dcdir==0) omem-=16;
else if (dcdir==1) omem+=16;
else if (dcdir==2) omem-=1;
else if (dcdir==3) omem+=1;
return IP;
}
unsigned char btg(unsigned char* cells, unsigned char IP){
unsigned char x=0;
while (x<20 && cells[omem]!=0){
if (dcdir==0) omem+=1;
else if (dcdir==1) omem-=1;
else if (dcdir==2) omem+=16;
else if (dcdir==3) omem-=16;
x++;
}
return IP;
}
unsigned char btclear(unsigned char* cells, unsigned char IP){
if (cells[omem]==0){
if (btdir==0) btdir=1;
else if (btdir==1) btdir=0;
else if (btdir==2) btdir=3;
else if (btdir==3) btdir=2;
}
else cells[omem]=0;
return IP;
}