Statistics
| Branch: | Revision:

root / main.c @ f437a169

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

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

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

    
32
// note that both controllers are on the same port, the reason is
33
// controllerOffset for faster input handling
34
#define CONTROLLER0_PORT   D
35
#define CONTROLLER0_LEFT   PD7
36
#define CONTROLLER0_RIGHT  PD6
37
#define CONTROLLER0_DOWN   PD5
38
#define CONTROLLER0_ROTATE PD4
39
#define CONTROLLER1_PORT   D
40
#define CONTROLLER1_LEFT   PD3
41
#define CONTROLLER1_RIGHT  PD2
42
#define CONTROLLER1_DOWN   PD1
43
#define CONTROLLER1_ROTATE PD0
44

    
45
// control mappings for atari joystick
46
#define LEFT   0b0001
47
#define DOWN   0b0100
48
#define RIGHT  0b0010
49
#define ROTATE 0b1000
50

    
51
// note NOGAMEAREA needs has to be one pixel bigger
52
// because we are lazy in moveSpriteHorizontal (forward check)
53
#define NOGAMEAREA 0b111111111
54
#define TICKS 40
55
#define NUMBER_OF_PLAYERS 2
56

    
57
struct player;
58

    
59
typedef struct sprite {
60
        volatile int8_t offset;
61
        volatile int8_t yLowerPos;
62
        volatile uint16_t block[4];
63
        volatile uint16_t raw;
64
        volatile struct player *owner;
65
        } sprite_t;
66

    
67
typedef struct player {
68
        volatile uint16_t score;
69
        volatile int8_t gameAreaStart;
70
        volatile int8_t gameAreaEnd;
71
        volatile sprite_t sprite;
72
        volatile uint8_t mirrored;
73
        volatile uint8_t controllerOffset;
74
        volatile uint8_t controllerInputMask;
75
        } player_t;
76

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

    
100
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 };
101
volatile player_t player[NUMBER_OF_PLAYERS];
102

    
103
volatile uint8_t keypressCounter = 0;
104

    
105
int main(){
106
        // hardware init
107
        // clock, data and output enable
108
        DDR(WALLBOARD_PORT) |= (1<<CLOCK_PIN) | (1<<DATA_RED_PIN) | (1<<DATA_GREEN_PIN) | (1<<OUTPUT_ENABLE_PIN);
109
        // gamepad (PD7 is for bootloadstart):
110
        // 7...4 left down right rotate
111
        //
112
        // move > rotate > down
113
        DDR(CONTROLLER0_PORT) &= ~((1<<CONTROLLER0_LEFT) | (1<<CONTROLLER0_RIGHT) | (1<<CONTROLLER0_DOWN) | (1<<CONTROLLER0_ROTATE));
114
        PORT(CONTROLLER0_PORT) |= (1<<CONTROLLER0_LEFT) | (1<<CONTROLLER0_RIGHT) | (1<<CONTROLLER0_DOWN) | (1<<CONTROLLER0_ROTATE);
115
        DDR(CONTROLLER1_PORT) &= ~((1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_ROTATE));
116
        PORT(CONTROLLER1_PORT) |= (1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_ROTATE);
117
        // address lines:
118
        DDR(ADDRESS_PORT) |= (1<<A3) | (1<<A2) | (1<<A1) | (1<<A0);
119

    
120
        // init player
121
        player[0].score = 0;
122
        player[0].gameAreaStart = 0;
123
        player[0].gameAreaEnd = 43;
124
        setNewSprite((sprite_t*)&(player[0].sprite));
125
        player[0].sprite.owner = (player_t*)&player[0];
126
        player[0].mirrored = 0;
127
        player[0].controllerInputMask = (1<<CONTROLLER0_LEFT) | (1<<CONTROLLER0_DOWN) | (1<<CONTROLLER0_RIGHT) | (1<<CONTROLLER0_ROTATE);
128
        player[0].controllerOffset = 4;
129
        // next one
130
        player[1].score = 0;
131
        player[1].gameAreaStart = 46;
132
        player[1].gameAreaEnd = 89;
133
        setNewSprite((sprite_t*)&(player[1].sprite));
134
        player[1].sprite.owner = (player_t*)&player[1];
135
        player[1].mirrored = 1;
136
        player[1].controllerInputMask = (1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_ROTATE);
137
        player[1].controllerOffset = 0;
138
        while(1){
139
                for(uint8_t delay = 0; delay < TICKS; delay++){
140
                        drawGameArea((player_t*)player);
141
                        if(delay%(TICKS/(NUMBER_OF_PLAYERS)-1) == 0){
142
                                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){ //fair handing of input
143
                                        handleGamepadInput((player_t*)&player[playerNumber],
144
                                                                ((~PIN(CONTROLLER0_PORT))& player[playerNumber].controllerInputMask)
145
                                                                        >>player[playerNumber].controllerOffset);
146
                                }
147
                        }
148
                }
149
                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
150
                        moveSpriteDown((sprite_t*)&(player[playerNumber].sprite));
151
                        clearCompleteLines((player_t*)&player[playerNumber]);
152
                        moveFlyingRowsDown((player_t*)&player[playerNumber]);
153
                }
154
        }
155
}
156

    
157
void setNewSprite(sprite_t* sprite){
158
        (*sprite).yLowerPos = (*((*sprite).owner)).gameAreaEnd-2;
159
        (*sprite).raw = pgm_read_word(bricks+(keypressCounter%(sizeof(bricks)/sizeof(uint16_t))));
160
        (*sprite).offset = 10;
161
        rawToBlock((uint16_t*)(*sprite).block, (*sprite).raw, (*sprite).offset);
162
        keypressCounter++;
163
}
164

    
165
void gameOver(player_t* player){
166
        for(int16_t line = (*player).gameAreaEnd; line >= (*player).gameAreaStart; line--){
167
                lines[line] = 0x0000;
168
        }
169
        (*player).score = 0;
170
        setNewSprite((sprite_t*)&((*player).sprite));
171
}
172

    
173
void drawScore(uint16_t* score, uint8_t round){
174
        if((*score) & (1<<round)){
175
                redDot();
176
        }
177
        else{
178
                noDot();
179
        }
180
}
181

    
182
static inline void drawGameArea(player_t player[NUMBER_OF_PLAYERS]){
183
        for(uint8_t round = 0; round < 16; round++){
184
                for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
185
                        if(player[playerNumber].mirrored){
186
                                drawScore((uint16_t*)&(player[playerNumber].score), round); // don't mirror the score, so it's easier to compare
187
                                for(int16_t line = player[playerNumber].gameAreaEnd; line >= player[playerNumber].gameAreaStart; line--){
188
                                        printLine(line, 15-round, (sprite_t*)&(player[playerNumber].sprite));
189
                                }
190
                        }
191
                        else{
192
                                for(int16_t line = player[playerNumber].gameAreaStart; line <= player[playerNumber].gameAreaEnd; line++){
193
                                        printLine(line, round, (sprite_t*)&(player[playerNumber].sprite));
194
                                }
195
                                drawScore((uint16_t*)&(player[playerNumber].score), round);
196
                        }
197
                }
198
                OE();
199
                _delay_us(80);
200
                ODE();
201
                _delay_us(1); // avoid ghosting
202
                PORT(ADDRESS_PORT) = round+1;
203
        }
204
}
205

    
206
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite){
207
        if((1<<round) & lines[line]){
208
                yellowDot();
209
        }
210
        else{
211
                if((*sprite).yLowerPos <= line && (*sprite).yLowerPos+4 > line){
212
                        if((1<<round) & (*sprite).block[line-(*sprite).yLowerPos]){
213
                                greenDot();
214
                        }
215
                        else{
216
                                noDot();
217
                        }
218
                }
219
                else{
220
                        noDot();
221
                }
222
        }
223
}
224

    
225
void handleGamepadInput(player_t* player, uint8_t input){
226
        switch(input){
227
                // move > rotate > down
228
                case (LEFT | DOWN | RIGHT | ROTATE):
229
                        keypressCounter+=2;
230
                case (DOWN | ROTATE):
231
                        rotateLeft((sprite_t*)&((*player).sprite));
232
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
233
                        moveSpriteDown((sprite_t*)&((*player).sprite));
234
                        keypressCounter+=2;
235
                        break;
236
                case (LEFT | DOWN | RIGHT):
237
                        keypressCounter+=2;
238
                case (DOWN):
239
                        moveSpriteDown((sprite_t*)&((*player).sprite));
240
                        keypressCounter+=1;
241
                        break;
242
                case (RIGHT):
243
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
244
                        keypressCounter+=1;
245
                        break;
246
                case (LEFT | RIGHT | ROTATE):
247
                        keypressCounter+=1;
248
                case (RIGHT | ROTATE):
249
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
250
                        keypressCounter+=1;
251
                case (ROTATE):
252
                        rotateLeft((sprite_t*)&((*player).sprite));
253
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
254
                        keypressCounter+=1;
255
                        break;
256
                case (DOWN | RIGHT):
257
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
258
                        moveSpriteDown((sprite_t*)&((*player).sprite));
259
                        keypressCounter+=2;
260
                        break;
261
                case (DOWN | RIGHT | ROTATE):
262
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
263
                        rotateLeft((sprite_t*)&((*player).sprite));
264
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
265
                        moveSpriteDown((sprite_t*)&((*player).sprite));
266
                        keypressCounter+=3;
267
                        break;
268
                case (LEFT):
269
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
270
                        keypressCounter+=1;
271
                        break;
272
                case (LEFT | ROTATE):
273
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
274
                        rotateLeft((sprite_t*)&((*player).sprite));
275
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
276
                        keypressCounter+=2;
277
                        break;
278
                case (LEFT | DOWN):
279
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
280
                        moveSpriteDown((sprite_t*)&((*player).sprite));
281
                        keypressCounter+=2;
282
                        break;
283
                case (LEFT | DOWN | ROTATE):
284
                        moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
285
                        rotateLeft((sprite_t*)&((*player).sprite));
286
                        rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
287
                        moveSpriteDown((sprite_t*)&((*player).sprite));
288
                        keypressCounter+=3;
289
                        break;
290
                // "nops"
291
                case (LEFT | RIGHT): // left+right = no move
292
                        keypressCounter+=2;
293
                case (0b0000): // nothing pressed
294
                        break;
295
        }
296
}
297

    
298
void rawToBlock(uint16_t block[4], uint16_t raw, int8_t offset){
299
        block[0] = raw & 0xf;
300
        block[1] = (raw>>4) & 0xf;
301
        block[2] = (raw>>8) & 0xf;
302
        block[3] = (raw>>12) & 0xf;
303

    
304
        if(offset > 0){
305
                for(uint8_t i = 0; i < 4; i++){
306
                        block[i] = block[i]<<offset;
307
                }
308
        }
309
        else if(offset < 0){
310
                for(uint8_t i = 0; i < 4; i++){
311
                        block[i] = block[i]>>(offset*-1);
312
                }
313
        }
314
}
315

    
316
void clearCompleteLines(player_t* player){
317
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd; line++){
318
                if((lines[line] & (0xffff & ~(NOGAMEAREA>>1))) == (0xffff & ~(NOGAMEAREA>>1))){
319
                        lines[line] ^= (0xffff & ~(NOGAMEAREA>>1));
320
                        (*player).score++;
321
                }
322
        }
323
}
324

    
325
void moveFlyingRowsDown(player_t* player){ // TODO needs fix becase we don't want to touch nogamearea
326
        for(uint8_t line = (*player).gameAreaStart; line <= (*player).gameAreaEnd-1; line++){
327
                if(lines[line] == 0x0000){ // TODO maybe add clear effect with green line?
328
                        lines[line] = lines[line+1];
329
                        // blank line to propagate movement
330
                        lines[line+1] = 0x0000;
331
                }
332
        }
333
}
334

    
335
void moveSpriteToLines(sprite_t* sprite){
336
        for(uint8_t j = 0; j < 4; j++){ // copy sprite to lines
337
                lines[(*sprite).yLowerPos+j] |= (*sprite).block[j];
338
        }
339
}
340

    
341
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead){
342
        for(int8_t i = 0; i < 4; i++){
343
                if((*player).sprite.yLowerPos+lookahead+i < (*player).gameAreaStart){
344
                        if((*player).sprite.block[i] == 0x0000){
345
                                continue;
346
                        }
347
                        else{
348
                                return 1;
349
                        }
350
                }
351
                else if((lines[(*player).sprite.yLowerPos+lookahead+i] & block[i]) != 0){ // collision detected
352
                        return 1;
353
                }
354
        }
355
        return 0;
356
}
357

    
358
void moveSpriteDown(sprite_t* sprite){
359
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, (uint16_t*)(*sprite).block, -1)){
360
                if((*sprite).yLowerPos >= (*(*sprite).owner).gameAreaEnd-4){
361
                        gameOver((player_t*)(*sprite).owner);
362
                        return;
363
                }
364
                moveSpriteToLines(sprite);
365
                setNewSprite(sprite);
366
                return;
367
        }
368
        (*sprite).yLowerPos--;
369
        if((*sprite).yLowerPos <= 0 && ((*sprite).block[-1*(*sprite).yLowerPos] != 0x00)){
370
                moveSpriteToLines(sprite);
371
                setNewSprite(sprite);
372
        }
373
}
374

    
375

    
376
void moveSpriteHorizontal(volatile sprite_t *sprite, int8_t dir){ // -1 if left, 1 if right
377
        uint16_t block[4];
378
        if(dir == 1){
379
                // check if block[*] >= 0x8000, so we cant move left
380
                for(uint8_t i = 0; i < 4; i++){
381
                        if((*sprite).block[i] >= (1<<15)){
382
                                return;
383
                        }
384
                }
385
                for(uint8_t i = 0; i < 4; i++){
386
                        block[i] = (*sprite).block[i] << 1;
387
                }
388
        }
389
        else{
390
                for(uint8_t i = 0; i < 4; i++){
391
                        if(((*sprite).block[i] & (NOGAMEAREA)) != 0x0000){
392
                                return;
393
                        }
394
                }
395
                for(uint8_t i = 0; i < 4; i++){
396
                        block[i] = (*sprite).block[i] >> 1;
397
                }
398
        }
399
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
400
                return;
401
        }
402
        else{
403
                for(uint8_t i = 0; i < 4; i++){
404
                        (*sprite).block[i] = block[i];
405
                }
406
                (*sprite).offset = (*sprite).offset+dir;
407
        }
408
}
409

    
410
void loopLines(){
411
        uint8_t tmp = lines[0];
412
        for(uint8_t line = 0; line < 89 ; line++){
413
                lines[line] = lines[line+1];
414
        }
415
        lines[89] = tmp;
416
}
417

    
418
inline void greenDot(){
419
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN);
420
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<CLOCK_PIN));
421
}
422
inline void redDot(){
423
        PORT(WALLBOARD_PORT) |= (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
424
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
425
}
426
inline void yellowDot(){
427
        PORT(WALLBOARD_PORT) |= (1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN);
428
        PORT(WALLBOARD_PORT) &= ~((1<<DATA_GREEN_PIN) | (1<<DATA_RED_PIN) | (1<<CLOCK_PIN));
429
}
430
inline void noDot(){
431
        PORT(WALLBOARD_PORT) |= (1<<CLOCK_PIN);
432
        PORT(WALLBOARD_PORT) &= ~(1<<CLOCK_PIN);
433
}
434

    
435
inline void OE(){
436
        PORT(WALLBOARD_PORT) |= (1<<OUTPUT_ENABLE_PIN);
437
}
438

    
439
inline void ODE(){
440
        PORT(WALLBOARD_PORT) &= ~(1<<OUTPUT_ENABLE_PIN);
441
}
442

    
443
void rotateLeft(sprite_t* sprite){
444
        uint16_t block[4];
445
        uint16_t tmp = (*sprite).raw;
446

    
447
        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) );
448

    
449
        rawToBlock(block, tmp, (*sprite).offset);
450

    
451
        // check if sprite violates borders // TODO needs to be updated if game area doesn't span the whole line
452
        for(uint8_t i = 0; i < 4; i++){
453
                if((*sprite).offset < 0){
454
                        if(((block[i]<<((*sprite).offset*-1)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0){
455
                                return;
456
                        }
457
                }
458
                else if((*sprite).offset > 0){
459
                        if((((block[i]>>((*sprite).offset)) ^ ((tmp&((0xf)<<(i*4)))>>(i*4))) != 0) || ((block[i] & (NOGAMEAREA>>1)) != 0x0000)){
460
                                return;
461
                        }
462
                }
463
                else{
464
                        break;
465
                }
466
        }
467

    
468
        // check first if rotated sprite crashes, if, drop to game
469
        if(checkPlayerSpriteForCollision((player_t*)(*sprite).owner, block, 0)){
470
                return;
471
        }
472
        else{
473
                (*sprite).raw = tmp;
474
                for(uint8_t i = 0; i < 4; i++){
475
                        (*sprite).block[i] = block[i];
476
                }
477
        }
478
        // TODO rotate with ifs
479
//        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);
480
}