Jump to content
IGNORED

Aagh, gosub makes my Atari screen go all weird!


The21zonz

Recommended Posts

14 hours ago, Gemintronic said:

I used to lean heavily on GOSUB and would run out of CPU time and roll the screen.

 

Once you get really dangerous with bB game making you'll find other reasons to avoid GOSUB use.

 

Currently I'm re-learning not to use GOSUBs the hard way.  Just say no to GOSUBs.  :)

How else do I clean up my code?

Link to comment
Share on other sites

15 hours ago, LatchKeyKid said:

Are you gosubing to another bank?   Something similar happened with me the first time I did that by mistake without properly addressing which one.

 

https://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#bankswitching

 

The proper way is detailed in the sidebar.

No. This is within the same bank

Link to comment
Share on other sites

16 hours ago, The21zonz said:

How else do I clean up my code?

 

It says this on the bB page:

 

"But you should avoid using gosub in cases where it really isn't necessary, such as if the code in the subroutine is only called from one spot, or if the subroutine is so short that it would actually be more efficient to just duplicate that code wherever it's needed."

 

Here is info about cycles and gosub:

 

gosub + return   =   12 cycles

 

Here is info about cycles and goto:

 

goto   =   3 cycles

 

Seems like a lot of new programmers will turn their code into tangled spaghetti by using gosub a billion times for no good reason. I treat gosub like it's radioactive. I only use it when I absolutely have to. When multiple areas of the program need to use the same large chunk of code, you basically have to use gosub to save space. I don't usually have a large chunk of code that needs to be used by multiple areas of a program, so I can avoid gosub most of the time.

 

I learned an if…then trick from batari years ago. It's in the section called Relief for ENDIF Addicts. You keep a chunk of code between an if…then and a label. You can have multiple lines of code all contained in that if…then/label area. You can think of it as little packages of code. You just have a bunch of if…then/label packages instead of using gosub a billion times, so you end up saving cycles.

 

I just updated the bB page with an adapted version of this post:

 

https://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#poap_subroutines

 

You might need to refresh/reload the page for the changes to show up.

  • Like 3
Link to comment
Share on other sites

23 hours ago, The21zonz said:

How else do I clean up my code?

 

It's a matter of personal style.  But, not using GOSUBs can force you to organize your code better.

 

Here is something I may have used a GOSUB for before:

 

main
 counter = counter + 1 : if counter = 255 then supercounter = supercounter + 1

 

 temp2 = read_playtime : if temp2 > 0 then goto sound_event
after_sound_event

 

I deal with sound effects in one particular place in my main loop.  So, I always return to immediately after this sound event code.  Simple to debug.  Less tempted to call the sound code from many places many more times.

 

Game assets are stashed away in the last bank anyway.  So, instead of GOSUB'ing to a fancy draw_playerbobo routine I just assign the sprite the first place I need it.  Usually just before starting the main loop.

 

main_begin
 lives = 160
 statusbarlength = 194
 lifecolor = _white

 rem //** Draggin C **//
 NUSIZ5 = $05
 player5:
 %00111110
 %11111101
 %10001100
 %10000110
 %01111100
 %00000110
 %00111110
 %00000111
 %10101011
 %11111111
 %01011010
 %00110110

end

 

main

 

 

  • Like 2
Link to comment
Share on other sites

On 9/9/2024 at 12:52 AM, Random Terrain said:

 

It says this on the bB page:

 

"But you should avoid using gosub in cases where it really isn't necessary, such as if the code in the subroutine is only called from one spot, or if the subroutine is so short that it would actually be more efficient to just duplicate that code wherever it's needed."

 

Here is info about cycles and gosub:

 

gosub + return   =   12 cycles

 

Here is info about cycles and goto:

 

goto   =   3 cycles

 

Seems like a lot of new programmers will turn their code into tangled spaghetti by using gosub a billion times for no good reason. I treat gosub like it's radioactive. I only use it when I absolutely have to. When multiple areas of the program need to use the same large chunk of code, you basically have to use gosub to save space. I don't usually have a large chunk of code that needs to be used by multiple areas of a program, so I can avoid gosub most of the time.

 

I learned an if…then trick from batari years ago. It's in the section called Relief for ENDIF Addicts. You keep a chunk of code between an if…then and a label. You can have multiple lines of code all contained in that if…then/label area. You can think of it as little packages of code. You just have a bunch of if…then/label packages instead of using gosub a billion times, so you end up saving cycles.

 

I just updated the bB page with an adapted version of this post:

 

https://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#poap_subroutines

 

You might need to refresh/reload the page for the changes to show up.

Guess I was doing a lot. (I only used GOSUB once!)

  • Like 2
Link to comment
Share on other sites

On 9/8/2024 at 6:52 PM, Random Terrain said:

 

It says this on the bB page:

 

"But you should avoid using gosub in cases where it really isn't necessary, such as if the code in the subroutine is only called from one spot, or if the subroutine is so short that it would actually be more efficient to just duplicate that code wherever it's needed."

 

Here is info about cycles and gosub:

 

gosub + return   =   12 cycles

 

Here is info about cycles and goto:

 

goto   =   3 cycles

 

Seems like a lot of new programmers will turn their code into tangled spaghetti by using gosub a billion times for no good reason. I treat gosub like it's radioactive. I only use it when I absolutely have to. When multiple areas of the program need to use the same large chunk of code, you basically have to use gosub to save space. I don't usually have a large chunk of code that needs to be used by multiple areas of a program, so I can avoid gosub most of the time.

 

I learned an if…then trick from batari years ago. It's in the section called Relief for ENDIF Addicts. You keep a chunk of code between an if…then and a label. You can have multiple lines of code all contained in that if…then/label area. You can think of it as little packages of code. You just have a bunch of if…then/label packages instead of using gosub a billion times, so you end up saving cycles.

 

I just updated the bB page with an adapted version of this post:

 

https://www.randomterrain.com/atari-2600-memories-batari-basic-commands.html#poap_subroutines

 

You might need to refresh/reload the page for the changes to show up.

Thanks for that explanation of the cycles, it's great to know that you can't abuse the GOSUBs and aim more at the GOTO, I'm having just problems with a sound and I think that's why, more or less what @Gemintronic wrote in his post.👌

  • Like 1
Link to comment
Share on other sites

gosub is extremely handy for structured programming, and saving on code size. The important part is not forgetting to return (this can happen if you might have a conditional goto that ends up skipping the return), or having too many levels of gosubs, which uses up RAM and could cause corruption. I would recommend never going over two levels of gosubs at any time. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...