z00m128 / sjasmplus

Command-line cross-compiler of assembly language for Z80 CPU.
http://z00m128.github.io/sjasmplus/
BSD 3-Clause "New" or "Revised" License
381 stars 54 forks source link

savesna PC counter seem to get lost between sjasmplus 1.14.5 and newer versions when using NexCreator #231

Closed J0hnniemac closed 5 months ago

J0hnniemac commented 5 months ago
Version Platform Topic
v1.15.x Darwin Johns-Mac-Studio-3.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64 savesna

I have a simple program

    device zxspectrum48
; Flash Border Colors on ZX Spectrum Next
    org $7878
    jp start
    align 256                   ; table must be aligned to a 256 byte boundry
IM2Table        ds 257                      ; 257 bytes for our table
StackSpaceEnd       ds 127                      ; safe place for stack
StackSpace      db 0
    display $
    ORG $8000           ; Start assembling at address 32768
    ld sp, StackSpace
start:
    ld hl, key_scan     ; Address of the key scan routine
    ld b, 7             ; Number of colors to cycle through

next_color:
    ld a, (hl)          ; Load border color from table
    out (254), a        ; Set border color
    inc hl              ; Move to next color in the table
    djnz next_color     ; Loop through all 7 colors

    call key_pressed    ; Check if a key has been pressed
    jr z, start         ; If no key pressed, repeat

    ret                 ; Return from program

; Check if any key is pressed
key_pressed:
    ld bc, 0xFEFE       ; Prepare for reading keyboard
    in a, (c)           ; Read keyboard port
    cpl                 ; Complement the result
    and 0x1F            ; Mask out relevant bits
    ret                 ; Return, zero flag is set if no key is pressed

; Data: Border color values (classic ZX Spectrum colors)
key_scan:
    db 0, 1, 2, 3, 4, 5, 6

    SAVESNA "test.sna",start

if I compile in version 1.14.5 and use the NexCreator tool with file test.sna,5,4000

./NexC NexList.txt test.nex

the text.net run and has PC set

if I compile in a version > 1.14.5 run ./NexC NexList.txt test.nex PC will = 0000 and fails to to run

checking the two sna file the header is different Screenshot 2024-05-07 at 21 31 29 different

ped7g commented 5 months ago

The highlighted difference between produced snap files is position of the stack. In recent versions the fake system variables and stack is by default at rather low address as most of the game/demo loaders do by using CLEAR command early in the basic loader.

To get stack up into higher area you can try device zxspectrum48, $FF32 instead, setting "RAMTOP" explicitly to $FF32 (or other address you like). This will produce same SP in the sna header as the 1.14.5 did.

Whether this is enough to fix it for NexCreator - I don't know, maybe it's sensitive also about the fake stack content, not just about it's position, up to you to try.

Technically both sna files are valid and working starting on the "start" address, so it's not sjasmplus's bug, but NexCreator's bug not being able to read the start address from the snapshot.

JFYI 1: you have start label after the ld sp,... instruction, so this one will be missed (not sure if this is intentional and NexCreator uses this as some magic way to set SP in nex header, or you just provided quick example and didn't notice you have start label after it).

JFYI 2: sjasmplus has SAVENEX instruction which is IMHO more powerful than NexCreator, plus it's native to sjasmplus, so you don't need the external tool. Maybe you should take a look on it, or maybe you don't like it because some reason (feedback is welcome)? http://z00m128.github.io/sjasmplus/documentation.html#c_savenex

J0hnniemac commented 5 months ago

Thanks - My preference is to use SAVENEX, I eventually got it working with the my same test code. device ZXSPECTRUMNEXT ; Flash Border Colors on ZX Spectrum Next org $7878 jp start ld SP, StackSpace align 256 ; table must be aligned to a 256 byte boundry IM2Table ds 257 ; 257 bytes for our table StackSpaceEnd ds 127 ; safe place for stack StackSpace db 0 display $ ORG $8000 ; Start assembling at address 32768

start: ld hl, key_scan ; Address of the key scan routine ld b, 7 ; Number of colors to cycle through

next_color: ld a, (hl) ; Load border color from table out (254), a ; Set border color inc hl ; Move to next color in the table djnz next_color ; Loop through all 7 colors

call key_pressed    ; Check if a key has been pressed
jr z, start         ; If no key pressed, repeat

ret                 ; Return from program

; Check if any key is pressed key_pressed: ld bc, 0xFEFE ; Prepare for reading keyboard in a, (c) ; Read keyboard port cpl ; Complement the result and 0x1F ; Mask out relevant bits ret ; Return, zero flag is set if no key is pressed

; Data: Border color values (classic ZX Spectrum colors) key_scan: db 0, 1, 2, 3, 4, 5, 6

SAVENEX OPEN "test.nex",start,StackSpace,5;
SAVENEX BANK 5,2,0
SAVENEX CLOSE

I was unable to get the sample code working with only getting blank screen and green boarder in CSpect Screenshot 2024-05-08 at 10 53 59

J0hnniemac commented 5 months ago

Thanks for you help, please close this bug, as you said NexCreator is at fault here