Code:
if (LineCount<ScreenBot && 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 r12
;push r13
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 r19
;pop r18
;pop r17
;pop r16
;pop r15
;pop r14
pop r26
pop r27
#endasm
delay_us(7); //adjust to center image on screen
//blast 16 bytes to the screen
PORTD.6=v1 & 0b10000000;
PORTD.6=v1 & 0b01000000;
PORTD.6=v1 & 0b00100000;
PORTD.6=v1 & 0b00010000;
PORTD.6=v1 & 0b00001000;
PORTD.6=v1 & 0b00000100;
PORTD.6=v1 & 0b00000010;
PORTD.6=v1 & 0b00000001;
PORTD.6=v2 & 0b10000000;
PORTD.6=v2 & 0b01000000;
PORTD.6=v2 & 0b00100000;
PORTD.6=v2 & 0b00010000;
PORTD.6=v2 & 0b00001000;
PORTD.6=v2 & 0b00000100;
PORTD.6=v2 & 0b00000010;
PORTD.6=v2 & 0b00000001;
PORTD.6=v3 & 0b10000000;
PORTD.6=v3 & 0b01000000;
PORTD.6=v3 & 0b00100000;
PORTD.6=v3 & 0b00010000;
PORTD.6=v3 & 0b00001000;
PORTD.6=v3 & 0b00000100;
PORTD.6=v3 & 0b00000010;
PORTD.6=v3 & 0b00000001;
PORTD.6=v4 & 0b10000000;
PORTD.6=v4 & 0b01000000;
PORTD.6=v4 & 0b00100000;
PORTD.6=v4 & 0b00010000;
PORTD.6=v4 & 0b00001000;
PORTD.6=v4 & 0b00000100;
PORTD.6=v4 & 0b00000010;
PORTD.6=v4 & 0b00000001;
PORTD.6=v5 & 0b10000000;
PORTD.6=v5 & 0b01000000;
PORTD.6=v5 & 0b00100000;
PORTD.6=v5 & 0b00010000;
PORTD.6=v5 & 0b00001000;
PORTD.6=v5 & 0b00000100;
PORTD.6=v5 & 0b00000010;
PORTD.6=v5 & 0b00000001;
PORTD.6=v6 & 0b10000000;
PORTD.6=v6 & 0b01000000;
PORTD.6=v6 & 0b00100000;
PORTD.6=v6 & 0b00010000;
PORTD.6=v6 & 0b00001000;
PORTD.6=v6 & 0b00000100;
PORTD.6=v6 & 0b00000010;
PORTD.6=v6 & 0b00000001;
PORTD.6=v7 & 0b10000000;
PORTD.6=v7 & 0b01000000;
PORTD.6=v7 & 0b00100000;
PORTD.6=v7 & 0b00010000;
PORTD.6=v7 & 0b00001000;
PORTD.6=v7 & 0b00000100;
PORTD.6=v7 & 0b00000010;
PORTD.6=v7 & 0b00000001;
PORTD.6=v8 & 0b10000000;
PORTD.6=v8 & 0b01000000;
PORTD.6=v8 & 0b00100000;
PORTD.6=v8 & 0b00010000;
PORTD.6=v8 & 0b00001000;
PORTD.6=v8 & 0b00000100;
PORTD.6=v8 & 0b00000010;
PORTD.6=v8 & 0b00000001;
//the rest of the image is blasted in asm
//becuase the registers don't have C names
#asm
;byte 9
BST R12,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST R12,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 10
BST r13,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r13,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 11
BST r14,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r14,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 12
BST r15,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r15,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 13
BST r16,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r16,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 14
BST r17,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r17,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 15
BST r18,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r18,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
;byte 16
BST r19,7
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,6
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,5
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,4
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,3
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,2
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,1
IN R30,0x12
BLD R30,6
OUT 0x12,R30
BST r19,0
IN R30,0x12
BLD R30,6
OUT 0x12,R30
clt ;clear 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
;pop r30
#endasm
end
end
//==================================
//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+
//==================================
// 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
//==================================
//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
//==================================
//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
//==================================
// set up the ports and timers
void main(void)
begin
//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 = 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
//init timer 0 to 1/uSec
TCCR0 = 2;
//initialize synch constants
LineCount = 1;
syncON = 0b00000000;
syncOFF = 0b00100000;
//Print "CORNELL"
video_puts(13,3,cu1);
//Print "ECE476"
video_puts(65,3,cu2);
//side lines
#define width 126
video_line(0,0,0,99,1);
video_line(width,0,width,99,1);
//top line & bottom lines
video_line(0,0,width,0,1);
video_line(0,99,width,99,1);
video_line(0,11,width,11,1);
video_line(0,89,width,89,1);
//init software timer
t=0;
time=0;
//init animation
x = 64;
y=50;
vx=1;
vy=1;
video_pt(x,y,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)
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_line(10,30,90,30,1);
video_line(10,31,90,31,1);
video_line(10,60,60,60,1);
video_line(10,61,60,61,1);
//a clock
if (++t>59)
begin
t=0;
time = time + 1;
sprintf(ts,"%05d",time);
video_putsmalls(92,93,ts);
end
//the animated point
video_pt(x,y,0) ; //erase old one
x = x + vx;
y = y + vy;
s = video_set(x,y);
if (s>0) vy = -vy; //if true, must be a line there
if (x==124 || x==1) vx = -vx;
if (y==88 || y==12) vy = -vy;
video_pt(x,y,1) ;
end //line 231
end //while
end //main
Lesezeichen