Shop OBEX P1 Docs P2 Docs Learn Events
Starting Spin-Interpreter with new bytecode — Parallax Forums

Starting Spin-Interpreter with new bytecode

Dear members,
I searched the forums really hard, but now I am stuck.

In order to understand how the Spin-interpreter is started from another cog, I wrote following program:
1. Spin-Code to blink on pin 27
- start new cog with pasm-prg
- stop it self
2. pasm-prg to start in another cog and
- copy the spin-byte-code (witch is stored in the DATA area as hex)
- start cog 0 with spin-interpreter

The byte-code stored in the DATA area is another spin-program to blink on Pin 26.

I wanted to avoid all the hassle with serial communication (SD-card, EEPROM) just to see, how to load another SPIN-prg to adress 0 in the Hub and how to start it.

I found some examples in the forums and booter.spin - and tried to strip the information down to the necessary.

In order to verify the steps, I used the Gear emulator. All runs well, until the Interpreter should start.
I saw the new Spin-code in Cog 0, but then nothing happens.

Testing on hardware showed the same result: nothing.

Is there someone out there, who would help me please ?

Thank You !

Comments

  • Technically, addresses 0 to 15 in the hub are configuration values; the first bit of code is offset to address 16.

    I think part of the problem you're having is that the second parameter of cognew() is supposed to be the address of a long, and you're providing the address of a byte.

    I do not know if your logic is sound. The purpose of cognew() -- when providing a Spin method as the first argument-- is to launch another interpreter and run the target code. My guess is that you're trying obfuscate the code you want to run. This seems counter to the idea the Propeller being very open.
  • Mike GreenMike Green Posts: 23,101
    edited 2018-12-30 17:06
    DongleBasic (in the Object Exchange) and this Flash driver both support loading a Spin program from a file and starting it up in Cog 0 as if it were just copied from EEPROM or serially from a PC. Look particularly at Winbond_Spin_Driver.spin in the Flash driver archive. BB_i2cSpiLdr.spin in DongleBasic is an example of a routine to load up Cog 0 from a block of EEPROM.

    Essentially, you have to have an assembly routine that copies the binary file of the new program (including the 16 byte header) to hub memory beginning at location 0. At this point, you can't have any Spin interpreters going because they're using the information in the program header. Once the copy is done, your assembly routine can start a Spin interpreter as shown.
  • Dear Jon and Mike,
    thank you for the quick answer.
    I have found the solution - now it works:
    CON
     _clkmode = xtal1 + pll16x
     _xinfreq = 5_000_000
    
    OBJ
      pin  : "Input Output Pins"
      time : "Timing"
    
    PUB Blink27
    
      repeat 3
        pin.High(27)
        time.Pause(200)   
        pin.Low(27)
        time.Pause(200)
        
      cognew(@ASM,@PRG01)
      cogstop (0)
        
    DAT
                   org 0
    ASM        mov hubram, par     'get pointer for spin-code
                   mov address, #0     ' Beginning of Hub
                   mov counter,#$61    ' 400 Bytes Spin-Programm
    
    :loop       rdlong spindata, hubram		'get spin-code long
    	       wrlong spindata, address		'write to ram
    	       add	address,#4		     'inc address
                   add  hubram, #4               'inc pointer
    	       djnz	counter,#:loop		'loop until done
    '
    ' Launch program in ram
    '
    launch	     rdword address,#$0004+2	'if pbase address invalid, shutdown
    		     cmp address,#$0010	wz
    	             if_nz  jmp #shutdown
    
    
    :delay           djnz time_xtal,#:delay	'allow 20ms to settle
    
                        rdbyte address,#$0004		'switch to selected clock
                        clkset address
                        mov cognr, #0      ' we want cog 0
                        or cognr, interpreter
                        coginit cognr		'reboot cog 0 with interpreter
                        cogid     cognr          'number of this cog
                        cogstop cognr            'stop this cog
    shutdown                        '
                            mov	dira,#0			'cancel any outputs
    			mov	smode,#$02		'stop clock
    			clkset	smode		'suspend until reset  
    '
    ' Constants
    ' 
    time_xtal     long 20 * 20000 / 4 / 1	'20ms (@20MHz, 1 inst/loop)
    smode         long 0
    interpreter   long  ($0004 << 16) | ($F004 << 2) | %0000 ' start Spin-Interpreter
    
    '
    ' Variables
    '
    counter  res  1
    hubram  res  1
    address  res  1
    spindata res  1
    cognr     res  1
    
    'Spin-Code in 400 Hex-Bytes as saved after build 
    PRG01 byte $00,$B4,$C4,$04,$6F,$6A,$10,$00,$84,$01,$9C,$01,$20,$00,$A0,$01
    .........
    

    Now led #27 blinks 3 times and then led #26
    What a good start in the new year of Propeller-discovery ;-)
    Greetings
Sign In or Register to comment.