Shop OBEX P1 Docs P2 Docs Learn Events
Good way to do Jump table in PASM2? — Parallax Forums

Good way to do Jump table in PASM2?

While translating the P1 graphics.spin into P2, I ran into this:
ror     t1,#16+2                'lookup command address
                        add     t1,#jumps
                        movs    :table,t1
                        rol     t1,#2
                        shl     t1,#3
:table                  mov     t2,0
                        shr     t2,t1
                        and     t2,#$FF
                        jmp     t2                      'jump to command


jumps                   byte    0                       '0
                        byte    setup_                  '1
                        byte    color_                  '2
                        byte    width_                  '3
                        byte    plot_                   '4
                        byte    line_                   '5
                        byte    arc_                    '6
                        byte    vec_                    '7
                        byte    vecarc_                 '8
                        byte    pix_                    '9
                        byte    pixarc_                 'A
                        byte    text_                   'B
                        byte    textarc_                'C
                        byte    textmode_               'D
                        byte    fill_                   'E
                        byte    loop                    'F

Is there a good way to do this in P2?
What I did was just jump into hubexec and do a series of CMP and IF_Z JMP.
This is a lot less efficient by a large factor though....

I think there use to be a special P2 instruction for doing this a long time ago, but I think it got cut...

Comments

  • evanhevanh Posts: 15,187
    Probably JMPREL but I've not tried to get my head around it. Failing that, ALTD or ALTS works well.
  • Wuerfel_21Wuerfel_21 Posts: 4,460
    edited 2020-02-01 00:31
    I think ALTGB+GETBYTE+EXECF(edit: no, JMP would be better?Edit2: no they're both 4 cycles) should do the trick?
  • RaymanRayman Posts: 13,859
    Oh yeah, JUMPREL was the instruction I was thinking of...
    That would help a bit, but probably still not as compact as original.
    I'll think about Wuerfel_21's idea...
  • RaymanRayman Posts: 13,859
    edited 2020-02-03 21:45
    looks like Wuerfel_21 has it...
    If I'm seeing it right, can use byte jump table, just like the original.

    ALTGB D, S will select byte #D from table starting at S
    Getbyte D brings that byte into D
    JMP to that D (and not #D) takes you where you need to go.

    I'll have to try this out...

    Of course, I remember from before that this only works when your are jumping to the lower 256 longs.
    If your cog code is longer, you need to switch to a word table...
  • roglohrogloh Posts: 5,157
    edited 2020-02-04 03:32
    Does an indirect jump to the address stored in register with any upper bits set trigger a jump to a hub exec address? I am wondering if I can make use of some upper bits in my jump vector table yet still stay in COG exec mode. How many bits if any can I steal from the long for holding an extra value I can use later without an additional table lookup needed, is it up to 12?

    eg.
    tjnf reg, vector
    
    where vector contains

    (value << 24) + COG address
  • cgraceycgracey Posts: 14,133
    I think the top 12 bits (of 32) can be used for other things.
  • roglohrogloh Posts: 5,157
    edited 2020-02-04 06:36
    Great, thanks Chip. I think this should come in handy for part of my bank selection code in the HyperRAM driver to select RAM or flash pins etc.
  • RaymanRayman Posts: 13,859
    Wuerfel_21's idea works.
    Here's the new jump table code. It's actually much simpler than the original...
                            'Trying new approach to jump table (suggested by Wuerfel_21)
                            'ALTGB D, S will select byte #D from table starting at S
                            'Getbyte D brings that byte into D
                            'JMP to that D (and not #D) takes you where you need to go.
                            
                            altgb   t1,#jumps
                            getbyte t1
                            jmp     t1
                            
    
    long
    jumps                   byte    0                       '0
                            byte    setup_                  '1
                            byte    color_                  '2
                            byte    width_                  '3
                            byte    plot_                   '4
                            byte    line_                   '5
                            byte    arc_                    '6
                            byte    vec_                    '7
                            byte    vecarc_                 '8
                            byte    pix_                    '9
                            byte    pixarc_                 'A
                            byte    text_                   'B
                            byte    textarc_                'C
                            byte    textmode_               'D
                            byte    fill_                   'E
                            byte    loop                    'F
    
Sign In or Register to comment.