Statistics
| Branch: | Revision:

root / main.c @ b2179a62

History | View | Annotate | Download (13.9 KB)

1
#define F_CPU 16000000L                                                                                                                                                  
2
#include <inttypes.h>
3
#include <avr/io.h>
4
#include <util/delay.h>
5
#include <avr/interrupt.h>
6
#include <avr/pgmspace.h>
7

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

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

    
30
#define CONTROLLER_PORT   D
31
#define CONTROLLER_LEFT   PD7
32
#define CONTROLLER_RIGHT  PD6
33
#define CONTROLLER_DOWN   PD5
34
#define CONTROLLER_ROTATE PD4
35

    
36
// control mappings for atari joystick
37
#define LEFT   0b0001
38
#define DOWN   0b1000
39
#define RIGHT  0b0010
40
#define ROTATE 0b0100
41

    
42
#define TICKS 40
43
#define NUMBER_OF_PLAYERS 1
44

    
45
struct player;
46

    
47
typedef struct sprite {
48
        volatile int8_t offset;
49
        volatile int8_t yLowerPos;
50
        volatile uint16_t block[4];
51
        volatile uint16_t raw;
52
        volatile struct player *owner;
53
        } sprite_t;
54

    
55
typedef struct player {
56
        volatile uint16_t score;
57
        volatile int8_t gameAreaStart;
58
        volatile int8_t gameAreaEnd;
59
        volatile sprite_t sprite;
60
        volatile uint8_t mirrored;
61
        } player_t;
62

    
63
int main(void);
64
inline void redDot(void);
65
inline void greenDot(void);
66
inline void yellowDot(void);
67
inline void noDot(void);
68
inline void OE(void);
69
inline void ODE(void);
70
void loopLines(void);
71
void rotateLeft(sprite_t* sprite);
72
void moveFlyingRowsDown(player_t* player);
73
void moveSpriteDown(sprite_t* sprite);
74
void clearCompleteLines(player_t* player);
75
void rawToBlock(uint16_t block[4], uint16_t raw, int8_t offset);
76
void moveSpriteToLines(sprite_t* sprite);
77
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead); // lookahead -1 -> look down
78
void moveSpriteHorizontal(volatile sprite_t *sprite, int8_t dir); // -1 if left, 1 if right
79
void handleGamepadInput(player_t* player, uint8_t input);
80
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite);
81
static inline void drawGameArea(player_t* player);
82
void gameOver(player_t* player);
83
void setNewSprite(sprite_t* sprite);
84
void drawScore(uint16_t* score, uint8_t round);
85

    
86
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 };
87
volatile player_t player[NUMBER_OF_PLAYERS];
88

    
89
const uint16_t bricks[] PROGMEM = { 
90
        /* +----+
91
         * |    |
92
         * | ## |
93
         * | ## |
94
         * |    |
95
         * +----+ */
96
        0b0000011001100000, 
97

    
98
        /* +----+
99
         * |    |
100
         * | #  |
101
         * | ## |
102
         * |  # |
103
         * +----+ */
104
        0b0000010001100010,
105

    
106
        /* +----+
107
         * |    |
108
         * |  # |
109
         * | ## |
110
         * | #  |
111
         * +----+ */
112
        0b0000001001100100, 
113

    
114
        /* +----+
115
         * |    |
116
         * | #  |
117
         * | ## |
118
         * | #  |
119
         * +----+ */
120
        0b0000010001100100, 
121

    
122
        /* +----+
123
         * | #  |
124
         * | #  |
125
         * | #  |
126
         * | #  |
127
         * +----+ */
128
        0b0100010001000100, 
129

    
130
        /* +----+
131
         * |    |
132
         * | #  |
133
         * | #  |
134
         * | ## |
135
         * +----+ */
136
        0b0000010001000110, 
137

    
138
        /* +----+
139
         * |    |
140
         * |  # |
141
         * |  # |
142
         * | ## |
143
         * +----+ */
144
        0b0000001000100110
145
};
146
volatile uint8_t keypressCounter = 0;
147

    
148
int main(){
149
        // hardware init
150
        // clock, data and output enable
151
        DDR(WALLBOARD_PORT) |= (1<<CLOCK_PIN) | (1<<DATA_RED_PIN) | (1<<DATA_GREEN_PIN) | (1<<OUTPUT_ENABLE_PIN);
152
        // gamepad (PD7 is for bootloadstart):
153
        // 7...4 left down right rotate
154
        //
155
        // move > rotate > down
156
        DDR(CONTROLLER_PORT) &= ~((1<<CONTROLLER_LEFT) | (1<<CONTROLLER_RIGHT) | (1<<CONTROLLER_DOWN) | (1<<CONTROLLER_ROTATE));
157
        PORT(CONTROLLER_PORT) |= (1<<CONTROLLER_LEFT) | (1<<CONTROLLER_RIGHT) | (1<<CONTROLLER_DOWN) | (1<<CONTROLLER_ROTATE);
158
        // address lines:
159
        DDR(ADDRESS_PORT) |= (1<<A3) | (1<<A2) | (1<<A1) | (1<<A0);
160

    
161
        // init player
162
        player[0].score = 0;
163
        player[0].gameAreaStart = 0;
164
        player[0].gameAreaEnd = 43;
165
        player[0].sprite.owner = (player_t*)&player[0];
166
        player[0].mirrored = 0;
167
        setNewSprite((sprite_t*)&(player[0].sprite));
168

    
169
        while(1){
170
                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
171
                        for(uint8_t delay = 0; delay < TICKS; delay++){
172
                                drawGameArea((player_t*)&player[playerNumber]);
173
                                if(delay%20 == 0){
174
                                        handleGamepadInput((player_t*)&player[playerNumber], ((~PIN(D))&(0b11110000))>>4);
175
                                }
176
                        }
177
                        moveSpriteDown((sprite_t*)&(player[playerNumber].sprite));
178
                        clearCompleteLines((player_t*)&player[playerNumber]);
179
                        moveFlyingRowsDown((player_t*)&player[playerNumber]);
180
                }
181
        }
182
}
183

    
184
void setNewSprite(sprite_t* sprite){
185
        (*sprite).yLowerPos = (*((*sprite).owner)).gameAreaEnd-2;
186
        (*sprite).raw = pgm_read_word(bricks+(keypressCounter%(sizeof(bricks)/sizeof(uint16_t))));
187
        (*sprite).offset = 6;
188
        rawToBlock((uint16_t*)(*sprite).block, (*sprite).raw, (*sprite).offset);
189
        keypressCounter++;
190
}
191

    
192
void gameOver(player_t* player){ // TODO FIXME
193
        for(int16_t line = (*player).gameAreaStart; line < (*player).gameAreaEnd; line++){
194
                lines[line] = 0xffff;
195
                drawGameArea(player);
196
        }
197
        for(int16_t line = (*player).gameAreaEnd-1; line >= (*player).gameAreaStart; line--){
198
                lines[line] = 0x0000;
199
                drawGameArea(player);
200
        }
201
        (*player).score = 0;
202
        setNewSprite((sprite_t*)&((*player).sprite));
203
}
204

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

    
214
static inline void drawGameArea(player_t* player){ // TODO needs changes for real multiplayer
215
        for(uint8_t round = 0; round < 16; round++){
216
                for(int16_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
217
                        printLine(line, round, (sprite_t*)&((*player).sprite));
218
                }
219
                drawScore((uint16_t*)&((*player).score), round);
220
                drawScore((uint16_t*)&((*player).score), round); // don't mirror the score, so it's easier to compare
221
                for(int16_t line = (*player).gameAreaEnd; line >= (*player).gameAreaStart; line--){
222
                        printLine(line, 15-round, (sprite_t*)&((*player).sprite));
223
                }
224
                OE();
225
                _delay_us(80);
226
                ODE();
227
                _delay_us(1); // avoid ghosting
228
                PORT(ADDRESS_PORT) = round+1;
229
        }
230
}
231

    
232
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite){
233
        if((1<<round) & lines[line]){
234
                yellowDot();
235
        }
236
        else{
237
                if((*sprite).yLowerPos <= line && (*sprite).yLowerPos+4 > line){
238
                        if((1<<round) & (*sprite).block[line-(*sprite).yLowerPos]){
239
                                greenDot();
240
                        }
241
                        else{
242
                                noDot();
243
                        }
244
                }
245
                else{
246
                        noDot();
247
                }
248
        }
249
}
250

    
251
void handleGamepadInput(player_t* player, uint8_t input){
252
        switch(input){
253
                // move > rotate > down
254
                case (LEFT | DOWN | RIGHT | ROTATE):
255
                case (DOWN | ROTATE):
256
                        rotateLeft((sprite_t*)&((*player).sprite));
257
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
258
                        moveSpriteDown((sprite_t*)&((*player).sprite));
259
                        keypressCounter+=2;
260
                        break;
261
                case (DOWN):
262
                case (LEFT | DOWN | RIGHT):
263
                        moveSpriteDown((sprite_t*)&((*player).sprite));
264
                        keypressCounter+=1;
265
                        break;
266
                case (RIGHT):
267
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
268
                        keypressCounter+=1;
269
                        break;
270
                case (RIGHT | ROTATE):
271
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
272
                        keypressCounter+=1;
273
                case (ROTATE):
274
                case (LEFT | RIGHT | ROTATE):
275
                        rotateLeft((sprite_t*)&((*player).sprite));
276
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
277
                        keypressCounter+=1;
278
                        break;
279
                case (DOWN | RIGHT):
280
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
281
                        moveSpriteDown((sprite_t*)&((*player).sprite));
282
                        keypressCounter+=2;
283
                        break;
284
                case (DOWN | RIGHT | ROTATE):
285
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
286
                        rotateLeft((sprite_t*)&((*player).sprite));
287
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
288
                        moveSpriteDown((sprite_t*)&((*player).sprite));
289
                        keypressCounter+=3;
290
                        break;
291
                case (LEFT):
292
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
293
                        keypressCounter+=1;
294
                        break;
295
                case (LEFT | ROTATE):
296
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
297
                        rotateLeft((sprite_t*)&((*player).sprite));
298
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
299
                        keypressCounter+=2;
300
                        break;
301
                case (LEFT | DOWN):
302
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
303
                        moveSpriteDown((sprite_t*)&((*player).sprite));
304
                        keypressCounter+=2;
305
                        break;
306
                case (LEFT | DOWN | ROTATE):
307
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
308
                        rotateLeft((sprite_t*)&((*player).sprite));
309
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
310
                        moveSpriteDown((sprite_t*)&((*player).sprite));
311
                        keypressCounter+=3;
312
                        break;
313
                // "nops"
314
                case (LEFT | RIGHT): // left+right = no move
315
                        keypressCounter+=2;
316
                case (0b0000): // nothing pressed
317
                        break;
318
        }
319
}
320

    
321
void rawToBlock(uint16_t block[4], uint16_t raw, int8_t offset){
322
        block[0] = raw & 0xf;
323
        block[1] = (raw>>4) & 0xf;
324
        block[2] = (raw>>8) & 0xf;
325
        block[3] = (raw>>12) & 0xf;
326

    
327
        if(offset > 0){
328
                for(uint8_t i = 0; i < 4; i++){
329
                        block[i] = block[i]<<offset;
330
                }
331
        }
332
        else if(offset < 0){
333
                for(uint8_t i = 0; i < 4; i++){
334
                        block[i] = block[i]>>(offset*-1);
335
                }
336
        }
337
}
338

    
339
void clearCompleteLines(player_t* player){
340
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
341
                if(lines[line] == 0xffff){
342
                        lines[line] = 0x00;
343
                        (*player).score++;
344
                }
345
        }
346
}
347

    
348
void moveFlyingRowsDown(player_t* player){
349
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
350
                if(lines[line] == 0x0000){ // TODO maybe add clear effect with green line?
351
                        lines[line] = lines[line+1];
352
                        // blank line to propagate movement
353
                        lines[line+1] = 0x0000;
354
                }
355
        }
356
}
357

    
358
void moveSpriteToLines(sprite_t* sprite){
359
        for(uint8_t j = 0; j < 4; j++){ // copy sprite to lines
360
                lines[(*sprite).yLowerPos+j] |= (*sprite).block[j];
361
        }
362
}
363

    
364
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead){
365
        for(int8_t i = 0; i < 4; i++){
366
                if((*player).sprite.yLowerPos+lookahead+i < (*player).gameAreaStart){
367
                        if((*player).sprite.block[i] == 0x0000){
368
                                continue;
369
                        }
370
                        else{
371
                                return 1;
372
                        }
373
                }
374
                else if((lines[(*player).sprite.yLowerPos+lookahead+i] & block[i]) != 0){ // collision detected
375
                        return 1;
376
                }
377
        }
378
        return 0;
379
}
380

    
381
void moveSpriteDown(sprite_t* sprite){
382
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, (uint16_t*)(*sprite).block, -1)){
383
                if((*sprite).yLowerPos >= (*(*sprite).owner).gameAreaEnd-4){
384
                        gameOver((player_t*)(*sprite).owner);
385
                        return;
386
                }
387
                moveSpriteToLines(sprite);
388
                setNewSprite(sprite);
389
                return;
390
        }
391
        (*sprite).yLowerPos--;
392
        if((*sprite).yLowerPos <= 0 && ((*sprite).block[-1*(*sprite).yLowerPos] != 0x00)){
393
                moveSpriteToLines(sprite);
394
                setNewSprite(sprite);
395
        }
396
}
397

    
398

    
399
void moveSpriteHorizontal(volatile sprite_t *sprite, int8_t dir){ // -1 if left, 1 if right
400
        uint16_t block[4];
401
        if(dir == 1){
402
                // check if block[*] >= 0x8000, so we cant move left
403
                for(uint8_t i = 0; i < 4; i++){
404
                        if((*sprite).block[i] >= (1<<15)){
405
                                return;
406
                        }
407
                }
408
                for(uint8_t i = 0; i < 4; i++){
409
                        block[i] = (*sprite).block[i] << 1;
410
                }
411
        }
412
        else{
413
                // check if block[*] is uneven, so we cant move right
414
                for(uint8_t i = 0; i < 4; i++){
415
                        if((*sprite).block[i] % 2){
416
                                return;
417
                        }
418
                }
419
                for(uint8_t i = 0; i < 4; i++){
420
                        block[i] = (*sprite).block[i] >> 1;
421
                }
422
        }
423
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
424
                return;
425
        }
426
        else{
427
                for(uint8_t i = 0; i < 4; i++){
428
                        (*sprite).block[i] = block[i];
429
                }
430
                (*sprite).offset = (*sprite).offset+dir;
431
        }
432
}
433

    
434
void loopLines(){
435
        uint8_t tmp = lines[0];
436
        for(uint8_t line = 0; line < 89 ; line++){
437
                lines[line] = lines[line+1];
438
        }
439
        lines[89] = tmp;
440
}
441

    
442
inline void greenDot(){
443
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN);
444
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN));
445
}
446
inline void redDot(){
447
        PORT(WALLBOARD_PORT) |= (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
448
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
449
}
450
inline void yellowDot(){
451
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
452
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
453
}
454
inline void noDot(){
455
        PORT(WALLBOARD_PORT) |= (1<<CLOCK_PIN);
456
        PORT(WALLBOARD_PORT) &= ~(1<<CLOCK_PIN);
457
}
458

    
459
inline void OE(){
460
        PORT(WALLBOARD_PORT) |= (1<<OUTPUT_ENABLE_PIN);
461
}
462

    
463
inline void ODE(){
464
        PORT(WALLBOARD_PORT) &= ~(1<<OUTPUT_ENABLE_PIN);
465
}
466

    
467
void rotateLeft(sprite_t* sprite){
468
        uint16_t block[4];
469
        uint16_t tmp = (*sprite).raw;
470

    
471
        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) );
472

    
473
        rawToBlock(block, tmp, (*sprite).offset);
474

    
475
        // check if sprite violates borders // TODO needs to be updated if game area doesn't span the whole line
476
        for(uint8_t i = 0; i < 4; i++){
477
                if((*sprite).offset < 0){
478
                        if(((block[i]<<((*sprite).offset*-1)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0){
479
                                return;
480
                        }
481
                }
482
                else if((*sprite).offset > 0){
483
                        if(((block[i]>>((*sprite).offset)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0){
484
                                return;
485
                        }
486
                }
487
                else{
488
                        break;
489
                }
490
        }
491

    
492
        // check first if rotated sprite crashes, if, drop to game
493
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
494
                return;
495
        }
496
        else{
497
                (*sprite).raw = tmp;
498
                for(uint8_t i = 0; i < 4; i++){
499
                        (*sprite).block[i] = block[i];
500
                }
501
        }
502
        // TODO rotate with ifs
503
//        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);
504
}