-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1402 lines (1176 loc) · 36.2 KB
/
main.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
/* FPGA address macros */
#define BOARD "DE1-SoC"
/* Memory */
#define DDR_BASE 0x00000000
#define DDR_END 0x3FFFFFFF
#define A9_ONCHIP_BASE 0xFFFF0000
#define A9_ONCHIP_END 0xFFFFFFFF
#define SDRAM_BASE 0xC0000000
#define SDRAM_END 0xC3FFFFFF
#define FPGA_ONCHIP_BASE 0xC8000000
#define FPGA_ONCHIP_END 0xC803FFFF
#define FPGA_CHAR_BASE 0xC9000000
#define FPGA_CHAR_END 0xC9001FFF
/* Cyclone V FPGA devices */
#define LEDR_BASE 0xFF200000
#define HEX3_HEX0_BASE 0xFF200020
#define HEX5_HEX4_BASE 0xFF200030
#define SW_BASE 0xFF200040
#define KEY_BASE 0xFF200050
#define JP1_BASE 0xFF200060
#define JP2_BASE 0xFF200070
#define PS2_BASE 0xFF200100
#define PS2_DUAL_BASE 0xFF200108
#define JTAG_UART_BASE 0xFF201000
#define JTAG_UART_2_BASE 0xFF201008
#define IrDA_BASE 0xFF201020
#define TIMER_BASE 0xFF202000
#define AV_CONFIG_BASE 0xFF203000
#define PIXEL_BUF_CTRL_BASE 0xFF203020
#define CHAR_BUF_CTRL_BASE 0xFF203030
#define AUDIO_BASE 0xFF203040
#define VIDEO_IN_BASE 0xFF203060
#define ADC_BASE 0xFF204000
/* Cyclone V HPS devices */
#define HPS_GPIO1_BASE 0xFF709000
#define HPS_TIMER0_BASE 0xFFC08000
#define HPS_TIMER1_BASE 0xFFC09000
#define HPS_TIMER2_BASE 0xFFD00000
#define HPS_TIMER3_BASE 0xFFD01000
#define FPGA_BRIDGE 0xFFD0501C
/* ARM A9 MPCORE devices */
#define PERIPH_BASE 0xFFFEC000 // base address of peripheral devices
#define MPCORE_PRIV_TIMER 0xFFFEC600 // PERIPH_BASE + 0x0600
/* Interrupt controller (GIC) CPU interface(s) */
#define MPCORE_GIC_CPUIF 0xFFFEC100 // PERIPH_BASE + 0x100
#define ICCICR 0x00 // offset to CPU interface control reg
#define ICCPMR 0x04 // offset to interrupt priority mask reg
#define ICCIAR 0x0C // offset to interrupt acknowledge reg
#define ICCEOIR 0x10 // offset to end of interrupt reg
/* Interrupt controller (GIC) distributor interface(s) */
#define MPCORE_GIC_DIST 0xFFFED000 // PERIPH_BASE + 0x1000
#define ICDDCR 0x00 // offset to distributor control reg
#define ICDISER 0x100 // offset to interrupt set-enable regs
#define ICDICER 0x180 // offset to interrupt clear-enable regs
#define ICDIPTR 0x800 // offset to interrupt processor targets regs
#define ICDICFR 0xC00 // offset to interrupt configuration regs
/* processor mode constants */
#define USER_MODE 0b10000
#define FIQ_MODE 0b10001
#define IRQ_MODE 0b10010
#define SVC_MODE 0b10011
#define ABORT_MODE 0b10111
#define UNDEF_MODE 0b11011
#define SYS_MODE 0b11111
#define KEYS_IRQ 73
#define PS2_IRQ 79
#define INT_ENABLE 0b01000000
#define INT_DISABLE 0b11000000
#define ENABLE 0x1
/* default VGA colors */
#define WHITE 0xFFFF
#define YELLOW 0xFFE0
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x001F
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define GREY 0xC618
#define PINK 0xFC18
#define ORANGE 0xFC00
#define BLACK 0x0
/* Screen and character buffer resolution */
#define RESOLUTION_X 320
#define RESOLUTION_Y 240
#define CHAR_MAX_X 80
#define CHAR_MAX_Y 60
/* Mouse Input Settings */
#define ACCELERATION 1
/* Tile Active */
#define DEAD 0
#define ALIVE 1
/* define absolute value function */
#define ABS(x) (((x) > 0) ? (x) : -(x))
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <float.h>
#include <string.h>
volatile int pixel_buffer_start; // global variable
/* LinkedList definition start */
typedef struct node{
int x;
int y;
bool isTile;
struct node* next;
}Node;
typedef struct linkedlist{
Node* head;
}LinkedList;
Node* createNode(int x,int y,bool isTile){
Node* temp = (Node*)malloc(sizeof(Node));
temp->x = x;
temp->y = y;
temp->next = NULL;
temp->isTile = isTile;
if(temp==NULL){
printf("Error: Could not allocate memory for node");
}
return temp;
}
LinkedList* init(){
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
list->head = NULL;
if(list==NULL){
printf("Error: Could not allocate memory for node");
}
return list;
}
bool isEmpty(LinkedList* list){
return (list->head==NULL);
}
void insertFront(LinkedList* list,int x,int y,bool isTile){
Node* temp = createNode(x,y,isTile);
temp->next = list->head;
list->head = temp;
}
void insertTail(LinkedList* list,int x,int y,bool isTile){
if(isEmpty(list)){
list->head=createNode(x,y,isTile);
return;
}
Node* temp = list->head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = createNode(x,y,isTile);
}
void deleteFront(LinkedList* list){
Node* temp = list->head;
list->head = list->head->next;
free(temp);
}
bool deleteTail(LinkedList* list){
if(isEmpty(list)){
return false;
}else if(list->head->next==NULL){
deleteFront(list);
return true;
}
Node* temp = list->head;
while(temp->next->next!=NULL){
temp=temp->next;
}
free(temp->next);
temp->next = NULL;
return true;
}
void deleteList(LinkedList* list){
while(list->head!=NULL){
deleteFront(list);
}
}
void printList(LinkedList* list){
if(isEmpty(list)){
printf("Error: List is empty.\n");
return;
}
Node* temp = list->head;
do{
printf("%d %d\n",temp->x,temp->y);
temp=temp->next;
}while(temp!=NULL);
printf("\n");
}
/* LinkedList definition end */
/* GLOBAL VARIABLES */
volatile int * pixel_ctrl_ptr = (int *)0xFF203020;
// global drawn pixels linked lists
LinkedList* pixel_list;
LinkedList* front_list;
LinkedList* back_list;
// game board
bool game_board[RESOLUTION_X][RESOLUTION_Y];
bool prev_board[RESOLUTION_X][RESOLUTION_Y];
int color_board[RESOLUTION_X][RESOLUTION_Y];
// global colour values
short int bg_color;
short int tile_color;
// game states
bool isPaused;
// linked tile size
int l_tile_size;
// mouse position and size variables
int mouse_x,mouse_y;
int mouse_width,mouse_height;
short int cursor_color;
// screen
volatile int screen;
enum Screens{MAIN_MENU, RANDOM, USER_DRAWING, PRESETS};
/* GLOBAL VARIABLES END */
/* GAME FUNCTIONS */
// program screens
void main_menu();
void random_screen();
void user_drawing();
void presets();
//void logic_gates();
// set the starting board state
void initialize_board();
// draw the game board (a rectangle in the screen)
void draw_board(int x0, int x1, int y0, int y1, int size);
// initialize the board randomly with prop % chance that a cell is alive at the start
void random_initialization(int x0, int x1, int y0, int y1, float prop);
// update and draw the board (a rectangle in the screen)
void update_board_state(int x0, int x1, int y0, int y1, int size);
// shapes and creatures:
void ship(int centre_x, int centre_y);
void block(int left_x, int top_y);
void tub(int centre_x, int centre_y);
void toad(int left_x, int top_y);
void blinker(int centre_x, int centre_y);
void glider(int left_x, int top_y);
void pulsar(int centre_x, int centre_y);
void draw_ECE243(int left_x, int top_y);
void glider_eater(int left_x, int top_y, bool hor_not_vert);
void glider_gun(int left_x, int top_y, bool hor_not_vert);
void and_gate(int left_x, int top_y);
void or_gate(int left_x, int top_y);
void not_gate(int left_x, int top_y);
/* GAME FUNCTIONS END */
/* HELPER FUNCTIONS */
// set up IRQ stack pointer
void set_up_IRQ();
// configure GIC
void config_GIC();
// configure keys
void config_KEYs();
//void config_SWs();
// interrupt service routine for KEYs
void KEY_ISR();
// configure mouse input (PS2 port)
void config_ps2();
// interrupt service routine for mouse input (PS2 port)
void MOUSE_ISR();
// interrupt mouse coordinates
void initialize_mouse_coords();
// parse left mouse button clicked procedures
void parse_lmb_click();
// parse right mouse button clicked procedures
void parse_rmb_click();
// check if mouse is in bounds (x1<x2,y1<y2)
bool mouse_bounds(int x1,int y1,int x2,int y2);
// update mouse coords and keep mouse position within screen bounds
void update_mouse_coords(int disp_x,int disp_y);
//void SW_ISR();
// enable interrupts after setting up devices
void enable_interrupts();
// plot a pixel with given colour at coordinates
void draw_pixel(int x, int y, short int line_color);
void draw_tile(int x, int y, int size, short int line_color);
void draw_tile_clear(int x,int y,int size,short int line_color);
// draw a string of text on character buffer
void draw_text(int x, int y, char * text_ptr);
// draw the cursor on the screen
void draw_cursor();
// wait for vertical synchronization
void wait_for_vsync();
// clear the screen on startup (slow clear)
void initial_clear();
// clears the character buffer
void initial_clear_chars();
// clear the screen between frames (fast clear);
void clear_screen(LinkedList* list,int size);
// clear everything
void clear(int tile_size);
// check if coordinates is on game board
bool check_bounds(int x,int y,int size);
/* HELPER FUNCTIONS END */
int main(void)
{
/* Initialize variables */
bg_color = BLACK;
tile_color = WHITE;
cursor_color = GREEN;
isPaused = true;
pixel_list = init();
back_list = init();
front_list = init();
//srand(time(NULL));
set_up_IRQ();
config_GIC();
config_KEYs();
config_ps2();
initialize_mouse_coords();
enable_interrupts();
initial_clear_chars();
/* Set up and clear both front and back buffers */
// set front pixel buffer to start of FPGA On-chip memory
*(pixel_ctrl_ptr + 1) = 0xC8000000; // first store the address in the
// back buffer
// now, swap the front/back buffers, to set the front buffer location
wait_for_vsync();
// initialize a pointer to the pixel buffer, used by drawing functions
pixel_buffer_start = *pixel_ctrl_ptr;
initial_clear();
// pixel_buffer_start points to the pixel buffer
// set back pixel buffer to start of SDRAM memory
*(pixel_ctrl_ptr + 1) = 0xC0000000;
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // we draw on the back buffer
initial_clear();
screen = MAIN_MENU;
main_menu();
}
void main_menu(){
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear_chars();
const int tile_size = 5; // 5x5
const int board_x = RESOLUTION_X/tile_size;
const int board_y = RESOLUTION_Y/tile_size;
isPaused = true;
// Title and menu options
char* str = "Game of Life\0";
draw_text((80-strlen(str))/2, 1,str);
str = "Welcome to Conway's Game of Life.\0";
draw_text((80-strlen(str))/2, 4, str);
str = "Press pushbutton 0 to start simulation.\0";
draw_text(4, 6, str);
str = "Use switches or the mouse to select a menu option.\0";
draw_text(4, 8, str);
char* credits = "Stephen and Yvonne, 2021\0";
draw_text(55,58,credits);
str = "1. Random\0";
draw_text(30, 12, str);
str = "2. Draw your own\0";
draw_text(30, 16, str);
str = "3. Presets\0";
draw_text(30, 20, str);
draw_ECE243(11, 30);
// simulation loop
int total_iterations = 0;
while (1)
{
if (screen != MAIN_MENU) break;
if (total_iterations > 0 && isPaused) // display the initial config
continue;
// start clearing previously drawn pixels
clear(tile_size);
// finish clearing previously drawn pixels
draw_cursor();
// draw, then update the game board
update_board_state(0,board_x,0,board_y, tile_size);
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
total_iterations++;
}
if (screen == RANDOM)
random_screen();
else if (screen == USER_DRAWING)
user_drawing();
else if (screen == PRESETS)
presets();
else
;
}
void random_screen(){
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear_chars();
const int tile_size = 6;
const int board_x0 = 15/tile_size;
const int board_y0 = 40/tile_size;
const int board_x1 = (RESOLUTION_X-15)/tile_size;
const int board_y1 = (RESOLUTION_Y-25)/tile_size;
isPaused = true;
// Title and instructions
char* str = "Game of Life Random Generator\0";
draw_text((80-strlen(str))/2, 1,str);
str = "Enjoy a simulation of Life with randomly generated tiles.\0";
draw_text(4, 4, str);
str = "Press pushbutton 0 to start simulation.\0";
draw_text(4, 6, str);
str = "Back to Main Menu<<\0"; // clicky stuff?
draw_text(2, 57, str);
char* credits = "Stephen and Yvonne, 2021\0";
draw_text(55,58,credits);
random_initialization(board_x0, board_x1, board_y0, board_y1, 0.9);
// simulation loop
int total_iterations = 0;
while (1)
{
if (screen != RANDOM) break;
if (total_iterations > 0 && isPaused) // display the initial config
continue;
// start clearing previously drawn pixels
clear(tile_size);
// finish clearing previously drawn pixels
// draw, then update the game board
update_board_state(board_x0,board_x1,board_y0,board_y1, tile_size);
draw_cursor();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
total_iterations++;
}
if (screen == USER_DRAWING)
user_drawing();
else if (screen == PRESETS)
presets();
else if (screen == MAIN_MENU)
main_menu();
else
;
}
void user_drawing(){
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear_chars();
const int tile_size = 5; // 5x5
const int board_x = RESOLUTION_X/tile_size;
const int board_y = RESOLUTION_Y/tile_size;
isPaused = true;
l_tile_size = tile_size;
// Title and instructions
char* str = "Game of Life - Draw your own\0";
draw_text((80-strlen(str))/2, 1,str);
str = "Drag the mouse on the screen to draw an initial configuration.\0";
draw_text(4, 4, str);
str = "Press pushbutton 0 to start simulation.\0";
draw_text(4, 6, str);
str = "Back to Main Menu<<\0"; // clicky stuff?
draw_text(2, 57, str);
char* credits = "Stephen and Yvonne, 2021\0";
draw_text(55,58,credits);
// simulation loop
int total_iterations = 0;
while (1)
{
if (screen != USER_DRAWING) break;
// start clearing previously drawn pixels
clear(tile_size);
// finish clearing previously drawn pixels
// draw, then update the game board
if(!isPaused){
update_board_state(0,board_x,0,board_y,tile_size);
}else{
draw_board(0,board_x,0,board_y,tile_size);
}
draw_cursor();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
total_iterations++;
}
// reset linked tile size
l_tile_size = 0;
if (screen == RANDOM)
random_screen();
else if (screen == PRESETS)
presets();
else if (screen == MAIN_MENU)
main_menu();
else
;
}
void presets(){
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear();
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
initial_clear_chars();
const int tile_size = 2; // 2x2
const int board_x = RESOLUTION_X/tile_size;
const int board_y = RESOLUTION_Y/tile_size;
isPaused = true;
// Title and instructions
char* str = "Game of Life Presets\0";
draw_text((80-strlen(str))/2, 1,str);
str = "Welcome to the gallery of Life creatures.\0";
draw_text(4, 4, str);
str = "Press pushbutton 0 to start simulation.\0";
draw_text(4, 6, str);
str = "Back to Main Menu<<\0"; // clicky stuff?
draw_text(2, 57, str);
char* credits = "Stephen and Yvonne, 2021\0";
draw_text(55,58,credits);
// 1. Still Life
str = "1. Still Life\0";
draw_text(4, 10, str);
block(49, 25);
tub(60, 25);
ship(70, 25);
// 2. Oscillators
str = "2. Oscillators\0";
draw_text(4, 20, str);
blinker(50, 50);
toad(60, 49);
pulsar(80, 50);
// 3. Space ships and Guns
str = "3. Space ships and Guns\0";
draw_text(4, 30, str);
glider(50, 70);
glider_gun(80, 70, true);
/*
// 4. Logic gates
str = "4. Logic gates and other cool stuff\0";
draw_text(4, 50, str);
draw_ECE243(200, 200);
/* // scratch this, this is kinda stupid
draw_board(0,RESOLUTION_X,0,RESOLUTION_Y);
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
draw_board(0,RESOLUTION_X,0,RESOLUTION_Y);
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
draw_ECE243(100, 100);
*/
// simulation loop
int total_iterations = 0;
while (1)
{
if (screen != PRESETS) break;
if (total_iterations > 0 && isPaused) // display the initial config
continue;
// start clearing previously drawn pixels
clear(tile_size);
// finish clearing previously drawn pixels
// draw, then update the game board
//update_board_state(0,RESOLUTION_X,0,RESOLUTION_Y);
update_board_state(0,board_x,0,board_y, tile_size);
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
total_iterations++;
}
if (screen == RANDOM)
random_screen();
else if (screen == USER_DRAWING)
user_drawing();
else if (screen == MAIN_MENU)
main_menu();
else
;
}
/*
void logic_gates(){
initial_clear();
initial_clear_chars();
const int tile_size = 4; // 2x2
const int board_x = RESOLUTION_X/tile_size;
const int board_y = RESOLUTION_Y/tile_size;
isPaused = true;
// draw text
char* str = "Game of Life Presets\0";
draw_text((80-strlen(str))/2, 1,str);
str = "- Logic gates\0";
draw_text((80-strlen(str))/2, 3,str);
str = "instructions blah blah\0";
draw_text(4, 6, str);
glider_gun(5, 10, true);
// simulation loop
int total_iterations = 0;
while (1)
{
if (total_iterations > 0 && isPaused) // display the initial config
continue;
// start clearing previously drawn pixels
deleteList(front_list);
front_list->head = pixel_list->head;//back_list->head;
//back_list->head = pixel_list->head;
clear_screen(front_list,tile_size);
pixel_list = init();
front_list = init();
// finish clearing previously drawn pixels
// draw, then update the game board
update_board_state(0,board_x,0,board_y, tile_size);
wait_for_vsync(); // swap front and back buffers on VGA vertical sync
pixel_buffer_start = *(pixel_ctrl_ptr + 1); // new back buffer
total_iterations++;
}
}
*/
/* draw, then update and compare the next board to the previous board */
void update_board_state(int x0, int x1, int y0, int y1, int size){
int total = 0,n = RESOLUTION_X/size,m = RESOLUTION_Y/size;
bool new_board[n][m];
for(int i=x0;i < x1;i++){
for(int j=y0;j < y1;j++){
total = (game_board[(i-1)%n][(j-1)%m]+game_board[(i-1)%n][j]
+game_board[(i-1)%n][(j+1)%m]+game_board[i][(j-1)%m]
+game_board[i][(j+1)%m]+game_board[(i+1)%n][(j-1)%m]
+game_board[(i+1)%n][j]+game_board[(i+1)%n][(j+1)%m])/ALIVE;
//printf("%d %d:%d\n",i,j,total);
if(game_board[i][j]==ALIVE){
color_board[i][j] = rand()%65535;
draw_tile(i*size,j*size,size,color_board[i][j]);
insertFront(pixel_list,i,j,true);
// cell dies if it has insufficient or too many neighbours
if(total < 2 || total > 3){
new_board[i][j] = DEAD;
color_board[i][j] = 0;
}
else{
new_board[i][j] = ALIVE;
}
}else{
// cell lives if it has 3 neighbours
if(total==3){
new_board[i][j] = ALIVE;
}
else{
new_board[i][j] = DEAD;
color_board[i][j] = 0;
}
}
if (prev_board[i][j]==ALIVE && new_board[i][j]==DEAD)
prev_board[i][j] = game_board[i][j];
}
}
for(int i=x0;i < x1;i++){
for(int j=y0;j < y1;j++){
game_board[i][j] = new_board[i][j];
}
}
}
void initialize_board(){
for(int i=0;i < RESOLUTION_X;i++){
for(int j=0;j < RESOLUTION_Y;j++){
game_board[i][j] = DEAD;
prev_board[i][j] = DEAD;
}
}
game_board[100][100] = ALIVE;
game_board[100][101] = ALIVE;
game_board[100][102] = ALIVE;
game_board[100][103] = ALIVE;
//random_initialization(0.90);
}
// 36x9 for horizontal
void glider_gun(int left_x, int top_y, bool hor_not_vert){
if (hor_not_vert){
block(left_x, top_y+4);
block(left_x+34, top_y+2);
for (int y = 0; y < 3; y++){
game_board[left_x+10][top_y+4+y] = ALIVE;
game_board[left_x+16][top_y+4+y] = ALIVE;
game_board[left_x+20][top_y+2+y] = ALIVE;
game_board[left_x+21][top_y+2+y] = ALIVE;
}
game_board[left_x+11][top_y+3] = ALIVE;
game_board[left_x+11][top_y+7] = ALIVE;
game_board[left_x+12][top_y+2] = ALIVE;
game_board[left_x+13][top_y+2] = ALIVE;
game_board[left_x+12][top_y+8] = ALIVE;
game_board[left_x+13][top_y+8] = ALIVE;
game_board[left_x+14][top_y+5] = ALIVE;
game_board[left_x+15][top_y+3] = ALIVE;
game_board[left_x+15][top_y+7] = ALIVE;
game_board[left_x+17][top_y+5] = ALIVE;
game_board[left_x+22][top_y+1] = ALIVE;
game_board[left_x+22][top_y+5] = ALIVE;
game_board[left_x+24][top_y] = ALIVE;
game_board[left_x+24][top_y+1] = ALIVE;
game_board[left_x+24][top_y+5] = ALIVE;
game_board[left_x+24][top_y+6] = ALIVE;
}
else{
}
}
void glider_eater(int left_x, int top_y, bool hor_not_vert){
}
void and_gate(int left_x, int top_y){
}
void or_gate(int left_x, int top_y){
}
void not_gate(int left_x, int top_y){
}
// spaceship 3x3
void glider (int left_x, int top_y){
game_board[left_x][top_y+2] = ALIVE;
game_board[left_x+1][top_y+2] = ALIVE;
game_board[left_x+2][top_y+2] = ALIVE;
game_board[left_x+2][top_y+1] = ALIVE;
game_board[left_x+1][top_y] = ALIVE;
}
// 12x12
void pulsar(int centre_x, int centre_y){
for (int d = 2; d <=4; d++)
{
game_board[centre_x+d][centre_y+6] = ALIVE;
game_board[centre_x+d][centre_y+1] = ALIVE;
game_board[centre_x+d][centre_y-6] = ALIVE;
game_board[centre_x+d][centre_y-1] = ALIVE;
game_board[centre_x-d][centre_y+6] = ALIVE;
game_board[centre_x-d][centre_y+1] = ALIVE;
game_board[centre_x-d][centre_y-6] = ALIVE;
game_board[centre_x-d][centre_y-1] = ALIVE;
game_board[centre_x+6][centre_y+d] = ALIVE;
game_board[centre_x+1][centre_y+d] = ALIVE;
game_board[centre_x-6][centre_y+d] = ALIVE;
game_board[centre_x-1][centre_y+d] = ALIVE;
game_board[centre_x+6][centre_y-d] = ALIVE;
game_board[centre_x+1][centre_y-d] = ALIVE;
game_board[centre_x-6][centre_y-d] = ALIVE;
game_board[centre_x-1][centre_y-d] = ALIVE;
}
}
// still-life, 3x3
void tub(int centre_x, int centre_y){
game_board[centre_x][centre_y+1] = ALIVE;
game_board[centre_x][centre_y-1] = ALIVE;
game_board[centre_x+1][centre_y] = ALIVE;
game_board[centre_x-1][centre_y] = ALIVE;
}
// still-life 2x2
void block(int left_x, int top_y){
game_board[left_x][top_y] = ALIVE;
game_board[left_x+1][top_y] = ALIVE;
game_board[left_x+1][top_y+1] = ALIVE;
game_board[left_x][top_y+1] = ALIVE;
}
// still-life 3x3
void ship(int centre_x, int centre_y){
game_board[centre_x][centre_y+1] = ALIVE;
game_board[centre_x][centre_y-1] = ALIVE;
game_board[centre_x+1][centre_y] = ALIVE;
game_board[centre_x-1][centre_y] = ALIVE;
game_board[centre_x+1][centre_y+1] = ALIVE;
game_board[centre_x-1][centre_y-1] = ALIVE;
}
// period 2 oscillator 3x3
void blinker(int centre_x, int centre_y){
for (int x = centre_x-1; x < centre_x+2; x++){
game_board[x][centre_y] = ALIVE;
}
}
// period 2 oscillator 4x4
void toad(int left_x, int top_y){
for (int x = left_x; x < left_x+3; x++){
game_board[x][top_y] = ALIVE;
game_board[x+1][top_y+1] = ALIVE;
}
}
// 37x5
void draw_ECE243(int left_x, int top_y){
//E
for (int d = 0; d <=4; d++){
game_board[left_x][top_y+d] = ALIVE;
game_board[left_x+1][top_y+d] = ALIVE;
}
for (int d = 2; d<=4; d++){
game_board[left_x+d][top_y] = ALIVE;
game_board[left_x+d][top_y+2] = ALIVE;
game_board[left_x+d][top_y+4] = ALIVE;
}
//C
for (int d = 0; d <=4; d++){
game_board[left_x+7][top_y+d] = ALIVE;
game_board[left_x+8][top_y+d] = ALIVE;
}
for (int d = 2; d<=4; d++){
game_board[left_x+7+d][top_y] = ALIVE;
game_board[left_x+7+d][top_y+4] = ALIVE;
}
//E
for (int d = 0; d <=4; d++){
game_board[left_x+14][top_y+d] = ALIVE;
game_board[left_x+15][top_y+d] = ALIVE;
}
for (int d = 2; d<=4; d++){
game_board[left_x+d+14][top_y] = ALIVE;
game_board[left_x+d+14][top_y+2] = ALIVE;
game_board[left_x+d+14][top_y+4] = ALIVE;
}
//2
for (int d = 0; d <=2; d++){
game_board[left_x+24][top_y+d] = ALIVE;
game_board[left_x+25][top_y+d] = ALIVE;
game_board[left_x+21][top_y+d+2] = ALIVE;
game_board[left_x+22][top_y+d+2] = ALIVE;
}
for (int d = 2; d<=4; d++){
game_board[left_x+d+21][top_y] = ALIVE;
game_board[left_x+d+21][top_y+2] = ALIVE;
game_board[left_x+d+21][top_y+4] = ALIVE;
}
//4
for (int d = 0; d <=2; d++){
game_board[left_x+28][top_y+d] = ALIVE;
game_board[left_x+29][top_y+d] = ALIVE;
game_board[left_x+28+d][top_y+2] = ALIVE;
}
for (int d = 0; d <=4; d++){
game_board[left_x+31][top_y+d] = ALIVE;
game_board[left_x+32][top_y+d] = ALIVE;
}
//3
for (int d = 0; d <=4; d++){
game_board[left_x+37][top_y+d] = ALIVE;
game_board[left_x+38][top_y+d] = ALIVE;
}
for (int d = 2; d<=4; d++){
game_board[left_x+d+33][top_y] = ALIVE;
game_board[left_x+d+33][top_y+2] = ALIVE;
game_board[left_x+d+33][top_y+4] = ALIVE;
}