Recommendation for declaring constants

Other misc things
Post Reply
vmorilla
Member
Posts: 22
Joined: Sun Feb 11, 2024 7:15 pm

Recommendation for declaring constants

Post by vmorilla »

Are there good practices for declaring constants in z88dk? I have tried the following three options:

a) Using #define:

Code: Select all

#define SCRM_TEXT1 0x1000
set_screen_mode(SCRM_TEXT1);
Obviously works, but in the assembler code produced there is no trace of 'SCRM_TEXT', so it becomes hard to debug:

Code: Select all

	ld	hl,0x1000
	call	_screen_mode
b) using const:

Code: Select all

const uint16_t SCRM_TEXT1 = 0x1000;
set_screen_mode(SCRM_TEXT1);
Which produces less efficient code:

Code: Select all

	ld	hl, (_SCRM_TEXT1)
	call	_screen_mode
c) using enums:

Code: Select all

typedef enum ScreenModeEnum {
    SCRM_TEXT1 = 0x1000
} ScreenMode;
which produces similar results to the first option: no trace of SCRM_TEXT in the generated code:

Code: Select all

	ld	hl,0x1000
	call	_screen_mode
What I would to achieve is the following kind of result:

Code: Select all

SCRM_TEXT1   equ  0x1000
		      
		ld hl, SCRM_TEXT1
		call _screen_mode
Any suggestion?

Thanks!
User avatar
dom
Well known member
Posts: 2317
Joined: Sun Jul 15, 2007 10:01 pm

Re: Recommendation for declaring constants

Post by dom »

My preferred way is #define or enums, but you can achieve what you want this way:

Code: Select all

extern __at(0x1000) int SCRM_TEXT1;

extern void screenmode(int);

void func()
{
   screenmode((int)&SCRM_TEXT1);
}
And several permutations of similar ideas.
vmorilla
Member
Posts: 22
Joined: Sun Feb 11, 2024 7:15 pm

Re: Recommendation for declaring constants

Post by vmorilla »

Thank you. Is there any possibility of defining such constants in assembler and using them in C. If this is the case, would the code generated improve in any way?
User avatar
dom
Well known member
Posts: 2317
Joined: Sun Jul 15, 2007 10:01 pm

Re: Recommendation for declaring constants

Post by dom »

It's the same pattern - we use this one in the library to pass link time values from the crt to the library. The generated code is the same as my previous example.

Code: Select all

; Assembler file

PUBLIC _SCRM_TEXT1
defc _SCRM_TEXT1 = 0x1000

Code: Select all

// C file

extern void screenmode(int);

extern void *SCRM_TEXT1;
#define SCRM_TEXT1 (int)&SCRM_TEXT1

void func()
{
   screenmode(SCRM_TEXT1);
}
vmorilla
Member
Posts: 22
Joined: Sun Feb 11, 2024 7:15 pm

Re: Recommendation for declaring constants

Post by vmorilla »

Thank you!
User avatar
jorgegv
Well known member
Posts: 312
Joined: Wed Nov 18, 2020 5:08 pm

Re: Recommendation for declaring constants

Post by jorgegv »

dom wrote: Sat Feb 17, 2024 7:45 pm It's the same pattern - we use this one in the library to pass link time values from the crt to the library. The generated code is the same as my previous example.

Code: Select all

; Assembler file

PUBLIC _SCRM_TEXT1
defc _SCRM_TEXT1 = 0x1000

Code: Select all

// C file

extern void screenmode(int);

extern void *SCRM_TEXT1;
#define SCRM_TEXT1 (int)&SCRM_TEXT1

void func()
{
   screenmode(SCRM_TEXT1);
}
Nice trick Dom. In this case, int and void* are both 16-bits, but what about constants that are just 8 bits? Are DEFC always defined as 16-bit numbers, and I should just change the (int) in the #define for a (uint8_t), for example?
User avatar
jorgegv
Well known member
Posts: 312
Joined: Wed Nov 18, 2020 5:08 pm

Re: Recommendation for declaring constants

Post by jorgegv »

I mean this:

Code: Select all

public _VAL8
defc _VAL8 = 0x25
And then:

Code: Select all

extern void *VAL8;
#define VAL8 (uint8_t)&VAL8
Post Reply