Shop OBEX P1 Docs P2 Docs Learn Events
TAQOZ Reloaded v2.8 - Writing Inline Assembly Language — Parallax Forums

TAQOZ Reloaded v2.8 - Writing Inline Assembly Language

bob_g4bbybob_g4bby Posts: 401
edited 2023-04-20 06:56 in Forth

Here's my notes on writing TAQOZ words using in-line assembly language. I'll up-issue as I learn more.

Comments

  • Just super Bob. I had a quick look and will have a better read of that later.

  • I took the time to read the document end to end and I can only repeat what Peter has already said. You must've prepared many, many quality docs in your career, obviously.

  • bob_g4bbybob_g4bby Posts: 401
    edited 2021-05-06 10:01

    Thanks both, I expect there's a lot that can be added, there's plenty more features in TIA. I fixed some mistakes so worth downloading before rereading.

    Very satisfying when a word assembles with no stack mismatch and then after a few more tweeks actually works. The reset button gets a lot of use as does the dump
    and stack display commands! I like the conventional syntax in TIA, so many forth assemblers use RPN which makes adopting code from other assemblers a real chore. Plus I found writing assembler in tiny chunks and testing the word out interactively in TAQOZ is fun, you get constantly rewarded with another working word you can trust. Deserves wider recognition as a tool for seriously high speed applications with fewer bugs.

  • @bob_g4bby said:
    ... and stack display commands! ....

    .... writing assembler in tiny chunks and testing the word out interactively in TAQOZ is fun, you get constantly rewarded with another working word you can trust. 
    

    Deserves wider recognition as a tool for seriously high speed applications with fewer bugs.

    You nailed it !
    That's why Peter is trying to promote the TAQOZ in so many ways but, unfortunately, the majority of folks are stuck with the conventional tools and methods (for various reasons which we know all too well). The hardest part is to start with something new but once you see how productive one can be with TAQOZ it's even harder to stop using it, especially that it includes interactive assembler too. And many more :).

  • @bob_g4bby said:
    I've just started learning how to write new TAQOZ words in assembly language instead of forth, because I have a ham radio project that will demand high speed in the signal path. Here's my notes at version 7. I'll up-issue as I learn more.

    Hi Bob, this document is really very helpful! Great!

    Perhaps you might add some lines about LOADMOD and COGMOD?
    LOADMOD ( startadr number_of_longs -- ) will copy PASM code from hub into cog ram starting at fixed address.
    COGMOD will execute any code, that has been loaded before.

    code test ( value n -- value+3*n 0 )
        add b,#3
        sub a,#1 wz
        if_nz jmp #\@COGMOD '\ loop from start
        ret
    end
    
    AT test 2+ 4 LOADMOD \ loads the asm instructions of test into cog ram
    
    10 5 COGMOD . . \ executes any loaded code
    

    The result is: 0 25
    It took me some time to find #\@COGMOD as a way to implement the loop....

  • bob_g4bbybob_g4bby Posts: 401
    edited 2021-09-12 18:24

    @Christof Eb.

    That looks useful, I will add that to both the inline code and glossary documents, this was new information to me,

    regards, Bob

  • Christof Eb.Christof Eb. Posts: 1,088
    edited 2021-09-15 19:15

    Hi can someone help, please? -solved
    I have now struggled for half a day to assemble some code with Taqoz 2.8 inline assembler and forward label reference for half a day, and cannot get it running.
    The version with the { } comment works. It fills a buffer with Sync3 adc data. The code within the comment shall bring a positive edge trigger.
    But if I delete the { }, jmp #la2 should skip the code until la2. Instead it does not work any more.
    So how can I do a forward jump?
    (setse4+waitse4 did not work too.)
    Many thanks!

    code fs3a ( d: stat c:aBuf b:len a:level -- d c b a )
        '\ setse4 a '\ from tos
        mov r1,#0 '\ clear diff1
        mov r2,#0 '\ clear diff2
    .l0 testp #adcpin wc  '\ waitse4 '\ wait for sample period done
        if_nc jmp #l0
        rdpin r0,#adcpin '\ get SINC3 accumulator
        sub r0,r1 '\ compute sample
        add r1,r0 '\ update diff1 value
        sub r0,r2 '\ compute sample
        add r2,r0 '\ update diff2 value
        shr r0,#9 '\ #7 justify 14-bit sample
        zerox r0,#17 '\ #13 trim 14-bit sample, now use sample somehow
        {
        jmp #la2
        cmp d,#2 wz     '\ stat 0: wait f minus, 1: w plus, 2: sample
        if_z jmp #la2     '\ la2
    
        cmp d,#1 wz     '\ stat 0: wait f minus, 1: w plus, 2: sample
        if_z jmp #la3     '\ la3
    
        cmp r0,a wc     '\ status is 0 - wait for minus
        if_c mov d,#1   '\ new status 1
        jmp #l0
    
    la3 cmp r0,a wc      '\ status is 1 - wait for plus
        if_nc mov d,#2   '\ new status 2   
        jmp #l0
        }
    la2 wrlong r0,c     '\ sampling write to buffer
        add c,#4        '\ next address in buffer
        djnz b,#l0  '\ loop for next period
        ret 
    end
    
    : fillSync3Adc ( -- 0 1 2 3 ) \ with trigger 18 bit 512 clocks
        setSync3
        2 \ trigger status 0 on stack: d
        aBuf \ Buffer address on stack: c
        aBufLen \ on stack: b
        adcReso 2/ \ trigger level on stack: a
        fs3a
    ;
    

    **Solved: I finally found the remark in PASM.fth, that forward references do only work for djnz!
    **

  • @bob_g4bby
    1. I think it should be helpful, if you could add some lines into your description about labels. While jumps backward do make no problems, forward jumps are only supported for djnz. jmp is not supprted. It took me several hours to find this "feature" because sometimes the forward jump seemed to work.

    At the moment the best workaround seems to be to use the skip instruction. It needs one bit set for every instruction, that needs to be skipped.

    cmp d,#2 wz    
    if_z skip #%11111111  '\ skips next 8 instructions   
    
    1. If you know the rules, which lead to or avoid "stack mismatch", please add them too. At the moment, I believe, that lines, which contain only whitespaces within PASM code, make problems.
  • I saw your article and thought the same thing - I will add the information on jumps.

  • Hi Bob,
    although I tried, I could not get
    ....
    ASM:
    ....
    FORTH:
    ....
    work. This switch back to Forth seems not to work?
    Perhaps you could check this and put it into your paper?

  • Hi Bob,
    in your passage "Taqoz temporary registers", it would be good to clarify, that some of these register names refer to the same cell.
    xx, yy=r0, zz=r1, r2, r3, r4, acc=ac
    I had some strange things going on, when I used both r1 and zz, thinking they would be independent...

  • Christof Eb.Christof Eb. Posts: 1,088
    edited 2022-12-17 11:20

    Edit: For some reason this does not work always. For a better variant see #16.

    Hi @bob_g4bby ,
    I finally found out how the switch back from assembler to Forth is intended to work and think, that I was able to patch it:

    --- switch from inline assembly back to Forth threads and continue compiling
    $31 := doNEXT \ was faulty
    
    FORTH ASSEMBLER
    pub POP,      ( #d -- )     %000101011 %110101100_0 DSCZ ;
    pub FORTH: r0 POP, \ delete return address
       PTRA doNEXT #s CALLDS, end [C] ] ;
    FORTH
    
    : testB (  --  ) \ does work
    1 \ to stack
    ." Before 1st Asm " 
    dup .
    ASM:
        mov a,#2
    FORTH:
       ."  After 1st Asm " dup .
    ASM:
        mov a,#3
    FORTH:
       ."  After 2nd Asm " .
    ;
    
    

    Perhaps you would like to test it and include the patch into your tutorial?
    Christof

  • Hi @"Christof Eb." ,
    I can't get testB to run - which is strange. I see the first print statement only Before 1st Asm 1 and then Taqoz reboots.
    I'm running KERNEL TAQOZ Forth for Parallax P2 Multicore MCU V2.10.0 'CHIP' 210812-1000 Prop_Ver G 320MHz. I downloaded _BOOT_P2.BIX from Peter Jakacki's website this morning.
    It's probably a version thing.

  • Christof Eb.Christof Eb. Posts: 1,088
    edited 2022-12-16 16:21

    Sorry Bob.
    Hm, this drives me crazy. The inline code above did work yesterday in my setup and in the morning today but not then any more. - Now it works again. Tried terminal serial transmit delay - this seems not to be the problem.
    One constellation crashs repeatable but only after 8 recompilations.
    Edit: At the moment I think it might be an alignment problem jumping from hub to cog.???

  • Christof Eb.Christof Eb. Posts: 1,088
    edited 2022-12-17 07:26

    @bob_g4bby
    Hi,
    the problem seems to be related somehow to code alignment. (?) At least I was able to influence the crashes inserting or not inserting a word before the ASM: I do not understand it. In addition I now learned that it is not a problem to overfill the hardware stack, which is implemented by a shifter.
    Do you want to try the following?

    FORTH ASSEMBLER
    pri PCNEXT  _pc @ 12 + ;
    
    pub FORTH: 
       end [C] ] ;
    FORTH
    
    
    : testD \ works
       1 \ to stack
    \   crlf
       begin
          ASM:
              add a,#1
              mov PTRA,##PCNEXT
              jmp #\$31           ' doNEXT
          FORTH:
             crlf dup .
          ASM:
              add a,#1
              mov PTRA,##PCNEXT
              jmp #\$31           ' doNEXT
          FORTH:
             crlf dup .
             1000 ms
       dup 10 > until
       drop
    ;
    
    testD
    
    

    Up to now I was not able to put the sequence

        mov PTRA,##PCNEXT
        jmp #\$31           ' doNEXT
    

    into the FORTH: macro. So this has to be added to the end of each assembler block.

  • bob_g4bbybob_g4bby Posts: 401
    edited 2022-12-17 11:36

    @"Christof Eb." that code runs and doesn't crash Taqoz, but I don't see any of the numbers printed from the "crlf dup ." words. The number 1 is left on the data stack.

  • Christof Eb.Christof Eb. Posts: 1,088
    edited 2022-12-17 11:53

    @bob_g4bby said:
    @"Christof Eb." that code runs and doesn't crash Taqoz, but I don't see any of the numbers printed from the "crlf dup ." words

    Oh, .... !
    As this relays on "doNEXT" being located at $31 it would be interesting to make sure, that there is not a version conflict here.

    TAQOZ# $31 cog@ .long --- FAE4_1F61 ok

    It would be interesting to hear, if you get that same result?
    But I don't want to steal your time, with my buggy attempts....

    Edit: My version comes from the Taqoz.zip in https://sourceforge.net/projects/tachyon-forth/files/TAQOZ/binaries/
    The good thing of this version is, that you can have the sources and docu with it.

  • I've loaded your Taqoz version and now testD works fine for me - numbers 2 to 11 are printed

  • bob_g4bbybob_g4bby Posts: 401
    edited 2022-12-26 14:17

    Peter tells me this code phrase will always produce the address of DONEXT :-

    ' CALL 8 + W@ 1- .L --- $0000_0031 ok

    See if that works for you, @"Christof Eb."
    Your example could be modified with that to:-

    FORTH ASSEMBLER
    pri PCNEXT  _pc @ 12 + ;
    ' CALL 8 + W@ 1- := doNEXT
    
    pub FORTH: 
       end [C] ] ;
    FORTH
    
    
    : testD \ works
       1 \ to stack
    \   crlf
       begin
          ASM:
              add a,#1
              mov PTRA,##PCNEXT
              jmp doNEXT           ' doNEXT
          FORTH:
             crlf dup .
          ASM:
              add a,#1
              mov PTRA,##PCNEXT
              jmp doNEXT           ' doNEXT
          FORTH:
             crlf dup .
             1000 ms
       dup 10 > until
       drop
    ;
    
    testD
    
    

    I've added your example code to 'Writing inline assembly language' - thank you very much for getting that going.

  • @bob_g4bby said:
    Peter tells me this code phrase will always produce the address of DONEXT :-

    ' CALL 8 + W@ 1- .L --- $0000_0031 ok

    Hi Bob,
    yes this works for the version from the zip too:
    Parallax P2 TAQOZ RELOADED sIDE V2.8 'CHIP' Prop_Ver G 200MHz 210401-1230
    I do not see the version from August as usable, because there is no documentation and or source for it. Up to this discussion I had thought, that these 2 versions of V2.8 would be pretty the same, but now we have learned, that Peter introduced some undocumented changements even into the very cog assembler kernel. I am really very thankful that Peter developed Taqoz, made it available and brought it to the maturity of V2.8. But on the other hand I do think, that the usability for me has improved very very much, since you wrote the documentation and since Peter stopped to change things....
    Christof

  • Hi Bob,
    today I found that at least in the version I use, there is a bug in LOADMOD: It does not drop it's 2 parameters.
    So the work around is simply: Do a "2DROP" after using LOADMOD.

       ...
       AT inLoop 2+ 38 LOADMOD 2drop \ bug in LOADMOD ==> 2drop
       COGMOD
       ...
    

    Probably worth to include this in the docu, as it takes a while to find such things....
    Christof

  • A note has been added about a possible LOADMOD bug to the Taqoz Reloaded Glossary . I see it too since I'm now using the same version as you. Many thanks

  • Hi @bob_g4bby
    Especially towards the end of cog memory it is not obvious, for what the cells are used, so I tried to make a memory map. Perhaps worth to be included. It is for the version _BOOT_P2.BIX in Taqoz.zip in https://sourceforge.net/projects/tachyon-forth/files/TAQOZ/binaries/ .

    Cheers Christof

  • @"Christof Eb." I've added the Cog RAM Map to the Inline Assembly Language document - many thanks for that - Bob

  • Hello Everyone,
    I finally got Taqoz V2.8 reloaded on my P2 Eval boards flash.
    So I wrote my first inline assembly which is a word that computes the Fibonacci numbers, I know that it is not the fastest
    one posted here but I think it is pretty good.

    HydraHacker

    code FSTFIBO ( n -- f )
           mov xx, #-1  
           mov yy, #1   
           add a, #1 
           rep #4, a 
           add xx, yy
           mov a, xx
           mov xx, yy 
           mov yy, a
           ret
    end
    ;
    
Sign In or Register to comment.