Lost in C?
LoopyByteloose
Posts: 12,537
I am not really lost in C, but I have found that Tachyon Forth and PropForth really do help with exploring the Propeller. Being interactive, one can actually confirm that cogs are up and running independently of each other. Also, it is a lot easier to tweak motor performance until you have found the right timing. Then you can transfer that info to a C application.
For the Propeller2, I think Forth will be my first choice. And after exploring in Forth, then C.
http://www.forth.org/lost-at-c.html
For the Propeller2, I think Forth will be my first choice. And after exploring in Forth, then C.
http://www.forth.org/lost-at-c.html
Comments
Braino and I have been pondering how to even start on a FFT in Forth for an eternity now. We pretty much concluded that the only way to do it is to throw out all commonly held ideas of good programming practice:)
Perhaps your article will help.
Brian will have to add this to his collection.
I guess since our Mayan calendars have all rolled over successfully, then you're starting on your second eternity! How cool is that???
Can you explain how that works, I don't see it fetching the value of ALPHA anywhere, looks more like it's adding one two the address of ALPHA and writing that out somewhere.
Edit: Na, scratch all the above, +! seems to do what we want.
Actually, the advice is to start with something easier than an algorithm I don't understand, (some folks have mentioned that FFT is a little involved), that way I could help you and to do some of the simple stuff you need to learn before tackling the complex stuff you want to do. If starting at the finish line, and painfully working your way backward to the beginning, (and back again a couple of times) is good programming practice, then YES you should definitely throw that away. I advise doing it the easy way, as the hard way is proving to be just that.
Since your recursion method has been shown to need a stack that is slightly larger than propforth is configure to accommodate, you should either
a) use a non recursive method using propforth, or
b) ask Peter to make a version of Tachyon configured to has enough room (10 entries of 4 elements of 32 bits each, I believe), plus one, so 41 elements minimum; or
c) reconfigure the propforth kernel to have enough room.
Option A would be easiest, but I imagine you will prefer C since it is almost totally impossible for a new comer, and it will give you evidence that forth is unusable.
So aside from the obvious, yes, I am completely stumped how to proceed.
Traditionally, Forth has been much better with integer maths and floating point has been done with add-on links. At least that is what I have read. The main attraction I have to is is that it is a great way to work interactively.
Fast Fourier Transforms provide useful and fast tools to microcontrollers that are working with frequency and spectrum analysis. If you really need them, C will provide the best on the Propeller.
It was my intention to just mention that Forth provides a way to poke around and do a lot of discovery in real time. It was never my intention to claim it would replace C.
And I do understand that the stack sizes are limited when compared to Forth on a PC.
+! is definitely simpler at ++ presumes you already fetched ALPHA's value to the stack, so it presumes there is a fetch instruction within ALPHA.
There is no floating point in the FFT that I am trying to implement.
My problems in making this work in Forth go much deeper than that.
Hi Loopy,
This article starts out with some rather astonishing claims in the first paragraph ...
This series of claims is fairly easy to refute. Indeed, the article goes on to contradict itself a bit later when it compares Forth to C ...
It is actually quite hard to do a comprehensive comparison of Forth with C - as Heater is discovering, the languages are just so different that implementing the same non-trivial algorithm in Forth and C is difficult. But it has been done ... here ... and the results are indeed that Forth programs do tend to be both slower and larger than C. And since C itself is (in general) both slower and larger than assembly language, the claim about Forth being as fast and compact as assembly is obvious nonsense.
Note that I am not criticizing Forth itself - learn it by all means! I would even agree that the Propeller architecture is more suited to Forth than it is to C. Just don't get too excited by the hype sometimes promulgated by adherents of various fringe languages.
Ross.
P.S. As you know, I'm a fan of C, but even I had to laugh at the "legibility" claim! Only a Forth programmer would think that the "legibility of C" was a thing to aspire to!
EDIT: If someone really wants to convert the C FFT to Forth I suggest creating global variables for all of the local/global variables used in the C version, and convert it from C to Forth on a line-by-line basis. Once you get this working, then you can move inner loops and other chunks of code into small Forth words that only use stack variables. Eventually you should be able to eliminate most of the global variables that were local variables in the original C code.
In other words "Get it working then optimize"; just got the nichrome wire glowing 1320F +- 5 degrees for the igniter system on my project. I was able to interactively tune the system from a TACHYON FORTH console. Nice, now I'll condense my debugged\working\sloppy FORTH code into a dictionary word called : IGNITE ( on/off -- ) ..... ;
If I needed "good" FFT on a prop I would be looking for some more silicon I think.
If there are problems implementing it, then it's a design flaw with how a particular language was implemented on a particular processor or that the language itself isn't capable of expressing even moderately complex algorithms and data structures without severe mangling.
Again I have to say, ignore the fact that this is an FFT. I am not looking to use an FFT in Forth. It's just an example of a bunch of calculations and logic typical of any program you might write.
Someone mentioned "good FFT". If it runs and produces the correct results it is "good". I have no delusions about making it fast or handle huge sample sets. It's just an exercise in learning Forth. Except in Forth.
The stack is not something I ever have to think about much unless I'm working in assembler, even there it's not always necessary, most propeller PASM programs do not use a stack at all.
Forth is a stack based language. But actually using the stack is really hard. There is no easy way to get at the parameters of a word if there are more than two or three of them. There is no easy way to get at data sitting on the stack that a word might want to manipulate. It's so hard in fact that more than one person has suggested giving up any pretense at good programming practice and just use a bunch of global variables.
I think this is the most surprising thing I have learned on my Forth travels so far. The normal FFT doe snot use stack much, it can be done without one. The recursive FFT is a much simpler piece of code and uses only about 40 stack slots for a 1024 point transform, hardly demanding. The fact that Forth is so bad at supporting this recursive approach (see stack issues above) is shocking given that it's stack based approach is often touted as a "good thing".
Still, I'm putting by best foot Forth until this works...
Forth really isn't a very good language to develop in. Forth code constantly thrashes around with the stack, which is very inefficient. Compiled C code will keep everything in registers and is more efficient than Forth. Forth interpreters have very little error-checking capability. C compilers have evolved to the point where they will warn you about common programming errors, and it's much easier to write error-free code in C than in Forth. The RPN nature of Forth means that almost everything you want to write in Forth has to be translate from a natural language form to RPN. It's much easier to translate a mathematical formula into C code.
On the Prop, I've programmed in Spin, PASM, PropBasic, C and Forth. Forth would be my last choice for any serious programming task.
I find Forth is easier than C because it is interactive. One gets immediate response, sometimes immediate results. With C you have a 'write-compile-load' cycle that is a bit slower.
A second reason is Forth immediately has a terminal up and running for bench work. If I have new components I want to verify, I can explore with Forth in a more direct fashion.
I don't expect Forth to be everything C is, but having an interpreted language with more than mere basic functions does allow people to explore what the Propeller is in a difference fashion.
I am not sure that the absence of error checking is really a serious default. In Forth, you learn that things don't work and just learn to avoid repeating the errors. In C, you get errors and warning messages that are rather cryptic and presume you understand quite of bit of jargon and conceptual information about compilers. The fact that the hardware is so different seems to be better supported by have more than one language solution to confirm what the hardware does.
RPN is not a difficulty for me. I have used RPN calculators for decades and loved the HP-41CX. Arguing that it isn't 'natural language' is rather dubious as no computer language is natural in any real sense.
~~~~~~~~~
I am just trying to say that Forth is a useful and valuable software language for the Propellers. I think the same of C. And I believe that really learning programming requires having some experience with contrasting languages. People that just hang onto one may never see obvious alternatives.
I always find it fascinating to see things from the viewpoint of ones who find C easier. A mention was made about the RPN nature of Forth (as if RPN is a bad thing), and that you would have to translate from a natural language to this, however no direct comparison was made with C in this regard although it was touted that it's easier to translate a mathematical formula (not very natural) into C code. Agreed, it's easier to enter a mathematical formula into C which then compiles that into something the machine may understand, usually assembler and but mostly calls to binary blobs of proprietary C libraries. On some processors the machine language is Forth so all that C ends up doing is compiling C source code into Forth code, and so the same can be done with Forth on other processors too. That's one way to look at it.
On the subject of natural language I would not have ever considered C to be anywhere near that mark and yet that odd beast Forth can be tamed to behave very naturally. Of course in the wrong hands it's just as likely to claw your eyes out and become "write-only-code" which unfortunately is also true. But once you tame it, or perhaps it tames you, then things work very nicely. As for peeking and poking, please don't, that makes me cringe, whereas in Forth I can turn all that into natural interactive words like 300 RPM or SLOW SPEED for instance.
When it comes to stack thrashing, I don't believe in thrashing myself, it's not necessary and what's a register but a glorified global variable anyway? Forth has very little error checking because there is no set way that you have to write code, you are not locked into a rigid syntax and rigid types and rigid libraries.
Seriously, Forth is my first choice for serious programming
Now, how do we turn these little sessions we have into something productive? Perhaps we could add a good front-end onto Forth that compiles mathematical formulae into Forth "machine code"? That's a thought.
People can get by without error checking on small projects, but it's a necessity on large projects where you must interface to code written by other people. I can't imagine writing code for a large project in a language that didn't use data types and error checking. That's one of my big concerns about programming in Spin.
I've spent enough time programming in Forth to know it strengths and weaknesses. I like the ability to define new words, but I don't seem much difference between that and defining functions in other languages. The interactive thing is useful, and I have done similar things in other languages by writing small monitor programs. Most Forth proponents claim that the ability to test code is one of the strengths of Forth. However, I think that practice promotes sloppy programming. I think a more methodical approach is better, especially for large projects. The interactive technique is useful when you don't understand how the system works, and you have to hack at it to learn how it works.
Except of course that first version is not Forth in Forth, you have to write:
Which of course is insane.
It's insane because Forth has no variables that you can refer to by name only memory addresses (a, c, d, e) that you can operate on (@ and !)
Except of course having a bunch of global variables is not nice programming practice. There are no local variables and if there were they are frowned on as "bad practice". So those variables can only be on the stack, as far as I am aware, when we come to perform that expression. Say we have the values of a, b, c, d on the stack the expression can evaluated, leaving the result on the stack, like so: Which of course is insane.
It's insane because now I don't have any names for my data at all and I have to start introducing a bunch of operations on a stack that are not part of my simple problem to begin with.
I must admit that last example is not how a Forther would do it, I leave that as an exercise to the reader, but it illustrates my point.
Conclusion: RPN is not an issue in itself, the way Forth implements it is causing the problems.
Forths interactivity may be nice, but given the speed at which you can do much the same in Spin, just hit F9, it's not much of a gain.
I don't understand te "binary blobs of proprietary C libraries" thing. Those blobs start out as C source like anything else. There is nothing "proprietary" about the libraries used in GCC, Catalina or many other compilers.
In assembly a good macro facility is a way to manage complexity and even create local variables. I figure the Forth aficionados must have mastered something similar, but I don't know what it is. As an aside, I ran PropForth in the Gears emulator and it appeared to work, but I couldn't connect a terminal to it to find out!
Update: most C complies will print their assembly output interspersed with the source code. As Heater says, they usually nail the instructions that match the source construct because that is C design center.
I look into it every once in a while and always give up on it quickly.
never see a register so how could it be?
A global variable may well be stored in some memory space reserved for data as in C. Or it could be on a dynamically allocated heap as in JavaScript. Potentially it could be pushed onto a stack at start up. Global variables are all about scope, visibility, and lifetime of a variables in a program not about how that is implemented by the compiler.
Forth is not C, the harder you try to force one to be the other, the more trouble you have. I may have mentioned this before, sorry to keep repeating myself.
We must face the fact that certain individuals may find it impossible to work in certain mediums, and some folks may never master some tools.
I ported lots of code in C where I only had a description of the algorithm, but didn't fully understand how the algorithm work. This is true of almost every CRC/error-correcting code that I've developed in C, and I've written quite a few of them. I've never fully understand the math behind these algorithms, but I've been able to write C code that implements them effficiently.
Forth advocates need to accept the fact that it makes sense to them only because their brains are wired that way. For most of use, our brains are wired differently, and we are more efficient using a compiler that generates efficient code for us. We don't have to worry about stacks and all the bookkeeping that's required to keep track of variables on the stack. Modern C compilers have made programming much more efficient. Unfortunately, the same can't be said for Forth. It's still as primitive as it was when Chuck Moore came up with it.
Duane J
I don't :-)
Don't get me wrong I have nothing against forth but it's just not a language for me. For what it's worth neither are cobol or python (nothing against cobol or python either). I have to admit I'm getting tired of the bickering about <insert programming language of your choice> VS <a language you don't care for>.
Can we please stop the language proselytizing. Use whatever language that you like!!