//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 //======================= // video stuff char* screen_pointer; char lastInputFlag; unsigned int screen_counter; //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; char screen[1600]; //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 t1 60 #pragma regalloc+ char syncON, syncOFF; int LineCount; unsigned char increment; unsigned char accumulator; char sineTable[256]; //need loc to avoid glitch unsigned char soundMode, playSound; //================================== //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) { //generate sound accumulator = accumulator + increment; OCR0 = 128 + sineTable[accumulator]; //start the Horizontal sync pulse PORTD = syncON; //update the curent scanline number LineCount ++ ; //{ inverted (Vertical) synch after line 247 if (LineCount==248) { syncON = 0b00100000; syncOFF = 0; } //back to regular sync after line 250 if (LineCount==251) { syncON = 0; syncOFF = 0b00100000; } //start new frame after line 262 if (LineCount==263) { LineCount = 1; } //delay_us(2); //adjust to make 5 us pulses //} sync pulse PORTD = syncOFF; if (LineCount=ScreenTop) { //read from master if(PINB.0!=lastInputFlag) { if(PINB.2==1) { screen_pointer = screen; //pad instructions #asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop #endasm } else { *screen_pointer++ = PINA; *screen_pointer++ = PINC; } lastInputFlag = PINB.0; PORTB = PORTB ^ 0x02; //flag ready } else { //pad instructions #asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop #endasm } //delay_us(1); //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 //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 //read from master if(PINB.0!=lastInputFlag) { if(PINB.2==1) { screen_pointer = screen; #asm nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop #endasm } else { *screen_pointer++ = PINA; *screen_pointer++ = PINC; } lastInputFlag = PINB.0; PORTB = PORTB ^ 0x02; //flag ready } } } #pragma warn+ //================================== // set up the ports and timers void main(void) { //init timer 1 to generate sync OCR1A = lineTime; //One NTSC line TCCR1B = 0b00001001; //full speed; clear-on-match TCCR1A = 0x00; //turn off pwm and oc lines TIMSK = 0x10; //enable interrupt T1 cmp TCCR0 = 1; OCR0 = 0; //init ports DDRD = 0xf0; //video out and switches //D.5 is sync:1000 ohm + diode to 75 ohm resistor //D.6 is video:330 ohm + diode to 75 ohm resistor //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; //set up communication ports DDRB=0b00001010; //PORTA[0,2] = input, rest output PINB.1 = 1; DDRA=0x00; //PORTA = input DDRC=0x00; //PORTC = input screen_pointer = screen; lastInputFlag = 0; screen_counter = 0; screen[100] = 0xff; screen[101] = 0xff; screen[102] = 0xff; screen[103] = 0xff; screen[104] = 0xff; for (i=0; i<256; i++) { sineTable[i] = (char)(63.5 * sin(6.283*((float)i)/256.0)) ; } soundMode = 0; playSound = 0; //enable sleep mode MCUCR = 0b10000000; #asm ("sei"); //The following loop executes once/video line during lines //1-230, then does all of the frame-} 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"); if(LineCount>230 && LineCount<260) { if(PINB.0!=lastInputFlag) { if(PINB.2==1) { screen_pointer = screen; } else { *screen_pointer++ = PINA; *screen_pointer++ = PINC; } lastInputFlag = PINB.0; PORTB = PORTB ^ 0x02; //flag ready } if(LineCount==259) { //check for audio flags soundMode = 0; if(PINB.4) soundMode++; if(PINB.5) soundMode+=2; if(soundMode>0) { TCCR0 = 0b01101001; if(soundMode==1) { //walking increment = 6; } else if(soundMode==2) { //error increment = 4; } else if(soundMode==3) { //win increment = 8; } } else { TCCR0 = 1; } } } } //while } //main