Forum Discussion
Software size is too big
Hello guys, i've tried small C lib, reduced device drivers and everything else but nothing works..
I am using Quartus V10.0SP1 and NIOSEDS V10.0SP1, A hello word simple program with a printf takes 4k of onchip mem... that's impossible right? I must be doing something wrong. It's the hello word example when creating a new project# include <stdio.h>
int main()
{
printf(" Hello world!");
return 0;
} It always overlap the sections .rwdata, heap and others.. Even if my onchipmemory is 10K Bytes.17 Replies
- Altera_Forum
Honored Contributor
--- Quote Start --- A hello word simple program with a printf takes 4k of onchip mem... that's impossible right? --- Quote End --- That could be quite normal: a complete printf implementation requires a lot of code. You must also account for the driver code which redirects the printf output string to the stdout device (i.e. jtag uart). Did you try the compiler directive -Os? This should optimise code size. Anyway, if your 4k code doesn't fit in a 10k memory, this is probably due to stack/heap or any data memory required by the stdio library and driver. - Altera_Forum
Honored Contributor
Well in another project i have a code with plenty of printfs and other stuffs, the code is 400 lines wide and the code size is 8k.
- Altera_Forum
Honored Contributor
Find the elf program file for your working and non-working projects and look at the symbols (nm file, or objdump -n file) and program sections (objdump -p file). That might help to identify the bloat.
The altera printf() pulls in a lot of code! I suspect it maps to fprintf(stdout, ...) so requires large tracts of stdio rather that being implemented as a console write - as a unix kernel's printf() is usually implemented. After all, the likelyhood of wanting an actual stdio implementation on a real fpga embedded application is minimal. - Altera_Forum
Honored Contributor
Maybe it is because you have included <stdio.h>. This is a lot of code which should be compiled even if you are not using all of it.
- Altera_Forum
Honored Contributor
dsl, how can i use the unix printf then?
I made the following changes and the code size went down from 28k to 13kbytes (A NORMAL PRINTF) template BSP/syslib: - uncheck Support C++ em properties->C/C++ Build do BSP Template - check Lightweight device driver no BSP Editor - check Reduced device drivers no BSP Editor - uncheck enable_clean_exit no BSP Editor - uncheck enable_exit no BSP Editor - configure optimize level to Size on properties->C/C++ Build do BSP Template - configure Debug level to none on properties->C/C++ Build do BSP Template Software: - configure optimize level to Size em properties->C/C++ Build do BSP Template - configure Debug level to none em properties->C/C++ Build do BSP Template IF I CHANGE the code to this
the size goes to 600bytes#include "sys/alt_stdio.h" //#include <stdio.h> int main() { //printf("Hello from Nios II!\n"); alt_putstr("Hello from Nios II!\n"); return 0; } - Altera_Forum
Honored Contributor
Other question is, how to read from the keyboard? I want nios to be a calculator and ask from jtag (nios-terminal) two numbers to sum.
How can i do that? Scanf is too large.. - Altera_Forum
Honored Contributor
Try getc and elaborate yourself the keystrokes sequence
- Altera_Forum
Honored Contributor
This works fine. But why it is initiatilized with some random values? If i type 101 it apears as a101 or sometimes b101 or sometimes ffef101gets(bitrate); // alt_putchar(c); //} alt_putstr(bitrate); - Altera_Forum
Honored Contributor
something like (untested):
will output an unsigned value to the terminal in decimal. The above will pull in the libc function to do divide (unless you added the hardware instruction and told gcc to use it). The 'divide by constant' can be replaced by a multiply and shift, but that requires the 64bit result from the multiply - which altera don't have support for on the older fpgas (needs the DSP based multiply) and might need a 64bit shift. Splitting the value into two 16bit values does make this possible (I've done that to avoid needing 64bit divide when converting 'long long'.) Writing printf() is just a SMOP (simple matter of programming).void put_uint(unsigned int x) { char b, *p = b + 9; unsigned int n; b = 0; do { n = x / 10; *--p = '0' + (x - n * 10); } while ((x = n) != 0); alt_putstr(p); } - Altera_Forum
Honored Contributor
Thanks for the code dsl, i am going to use. Can you look at my older post please? I"ve edited it