Statistics
| Branch: | Revision:

root / main.c @ 69bc3638

History | View | Annotate | Download (13.6 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

    
23
#define CONTROLLER_PORT   D
24
#define CONTROLLER_LEFT   PD7
25
#define CONTROLLER_RIGHT  PD6
26
#define CONTROLLER_DOWN   PD5
27
#define CONTROLLER_ROTATE PD4
28

    
29
// control mappings for atari joystick
30
#define LEFT   0b0001
31
#define DOWN   0b1000
32
#define RIGHT  0b0010
33
#define ROTATE 0b0100
34

    
35
#define TICKS 40
36
#define NUMBER_OF_PLAYERS 1
37

    
38
struct player;
39

    
40
typedef struct sprite {
41
        volatile int8_t offset;
42
        volatile int8_t yLowerPos;
43
        volatile uint16_t block[4];
44
        volatile uint16_t raw;
45
        volatile struct player *owner;
46
        } sprite_t;
47

    
48
typedef struct player {
49
        volatile uint16_t score;
50
        volatile int8_t gameAreaStart;
51
        volatile int8_t gameAreaEnd;
52
        volatile sprite_t sprite;
53
        volatile uint8_t mirrored;
54
        } player_t;
55

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

    
79
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 };
80
volatile player_t player[NUMBER_OF_PLAYERS];
81

    
82
const uint16_t bricks[] PROGMEM = { 
83
        /* +----+
84
         * |    |
85
         * | ## |
86
         * | ## |
87
         * |    |
88
         * +----+ */
89
        0b0000011001100000, 
90

    
91
        /* +----+
92
         * |    |
93
         * | #  |
94
         * | ## |
95
         * |  # |
96
         * +----+ */
97
        0b0000010001100010,
98

    
99
        /* +----+
100
         * |    |
101
         * |  # |
102
         * | ## |
103
         * | #  |
104
         * +----+ */
105
        0b0000001001100100, 
106

    
107
        /* +----+
108
         * |    |
109
         * | #  |
110
         * | ## |
111
         * | #  |
112
         * +----+ */
113
        0b0000010001100100, 
114

    
115
        /* +----+
116
         * | #  |
117
         * | #  |
118
         * | #  |
119
         * | #  |
120
         * +----+ */
121
        0b0100010001000100, 
122

    
123
        /* +----+
124
         * |    |
125
         * | #  |
126
         * | #  |
127
         * | ## |
128
         * +----+ */
129
        0b0000010001000110, 
130

    
131
        /* +----+
132
         * |    |
133
         * |  # |
134
         * |  # |
135
         * | ## |
136
         * +----+ */
137
        0b0000001000100110
138
};
139
volatile uint8_t keypressCounter = 0;
140

    
141
int main(){
142
        // hardware init
143
        // clock, data and output enable
144
        DDR(WALLBOARD_PORT) = (1<<CLOCK_PIN) | (1<<DATA_RED_PIN) | (1<<DATA_GREEN_PIN) | (1<<OUTPUT_ENABLE_PIN);
145
        // gamepad (PD7 is for bootloadstart):
146
        // 7...4 left down right rotate
147
        //
148
        // move > rotate > down
149
        DDR(CONTROLLER_PORT) &= ~((1<<CONTROLLER_LEFT) | (1<<CONTROLLER_RIGHT) | (1<<CONTROLLER_DOWN) | (1<<CONTROLLER_ROTATE));
150
        PORT(CONTROLLER_PORT) |= (1<<CONTROLLER_LEFT) | (1<<CONTROLLER_RIGHT) | (1<<CONTROLLER_DOWN) | (1<<CONTROLLER_ROTATE);
151
        // address lines:
152
        DDR(C) = 0xff;
153

    
154
        // init player
155
        player[0].score = 0;
156
        player[0].gameAreaStart = 0;
157
        player[0].gameAreaEnd = 43;
158
        player[0].sprite.owner = (player_t*)&player[0];
159
        player[0].mirrored = 0;
160
        setNewSprite((sprite_t*)&(player[0].sprite));
161

    
162
        while(1){
163
                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
164
                        for(uint8_t delay = 0; delay < TICKS; delay++){
165
                                drawGameArea((player_t*)&player[playerNumber]);
166
                                if(delay%20 == 0){
167
                                        handleGamepadInput((player_t*)&player[playerNumber], ((~PIN(D))&(0b11110000))>>4);
168
                                }
169
                        }
170
                        moveSpriteDown((sprite_t*)&(player[playerNumber].sprite));
171
                        clearCompleteLines((player_t*)&player[playerNumber]);
172
                        moveFlyingRowsDown((player_t*)&player[playerNumber]);
173
                }
174
        }
175
}
176

    
177
void setNewSprite(sprite_t* sprite){
178
        (*sprite).yLowerPos = (*((*sprite).owner)).gameAreaEnd-2;
179
        (*sprite).raw = pgm_read_word(bricks+(keypressCounter%(sizeof(bricks)/sizeof(uint16_t))));
180
        (*sprite).offset = 6;
181
        rawToBlock((uint16_t*)(*sprite).block, (*sprite).raw, (*sprite).offset);
182
        keypressCounter++;
183
}
184

    
185
void gameOver(player_t* player){ // TODO FIXME
186
        for(int16_t line = (*player).gameAreaStart; line < (*player).gameAreaEnd; line++){
187
                lines[line] = 0xffff;
188
                drawGameArea(player);
189
        }
190
        for(int16_t line = (*player).gameAreaEnd-1; line >= (*player).gameAreaStart; line--){
191
                lines[line] = 0x0000;
192
                drawGameArea(player);
193
        }
194
        (*player).score = 0;
195
        setNewSprite((sprite_t*)&((*player).sprite));
196
}
197

    
198
void drawScore(uint16_t* score, uint8_t round){
199
        if((*score) & (1<<round)){
200
                redDot();
201
        }
202
        else{
203
                noDot();
204
        }
205
}
206

    
207
static inline void drawGameArea(player_t* player){ // TODO needs changes for real multiplayer
208
        for(uint8_t round = 0; round < 16; round++){
209
                for(int16_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
210
                        printLine(line, round, (sprite_t*)&((*player).sprite));
211
                }
212
                drawScore((uint16_t*)&((*player).score), round);
213
                drawScore((uint16_t*)&((*player).score), round); // don't mirror the score, so it's easier to compare
214
                for(int16_t line = (*player).gameAreaEnd; line >= (*player).gameAreaStart; line--){
215
                        printLine(line, 15-round, (sprite_t*)&((*player).sprite));
216
                }
217
                OE();
218
                _delay_us(80);
219
                ODE();
220
                PORT(C) = round+1;
221
        }
222
}
223

    
224
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite){
225
        if((1<<round) & lines[line]){
226
                yellowDot();
227
        }
228
        else{
229
                if((*sprite).yLowerPos <= line && (*sprite).yLowerPos+4 > line){
230
                        if((1<<round) & (*sprite).block[line-(*sprite).yLowerPos]){
231
                                greenDot();
232
                        }
233
                        else{
234
                                noDot();
235
                        }
236
                }
237
                else{
238
                        noDot();
239
                }
240
        }
241
}
242

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

    
313
void rawToBlock(uint16_t block[4], uint16_t raw, int8_t offset){
314
        block[0] = raw & 0xf;
315
        block[1] = (raw>>4) & 0xf;
316
        block[2] = (raw>>8) & 0xf;
317
        block[3] = (raw>>12) & 0xf;
318

    
319
        if(offset > 0){
320
                for(uint8_t i = 0; i < 4; i++){
321
                        block[i] = block[i]<<offset;
322
                }
323
        }
324
        else if(offset < 0){
325
                for(uint8_t i = 0; i < 4; i++){
326
                        block[i] = block[i]>>(offset*-1);
327
                }
328
        }
329
}
330

    
331
void clearCompleteLines(player_t* player){
332
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
333
                if(lines[line] == 0xffff){
334
                        lines[line] = 0x00;
335
                        (*player).score++;
336
                }
337
        }
338
}
339

    
340
void moveFlyingRowsDown(player_t* player){
341
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
342
                if(lines[line] == 0x0000){ // TODO maybe add clear effect with green line?
343
                        lines[line] = lines[line+1];
344
                        // blank line to propagate movement
345
                        lines[line+1] = 0x0000;
346
                }
347
        }
348
}
349

    
350
void moveSpriteToLines(sprite_t* sprite){
351
        for(uint8_t j = 0; j < 4; j++){ // copy sprite to lines
352
                lines[(*sprite).yLowerPos+j] |= (*sprite).block[j];
353
        }
354
}
355

    
356
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead){
357
        for(int8_t i = 0; i < 4; i++){
358
                if((*player).sprite.yLowerPos+lookahead+i < (*player).gameAreaStart){
359
                        if((*player).sprite.block[i] == 0x0000){
360
                                continue;
361
                        }
362
                        else{
363
                                return 1;
364
                        }
365
                }
366
                else if((lines[(*player).sprite.yLowerPos+lookahead+i] & block[i]) != 0){ // collision detected
367
                        return 1;
368
                }
369
        }
370
        return 0;
371
}
372

    
373
void moveSpriteDown(sprite_t* sprite){
374
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, (uint16_t*)(*sprite).block, -1)){
375
                if((*sprite).yLowerPos >= (*(*sprite).owner).gameAreaEnd-4){
376
                        gameOver((player_t*)(*sprite).owner);
377
                        return;
378
                }
379
                moveSpriteToLines(sprite);
380
                setNewSprite(sprite);
381
                return;
382
        }
383
        (*sprite).yLowerPos--;
384
        if((*sprite).yLowerPos <= 0 && ((*sprite).block[-1*(*sprite).yLowerPos] != 0x00)){
385
                moveSpriteToLines(sprite);
386
                setNewSprite(sprite);
387
        }
388
}
389

    
390

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

    
426
void loopLines(){
427
        uint8_t tmp = lines[0];
428
        for(uint8_t line = 0; line < 89 ; line++){
429
                lines[line] = lines[line+1];
430
        }
431
        lines[89] = tmp;
432
}
433

    
434
inline void greenDot(){
435
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN);
436
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN));
437
}
438
inline void redDot(){
439
        PORT(WALLBOARD_PORT) |= (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
440
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
441
}
442
inline void yellowDot(){
443
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
444
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
445
}
446
inline void noDot(){
447
        PORT(WALLBOARD_PORT) |= (1<<CLOCK_PIN);
448
        PORT(WALLBOARD_PORT) &= ~(1<<CLOCK_PIN);
449
}
450

    
451
inline void OE(){
452
        PORT(WALLBOARD_PORT) |= (1<<OUTPUT_ENABLE_PIN);
453
}
454

    
455
inline void ODE(){
456
        PORT(WALLBOARD_PORT) &= ~(1<<OUTPUT_ENABLE_PIN);
457
}
458

    
459
void rotateLeft(sprite_t* sprite){
460
        uint16_t block[4];
461
        uint16_t tmp = (*sprite).raw;
462

    
463
        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) );
464

    
465
        rawToBlock(block, tmp, (*sprite).offset);
466

    
467
        // check if sprite violates borders
468
        for(uint8_t i = 0; i < 4; i++){
469
                if((*sprite).offset < 0){
470
                        if(((block[i]<<((*sprite).offset*-1)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0){
471
                                return;
472
                        }
473
                }
474
                else if((*sprite).offset > 0){
475
                        if(((block[i]>>((*sprite).offset)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0){
476
                                return;
477
                        }
478
                }
479
        }
480

    
481
        // check first if rotated sprite crashes, if, drop to game
482
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
483
                return;
484
        }
485
        else{
486
                (*sprite).raw = tmp;
487
                for(uint8_t i = 0; i < 4; i++){
488
                        (*sprite).block[i] = block[i];
489
                }
490
        }
491
        // TODO rotate with ifs
492
//        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);
493
}