Recent

Author Topic: Running a minimal fpc on a microcontroller  (Read 8728 times)

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Running a minimal fpc on a microcontroller
« on: May 28, 2019, 12:26:58 am »
The fpc specifications require at least 8 MB of memory, and 80 MB of storage. Would it be possible to make a very minimal build that would run on an ARM Cortex microcontroller? Say, at most 256 kB of memory and 1 MB of storage. How would that look like? An old build?

If that is not possible, what else could be done?

It wouldn't have an OS, so what other requirements (memory management, libraries and such) would there be?


The goal is to be able to compile and link small (Object) Pascal units and programs, at most a few kilobytes each. A subset is fine, objects would be nice. A bit like LUA.

If possible, is there some documentation somewhere, or do I download the source and try to figure it out myself?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Running a minimal fpc on a microcontroller
« Reply #1 on: May 28, 2019, 04:14:42 am »
I think those values are very old (like FPC 1.0.x and dos).  You can simply start debugging a typical job and try to avoid and minimize buffers.

But 256kb is awfully little, and you probably also need to run AS and LD after. Doesn't seem easy to me.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: Running a minimal fpc on a microcontroller
« Reply #2 on: May 28, 2019, 08:35:05 am »
The compiler binary itself is already 3MB. The compiler is not designed for minimal size, but for maximal maintainability and portability. This means it contains lots of abstractions, which is bad both for code size and memory use.

I don't think it's possible to adapt the compiler to fit in the constraints you mention.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Running a minimal fpc on a microcontroller
« Reply #3 on: May 28, 2019, 10:00:13 am »
Would it be possible to make a very minimal build that would run on an ARM Cortex microcontroller? Say, at most 256 kB of memory and 1 MB of storage.
Running FPC on 256Kb/1Mb is not a realistic request. Since I don't think cross compilation is what you're after, maybe building your own pascal compiler or interpreter could satisfy you? You might even try to get away with just adapting and cross compiling PascalScript or some other simpler Pascal interpreter and then building around it. Lot's of work, but result would be similar to existing MicroPython or Lua...
« Last Edit: May 28, 2019, 10:02:01 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

mischi

  • Full Member
  • ***
  • Posts: 170
Re: Running a minimal fpc on a microcontroller
« Reply #4 on: May 28, 2019, 01:47:00 pm »
Maybe the pico pascal compiler (https://github.com/paulherman/ppc) can help you.

If you could get along with old style iso pascal without objects, the p5 pascal compiler might do a job.

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Running a minimal fpc on a microcontroller
« Reply #5 on: May 28, 2019, 05:46:35 pm »
A tough challenge.  Some ideas:
  • remove compiler messages and help (~ 80kB)
  • drop debug info support (this will probably shave off a few kB)
  • remove unicode support in compiler[\li]
    • remove multiple dialect support (macpas/fpc/delphi/objfpc/iso) and only support a single dialect (probably complicated, I think these dialect checks are sprinkled over several units)
    I'll speculate that this will drop the compiler size closer to 2 MB in size.

    In embedded you have to provide build-in file system support of some kind and memory management. At least there is a lightweight heap manager in RTL.  Then (as marcov mentioned) you need binutlis such an assembler and linker.  Not sure this is available, so one may have to include an embedded linker in the compiler.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Running a minimal fpc on a microcontroller
« Reply #6 on: May 28, 2019, 08:39:39 pm »
If you could get along with old style iso pascal without objects, the p5 pascal compiler might do a job.

A p5 compiler that can be compiled by FPC is available from https://github.com/tangentstorm/pascal/tree/master/p5.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Running a minimal fpc on a microcontroller
« Reply #7 on: May 28, 2019, 08:49:32 pm »
8 MB RAM is too small for modern compilers. Maybe it's better to use some other small Pascal compilers or create one new. It will depends on what features you want to support in the compiler. OOP compilers are complex but classic Pascal compilers are smaller.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Running a minimal fpc on a microcontroller
« Reply #8 on: May 28, 2019, 09:08:44 pm »
(I can remember my first attempt at porting pre 1.0 FPC to FreeBSD. The year was 1998, and the idea was to just make cycle with a target linux, but then on a FreeBSD system). The non production FreeBSD system was an old 486 with 8MB, and a 32MB swap.

After 6 hours compiling it bombed out with a linking error, sigh. Slow porting it is :)

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Running a minimal fpc on a microcontroller
« Reply #10 on: May 29, 2019, 09:44:29 am »
Thanks for the suggestions. I'll go through them.

Why would the fpc.exe be 3 MB? On my PC it's only 131 kB. Symbols?

I want to try and make an "interactive" microcontroller, so I can have a dashboard and change functions while it is running. It isn't meant as a replacement for a PC, like a Raspberry Pi, (all communication through USB / Ethernet), but as an interactive way to build your circuit.

The most common MCU's have 64 kB of Flash and 20 kB of RAM. That's not much. The largest practical MCU's are like this: https://uk.farnell.com/stmicroelectronics/nucleo-l432kc/dev-board-nucleo-32/dp/2580786. It has 256 kB of Flash and 64 kB of RAM.

There are ARM  MCU's with 2 MB Flash and 1 MB RAM on-board, but they are around 4 cm2 in size and have 100+ pins, so they're hard to fit on a bread-board. Adding external Flash and RAM just to get the compiler running would be counter-productive and make it reasonably expensive.

My options for programming would be:

1. External compiler. Possible, but not interactive enough. A dashboard is like a debugger running on the device.

2. Assembler. Doable, but too low-level.

3. Interpreter: Slow but doable. But Pascal, no Basic or whatever. It would mess up your timing and be unusable for handling interrupts. Meh.

4. Tiny compiler, with the code split in units / objects and interactive debugging. Actually, something like Turbo Pascal 1.0 would work.

Optimally, the program is stored in P-code, for example like a ZX_Spectrum did it. It might be interesting to see if I could make a parse tree editor. And units / objects are loaded and linked in memory on demand. Objects would simplify memory management as well. Ok, that's a lot of work. But with a lot of re-use probably doable.


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Running a minimal fpc on a microcontroller
« Reply #11 on: May 29, 2019, 10:03:40 am »
Why would the fpc.exe be 3 MB? On my PC it's only 131 kB. Symbols?
fpc(.exe) is not the compiler, it does some checks and set-up, and calls e.g. ppcx64 which is the "real" compiler.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Running a minimal fpc on a microcontroller
« Reply #12 on: May 29, 2019, 11:22:45 am »
Ah, thanks. That size was the first thing I checked. It looked small, but not impossible for a console application. I should have looked better.

I'll check all the suggestions first.


SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Running a minimal fpc on a microcontroller
« Reply #13 on: May 29, 2019, 04:08:09 pm »
That P-5 compiler documentation was interesting.

After reading the above and thinking about it, it seems to me, that the goal is to store the program as compact as possible, to minimize overhead. And if I do that by analyzing it, the parsing is already done.

Store everything that is not a constant,  variable or set / array / list as a record (minimalist object), try to simplify expressions as much as possible while retaining object names and use a JIT for code generation.

Most everything else is either operating system or user interface. And I think it's probably simpler to just write everything myself, than to try and reuse as much existing code as possible. Although the source code of an ARM assembler would be useful. For the OS I'm going to look at RTOS, but probably I'll do that myself as well.

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Running a minimal fpc on a microcontroller
« Reply #14 on: May 30, 2019, 08:29:43 pm »
I was trying doing something similar but running in a MOS6502. The idea is to make a compiler/interpreter running in a Operative System writen using the same compiler.

Currently I'm improving my 6502 compiler and I'm experimenting building a simple bootstraping compiler.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018