Exercises in microspace optimization

SpokePOV kit for bikes

Moderators: adafruit_support_bill, adafruit

Exercises in microspace optimization

Postby litghost » Tue May 04, 2010 4:20 am

Disclaimer, I have not tested anything here, try at your own risk, etc, etc.

While I was waiting for my SpokePOV to come in the mail, I was looking at the firmware to see how spiffy I could could tweak it. So I load up the code in AVR Studio and what do I find for the code size:
Code: Select all
Program:    1966 bytes (96.0% Full)

Here was the starting flags:
Code: Select all
-Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  -DF_CPU=8000000UL


Wow, that is not much to work with. So I started looking around for some size optimizations I could add to squeeze some more code in.


I looked around and found a list of size optimizations here.

Results in order applied (I did not attempt to test every configuration with every other configuration):
  • Added -Wl,--relax: 1964 bytes (95.9% Full)
  • Added -ffreestanding and void main() __attribute__ ((noreturn)); : 1942 bytes (94.8% Full) :!: Dropped 22 bytes!
  • Added -fno-tree-scev-cprop: no effect still 1942 bytes (94.8% Full)
  • Added -ffunction-sections, -fdata-sections, -Wl,--gc-sections: Program: 1910 bytes (93.3% Full) :!: Dropped 32 bytes!

Wow, a ~2.8% reduction in byte count for what should not effect the running code (have not tested, but the largest drops are from removing the main stack return code and dead code removal, two "safe" operations).

At this point I started looking in the code for repeated structures. I found the "NOP; NOP; NOP; NOP;" (or NOP4 for short) blocks present throughout the code, with the relatively consistent comment "// wait 500 ns". The delay comes from wait 4 cpu cycles before starting the next operation. So in theory any operation that takes 4 cpu cycles without changing the state of the CPU is an equivalent replacement. Looking at the source listing the NOP4 code takes 8 bytes each time it is used (2 bytes per NOP * 4). So as long as I can find an equivalent timing operation that has no effect on the state that is less than 8 bytes is a net gain per use.

SpokePOV uses 9 NOP4, or 80 bytes for timing code the does nothing else.

My first step was to replace "NOP; NOP; NOP; NOP;" with "wait500ns();" where "wait500ns();" is
Code: Select all
#define wait500ns() NOP; NOP; NOP; NOP;


This allows me to change one place and every instance of NOP4.

Looking at the instruction chart located here, I pick found an instruction that could work, RJMP. RJMP takes the PC (program counter) and changes it too PC= PC + k + 1. It also happens to take two cpu cycles. During non-jump operations, the PC is incremented by one each clock cycle (if you ignore interrupts and the like). The key here is an "rjmp +.0" is a NOP that takes two cycles! It also happens to be encoded in 2 bytes, so wait500ns() can be redefined to:
Code: Select all
#define JMP_P1 asm volatile("rjmp .+0");
#define wait500ns() JMP_P1; JMP_P1;


Resulting in:
Code: Select all
Program:    1874 bytes (91.5% Full)


That's all I found in my first pass.

tl;dr Final results: 92 bytes (~46 instructions) recovered while learning avr-gcc flags and AVR ASM with hopefully no effect on execution.

Anyone else can squeeze the SpokePOV firmware anymore?
Attachments
SpokePOV.7z
Slightly smaller SpokePOV firmware, untested
(5.87 KiB) Downloaded 94 times
litghost
 
Posts: 1
Joined: Tue May 04, 2010 3:27 am

Re: Exercises in microspace optimization

Postby adafruit » Tue May 04, 2010 10:32 pm

sadly, there arent any pin-compatible chips with more space :/
User avatar
adafruit
 
Posts: 10491
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Exercises in microspace optimization

Postby fat16lib » Wed May 05, 2010 8:30 am

There may soon be a part, ATtiny4313, that Atmel claims is pin compatible with the ATtiny2313 and has 4K flash.

http://www.atmel.com/dyn/corporate/view_detail.asp?ref=&FileName=attiny4313.htm&SEC_NAME=Product&source=ew2010_mcu_pr

I say may because Atmel's soon can be a long time.
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm


Re: Exercises in microspace optimization

Postby fat16lib » Thu May 06, 2010 1:35 pm

You could request samples. The ATmega328 was announced in 2007 then delayed. Finally I got samples for my birthday in Apr 2008. I think retail quantities were shipped about Sep 2008.

As Adafruit says 1.5 years.
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm


Return to SpokePOV

Who is online

Users browsing this forum: No registered users and 1 guest

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]