[z88dk-dev] cpc target firmware ownership of exx set

Bridge to the z88dk-developers mailing list
Post Reply
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

[z88dk-dev] cpc target firmware ownership of exx set

Post by alvin »

A problem in the forum with compiling for the cpc target was traced to the exx set not being saved for the firmware. Compiling with subtype=noint (which disables interrupts and avoids the cpc isr missing its exx set at least) partially solved the problem so I looked at a complete solution tonight.

There are two files affected:

the crt: http://z88dk.cvs.sourceforge.net/viewvc ... xt%2Fplain
the firmware interposer: http://z88dk.cvs.sourceforge.net/viewvc ... xt%2Fplain

The idea is simple. Some memory is set aside to hold the firmware's exx set. At startup the current exx set is saved to the firmware's saved copy. An interposer interrupt routine checks to see if firmware's exx set is active and if not it saves the process exx set, loads the saved firmware exx set, calls the cpc int routine, saves the firmware exx set, restores the process exx set and returns. The firmware interposer inserts code that loads and saves the firmware exx set around the firmware call. There's no need to save the process exx set since the fact the firmware is being called means the library is not using the exx set.

What I don't like is that the cpc interrupt can occur 300 times per second and that slows down things enough without this extra interposer code. But if people want to go fast they can stop using the cpc interrupt.

The firmware interposer should not be enabling interrupts out of the program's control. The new library has a function call to push the current ei state onto the stack and then pop it off the stack later; this allows the subroutine to restore ei state to whatever the settings were rather than enabling all the time. Maybe that's the way to go. I don't like how the overhead adds up for a firmware call but I think if you're using the firmware you're not expecting things to be fast anyway.

A couple of questions I have about the crt:

1. Why is the stack moved down 6530 bytes in the startup?

ld hl,-6530 ; que??
;;ld hl,-64 ; reserve space for 32 entries on the exit stack
add hl,sp
ld sp,hl
ld (exitsp),sp

Is it being moved from high memory to below the area reserved for AMSDOS? Would it be better to load the sp value from the RAMTOP system variable?

2. Why is the org address set to 0x6000? As far as I can tell the lowest address without problems is 0x4000.



------------------------------------------------------------------------------
Mobile security can be enabling, not merely restricting. Employees who
bring their own devices (BYOD) to work are irked by the imposition of MDM
restrictions. Mobile Device Manager Plus allows you to control only the
apps on BYO-devices by containerizing them, leaving personal data untouched!
https://ad.doubleclick.net/ddm/clk/3045 ... 31938128;j
User avatar
dom
Well known member
Posts: 2090
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

On Sat, 14 May 2016, alvin (alvin_albrecht@...) wrote:
1. Why is the stack moved down 6530 bytes in the startup?

ld hl,-6530 ; que??
;;ld hl,-64 ; reserve space for 32 entries on the exit stack
add hl,sp
ld sp,hl
ld (exitsp),sp
I'm not sure, the crt was modifed by a CPC user warp6 so it got
incorporated back in 2003. I'm guessing it might be something to do with
paging? - When the firmware is called is the page the stack is located in
paged out?
Is it being moved from high memory to below the area reserved for
AMSDOS? Would it be better to load the sp value from the RAMTOP system
variable?

2. Why is the org address set to 0x6000? As far as I can tell the
lowest address without problems is 0x4000.
Let's set it to that then, I'm guessing it was just an arbitrary choice
chosen from some online examples!



------------------------------------------------------------------------------
Mobile security can be enabling, not merely restricting. Employees who
bring their own devices (BYOD) to work are irked by the imposition of MDM
restrictions. Mobile Device Manager Plus allows you to control only the
apps on BYO-devices by containerizing them, leaving personal data untouched!
https://ad.doubleclick.net/ddm/clk/3045 ... 31938128;j
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

On Sat, 14 May 2016, alvin (alvin_albrecht@...) wrote:
1. Why is the stack moved down 6530 bytes in the startup?

ld hl,-6530 ; que??
;;ld hl,-64 ; reserve space for 32 entries on the exit stack
add hl,sp
ld sp,hl
ld (exitsp),sp
I'm not sure, the crt was modifed by a CPC user warp6 so it got
incorporated back in 2003. I'm guessing it might be something to do with
paging? - When the firmware is called is the page the stack is located in
paged out?
I found some technical information on the cpc. It's still surprising that all these years after market there still isn't a complete scan of the cpc firmware technical guide. It's missing several chapters including a few of the most important ones that explain memory map and firmware usage.

ROMs can be paged in in bottom 16k or top 16k with the latter overlapping the display ram. Available ram exists from 0x40 up to 0xbffff but installed ROMs can reserve memory at the bottom of the ram or the top as they are initialized, effectively squeezing from the bottom and the top of the available area. The bottom 0x40 bytes include many useful stubs for long jumps to different ROMs. The Basic rom reserves some space as does the AMSDOS disk stuff. According to stef's wiki entry, programs can be loaded to 0x1200 from Basic without having to use a memory command to push basic down so that's what I've changed the default org to. To figure out where the top ram location is, I am using the contents of $ae60 which is set up by basic to indicate the last UDG byte and is the last available ram address. $ae60 is good for any cpc model so I set the stack there and then drop down 64 bytes to reserve space for the exit stack. In this way the stack is outside the paging area an!
d we get
max use of ram compared to the arbitrary and unsafe adjustment of 6500 bytes that was there before.

I also found a section on using the exx set and they had some sample code that I almost straight copied. It turns out only bc' has to be saved and while interrupts are enabled, f' has to be saved because the carry is some indicator for whether the interrupt path of some code should run instead of the regular path. Interrupts are re-enabled while they are being serviced.

I stole subtype=noint to mean exactly what it says -- the crt will not supply the interrupt interposer but it still must be supplied by the user program so this is an opportunity for the user to set up his own isr.

I also added the stackptr pragma found on the spectrum.

Anyway, I think the cpc port should be rock solid now.



------------------------------------------------------------------------------
Mobile security can be enabling, not merely restricting. Employees who
bring their own devices (BYOD) to work are irked by the imposition of MDM
restrictions. Mobile Device Manager Plus allows you to control only the
apps on BYO-devices by containerizing them, leaving personal data untouched!
https://ad.doubleclick.net/ddm/clk/3045 ... 31938128;j
stefano
Well known member
Posts: 2145
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

>Anyway, I think the cpc port should be rock solid now.


Excellent, the CPC target always gave me headaches ..it is built advanced logics which to be honest are far away from my comfort zone :P

I confirm sometime the memory allocation was arbitrary, sometimes I did last minutes corrections, (e.g. when moving from disk to cassette or vice-versa) I had to trim the code position to see it run everywhere.

I'm surprised as you are for the lack of documentation on such popular micros.. that's why I'm working on basck.c ..which just permitted me to get a not fully accurate but running assembly source of the MSX1 ROM ;)



------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/3052 ... 32659582;e
Post Reply