define WIDTH 5 ; width of graphic
define HEIGHT 5 ; height of graphic
lda #$00 ; create a pointer at $10
sta $10 ; which points to where
lda #$02 ; the graphic should be drawn
sta $11
lda #$00 ; number of rows we've drawn
sta $12 ; is stored in $12
ldx #$00 ; index for data
ldy #$00 ; index for screen column
move:
jsr clearScreen
;brk
jsr moveDown
jsr moveRight
;draw will modify our pointer, we need to backup and reset it later
lda $10
sta $14;backup
lda $11
sta $15
jsr draw
lda $14
sta $10;reset
lda $15
sta $11
;brk
jsr move
draw:
lda data,x
sta ($10),y
inx
iny
cpy #WIDTH
;brk
bne draw
inc $12 ; increment row counter
lda #HEIGHT ; are we done yet?
cmp $12
bne continue ; if not equal, go to continue
;beq done; ; comment out bne continue and beq done > movin
;lda $04;
;sta $11
ldx #$00; //reset index
ldy #$00;
sty $12
rts
continue:
;brk;
lda $10 ; load pointer
clc
adc #$20 ; add 32 to drop one row
sta $10
lda $11 ; carry to high byte if needed
adc #$00
cmp #$06
beq done
sta $11
ldy #$00
beq draw
;rts
done: brk ; stop when finished
;rts
data:
dcb 00,03,03,03,00
dcb 07,00,00,00,07
dcb 07,00,00,00,07
dcb 07,00,00,00,07
dcb 00,03,03,03,00
moveRight:
sty $13
ldy $10
iny
sty $10
ldy $13
rts
moveDown:
lda $10
clc
adc #$20 ;add a line
sta $10
lda $11
adc #$00
cmp #$06
beq done
sta $11
rts
resetPage:
lda #$02
sta $11
rts
clearScreen: ;we can create another sub route called clearPrevious to avoid flashing screen
sta $22 ;backup registers
stx $23
sty $24
lda #$00 ; create a pointer at $80
sta $20 ; which points to where
lda #$02 ; the graphic should be drawn
sta $21
lda #$00 ;black color
ldy #$00 ;
mloop:
sta ($20), y
iny
bne mloop
ldx $21
inx
stx $21
cpx #$06
bne mloop
lda $22 ;revert registers
ldx $23
ldy $24
rts
The code above is what I’ve done so far.
In Lab 3 our group will create a shape that will bounce around that will reflect when it reaches screen edge.
So far, the shape is able to move from top left to bottom right. But when the it is drawing the picture at bottom right, it will write data to 0x0600, where our program located. Then the program crushed.
Procedure:
When the program starts, we load the image from dcb data. and draw it at the address where our pointer point at. Then we modify the pointer, clear screen, then draw it again.
move:
jsr clearScreen
;brk
jsr moveDown
jsr moveRight
;draw will modify our pointer, we need to backup and reset it later
lda $10
sta $14;backup
lda $11
sta $15
jsr draw
lda $14
sta $10;reset
lda $15
sta $11
;brk
jsr move
However, draw modifies our pointer to complete drawing, therefore, we backed it up and restored it after drawing finished. After restoring pointer, we come back to move sub route, and clear screen again, then modify our pointer and draw it again.
clearScreen: ;we can create another sub route called clearPrevious to avoid flashing screen
sta $22 ;backup registers
stx $23
sty $24
lda #$00 ; create a pointer at $80
sta $20 ; which points to where
lda #$02 ; the graphic should be drawn
sta $21
lda #$00 ;black color
ldy #$00 ;
mloop:
sta ($20), y
iny
bne mloop
ldx $21
inx
stx $21
cpx #$06
bne mloop
lda $22 ;revert registers
ldx $23
ldy $24
rts
Clear screen is simple. we fill 00 from address 0x0200 until 0x05FF. But it is very slow. This is planned to be optimized. Because we know where we drew, so we just need to erase that addresses.
moveRight:
sty $13
ldy $10
iny
sty $10
ldy $13
rts
moveDown:
lda $10
clc
adc #$20 ;add a line
sta $10
lda $11
adc #$00
cmp #$06
beq done
sta $11
rts
moveRight and moveDown is also simple.
In moveRight, we just increased pointer index by 1, so it moves right 1 pixel. But the shape does not reflect after it reaches right edge. We need to check if it reaches right edge and change the increase so decrease if it reaches right edge.
In moveDown, we increase pointer index by 32(hex 20). if there is a borrow, it means we need to switch page(e.g. from 0x0200 to 0x0300), so we add 0. if there is a borrow, it will add 1 instead of 0.
We will finish this lab next week. We plan to create an X and Y coordinator function to manage where our shape is, so we can easily to deal with the reflection issue.
To be continued!