don't click here

Z80 in ASM68k with macros

Discussion in 'Engineering & Reverse Engineering' started by Hivebrain, Jul 12, 2021.

  1. Hivebrain

    Hivebrain

    Administrator
    3,049
    161
    43
    53.4N, 1.5W
    Github
    https://github.com/cvghivebrain/axm68k

    I made macros for all the Z80 instructions in ASM68k. In order to get certain instructions working (add, and, neg, nop, or & sub) which are shared with 68000, I modified ASM68k (now called AXM68k) to disable them, and then re-enabled them with further macros.

    I've tested it on Sonic 1's sound driver and Z80 startup code, and it works as expected, but it could do with more thorough testing. "ex af,af'" must be changed to "ex af,af" (without the apostrophe) or ASM68k throws up an error. Every other instruction can be used as-is.

    You may now delete AS.

    Credit to MarkeyJester for his help, and AURORA☆FIELDS for the idea.
     
    • Like Like x 3
    • Useful Useful x 2
    • List
  2. AURORA☆FIELDS

    AURORA☆FIELDS

    The cute one here Tech Member
    216
    24
    18
    Finland
    AMPS
    this is cool, though definitely a few possible issues I see with this:
    1. jr is not restricted to -$80 to $7F range, meaning invalid jump addresses will be accepted by the assembler
    2. Using invalid registers and register combinations seem to not be rejected and instead have unpredictable behavior
    3. using dc.w with ae+ will break the output of these macros
    4. Possible slow performance and huge listings dumps from all that string conparison
    5. It would be a good thing to mention using obj to tell which address is used for z80 code
    This is really cool and easier for portability given you don't need to replace command names, but I feel it could still be improved :P
     
    • Agree Agree x 3
    • Informative Informative x 1
    • List
  3. Hivebrain

    Hivebrain

    Administrator
    3,049
    161
    43
    53.4N, 1.5W
    Github
    1. Do you mean it'll accept it if the address is +$90?
    2. I changed things a bit so assembly will fail on invalid registers in most cases.
    3. Can you give an example?
    4. I haven't tested it on something as large as a SMS/GG game, but it seemed pretty quick doing the S1 sound driver.
    5. Good idea, I also added phase/dephase macros for more AS compatibility.
     
  4. AURORA☆FIELDS

    AURORA☆FIELDS

    The cute one here Tech Member
    216
    24
    18
    Finland
    AMPS
    1. Yup. It will assemble all offsets between -$80 and $FF, meaning that if you write some Z80 code yourself and accidentally add too much stuff, then suddenly jump targets for example will be off by a lot and its difficult to find the issue. This is why I implemented some of the complex ways to replace data bytes in my version: I didn't actually find a way to restrict offsets to signed only
    3:
    Code (Text):
    1.     ae+
    2.     cpu z80
    3.     nop        ; $00
    4.     ind        ; $ED $AA
    5.  
    6.     ; expected output: $00 $ED $AA
    7.     ; actual output: $00 $00 $ED $AA
    8.     ; ae+ automatically aligns word/long accessed to word boundaries. Solution: use bytes only.
     
    • Informative Informative x 1
    • List
  5. Hivebrain

    Hivebrain

    Administrator
    3,049
    161
    43
    53.4N, 1.5W
    Github
    I disabled ae for Z80, and added a range check for jr.

    One thing I found is apparently "if narg = 2" doesn't work, it has to be "if narg=2" without spaces. Something to be careful of in future.
     
  6. AURORA☆FIELDS

    AURORA☆FIELDS

    The cute one here Tech Member
    216
    24
    18
    Finland
    AMPS
    I'm really not sure if that fix will work in all cases, particularly forward jumps or ones that work based on a variable

    Also it's not well known but ASM68K will actually allow you to use spaces in any statement if you use opt ws+

    Finally please use pusho and popo so that the user options are correctly restored after changing cpu mode
     
  7. Hivebrain

    Hivebrain

    Administrator
    3,049
    161
    43
    53.4N, 1.5W
    Github
    You're right about jr not working:

    Code (Text):
    1.  
    2. D:\GITHUB\S1DISASM\SMPS.ASM(45) : Error : Expression must evaluate
    3.  jr_ptr: = (load_sample)-*-2
    4. D:\GITHUB\S1DISASM\SMPS.ASM(75) : Error : Expression must evaluate
    5.  jr_ptr: = (play_segapcm)-*-2
    This is the S1 sound driver I'm testing it on. Why isn't it evaluating? Looks fine to me.

    Edit: I think I know what's happening. It doesn't know what those addresses are because they're ahead of the macro. That's why you mentioned forward jumps.
     
    Last edited: Jul 15, 2021