October 31, 2020

Mini Kyu

How big is Kyu? This is something I never worried about with target systems like the BBB or OrangePi, with 512M or 1G of ram.

I recently started thinking about whether I could put Kyu onto a Cortex-M3 ARM board. I am doing a lot of work with the STM32F103 chip, which has 128K of flash (on some versions) and 20K of ram.

So I took a look at a recent build of Kyu for the Orange Pi. The code is 184,248 bytes.

184,248 bytes -- Normal build.
122,072 bytes -- without network
91,554  bytes -- without network, thumb code
So without the network, it would barely fit. On the other hand the compiler for the Cortex-M3 generates "thumb" instructions, which are more compact, so it would be smaller. The claim is that thumb code is 30 percent smaller or maybe 23.5 percent smaller depending what source you believe. So take 25 percent and this would be 91,554 bytes. So it would fit, but not leave a lot for application code.

Figuring out how to make do with 20K of ram would be another challenge.

Breakdown by object file

It is interesting to look at the sizes of the object files that get linked together to produce Kyu. Some of these are composites of numerous smaller object files that have been partially linked, but that is convenient for the current purposes. I ran "strip" on the object files and produced the following table of sizes.
21640  board.o
3336   console.o
12732  dlmalloc.o
4316   kyulib.o
32100  lib.o
17284  machine.o
1384   main.o
1284   prf.o
1412   random.o
2924   symbols.o
5472   test_io.o
10896  test_kyu.o
768    test_net.o
2984   tests.o
15968  thread.o
1264   timer.o
668    user.o
688    version.o

29212  tcp_xinu.o
25128  net.o
The last two files are the network code, which are roughly 54K in size.

The dlmalloc allocator is fairly big and could be replaced with something more primitive.
The "lib.o" module is 32K, and much of this is probably an imported fancy printf that could be replaced with something more basic also.
The test routines could be gotten rid of once Kyu was up and running, making room for application code.

Other chips

Getting Kyu to run on an STM32F103 would be a ship in a bottle exercise for sure. But there are other Cortex-M3 and Cortex-M4 chips that could be considered and that have significantly more generous resources:
STM32F205G  -  1M flash, 128K ram, Cortex-M3, 120Mhz (Particle Photon board)
NXP LPC1768 -  512K flash, 64K ram
STM32F401CC -  256K flash, 64K ram
STM32F411CE -  512K flash, 128K ram, Cortex-M4, 120Mhz
I have two of the F205 boards on hand, and a bundle of the F411 on the way. Either of these would seem reasonable candidates for trying to get Kyu running on a Cortex-M3 microcontroller. As near as I can tell, the main thing Cortex-M4 adds are floating point instructions, which are neither here nor there to me.

The STM32F411 boards cost about $5 each, and would take the record for the smallest and least expensive board that is able to run Kyu. Trying to move from there to the STM32F103 might be good for bragging rights, but would really be a waste of time and energy.

Have any comments? Questions? Drop me a line!

Kyu / tom@mmto.org