Menu

Table of Pointers

Help
2015-09-25
2015-09-25
  • Chris Roper

    Chris Roper - 2015-09-25

    I am trying to create a table of pointers to functions but this test code is not working.

    No errors are generated, it compiles and flashes fine, but does nothing.
    Am I attempting the impossile or do I have a syntax error ?

    ' table test
    
    #chip 18F25K50
    
    dim fnadr as word
    dir portb out
    
    Do
      for index = 1 to 4
        ReadTable test, index, fnadr
        IndCall fnadr
        wait 250 ms
      next
    Loop
    
    END
    
    sub setled1()
        portb   = 0
        portb.0 = 1
    end sub
    
    sub setled2()
        portb   = 0
        portb.1 = 1
    end sub
    
    sub setled3()
        portb   = 0
        portb.2 = 1
    end sub
    
    sub setled4()
        portb   = 0
        portb.3 = 1
    end sub
    
    table test as Word
      @setled1
      @setled2
      @setled3
      @setled4
    end table
    

    I am testing with the Microchip 28 pin demo board.
    Ultimately I need two tables of 255 entries each, the above is just a simplified test of concept.

    Any advice and help would be appreciated.

    Chrees
    Chris

     

    Last edit: Chris Roper 2015-09-25
  • Chris Roper

    Chris Roper - 2015-09-25

    I have made some progress:

    By adding:

    setled1 : wait 250 ms
    setled2 : wait 250 ms
    setled3 : wait 250 ms
    setled4 : wait 250 ms
    

    Between the 'dir portb out' and the 'do' lines the program runs.

    I would say that the table of pointers is not being populated becouse the functions are not compiled unless specificaly called.

    However the 'wait' statement is needed for some reason
    Just having:

    setled1
    setled2
    setled3
    setled4
    

    fails the same as the first test code

    What am I missing?

    Cheers
    Chris

    EDIT:
    Another test, changing the wait to 100 ms implies that the setup code is looping, dispite being out side of the loop. Eventualy it stops entierly, my guess is that the table is still not being populated correctly and that the Processor is resetting when it enters the loop.

     

    Last edit: Chris Roper 2015-09-25
  • Anobium

    Anobium - 2015-09-25

    You do not think you can have pointers in tables as you are defining them.

    Data tables can be define by (in Great Cow Basic v0.95 or greater - you will be able to use items 1, 2 and 3 in earlier versions).
    1. a single value on each line
    2. multiple elements on a single line separated by commas
    3. constants and calculations within the single line data table entries are permitted
    4. an external data source file

    The table is not being created beyond the four byte length of the table.

    So, the question is. What are you trying to achieve ? I am sure we have other methods to achieve the goal you are looking require.

     
  • Chris Roper

    Chris Roper - 2015-09-25

    I am using the v0.94 build and have tried with both multypal eliment single line and the version shown above.

    I was reading item 3 to imply that I could use the @function style as a calculation/constant.

    Here is what I am trying to achive:

    Given a Byte lookup and execute the two functions assosiated with fetching the argument and executing the opperation on the argument.

    It is a CPU emulation for educational use and the lookup tables would change between CPU types.

    table opcode2fnAdr as Word ' return pointer to opcode execution subroutine
    ' 0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  A  |  B  |  C  |  D  |  E  |  F  ;
          @ora, @nop, @slo, @nop, @ora, @asl, @slo, @php, @ora, @asl, @nop, @nop, @ora, @asl, @slo,; 0
    @bpl, @ora, @nop, @slo, @nop, @ora, @asl, @slo, @clc, @ora, @nop, @slo, @nop, @ora, @asl, @slo,; 1
    @jsr, @amd, @nop, @rla, @b1t, @amd, @rol, @rla, @plp, @amd, @rol, @nop, @b1t, @amd, @rol, @rla,; 2
    @bmi, @amd, @nop, @rla, @nop, @amd, @rol, @rla, @sec, @amd, @nop, @rla, @nop, @amd, @rol, @rla ; 3
    @rti, @eor, @nop, @sre, @nop, @eor, @lsr, @sre, @pha, @eor, @lsr, @nop, @jmp, @eor, @lsr, @sre ; 4
    @rti, @eor, @nop, @sre, @nop, @eor, @lsr, @sre, @pha, @eor, @lsr, @nop, @jmp, @eor, @lsr, @sre ; 5
    @rts, @adc, @nop, @rra, @nop, @adc, @ror, @rra, @pla, @adc, @ror, @nop, @jmp, @adc, @ror, @rra ; 6
    @bvs, @adc, @nop, @rra, @nop, @adc, @ror, @rra, @sei, @adc, @nop, @rra, @nop, @adc, @ror, @rra ; 7
    @nop, @sta, @nop, @sax, @sty, @sta, @stx, @sax, @dey, @nop, @txa, @nop, @sty, @sta, @stx, @sax ; 8
    @bcc, @sta, @nop, @nop, @sty, @sta, @stx, @sax, @tya, @sta, @txs, @nop, @nop, @sta, @nop, @nop ; 9
    @ldy, @lda, @ldx, @lax, @ldy, @lda, @ldx, @lax, @tay, @lda, @tax, @nop, @ldy, @lda, @ldx, @lax ; A
    @bcs, @lda, @nop, @lax, @ldy, @lda, @ldx, @lax, @clv, @lda, @tsx, @lax, @ldy, @lda, @ldx, @lax ; B
    @cpy, @cmp, @nop, @dcp, @cpy, @cmp, @dec, @dcp, @iny, @cmp, @dex, @nop, @cpy, @cmp, @dec, @dcp ; C
    @bne, @cmp, @nop, @dcp, @nop, @cmp, @dec, @dcp, @cld, @cmp, @nop, @dcp, @nop, @cmp, @dec, @dcp ; D
    @cpx, @sbc, @nop, @isb, @cpx, @sbc, @inc, @isb, @inx, @sbc, @nop, @sbc, @cpx, @sbc, @inc, @isb ; E
    @beq, @sbc, @nop, @isb, @nop, @sbc, @inc, @isb, @sed, @sbc, @nop, @isb, @nop, @sbc, @inc, @isb ; F
    end table
    
    table opcode2AdMod as Word ' return pointer to addressing mode subroutine
    ' 0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  A  |  B  |  C  |  D  |  E  |  F  ;
          @idx, @imp, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @acc, @imm, @aba, @aba, @aba, @aba ; 0
    @rel, @dxy, @imp, @dxy, @zpx, @zpx, @zpx, @zpx, @imp, @aby, @imp, @aby, @abx, @abx, @abx, @abx ; 1
    @abo, @idx, @imp, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @acc, @imm, @abo, @abo, @abo, @abo ; 2
    @rel, @idy, @imp, @idy, @zpx, @zpx, @zpx, @zpx, @imp, @aby, @imp, @aby, @abx, @abx, @abx, @abx ; 3
    @imp, @idx, @imp, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @acc, @imm, @abo, @abo, @abo, @abo ; 4
    @rel, @idy, @imp, @idy, @zpx, @zpx, @zpx, @zpx, @imp, @aby, @imp, @aby, @abx, @abx, @abx, @abx ; 5
    @imp, @idx, @imp, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @acc, @imm, @ind, @abo, @abo, @abo ; 6
    @rel, @idy, @imp, @idy, @zpx, @zpx, @zpx, @zpx, @imp, @aby, @imp, @aby, @abx, @abx, @abx, @abx ; 7
    @imm, @idx, @imm, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @imp, @imm, @abo, @abo, @abo, @abo ; 8
    @rel, @idy, @imp, @idy, @zpx, @zpx, @zpy, @zpy, @imp, @aby, @imp, @aby, @abx, @abx, @aby, @aby ; 9
    @imm, @idx, @imm, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @imp, @imm, @abo, @abo, @abo, @abo ; A
    @rel, @idy, @imp, @idy, @zpx, @zpx, @zpy, @zpy, @imp, @aby, @imp, @aby, @abx, @abx, @aby, @aby ; B
    @imm, @idx, @imm, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @imp, @imm, @abo, @abo, @abo, @abo ; C
    @rel, @dxy, @imp, @dxy, @zpx, @zpx, @zpx, @zpx, @imp, @aby, @imp, @aby, @abx, @abx, @abx, @abx ; D
    @imm, @idx, @imm, @idx, @zpg, @zpg, @zpg, @zpg, @imp, @imm, @imp, @imm, @abo, @abo, @abo, @abo ; E
    @rel, @dxy, @imp, @dxy, @zpx, @zpx, @zpx, @zpx, @imp, @aby, @imp, @aby, @abx, @abx, @abx, @abx ; F
    end table
    
    ' Fetch Decode and Execute opcode
    
    exeopcode(PC)
    
    dim AdMod as word
    dim FnAdr as word
    dim opArg as word
    
    sub exeopcode(adr as word)
       ' Obtain Operation Argument
      ReadTable opcode2AdMod, opcode, AdMod
      IndCall AdMod
      ' Execute Operation
      ReadTable opcode2FnAdr, opcode, FnAdr
      IndCall FnAdr
    end sub
    

    To attempt that dual lookup with a Select Case or If Then statements would be too slow and verbose.

    Cheers
    Chris

     

    Last edit: Chris Roper 2015-09-25
  • Anobium

    Anobium - 2015-09-25

    Thank you - this helps me understand.

    Leave this for a day or two and check back. But, you may be able to use one of the new features of v0.95 of Great Cow Basic to do this. Need to talk to Hugh to get his view.

    Please hold in there.

     
  • Chris Roper

    Chris Roper - 2015-09-25

    Thanks Evan,

    Much appreciated
    Cheers
    Chris

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.