Hack-a-Day / 2018-Supercon-Badge

Retrocomputing badge for the 2018 Hackaday Superconference
https://hackaday.io/project/161859-2018-hackaday-superconference-badge
MIT License
27 stars 8 forks source link

BASIC: problems with GOSUB #14

Closed szczys closed 6 years ago

szczys commented 6 years ago

I'm testing out a program and believe I've encountered some problem with calling GOSUB from withing a GOSUB call. Is this not allowed? I'm getting bizarre behavior. You can try this out -- I'm using external circuit but you should still hear the notes int he speaker and they should be changing instead of repeating.

10 D = 0
20 L = 1
30 C = 2
40 edr D,0
50 edr L,0
60 edr C,0
70 eout D,0
80 eout L,0
90 eout C,0
100 goto 300
300 Q=25
301 P=100
302 gosub 2800
310 gosub 2000
320 gosub 2200
330 gosub 2400
340 gosub 2600
350 goto 300
500 P=400
510 Q=25
520 gosub 2800
530 for i=1 to 6
540 gosub 2000
550 next i
560 for i=1 to 6
570 gosub 2200
580 next i
590 for i=1 to 6
600 gosub 2400
610 next i
620 goto 500
1000 rem Pulse clock
1010 eout C,1
1020 eout C,0
1030 return
1100 rem Pulse latch
1110 eout L,1
1120 eout L,0
1130 return
1200 rem Clock in "x" times
1210 for y=1 to x
1220 gosub 1000
1230 next y
1240 return
2000 rem Chopsticks FG
2010 eout D,0
2020 x=3
2030 gosub 1200
2040 eout D,1
2050 x=2
2060 gosub 1200
2070 eout D,0
2080 x=3
2090 gosub 1200
2100 gosub 1100
2110 tune 65,67,0,P
2111 wait P
2115 gosub 2800
2120 return
2200 rem Chopsticks EA
2210 eout D,0
2220 x=2
2230 gosub 1200
2240 eout D,1
2242 gosub 100
2244 eout D,0
2246 gosub 1200
2248 eout D,1
2250 gosub 1000
2252 eout D,0
2254 gosub 1200
2256 gosub 1100
2257 tune 64,69,0,P
2258 wait P
2259 gosub 2800
2260 return
2400 rem Chopsticks DB
2410 eout D,0
2412 gosub 1000
2414 eout D,1
2416 gosub 1000
2418 eout D,0
2420 x=4
2422 gosub 1200
2424 eout D,1
2426 gosub 1000
2428 eout D,0
2430 gosub 1000
2432 gosub 1100
2433 tune 62,71,0,P
2434 wait P
2435 gosub 2800
2436 return
2600 rem Chopsticks CC
2610 eout D,1
2612 gosub 1000
2614 eout D,0
2616 x=6
2618 gosub 1200
2620 eout D,1
2622 gosub 1000
2624 gosub 1100
2625 tune 60,71,0,P
2626 wait P
2627 gosub 2800
2628 return
2800 rem Clear and pause
2810 eout D,0
2820 x=8
2830 gosub 1200
2840 gosub 1100
2850 wait Q
2860 return
szczys commented 6 years ago

I tested this with the old tokenizer and I get Unreachable line 2800 called from 302

Any idea what's causing that error?

szczys commented 6 years ago

Tried using gosub to lines that are not the 'rem' command but that doesn't seem to fix the problem.

10 D = 0
20 L = 1
30 C = 2
40 edr D,0
50 edr L,0
60 edr C,0
70 eout D,0
80 eout L,0
90 eout C,0
100 goto 300
300 Q=25
301 P=100
302 gosub 2810
310 gosub 2010
320 gosub 2210
330 gosub 2410
340 gosub 2610
350 goto 300
1000 rem Pulse clock
1010 eout C,1
1020 eout C,0
1030 return
1100 rem Pulse latch
1110 eout L,1
1120 eout L,0
1130 return
1200 rem Clock in "x" times
1210 for y=1 to x
1220 gosub 1010
1230 next y
1240 return
2000 rem Chopsticks FG
2010 eout D,0
2020 x=3
2030 gosub 1210
2040 eout D,1
2050 x=2
2060 gosub 1210
2070 eout D,0
2080 x=3
2090 gosub 1210
2100 gosub 1110
2110 tune 65,67,0,P
2111 wait P
2115 gosub 2810
2120 return
2200 rem Chopsticks EA
2210 eout D,0
2220 x=2
2230 gosub 1210
2240 eout D,1
2242 gosub 100
2244 eout D,0
2246 gosub 1210
2248 eout D,1
2250 gosub 1010
2252 eout D,0
2254 gosub 1210
2256 gosub 1110
2257 tune 64,69,0,P
2258 wait P
2259 gosub 2810
2260 return
2400 rem Chopsticks DB
2410 eout D,0
2412 gosub 1010
2414 eout D,1
2416 gosub 1010
2418 eout D,0
2420 x=4
2422 gosub 1210
2424 eout D,1
2426 gosub 1010
2428 eout D,0
2430 gosub 1010
2432 gosub 1110
2433 tune 62,71,0,P
2434 wait P
2435 gosub 2810
2436 return
2600 rem Chopsticks CC
2610 eout D,1
2612 gosub 1010
2614 eout D,0
2616 x=6
2618 gosub 1210
2620 eout D,1
2622 gosub 1010
2624 gosub 1110
2625 tune 60,71,0,P
2626 wait P
2627 gosub 2810
2628 return
2800 rem Clear and pause
2810 eout D,0
2820 x=8
2830 gosub 1210
2840 gosub 1110
2850 wait Q
2860 return
jaromir-sukuba commented 6 years ago

Please, rerun this code with 1.13 In 1.12, I added error code when GOSUB stack is overflown, it was silently ignored (and causing problems) before. Also, RETURN underflow was added in 1.13

szczys commented 6 years ago

I get gosub stack overflow at line 1220 after hearing the tones 4 times.

jaromir-sukuba commented 6 years ago

I didn't trace your code manually to count GOSUB levels, but it looks quite innocent. I just commited 1.14, with GOSUB stack depth increased from 10 to 30 levels. See if it helps. If not, you may have unbalanced GOSUB/RETURN.

szczys commented 6 years ago

I believe line 2242 was causing the problem... It's a gosub to 100 instead of 1000. I'll do more testing and then close.

Sorry if I wasted time on this. I do think the overflow error is a good one to have enabled.

jaromir-sukuba commented 6 years ago

No time wasted, this is very valuable practical feedback. The overflow indication is definitely needed and now we have it. Also, I need to add overflow indication for nested FOR loops.

szczys commented 6 years ago

Everything is working as expected with GOSUB. the error was in my code.