News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_Ynot.zer0

Programming in C on the CPC

Started by Ynot.zer0, 18:20, 06 February 12

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MacDeath

Category:C - CPCWiki


Category:Programming - CPCWiki


as those articles are CPC oriented, it could be indeed nice to have some links.


The Category : C page would be nice for this, just add in it a paragraph and links.

Also a few mentions at the following pages :

SDCC - CPCWiki
Z88DK - CPCWiki

Gryzor

Quote from: arnoldemuQuote from: Mochilote on 30 September 2012, 17:04:56One new tutorial:


-Sprites III: Transparency (C & ASM with SDCC)
Are the links to these tutorials also on the wiki?
It would be good to also have a list that others can look at.
I think a programming section would need to get organised...

Xifos

Hi,

another question, this time it's about sdcc.

Does this compiler support the undocumented Z80 instructions ?

Like ld ixh,a

:)

Marq

Quote from: Xifos on 19:25, 11 December 12
Hi,

another question, this time it's about sdcc.

Does this compiler support the undocumented Z80 instructions ?

Like ld ixh,a

:)

SDCC uses as-z80 internally. At least the inline assembly doesn't support the format you propose. Then again, IX/IY commands are (IIRC) just HL commands with a prefix of $dd of $fd, so you could write them like this:

_asm
.db 0xdd ; same as ld ixh,a
ld h,a
_endasm;

arnoldemu

Quote from: Xifos on 19:25, 11 December 12
Hi,

another question, this time it's about sdcc.

Does this compiler support the undocumented Z80 instructions ?

Like ld ixh,a

:)
no. Well I could not get them to work.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

db6128

I'm sure you knew to check this, but just in case, did you try calling them LX, HY, etc.?
Quote from: Devilmarkus on 13:04, 27 February 12
Quote from: ukmarkh on 11:38, 27 February 12[The owner of one of the few existing cartridges of Chase HQ 2] mentioned to me that unless someone could find a way to guarantee the code wouldn't be duplicated to anyone else, he wouldn't be interested.
Did he also say things like "My treasureeeeee" and is he a little grey guy?

Xifos

Well, that was just a question, it is not really a problem...
Thanks for the replies.

And i wonder : what is faster ?

ld ixh,a

ld (memory),a

The question is only for inline asm (for sprite routines, stuff that needs speed), i enjoy coding in c and sdcc seems to be a good compiler for cpc/cpc+.
:)

db6128

Quote from: Xifos on 18:36, 13 December 12And i wonder : what is faster ?

ld ixh,a

ld (memory),a
As per the Z80 manual and Kev's list of timings, LD (nnnn),A takes 4 NOPs whereas LD IXH,A is twice as fast at 2 NOPs.

Even though the 8-bit halves of the index registers are always 1 NOP slower than normal 8-bit registers, they can still be very useful when all the others are occupied. like on the outside of loops, etc.
Quote from: Devilmarkus on 13:04, 27 February 12
Quote from: ukmarkh on 11:38, 27 February 12[The owner of one of the few existing cartridges of Chase HQ 2] mentioned to me that unless someone could find a way to guarantee the code wouldn't be duplicated to anyone else, he wouldn't be interested.
Did he also say things like "My treasureeeeee" and is he a little grey guy?

Xifos

Thanks a lot guys !
:)

I tried the .db 0xDD / .db 0xFD method !
It works !
(i had to use 0xFD for iy rather than ix because i was in a function and i think ix must not be modified in that case)

Mochilote


db6128

Quote from: Xifos on 21:42, 13 December 12(i had to use 0xFD for iy rather than ix because i was in a function and i think ix must not be modified in that case)
Two things from SDCC's official user manual:
QuoteThe stack frame is similar to that generated by the IAR Z80 compiler. IX is used as the base pointer, HL and IY are used as a temporary registers, and BC and DE are available for holding variables. Return values for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four bytes).
So, yes, this suggests that you should not be using IX. As for IY, maybe SDCC will use it between your blocks of ASM, which would mean you couldn't retrieve values if they're separated by C code. I don't know enough to guess whether changing IY might confuse SDCC if it is already using it as a temporary value between your sections of ASM.
However:
Quote--reserve-regs-iy This option tells the compiler that it is not allowed to use register pair iy. The option can be useful for systems where iy is reserved for the OS.
Quote from: Devilmarkus on 13:04, 27 February 12
Quote from: ukmarkh on 11:38, 27 February 12[The owner of one of the few existing cartridges of Chase HQ 2] mentioned to me that unless someone could find a way to guarantee the code wouldn't be duplicated to anyone else, he wouldn't be interested.
Did he also say things like "My treasureeeeee" and is he a little grey guy?

Xifos

>db6128 and the others

Thanks for your detailed answers.
I used a push ix at the beginning of my asm routine and a pop ix at the end !
Better speed using ixh/ixl/iyh/iyl for storing values.

>Mochilote

Interesting tutorials !

cpcitor

#37
Quote from: Mochilote on 08:53, 05 March 12
Hi, the tutorial index is here: AMSTRAD CPC PROGRAMMING TUTORIALS and i am 'the someone'  :laugh:

Hello,

These seem really interesting.

Once a forum discussion brings interesting facts or conclusions, everyone wins if those are included in the wiki.
As a first step, I mentioned your tutorial in Programming:Cross Development - CPCWiki


Feel free to review/criticize/correct.
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made in 2013 the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC, later forked into CPCTelera.

Gryzor

Thanks for the edit :)

(I edited your post slightly since link generation is automatic)

Optimus

I have used IX/IY and their 8bit counterparts with SDCC. You only need to be careful to PUSH/POP IX/IY in the beginning/end of your asm code when it's inside a C function that you call.


Though, my SDCC framework uses SDCC2Pasmo from NoRecess, to use the Pasmo assembler with Maxam syntax I prefer, so iirc those where supported as hx/lx or hix/lix (don't remember).
But yes, the alternative is with db, that's how I did it in other assemblers years ago.


cpcitor

Quote from: Mochilote on 11:42, 05 February 13
-Keyboard: Reading HW, multiple simultaneous keypresses, redefine (C & ASM with SDCC)

It would be interesting to make explicit the pros and cons of using this rewritten keyboard scan routine, by comparing with the firmware routine.
For example comparing size, speed, resources used (registers, memory area, etc).

Also, if one chooses to use all CPC memory and wipe all firmware areas (save some for the stack), disabling the firmware and perhaps using one's personal interrupt handler, is it still possible to manually call the firmware keyboard scan routine (say, to save some bytes in my ROM-based game for Amstrad CPC 16KBs ROM Game Development Competition! ) ?

Quote from: Mochilote on 11:42, 05 February 13
I hope you like it,

I do !
As a tutorial on how to read hardware from C, it's pretty nice.
If I use it I'll credit you for sure !
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made in 2013 the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC, later forked into CPCTelera.

AMSDOS

Quote from: cpcitor on 13:11, 05 February 13
It would be interesting to make explicit the pros and cons of using this rewritten keyboard scan routine, by comparing with the firmware routine.
For example comparing size, speed, resources used (registers, memory area, etc).


For the programs I do, KM TEST KEY (&BB1E) does the job adequately, though the more complicated the program and more players to go with it, resorting to the hardware direct maybe the only option, though I cannot get over the size of that routine, I'm not even going to try and understand all that C code that goes with it.  ???
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

cpcitor

Quote from: AMSDOS on 08:47, 06 February 13
For the programs I do, KM TEST KEY (&BB1E) does the job adequately, though the more complicated the program and more players to go with it, resorting to the hardware direct maybe the only option, though I cannot get over the size of that routine, I'm not even going to try and understand all that C code that goes with it.  ???

KM TEST KEY looks equivalent to BASIC's INKEY(number), which seems fine: both allow you to get the pressed status of each key as accurately as the hardware can tell.

I can understand constraints like "I killed the firmware-reserved part of the RAM, use my own interrupt scheme" to force someone into scanning the keyboard oneself, but you do not mention those reasons.

How can the "complication of the program" and the "number of players" call for direct hardware scanning ? What's specific in this case ?
Had a CPC since 1985, currently software dev professional, including embedded systems.

I made in 2013 the first CPC cross-dev environment that auto-installs C compiler and tools: cpc-dev-tool-chain: a portable toolchain for C/ASM development targetting CPC, later forked into CPCTelera.

AMSDOS

#44
Quote from: cpcitor on 09:10, 06 February 13
KM TEST KEY looks equivalent to BASIC's INKEY(number), which seems fine: both allow you to get the pressed status of each key as accurately as the hardware can tell.


I think so, though with KM TEST KEY - 'A' contains the key/joystick number and if found, the Zero Flag is false. If it's not found then the Zero Flag is true, Carry is False, 'A' & 'HL' are corrupt. The 'C' Register holds the Shift and Control Status and other registers are preserved.


Quote
I can understand constraints like "I killed the firmware-reserved part of the RAM, use my own interrupt scheme" to force someone into scanning the keyboard oneself, but you do not mention those reasons.

How can the "complication of the program" and the "number of players" call for direct hardware scanning ? What's specific in this case ?


i'm not the best person to explain why use Hardware instead of Firmware, though using Hardware could simply mean freeing up the area of memory occupied by the Firmware. From a number of players perspective, a game which has a number of players playing at the same time maybe more critical using Hardware over Firmware to deal with the number of signals coming in at the same time perhaps.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Mochilote

After reading your comments i have modified the tutorial to include keyboard firmware method from SDCC and the comparison with direct reading of HW:

-Keyboard: Reading HW, multiple simultaneous keypresses, redefine, Firmware vs. HW (C & ASM with SDCC)


Now that is more complete I hope that everyones like it  ;)

Mochilote.

AMSDOS

Quote from: Mochilote on 16:58, 07 February 13
After reading your comments i have modified the tutorial to include keyboard firmware method from SDCC and the comparison with direct reading of HW:

-Keyboard: Reading HW, multiple simultaneous keypresses, redefine, Firmware vs. HW (C & ASM with SDCC)


Now that is more complete I hope that everyones like it  ;)

Mochilote.


I think the other problem with KM TEST KEY is Jumping to the appropriate area depending on if a result was or wasn't found. I wonder how that test would go if 'JP Z, _end_IsKeyPressed' was 'JR Z, _end_IsKeyPressed', in that situation if the condition is not met 7 clock cycles is only used, though 12 clock cycles if a key is found.



* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

MaV

Quote from: AMSDOS on 10:28, 08 February 13
..., in that situation if the condition is not met 7 clock cycles is only used, though 12 clock cycles if a key is found.
That's basically right, but be careful! You're basing your instruction timings on the original information for the Z80. The CPC lengthens the cycles to a multiple of four, so 7 cycles become 8 (= 2 NOPs or 2us).

documentations:devices:z80 [Grimware]
Das Schneider CPC Systembuch: Anhang: Die Z80: Ausführungszeiten für die

cpcitor has created a PDF recently:
Craving for speed ? A visual cheat sheet to help optimizing your code to death.
Black Mesa Transit Announcement System:
"Work safe, work smart. Your future depends on it."

AMSDOS

Quote from: MaV on 11:24, 08 February 13
That's basically right, but be careful! You're basing your instruction timings on the original information for the Z80. The CPC lengthens the cycles to a multiple of four, so 7 cycles become 8 (= 2 NOPs or 2us).

documentations:devices:z80 [Grimware]
Das Schneider CPC Systembuch: Anhang: Die Z80: Ausführungszeiten für die

cpcitor has created a PDF recently:
Craving for speed ? A visual cheat sheet to help optimizing your code to death.


Oh okay that's interesting. That would mean a "JP Z,somewhere" would always be 3us regardless of what the condition is. "JR Z,somewhere" on the other hand would be 1us faster if the condition is not met and at worst no slower than a "JP Z,somewhere" if the condition is met.  :D
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

db6128

Quote from: AMSDOS on 12:40, 08 February 13That would mean a "JP Z,somewhere" would always be 3us regardless of what the condition is. "JR Z,somewhere" on the other hand would be 1us faster if the condition is not met and at worst no slower than a "JP Z,somewhere" if the condition is met.  :D
Yes, and this is actually quite 'useful' from a programming perspective, although of course, a lack of slowdown would be far better! Another similar fact is this makes DJNZ the same speed as DEC r:JR NZ, which (again, although slower) simplifies optimisation when the B register is already in use, e.g. for OUTs. Speaking of the CPC and B, don't get me started on the CPC's way of doing I/O ;) or is it really the Z80's fault here? :P
Quote from: Devilmarkus on 13:04, 27 February 12
Quote from: ukmarkh on 11:38, 27 February 12[The owner of one of the few existing cartridges of Chase HQ 2] mentioned to me that unless someone could find a way to guarantee the code wouldn't be duplicated to anyone else, he wouldn't be interested.
Did he also say things like "My treasureeeeee" and is he a little grey guy?

Powered by SMFPacks Menu Editor Mod