CPCWiki forum

General Category => Programming => Topic started by: Typhon on 23:32, 05 May 25

Title: CPCTelera KeyID to ASCII and reverse
Post by: Typhon on 23:32, 05 May 25
Hello!

Just a quick CPCTelera question.

What's an easy way to convert an ASCII value (char) to a cpct_keyID and vice-versa.

cpct_getKeypressedAsASCII from 1.5 dev branch only works on keys actively pressed. I want to be able to convert back and forth on demand (for redefine, high score entry etc) without having to resort to a massive switch or if/then else.

Thanking you in Advance.
T.

(new game coming soon from Typhonsoft!)
Title: Re: CPCTelera KeyID to ASCII and reverse
Post by: lightforce6128 on 11:25, 06 May 25
I do not have experience with CPCTelera. But do not use a bunch of if/then or switch/case! Converting from one (smaller) number to another (smaller) number can usually be acomplished best by using an array / a table for translation. If conversion in both directions is needed, you can either use two tables, or stay with one table, but you have to implement a search-loop for one of the directions.

// Define two tables:
char from_key_code_to_ascii [128] = { ... };
unsigned char from_ascii_to_key_code [128] = { ... }

// Convert from key code to ASCII:
int key_code = 69;
assert  from_key_code_to_ascii [key_code] == 'A';

// Convert from ASCII to key code:
char ascii = 'A';
assert  from_ascii_to_key_code [int(ascii)] == 69;

With this example one problem gets apparent: Should key 69 map to uppercase "A" or to lowercase "a"? Often more than one key is used to create a character.


Sometimes it is possible to find a mathematical formula for conversion, and maybe this one is even invertible for the other direction. But as far as I know there is no relation between the key codes and ASCII numbers in the CPC.
Title: Re: CPCTelera KeyID to ASCII and reverse
Post by: lightforce6128 on 12:06, 06 May 25
No reason to reinvent the wheel: The operating system already contains the table (at least for one direction) and routines to access it:

For the CPC 464 the tables are stored in the lower ROM at
For other CPCs the location of the tables might be shifted a bit.

If your program uses the memory differently then intended by the operating system, the tables and routines should still work, as they do not need access to initialized RAM areas. But paging of ROMs needs to be done in the right way.
Title: Re: CPCTelera KeyID to ASCII and reverse
Post by: Typhon on 12:15, 06 May 25
I don't have access to firmware though.. 

Having looked at the CPCTelera keypressedasascii asm, I think I'll go with your suggestion if using a lookup table. I see there's already one available for one direction at least: the cpckeyidtoascii[80] table.

Thank you! 
Title: Re: CPCTelera KeyID to ASCII and reverse
Post by: andycadley on 12:17, 06 May 25
If you need a full table and need to kick the OS out of the way, you can build up your own by just looping over calls to KM_GET_TRANSLATE before ditching the OS. This can avoid having to prod around inside the ROM trying to guess the version (although that is an option if space is tight)

The advantage of using the OS rather than a pre built table is that it will correctly identify things like an Azerty keyboard layout for you.
Title: Re: CPCTelera KeyID to ASCII and reverse
Post by: awergh on 12:26, 06 May 25
Usually with cpctelera you turn off the firmware as some of the functions expect for it to be disabled.

I have been using this getKey function from ronaldo for my games when I want to get a key from the player
https://www.cpcwiki.eu/forum/programming/cpctelera-question-entering-your-name-into-a-high-score-table/

It looks like it is doesn't do the ascii conversion which the function you mention does.
Powered by SMFPacks Menu Editor Mod