neilsf / xc-basic3

A BASIC cross compiler for MOS 6502-based machines
MIT License
42 stars 5 forks source link

Strange issue with SPRITE parameter #231

Closed AGPX closed 1 year ago

AGPX commented 1 year ago

Hi again,

I'm struggling to understand why the following code not works (sprite is not moving) when I use a variable in the SPRITE instruction (s or THIS.spr instead of 0). If you decomment the 2 lines commented below (and comment the next one), it works. Why? Tested on latest version 3.1, no error or warning during compilation:

xcbasic3.exe src\test.bas output\test.prg 
Complete. (0)
+---------------+-------+-------+
|    Segment    | Start |  End  |
+---------------+-------+-------+
|BASIC Loader   | $0801 | $080C |
|Program code   | $080D | $09EB |
|Library        | $09EC | $09FF |
|Variables*     | $0A00 | $0A0E |
+---------------+-------+-------+
(*) Uninitialized segment.
' This code doesn't work. If you decomment the 2 lines and comment the following one, it works. Why?

TYPE TSPRITE
    spr AS BYTE
    x   AS INT
    y   AS BYTE

    SUB setPosition() STATIC
        'SPRITE 0 AT THIS.x, THIS.y
        SPRITE THIS.spr AT THIS.x, THIS.y
    END SUB

    SUB move() STATIC
        THIS.x = THIS.x + 1
        CALL THIS.setPosition()
    END SUB

    SUB init(s as BYTE, px AS INT, py AS BYTE, c AS BYTE) STATIC
        'SPRITE 0 ON COLOR 7
        SPRITE s ON COLOR c

        THIS.spr = s
        THIS.x = px
        THIS.y = py

        CALL THIS.setPosition()
    END SUB
END TYPE

DIM spr AS TSPRITE
CALL spr.init(0, 24, 100, 7)

FOR k as INT = 0 TO 320
    CALL spr.move()
    DO WHILE PEEK($D012) <> 240: LOOP
NEXT

Notice that if I put the variable s between parathesis, the compiler crash (not sure if it's related):

SPRITE (s) ON COLOR c

object.Exception@source\language\factor.d(118): Add case for XCBASIC.Factor
----------------
0x00007FF713015A37
0x00007FF712EDAEB1
0x00007FF712F16096
0x00007FF712ED8BEE
3
0x00007FF712ED8BF8
0x00007FF712EDA1B8
4
0x00007FF712ED8BF8
0x00007FF712F5F083
0x00007FF712EC2DFD
0x00007FF712EC3724
2
0x00007FF712EC376B
0x00007FF712D62E06
0x00007FF71302DFE3
0x00007FF71302DE3F
0x00007FF71302DF3F
0x00007FF71302DE3F
0x00007FF71302DD4A
0x00007FF713017F29
0x00007FF712D68422
0x00007FF71308A53C
0x00007FFE54F17614 in BaseThreadInitThunk
0x00007FFE551226F1 in RtlUserThreadStart
neilsf commented 1 year ago

Looking at it I'd say it's a bug in the compiler. I'll check.

neilsf commented 1 year ago

Fixed in https://github.com/neilsf/xc-basic3/releases/tag/v3.1.6 Thanks for the bug report!