z88dk / z88dk

The development kit for over a hundred z80 family machines - c compiler, assembler, linker, libraries.
https://www.z88dk.org
Other
913 stars 174 forks source link

48-bit genmath library crashes on Z180 #1535

Closed jblang closed 4 years ago

jblang commented 4 years ago

The 48-bit genmath library crashes on Z180 because it uses undocumented instructions to access ixl/ixh registers. These are not supported by the Z180 (nor are any undocumented instructions) and cause a TRAP interrupt which vectors to 0H. When running under CP/M this manifests as the program unexpectedly exiting with no explanation as to why.

IXL/IXH is used in float.asm and ldbchl.asm. Here is a potential rewrite of the relevant sections of code that should work on Z180. Note I have not tested this yet because I am using a nightly build for Windows and I am not sure how to rebuild the library.

https://github.com/z88dk/z88dk/blob/master/libsrc/math/genmath/c/sccz80/float.asm#L37-L44

    ld  b,e
    ld  c,l
    push    bc
    pop ix
    ld  c,d
        ld  d,l
        ld  e,0
        ld  b,e
        LD      A,32+128

https://github.com/z88dk/z88dk/blob/master/libsrc/math/genmath/c/sccz80/float.asm#L50-L57:

    ld  c,0
    ld  b,l
    push    bc
    pop ix
    ld  c,h
    ld  de,0
    ld  b,e
    ld  a,16 + 128
    jr  gonorm

https://github.com/z88dk/z88dk/blob/master/libsrc/math/genmath/c/sccz80/ldbchl.asm#L9-L23:

.ldbchl ld      e,(hl)
        inc     hl
        ld      d,(hl)
        inc     hl
        ld      c,(hl)
        inc     hl
        ld      b,(hl)
    push    bc
    pop ix
        inc     hl
        ld      c,(hl)
        inc     hl
        ld      b,(hl)
        inc     hl
    ret
feilipu commented 4 years ago

@jblang pending getting it fixed in next opportunity, try either math48 -lmath48 or math32 --math32 as an alternative maths library.

jblang commented 4 years ago

Already tried -lmath48 and it works.

jblang commented 4 years ago

I tried math32 and it works too. I see the docs say it supports the Z180 hardware multiply. Is that out of the box or is there an additional flag I have to use? Is the Z180 autodetected so that the same binary will work on both Z80 and Z180?

feilipu commented 4 years ago

Support for z180 and z80n (Spectrum Next hardware), works out of the box. You just need to compile with the appropriate machine type included, -mz180 or -mz80n respectively. The machine type is always associated with the target specification.

The only place where you'll need to specify the machine type is where you're using classic CP/M target +cpm, the newlib CP/M target +cpm -clib=new, or the newlib HBIOS target +hbios, as they all will default to the general case of the z80 instruction set.

You can check the respective .cfg file associated with the target you're using, to see whether a different machine type needs to be added.

suborb commented 4 years ago

genmath uses ixh/ixl quite extensively - I can count about 70 uses so I don't think fixing it up is viable.

Given that math48 has the same characteristics it might be sensible to switch it in as the default maths library for classic, retaining genmath for targets where using the alternate set causes issues.

feilipu commented 4 years ago

Perhaps "won't fix", and change the default?