Statistics
| Branch: | Revision:

root / main.c @ 76dc4e0b

History | View | Annotate | Download (21.3 KB)

1
#define F_CPU 16000000L
2

    
3
#include <inttypes.h>
4
#include <avr/io.h>
5
#include <util/delay.h>
6
#include <avr/interrupt.h>
7
#include <avr/pgmspace.h>
8
#include "bricks.h"
9
#include "scorefont.h"
10

    
11
//let's make our lives easier :)
12
#define PORT_(port) PORT ## port
13
#define DDR_(port)  DDR  ## port
14
#define PIN_(port)  PIN  ## port
15
#define PORT(port) PORT_(port)
16
#define DDR(port)  DDR_(port)
17
#define PIN(port)  PIN_(port)
18

    
19
// wallboard pins:
20
#define WALLBOARD_PORT         B
21
#define CLOCK_PIN         PB0
22
#define DATA_GREEN_PIN         PB3
23
#define DATA_RED_PIN         PB1
24
#define OUTPUT_ENABLE_PIN PB2
25
// address lines - please note that the address lines have to be
26
// on the same port and in the current implementation are on 0-3
27
#define ADDRESS_PORT         C
28
#define A0                 PC0
29
#define A1                 PC1
30
#define A2                 PC2
31
#define A3                 PC3
32

    
33
// note that both controllers are on the same port, the reason is
34
// controllerOffset for faster input handling
35
#define CONTROLLER0_PORT   D
36
#define CONTROLLER0_PLAYER0_LEFT   PD7
37
#define CONTROLLER0_PLAYER0_RIGHT  PD6
38
#define CONTROLLER0_PLAYER0_DOWN   PD5
39
#define CONTROLLER0_PLAYER0_ROTATE PD4
40
#define CONTROLLER1_PORT   D
41
#define CONTROLLER1_PLAYER0_LEFT   PD3
42
#define CONTROLLER1_PLAYER0_RIGHT  PD2
43
#define CONTROLLER1_PLAYER0_DOWN   PD1
44
#define CONTROLLER1_PLAYER0_ROTATE PD0
45

    
46
// control mappings for atari joystick
47
#define PLAYER0_LEFT   0b0100
48
#define PLAYER0_DOWN   0b0010 
49
#define PLAYER0_RIGHT  0b1000
50
#define PLAYER0_ROTATE 0b0001
51

    
52
#define PLAYER1_LEFT   0b1000
53
#define PLAYER1_DOWN   0b0010 
54
#define PLAYER1_RIGHT  0b0100
55
#define PLAYER1_ROTATE 0b0001
56

    
57
// note NOGAMEAREA needs has to be one pixel bigger
58
// because we are lazy in moveSpriteHorizontal (forward check)
59
#define NOGAMEAREA 0b111111111
60
#define TICKS 40
61
#define NUMBER_OF_PLAYERS 2
62

    
63
struct player;
64

    
65
typedef struct sprite {
66
        volatile int8_t offset;
67
        volatile int8_t yLowerPos;
68
        volatile uint16_t block[4];
69
        volatile uint16_t raw;
70
        volatile struct player *owner;
71
        } sprite_t;
72

    
73
typedef struct player {
74
        volatile uint16_t score;
75
        volatile uint16_t oldScore;
76
        volatile int8_t gameAreaStart;
77
        volatile int8_t gameAreaEnd;
78
        volatile sprite_t sprite;
79
        volatile uint8_t mirrored;
80
        volatile uint8_t controllerOffset;
81
        volatile uint8_t controllerInputMask;
82
        volatile sprite_t nextSprite;
83
        } player_t;
84

    
85
int main(void);
86
inline void redDot(void);
87
inline void greenDot(void);
88
inline void yellowDot(void);
89
inline void noDot(void);
90
inline void OE(void);
91
inline void ODE(void);
92
void loopLines(void);
93
void rotateLeft(sprite_t* sprite);
94
void moveFlyingRowsDown(player_t* player);
95
void moveSpriteDown(sprite_t* sprite);
96
void clearCompleteLines(player_t* player);
97
void rawToBlock(uint16_t block[4], uint16_t raw, int8_t offset);
98
void moveSpriteToLines(sprite_t* sprite);
99
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead); // lookahead -1 -> look down
100
void moveSpriteHorizontal(volatile sprite_t *sprite, int8_t dir); // -1 if left, 1 if right
101
void handleGamepadInput(player_t* player, uint8_t player_number, uint8_t input);
102
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite, uint8_t player_number);
103
static inline void drawGameArea(player_t player[NUMBER_OF_PLAYERS]);
104
void gameOver(player_t* player);
105
void setNewSprite(volatile sprite_t* sprite);
106
void drawScore(uint16_t* score, uint8_t round);
107

    
108
volatile uint16_t lines[90] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
109
volatile player_t player[NUMBER_OF_PLAYERS];
110

    
111
volatile uint8_t keypressCounter = 0;
112

    
113
int main(){
114
        // hardware init
115
        // clock, data and output enable
116
        DDR(WALLBOARD_PORT) |= (1<<CLOCK_PIN) | (1<<DATA_RED_PIN) | (1<<DATA_GREEN_PIN) | (1<<OUTPUT_ENABLE_PIN);
117
        // gamepad (PD7 is for bootloadstart):
118
        // 7...4 left down right rotate
119
        //
120
        // move > rotate > down
121
        DDR(CONTROLLER0_PORT) &= ~((1<<CONTROLLER0_PLAYER0_LEFT) | (1<<CONTROLLER0_PLAYER0_RIGHT) | (1<<CONTROLLER0_PLAYER0_DOWN) | (1<<CONTROLLER0_PLAYER0_ROTATE));
122
        PORT(CONTROLLER0_PORT) |= (1<<CONTROLLER0_PLAYER0_LEFT) | (1<<CONTROLLER0_PLAYER0_RIGHT) | (1<<CONTROLLER0_PLAYER0_DOWN) | (1<<CONTROLLER0_PLAYER0_ROTATE);
123
        DDR(CONTROLLER1_PORT) &= ~((1<<CONTROLLER1_PLAYER0_LEFT) | (1<<CONTROLLER1_PLAYER0_RIGHT) | (1<<CONTROLLER1_PLAYER0_DOWN) | (1<<CONTROLLER1_PLAYER0_ROTATE));
124
        PORT(CONTROLLER1_PORT) |= (1<<CONTROLLER1_PLAYER0_LEFT) | (1<<CONTROLLER1_PLAYER0_RIGHT) | (1<<CONTROLLER1_PLAYER0_DOWN) | (1<<CONTROLLER1_PLAYER0_ROTATE);
125
        // address lines:
126
        DDR(ADDRESS_PORT) |= (1<<A3) | (1<<A2) | (1<<A1) | (1<<A0);
127

    
128
        // init player
129
        player[0].score = 0;
130
        player[0].oldScore = 0;
131
        player[0].gameAreaStart = 0;
132
        player[0].gameAreaEnd = 28;
133
        player[0].sprite.owner = (player_t*)&player[0];
134
        setNewSprite((sprite_t*)&(player[0].sprite));
135
        player[0].mirrored = 0;
136
        player[0].controllerInputMask = (1<<CONTROLLER0_PLAYER0_LEFT) | (1<<CONTROLLER0_PLAYER0_DOWN) | (1<<CONTROLLER0_PLAYER0_RIGHT) | (1<<CONTROLLER0_PLAYER0_ROTATE);
137
        player[0].controllerOffset = 4;
138
        player[0].nextSprite.owner = (player_t*)&player[0];
139
        setNewSprite((sprite_t*)&(player[0].nextSprite));
140
        // next one
141
        player[1].score = 0;
142
        player[1].oldScore = 0;
143
        player[1].gameAreaStart = 37;
144
        player[1].gameAreaEnd = 65;
145
        player[1].sprite.owner = (player_t*)&player[1];
146
        setNewSprite((sprite_t*)&(player[1].sprite));
147
        player[1].mirrored = 1;
148
        player[1].controllerInputMask = (1<<CONTROLLER1_PLAYER0_LEFT) | (1<<CONTROLLER1_PLAYER0_DOWN) | (1<<CONTROLLER1_PLAYER0_RIGHT) | (1<<CONTROLLER1_PLAYER0_ROTATE);
149
        player[1].controllerOffset = 0;
150
        player[1].nextSprite.owner = (player_t*)&player[1];
151
        setNewSprite((sprite_t*)&(player[1].nextSprite));
152
        while(1){
153
                for(uint8_t delay = 0; delay < TICKS; delay++){
154
                        drawGameArea((player_t*)player);
155
                        if(delay%(TICKS/(NUMBER_OF_PLAYERS)) == 0){
156
                                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){ //fair handing of input
157
                                        handleGamepadInput(&player[playerNumber], playerNumber,
158
                                                                ((~PIN(CONTROLLER0_PORT))& player[playerNumber].controllerInputMask)
159
                                                                        >>player[playerNumber].controllerOffset);
160
                                }
161
                        }
162
                }
163
                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
164
                        moveSpriteDown((sprite_t*)&(player[playerNumber].sprite));
165
                        clearCompleteLines((player_t*)&player[playerNumber]);
166
                        moveFlyingRowsDown((player_t*)&player[playerNumber]);
167
                }
168
                if((player[0].score > 5)
169
                  && (player[0].score-2>=player[0].oldScore)
170
                  && (player[0].gameAreaEnd-player[0].gameAreaStart > 12)    // avoid way to small
171
                  && (player[1].gameAreaEnd-player[1].gameAreaStart > 12)){ // game areas.
172
                        player[0].gameAreaEnd++;
173
                        player[1].gameAreaStart++;
174
                }
175
                if((player[1].score > 5)
176
                  && (player[1].score-2>=player[1].oldScore)
177
                  && (player[0].gameAreaEnd-player[0].gameAreaStart > 12)    // avoid way to small
178
                  && (player[1].gameAreaEnd-player[1].gameAreaStart > 12)){ // game areas.
179
                        player[0].gameAreaEnd--;
180
                        player[1].gameAreaStart--;
181
                }
182
                player[0].oldScore = player[0].score;
183
                player[1].oldScore = player[1].score;
184
        }
185
}
186

    
187
void setNewSprite(volatile sprite_t* sprite){
188
        (*sprite).yLowerPos = (*((*sprite).owner)).gameAreaEnd-2;
189
        (*sprite).raw = pgm_read_word(bricks+(keypressCounter%(sizeof(bricks)/sizeof(uint16_t))));
190
        (*sprite).offset = 10;
191
        rawToBlock((uint16_t*)(*sprite).block, (*sprite).raw, (*sprite).offset);
192
        keypressCounter++;
193
}
194

    
195
void gameOver(player_t* player){
196
        for(int16_t line = (*player).gameAreaEnd; line >= (*player).gameAreaStart; line--){
197
                lines[line] = 0x0000;
198
        }
199
        (*player).score = 0;
200
        setNewSprite((sprite_t*)&((*player).sprite));
201
}
202

    
203
void drawScore(uint16_t* score, uint8_t round){
204
        if((*score) & (1<<round)){
205
                redDot();
206
        }
207
        else{
208
                noDot();
209
        }
210
}
211

    
212
inline void printYellowBar(void){
213
        yellowDot();
214
}
215

    
216
inline void printGreenBar(void){
217
        greenDot();
218
}
219

    
220
inline void printRedBar(void){
221
        redDot();
222
}
223

    
224
inline void printEmptyBar(void){
225
        noDot();
226
}
227

    
228
inline void printScoreFontLine(uint8_t scoreNibble, uint8_t step, uint8_t mirror){
229
        if(mirror){
230
                for(int8_t i = 7; i >=0; i--){
231
                        if(score_font[scoreNibble][step] & (1<<i)){
232
                                yellowDot();
233
                        }
234
                        else{
235
                                noDot();
236
                        }
237
                }
238
        }
239
        else{
240
                for(uint8_t i = 0; i < 8; i++){
241
                        if(score_font[scoreNibble][step] & (1<<i)){
242
                                yellowDot();
243
                        }
244
                        else{
245
                                noDot();
246
                        }
247
                }
248
        }
249
}
250

    
251
static inline void drawGameArea(player_t player[NUMBER_OF_PLAYERS]){
252
        PORT(ADDRESS_PORT) = 0;
253
        for(uint8_t round = 0; round < 16; /*round++*/){
254
                for(int16_t line = player[1].gameAreaStart; line <= player[1].gameAreaEnd; line++){
255
                        printLine(line, 15-round/2, (sprite_t*)&(player[1].sprite), 1);
256
                }
257
                printGreenBar();
258
                for(int16_t line = 0; line < 8 ; line++){ //player[1].gameAreaEnd; line < player[0].gameAreaStart; line--){
259
                        if(round <=15 && round >=12){
260
                                printScoreFontLine((player[1].score>>4)&0x0f,15-round, 1);
261
                                break;
262
                        }
263
                        if(round <= 10 && round >= 7){
264
                                printScoreFontLine((player[1].score)&0x0f,10-round, 1);
265
                                break;
266
                        }
267
                        if(round == 5){
268
                                printYellowBar();
269
                        }
270
                        else{
271
                                if(round < 4){
272
                                        if(((line >= 2 && line <= 5)) && (player[1].nextSprite.block[line-2] & (1<<(14-round)))){
273
                                                yellowDot();
274
                                        }
275
                                        else{
276
                                                noDot();
277
                                        }
278
                                }
279
                                else{
280
                                        noDot();
281
                                }
282
                        }
283
                }
284
                printYellowBar();
285
                for(int16_t line = 0; line < 8 ; line++){
286
                        if(round >=0 && round <= 3){
287
                                printScoreFontLine((player[0].score>>4)&0x0f, round, 0);
288
                                break;
289
                        }
290
                        if(round >=5 && round <= 8){
291
                                printScoreFontLine(player[0].score&0x0f, round-5, 0);
292
                                break;
293
                        }
294
                        if(round == 10){
295
                                printYellowBar();
296
                        }
297
                        else{
298
                                if(round > 11){
299
                                        if(((line >= 2 && line <= 5)) && (player[0].nextSprite.block[5-line] & (1<<(25-round)))){
300
                                                yellowDot();
301
                                        }
302
                                        else{
303
                                                noDot();
304
                                        }
305
                                }
306
                                else{
307
                                        noDot();
308
                                }
309
                        }
310
                }
311
                printRedBar();
312
                for(int16_t line = player[0].gameAreaEnd; line >= player[0].gameAreaStart; line--){
313
                        printLine(line, 15-round/2, (sprite_t*)&(player[0].sprite), 0);
314
                }
315

    
316
                OE();
317
                _delay_us(151);
318
                ODE();
319
                round++;
320
                PORT(ADDRESS_PORT) = round;
321
                _delay_us(10); // avoid ghosting
322
        }
323
}
324

    
325
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite, uint8_t player_number){
326
        if((1<<round) & lines[line]){
327
                yellowDot();
328
                yellowDot();
329
        }
330
        else{
331
                if((*sprite).yLowerPos <= line && (*sprite).yLowerPos+4 > line){
332
                        if((1<<round) & (*sprite).block[line-(*sprite).yLowerPos]){
333
                                switch(player_number){
334
                                        case 0:
335
                                                redDot();
336
                                                redDot();
337
                                                break;
338
                                        case 1:
339
                                                greenDot();
340
                                                greenDot();
341
                                                break;
342
                                        default:
343
                                                yellowDot();
344
                                                yellowDot();
345
                                                break;
346
                                }
347
                        }
348
                        else{
349
                                noDot();
350
                                noDot();
351
                        }
352
                }
353
                else{
354
                        noDot();
355
                        noDot();
356
                }
357
        }
358
}
359

    
360
void handleGamepadInput(player_t* player, uint8_t player_number, uint8_t input){
361
        if(player_number == 0){
362
                switch(input){
363
                        // move > rotate > down
364
                        case (PLAYER0_LEFT | PLAYER0_DOWN | PLAYER0_RIGHT | PLAYER0_ROTATE):
365
                                keypressCounter+=2;
366
                        case (PLAYER0_DOWN | PLAYER0_ROTATE):
367
                                rotateLeft((sprite_t*)&((*player).sprite));
368
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
369
                                moveSpriteDown((sprite_t*)&((*player).sprite));
370
                                keypressCounter+=2;
371
                                break;
372
                        case (PLAYER0_LEFT | PLAYER0_DOWN | PLAYER0_RIGHT):
373
                                keypressCounter+=2;
374
                        case (PLAYER0_DOWN):
375
                                moveSpriteDown((sprite_t*)&((*player).sprite));
376
                                keypressCounter+=1;
377
                                break;
378
                        case (PLAYER0_RIGHT):
379
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
380
                                keypressCounter+=1;
381
                                break;
382
                        case (PLAYER0_LEFT | PLAYER0_RIGHT | PLAYER0_ROTATE):
383
                                keypressCounter+=1;
384
                        case (PLAYER0_RIGHT | PLAYER0_ROTATE):
385
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
386
                                keypressCounter+=1;
387
                        case (PLAYER0_ROTATE):
388
                                rotateLeft((sprite_t*)&((*player).sprite));
389
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
390
                                keypressCounter+=1;
391
                                break;
392
                        case (PLAYER0_DOWN | PLAYER0_RIGHT):
393
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
394
                                moveSpriteDown((sprite_t*)&((*player).sprite));
395
                                keypressCounter+=2;
396
                                break;
397
                        case (PLAYER0_DOWN | PLAYER0_RIGHT | PLAYER0_ROTATE):
398
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
399
                                rotateLeft((sprite_t*)&((*player).sprite));
400
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
401
                                moveSpriteDown((sprite_t*)&((*player).sprite));
402
                                keypressCounter+=3;
403
                                break;
404
                        case (PLAYER0_LEFT):
405
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
406
                                keypressCounter+=1;
407
                                break;
408
                        case (PLAYER0_LEFT | PLAYER0_ROTATE):
409
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
410
                                rotateLeft((sprite_t*)&((*player).sprite));
411
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
412
                                keypressCounter+=2;
413
                                break;
414
                        case (PLAYER0_LEFT | PLAYER0_DOWN):
415
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
416
                                moveSpriteDown((sprite_t*)&((*player).sprite));
417
                                keypressCounter+=2;
418
                                break;
419
                        case (PLAYER0_LEFT | PLAYER0_DOWN | PLAYER0_ROTATE):
420
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
421
                                rotateLeft((sprite_t*)&((*player).sprite));
422
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
423
                                moveSpriteDown((sprite_t*)&((*player).sprite));
424
                                keypressCounter+=3;
425
                                break;
426
                        // "nops"
427
                        case (PLAYER0_LEFT | PLAYER0_RIGHT): // left+right = no move
428
                                keypressCounter+=2;
429
                        case (0b0000): // nothing pressed
430
                                break;
431
                }
432
        }
433
        else if(player_number == 1){
434
                switch(input){
435
                        // move > rotate > down
436
                        case (PLAYER1_LEFT | PLAYER1_DOWN | PLAYER1_RIGHT | PLAYER1_ROTATE):
437
                                keypressCounter+=2;
438
                        case (PLAYER1_DOWN | PLAYER1_ROTATE):
439
                                rotateLeft((sprite_t*)&((*player).sprite));
440
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
441
                                moveSpriteDown((sprite_t*)&((*player).sprite));
442
                                keypressCounter+=2;
443
                                break;
444
                        case (PLAYER1_LEFT | PLAYER1_DOWN | PLAYER1_RIGHT):
445
                                keypressCounter+=2;
446
                        case (PLAYER1_DOWN):
447
                                moveSpriteDown((sprite_t*)&((*player).sprite));
448
                                keypressCounter+=1;
449
                                break;
450
                        case (PLAYER1_RIGHT):
451
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
452
                                keypressCounter+=1;
453
                                break;
454
                        case (PLAYER1_LEFT | PLAYER1_RIGHT | PLAYER1_ROTATE):
455
                                keypressCounter+=1;
456
                        case (PLAYER1_RIGHT | PLAYER1_ROTATE):
457
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
458
                                keypressCounter+=1;
459
                        case (PLAYER1_ROTATE):
460
                                rotateLeft((sprite_t*)&((*player).sprite));
461
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
462
                                keypressCounter+=1;
463
                                break;
464
                        case (PLAYER1_DOWN | PLAYER1_RIGHT):
465
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
466
                                moveSpriteDown((sprite_t*)&((*player).sprite));
467
                                keypressCounter+=2;
468
                                break;
469
                        case (PLAYER1_DOWN | PLAYER1_RIGHT | PLAYER1_ROTATE):
470
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
471
                                rotateLeft((sprite_t*)&((*player).sprite));
472
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
473
                                moveSpriteDown((sprite_t*)&((*player).sprite));
474
                                keypressCounter+=3;
475
                                break;
476
                        case (PLAYER1_LEFT):
477
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
478
                                keypressCounter+=1;
479
                                break;
480
                        case (PLAYER1_LEFT | PLAYER1_ROTATE):
481
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
482
                                rotateLeft((sprite_t*)&((*player).sprite));
483
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
484
                                keypressCounter+=2;
485
                                break;
486
                        case (PLAYER1_LEFT | PLAYER1_DOWN):
487
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
488
                                moveSpriteDown((sprite_t*)&((*player).sprite));
489
                                keypressCounter+=2;
490
                                break;
491
                        case (PLAYER1_LEFT | PLAYER1_DOWN | PLAYER1_ROTATE):
492
                                moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
493
                                rotateLeft((sprite_t*)&((*player).sprite));
494
                                rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
495
                                moveSpriteDown((sprite_t*)&((*player).sprite));
496
                                keypressCounter+=3;
497
                                break;
498
                        // "nops"
499
                        case (PLAYER1_LEFT | PLAYER1_RIGHT): // left+right = no move
500
                                keypressCounter+=2;
501
                        case (0b0000): // nothing pressed
502
                                break;
503
                }
504
        }
505
        else{
506
                return;
507
        }
508
}
509

    
510
void rawToBlock(uint16_t block[4], uint16_t raw, int8_t offset){
511
        block[0] = raw & 0xf;
512
        block[1] = (raw>>4) & 0xf;
513
        block[2] = (raw>>8) & 0xf;
514
        block[3] = (raw>>12) & 0xf;
515

    
516
        if(offset > 0){
517
                for(uint8_t i = 0; i < 4; i++){
518
                        block[i] = block[i]<<offset;
519
                }
520
        }
521
        else if(offset < 0){
522
                for(uint8_t i = 0; i < 4; i++){
523
                        block[i] = block[i]>>(offset*-1);
524
                }
525
        }
526
}
527

    
528
void clearCompleteLines(player_t* player){
529
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
530
                if((lines[line] & (0xffff & ~(NOGAMEAREA>>1))) == (0xffff & ~(NOGAMEAREA>>1))){
531
                        lines[line] ^= (0xffff & ~(NOGAMEAREA>>1));
532
                        (*player).score++;
533
                }
534
        }
535
}
536

    
537
void moveFlyingRowsDown(player_t* player){ // TODO needs fix becase we don't want to touch nogamearea
538
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd-1; line++){
539
                if(lines[line] == 0x0000){ // TODO maybe add clear effect with green line?
540
                        lines[line] = lines[line+1];
541
                        // blank line to propagate movement
542
                        lines[line+1] = 0x0000;
543
                }
544
        }
545
}
546

    
547
void moveSpriteToLines(sprite_t* sprite){
548
        for(uint8_t j = 0; j < 4; j++){ // copy sprite to lines
549
                lines[(*sprite).yLowerPos+j] |= (*sprite).block[j];
550
        }
551
}
552

    
553
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead){
554
        for(int8_t i = 0; i < 4; i++){
555
                if((*player).sprite.yLowerPos+lookahead+i < (*player).gameAreaStart){
556
                        if((*player).sprite.block[i] == 0x0000){
557
                                continue;
558
                        }
559
                        else{
560
                                return 1;
561
                        }
562
                }
563
                else if((lines[(*player).sprite.yLowerPos+lookahead+i] & block[i]) != 0){ // collision detected
564
                        return 1;
565
                }
566
        }
567
        return 0;
568
}
569

    
570
void moveSpriteDown(sprite_t* sprite){
571
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, (uint16_t*)(*sprite).block, -1)){
572
                if((*sprite).yLowerPos >= (*(*sprite).owner).gameAreaEnd-4){
573
                        gameOver((player_t*)(*sprite).owner);
574
                        return;
575
                }
576
                moveSpriteToLines(sprite);
577
                *sprite = (((player_t*)(*sprite).owner)->nextSprite);
578
                setNewSprite(&(*sprite).owner->nextSprite);
579
                return;
580
        }
581
        (*sprite).yLowerPos--;
582
        if((*sprite).yLowerPos <= 0 && ((*sprite).block[-1*(*sprite).yLowerPos] != 0x00)){
583
                moveSpriteToLines(sprite);
584
                *sprite = (((player_t*)(*sprite).owner)->nextSprite);
585
                setNewSprite(&(*sprite).owner->nextSprite);
586
        }
587
}
588

    
589

    
590
void moveSpriteHorizontal(volatile sprite_t *sprite, int8_t dir){ // -1 if left, 1 if right
591
        uint16_t block[4];
592
        if(dir == 1){
593
                // check if block[*] >= 0x8000, so we cant move left
594
                for(uint8_t i = 0; i < 4; i++){
595
                        if((*sprite).block[i] >= (1<<15)){
596
                                return;
597
                        }
598
                }
599
                for(uint8_t i = 0; i < 4; i++){
600
                        block[i] = (*sprite).block[i] << 1;
601
                }
602
        }
603
        else{
604
                for(uint8_t i = 0; i < 4; i++){
605
                        if(((*sprite).block[i] & (NOGAMEAREA)) != 0x0000){
606
                                return;
607
                        }
608
                }
609
                for(uint8_t i = 0; i < 4; i++){
610
                        block[i] = (*sprite).block[i] >> 1;
611
                }
612
        }
613
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
614
                return;
615
        }
616
        else{
617
                for(uint8_t i = 0; i < 4; i++){
618
                        (*sprite).block[i] = block[i];
619
                }
620
                (*sprite).offset = (*sprite).offset+dir;
621
        }
622
}
623

    
624
void loopLines(){
625
        uint8_t tmp = lines[0];
626
        for(uint8_t line = 0; line < 89 ; line++){
627
                lines[line] = lines[line+1];
628
        }
629
        lines[89] = tmp;
630
}
631

    
632
inline void greenDot(){
633
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN);
634
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN));
635
}
636
inline void redDot(){
637
        PORT(WALLBOARD_PORT) |= (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
638
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
639
}
640
inline void yellowDot(){
641
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
642
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
643
}
644
inline void noDot(){
645
        PORT(WALLBOARD_PORT) |= (1<<CLOCK_PIN);
646
        PORT(WALLBOARD_PORT) &= ~(1<<CLOCK_PIN);
647
}
648

    
649
inline void OE(){
650
        PORT(WALLBOARD_PORT) |= (1<<OUTPUT_ENABLE_PIN);
651
}
652

    
653
inline void ODE(){
654
        PORT(WALLBOARD_PORT) &= ~(1<<OUTPUT_ENABLE_PIN);
655
}
656

    
657
void rotateLeft(sprite_t* sprite){
658
        uint16_t block[4];
659
        uint16_t tmp = (*sprite).raw;
660

    
661
        tmp =((tmp&(1<<15))>>(15-3) | (tmp&(1<<14))>>(14-7) | (tmp&(1<<13))>>(13-11) | (tmp&(1<<12))<<(15-12) | (tmp&(1<<11))>>(11-2) | (tmp&(1<<10))>>(10-6) | (tmp&(1<<9))<<(10-9) | (tmp&(1<<8))<<(14-8) | (tmp&(1<<7))>>(7-1) | (tmp&(1<<6))>>(6-5) | (tmp&(1<<5))<<(9-5) | (tmp&(1<<4))<<(13-4) | (tmp&(1<<3))>>(3-0) | (tmp&(1<<2))<<(4-2) | (tmp&(1<<1))<<(8-1) | (tmp&(1<<0))<<(12-0) );
662

    
663
        rawToBlock(block, tmp, (*sprite).offset);
664

    
665
        // check if sprite violates borders // TODO needs to be updated if game area doesn't span the whole line
666
        for(uint8_t i = 0; i < 4; i++){
667
                if((*sprite).offset < 0){
668
                        if(((block[i]<<((*sprite).offset*-1)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0){
669
                                return;
670
                        }
671
                }
672
                else if((*sprite).offset > 0){
673
                        if((((block[i]>>((*sprite).offset)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0) || ((block[i] & (NOGAMEAREA>>1)) != 0x0000)){
674
                                return;
675
                        }
676
                }
677
                else{
678
                        break;
679
                }
680
        }
681

    
682
        // check first if rotated sprite crashes, if, drop to game
683
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
684
                return;
685
        }
686
        else{
687
                (*sprite).raw = tmp;
688
                for(uint8_t i = 0; i < 4; i++){
689
                        (*sprite).block[i] = block[i];
690
                }
691
        }
692
        // TODO rotate with ifs
693
//        l = ((u&(1<<7)) ? (1<<7)>>4 : 0) | ((u&(1<<6)) ? ((1<<6)>>1) : 0) | ((u&(1<<3)) ? ((1<<3)>>1) : 0) | ((u&(1<<2)) ? ((1<<2)<<4) : 0) | ((l&(1<<6)) ? (1<<6)>>1 : 0) | ((l&(1<<2)) ? (1<<2)<<2 : 0) | ((l&(1<<3)) ? (1<<3)>>3 : 0) | ((l&(1<<7)) ? (1<<7)>>6 : 0);
694
}