[MSX-C] Q&A official thread

Страница 2/55
1 | | 3 | 4 | 5 | 6 | 7

By AxelStone

Prophet (2674)

Аватар пользователя AxelStone

26-08-2015, 08:28

ARTRAG wrote:

yes, I use the msdos version of Hi Tech C v7.8pl2. It comes with its own libraries but none msx specific

Sadly I think that MSX specific stuff is very interesting for a C compiler Sad .

JaviLM wrote:

It's not a compiler bug. The MSX-C V1.2 User's Manual explains this issue in the description of the printf() function (page 208):

Quote:

Warning
When printing char values with %d, %x or %o always cast the variable to an int. This is because the value is passed to printf() as an int. Without the cast, the high order byte is undefined and it may be displayed incorrectly. The same happens with other types that are a single byte: BOOL, STATUS and TINY. %c doesn't require casting to int.

Thanks JaviLM, now its clear Wink . I supossed efectively it was a printf issue since comparations works fine.

By anonymous

incognito ergo sum (109)

Аватар пользователя anonymous

26-08-2015, 09:37

And by the way, I've recovered my old account. :-)

I haven't decided yet whether I'll keep posting as JaviLM or as ag0ny, because this nickname has some history, but I haven't used it for some years. Expect to see messages from both accounts for a few days until I decide which one I'll keep.

By AxelStone

Prophet (2674)

Аватар пользователя AxelStone

26-08-2015, 10:16

I remember Agony as a very impressive Amiga game Smile

By AxelStone

Prophet (2674)

Аватар пользователя AxelStone

27-08-2015, 18:54

Here goes again.

Question: I'm not sure how constant pointers works. I'd like to define a pointer in FBE5H. It's NEWKEY data area, where it's stored if a file of keyboard matrix is read. For example if I want to read key M, I must read row 4 column 2 (this is, second bit in row 4). In order to make that I do the following:

readkey(row,col)
TINY row,col; 
{
    TINY key;
    TINY *adr=0xFBE5;
    key=adr[row] & ((TINY)1<<col);
}

This code works, when M is pressed key value is 0. I'd like to define address as constant so:

#define NEWKEY (*(TINY *)0xFBE5
readkey(row,col)
TINY row,col; 
{
    TINY key;
    key=NEWKEY+row & ((TINY)1<<col);
}

This code doesn't works, it doesn't detect key M pressed, key allways values 0. As you can see I can't use NEWKEY[row], it produces compilation error, so I use alternative way to access *arrays (x+y instead x[y]). I'm clearly reading wrong address. Any idea?

Thanks!

By Grauw

Ascended (8379)

Аватар пользователя Grauw

27-08-2015, 20:31

Big disclaimer before I speak: my experience with C is very limited.

It’s worth mentioning #define is a preprocessor instruction, it performs a literal search/replacement on the file before the C compiler gets to process it.

I don’t know if the K&R C supports the const keyword…? If so you could maybe define it as a const pointer variable (const TINY * NEWKEY = 0xFBE5).

Anyway, you seem to be missing a ) at the end of the NEWKEY define? Should cause a compile error…

Assuming that was a typo and your actual program did have the ); the + row does not work, because (*(TINY *)0xFBE5) gives the value of FBE5H (the first * does this), so you’re adding the row number to the value rather than to the address. What would work is #define NEWKEY (TINY *)0xFBE5 and then doing key=*(NEWKEY + row) & ((TINY)1<<col). Note the value is now read from the address after adding the row number.

By AxelStone

Prophet (2674)

Аватар пользователя AxelStone

27-08-2015, 21:07

Sorry it was a typo, it's efectively #define NEWKEY (*(TINY *)0xFBE5). You are right, defines are replaced for its value in the compiled code, so perhaps it's something wrong.

I already used option *(NEWKEY+row) but I obtain a compilation error (bad indirection). The other notation (const TINY * NEWKEY = 0xFBE5) doesn't works on MSX-C, it doesn't compile. I think that const it's not supported in MSX-C, if you try a simple "const int life=3;" it gives the same error compilation.

I tryed to follow a code example that makes the following:

#define T32NAM *((int *)0xf3bd)
...
vpoke(T32NAM + (Hy+1) * 32 + (Hx+1), ' ');

As you can see, it uses T32NAM+value to get the address Question

By Sylvester

Champion (398)

Аватар пользователя Sylvester

27-08-2015, 21:24

What is the difference between the NEWKEY and SNSMAT ($0141) ? because snsmat() exists in te msxclib and can be used to detect if a specific key from row X of the keyboard matrix is pressed.

By AxelStone

Prophet (2674)

Аватар пользователя AxelStone

27-08-2015, 22:12

Sylvester wrote:

What is the difference between the NEWKEY and SNSMAT ($0141) ? because snsmat() exists in te msxclib and can be used to detect if a specific key from row X of the keyboard matrix is pressed.

None, you can use both of them to detect key pressed. Perhaps the advantage of NEWKEY is that you can also use OLDKEY in order to disable functions as autofire.

However I've found the solution Big smile

#define NEWKEY (TINY *)0xFBE5

readkb(row,col) 
TINY row,col;
{
    TINY key;
    key=*(NEWKEY+row) & (1<<col);
}

At least it works, thanks for your help Wink . The same code works using snsmat(row).

By ARTRAG

Enlighted (6234)

Аватар пользователя ARTRAG

27-08-2015, 23:11

Use return key;
to end the function and define the function as
TINY readkb (row,col) etc

By AxelStone

Prophet (2674)

Аватар пользователя AxelStone

27-08-2015, 23:43

Yeah, let's paste the final version:

#define NEWKEY (TINY *)0xFBE5

TINY readkb(row,col) 
TINY row,col;
{
    TINY key;
    key=*(NEWKEY+row) & (1<<col);
    return key;
}

With this function on your main loop, you read keyboard Wink .

Страница 2/55
1 | | 3 | 4 | 5 | 6 | 7