CC65 interop with assembly

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
vnsbr
Posts: 22
Joined: Sun Feb 17, 2019 5:18 pm

CC65 interop with assembly

Post by vnsbr » Thu Sep 10, 2020 5:47 am

Hello im trying to learn about inter op between cc65 and ca65, also would try to use this function https://codebase64.org/doku.php?id=base ... -bit_angle. I guess that is a nice use case to learn how they communicate? things i read about "sending variables" back and forth, to be confirmed:

1) if it is only one parameter it gets stored in accumulator (A) if byte and A/X if word
2) no need to push the stack the value back if only returns one parameter ( not sure?)
3) if more than one parameter its is necessary to pop the stack for x-1 where x ins the number of parameters. and later reconstruct the stack(not sure how to do this?)

here is my first try:

Code: Select all

.export _atan2
_atan2:
	ldy #3
	lda (sp),y
	sta <SCRX ;x1

	dey
	lda (sp),y
	sta <SRC ;x2
	
	dey
	lda (sp),y ;y1
	sta <SCRY

	dey
	lda (sp),y ;y2
	sta <DST

	lda <SCRX
	sbc <SRC
	bcs *+4
	eor #$ff
	tax
	rol octant
	lda <SCRY
	sbc <DST
	bcs *+4
	eor #$ff
	tay
	rol octant
	lda log2_tab,x
	sbc log2_tab,y
	bcc *+4
	eor #$ff
	tax
	lda octant
	rol
	and #%111
	tay
	lda atan_tab,x
	eor octant_adjust,y
	jsr incsp2 ;my tentative to restore sp, only 2 because it is 4 bytes? one for each byte i guess :P
	rts
I ommited the tables at the end since they dont change I guess.

and this is how i try to use on the C side:

Code: Select all

byte __fastcall__ atan2(byte x1, byte x2, byte y1, byte y2);
(...)
byte angle;
angle = atan2(10,30,20,40); //just some mock to test everything doesn't explode
i can see something is terribly wrong since the PPU only renders garbage doing this.

sorry for the n00B questions but if some one can direct to some kind of guide on this i appreciate :D


User avatar
Memblers
Site Admin
Posts: 3876
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Re: CC65 interop with assembly

Post by Memblers » Fri Sep 11, 2020 11:03 am

This link also has info on the built-in functions, popa, popax, etc. (I know it's linked the in other article, but in case anyone missed that)
https://github.com/cc65/wiki/wiki/Param ... urn-stacks

I don't have a ton of experience doing this, but I did successfully use parameters in this project: https://github.com/Memblers/mp3player
The original asm routine took parameters as A,X,Y. The code at the top adapts the C parameters to it.

Code: Select all

_mp3_command:		; C function entry
	sta temp_y	; parameter 2
        jsr popa
        tax
        jsr popa
        ldy temp_y
        
;mp3_command:		; assembly entry
	pha
	lda #$7E
	jsr mp3_send
	lda #$FF
	jsr mp3_send
	lda #$06
	jsr mp3_send
	pla
        sta last_command
	jsr mp3_send
	lda #0
	jsr mp3_send
	txa
	jsr mp3_send
	tya
	jsr mp3_send
	lda #$EF
	jsr mp3_send    
        rts     

Code: Select all

void __fastcall__ mp3_command(CMD command, unsigned char param1, unsigned char param2);

User avatar
rainwarrior
Posts: 7836
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: CC65 interop with assembly

Post by rainwarrior » Sat Sep 12, 2020 4:32 pm

Probably the information is redundant to the stuff on that wiki page, but here's where the relevant stuff is documented:
https://cc65.github.io/doc/cc65-intern.html

Post Reply