Not sure why this code always crashes

Started by Miasma, May 24, 2022, 02:26:15 PM

Previous topic - Next topic

Miasma

I've written some code to produce sound and visuals on a Vectrex. It's a bit of a mess, but it does (sort of) work. The premise is that you send in bytes over the USB serial, and the Vectrex plays a corresponding note, and updates visuals for it.

Problem is that it always crashes after a certain number of notes. It's fine if it's just left running, but when notes are coming through it always crashes eventually. I've tried commenting out all the 3D graphics stuff, and also all the sound generating stuff, but it still seems to crash even then.

call IntensitySprite(80)

call SetFrameRate(60)


char_rcd = 0

notice = { _
    {-70, 70, "1"}, _
    {-70, 50, "2"}, _
    {-70, 30, "3"}}

notes = { _
    {22,102}, _
    {21,36}, _
    {19,244}, _
    {18,213}, _
    {17,199}, _
    {16,199}, _
    {15,214}, _
    {14,243}, _
    {14,28}, _
    {13,81}, _
    {12,146}, _
    {11,221}, _
    {11,51}, _
    {10,146}, _
    {9,250}, _
    {9,107}, _
    {8,227}, _
    {8,100}, _
    {7,235}, _
    {7,121}, _
    {7,14}, _
    {6,169}, _
    {6,73}, _
    {5,239}, _
    {5,153}, _
    {5,73}, _
    {4,253}, _
    {4,181}, _
    {4,114}, _
    {4,50}, _
    {3,246}, _
    {3,189}, _
    {3,135}, _
    {3,84}, _
    {3,36}, _
    {2,247}, _
    {2,205}, _
    {2,164}, _
    {2,126}, _
    {2,91}, _
    {2,57}, _
    {2,25}, _
    {1,251}, _
    {1,222}, _
    {1,195}, _
    {1,170}, _
    {1,146}, _
    {1,124}, _
    {1,102}, _
    {1,82}, _
    {1,63}, _
    {1,45}, _
    {1,28}, _
    {1,12}, _
    {0,253}, _
    {0,239}, _
    {0,226}, _
    {0,213}, _
    {0,201}, _
    {0,190}, _
    {0,179}, _
    {0,169}, _
    {0,160}, _
    {0,151}, _
    {0,142}, _
    {0,134}, _
    {0,127}, _
    {0,120}, _
    {0,113}, _
    {0,107}, _
    {0,101}, _
    {0,95}, _
    {0,90}, _
    {0,85}, _
    {0,80}, _
    {0,75}, _
    {0,71}, _
    {0,67}, _
    {0,63}, _
    {0,60}, _
    {0,56}, _
    {0,53}, _
    {0,50}, _
    {0,47}, _
    {0,45}, _
    {0,42}, _
    {0,40}, _
    {0,38}, _
    {0,36}, _
    {0,34}, _
    {0,32}, _
    {0,30}, _
    {0,28}, _
    {0,27}, _
    {0,25}, _
    {0,24}, _
    {0,22}, _
    {0,21}, _
    {0,20}, _
    {0,19}, _
    {0,18}, _
    {0,17}, _
    {0,16}, _
    {0,15}, _
    {0,14}, _
    {0,13}, _
    {0,13}, _
    {0,12}, _
    {0,11}, _
    {0,11}, _
    {0,10}, _
    {0,9}, _
    {0,9}, _
    {0,8}, _
    {0,8}, _
    {0,7}, _
    {0,7}, _
    {0,7}, _
    {0,6}, _
    {0,6}}


cameraTrans = {0, 0, 0}
call CameraTranslate(cameraTrans)
call Randomize
call ScaleSprite(64, 324 / 0.097)

shipWidth = 7.0
shipHeight = shipWidth * 0.75
podDiameter = shipWidth * 5 / 8
ships = NewShip()

'crossDisplay = LinesSprite(crosshair)
sprite_mag = 4
play = false
print "done..."
frames = 0
vol = 0
c = 40
p = 30
char_rcd = 0
startframe = 0
arp = 0

while true
    controls = WaitForFrame(JoystickNone, Controller1, JoystickNone)

for i = 1 to UBound(ships)
    call SpriteRotate(ships[i, 1], 0.4, 0, 0)
next i
if UBound(ships) > 10 then
for i = 1 to UBound(ships)
    call RemoveSprite(ships[i, 1])
next i
'call ClearScreen()

'cameraTrans = {0, 0, 0}
'call CameraTranslate(cameraTrans)
'call Randomize
'call ScaleSprite(64, 324 / 0.097)

ships = NewShip()
endif
    'call SpriteRotate(triangleSprite, 1)
    frames = frames + 1

if feof(Stdin) = false then
call ReturnToOriginSprite()
         ships = AppendArrays(ships, NewShip())
'print "data!"
c = fgetc(Stdin)
command = false
if c = 34 or c = 13 then
command = true
endif
if c = 1 then
command = true
if arp = 1 then
arp = 0
else
arp = 1
endif
endif
if not command then
p = c - 30
endif
'xy[1,1] = xy[1,1] + 1
'info[1, 3] = "SCALE: " + p
char_rcd = char_rcd + 1
play = true
         vol = 15
         startframe = frames
endif

    if controls[1, 4] then
print "play from joy", UBound(ships)
play = true
vol = 15
startframe = frames
endif

if play and vol > -1 then
         
if vol > -1 and (frames - startframe) mod 4 = 0 then
vol = vol - 1
endif

if frames - startframe = 1 then
              'print "playing something!"

call Sound({{0, notes[p,2]}, {1, notes[p,1]}})
call Sound({{7, $3e}, {8, vol}})
if vol > 10 then
for i = 1 to UBound(ships)
call SpriteSetMagnification(ships[i, 1], p/10)
next i
sprite_mag = p/10
endif
endif

if frames - startframe = 2 then
arp_add = 0
if arp then
arp_add = 5
endif
call Sound({{0, notes[p+arp_add,2]}, {1, notes[p+arp_add,1]}})
call Sound({{7, $3e}, {8, vol}})

if vol > 10 then
for i = 1 to UBound(ships)
call SpriteSetMagnification(ships[i, 1], (p+arp_add)/10)
next i
sprite_mag =(p+arp_add)/10
endif
endif

if frames - startframe = 3 then
arp_add = 0
if arp then
arp_add = 10
endif
call Sound({{0, notes[p+arp_add,2]}, {1, notes[p+arp_add,1]}})
call Sound({{7, $3e}, {8, vol}})
if vol > 10 then
for i = 1 to UBound(ships)
call SpriteSetMagnification(ships[i, 1], (p+arp_add)/10)
next i
sprite_mag =(p+arp_add)/10
endif
endif

if frames - startframe = 4 then
startframe = frames
endif

endif

     if sprite_mag > 1 or sprite_mag < 1 then
if sprite_mag > 4 then
sprite_mag = sprite_mag - 0.1
endif
if sprite_mag < 4 then
sprite_mag = sprite_mag + 0.1
endif
for i = 1 to UBound(ships)
call SpriteSetMagnification(ships[i, 1], sprite_mag)
next i
endif

    'if controls[1, 6] then
    '    stop
    'endif
endwhile

function TieFighter(shipWidth, shipHeight, podDiameter, wingSides, podSides)

tie = { _
{ MoveTo, -10,-10,10},_
{ DrawTo, 10,-10,10},_
{ DrawTo, 10,10,10}, _
{ DrawTo, -10,10,10}, _
{ DrawTo, -10,-10,10}, _
{ DrawTo, -10,-10,-10}, _
{ DrawTo, 10,-10,-10},_
{ DrawTo, 10,10,-10}, _
{ DrawTo, -10,10,-10}, _
{ DrawTo, -10,-10,-10}, _
{ MoveTo, 10,10,10}, _
{ DrawTo, 10,10,-10}, _
{ MoveTo, -10,10,-10}, _
{ DrawTo, -10,10,10}, _
{ MoveTo, 10,-10,-10}, _
{ DrawTo, 10,-10,10} _
}
    return tie
endfunction



function NewShip()
    dim ship[1, 2]
    ship[1, 1] = Lines3DSprite(TieFighter(shipWidth, shipHeight, podDiameter, 6, 8))
    ' Choose a distance from us between 150 and 300 meters
    z = rand() mod 150 + 150
    ' Choose an apparent position on the screen in meters (the screen is
    ' about 0.15 meters by 0.18 meters)
    x = (rand() mod (150 - shipWidth) - 75) / 1000.0
    y = (rand() mod (180 - shipHeight) - 90) / 1000.0
    ' Scale the apparent position on the screen to an actual translation based on
    ' the ship's z distance (and assuming the user sits 0.5 meters from the screen)
    x = x / 0.5 * (z + 0.5)
    y = y / 0.5 * (z + 0.5)
    ship[1, 2] = {x, y, z}
    call SpriteTranslate(ship[1, 1], ship[1, 2])
    ' Set the rotation of the ship
    call SpriteRotate(ship[1, 1], rand() mod 360, rand() mod 360, rand() mod 360)
    return ship
endfunction

Vectrex32

So if you've commented out the sound and graphics, you're basically just getting characters from the PC, and it crashes?

Is the BASIC program crashing, or is the entire V32 crashing? One thing to try is, after it crashes, switch to a terminal window, and see if BASIC is still alive.

How are you sending the notes? Is it possible one of them is an ASCII code 3? That would be interpreted as a Ctrl+C and would break out of the program. There's a way to disable Ctrl+C processing.

- Bob

Miasma

No ASCII codes under 34 are sent so it can't be that it's sending code 3. I've just tried it and switched to a serial window and BASIC is definitely not alive any more after it crashes!

I'm currently sending the bytes over USB from a laptop. I'm not sending the data particularly fast, so I don't think that's the problem.

Vectrex32

Do you have the latest version of Vectrex32 firmware (1.27)?

What's the program you're running on the PC? I could try to reproduce the crash.

Miasma

It's actually running 1.28 - the firmware you sent me with feof.

This is the python script I'm using to send the data:

import serial
import time

ser = serial.Serial("COM35", 9600)

n = 127
while 1:
    c = n
    if c != 34 and c != 13:
        c = chr(c)
        ser.write(c.encode())
        print(c)
    n -= 1
    if n < 33:
        n = 127
    time.sleep(0.1)

Vectrex32

You have a memory leak. You keep creating ReturnToOriginSprite objects, but you never remove them.

I noticed that as the program ran, the tones changed more slowly, so something was loading down the system. I Ctrl+Ced out, printed GetCompiledSpriteCount() and it was over 1000. I called DumpSprites() and saw lots of ReturnToOrigin sprites.

I uncommented the call to ClearScreen() on line 168 and that seems to have fixed it.

Miasma

Thank you so, so much! Works perfectly now!