PS2 and program crash/hang

Greetings all.
I have been dissecting parts of the PS2 code that is made from powerpod, I’m trying to get PS2 control as simple as I can then work from that point but I ran in to an issue.
This program {see below} will work for a time then after a 2min or so will simply stop functioning. Ps2 data will also stop coming in on the terminal.

Adding a pause of 500 {pause 500} or so will delay the hang/crash. Seems to crash after a specific amount of iterations.
Perhaps the issue is that the ram is full or something? I tried adding a clear to the main part of the program but still no change.

With the powerpod hex program and other programs I have not had this issue, power is good and batteries are charged.

Also a side question if anyone feels like explaining this.
In the powerpod Hex program are the following “ if (DualShock(2).bit3 = 0)”
I understand that “dualShock(2)” is data on the shape{X circle tri box} and shoulder buttons, so pressing X will give you a value of 191 or pressing shoulder button R1 will give the value 247.

But what is the “.bit3” part, how is the program to know that bit3 is R1



DAT con P12
CMD con P13
SEL con P14
CLK con P15

Index var Byte

DeadZone con 28
PadMode con $79

DualShock var Byte(7)
DS2Mode var Byte
LastButton var Byte(2)


clear
high CLK
pause 500

Main
clear

;pause 100
;sound 9,[100\2000]

LastButton(0) = 255
LastButton(1) = 255

	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$1\8]
	shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]
	high SEL
	pause 1

	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]	
	shiftin DAT,CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
		DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
	high SEL
	pause 1	

	if DS2Mode <> PadMode then
	sound 9,[100\1000]
		low SEL
		shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
		high SEL
		pause 1

		low SEL
		shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
		high SEL
		pause 1

		;low SEL
		;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
		;high SEL
		;pause 1

		low SEL
		shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
		high SEL
		pause 1

		low SEL
		shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
		high SEL
		pause 1

		low SEL
		shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
		high SEL
		pause 288

		goto main
	endif
	
	
		serout S_OUT,i57600,[13, hex2 DS2mode\2]

for index = 1 to 18

serout S_OUT,i57600,"   ", dec3 Dualshock(index)\3]
next



	if (DualShock(2).bit3 = 0) and LastButton(1).bit3 then	;R1 Button test
		sound 9,[100\4000]
	elseif (DualShock(2).bit2 = 0) and LastButton(1).bit2	;L1 Button test
	sound 9,[100\4000]
	elseif (DualShock(2).bit1 = 0) and LastButton(1).bit1	;R2 Button test
	sound 9,[100\4000]
	elseif (DualShock(2).bit0 = 0) and LastButton(1).bit0	;L2 Button test
	sound 9,[100\4000]
	else
		gosub skip_Default
	endif
	; default codestuff
	
	skip_Default
	
	goto main
	
	;subs~~~~~~~~~~~~~~~~~~~~~~~~
	

The “bits” of data read from the PS2 wireless joystick have been defined by the way the hardware was wired. here are some notes:

Alan KM6VV

Looks like i found the issue, i had a gosub without a return and in the middle of the program, bad coding on my part :blush: though why it reset after 466 or so iterations or so i do not know but all fixed now.

Thanks for the info Km6vv, though I’m not totally sure of the meaning of the table, i will look in to it :smiley:
Kezat,

That would be because you eventually ran out of ram and started over writing system register memory locations. Every gosub pushes a return address on the stack. If you do enough gosubs without a return you will use all the ram. So it appears in this particular program you could do 466 nested gosubs before hitting your ram limit. :slight_smile:

The number of nested gosubs possible on an AtomPro will vary depending on how much user and system ram is used(eg how much ram is left for the stack). Also you may see other wierd things besides a reset if you trash system register memory locations. You could even trash user memory locations causing your program to produce wierd values. But in most cases you just get a reset.