-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
528 lines (484 loc) · 12.7 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
/*********************************************
Project : X si 0
**********************************************
Chip type: ATmega164A
Clock frequency: 20 MHz
Compilers: CVAVR 2.x
*********************************************/
#include <mega164a.h>
#include <stdio.h>
#include <delay.h>
#include <string.h>
#include <stdlib.h>
#include "defs.h"
#include "tictactoe.h"
//*************************************************************************************************
//*********** BEGIN SERIAL STUFF (interrupt-driven, generated by Code Wizard) *********************
//*************************************************************************************************
#ifndef RXB8
#define RXB8 1
#endif
#ifndef TXB8
#define TXB8 0
#endif
#ifndef UPE
#define UPE 2
#endif
#ifndef DOR
#define DOR 3
#endif
#ifndef FE
#define FE 4
#endif
#ifndef UDRE
#define UDRE 5
#endif
#ifndef RXC
#define RXC 7
#endif
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART0 Receiver buffer
#define RX_BUFFER_SIZE0 8
char rx_buffer0[RX_BUFFER_SIZE0];
#if RX_BUFFER_SIZE0 <= 256
unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
#else
unsigned int rx_wr_index0,rx_rd_index0,rx_counter0;
#endif
// This flag is set on USART0 Receiver buffer overflow
bit rx_buffer_overflow0;
// USART0 Receiver interrupt service routine
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer0[rx_wr_index0++]=data;
#if RX_BUFFER_SIZE0 == 256
// special case for receiver buffer size=256
if (++rx_counter0 == 0) rx_buffer_overflow0=1;
#else
if (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
if (++rx_counter0 == RX_BUFFER_SIZE0)
{
rx_counter0=0;
rx_buffer_overflow0=1;
}
#endif
}
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART0 Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter0==0);
data=rx_buffer0[rx_rd_index0++];
#if RX_BUFFER_SIZE0 != 256
if (rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
#endif
#asm("cli")
--rx_counter0;
#asm("sei")
return data;
}
#pragma used-
#endif
// USART0 Transmitter buffer
#define TX_BUFFER_SIZE0 8
char tx_buffer0[TX_BUFFER_SIZE0];
#if TX_BUFFER_SIZE0 <= 256
unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;
#else
unsigned int tx_wr_index0,tx_rd_index0,tx_counter0;
#endif
// USART0 Transmitter interrupt service routine
interrupt [USART0_TXC] void usart0_tx_isr(void)
{
if (tx_counter0)
{
--tx_counter0;
UDR0=tx_buffer0[tx_rd_index0++];
#if TX_BUFFER_SIZE0 != 256
if (tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;
#endif
}
}
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART0 Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter0 == TX_BUFFER_SIZE0);
#asm("cli")
if (tx_counter0 || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
{
tx_buffer0[tx_wr_index0++]=c;
#if TX_BUFFER_SIZE0 != 256
if (tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
#endif
++tx_counter0;
}
else
UDR0=c;
#asm("sei")
}
#pragma used-
#endif
//*************************************************************************************************
//********************END SERIAL STUFF (USART0) **************************************************
//*************************************************************************************************
//******* if you need USART1, enable it in Code Wizard and copy coresponding code here *********
//*************************************************************************************************
char currentLED = 1;
char currentColor = GREEN;
char currentMove = 1;
char tempLED, foundLED, row, col;
char i,j;
//ttt game board to work with
struct tttBoard tttB;
char nextAvailableLED(){
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
if(isAvailable(i,j) == TRUE){
return (i*3 + j + 1);
}
}
}
}
void setLED(char led, char value, char state){
if(led == 1 && value == GREEN){
LED1A = state;
}
if(led == 1 && value == RED){
LED1B = state;
}
if(led == 2 && value == GREEN){
LED2A = state;
}
if(led == 2 && value == RED){
LED2B = state;
}
if(led == 3 && value == GREEN){
LED3A = state;
}
if(led == 3 && value == RED){
LED3B = state;
}
if(led == 4 && value == GREEN){
LED4A = state;
}
if(led == 4 && value == RED){
LED4B = state;
}
if(led == 5 && value == GREEN){
LED5A = state;
}
if(led == 5 && value == RED){
LED5B = state;
}
if(led == 6 && value == GREEN){
LED6A = state;
}
if(led == 6 && value == RED){
LED6B = state;
}
if(led == 7 && value == GREEN){
LED7A = state;
}
if(led == 7 && value == RED){
LED7B = state;
}
if(led == 8 && value == GREEN){
LED8A = state;
}
if(led == 8 && value == RED){
LED8B = state;
}
if(led == 9 && value == GREEN){
LED9A = state;
}
if(led == 9 && value == RED){
LED9B = state;
}
}
void blinkLED(){
if(currentLED == 1 && currentColor == GREEN){
LED1A = ~LED1A;
}
if(currentLED == 1 && currentColor == RED){
LED1B = ~LED1B;
}
if(currentLED == 2 && currentColor == GREEN){
LED2A = ~LED2A;
}
if(currentLED == 2 && currentColor == RED){
LED2B = ~LED2B;
}
if(currentLED == 3 && currentColor == GREEN){
LED3A = ~LED3A;
}
if(currentLED == 3 && currentColor == RED){
LED3B = ~LED3B;
}
if(currentLED == 4 && currentColor == GREEN){
LED4A = ~LED4A;
}
if(currentLED == 4 && currentColor == RED){
LED4B = ~LED4B;
}
if(currentLED == 5 && currentColor == GREEN){
LED5A = ~LED5A;
}
if(currentLED == 5 && currentColor == RED){
LED5B = ~LED5B;
}
if(currentLED == 6 && currentColor == GREEN){
LED6A = ~LED6A;
}
if(currentLED == 6 && currentColor == RED){
LED6B = ~LED6B;
}
if(currentLED == 7 && currentColor == GREEN){
LED7A = ~LED7A;
}
if(currentLED == 7 && currentColor == RED){
LED7B = ~LED7B;
}
if(currentLED == 8 && currentColor == GREEN){
LED8A = ~LED8A;
}
if(currentLED == 8 && currentColor == RED){
LED8B = ~LED8B;
}
if(currentLED == 9 && currentColor == GREEN){
LED9A = ~LED9A;
}
if(currentLED == 9 && currentColor == RED){
LED9B = ~LED9B;
}
}
void initLEDS(){
setLED(1,GREEN,1);
setLED(1,RED,0);
for(i=2; i<=9; i++){
setLED(i,GREEN,0);
setLED(i,RED,0);
}
currentLED = 1;
currentColor = GREEN;
currentMove = 1;
}
//reset only the game board values
void initBoard(){
for(i=0; i<3; i++){
for(j=0; j<3; j++){
tttB.board[i][j] = 0;
}
}
}
//reset only the game board winner
void initWinner(){
tttB.winner = -1;
}
//reset everything on the game board
void initAll(){
initLEDS();
initBoard();
initWinner();
}
bool isAvailable(char row, char col){
if(tttB.board[row][col] != 0){
return FALSE;
}
else return TRUE;
}
bool isBoardFull(){
for(i=0; i<3; i++){
for(j=0; j<3; j++){
if(isAvailable(i,j) == TRUE){
return FALSE;
}
}
}
return TRUE;
}
//checking if the game board is in any end state
//returning the state of the game board
bool checkForEndState(){
//initializing state with default value for no clear winner
char state = -1;
//check if the board is full
if(isBoardFull() == TRUE){
//the game is a tie
state = 0;
return state;
}
else{
//checking rows
for(i=0; i<3; i++){
if(tttB.board[i][0] != 0 && tttB.board[i][0] == tttB.board[i][1] && tttB.board[i][1] == tttB.board[i][2]){
state = tttB.board[i][0];
return state;
}
}
//checking columns
for(j=0; j<3; j++){
if(tttB.board[0][j] != 0 && tttB.board[0][j] == tttB.board[1][j] && tttB.board[1][j] == tttB.board[2][j]){
state = tttB.board[0][j];
return state;
}
}
//checking first diagonal
if(tttB.board[0][0] != 0 && tttB.board[0][0] == tttB.board[1][1] && tttB.board[1][1] == tttB.board[2][2]){
state = tttB.board[0][0];
return state;
}
//checking second diagonal
if(tttB.board[0][2] != 0 && tttB.board[0][2] == tttB.board[1][1] && tttB.board[1][1] == tttB.board[2][0]){
state = tttB.board[0][2];
return state;
}
}
return state;
}
void assignWinner(char state){
tttB.winner = state;
delay_ms(4000);
initAll();
}
//put value in designated row and column on the game board
void put(char row, char col, char value){
char state;
//if the row and column haven't been occupied
if(tttB.winner == -1 && isAvailable(row,col) == TRUE){
//put value
tttB.board[row][col] = value;
setLED((row)*3 + col + 1, value, 1);
//check for end state every successful allocation
state = checkForEndState();
if(state != -1){
assignWinner(state);
}
else{
currentLED = nextAvailableLED();
if(currentColor == GREEN){
currentColor = RED;
}
else if(currentColor == RED){
currentColor = GREEN;
}
}
}
}
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
blinkLED();
}
void checkLeft(){
if(SW_LEFT == 0){
delay_ms(30);
if(SW_LEFT == 0){
while(SW_LEFT == 0){
wdogtrig();
}
setLED(currentLED, currentColor, 0);
tempLED = currentLED;
foundLED = FALSE;
while(!foundLED){
if(tempLED == 1){
tempLED = 9;
}
else{
tempLED --;
}
if(tempLED % 3 != 0){
row = tempLED / 3;
col = tempLED % 3 - 1;
}
else{
row = tempLED / 3 - 1;
col = 3 - 1;
}
if(isAvailable(row, col) == TRUE){
currentLED = tempLED;
foundLED = true;
}
}
}
}
}
void checkRight(){
if(SW_RIGHT == 0){
delay_ms(30);
if(SW_RIGHT == 0){
while(SW_RIGHT == 0){
wdogtrig();
}
setLED(currentLED, currentColor, 0);
tempLED = currentLED;
foundLED = FALSE;
while(!foundLED){
if(tempLED == 9){
tempLED = 1;
}
else{
tempLED ++;
}
if(tempLED % 3 != 0){
row = tempLED / 3;
col = tempLED % 3 - 1;
}
else{
row = tempLED / 3 - 1;
col = 3 - 1;
}
if(isAvailable(row, col) == TRUE){
currentLED = tempLED;
foundLED = true;
}
}
}
}
}
void checkSelect(){
if(SW_SELECT == 0){
delay_ms(30);
if(SW_SELECT == 0){
while(SW_SELECT == 0){
wdogtrig();
}
setLED(currentLED, currentColor, 0);
if(currentLED % 3 != 0){
put(currentLED / 3, currentLED % 3 - 1, currentColor);
}
else{
put(currentLED / 3 - 1, 3 - 1, currentColor);
}
}
}
}
void checkButtons(){
checkLeft();
checkRight();
checkSelect();
}
void main (void)
{
Init_initController();
#asm("sei");
initAll();
while(TRUE){
wdogtrig();
checkButtons();
}
}