Shop OBEX P1 Docs P2 Docs Learn Events
Propeller and Linux, PASM — Parallax Forums

Propeller and Linux, PASM

homosapienhomosapien Posts: 147
edited 2017-07-26 12:17 in Propeller 1
Have switched most of my computers to Linux Mint, and have been using SimpleIDE to program in C on the prop.

I have an upcoming project that will require close-tolerance timing of outputs, which I would usually turn to a mix of SPIN and PASM for, but have lost that ability with the latest version of SimpleIDE, which as far as I can see only allows C.

I have poked around a bit looking for information, much seems old and changing over time. I see an old thread (http://forums.parallax.com/discussion/161455/tool-for-spin-on-linux) which details using OpenSpin and pi-propeller-load. Is this still the way to use SPIN from Linux ?

Or maybe a better way is to use PASM with C. Any instructions on how to do that within the SimpleIDE? How does one do In-line vs COG ? Is there any difference?

Also, I have kind of been ignoring the different memory models used when making a C program - any links to current documentation on how the different models work (LMM, CMM,COG etc) and what the heck is the fcashe and how does it come into play?

Sorry for what may seem to be a bucket of confused questions, love the prop and the 8 cores, no so much the development arena if not coming from Windows...


Nate

Comments

  • frank freedmanfrank freedman Posts: 1,983
    edited 2017-07-25 22:03
    I still use BST for spin/pasm under Debian stretch then SID. If using just spin and pasm, BST still is the easiest way for me. Checkout the book sanandak has out, I am about to get into the last section on C/pasm. scanning ahead, it looks like he gets in the memory models and also inline pasm. Don't know if it will help you or not, but working through his examples, I am using gvim for the editor and command line compile and loader.
  • Propeller and Linux

    Finally someone speaks my language :)

    I only jest. We have lots of other Linux users here, they're simply not the ones that usually ask the questions (they're the ones answering them).

    You do certainly have some options for Propeller and Linux. For one, you may well enjoy PropellerIDE which is a Parallax-supported (ish), cross-platform, Spin + Basic development environment. BST as Frank suggests is another good (but much older) solution that looks almost exactly like The Propeller Tool. There's also OpenSpin if you just prefer to compile your Spin files from the command-line. But I won't go into any further details on the Spin/Basic/Forth/etc world, because that's foreign domain for me.

    I live in the C++ world of the Propeller and (on Linux) it is a very joyous (and lonely) one. In the hopes of making it less lonely, I created PropWare. PropWare gives you a build system to help you compile your code and run it on your Propeller, using the CMake build system. Along with the build system you also get easy access to a suite of libraries: Simple, libpropeller, libArduino, PropWare's own namespace, and (as of quite recently) an efficient JSON library as well. It's all documented on PropWare's website and in relatively active development, so please let me know what you needs the most work and I'll focus my attention there. That being said, it's also getting to be reasonably mature, with some of the objects like SPI and the SD card/FAT filesystem reaching 4 years old.

    Even if you don't end up using PropWare's libraries or its build system (you can use either one without the other btw), you will certainly find LOTS of helpful code snippets in it and its dependent libraries. Inline assembly, cogc, fcache and others are demonstrated in various ways.

    Speaking of fcache, I'll find you some good links to read on that and the memory models in the next post.
  • See this page for a detailed explanation of the different memory models: https://github.com/parallaxinc/propgcc-docs/blob/master/doc/Memory.md
    Fcache is explained at the bottom of that page, here: https://github.com/parallaxinc/propgcc-docs/blob/master/doc/Memory.md#miscellaneous-functions

    When using a PropWare-based application, fcache is a bit easier to use thanks to some macros defined in PropWare.h. Here's an example:
    /**
     * @brief       Shift out one word of data (FCache function)
     *
     * @param[in]   data        A fully configured, ready-to-go, data word
     * @param[in]   bits        Number of shiftable bits in the data word
     * @param[in]   bitCycles   Delay between each bit; Unit is clock cycles
     * @param[in]   txMask      Pin mask of the TX pin
     */
    inline void shift_out_data (uint32_t data, uint32_t bits, const uint32_t bitCycles, const uint32_t txMask) const {
        volatile uint32_t waitCycles = bitCycles;
        __asm__ volatile (
                FC_START("ShiftOutDataStart%=", "ShiftOutDataEnd%=")
                "        add %[_waitCycles], CNT                                           \n\t"
    
                "loop%=:                                                                   \n\t"
                "        waitcnt %[_waitCycles], %[_bitCycles]                             \n\t"
                "        shr %[_data],#1 wc                                                \n\t"
                "        muxc outa, %[_mask]                                               \n\t"
                "        djnz %[_bits], #" FC_ADDR("loop%=", "ShiftOutDataStart%=") "      \n\t"
                FC_END("ShiftOutDataEnd%=")
        : [_data] "+r"(data),
        [_waitCycles] "+r"(waitCycles),
        [_bits] "+r"(bits)
        : [_mask] "r"(txMask),
        [_bitCycles] "r"(bitCycles));
    }
    

    FC_START, FC_END, and FC_ADDR help to deal with the fact that addresses are a bit wonky in fcache, but you can see their definitions here. You can also see example of fcache without PropWare's macros in libpropeller's I2C objects.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2017-07-26 00:16
    COG: Code is compiled to PASM, and must fit in a cog. Good for drivers or very small things.

    LMM: Large Memory Model - The code is compiled as PASM with a few small differences. It is executed by a small "interpreter" that handles fetching the next instruction, executing it, and dealing with branches. The interpreter is incredibly thin, so this is almost a real PASM instruction every 16 clocks.

    CMM: Compressed Memory Model - The code is compiled to a set of instruction tokens that are smaller than 32 bits, much like Spin, but the compiler includes Gcc's optimizer, and the interpreter is about 2x faster than Spin or so.

    XMM: eXtended Memory Model - This uses an SD card to hold code larger than the 32kb Prop memory. Code is compiled like LMM, but the interpreter includes code to pull pages on demand from the SD card, and needs an SD driver cog, so it's considerably slower, and has more overhead than straight LMM, but overcomes the 32kb limit of the Prop. Not used often.

    XMM, LMM and CMM, depending on the compiler options, can benefit from the use of the FCache, which is a small area of cog ram set aside for raw PASM code, like inner loops, that benefit from running without interpreter overhead. I *think* the FCache is larger on LMM code because it has a smaller interpreter, but I may be wrong there - hopefully @ERSmith or someone who knows more will correct this if I'm off here. Code compiled for the FCache uses real PASM, so it's 32-bits per instruction, unlike CMM, so CMM set to "optimize for size" won't use this, but "optimize for speed" will, where possible.
  • JasonDorie wrote: »
    XMM: eXtended Memory Model - This uses an SD card to hold code larger than the 32kb Prop memory. Code is compiled like LMM, but the interpreter includes code to pull pages on demand from the SD card, and needs an SD driver cog, so it's considerably slower, and has more overhead than straight LMM, but overcomes the 32kb limit of the Prop. Not used often.
    XMM does not primarily us an SD card for external storage. It works best with a SPI or SQI flash chip but can also use any kind of external memory if you write an external memory driver for it. We developed drivers for SPI/SQI flash and SRAM as well as EEPROM and SD cards. However, you're correct that it is seldom if ever used. In fact, Parallax has removed support for it from the most recent release of SimpleIDE. It is, of course, still available through the command line GCC tools.
  • homosapienhomosapien Posts: 147
    edited 2017-07-26 12:45
    Thanks guys for all the helpful information, the 'secret' manual to program the prop with C/PASM, looks like I have a lot of reading to do.

    I think I will read the 'Propeller Programming' book recommended by Frank, and then be back with more questions.

    DavidZ, The PropWare looks great, may end up going with that eventually, I do like the simplicity of the command line for many operations in Linux. I am also going to be spending a lot of time reading all the links your information has unearthed. First quick question - from my first perusal, it looks like the fcashe is simply a way to use PASM in-line, is that correct?

    It also looks like one can use PASM in it's own cog simply by using SimpleIDE and appending a '.cog' (or something like that, have lost the area where I read this...) on the name of the file. It will probably be explained in the 'Propeller Programming' book, but does anyone have a quick description of how to exchange data between the PASM cog and C program running in another cog (use malloc() and somehow pass that address)? How to start and stop the PASM cog?

    Thanks again,
    Nate
  • DavidZemonDavidZemon Posts: 2,973
    edited 2017-07-26 13:51
    homosapien wrote: »
    DavidZ, The PropWare looks great, may end up going with that eventually, I do like the simplicity of the command line for many operations in Linux. I am also going to be spending a lot of time reading all the links your information has unearthed. First quick question - from my first perusal, it looks like the fcashe is simply a way to use PASM in-line, is that correct?

    For all intents and purposes, yes. I do think it's important to remember that fcache has to first copy your entire block of code from HUB to COG RAM and then execute it. I think it's important to remember that bit of overhead so you don't overuse it for simple 2 or 3 instruction routines.
    homosapien wrote: »
    It also looks like one can use PASM in it's own cog simply by using SimpleIDE and appending a '.cog' (or something like that, have lost the area where I read this...) on the name of the file. It will probably be explained in the 'Propeller Programming' book, but does anyone have a quick description of how to exchange data between the PASM cog and C program running in another cog (use malloc() and somehow pass that address)? How to start and stop the PASM cog?

    Thanks again,
    Nate

    SimpleIDE isn't the only special snowflake that can do .cog objects :). PropWare's build system supports both cogc and cogcpp. You have the general idea though: compile two source files (main and a driver) and main can be compact using one of the many memory models available where as your driver can be fast (compiled directly to PASM). Once the driver is compiled, there is some crazy black magic that creates an accessible symbol which the Propeller's built-in coginit instruction can work with. You then provide the coginit instruction with that symbol and a "mailbox" variable, just as you would in Spin. You have to do quite complex dance to get this to work, so I'm not really a big fan of it... but it's there if you want it. Personally, I find inline assembly (often combined with fcache) to be a more worthwhile solution.

    Okay, so how do you start code in a new cog? You have options:

    PropGCC's raw cognew() and coginit()
    cognew(code, param) is simply a light wrapper around coginit(0x08, code, param). This should look very familiar for the avid PASM programmer. Generally not advisable for cogs running "high level" C or C++ code because that code will need access to a stack... a stack which doesn't exist for that cog. These are primarily useful for kicking off cogc/cogcpp or raw assembly drivers

    PropGCC's cogstart()
    cogstart(func, par, stack, stacksize) is far more useful for running high-level code in another cog. This method will takes a function pointer as the first parameter and will start up a new cog and give it stack space per the third and fourth parameters. And of course, you have the "par" parameter for your mailbox variable as usual.

    Simple's cog_run()
    cog_run(function, stacksize) gives you an easier-to-invoke wrapper around cogstart(). This takes a function pointer and integer value for the stacksize only. No mailbox parameter! And note that this function will use malloc for allocating the stack too... so be careful about code size :)

    PropWare's PropWare::Runnable
    This class gives you (what I think is) easy and object-oriented access to running code in different cogs. Simply create a class that inherits from PropWare::Runnable and then kick it off with PropWare::Runnable::invoke(myCoolObject). An example of its use is provided in the docs.




    Whew! That's all that I can think of off the top of my head.
  • Heater.Heater. Posts: 21,230
    homosapien,
    I do like the simplicity of the command line for many operations in Linux.
    Excellent.

    In that case one can write Spin using Vim with syntax highlighting: http://forums.parallax.com/discussion/118328/spin-syntax-definition-for-vim

    For command line compilation we have OpenSpin, BST and Homespun.
    https://github.com/ZiCog/HomeSpun

    For pure PASM only programs there is Cliff Biffles PASM assembler, propasm:
    https://github.com/cbiffle/propasm

    A little searching will find a bunch of command line loaders for the Propeller.

    There is a lot of tools for the Propeller that run on Linux. Sadly they are not always as high profile as we would like.
  • Ah, there is the Heater we all know and not one mention of M$ of the the tablet which isn't and shall not be named here.........
  • Thanks again for all the clarifications, I will need a little time to sort through all this information and figure out my best course of action.

    DavidZ, I will give propWare's PropWare::Runnable a good look. I have never gotten much into C++ (only C), it may be what I am looking for. I have always been a little unimpressed with the clunky way SPIN/PASM had to use the PAR long to setup the communication pipeline - I always had confusion keeping track of bytes/words/longs and exactly which data I had stashed where.

    To clarify, my understanding is that in-line PASM is fast, but not totally deterministic, while a cog running PASM (excepting main ROM reads/writes) is. Yes?

    Heater, I switched to Linux after the Microsoft you-WILL-upgrade-to-Windows10 craziness, and have never looked back. However, I have never embraced all these wacky keyboard-shortcut editors, I pretty much hate both vi and emacs. I use xed/gedit (ducks and runs for exit -->)
  • I have been using Linux Mint for very many years and use BST (0.19.4pre10) for compiling Spin/PASM but really only PASM for my Tachyon systems. For an editor I find xed/gedit a bit limited and have used medit instead although I am trialling sublime since I managed to do syntax highlighting for Tachyon. My Prop systems are interactive so I use the serial terminal all the time and prefer minicom for this purpose since it is fully ANSI compatible and easily handles very high baud rates (I default to 921600 for TeraTerm and Bluetooth compatibility).

    The very useful thing about BST is that it produces a real listing which is important for me since I need to optimize the memory map of my Tachyon kernel.

  • Heater.Heater. Posts: 21,230
    Oddly enough I switched from Windows to Linux round about the time of Windows 98. Hardly ever used it since.

    Except a year ago I was coerced into using Win 10 on a Surface Pro. I was pleased to find that it runs all my old favorite programs from Linux world. And now has a BASH shell built in. It's only blue screened once, which happened to be yesterday in the middle of a rather important meeting. The programs it does have problems with are all Windows applications!

    Folks round here think I have gone senile since I stopped bashing on Windows so much.
  • Heater. wrote: »
    Oddly enough I switched from Windows to Linux round about the time of Windows 98. Hardly ever used it since.

    Except a year ago I was coerced into using Win 10 on a Surface Pro. I was pleased to find that it runs all my old favorite programs from Linux world. And now has a BASH shell built in. It's only blue screened once, which happened to be yesterday in the middle of a rather important meeting. The programs it does have problems with are all Windows applications!

    Folks round here think I have gone senile since I stopped bashing on Windows so much.

    The other problem I have with Windows is not so much Windows itself but the NTFS file system. The problem is when it has a problem, it can be a real problem and it seems like you have to run Windows to fix the problem. The problem is Windows doesn't want to run because it encounters a problem. You see the problem?


  • Heater.Heater. Posts: 21,230
    Problem is I have never seen that particular problem.

    I do which Windows file systems had sensible access rights. Changing permissions on a file confused git which then confuses me as a file is said to have changed when it has not. Luckily git has an option to ignore that particular Windows failing.

  • Cluso99Cluso99 Posts: 18,069
    Heater. wrote: »
    Oddly enough I switched from Windows to Linux round about the time of Windows 98. Hardly ever used it since.

    Except a year ago I was coerced into using Win 10 on a Surface Pro. I was pleased to find that it runs all my old favorite programs from Linux world. And now has a BASH shell built in. It's only blue screened once, which happened to be yesterday in the middle of a rather important meeting. The programs it does have problems with are all Windows applications!

    Folks round here think I have gone senile since I stopped bashing on Windows so much.
    But you found BASH, so aren't you BASHing on Windows now??? ;)
  • Heater.Heater. Posts: 21,230
    Weird isn't it. I am BASHing on Windows all the time now a days. While I bash on it less.

    Sometimes I have to bash on BASH on Windows as it does not work as well as BASH on Linux/Unix yet. And of course not everything you want to do with BASH is doable with BASH on Windows due to limitations of the underlying Windows OS.

  • Cluso99Cluso99 Posts: 18,069
    Heater. wrote: »
    Weird isn't it. I am BASHing on Windows all the time now a days. While I bash on it less.

    Sometimes I have to bash on BASH on Windows as it does not work as well as BASH on Linux/Unix yet. And of course not everything you want to do with BASH is doable with BASH on Windows due to limitations of the underlying Windows OS.
    I like it :):);)
  • Nate - programming the propeller in C isn't too bad - but there is a bit of a learning curve. Here is a quick intro to the Large Memory Model (where all the code is stored in HUB and each instruction is copied sequentially to cogs)

    This is from (shameless plug) my book on leanpub
    Propeller Programming. I hope that can help you get started. I also discuss cog-c and c-pasm programming in there.

    Sincerely,
    Sridhar
Sign In or Register to comment.