Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

How to make very compact code

Altera_Forum
Honored Contributor II
2,177 Views

I have some code that gets run whenever the processor is reset and basically reads a register continually, waits for it to be set to '1' and when it is, sets up a DMA (by setting the registers directly NOT using the HAL) and sets a few more registers. So, it basically consists of about 20 reads and writes to 32 bit registers, and a few small (less than 128 iterations) simple loops. 

 

Why does it take 17KB when I compile it using IDE? What can I do to make it nice and small so I can fit in in some M4K blocks and make it part of the configuration file? I want to get it down to 8KB.  

 

Many thanks, 

Brian
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
501 Views

I did it by editing the NIOS-II installation. 

 

Comment out unnecesary calls from components\altera_hal\HAL\src\alt_main.c 

 

Comment out all .c files from the makefile components\altera_hal\HAL\src\component.mk - leaving these: 

 

C_LIB_SRCS += alt_main.c  

alt_irq_handler.c  

alt_irq_register.c  

 

You may also need to comment out lines from other makefiles - such as components\altera_avalon_cfi_flash\HAL\src\component.mk (remove all of the entries) 

 

 

Also make sure you set compiler optimisations to "minimum size", and also tick the "small C library" option.
0 Kudos
Altera_Forum
Honored Contributor II
501 Views

If you are using the JTAG UART, the driver for the UART requires 4kBytes of memory just for the software buffers. 

 

You could modify the altera_avalon_jtag_uart.h to lower the size of the software buffers, or you could remove the JTAG UART from your design.
0 Kudos
Altera_Forum
Honored Contributor II
501 Views

Thanks for your help. We got it down to around 9K. I think it can go smaller though. I am going to remove the JTAG UART all together and see what impact that has.

0 Kudos
Altera_Forum
Honored Contributor II
501 Views

I assume you're using alt_main() instead of main(). If you're not using any standard library stuff, that should link out, too. Also, you can do your own initialization instead of alt_sys_init(). 

 

Another thing you can do is get rid of interrupts and just poll everything. More CPU use, and it's not as fast, but it's a lot simpler to debug. 

 

I've got a system with a 32 MHz Nios II/e using 2K of ROM and 512 bytes of RAM on-chip, and nothing external. It shares a Cyclone 1C3 with some DMA and FIFO stuff. It took some work, and there's a _mulsi3() routine that refuses to go away (even though it's not referenced at all). 

 

I think there's a flag you can pass to nios2-elf-nm when you run it on your final elf file so that it shows you how much space each object in the file takes. That can at least help you see where yourr memory is going.
0 Kudos
Altera_Forum
Honored Contributor II
501 Views

What are the broad steps to roll my own system_init. 

 

george
0 Kudos
Altera_Forum
Honored Contributor II
501 Views

[list][*]Use alt_main() instead of main(). 

[*]Copy the alt_sys_init() function that is generated in your system library over to your own source file 

[*]Delete any parts of your alt_sys_init() that you don't need. Make sure to give it a different name, if you're not just copying its code straight into your alt_main(). 

[*]So long as you don't reference anything in alt_sys_init.c (including globals), it won't link alt_sys_init.o into your executable. 

[/list]That should do it.
0 Kudos
Reply