Shop OBEX P1 Docs P2 Docs Learn Events
Try running this code there is a surprise in it — Parallax Forums

Try running this code there is a surprise in it

metron9metron9 Posts: 1,100
edited 2007-04-27 16:34 in BASIC Stamp
I have not quite figured out how to come back from program 2 yet. My goal was to make a memory swap function to run subroutines written compiled and stored on external memory for the BS2. I am just using data statements in this to see if it could be done.

If I knew how the tokenizer handled pointers for goto and returns it would be easier.

As it stands now though, one could use a setup program almost 2000 bytes long then pull in the main program but once it was done rebooting the stamp would only run the first program. Unles I can find a way to jump to the bootloader code fafter the first swap it could only be done once.

Fun to play with though, reminds me of my C64 days.



' {$STAMP BS2}
' {$PBASIC 2.5}
' {$PORT COM2}

BUFFER VAR Byte
FN VAR Byte
WL VAR Word

DATA 0,0,0,0
DATA $00,$00,$A6
DATA $D9,$D3,$EC,$69,$36,$34,$53,$36,$35,$4B,$9A,$29,$9B,$9A,$25
DATA $CD,$90,$66,$54,$13,$84,$4C,$35,$07,$C0



MAIN:
DEBUG "This is program 1"


BOOT:
WL=$07E0
FOR FN=0 TO 31
READ FN,BUFFER
WRITE WL,BUFFER
WL=WL+1
NEXT
DEBUG CR,"STARTING PROGRAM 2",CR
GOTO MAIN





▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-04-27 09:12
    Be careful.
    Due to the last GOTO MAIN, the same eeprom locations are overwritten every loop.
    This will wear out your stamp in a short time.

    regards peter
  • metron9metron9 Posts: 1,100
    edited 2007-04-27 14:30
    Hi Peter. I would not have posted it if it did that. I had hoped it would fall through to the boot loader again but it simply ends. I wanted to be able to use it like the other stamps with memory slots. Right now it is only capable of reading in a second program that is smaller than the first. The problem is each program loaded would have to be smaller than the preceding program and include it's own boot loader since the program running can not execute code outside its own program space.

    If I knew how the interpreter handled the GOTO pointer, then you could overlay the goto pointer to a specific point allowing the boot loader to be in eeproms lower memory 0...xxx.

    You are correct, running subroutines if many swaps are required would not be good. But it does have potential for a boot loader to run external programs stored on external eeprom like the other stamps with program slots if one could decipher the goto interpretation of tokenized code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-04-27 15:41
    So what code do the DATA represent?

    I remember I did something like this back in 2000/2001.
    A search through the basic stamp archive might·give the thread.
    The way I tried it was·first defining word variables and aliases using those words
    for variables used to load in·other code (from external eeprom), without overwriting
    the bootloader.·As program code starts at·$7FF·and grows
    downwards I don't think you should overwrite addresses $7E0-$7FF.
    Basically, every program should start with the identical bootloader code.
    You only read in the code that follows the bootloader code.

    regards peter
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-04-27 15:57
    Hi Metron

    If you are interested in the inner workings of the Stamp regarding the tokenizer coding of things like GOTO and RETURN, you should get a copy of Brian Forbes' Book.

    There is an example of a self-replacing program in the BASIC Stamp 1 app notes, which I think is still available on the Parallax downloads page. There are some very clever programs there, many of them having been written by Scott Edwards, I think.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • metron9metron9 Posts: 1,100
    edited 2007-04-27 16:34
    I will order that book if it still is available.

    I see the app note on the BS1. Interesting it overwrites its own code that is executing. I did not think to do this as typically the boot loader section doesn't overwrite itself.

    I will give it a go tonight on the BS2. Should be easy to implement a multi program boat loader on the BS2 using this method. I will post it as a project when it is stable.

    Thanks Tracy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
Sign In or Register to comment.