Shop OBEX P1 Docs P2 Docs Learn Events
From Spin1 to Spin2 — Parallax Forums

From Spin1 to Spin2

JonnyMacJonnyMac Posts: 9,160
edited 2022-04-11 16:41 in PASM2/Spin2 (P2)

I've had the opportunity to convert a lot of Spin1 to Spin2 and find myself running a bit of a routine. With more people trying the P2, I thought I'd move comments I left in another thread to make them available to a wider audience. Opinions expressed here are my own, and as ever, I appreciate your thoughtful feedback.

In most cases, moving from the P1 to the P2 doesn't involve a lot of drama. This is what I do:

  1. Use SaveAs to make a copy of the file with the .spin2 extension
  2. Add blank parenthesis to method names/calls that don't have parameters.
  3. Declare return value by name (after colon) for methods that return a value
P1: pub get_temp
P2: pub get_temp() : tempC
  1. Replace/remove P1 objects/calls (e.g., I no longer need my jm_time_80.spin object)
  2. Use F9 to compile P2 file and work through required changes

I'm taking an online Python programming course, so I though I'd write a little utility that will hunt through a Spin1 file and look for operators and keywords that need to change -- a few P1 operators will not flag a P2 compiler error, but won't work properly, either. The Spin2 compiler will tell where a problem exists, but not how to fix it. The idea behind this little program is to provide hints. This is the output from a test.

Processing d:/programming/p1_spin/jm_max7219_demo.spin

0139  t := cnt
      * (p1) cnt      @Col 6   --> (p2) getct

0142  waitcnt(t += constant(100 * MS_001))                         ' hold 1/10th second
      * (p1) waitcnt  @Col 1   --> (p2) waitct
      * (p1) constant @Col 14  --> (p2) N/A

0207  prng.seed(-cnt, -cnt ~> 2, $CAFE_BABE, cnt <- 2, cnt)         ' seed randomizer
      * (p1) ~>       @Col 22  --> (p2) sar
      * (p1) <-       @Col 44  --> (p2) rol

Done. 5 suggestions in 3 lines.

This is version 1, so if you're a Python programmer, please feel free to jump in and improve it -- again, I'm a newish Python programmer so it would be nice to see ideas from others.

Not everything is going to be an easy fix. The P2 doesn't have counters like the P1, so we'll have to use code or smart pins in P2 updates. There are no waitxxx instructions, so those situations will have to be dealt with differently. This doesn't look at any PASM instructions -- that's a detailed process, anyway.

Note: Remove ".txt" from end of filename before attempting to run.

Comments

  • I just noticed in that demo output that the program didn't detect 'cnt' in line 207. I don't know why -- and it's very late....

  • JonnyMacJonnyMac Posts: 9,160
    edited 2022-04-10 16:44

    Fixed that problem, made some code improvements, and updated the token lists. This version is nicer -- updated output:

    D:\>python p2_hints_1v2.py d:/programming/p1_spin/p1_template.spin
    Processing d:/programming/p1_spin/p1_template.spin
    
    0065  term.str(string("Hello, World!", 13, 13))
    ----           ^
          * (p1) string(    @Col 10  --> (p2) @
    
    0068  waitcnt(0)
    ----  ^
          * (p1) waitcnt    @Col 1   --> (p2) waitct
    
    0090  prng.seed(-cnt, -cnt ~> 2, $CAFE_BABE, cnt <- 2, cnt)         ' seed randomizer
    ----             ^     ^   ^                 ^   ^     ^
          * (p1) cnt        @Col 12  --> (p2) getct
          * (p1) cnt        @Col 18  --> (p2) getct
          * (p1) ~>         @Col 22  --> (p2) sar
          * (p1) cnt        @Col 40  --> (p2) getct
          * (p1) <-         @Col 44  --> (p2) rol
          * (p1) cnt        @Col 50  --> (p2) getct
    
    Done. 8 suggestions in 3 lines.
    

    See first post for updated listing.

  • JonnyMacJonnyMac Posts: 9,160
    edited 2022-04-10 17:49

    Found a gotcha and changed from using two lists to a single dictionary -- this makes adding P1->P2 change suggestions much easier.

    Output now looks like this:

    D:\>python p2_hints_1v4.py d:/programming/p1_spin/p1_template.spin
    Processing d:/programming/p1_spin/p1_template.spin
    
    0065    term.str(string("Hello, World!", 13, 13))
    ----             ^
          * Col 012  (p1) string(     (p2) @
    
    0066    time.pause(100)
    ----    ^
          * Col 003  (p1) time.pause  (p2) waitms
    
    0069      waitcnt(0)
    ----      ^
          * Col 005  (p1) waitcnt     (p2) waitct
    
    0091    prng.seed(-cnt, -cnt ~> 2, $CAFE_BABE, cnt <- 2, cnt)         ' seed randomizer
    ----               ^     ^   ^                 ^   ^     ^
          * Col 014  (p1) cnt         (p2) getct()
          * Col 020  (p1) cnt         (p2) getct()
          * Col 024  (p1) ~>          (p2) sar
          * Col 042  (p1) cnt         (p2) getct()
          * Col 046  (p1) <-          (p2) rol
          * Col 052  (p1) cnt         (p2) getct()
    
    Done. 9 suggestions in 4 lines.
    

    See first post for updated listing.

  • Very cool. Keep it up young fellow!

Sign In or Register to comment.