//video gen and sound //C.6 is sync:1000 ohm + diode to 75 ohm resistor //C.7 is video:330 ohm + diode to 75 ohm resistor #pragma regalloc- //I allocate the registers myself #pragma optsize- //optimize for speed #include #include #include #include #include #include "common.h" //common between audio & video boards //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 line13 208 #define line21 336 #define line29 464 #define line37 592 #define line45 720 //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; //animation char x, y, s, vx, vy, a, b, c, d, CMNDIn; //the six notes being displayed on the screen char notes[6]; //the screen char screen[1600], t, ts[10]; // frame 0 or 1 of 2-frame cycle char count; //misc variables unsigned char badCount; //frame count for "bad note" indicator unsigned char mode; //current mode (song or freeplay) unsigned char sel; //current selected menu item //menu items char menuItem0[] = "RESUME"; char menuItem1[] = "FREEPLAY"; char menuItem2[] = "HOT CROSS BUNS"; char menuItem3[] = "YANKEE DOODLE"; char menuItem4[] = "ODE TO JOY"; char eraseMenu[] = " "; //lookup table for vertical positions of notes flash char vertposTable[] = {5,9,13,17,21,25,10,14,18}; //lookup table for finger presses flash char fingerpressTable[] = {0x01,0x03,0x02,0x06,0x04,0x0C,0x08,0x18,0x10}; //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 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, 0b11001100, 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 }; //================================== //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) { //start the Horizontal sync pulse PORTC = syncON; //update the curent scanline number LineCount ++ ; //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b01000000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b01000000; 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 PORTC = 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 portC.7 ;the nop can be eliminated to make the display narrower .macro videobits ;regnum BST @0,7 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,6 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,5 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,4 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,3 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,2 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,1 IN R30,0x15 BLD R30,7 nop OUT 0x15,R30 BST @0,0 IN R30,0x15 BLD R30,7 nop OUT 0x15,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,0x15 BLD R30,7 OUT 0x15,R30 pop r19 pop r18 pop r17 pop r16 pop r15 pop r14 #endasm 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){ #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 } #pragma warn+ //================================== // put a big character on the screen // c is index into bitmap void video_putchar(char x, char y, char c) { 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 } //================================== // put a string of big characters on the screen void video_puts(char x, char y, char *str) { 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 } //================================== // 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) { 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); } //================================== // 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) { 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 if (str[i]==0x20) video_smallchar(x,y,12); else video_smallchar(x,y,str[i]-0x40+12); x = x+4; 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) { 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 } //================================== //return the value of one point //at x,y with color nonzero=white 0=black char video_set(char x, char y) { //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))); } //Plot a horizontal line void video_line_h(char x1, char y1, char x2, char y2, char c) { char ii; for (ii = x1; ii <= x2; ii++) { video_pt(ii, y1, c); } } //Plot a vertical line void video_line_v(char x1, char y1, char x2, char y2, char c) { char ii; for (ii = y1; ii <= y2; ii++) { video_pt(x1, ii, c); } } //Draw and erase notes and/or thier fret dots void draw(char note, char pos, char dotonly, char color) { a = pos*17 + 31; b = vertposTable[note]; if (note == NoNote) return; if (!dotonly) { if (note < 6) { //music note pointing down video_line_v(a+1, b+4, a+1, b+26, color); video_line_v(a+2, b+3, a+2, b+7, color); video_line_v(a+3, b+2, a+3, b+7, color); video_line_v(a+4, b+1, a+4, b+7, color); video_line_v(a+5, b+1, a+5, b+7, color); video_line_v(a+6, b+1, a+6, b+6, color); video_line_v(a+7, b+1, a+7, b+5, color); video_line_v(a+8, b+2, a+8, b+4, color); } else { //music note pointing up video_line_v(a+8, b+1, a+8, b+23, color); video_line_v(a+7, b+20, a+7, b+24, color); video_line_v(a+6, b+20, a+6, b+25, color); video_line_v(a+5, b+20, a+5, b+26, color); video_line_v(a+4, b+20, a+4, b+26, color); video_line_v(a+3, b+21, a+3, b+26, color); video_line_v(a+2, b+22, a+2, b+26, color); video_line_v(a+1, b+23, a+1, b+25, color); } } //Draw fret dots if (fingerpressTable[note] & 0x10) { c = 50; video_line_h(a+4, c+3, a+5, c+3, color); video_line_h(a+3, c+4, a+6, c+4, color); video_line_h(a+3, c+5, a+6, c+5, color); video_line_h(a+4, c+6, a+5, c+6, color); } if(fingerpressTable[note] & 0x08) { c = 60; video_line_h(a+4, c+3, a+5, c+3, color); video_line_h(a+3, c+4, a+6, c+4, color); video_line_h(a+3, c+5, a+6, c+5, color); video_line_h(a+4, c+6, a+5, c+6, color); } if(fingerpressTable[note] & 0x04) { c = 70; video_line_h(a+4, c+3, a+5, c+3, color); video_line_h(a+3, c+4, a+6, c+4, color); video_line_h(a+3, c+5, a+6, c+5, color); video_line_h(a+4, c+6, a+5, c+6, color); } if(fingerpressTable[note] & 0x02) { c = 80; video_line_h(a+4, c+3, a+5, c+3, color); video_line_h(a+3, c+4, a+6, c+4, color); video_line_h(a+3, c+5, a+6, c+5, color); video_line_h(a+4, c+6, a+5, c+6, color); } if(fingerpressTable[note] & 0x01) { c = 90; video_line_h(a+4, c+3, a+5, c+3, color); video_line_h(a+3, c+4, a+6, c+4, color); video_line_h(a+3, c+5, a+6, c+5, color); video_line_h(a+4, c+6, a+5, c+6, color); } } //Draw 5 vertical bubbles for the to-press/pressed button indicator void draw_circles(char pos, char color) { //finger press 1 c = pos*17 + 31; d = 50; video_line(c+4,d,c+5,d,color); video_line(c+2,d+1,c+3,d+1,color); video_line(c+6,d+1,c+7,d+1,color); video_line(c+1,d+2,c+1,d+3,color); video_line(c+8,d+2,c+8,d+3,color); video_line(c,d+4,c,d+5,color); video_line(c+9,d+4,c+9,d+5,color); video_line(c+8,d+6,c+8,d+7,color); video_line(c+1,d+6,c+1,d+7,color); video_line(c+2,d+8,c+3,d+8,color); video_line(c+6,d+8,c+7,d+8,color); video_line(c+4,d+9,c+5,d+9,color); //finger press 2 //c = 30; d = 60; video_line(c+4,d,c+5,d,color); video_line(c+2,d+1,c+3,d+1,color); video_line(c+6,d+1,c+7,d+1,color); video_line(c+1,d+2,c+1,d+3,color); video_line(c+8,d+2,c+8,d+3,color); video_line(c,d+4,c,d+5,color); video_line(c+9,d+4,c+9,d+5,color); video_line(c+8,d+6,c+8,d+7,color); video_line(c+1,d+6,c+1,d+7,color); video_line(c+2,d+8,c+3,d+8,color); video_line(c+6,d+8,c+7,d+8,color); video_line(c+4,d+9,c+5,d+9,color); //finger press 3 //c = 30; d = 70; video_line(c+4,d,c+5,d,color); video_line(c+2,d+1,c+3,d+1,color); video_line(c+6,d+1,c+7,d+1,color); video_line(c+1,d+2,c+1,d+3,color); video_line(c+8,d+2,c+8,d+3,color); video_line(c,d+4,c,d+5,color); video_line(c+9,d+4,c+9,d+5,color); video_line(c+8,d+6,c+8,d+7,color); video_line(c+1,d+6,c+1,d+7,color); video_line(c+2,d+8,c+3,d+8,color); video_line(c+6,d+8,c+7,d+8,color); video_line(c+4,d+9,c+5,d+9,color); //finger press 4 //c = 30; d = 80; video_line(c+4,d,c+5,d,color); video_line(c+2,d+1,c+3,d+1,color); video_line(c+6,d+1,c+7,d+1,color); video_line(c+1,d+2,c+1,d+3,color); video_line(c+8,d+2,c+8,d+3,color); video_line(c,d+4,c,d+5,color); video_line(c+9,d+4,c+9,d+5,color); video_line(c+8,d+6,c+8,d+7,color); video_line(c+1,d+6,c+1,d+7,color); video_line(c+2,d+8,c+3,d+8,color); video_line(c+6,d+8,c+7,d+8,color); video_line(c+4,d+9,c+5,d+9,color); //finger press 5 //c = 30; d = 90; video_line(c+4,d,c+5,d,color); video_line(c+2,d+1,c+3,d+1,color); video_line(c+6,d+1,c+7,d+1,color); video_line(c+1,d+2,c+1,d+3,color); video_line(c+8,d+2,c+8,d+3,color); video_line(c,d+4,c,d+5,color); video_line(c+9,d+4,c+9,d+5,color); video_line(c+8,d+6,c+8,d+7,color); video_line(c+1,d+6,c+1,d+7,color); video_line(c+2,d+8,c+3,d+8,color); video_line(c+6,d+8,c+7,d+8,color); video_line(c+4,d+9,c+5,d+9,color); } //================================== // set up the ports and timers void main(void) { unsigned char ii; //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 //set up SPI: slave, mode 3, LSB first SPCR = 0x6E; //init notes to NoNote for (ii = 0; ii < 8; ii++) { notes[ii] = NoNote; } //init ports DDRC = 0xf0; //video out and switches //C.6 is sync:1000 ohm + diode to 75 ohm resistor //C.7 is video:330 ohm + diode to 75 ohm resistor DDRB = 0b01001000; // MISO and PB3 are outputs, rest are inputs PORTB.3 = 1; // PB3 (which is RQ) initially high DDRD = 0xFF; // PORTD is output (LED is on PD7) PORTD = 0x00; // LED initially on //initialize to freeplay mode mode = FreeMode; //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //initialize counters count = 0; badCount = 0; //draw treble clef x = 1; //zeroth pixel in x direction y = -1; //zeroth pixel in y direction video_line(x+13, y+1, x+15, y+1, 1); video_line(x+12, y+2, x+16, y+2, 1); video_line(x+12, y+3, x+16, y+3, 1); video_line(x+11, y+4, x+17, y+4, 1); video_line(x+11, y+5, x+13, y+5, 1); video_line(x+16, y+5, x+17, y+5, 1); video_line(x+10, y+6, x+10, y+16, 1); video_line(x+11, y+6, x+11, y+23, 1); video_line(x+12, y+6, x+12, y+7, 1); video_line(x+17, y+6, x+17, y+16, 1); video_line(x+18, y+6, x+18, y+14, 1); video_line(x+16, y+12, x+16, y+18, 1); video_line(x+15, y+14, x+15, y+19, 1); video_line(x+14, y+16, x+14, y+20, 1); video_line(x+13, y+17, x+13, y+21, 1); video_line(x+12, y+18, x+12, y+26, 1); video_line(x+13, y+26, x+13, y+27, 1); video_line(x+10, y+20, x+10, y+24, 1); video_line(x+9, y+21, x+9, y+25, 1); video_line(x+8, y+22, x+8, y+26, 1); video_line(x+7, y+23, x+7, y+28, 1); video_line(x+6, y+24, x+6, y+29, 1); video_line(x+5, y+25, x+5, y+31, 1); video_line(x+4, y+27, x+4, y+33, 1); video_line(x+3, y+28, x+3, y+40, 1); video_line(x+2, y+30, x+2, y+38, 1); video_line(x+1, y+33, x+1, y+36, 1); video_line(x+12, y+28, x+18, y+28, 1); video_line(x+11, y+29, x+19, y+29, 1); video_line(x+10, y+30, x+20, y+30, 1); video_line(x+9, y+31, x+21, y+31, 1); video_line(x+8, y+32, x+11, y+32, 1); video_line(x+8, y+33, x+10, y+33, 1); video_line(x+7, y+34, x+9, y+34, 1); video_line(x+7, y+35, x+7, y+37, 1); video_line(x+8, y+35, x+8, y+39, 1); video_pt(x+9, y+40, 1); video_pt(x+10, y+41, 1); video_line(x+14, y+32, x+14, y+36, 1); video_line(x+15, y+36, x+15, y+41, 1); video_line(x+16, y+41, x+16, y+47, 1); video_line(x+8, y+35, x+8, y+39, 1); video_line(x+18, y+32, x+22, y+32, 1); video_line(x+20, y+33, x+22, y+33, 1); video_line(x+21, y+34, x+21, y+35, 1); video_line(x+22, y+34, x+22, y+41, 1); video_line(x+23, y+34, x+23, y+39, 1); video_line(x+21, y+40, x+21, y+42, 1); video_line(x+20, y+42, x+20, y+43, 1); video_line(x+19, y+43, x+19, y+44, 1); video_line(x+17, y+44, x+18, y+44, 1); video_line(x+9, y+45, x+17, y+45, 1); video_line(x+7, y+44, x+9, y+44, 1); video_line(x+6, y+43, x+7, y+43, 1); video_line(x+5, y+42, x+6, y+42, 1); video_line(x+4, y+41, x+5, y+41, 1); video_line(x+4, y+39, x+4, y+40, 1); video_line(x+17, y+47, x+17, y+57, 1); video_line(x+18, y+51, x+18, y+56, 1); video_line(x+16, y+57, x+16, y+58, 1); video_line(x+15, y+58, x+15, y+59, 1); video_line(x+10, y+59, x+15, y+59, 1); video_line(x+9, y+58, x+11, y+58, 1); video_line(x+8, y+57, x+11, y+57, 1); video_line(x+7, y+56, x+12, y+56, 1); video_line(x+7, y+55, x+13, y+55, 1); video_line(x+7, y+54, x+13, y+54, 1); video_line(x+7, y+53, x+13, y+53, 1); video_line(x+8, y+52, x+12, y+52, 1); video_line(x+9, y+51, x+11, y+51, 1); //draw bubbles for tabs draw_circles(5, 1); //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); //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"); //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) { PORTD = ~PORTD; //toggle LED PORTB.3 = 0; //bring RQ low CMNDIn = SPDR; //read command from SPI //State machine for received video commands switch (CMNDIn & 0xF0) { //Shift a note in from the right case SLSET: if (count == 0) { for (ii = 0; ii < 3; ii++) { draw(notes[ii], ii, 0, 0); notes[ii] = notes[ii+1]; draw(notes[ii], ii, 0, 1); } } else { for (ii = 3; ii < 5; ii++) { draw(notes[ii], ii, 0, 0); notes[ii] = notes[ii+1]; draw(notes[ii], ii, 0, 1); } draw(notes[5], 5, 0, 0); notes[5] = CMNDIn & 0x0F; draw(notes[5], 5, 0, 1); } break; //set the right most note and then shift all left case SETSL: if (count == 0) { for (ii = 0; ii < 3; ii++) { draw(notes[ii], ii, 0, 0); notes[ii] = notes[ii+1]; draw(notes[ii], ii, 0, 1); } } else { draw(notes[5], 5, 0, 0); notes[5] = CMNDIn & 0x0F; for (ii = 3; ii < 5; ii++) { draw(notes[ii], ii, 0, 0); notes[ii] = notes[ii+1]; draw(notes[ii], ii, 0, 1); } notes[5] = CMNDIn & 0x0F; draw(notes[5], 5, 0, 1); } break; //draw note in 6th position case SET: draw(notes[5], 5, 0, 0); notes[5] = CMNDIn & 0x0F; draw(notes[5], 5, 0, 1); break; case LOAD: if (count == 0) { for (ii = 0; ii < 5; ii++) { notes[ii] = notes[ii+1]; } notes[5] = CMNDIn & 0x0F; } break; //Display the contents of the note buffer case DISP: if (count == 0) { for (ii = 0; ii < 6; ii++) draw(notes[ii], ii, 0, 1); } break; //Draw the menu case MENU: if (count == 0) { for (ii = 0; ii < 6; ii++) draw(notes[ii], ii, 1, 0); if (mode == FreeMode) draw_circles(5, 0); else draw_circles(0, 0); sel = 0; } else { if ((CMNDIn & 0x0F) == 0x01) { video_putsmalls(32, 56, menuItem0); video_putsmalls(32, 65, menuItem1); video_putsmalls(32, 74, menuItem2); video_putsmalls(32, 83, menuItem3); video_putsmalls(32, 92, menuItem4); } else { video_putsmalls(32, 56, menuItem1); video_putsmalls(32, 65, menuItem2); video_putsmalls(32, 74, menuItem3); video_putsmalls(32, 83, menuItem4); } video_line_h(30, 54, 88, 54, 1); video_line_h(30, 62, 88, 62, 1); video_line_v(30, 55, 30, 61, 1); video_line_v(88, 55, 88, 61, 1); } break; //change the selected menu item case MENUS: x = 54+sel*9; y = 62+sel*9; video_line_h(30, x, 88, x, 0); video_line_h(30, y, 88, y, 0); video_line_v(30, x+1, 30, y-1, 0); video_line_v(88, x+1, 88, y-1, 0); sel = CMNDIn & 0x0F; x = 54+sel*9; y = 62+sel*9; video_line_h(30, x, 88, x, 1); video_line_h(30, y, 88, y, 1); video_line_v(30, x+1, 30, y-1, 1); video_line_v(88, x+1, 88, y-1, 1); break; //clear the menu case CLRM: if (count == 0) { x = 54+sel*9; y = 62+sel*9; video_line_h(30, x, 88, x, 0); video_line_h(30, y, 88, y, 0); video_line_v(30, x+1, 30, y-1, 0); video_line_v(88, x+1, 88, y-1, 0); video_putsmalls(32, 56, eraseMenu); video_putsmalls(32, 65, eraseMenu); video_putsmalls(32, 74, eraseMenu); video_putsmalls(32, 83, eraseMenu); video_putsmalls(32, 92, eraseMenu); } else { if (mode == FreeMode) draw_circles(5, 1); else draw_circles(0, 1); for (ii = 0; ii < 6; ii++) draw(notes[ii], ii, 1, 1); } break; //clear the staff and draw the bubbles on the left or right of the screen case MODE: if (count == 0) { for (ii = 0; ii < 6; ii++) { draw(notes[ii], ii, 0, 0); notes[ii] = NoNote; } } else { if (mode == FreeMode) draw_circles(5, 0); else draw_circles(0, 0); mode = CMNDIn & 0x0F; if (mode == FreeMode) draw_circles(5, 1); else draw_circles(0, 1); } break; //draw a small x in the bottom corner of the screen case BAD: if (count == 0) { video_line(10,70,18,78,1); video_line(18,70,10,78,1); badCount = 0; } break; //no explicit operation, erase bad note indicator after 1/6 second case NOP: if (badCount == 10) { video_line(10,70,18,78,0); video_line(18,70,10,78,0); badCount = 11; } break; } //increment counter for bad note indicator if (badCount < 10) badCount++; //draw the staff lines efficiently for (ii = 0; ii < 16; ii++) { screen[line13+ii] = 0xFF; screen[line21+ii] = 0xFF; screen[line29+ii] = 0xFF; screen[line37+ii] = 0xFF; screen[line45+ii] = 0xFF; } // toggle frame counter, pull RQ high if second frame has finished if (count == 1) { PORTB.3 = 1; count = 0; } else { count++; } } //line 231 } //while } //main