gnuarm.del...@gmail.com writes:
>
http://www.padauk.com.tw/upload/doc/PFS173_datasheet_v000_EN_20180816.pdf
> ... in a 14 pin SOP (I think) it is only 0.05 USD qty 100. Not bad.
> 1. 3KW MTP program space (programmable more than 1,000 times)
> 2. 256 Bytes data space...
> This MCU has a real lack of registers... Can a viable Forth be
> written for it?
Besides handling the addressing modes you also have to decide the data
cell size. It's an 8-bit MCU with very little memory, but Forth
traditionally uses data cells that are the same size as addresses and
that are big enough for typical integers, i.e. 16 bits. I remember that
some Dutch Forthers have written Forths with 8-bit data cells (that
means the data stack is 1 byte wide) but I don't know how usable those
were. Do you want 8 bit cells, or 16 bit, or something else?
> I realize code can be output by a cross-compiler and optimized for the
> instruction set. I'm wondering how painful the code would be. On the
> other hand I expect it to be horrible for any other language too.
I'd guess that the usual Forth VM using subroutines (words) with deeply
nested factoring, passing parameters on the data stack, and doing
intermediate calculations as stack operations, would take a considerable
hit in speed and maybe code density.
In C for something like this, you'd use locals freely and avoid indirect
function calls and reentrancy. An MCU-oriented C compiler would then
possibly allocate a pseudo-register for each local or intermediate
result in the program, analyze the whole program's call graph, and use
graph coloring (or whatever) to assign static memory cells to the
pseudo-registers so they'd be re-used without interfering with each
other. It would also inline functions that were called only once, etc.
You maybe still take a hit compared to writing in assembler, where you'd
probably judiciously use the accumulator for parameter and return values
in the lowest level functions, and use memory slots at the higher
levels.
It's not too hard to write Forth in a style where the compiler can
statically know the stack picture (i.e. don't use things like ?DUP).
The compiler could then do something like the above C example, but it
seems like an abstraction inversion: what do you get from it?
I have no real experience programming devices this small so don't have a
clear sense of whether carefully written assembly code is different from
what's reasonable for a compiler. But, this Padauk device has more
program and data memory than small PIC and AVR parts that are often
programmed in C. So it all seems doable in typical MCU applications
where even a significant efficiency hit won't matter much.