Page 1 of 1

extern ZP variables in cc65 and warnings.

Posted: Mon Jan 15, 2018 1:07 am
by na_th_an
I've been having this problem for months. It just seems to be an ugly warning as everything seems to work, but I don't like warnings.

It seems that tschak909 had the same problem here: viewtopic.php?p=211844#p211844 and got it sorted out, but the same solution doesn't work for me.

This is my issue:

I created a couple of pointers in ZP in the crt0 file. They are read by a modification I made to neslib, and have to be written from by C code in my main module. Such pointers are defined as:

Code: Select all

.export _SCROLL_WRITE, _ATTRIB_WRITE

[...]

.segment "ZEROPAGE"

; [...] <- neslib stuff being defined here as in lots of "var .res N"

_SCROLL_WRITE:    .res 2
_ATTRIB_WRITE:    .res 2
Those symbols I import from my main module:

Code: Select all

extern unsigned char *SCROLL_WRITE;
#pragma zpsym ("SCROLL_WRITE")
extern unsigned char *ATTRIB_WRITE;
#pragma zpsym ("ATTRIB_WRITE")
But keep getting the warnings:

Code: Select all

ld65: Warning: Address size mismatch for `_ATTRIB_WRITE': Exported from crt0.o, crt0-pantanow.s(81) as `zeropage', import in game.o, game.s(20597) as `absolute'
ld65: Warning: Address size mismatch for `_SCROLL_WRITE': Exported from crt0.o, crt0-pantanow.s(80) as `zeropage', import in game.o, game.s(20597) as `absolute'
I even updated to the latest win32 snapshot of cc65, to no avail. Any pointers on what I'm doing wrong / what's missing?

Thanks.

Re: extern ZP variables in cc65 and warnings.

Posted: Mon Jan 15, 2018 3:17 am
by DRW
The error message doesn't seem to imply this, but shouldn't it be .exportzp in the Assembly file instead of .export?

Re: extern ZP variables in cc65 and warnings.

Posted: Mon Jan 15, 2018 3:44 am
by calima
Either the warning is wrong, or your crt0.s has another .section directive in between, causing them to be normal symbols. I use ZP symbols all the time, and don't have any warnings.

edit: Look at the generated game.s asm. It should have those as importzp, not import.

Re: extern ZP variables in cc65 and warnings.

Posted: Mon Jan 15, 2018 2:57 pm
by na_th_an
Thanks both. I'll double-check.

Re: extern ZP variables in cc65 and warnings.

Posted: Fri Apr 27, 2018 11:23 am
by na_th_an
Sorry for necromancing my own thread, but I forgot, time passed, and I didn't actually check what I was supposed to check. Anyways.

1.- There's absolutely not a different .section directive going in between:

Code: Select all

.segment "ZEROPAGE"

NTSC_MODE: 			.res 1
FRAME_CNT1: 		.res 1
FRAME_CNT2: 		.res 1
VRAM_UPDATE: 		.res 1
NAME_UPD_ADR: 		.res 2
PAL_UPDATE: 		.res 1
PAL_BG_PTR: 		.res 2
PAL_SPR_PTR: 		.res 2
SCROLL_X: 			.res 1
SCROLL_Y: 			.res 1
SCROLL_X1: 			.res 1
SCROLL_Y1: 			.res 1
PAD_STATE: 			.res 2		;one byte per controller
PAD_STATEP: 		.res 2
PAD_STATET: 		.res 2
PPU_CTRL_VAR: 		.res 1
PPU_CTRL_VAR1: 		.res 1
PPU_MASK_VAR: 		.res 1
RAND_SEED: 			.res 2
FT_TEMP: 			.res 3
_SCROLL_WRITE:		.res 2 		; <----
_ATTRIB_WRITE: 		.res 2 		; <----
[... etc]
2.- The generated .s file doesn't have an import for _SCROLL_WRITE or _ATTRIB_WRITE. In fact, I am not using the pointers in C code, but in inline assembly within a C function:

Code: Select all

void scroll_writers_realloc (void) {
	if (wtp) wtp --; else wtp = 59;
	if (wtp < 30) {
		wtpr = wtp;
		__asm__ ("ldx _wtpr");
		__asm__ ("lda _shl5_hi, x");
		__asm__ ("clc");
		__asm__ ("adc #$20");
		__asm__ ("sta <_SCROLL_WRITE");
		__asm__ ("lda _shl5_lo, x");
		__asm__ ("sta <_SCROLL_WRITE+1");
		__asm__ ("lda _shr2, x");
		__asm__ ("tay");
		__asm__ ("lda #$23");
		__asm__ ("sta <_ATTRIB_WRITE");
		__asm__ ("lda _attrs_adds, y"); 
		__asm__ ("sta <_ATTRIB_WRITE+1");
	} else {
		wtpr = wtp - 30;
		__asm__ ("ldx _wtpr");
		__asm__ ("lda _shl5_hi, x");
		__asm__ ("clc");
		__asm__ ("adc #$28");
		__asm__ ("sta <_SCROLL_WRITE");
		__asm__ ("lda _shl5_lo, x");
		__asm__ ("sta <_SCROLL_WRITE+1");
		__asm__ ("lda _shr2, x");
		__asm__ ("tay");
		__asm__ ("lda #$2B");
		__asm__ ("sta <_ATTRIB_WRITE");
		__asm__ ("lda _attrs_adds, y"); 
		__asm__ ("sta <_ATTRIB_WRITE+1");
	}
}
I guess that's the real problem (by the way, game.c compiles without the extern definitions I mentioned in the OP). Any ideas to get rid of the warning?

Code: Select all

ld65: Warning: Address size mismatch for `_ATTRIB_WRITE': Exported from crt0.o, crt0-pantanow.s(81) as `zeropage', import in game.o, game.s(20740) as `absolute' ld65: Warning: Address size mismatch for `_SCROLL_WRITE': Exported from crt0.o, crt0-pantanow.s(80) as `zeropage', import in game.o, game.s(20740) as `absolute'
The most interesting part is that 20740 lines is exactly the size of the generated game.s file.

Re: extern ZP variables in cc65 and warnings.

Posted: Fri Apr 27, 2018 12:00 pm
by calima
Well that does make it clear, you're specifying them as inline assembly *text*. Neither the compiler nor assembler know you are accessing them.

Switch to using the inline assembly form where you pass the variable from outside, not via the text.

Re: extern ZP variables in cc65 and warnings.

Posted: Fri Apr 27, 2018 12:18 pm
by gauauu
calima wrote:Well that does make it clear, you're specifying them as inline assembly *text*. Neither the compiler nor assembler know you are accessing them.

Switch to using the inline assembly form where you pass the variable from outside, not via the text.
To clarify what that means:

Code: Select all

__asm__ ("sta <%v", ATTRIB_WRITE);
The %v and other format specifiers are defined here https://www.cc65.org/doc/cc65-9.html.

Re: extern ZP variables in cc65 and warnings.

Posted: Fri Apr 27, 2018 2:27 pm
by na_th_an
Thanks. I usually use the "%v", etc format - but I guess that snippet was coded some time ago when I haven't read the documentation properly.

Now the warning is gone. Thank you for the enlightement. Now the code is written properly

Code: Select all

void scroll_writers_realloc (void) {
	if (wtp) wtp --; else wtp = 59;
	if (wtp < 30) {
		wtpr = wtp;
		__asm__ ("ldx %v", wtpr);
		__asm__ ("lda %v, x", shl5_hi);
		__asm__ ("clc");
		__asm__ ("adc #$20");
		__asm__ ("sta <%v", SCROLL_WRITE);
		__asm__ ("lda %v, x", shl5_lo);
		__asm__ ("sta <%v+1", SCROLL_WRITE);
		__asm__ ("lda %v, x", shr2);
		__asm__ ("tay");
		__asm__ ("lda #$23");
		__asm__ ("sta <%v", ATTRIB_WRITE);
		__asm__ ("lda %v, y", attrs_adds); 
		__asm__ ("sta <%v+1", ATTRIB_WRITE);
	} else {
		wtpr = wtp - 30;
		__asm__ ("ldx %v", wtpr);
		__asm__ ("lda %v, x", shl5_hi);
		__asm__ ("clc");
		__asm__ ("adc #$28");
		__asm__ ("sta <%v", SCROLL_WRITE);
		__asm__ ("lda %v, x", shl5_lo);
		__asm__ ("sta <%v+1", SCROLL_WRITE);
		__asm__ ("lda %v, x", shr2);
		__asm__ ("tay");
		__asm__ ("lda #$2B");
		__asm__ ("sta <%v", ATTRIB_WRITE);
		__asm__ ("lda %v, y", attrs_adds); 
		__asm__ ("sta <%v+1", ATTRIB_WRITE);
	}
}