//////////////////////////////////////////// // Joseph Kasnicki (jkk38) // // Colin Barth (csb42) // // ECE 476 Final Project // // Cornell Spring 2008 // // Uses video code written by Bruce Land // // // // Bogey Warnings: // // 1. Overflow possible in position // // calculation // // 2. unreferenced globabl variable pos // //////////////////////////////////////////// #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include #include "notes.c" #include "video_functions.c" //cycles = 63.625 * 16 Note NTSC is 63.55 //but this line duration makes each frame exactly 1/60 sec //which is nice for keeping a realtime clock #define lineTime 1018 #define ScreenTop 30 #define ScreenBot 230 //use definitions #define WIDTH 126 #define HEIGHT 99 #define TSTATUS 11 #define LSTATUS 13 //NOTE that v1 to v8 and i must be in registers! register char v1 @4; register char v2 @5; register char v3 @6; register char v4 @7; register char v5 @8; register char v6 @9; register char v7 @10; register char v8 @11; register int i @12; #pragma regalloc+ //stop allocating registers //video variables char syncON, syncOFF; int LineCount; int score; char screen[1600], t; //status variables #define DEFAULT_SPEED 1 //MUST be either 1, or 2 #define MAX_NOTES_TO_SCROLL 45 //more than is on screen, so no slowdown is visible #define SPAWN_LOC 118 //x distance notes spawn at, MUST be onscreen #define SPAWN_SPACE 14 //useful to have a default distance between notes #define LINES_TO_CLEAR 3 //speed at which to clear screen (MUST be < 5) unsigned char speed; unsigned char fullPresses, halfPresses, lastFullPresses, lastHalfPresses, changed, justCleared; unsigned char buttons, prevButtons; unsigned char readyForGame; unsigned char notePlayed; //score variables #define SCORE_PLUS 2 #define SCORE_MINUS 1 //menu location #define MENU_X 45 #define MENU_Y 20 //video buffers unsigned char buffer; #define CLEAR_SCREEN 0 #define DRAW_MENU 1 #define DRAW_GAME 2 #define DECODE_BUTTONS 3 #define SHIFT_LEFT 4 //Point plot lookup table //One bit masks //needed for video functions flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //SONGS #define FORMAT_LENGTH 4 #define NUM_SONGS 4 #define FREEPLAY 1 #define CSCALE 2 #define MARY 3 #define TITANIC 4 #define START_INDEX_FREE 0 #define END_INDEX_FREE -1 #define START_INDEX_CSCALE 0 #define END_INDEX_CSCALE 14 #define START_INDEX_MARY 15 #define END_INDEX_MARY 40 #define START_INDEX_TITANIC 41 #define END_INDEX_TITANIC 81 #define SONGS_LENGTH 82 //Each note is for entries in char array. Entries are: // 1) fullPress char for note // 2) halfPress char for note // 3) dummy place holder, could be anything, may be useful in future // 4) number of pixels to the next note //flash array to store all songs flash char songs[SONGS_LENGTH*FORMAT_LENGTH]= {//C Scale [0-14] Cbutf,Cbuth,1,SPAWN_SPACE,Dbutf,Dbuth,1,SPAWN_SPACE,Ebutf,Ebuth,1,SPAWN_SPACE, Fbutf,Fbuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE, Bbutf,Bbuth,1,SPAWN_SPACE,Cbut2f,Cbut2h,1,SPAWN_SPACE,Dbut2f,Dbut2h,1,SPAWN_SPACE, Ebut2f,Ebut2h,1,SPAWN_SPACE,Fbut2f,Fbut2h,1,SPAWN_SPACE,Gbut2f,Gbut2h,1,SPAWN_SPACE, Abut2f,Abut2h,1,SPAWN_SPACE,Bbut2f,Bbut2h,1,SPAWN_SPACE,Cbut3f,Cbut3h,1,SPAWN_SPACE, //Mary Had A Little Lamb [15-40] Bbutf,Bbuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE, Abutf,Abuth,1,SPAWN_SPACE, Bbutf,Bbuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE, Bbutf,Bbuth,1,SPAWN_SPACE*2,Abutf,Abuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE, Abutf,Abuth,1,SPAWN_SPACE*2,Bbutf,Bbuth,1,SPAWN_SPACE,Dbut2f,Dbut2h,1,SPAWN_SPACE, Dbut2f,Dbut2h,1,SPAWN_SPACE*2,Bbutf,Bbuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE, Gbutf,Gbuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE, Bbutf,Bbuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE, Abutf,Abuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE, Abutf,Abuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE, //Titantic - My Heart Will Go On [41-81] (BEST RECORDER SONG EVER) Gbutf,Gbuth,1,SPAWN_SPACE*1.5,Gbutf,Gbuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE, Gbutf,Gbuth,1,SPAWN_SPACE,FSbutf,FSbuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE*2, Gbutf,Gbuth,1,SPAWN_SPACE,FSbutf,FSbuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE*2, Abutf,Abuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE*2.5,Abutf,Abuth,1,SPAWN_SPACE*2, Gbutf,Gbuth,1,SPAWN_SPACE*1.5,Gbutf,Gbuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE, Gbutf,Gbuth,1,SPAWN_SPACE,FSbutf,FSbuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE*2.5, Gbutf,Gbuth,1,SPAWN_SPACE,Dbutf,Dbuth,1,SPAWN_SPACE*8,Gbutf,Gbuth,1,SPAWN_SPACE*4, Abutf,Abuth,1,SPAWN_SPACE*3.5,Dbutf,Dbuth,1,SPAWN_SPACE,Dbut2f,Dbut2h,1,SPAWN_SPACE*2, Cbut2f,Cbut2h,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE,Abutf,Abuth,1,SPAWN_SPACE*2, Bbutf,Bbuth,1,SPAWN_SPACE,Cbut2f,Cbut2h,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE*2, Abutf,Abuth,1,SPAWN_SPACE,Gbutf,Gbuth,1,SPAWN_SPACE,FSbutf,FSbuth,1,SPAWN_SPACE, Gbutf,Gbuth,1,SPAWN_SPACE*2,FSbutf,FSbuth,1,SPAWN_SPACE,FSbutf,FSbuth,1,SPAWN_SPACE, Gbutf,Gbuth,1,SPAWN_SPACE*2,Abutf,Abuth,1,SPAWN_SPACE,Bbutf,Bbuth,1,SPAWN_SPACE*2, Abutf,Abuth,1,SPAWN_SPACE*2,Gbutf,Gbuth,1,SPAWN_SPACE }; // will hold positions for the notes in a song int positions[SONGS_LENGTH]; #include "bitmap.c" //======================================================================== //This is the sync generator and raster generator. It MUST be entered from //sleep mode to get accurate timing of the sync pulses //Written by: Bruce Land //======================================================================== #pragma warn- interrupt [TIM1_COMPA] void t1_cmpA(void) begin //start the Horizontal sync pulse PORTD = syncON; //update the curent scanline number LineCount ++ ; //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 if (LineCount==263) begin LineCount = 1; end delay_us(2); //adjust to make 5 us pulses //end sync pulse PORTD = syncOFF; if (LineCount=ScreenTop) begin //compute byte index for beginning of the next line //left-shift 4 would be individual lines // <<3 means line-double the pixels //The 0xfff8 truncates the odd line bit //i=(LineCount-ScreenTop)<<3 & 0xfff8; // #asm push r16 lds r12, _LineCount lds r13, _Linecount+1 ldi r16, 30 sub r12, r16 ldi r16,0 sbc r13, r16 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 mov r16,r12 andi r16,0xf0 mov r12,r16 pop r16 #endasm //load 16 registers with screen info #asm push r14 push r15 push r16 push r17 push r18 push r19 push r26 push r27 ldi r26,low(_screen) ;base address of screen ldi r27,high(_screen) add r26,r12 ;offset into screen (add i) adc r27,r13 ld r4,x+ ;load 16 registers and inc pointer ld r5,x+ ld r6,x+ ld r7,x+ ld r8,x+ ld r9,x+ ld r10,x+ ld r11,x+ ld r12,x+ ld r13,x+ ld r14,x+ ld r15,x+ ld r16,x+ ld r17,x+ ld r18,x+ ld r19,x pop r27 pop r26 #endasm delay_us(4); //adjust to center image on screen //blast 16 bytes to the screen #asm ;but first a macro to make the code shorter ;the macro takes a register number as a parameter ;and dumps its bits serially to portD.6 ;the nop can be eliminated to make the display narrower .macro videobits ;regnum BST @0,7 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,6 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,5 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,4 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,3 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,2 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,1 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,0 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 .endm videobits r4 ;video line -- byte 1 videobits r5 ;byte 2 videobits r6 ;byte 3 videobits r7 ;byte 4 videobits r8 ;byte 5 videobits r9 ;byte 6 videobits r10 ;byte 7 videobits r11 ;byte 8 videobits r12 ;byte 9 videobits r13 ;byte 10 videobits r14 ;byte 11 videobits r15 ;byte 12 videobits r16 ;byte 13 videobits r17 ;byte 14 videobits r18 ;byte 15 videobits r19 ;byte 16 clt ;clear video after the last pixel on the line IN R30,0x12 BLD R30,6 OUT 0x12,R30 pop r19 pop r18 pop r17 pop r16 pop r15 pop r14 #endasm end end #pragma warn+ //======================================================================== //================================================ // Video Functions //================================================ // put a big character on the screen // c is index into bitmap // Written by: Bruce Land void video_putchar(char x, char y, char c) begin v7 = x; for (v6=0;v6<7;v6++) begin v1 = ascii[c][v6]; v8 = y+v6; video_pt(v7, v8, (v1 & 0x80)==0x80); video_pt(v7+1, v8, (v1 & 0x40)==0x40); video_pt(v7+2, v8, (v1 & 0x20)==0x20); video_pt(v7+3, v8, (v1 & 0x10)==0x10); video_pt(v7+4, v8, (v1 & 0x08)==0x08); end end // put a string of big characters on the screen, horizontally // Written by: Bruce Land void video_puts(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin video_putchar(x,y,str[i]); x = x+6; end end //put the note, with or without the sharp void video_printnote(char x, char y, char note, char sharp) begin if (note == ' ') { video_putchar(x,y,' '); video_putchar(x+5,y,' '); } else if (sharp == ' ') { video_putchar(x,y,' '); video_putchar(x+5,y,' '); video_putchar(x+2,y,note); } else { video_putchar(x,y,note); video_putchar(x+5,y,sharp); } end //plot a line //at x1,y1 to x2,y2 with color 1=white 0=black 2=invert //NOTE: this function requires signed chars //Code is from David Rodgers, //"Procedural Elements of Computer Graphics",1985 void video_line(char x1, char y1, char x2, char y2, char c) begin int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; x = x1; y = y1; dx = cabs(x2-x1); dy = cabs(y2-y1); s1 = csign(x2-x1); s2 = csign(y2-y1); xchange = 0; if (dy>dx) begin temp = dx; dx = dy; dy = temp; xchange = 1; end e = ((int)dy<<1) - dx; for (j=0; j<=dx; j++) begin video_pt(x,y,c) ; if (e>=0) begin if (xchange==1) x = x + s1; else y = y + s2; e = e - ((int)dx<<1); end if (xchange==1) y = y + s2; else x = x + s1; e = e + ((int)dy<<1); end end //================================================ //print Note name void video_printnotename(char x, char y, unsigned char fullPresses, unsigned char halfPresses) begin char note; char sharp; if (halfPresses == Cbuth && fullPresses == Cbutf) {note = 'C'; sharp = ' ';} else if (halfPresses == CSbuth && fullPresses == CSbutf) {note = 'C'; sharp = '#';} else if (halfPresses == Dbuth && fullPresses == Dbutf) {note = 'D'; sharp = ' ';} else if (halfPresses == DSbuth && fullPresses == DSbutf) {note = 'D'; sharp = '#';} else if (halfPresses == Ebuth && fullPresses == Ebutf) {note = 'E'; sharp = ' ';} else if (halfPresses == Fbuth && fullPresses == Fbutf) {note = 'F'; sharp = ' ';} else if (halfPresses == FSbuth && fullPresses == FSbutf) {note = 'F'; sharp = '#';} else if (halfPresses == Gbuth && fullPresses == Gbutf) {note = 'G'; sharp = ' ';} else if (halfPresses == GSbuth && fullPresses == GSbutf) {note = 'G'; sharp = '#';} else if (halfPresses == Abuth && fullPresses == Abutf) {note = 'A'; sharp = ' ';} else if (halfPresses == ASbuth && fullPresses == ASbutf) {note = 'A'; sharp = '#';} else if (halfPresses == Bbuth && fullPresses == Bbutf) {note = 'B'; sharp = ' ';} else if (halfPresses == Cbut2h && fullPresses == Cbut2f) {note = 'C'; sharp = ' ';} else if (halfPresses == CSbut2h && fullPresses == CSbut2f) {note = 'C'; sharp = '#';} else if (halfPresses == Dbut2h && fullPresses == Dbut2f) {note = 'D'; sharp = ' ';} else if (halfPresses == DSbut2h && fullPresses == DSbut2f) {note = 'D'; sharp = '#';} else if (halfPresses == Ebut2h && fullPresses == Ebut2f) {note = 'E'; sharp = ' ';} else if (halfPresses == Fbut2h && fullPresses == Fbut2f) {note = 'F'; sharp = ' ';} else if (halfPresses == FSbut2h && fullPresses == FSbut2f) {note = 'F'; sharp = '#';} else if (halfPresses == Gbut2h && fullPresses == Gbut2f) {note = 'G'; sharp = ' ';} else if (halfPresses == GSbut2h && fullPresses == GSbut2f) {note = 'G'; sharp = '#';} else if (halfPresses == Abut2h && fullPresses == Abut2f) {note = 'A'; sharp = ' ';} else if (halfPresses == ASbut2h && fullPresses == ASbut2f) {note = 'A'; sharp = '#';} else if (halfPresses == Bbut2h && fullPresses == Bbut2f) {note = 'B'; sharp = ' ';} else if (halfPresses == Cbut3h && fullPresses == Cbut3f) {note = 'C'; sharp = ' ';} else {note = ' '; sharp = ' ';} //comment this if don't have audio MCU connected if (notePlayed != 0) { if (note == ' ') { note = 'X'; } } else { note = ' '; sharp = ' '; } video_printnote(x,y+76,note,sharp); end //text string definitions //================================== char scoreMsg[]="SCORE: "; char menu[]="MENU"; char freeplay[]="FREEPLAY"; char cscale[]="C SCALE"; char mary[]="MARY"; char titanic[]="TITANIC"; char ts[3]; void initialize (void); //================================== // set up the ports and timers void main(void) begin //variables char start_i; int i; char lineClear = 1, line1, line2; unsigned char notes_scrolled = 0; char menuBuffer = 0; char menuItem = 1; char curSong; int songStartI, songEndI, curStartI; initialize(); // set up ports and variables //The following loop executes once/video line during lines //1-230, then does all of the frame-end processing while(1) begin //stall here until next line starts //sleep enable; mode=idle //use sleep to make entry into sync ISR uniform time #asm ("sleep"); //The following code executes during the vertical blanking //Code here can be as long as //a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec if (LineCount==231) begin //video buffer state machine switch(buffer) begin case CLEAR_SCREEN: // loop on self until screen is cleared line1 = lineClear; line2 = lineClear+LINES_TO_CLEAR; if (line2>HEIGHT) line2=HEIGHT; for (lineClear = line1; lineClear 1 && buttons != prevButtons) menuItem--; break; case 0x08: //SW5 (RESET) break; end break; case DRAW_GAME: //initialize variables t=0; buffer=0; speed = DEFAULT_SPEED; notePlayed = 0; justCleared = 0; score = 0; readyForGame = 0; // set indexes into songs[] array based on choosen song switch (curSong) begin case FREEPLAY: songStartI = START_INDEX_FREE; songEndI = END_INDEX_FREE; break; case CSCALE: songStartI = START_INDEX_CSCALE; songEndI = END_INDEX_CSCALE; break; case MARY: songStartI = START_INDEX_MARY; songEndI = END_INDEX_MARY; break; case TITANIC: songStartI = START_INDEX_TITANIC; songEndI = END_INDEX_TITANIC; end curStartI = songStartI; positions[songStartI] = SPAWN_LOC; for (i=songStartI+1; i<=songEndI; i++) { positions[i]=positions[i-1]+songs[((i-1)<<2)+3]; } //Print "SCORE: " video_puts(18,3,scoreMsg); video_line(0,TSTATUS,WIDTH,TSTATUS,1); //status bar sprintf(ts,"%03d",score); buffer = DECODE_BUTTONS; break; case DECODE_BUTTONS: // Stage 1 of 2 of game video generation lastFullPresses = fullPresses; lastHalfPresses = halfPresses; //PortC and PortD are buttons // For buttons 0,6,7, halfPress is either button (not both), fullPress is both // For buttons 1-5, halfPresses never occur, so fullPress is either or both buttons halfPresses = PINA^PINC; fullPresses = (PINA&PINC)|(halfPresses & 0b01111100); halfPresses = halfPresses & 0b10000011; //figure out what changed since last time for efficient printing to screen changed = (fullPresses ^ lastFullPresses) | (halfPresses ^ lastHalfPresses); notePlayed = (~PINB) & 0b00010000; video_printnotename(2,13,fullPresses,halfPresses); video_putoutlines(3,13); //Check for game reset buttons = (~PINB) & 0x0f; if (buttons == 0x08) { //(RESET) readyForGame = 0; buffer = CLEAR_SCREEN; break; } video_puts(60,3,ts); //print current score if (justCleared == 1) { // If just cleared a set of notes, print all user buttons justCleared = 0; video_putbuttons(3,13,fullPresses,halfPresses, 255); } else { // If haven't just cleared, only need to print notes that changed video_putbuttons(3,13,fullPresses,halfPresses, changed); } // shift positions for notes in current song for (i=curStartI; i<=songEndI; i++) { positions[i]=positions[i]-speed; } start_i = curStartI; buffer = SHIFT_LEFT; break; case SHIFT_LEFT: // Stage 2 of 2 of game video generation // loop through the notes in the current song that haven't been scrolled already for (i=start_i; i<=songEndI; i++) begin if (positions[i] <= SPAWN_LOC) begin if (positions[i]<=2+speed) { // note should disappear positions[i] = 0; justCleared = 1; curStartI = i+1; //checks Buttons and updates score if ((songs[(i<<2)]==fullPresses || songs[(i<<2)]==lastFullPresses) && (songs[(i<<2)+1]==halfPresses || songs[(i<<2)+1]==lastHalfPresses)) score=score+SCORE_PLUS; else score=score-SCORE_MINUS; sprintf(ts,"%03d",score); } else { // shift buttons left video_shiftleft(positions[i],13,songs[(i<<2)],songs[(i<<2)+1],speed); } // loop back on self if already shifted the most we can draw in one frame if(++notes_scrolled==MAX_NOTES_TO_SCROLL) { start_i = i+1; i = songEndI+1; //cut out of for loop } end //end if else begin i = songEndI+1; end end //end for if (notes_scrolled == MAX_NOTES_TO_SCROLL) { // loop back on self if already shifted the most we can draw in one frame notes_scrolled=0; buffer = SHIFT_LEFT; } else { notes_scrolled=0; buffer = DECODE_BUTTONS; } break; end //switch on video buffer end //line 231 end //while end //main void initialize (void) { //init timer 1 to generate sync OCR1A = lineTime; //One NTSC line TCCR1B = 9; //full speed; clear-on-match TCCR1A = 0x00; //turn off pwm and oc lines TIMSK = 0x10; //enable interrupt T1 cmp //init ports DDRA = 0x00; //PORTA is input for pushbuttons DDRB = 0x00; //B.4 is input from Audio MCU for whether user is blowing //Switches are connected to PORTB to control game menu //SW2 <-> B.0 //SW3 <-> B.1 //SW4 <-> B.2 //SW5 <-> B.3 DDRC = 0x00; //PORTC is input for pushbuttons DDRD = 0xf0; //D.5 and D.6 are for video out //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //Print boundary lines video_line(0,0,0,HEIGHT,1); //left video_line(WIDTH,0,WIDTH,HEIGHT,1); //right video_line(0,0,WIDTH,0,1); //top video_line(0,HEIGHT,WIDTH,HEIGHT,1); //bottom //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); }