Jump to content
IGNORED

making Locate work in assembly


Recommended Posts

This modified version of code by Gauntman allows you to use a joystick to move a text character around the screen.

 

What I'm trying to do now is the assembly-language equivalent of Basic's Locate command but the character you control ploughs through everything. The joystick routines make sure you're not exiting the screen, then (supposedly) locates what's left, right, above, or below you, erases your current position, and then moves you to the new place.

 

*=$4000

begin   lda #<scr       ; start by calculating look-up tables
       sta $d0         ; at the same time, we will clear the
       lda #>scr       ; screen
       sta $d1

       ldx #0		
c0      lda #0		; draw a character
       ldy #39         ; clear a single line
c1      ;sta ($d0),y
       dey
       bpl c1

       lda $d1         ; store address into look-up table
       sta schi,x
       lda $d0
       sta sclo,x
       clc             ; update zero page to next line
       adc #40         ; 40 bytes per line
       sta $d0
       bcc c2
       inc $d1
c2      inx             ; for all 24 lines...
       cpx #24
       bne c0

       lda #4          ; set some colors
       sta 708         ; make ANTIC4 slightly more readable
       lda #6
       sta 709
       lda #8
       sta 710

       lda #<DLIST     ; finally, point ANTIC to new display
       sta 560
       lda #>DLIST
       sta 561

; Plot a character
       ldx #20         ; vertical position
       ldy #20         ; horizontal position

       lda sclo,x      ; set up zero-page address
       sta $d0
       lda schi,x
       sta $d1
       lda #33         ; plot the character
       sta ($d0),y

; Plot a block
       ldx #8          ; upper corner
       ldy #8
       lda sclo,x      ; set up zero-page addresses
       sta $d0
       lda schi,x
       sta $d1
       inx
       lda sclo,x
       sta $d2
       lda schi,x
       sta $d3

       lda #34         ; plot the character block...
       sta ($d0),y     ; warning - no boundary testing
       sta ($d2),y
       iny
       sta ($d0),y
       sta ($d2),y

; Main loop

j1      
lda $14
wait1
cmp $14
beq wait1

lda 632		; read joystick
left
cmp #11
bne right
jmp moveleft	; or JSR?
right
cmp #7
bne up
jmp moveright
up
cmp #14
bne down
jmp moveup
down
cmp #13
beq movedown
jmp j1
;				
moveleft	
lda 1536
cmp #0
bne moveleft1
jmp j1
moveleft1	
dec 1536
jsr checkchar
lda 1538
cmp #0
beq moveleft2
inc 1536
jmp j1
moveleft2
jsr erasechar
dec 1536
jmp plotchar
;
moveright
lda 1536
cmp #39		;x position too small?
bne moveright1
jmp j1
moveright1
jsr erasechar
inc 1536
jmp plotchar
;	

moveup
lda 1537
cmp #0
bne moveup1
jmp j1
moveup1
jsr erasechar
dec 1537
jmp plotchar
;
movedown
lda 1537
cmp #22
bne movedown1
jmp j1
movedown1
jsr erasechar
inc 1537
jmp plotchar
;
return	jmp j1

checkchar	
; Plot a character
       ldx 1537         ; vertical position
       ldy 1536         ; horizontal position

       lda sclo,x      ; set up zero-page address
       sta $d0
       lda schi,x
       sta $d1
       lda ($d0),y	;read character
       sta 1538	;store it in 1538
rts	

erasechar	
; Plot a character
       ldx 1537         ; vertical position
       ldy 1536         ; horizontal position

       lda sclo,x      ; set up zero-page address
       sta $d0
       lda schi,x
       sta $d1
       lda #0         ; plot the character
       sta ($d0),y
rts	

plotchar	
; Plot a character
       ldx 1537         ; vertical position
       ldy 1536         ; horizontal position

       lda sclo,x      ; set up zero-page address
       sta $d0
       lda schi,x
       sta $d1
       lda #33         ; plot the character
       sta ($d0),y
jmp j1


DLIST
       .byte 112,112,112                        ; blank lines
       .byte 68,<scr,>scr                       ; 24 lines of ANTIC 4
       .byte 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
       .byte 4,4,4,4,4,4,4
       .byte 65,<DLIST,>DLIST                   ; Jump to top

       *=$5000
;scr     .ds 40*24                                ; 960 bytes for screen
scr	.sbyte "****************************************"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "*                       ===============*"
.sbyte "*                       ===============*"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "* ================                     *"
.sbyte "* ================                     *"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "*               ==========             *"
.sbyte "*               ==========             *"
.sbyte "*                                      *"
.sbyte "*                                      *"
.sbyte "* ==========                           *"
.sbyte "* ==========                           *"
.sbyte "*                                      *"
.sbyte "****************************************"	
sclo    .ds 24
schi    .ds 24

Link to comment
Share on other sites

Well, you perform zhe locate check only in one location but not in all four...

 

Tips:

- You should definition xpos = 1536, ypos = 1537, instead of using addresses

- In the j1 routine, you can ldx ypos and ldy xpos without modifying 1536/1538 an keep them there

- Instead of dec/inc 1536/1537 use DEX/INX/DEY/INY to compute the new "target" position without changing the actual value

- Write back x/y to xpos/ypos only at the end

- Every direction of the stick corresponds to a cleared bit, this version uses LSR to check so also diagonals work

- All this is so fast that is easier to save code and always clear/draw the character

 

Example (MADS syntax, ORG instead of start, byte instead of sbyte)

 

org $4000

p1	= $d0
p2	= $d2

xpos	= 1536
ypos	= 1537
joystick = 1538


begin   lda #<scr       ; start by calculating look-up tables
       sta p1         ; at the same time, we will clear the
       lda #>scr       ; screen
       sta p1+1

       ldx #0          
c0      lda #0          ; draw a character
       ldy #39         ; clear a single line
c1      ;sta (p1),y
       dey
       bpl c1

       lda p1+1         ; store address into look-up table
       sta schi,x
       lda p1
       sta sclo,x
       clc             ; update zero page to next line
       adc #40         ; 40 bytes per line
       sta p1
       bcc c2
       inc p1+1
c2      inx             ; for all 24 lines...
       cpx #24
       bne c0

       lda #4          ; set some colors
       sta 708         ; make ANTIC4 slightly more readable
       lda #6
       sta 709
       lda #8
       sta 710

       lda #<DLIST     ; finally, point ANTIC to new display
       sta 560
       lda #>DLIST
       sta 561

; Plot a character
       ldx #20         ; vertical position
stx xpos
       ldy #20         ; horizontal position
sty ypos

       lda sclo,x      ; set up zero-page address
       sta p1
       lda schi,x
       sta p1+1
       lda #33         ; plot the character
       sta (p1),y

; Plot a block
       ldx #8          ; upper corner
       ldy #8
       lda sclo,x      ; set up zero-page addresses
       sta p1
       lda schi,x
       sta p1+1
       inx
       lda sclo,x
       sta p2
       lda schi,x
       sta p2+1

       lda #34         ; plot the character block...
       sta (p1),y     ; warning - no boundary testing
       sta (p2),y
       iny
       sta (p1),y
       sta (p2),y

; Main loop

j1      
       lda $14
wait1
       cmp $14
       beq wait1

ldx ypos
ldy xpos
lda #0
jsr plotchar	;Always erase

       lda 632         ;read joystick
sta joystick	;Shift area for direction bits 

lsr joystick
bcs no_up
cpx #0
beq no_stick
dex
jsr checkchar
beq no_up
inx
no_up
lsr joystick
bcs no_down
cpx #24
beq no_stick
inx
jsr checkchar
beq no_down
dex
no_down
lsr joystick
bcs no_left
cpy #0
beq no_stick
dey
jsr checkchar
beq no_left
iny
no_left
lsr joystick
bcs no_right
cpy #39
beq no_stick
iny
jsr checkchar
beq no_right
dey
no_right
no_stick
lda #33
jsr plotchar
sty xpos
stx ypos
jmp j1

;	Locate char at (X,Y) into <A>
checkchar
       lda sclo,x      ; set up zero-page address
       sta p1
       lda schi,x
       sta p1+1
       lda (p1),y	;read character
       rts     
       
       ; Plot a character <A> at (Y,X)
plotchar        
pha
lda sclo,x      ; set up zero-page address
       sta p1
       lda schi,x
       sta p1+1
       pla
       sta (p1),y
       rts

DLIST
       .byte 112,112,112                        ; blank lines
       .byte 68,<scr,>scr                       ; 24 lines of ANTIC 4
       .byte 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
       .byte 4,4,4,4,4,4,4
       .byte 65,<DLIST,>DLIST                   ; Jump to top

       *=$5000
;scr     .ds 40*24                                ; 960 bytes for screen
scr     .byte "****************************************"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "*                       ===============*"
       .byte "*                       ===============*"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "* ================                     *"
       .byte "* ================                     *"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "*               ==========             *"
       .byte "*               ==========             *"
       .byte "*                                      *"
       .byte "*                                      *"
       .byte "* ==========                           *"
       .byte "* ==========                           *"
       .byte "*                                      *"
       .byte "****************************************"       
sclo    .ds 24
schi    .ds 24

Edited by JAC!
Link to comment
Share on other sites

If I wanted decorative characters that could be passed through and they were restricted to, say, atascii 0-9, what would be the best way to do it?

 

If I subtracted 10 from what's read by the Locate routine and did either a BMI or BPL? Or is there a better way?

Link to comment
Share on other sites

If the char set should be the same for all movement directions (which I assume), it can be done centrally without changing the rest like this:

; Return <A> <> 0 and Z=0 if not transparent, <A>=0, <Z>=1 if transparent.
checkchar
       lda sclo,x      ; set up zero-page address
       sta p1
       lda schi,x
       sta p1+1
       lda (p1),y      ;read character
       beq check_exit  ;Z=1
       cmp #'0
       bcc check_exit  ;less then 0, also Z=0
       cmp #'9 + 1     ;first char after last transparent one
       bcs check_exit  ;greater then 9, also Z=0
       lda #0          ;set Z=1
check_exit
       rts

Link to comment
Share on other sites

Thanks Jac and MaPa. I'm going to be adding my player-missile routine to this shortly and wanted to get the locate function working before I proceeded. Thanks again!

 

And I should say that I think using software collisions instead of the player hardware collision seems to be the way to go.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...