//video gen and sound //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //B.3 is sound and should have a 10k resistor to gnd #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include //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 begin { #define end } #define ScreenTop 30 #define ScreenBot 230 #define width 126 #define countMS 62 //ticks/mSec #define MAX_SCORE_INDEX 98 #define MAX_PACER 129 //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+ char syncON, syncOFF; int LineCount; char screen[1600]; char KEYBOARD[] = "KEYBOARD"; char MANIA[] = "MANIA"; char CU[] = "CORNELL"; char ECE476[] = "ECE476"; char FINAL[] = "FINAL"; char PROJ[] = "PROJECT"; char YANG[] = "YANG"; char CHING[] = "CHING"; char CHANG[] = "CHANG"; char CAROLE[] = "CAROLE"; char JEAN[] = "JEAN"; char WU[] = "WU"; char EXCELLENT[] = "EXCELLENT"; char GOOD[] = "GOOD"; char PASS[] = "PASS"; char YOUR[] = "YOUR"; char SCOREDISPLAY[] = "SCORE"; char OUT[] = "OUT"; char OF[] = "OF"; char HIGHEST[] = "HIGHEST"; char FAIL[] = "FAIL"; // the state enum{MANUAL, SCORE, PLAYING, RESULT, FINISH}state; enum{ERASE_SCREEN, SCORE_DISPLAY, HIGH_SCORE, SCORE_NUMBER, NOTE_ONE, NOTE_TWO, NOTE_THREE, NOTE_FOUR}result_state; //==== the flags ===/ // flags for menu unsigned char t_sem, t_lock; // flags for drawing unsigned char playing_score, display_score, draw_clef, erase, erase_screen, draw_done, start, display_result; // flags for correctness and sound unsigned char correct_strike, check_note, sound; // index unsigned char score_index, display_score_index, notetable_index, strike_index, old_r_index, soundarray_index; // choices char score_choice, choice_strike, keystrike; // counter unsigned char pace_count, pacer_count, playing_duration, display_duration; //grade int total_point, actual_point; int max_point[5]; char ap[4], tp[4], hp[4]; float finalscore; // coordinate char pacer_x, pacer_y, score_x, score_y, note_x, note_y, check_x, check_y; //RXC ISR variables unsigned char r_index; //current string index unsigned char r_buffer[15]; //input string unsigned char r_ready; //flag for receive done unsigned char r_char; //current character //TX empth ISR variables unsigned char t_index; //current string index unsigned char t_buffer[35]; //output string unsigned char t_ready; //flag for transmit done unsigned char t_char; //current character //char* current_score; char notetable[9] = {'q','a','s','d','f','g','h','j','k' }; char soundarray[16] = { 0, 120, 0, 106, 94, 0, 90, 80, 71,0, 63, 60, 0, 0, 0, 0}; //symbol[0] is the pacer, symbol[1] is the check mark, symbol[2] is a cross which indicates wrong key hit flash char symbol[3][4]={ //pacer 0b01100000, 0b11110000, 0b11110000, 0b01100000, //correct 0b00000000, 0b00010000, 0b10100000, 0b01000000, //wrong 0b10100000, 0b01000000, 0b10100000, 0b00000000 }; //treble clef flash char high[2][31]={ 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000011, 0b00000101, 0b00001001, 0b00010001, 0b00010011, 0b00100101, 0b00100101, 0b01001001, 0b01001001, 0b01000101, 0b00100101, 0b00010011, 0b00001001, 0b00000101, 0b00000011, 0b00000001, 0b00000001, 0b00100001, 0b00010010, 0b00001100, 0b00100000, 0b01010000, 0b01001000, 0b10001000, 0b10010000, 0b10010000, 0b00100000, 0b00100000, 0b01000000, 0b01000000, 0b10000000, 0b00000000, 0b00000000, 0b00000000, 0b11000000, 0b00100000, 0b00010000, 0b00010000, 0b00001000, 0b00001000, 0b00001000, 0b00010000, 0b00010000, 0b00100000, 0b00100000, 0b01000000, 0b10000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 }; //pre-coded 4 piano score; score ends with 'q' flash char score_stored[5][98]={ //score 0 default: twinkle twinkle little star 5,2,3,2,3,4,4,2,2,2,2,4, //12 1,2,2,2,3,2,4,2,5,2,5,2,5,4, //14 5,2,3,2,3,4,4,2,2,2,2,4, //12 1,2,3,2,5,2,5,2,3,8, //10 2,2,2,2,2,2,2,2,2,2,3,2,4,4, //14 3,2,3,2,3,2,3,2,3,2,4,2,5,4, //14 5,2,3,2,3,4,4,2,2,2,2,4, //12 1,2,3,2,5,2,5,2,1,8, //10 //score 1 mary has a little lamb 3,3,2,1,1,2,2,2,3,2,3,2,3,4, //16 2,2,2,2,2,4,3,2,5,2,5,4, //16 3,3,2,1,1,2,2,2,3,2,3,2,3,4, //16 2,2,2,1,2,1,3,2,2,2,1,8, //16 //end 'q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q', //18 'q','q','q','q','q','q','q','q','q','q','q','q','q','q', //14 'q','q','q','q','q','q','q','q','q','q','q','q','q','q', //14 //score 2 london bridge falling down 5,3,6,1,5,2,4,2,3,2,4,2,5,4, //16 2,2,3,2,4,4,3,2,4,2,5,4, //16 5,3,6,1,5,2,4,2,3,2,4,2,5,4, //16 2,4,5,4,3,2,1,6, //16 //end 'q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q', //18 'q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q','q', //18 'q','q','q','q','q','q','q','q','q','q','q','q','q','q', //14 // score 3 twinkle twinkle little star 14*6 1,2,1,2,5,2,5,2,6,2,6,2,5,4, 4,2,4,2,3,2,3,2,2,2,2,2,1,4, 5,2,5,2,4,2,4,2,3,2,3,2,2,4, 5,2,5,2,4,2,4,2,3,2,3,2,2,4, 1,2,1,2,5,2,5,2,6,2,6,2,5,4, 4,2,4,2,3,2,3,2,2,2,2,2,1,4, //end 'q','q','q','q','q','q','q','q','q','q','q','q','q','q', // score 4 bee 5,2,3,2,3,4,4,2,2,2,2,4, //12 1,2,2,2,3,2,4,2,5,2,5,2,5,4, //14 5,2,3,2,3,4,4,2,2,2,2,4, //12 1,2,3,2,5,2,5,2,3,8, //10 2,2,2,2,2,2,2,2,2,2,3,2,4,4, //14 3,2,3,2,3,2,3,2,3,2,4,2,5,4, //14 5,2,3,2,3,4,4,2,2,2,2,4, //12 1,2,3,2,5,2,5,2,1,8 //10 }; //Point plot lookup table //One bit masks flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //define some character bitmaps //5x7 characters flash char notes[9][12]={ // space 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, // eighth note 0b00001100, 0b00001010, 0b00001010, 0b00001001, 0b00001001, 0b00001000, 0b00001000, 0b00101000, 0b01111000, 0b11111000, 0b11110000, 0b01100000, // quarter note 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00101000, 0b01111000, 0b11111000, 0b11110000, 0b01100000, // dotted quarter note 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00101000, 0b01111000, 0b11111000, 0b11110010, 0b01100000, // half note 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00101000, 0b01011000, 0b10001000, 0b10010000, 0b01100000, // sixteenth note 0b00001100, 0b00001010, 0b00001010, 0b00001001, 0b00001101, 0b00001010, 0b00001010, 0b00101001, 0b01111001, 0b11111000, 0b11110000, 0b01100000, // dotted half note 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00101000, 0b01011000, 0b10001000, 0b10010010, 0b01100000, // rest 0b00000000, 0b00000000, 0b00000000, 0b01111110, 0b01111110, 0b01111110, 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, // whole note 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00110000, 0b01001000, 0b10000100, 0b01001000, 0b00110000 }; flash char bitmap[38][7]={ //0 0b01110000, 0b10001000, 0b10011000, 0b10101000, 0b11001000, 0b10001000, 0b01110000, //1 0b00100000, 0b01100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //2 0b01110000, 0b10001000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b11111000, //3 0b11111000, 0b00010000, 0b00100000, 0b00010000, 0b00001000, 0b10001000, 0b01110000, //4 0b00010000, 0b00110000, 0b01010000, 0b10010000, 0b11111000, 0b00010000, 0b00010000, //5 0b11111000, 0b10000000, 0b11110000, 0b00001000, 0b00001000, 0b10001000, 0b01110000, //6 0b01000000, 0b10000000, 0b10000000, 0b11110000, 0b10001000, 0b10001000, 0b01110000, //7 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b10000000, //8 0b01110000, 0b10001000, 0b10001000, 0b01110000, 0b10001000, 0b10001000, 0b01110000, //9 0b01110000, 0b10001000, 0b10001000, 0b01111000, 0b00001000, 0b00001000, 0b00010000, //A 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, //B 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10001000, 0b10001000, 0b11110000, //C 0b01110000, 0b10001000, 0b10000000, 0b10000000, 0b10000000, 0b10001000, 0b01110000, //D 0b11110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11110000, //E 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b11111000, //F 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b10000000, //G 0b01110000, 0b10001000, 0b10000000, 0b10011000, 0b10001000, 0b10001000, 0b01110000, //H 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, 0b10001000, //I 0b01110000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //J 0b00111000, 0b00010000, 0b00010000, 0b00010000, 0b00010000, 0b10010000, 0b01100000, //K 0b10001000, 0b10010000, 0b10100000, 0b11000000, 0b10100000, 0b10010000, 0b10001000, //L 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b11111000, //M 0b10001000, 0b11011000, 0b10101000, 0b10101000, 0b10001000, 0b10001000, 0b10001000, //N 0b10001000, 0b10001000, 0b11001000, 0b10101000, 0b10011000, 0b10001000, 0b10001000, //O 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //P 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10000000, 0b10000000, 0b10000000, //Q 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10010000, 0b01101000, //R 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10100000, 0b10010000, 0b10001000, //S 0b01111000, 0b10000000, 0b10000000, 0b01110000, 0b00001000, 0b00001000, 0b11110000, //T 0b11111000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, //U 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //V 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, //W 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10101000, 0b10101000, 0b01010000, //X 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b01010000, 0b10001000, 0b10001000, //Y 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b00100000, 0b00100000, //Z 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b11111000, //figure1 0b01110000, 0b00100000, 0b01110000, 0b10101000, 0b00100000, 0b01010000, 0b10001000, //figure2 0b01110000, 0b10101000, 0b01110000, 0b00100000, 0b00100000, 0b01010000, 0b10001000 }; //================================ //3x5 font numbers, then letters //packed two per definition for fast //copy to the screen at x-position divisible by 4 flash char smallbitmap[39][5]={ //0 0b11101110, 0b10101010, 0b10101010, 0b10101010, 0b11101110, //1 0b01000100, 0b11001100, 0b01000100, 0b01000100, 0b11101110, //2 0b11101110, 0b00100010, 0b11101110, 0b10001000, 0b11101110, //3 0b11101110, 0b00100010, 0b11101110, 0b00100010, 0b11101110, //4 0b10101010, 0b10101010, 0b11101110, 0b00100010, 0b00100010, //5 0b11101110, 0b10001000, 0b11101110, 0b00100010, 0b11101110, //6 0b11001100, 0b10001000, 0b11101110, 0b10101010, 0b11101110, //7 0b11101110, 0b00100010, 0b01000100, 0b10001000, 0b10001000, //8 0b11101110, 0b10101010, 0b11101110, 0b10101010, 0b11101110, //9 0b11101110, 0b10101010, 0b11101110, 0b00100010, 0b01100110, //: 0b00000000, 0b01000100, 0b00000000, 0b01000100, 0b00000000, //= 0b00000000, 0b11101110, 0b00000000, 0b11101110, 0b00000000, //blank 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, //A 0b11101110, 0b10101010, 0b11101110, 0b10101010, 0b10101010, //B 0b11001100, 0b10101010, 0b11101110, 0b10101010, 0b11001100, //C 0b11101110, 0b10001000, 0b10001000, 0b10001000, 0b11101110, //D 0b11001100, 0b10101010, 0b10101010, 0b10101010, 0b11001100, //E 0b11101110, 0b10001000, 0b11101110, 0b10001000, 0b11101110, //F 0b11101110, 0b10001000, 0b11101110, 0b10001000, 0b10001000, //G 0b11101110, 0b10001000, 0b10001000, 0b10101010, 0b11101110, //H 0b10101010, 0b10101010, 0b11101110, 0b10101010, 0b10101010, //I 0b11101110, 0b01000100, 0b01000100, 0b01000100, 0b11101110, //J 0b00100010, 0b00100010, 0b00100010, 0b10101010, 0b11101110, //K 0b10001000, 0b10101010, 0b11001100, 0b11001100, 0b10101010, //L 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11101110, //M 0b10101010, 0b11101110, 0b11101110, 0b10101010, 0b10101010, //N 0b00000000, 0b11001100, 0b10101010, 0b10101010, 0b10101010, //O 0b01000100, 0b10101010, 0b10101010, 0b10101010, 0b01000100, //P 0b11101110, 0b10101010, 0b11101110, 0b10001000, 0b10001000, //Q 0b01000100, 0b10101010, 0b10101010, 0b11101110, 0b01100110, //R 0b11101110, 0b10101010, 0b11001100, 0b11101110, 0b10101010, //S 0b11101110, 0b10001000, 0b11101110, 0b00100010, 0b11101110, //T 0b11101110, 0b01000100, 0b01000100, 0b01000100, 0b01000100, //U 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b11101110, //V 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b01000100, //W 0b10101010, 0b10101010, 0b11101110, 0b11101110, 0b10101010, //X 0b00000000, 0b10101010, 0b01000100, 0b01000100, 0b10101010, //Y 0b10101010, 0b10101010, 0b01000100, 0b01000100, 0b01000100, //Z 0b11101110, 0b00100010, 0b01000100, 0b10001000, 0b11101110 }; /*/================================== //timer 0 overflow ISR interrupt [TIM0_COMP] void timer0_overflow(void) { time--; }*/ // ================================= //UART xmit-empty ISR interrupt [USART_DRE] void uart_send(void) begin t_char = t_buffer[++t_index]; if (t_char == 0) begin UCSRB.5=0; //kill isr t_ready=1; //transmit done end else UDR = t_char ; //send the char end //================================== //UART character-ready ISR interrupt [USART_RXC] void uart_rec(void) begin r_char=UDR; //get a char UDR=r_char; //then print it //build the input string if (r_char != '\r') r_buffer[r_index++]=r_char; else begin putchar('\n'); //use putchar to avoid overwrite r_buffer[r_index]=0x00; //zero terminate r_ready=1; //signal cmd processor UCSRB.7=0; //stop rec ISR end end //================================== //This is the sync generator and raster generator. It MUST be entered from //sleep mode to get accurate timing of the sync pulses #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+ //================================== //plot one point //at x,y with color 1=white 0=black 2=invert #pragma warn- void video_pt(char x, char y, char c) begin #asm ; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it push r16 ldd r30,y+2 ;get x lsr r30 lsr r30 lsr r30 ;divide x by 8 ldd r12,y+1 ;get y lsl r12 ;mult y by 16 clr r13 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 add r12, r30 ;add in x/8 ;v2 = screen[i]; r5 ;v3 = pos[x & 7]; r6 ;v4 = c r7 ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 ld r5,Z ;get screen byte ldd r26,y+2 ;get x ldi r27,0 andi r26,0x07 ;form x & 7 ldi r30,low(_pos*2) ldi r31,high(_pos*2) add r30,r26 adc r31,r27 lpm r6,Z ld r16,y ;get c ;if (v4==1) screen[i] = v2 | v3 ; ;if (v4==0) screen[i] = v2 & ~v3; ;if (v4==2) screen[i] = v2 ^ v3 ; cpi r16,1 brne tst0 or r5,r6 tst0: cpi r16,0 brne tst2 com r6 and r5,r6 tst2: cpi r16,2 brne writescrn eor r5,r6 writescrn: ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 st Z, r5 ;write the byte back to the screen pop r16 #endasm end #pragma warn+ //================================== //draw a symbol on the screen with index c //c=0: pacer, c=1: check mark, c=2: cross mark //x and y indicate the symbol's position void video_putsymbol(char x, char y, char c) begin v7 = x; for (v6=0;v6<4;v6++) begin v1 = symbol[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); end end //================================== //draw treble clef on the screen //c=0: right portioin of treble clef, c=1: left portion of treble clef //x and y indicate the treble clef's position void video_puthigh(char x, char y, char c) begin v7 = x; for (v6=0;v6<31;v6++) begin v1 = high[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); video_pt(v7+5, v8, (v1 & 0x04)==0x04); video_pt(v7+6, v8, (v1 & 0x02)==0x02); video_pt(v7+7, v8, (v1 & 0x01)==0x01); end end //================================== //draw a note on the screen //c=0: space, c=1: eighth note, c=2: quarter note, c=3: dotted quarter note //c=4: half note, c=5: sixteenth note, c=6: dotted half note, c=7: rest, c=8: whole note //x and y indicate the treble clef's position void video_putnote(char x, char y, char c) begin v7 = x; for (v6=0;v6<12;v6++) begin v1 = notes[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); video_pt(v7+5, v8, (v1 & 0x04)==0x04); video_pt(v7+6, v8, (v1 & 0x02)==0x02); video_pt(v7+7, v8, (v1 & 0x01)==0x01); end end //================================== // put a big character on the screen // c is index into bitmap void video_putchar(char x, char y, char c) begin v7 = x; for (v6=0;v6<7;v6++) begin v1 = bitmap[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 void video_puts(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_putchar(x,y,str[i]-0x30); else video_putchar(x,y,str[i]-0x40+9); x = x+6; end end //================================== // put a small character on the screen // x-cood must be on divisible by 4 // c is index into bitmap void video_smallchar(char x, char y, char c) begin char mask; i=((int)x>>3) + ((int)y<<4) ; if (x == (x & 0xf8)) mask = 0x0f; //f8 else mask = 0xf0; screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask); screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask); screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask); screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); end //================================== // put a string of small characters on the screen // x-cood must be on divisible by 4 void video_putsmalls(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_smallchar(x,y,str[i]-0x30); else video_smallchar(x,y,str[i]-0x40+12); x = x+4; end end //================================== //return the value of one point //at x,y with color 1=white 0=black 2=invert char video_set(char x, char y) begin //The following construction //detects exactly one bit at the x,y location i=((int)x>>3) + ((int)y<<4) ; return ( screen[i] & 1<<(7-(x & 0x7))); 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=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 video_pt(x,y,c); // insert another video_pt to plot the end point end //=============================== //plot a horizontal line across the TV //at y with color white void video_hline(char y ){ i = (int)y << 4; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff;// 10 screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i++] = 0xff; screen[i] = 0xff;// 16 } //********************************************************** // -- non-blocking keyboard check initializes ISR-driven // receive. This routine merely sets up the ISR, which then //does all the work of getting a command. void gets_int(void) begin r_ready=0; r_index=0; UCSRB.7=1; end //********************************************************** // -- nonblocking print: initializes ISR-driven // transmit. This routine merely sets up the ISR, then //send one character, The ISR does all the work. void puts_int(void) begin t_ready=0; t_index=0; if (t_buffer[0]>0) begin putchar(t_buffer[0]); UCSRB.5=1; end end //================================== void initialize(){ //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 DDRD = 0xff; //video out //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //serial setop for debugging using printf, etc. UCSRB = 0x18 ; UBRRL = 103 ; //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //initialize state state = MANUAL; result_state = ERASE_SCREEN; //initialize the flags t_sem = 1; t_lock = 0; playing_score = 0; display_score = 0; draw_clef = 0; erase = 0; erase_screen = 0; draw_done = 0; start = 2; display_result = 0; correct_strike = 2; check_note = 0; sound = 0; // initialize the index score_index = 0; display_score_index = 0; notetable_index = 0; strike_index = 0; old_r_index = 0; soundarray_index = 0; // choices score_choice = 'q'; choice_strike = 'q'; keystrike = 'q'; // counter pace_count = -1; pacer_count = 0; playing_duration = 0; display_duration = 0; //grade total_point = 0; actual_point = 0; max_point[0] = 0; max_point[1] = 0; max_point[2] = 0; max_point[3] = 0; max_point[4] = 0; // coordinate pacer_x = 18; pacer_y = 44; score_x = 30; score_y = 32; note_x = 0; note_y = 0; check_x = 0; check_y = 0; // initialize the hyperten r_ready=0; t_ready=1; t_index = 0; r_index = 0; r_buffer[0] = 'n'; //use OC0 (pin B.3) for music DDRB.3 = 1 ; soundarray_index = 0; //Print on HyperTerminal putsf( "welcome to keyboard mania\n\r"); //Print welcome page on TV Screen video_puts(13,3,KEYBOARD); video_puts(70,3,MANIA); video_putnote(10, 22 , 1); video_putnote(20, 35 , 2); video_putnote(30, 25 , 3); video_putnote(40, 18 , 1); video_putnote(50, 22 , 2); video_putnote(60, 18 , 3); video_putnote(70, 20 , 4); video_putnote(80, 30 , 5); video_putnote(90, 16 , 6); video_putnote(100, 13 , 7); video_putnote(110, 15 , 8); video_puthigh(2,59, 0); video_puthigh(10,59, 1); video_puts(35,50,ECE476); video_puts(35,60,FINAL); video_puts(70,60,PROJ); video_puts(35,70,YANG); video_puts(60,70,CHING); video_puts(95,70,CHANG); video_puts(35,80,CAROLE); video_puts(70,80,JEAN); video_puts(98,80,WU); //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); } //================================== // set up the ports and timers void main(void) { initialize(); //The following loop executes once/video line during lines //1-230, then does all of the frame-end processing while(1){ //stall here until next line starts //sleep enable; mode=idle //use sleep to make entry into sync ISR uniform time #asm ("sleep"); //display menu through the HyperTerminal on the PC if ( LineCount == 225 ){ // line 225 switch( state ) { case MANUAL: if( t_ready && !t_lock ){ // if the trasmit of the previous one is done if ( t_sem ){ sprintf(t_buffer, "Choose a score: \n\r "); // t_buffer puts new one into t_buffer putsf("1. Mary Has a Little Lamb\n\r"); putsf("2. London Bridge Falling Down\n\r"); putsf("3. Twinkle Twinkle Little Star\n\r"); putsf("4. Little Bee\n\r"); puts_int(); // start transmitting t_sem = 0; // semaphore to lock one of the process } else{ gets_int(); // receiving t_sem = 1; t_lock = 1; // lock to prevent 2nd entering } } if ( r_ready && t_lock){ // if done receiving from the PC for( i=0; i<1600; i++) // erase the TV screen screen[i]=0; sscanf(r_buffer, "%d\r", &score_choice); // set the value in r_buffer to score_choice t_lock = 0; display_score = 1; // set flag to display the score on the TV if (score_choice > 0 && score_choice < 5 ) // valide choice state = SCORE; else{ score_choice = 0; r_buffer[0] = 'n'; } } break; case SCORE: if( t_ready && !t_lock ){ // if the transmit of the previous one is done if ( t_sem ){ sprintf(t_buffer, "enter y to start and q to quit:\n\r"); // t_buffer puts new one into t_buffer puts_int(); // start transmitting t_sem = 0; // semaphore to lock one of the process } else{ gets_int(); // receiving t_sem = 1; t_lock = 1; // lock to prevent 2nd entering } } if ( r_ready && t_lock){ // if done receiving from the PC sscanf(r_buffer, "%c\r", &choice_strike); // set the value in r_buffer to score_choice if ( choice_strike == 'y' ){ putsf("start to play the chosen piece\n\r"); // if user decides to play the music state = PLAYING; playing_score = 1; t_lock = 0; } else if ( choice_strike == 'q' ){ // or if the user decides to select another score putsf("abort the chosen piece\n\r"); for ( i=0; i<1600; i++ ) // clear the screen screen[i] = 0; state = FINISH; t_lock = 0; } else t_lock = 0; } break; case PLAYING: if( t_ready && !t_lock ){ r_buffer[0] = 'n' ; // initialize the first element of r_buffer gets_int(); t_lock = 1; } if ( t_lock ){ if ( r_ready ){ // ternminate display_score = 0; playing_score = 0; display_result = 1; state = RESULT; TCCR0 = 0; // stop DDS t_lock = 0; } } break; case RESULT: if( t_ready && !t_lock ){ gets_int(); t_lock = 1; } if ( t_lock ){ if ( r_ready ) // ternminate state = FINISH; } break; case FINISH: for ( i=0; i<1600; i++ ) // clear the screen screen[i] = 0; //initialize state state = MANUAL; result_state = ERASE_SCREEN; //initialize the flags t_sem = 1; t_lock = 0; playing_score = 0; display_score = 0; draw_clef = 0; erase = 0; erase_screen = 0; draw_done = 0; start = 2; display_result = 0; correct_strike = 2; check_note = 0; sound = 0; // initialize the index score_index = 0; display_score_index = 0; notetable_index = 0; strike_index = 0; old_r_index = 0; soundarray_index = 0; // choices score_choice = 'q'; choice_strike = 'q'; keystrike = 'q'; // counter pace_count = -1; pacer_count = 0; playing_duration = 0; display_duration = 0; // coordinate pacer_x = 18; pacer_y = 44; score_x = 30; score_y = 32; note_x = 0; note_y = 0; check_x = 0; check_y = 0; // initialize the hyperten r_ready=0; t_ready=1; t_index = 0; r_index = 0; r_buffer[0] = 'n'; //point total_point = 0; actual_point = 0; break; } } // line 225 if ( LineCount == 227 ){ // line 227 if ( display_score && ( ( start!=0 ) || ( (start==0) && (pace_count==14) ) ) ){ // display score note_y = score_y - ((score_stored[score_choice][display_score_index])<< 1) ; // displaying the score according to the index value note_x = score_x; display_score_index++; display_duration = score_stored[score_choice][display_score_index]; score_x += ((display_duration<<2)+(display_duration<<1)); //increment 12 if display_duration==2 increment 18 if display_duration==3 display_score_index++; } if ( playing_score ){ pace_count++; if ( pace_count == 20 && playing_duration == 0 && start == 0){ keystrike = r_buffer[strike_index]; if ( keystrike != 'n' ){ // if strike too early correct_strike = 0; } } if ( pace_count == 30 ){ pace_count = 0; // reset the pacer pacer_x += 6; if ( pacer_x >= 126 ){ pacer_x = 30; if ( pacer_y == 94 ){ pacer_y = 44; } else{ pacer_y = 94; } if ( !draw_done ){ erase = 1; } } pacer_count++; if ( start != 0 ) // for beginning pacing start--; else TCCR0 = 0b00011100; // enable DDS } // pace_count == 30 if ( pace_count == 5 && start == 0 ){ if ( r_index != old_r_index ) {// key has been pressed keystrike = r_buffer[old_r_index]; // get the keystrike sound = 1; // to output sound OCR0 = soundarray[0]; old_r_index = r_index; } else sound = 0; if ( playing_duration == 0 ){ if (correct_strike == 2 ){ // if no set value for correct_strike keystrike = r_buffer[strike_index]; notetable_index = score_stored[score_choice][score_index]; if ( keystrike == notetable[notetable_index] ){ correct_strike = 1; } else { correct_strike = 0; } } if (r_index != 0 ) // if r_index is not 0 r_index--; // decrement the value to save buffer size r_buffer[r_index] = 'n'; // set the goiong to read value from r_buffer to 'n' strike_index = r_index; // update strike_index old_r_index = r_index; if ( pacer_count >= MAX_PACER || score_stored[score_choice][score_index] == 'q' ){ playing_score = 0; TCCR0 = 0; // stop DDS } score_index++; playing_duration = score_stored[score_choice][score_index]; score_index++; check_x = pacer_x; check_y = pacer_y-30; check_note = 1; } playing_duration--; // decrement } } // playing score if ( display_result ){ if ( actual_point > max_point[score_choice] ) max_point[score_choice] = actual_point; //convert from integer to string itoa(actual_point, ap); itoa(total_point, tp); itoa(max_point[score_choice], hp); }// line 227 if ( LineCount == 229 ){// line229 if ( sound && pace_count == 6){ soundarray_index = keystrike & 0x0f; if (soundarray_index >= 0 && soundarray_index <= 7 && soundarray[soundarray_index]>0) TCCR0 = 0b00011100 ; //not a rest if (soundarray_index >= 0 && soundarray_index <= 8) OCR0 = soundarray[soundarray_index]; old_r_index = r_index; } }//end of line229 if( LineCount == 231 ){ // line 231 if ( erase ){ //if time to erase the score if ( score_y == 32 ){ // erase upper score for( i = 50; i< 768; i++ ){ // 50 = 16*3 + 2; 768 = 800 -16*2 screen[i] = 0; } } else{ // erase lower score for( i = 850; i< 1568; i++ ){ //850 = 800+16*3; 1568 = 1600-16*2 screen[i] = 0; } } erase = 0; // update the flags' value display_score = 1; } else if ( draw_clef ){ if ( score_y == 32 ){ // draw lower score; score_y has been updated video_puthigh(2,59, 0); video_puthigh(10,59, 1); video_hline( 69 ); video_hline( 73 ); video_hline( 77 ); video_hline( 81 ); video_hline( 85 ); } else{ // draw upper score video_puthigh(2, 11, 0); video_puthigh(10,11, 1); video_hline( 19 ); video_hline( 23 ); video_hline( 27 ); video_hline( 31 ); video_hline( 35 ); if ( start != 0 ){ display_score = 1; } } draw_clef = 0; } else if ( display_score && ( ( start!=0 ) || ( (start==0) && pace_count==15) ) ) { video_putnote(note_x, note_y, display_duration); if ( note_y == 30 || note_y == 80 ) { video_line( note_x-2, note_y+9 , note_x+6, note_y+9 , 1 ); } if ( score_x >= 126 ) { //if should go to next line score_x = 30; if ( display_score_index >= MAX_SCORE_INDEX || score_stored[score_choice][display_score_index] == 'q'){ draw_done = 1; } if ( score_y == 32 ){ score_y = 82; // display_score = 0; } else{ score_y = 32; } draw_clef = 1; display_score = 0; } } // display score if ( playing_score ){ if ( pace_count == 0 ){ // if time to put pacer video_putsymbol( pacer_x, pacer_y, 0); // output the pacer } if ( check_note ){ // if time to display show correctness if ( correct_strike ){ actual_point++; total_point++; video_putsymbol( check_x, check_y, 1); // correct symbol } else{ total_point++; video_putsymbol( check_x, check_y, 2); // incorrect symbol } check_note = 0; // reset the values correct_strike = 2; } }// playing_score if ( display_result ){ switch ( result_state ){ case ERASE_SCREEN: for ( i=0; i<1600; i++ ) // clear the screen screen[i] = 0; finalscore = (float)(((float)actual_point)/total_point); result_state = SCORE_DISPLAY; break; case SCORE_DISPLAY: if ( finalscore> 0.9 ) video_puts(25,10,EXCELLENT); else if (finalscore > 0.7 ) video_puts(25,10,GOOD); else if (finalscore > 0.5 ) video_puts(25,10,PASS); else{ video_puts(25,10,FAIL); } video_puts(55,20,SCOREDISPLAY); result_state = HIGH_SCORE; break; case HIGH_SCORE: video_puts(25,40,HIGHEST); video_puts(75,40,SCOREDISPLAY); result_state = SCORE_NUMBER; break; case SCORE_NUMBER: video_puts(25,20,YOUR); video_puts(40,30,OUT); video_puts(60,30,OF); video_puts(25,30,ap); video_puts(75,30,tp); result_state = NOTE_ONE; break; case NOTE_ONE: video_puts(110,40,hp); video_putnote(20, 85 , 2); video_putnote(30, 75 , 3); video_puthigh(10,12, 1); result_state = NOTE_TWO; break; case NOTE_TWO: video_putnote(40, 68 , 1); video_putnote(50, 72 , 2); video_putnote(60, 68 , 3); result_state = NOTE_THREE; break; case NOTE_THREE: video_putnote(70, 70 , 4); video_putnote(80, 80 , 5); video_putnote(90, 66 , 6); result_state = NOTE_FOUR; break; case NOTE_FOUR: video_putnote(100, 63 , 7); video_putnote(110, 65 , 8); video_puthigh(2,12, 0); result_state = ERASE_SCREEN; display_result = 0; break; } // switch case } // display_result } //line 231 } //while } //main