1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <avr/signal.h>
|
4 |
|
5 | #include "font.h"
|
6 |
|
7 | int pong_mode;
|
8 |
|
9 | /***************************************\
|
10 | * Ports:
|
11 | * A0-6: Dot matrix rows
|
12 | * D0-4: Dot matrix columns
|
13 | * B0: Player 2 UP
|
14 | * B1: Player 2 DOWN
|
15 | * B2: Player 1 UP
|
16 | * B3: Player 1 DOWN
|
17 | * B4: Player 2 score LED
|
18 | * B5: Player 1 score LED
|
19 | \***************************************/
|
20 |
|
21 | /***************************************\
|
22 | * Framebuffer(5x7 dot matrix):
|
23 | * 6 x x x x O
|
24 | * x x x x O <- p2_pos = 5
|
25 | * x x x x x
|
26 | * x x x x x
|
27 | * x x x x x
|
28 | * x x x x O
|
29 | * 0 x x x x O <- p2_pos = 0
|
30 | * 0 4
|
31 | \***************************************/
|
32 |
|
33 | volatile unsigned char framebuffer[5];
|
34 | volatile unsigned char framebuffer_pos = 0x00;
|
35 |
|
36 | volatile char ball_x;
|
37 | volatile char ball_y;
|
38 | volatile char ball_dx;
|
39 | volatile char ball_dy;
|
40 | volatile char p1_pos; /* Position of player 1 paddle */
|
41 | volatile char p2_pos; /* Position of player 2 paddle */
|
42 | volatile char ballClock; /* Move ball only every to tick */
|
43 | volatile char redraw_needed;
|
44 | volatile char speed_counter; /* increase speed as game goes on */
|
45 | const char speed_bumper = 10; /* increase speed after this many movement_timer ticks */
|
46 | volatile char current_speed;
|
47 | volatile char score;
|
48 | const unsigned char d1[] = {0x00, 0x10, 0x20, 0x7f, 0x00}; /* the number 1 */
|
49 | const unsigned char d2[] = {0x47, 0x49, 0x49, 0x49, 0x31}; /* the number 2 */
|
50 |
|
51 |
|
52 | #define BV(x) _BV(x)
|
53 | #define outw(a, b) (a = (b))
|
54 | #define outp(a, b) (b = (a))
|
55 | #define inp(a) (a)
|
56 |
|
57 |
|
58 | /* Draw paddles and ball */
|
59 | void drawScene()
|
60 | {
|
61 | framebuffer[0] = BV(p1_pos) | BV(p1_pos+1);
|
62 | framebuffer[1] = 0;
|
63 | framebuffer[2] = 0;
|
64 | framebuffer[3] = 0;
|
65 | framebuffer[4] = BV(p2_pos) | BV(p2_pos+1);
|
66 | framebuffer[(unsigned char)ball_x] |= BV(ball_y);
|
67 | }
|
68 |
|
69 | void variable_init(void)
|
70 | {
|
71 | ball_x = 2;
|
72 | ball_y = 3;
|
73 | ball_dx = 1;
|
74 | ball_dy = 0;
|
75 | p1_pos = 2;
|
76 | p2_pos = 2;
|
77 | ballClock = 0;
|
78 | redraw_needed = 1;
|
79 | speed_counter = 0;
|
80 | current_speed = 3;
|
81 | outw(OCR1A,40000);
|
82 | }
|
83 |
|
84 | void ioinit(void)
|
85 | {
|
86 | /* Set port C and B0-3, B6-7, to internal pullup, B4-5 output */
|
87 | outp(0x30, DDRB);
|
88 | outp(0x00, DDRC);
|
89 | outp(0xcf, PORTB);
|
90 | outp(0xff, PORTC);
|
91 |
|
92 | /* Set port A and D to output */
|
93 | outp(0xff, DDRA);
|
94 | outp(0xff, DDRD);
|
95 |
|
96 | /* Enable 8-bit counter1, running on clk speed */
|
97 | outp(BV(CS01), TCCR0);
|
98 |
|
99 | /* Set OCR1A as max value and Enable 16-bit counter,
|
100 | * running on clk/8 speed */
|
101 | outp(BV(WGM12)|BV(CS11), TCCR1B);
|
102 |
|
103 | /* */
|
104 | outw(OCR1A,40000);
|
105 |
|
106 | /* Enable overlow when counter1 reaches OCR1A */
|
107 | /*outp(BV(TOV1), TIFR);*/
|
108 |
|
109 | /* Enable overflow interrupt for counter 0 and counter 1*/
|
110 | timer_enable_int(BV(TOIE0)|BV(OCIE1A));
|
111 |
|
112 | /* Enable interrupts */
|
113 | sei();
|
114 | }
|
115 |
|
116 | /* Switch leds to indicate who's leading */
|
117 | void update_score_output(void) {
|
118 | if (score == 1)
|
119 | {
|
120 | outp(0xef, PORTB); /* turn on led for player 1 */
|
121 | }
|
122 | else if(score == -1)
|
123 | {
|
124 | outp(0xdf, PORTB); /* turn on led for player 2 */
|
125 | }
|
126 | else
|
127 | {
|
128 | outp(0xcf, PORTB); /* turn off leds */
|
129 | }
|
130 | }
|
131 |
|
132 | void victory(void)
|
133 | {
|
134 | unsigned char buttons;
|
135 | int i;
|
136 | int j;
|
137 |
|
138 | /* turn off counter */
|
139 | outp(0x00, TCCR1B);
|
140 |
|
141 | /* turn on both leds */
|
142 | /* outp(0xff, PORTB); */
|
143 |
|
144 | /* Display winner */
|
145 | if (score == 1)
|
146 | {
|
147 | framebuffer[0] = d1[0];
|
148 | framebuffer[1] = d1[1];
|
149 | framebuffer[2] = d1[2];
|
150 | framebuffer[3] = d1[3];
|
151 | framebuffer[4] = d1[4];
|
152 | }
|
153 | else
|
154 | {
|
155 | framebuffer[0] = d2[0];
|
156 | framebuffer[1] = d2[1];
|
157 | framebuffer[2] = d2[2];
|
158 | framebuffer[3] = d2[3];
|
159 | framebuffer[4] = d2[4];
|
160 | }
|
161 | sei();
|
162 |
|
163 | /* wait a little */
|
164 | for (i = 0; i < 5000; i++)
|
165 | for (j = 0; j < 1000; j++) j=j;
|
166 | /* wait for buttonpress to start again */
|
167 | buttons = ~inp(PINB);
|
168 | while( (buttons & 0x0f) == 0x00 ) {
|
169 | unsigned char wait_count;
|
170 | for (wait_count = 0; wait_count < 0xff; ++wait_count);
|
171 | buttons = ~inp(PINB);
|
172 | }
|
173 |
|
174 | cli();
|
175 |
|
176 | /* Reinit */
|
177 | variable_init();
|
178 | ioinit();
|
179 | score = 0;
|
180 | speed_counter = 0;
|
181 | }
|
182 |
|
183 | /* Movement */
|
184 | void timer_movement(unsigned char portB)
|
185 | {
|
186 | unsigned char p1_up; /* Player 1 up button pressed */
|
187 | unsigned char p1_dn; /* Player 1 down button pressed */
|
188 | unsigned char p2_up; /* Player 2 up button pressed */
|
189 | unsigned char p2_dn; /* Player 2 down button pressed */
|
190 | char pos;
|
191 | char loose;
|
192 | char paddle_bounce;
|
193 | int i;
|
194 | int j;
|
195 |
|
196 | /* Read buttons */
|
197 | p1_up = portB & 0x04; /* Pin B2 */
|
198 | p1_dn = portB & 0x08; /* Pin B3 */
|
199 | p2_up = portB & 0x01; /* Pin B0 */
|
200 | p2_dn = portB & 0x02; /* Pin B1 */
|
201 |
|
202 | /* Stop paddle movement at wall */
|
203 | p1_up = (p1_up && p1_pos < 5);
|
204 | p1_dn = (p1_dn && p1_pos > 0);
|
205 | p2_up = (p2_up && p2_pos < 5);
|
206 | p2_dn = (p2_dn && p2_pos > 0);
|
207 |
|
208 | /* Move paddles */
|
209 | if (p1_up) p1_pos++;
|
210 | if (p1_dn) p1_pos--;
|
211 | if (p2_up) p2_pos++;
|
212 | if (p2_dn) p2_pos--;
|
213 |
|
214 | /* Move ball only every two clock tick */
|
215 | if ((ballClock = !ballClock))
|
216 | {
|
217 | /* Bounce ball on wall */
|
218 | if (ball_y == 0) ball_dy = -ball_dy;
|
219 | if (ball_y == 6) ball_dy = -ball_dy;
|
220 |
|
221 | /* Bounce ball on paddle */
|
222 | loose = 0;
|
223 | paddle_bounce = 0;
|
224 | pos = (ball_x == 1)?p1_pos:p2_pos;
|
225 | if (ball_x == 1 || ball_x == 3)
|
226 | {
|
227 | loose = 1;
|
228 | if (ball_y == pos || ball_y == pos+1)
|
229 | {
|
230 | ball_dx = -ball_dx;
|
231 | loose = 0;
|
232 | paddle_bounce = 1;
|
233 | }
|
234 | else if (ball_y == pos-1 && ball_dy == 1)
|
235 | {
|
236 | ball_dx = -ball_dx;
|
237 | ball_dy = -ball_dy;
|
238 | loose = 0;
|
239 | paddle_bounce = 1;
|
240 | }
|
241 | else if (ball_y == pos+2 && ball_dy == -1)
|
242 | {
|
243 | ball_dx = -ball_dx;
|
244 | ball_dy = -ball_dy;
|
245 | loose = 0;
|
246 | paddle_bounce = 1;
|
247 | }
|
248 |
|
249 | }
|
250 | if (loose)
|
251 | {
|
252 | if (ball_x == 1) /* player 2 won */
|
253 | {
|
254 | if (score == -1) /* total victory! */
|
255 | {
|
256 | victory();
|
257 | return;
|
258 | }
|
259 | else
|
260 | {
|
261 | score--;
|
262 | update_score_output();
|
263 |
|
264 | ball_x += ball_dx;
|
265 | ball_y += ball_dy;
|
266 | redraw_needed = 1;
|
267 |
|
268 | outp(0x00, TCCR1B); /* turn off counter */
|
269 | sei(); /* Re-enable interrupts */
|
270 | /* Wait a little */
|
271 | for (i = 0; i < 5000; i++)
|
272 | for (j = 0; j < 1000; j++) j=j;
|
273 | cli(); /* Disable interrupts again */
|
274 | outp(BV(WGM12)|BV(CS11), TCCR1B); /* turn on counter */
|
275 | }
|
276 | }
|
277 | else { /* player 1 won */
|
278 | if (score == 1) /* total victory! */
|
279 | {
|
280 | victory();
|
281 | return;
|
282 | }
|
283 | else
|
284 | {
|
285 | score++;
|
286 | update_score_output();
|
287 |
|
288 | ball_x += ball_dx;
|
289 | ball_y += ball_dy;
|
290 | redraw_needed = 1;
|
291 |
|
292 | outp(0x00, TCCR1B); /* turn off counter */
|
293 | sei(); /* Re-enable interrupts */
|
294 | /* Wait a little */
|
295 | for (i = 0; i < 5000; i++)
|
296 | for (j = 0; j < 1000; j++) j=j;
|
297 | cli(); /* Disable interrupts again */
|
298 | outp(BV(WGM12)|BV(CS11), TCCR1B); /* turn on counter */
|
299 | }
|
300 | }
|
301 | variable_init();
|
302 | update_score_output();
|
303 | }
|
304 |
|
305 | /* Change vertical speed of ball according to paddle speed */
|
306 | if (paddle_bounce)
|
307 | {
|
308 | if (ball_x == 1 && p1_up && ball_dy < 1) ball_dy++;
|
309 | if (ball_x == 1 && p1_dn && ball_dy > -1) ball_dy--;
|
310 | if (ball_x == 3 && p2_up && ball_dy < 1) ball_dy++;
|
311 | if (ball_x == 3 && p2_dn && ball_dy > -1) ball_dy--;
|
312 | }
|
313 |
|
314 | /* Wall bounce after paddle bounce */
|
315 | if (ball_y == 0 && ball_dy == -1) ball_dy = -ball_dy;
|
316 | if (ball_y == 6 && ball_dy == 1) ball_dy = -ball_dy;
|
317 |
|
318 | /* Move ball */
|
319 | ball_x += ball_dx;
|
320 | ball_y += ball_dy;
|
321 | }
|
322 |
|
323 | redraw_needed = 1;
|
324 | }
|
325 |
|
326 | /* Framebuffer output to led matrix */
|
327 | SIGNAL(SIG_OVERFLOW0)
|
328 | {
|
329 | if (++framebuffer_pos > 4) framebuffer_pos = 0;
|
330 |
|
331 | /* Change output to dot matrix */
|
332 | outp(0xff, PORTA); /* Avoid ghosting */
|
333 | outp(BV(framebuffer_pos), PORTD);
|
334 | outp(~framebuffer[framebuffer_pos], PORTA);
|
335 |
|
336 | if (!pong_mode) return;
|
337 | /* Redraw scene during "vertical retrace" */
|
338 | if (framebuffer_pos == 4 && redraw_needed)
|
339 | {
|
340 | drawScene();
|
341 | redraw_needed = 0;
|
342 | }
|
343 | }
|
344 |
|
345 | /* Movement */
|
346 | SIGNAL(SIG_OUTPUT_COMPARE1A)
|
347 | {
|
348 | if (!pong_mode) return;
|
349 | if (++speed_counter == speed_bumper) {
|
350 | speed_counter = 0;
|
351 | outw(OCR1A, (40000 / ++current_speed) * 3);
|
352 | if (current_speed > 100) current_speed = 100;
|
353 | }
|
354 |
|
355 | timer_movement(~inp(PINB));
|
356 | }
|
357 |
|
358 | int main_pong(void)
|
359 | {
|
360 | score = 0;
|
361 | variable_init();
|
362 | ioinit();
|
363 | for(;;);
|
364 | }
|
365 |
|
366 | int main_font(void)
|
367 | {
|
368 | unsigned char portB;
|
369 | unsigned char nextPressed;
|
370 | unsigned char prevPressed;
|
371 | int cf; /* Current font */
|
372 |
|
373 | nextPressed = 0;
|
374 | prevPressed = 0;
|
375 | cf = 0;
|
376 | framebuffer[0] = font[cf][0];
|
377 | framebuffer[1] = font[cf][1];
|
378 | framebuffer[2] = font[cf][2];
|
379 | framebuffer[3] = font[cf][3];
|
380 | framebuffer[4] = font[cf][4];
|
381 |
|
382 | ioinit();
|
383 |
|
384 | for(;;)
|
385 | {
|
386 | portB = ~inp(PINB);
|
387 |
|
388 | /* Prev button */
|
389 | if (portB & 0x01)
|
390 | {
|
391 | if (!prevPressed)
|
392 | {
|
393 | cf--;
|
394 | if (cf < 0) cf = 0;
|
395 | framebuffer[0] = font[cf][0];
|
396 | framebuffer[1] = font[cf][1];
|
397 | framebuffer[2] = font[cf][2];
|
398 | framebuffer[3] = font[cf][3];
|
399 | framebuffer[4] = font[cf][4];
|
400 | }
|
401 | prevPressed = 1;
|
402 | }
|
403 | else
|
404 | prevPressed = 0;
|
405 |
|
406 | /* Next button */
|
407 | if (portB & 0x02)
|
408 | {
|
409 | if (!nextPressed)
|
410 | {
|
411 | cf++;
|
412 | if (cf >= font_count) cf = font_count - 1;
|
413 | framebuffer[0] = font[cf][0];
|
414 | framebuffer[1] = font[cf][1];
|
415 | framebuffer[2] = font[cf][2];
|
416 | framebuffer[3] = font[cf][3];
|
417 | framebuffer[4] = font[cf][4];
|
418 | }
|
419 | nextPressed = 1;
|
420 | }
|
421 | else
|
422 | nextPressed = 0;
|
423 | }
|
424 | }
|
425 |
|
426 | int main(void)
|
427 | {
|
428 | unsigned char portB;
|
429 |
|
430 | framebuffer[0] = 0;
|
431 | framebuffer[1] = 0;
|
432 | framebuffer[2] = 0;
|
433 | framebuffer[3] = 0;
|
434 | framebuffer[4] = 0;
|
435 | pong_mode = 0;
|
436 | ioinit();
|
437 |
|
438 | for (;;)
|
439 | {
|
440 | portB = ~inp(PINB);
|
441 | if (portB & 0x01) {pong_mode = 1; main_pong();}
|
442 | if (portB & 0x02) {pong_mode = 0; main_font();}
|
443 | }
|
444 | }
|