CHRISTMAS LITES OPTIMIZATION

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Sun Dec 31, 2006 9:42 am

Dude!
You're a hard man to beat. But here goes...

35 bytes.

Code: Select all

Device = 18F452 
Clock = 20 
Config WDT = OFF,
       STVR = OFF  

EEPROM = (1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1)

Clear(TRISB)
Inc(EEADR)
EECON1 = 1
LATB = EEDATA
ASM
    bra 0
End ASM

End
How this works, is amazing. Hopefully someone can tell me. :twisted:

Added: key is using MCS Bootloader (it's part of Swordfish right)
Best regards,
DT

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sun Dec 31, 2006 3:32 pm

Darrel,

You're very good.. :D

We have whittled the user code down to it's smallest parts converting to only 5 to 6 Assembly Instructions, which is 10-12 bytes. There are another 3 instructions at the end and a couple goto's in the beginning. The rest of those bytes are sitting somewhere and it appears that you are finding them.

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Mon Jan 01, 2007 5:51 am

Thanks xor,

But the "Good" part is debatable, as I'm sitting here on New Years Eve, still trying to come up with a better way. :oops:

I wasn't satisfied with the last version at 35 bytes, because it relied on the bootloader for the timing, so in effect it really needed more code space than what Swordfish compiled to.

But, I found that with a change to 1 instruction, it doesn't need the bootloader anymore. Instead, it jumps over the end of the program, into the space with all FFFF's and executes over 1 million NOP's before rolling over to address 0000 again. 8)

Hopefully, this will put the subject to bed.
Still 35 bytes.

Code: Select all

Device = 18F452 
Clock  = 20 
Config  WDT = OFF
       
EEPROM = (1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1,
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1)

Clear(TRISB)
EECON1 = 1
LATB = EEDATA
Inc(EEADR)

ASM
    bra 0x20  ; <-- Here's the difference
End ASM

End
______________
Happy New Year!
-- DT --

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Tue Jan 02, 2007 3:50 pm

Darrel,

You have some great ideas. Here is a trick to to get rid of the ASM and still keep it at 35 Bytes:

Code: Select all

Device = 18F452
Clock = 8
 ' WDT = OFF  ... by default

EEPROM = (1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1, 
          1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1) 

TRISB = 0 
EECON1 = 1 
LATB = EEDATA 
Inc(EEADR) 
PCL.5 = 1

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Wed Jan 03, 2007 4:38 am

Ha! There you go. :idea:

Now it's the smallest 100% Swordfish program to do the job.

And since you mentioned you were doing it on another forum too, and I assume that's a different language. Let me just continue the Sillyness with this 100% ASM program that I think would be the smallest, for any language. Compiled with MPLAB...

12 bytes

Code: Select all

 PROCESSOR PIC18F452
 RADIX DEC
  #include P18F452.inc
    __CONFIG    _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H
    __CONFIG    _CONFIG2H, _WDT_OFF_2H
    __CONFIG    _CONFIG4L, _LVP_OFF_4L & _DEBUG_OFF_4L

    ORG    0
    CLRF   TRISB
    MOVLW  1
    MOVWF  EECON1
    MOVFF  EEDATA,LATB
    INCF   EEADR,1

    org  0xF00000
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
    de    1,2,4,8,16,32,64,128,128,64,32,16,8,4,2,1
   
    end
Image
Best regards,
DT

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Thu Jan 18, 2007 7:12 pm

I don't have a way to test this right now, but I think I can squeak something more out of your ASM code....it never ends... 8) :

Code: Select all

    ORG      0 
    CLRF     TRISB 
    MOVFF    TRISE, EECON1 
    MOVFF    EEDATA,LATB 
    INCF     EEADR,1
With some P18's this could work:

Code: Select all

    ORG      0 
    CLRF     TRISB 
    MOVFF    CMCON, EECON1 
    MOVFF    EEDATA,LATB 
    INCF     EEADR,1

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Fri Jan 19, 2007 12:12 am

I'm sure those would work, but it doesn't reduce the code size.

MOVFF is a 2-word instruction

Code: Select all

    ORG      0             
    CLRF     TRISB         ; 1
    MOVFF    CMCON, EECON1 ; 2
    MOVFF    EEDATA,LATB   ; 2
    INCF     EEADR,1       ; 1
                           ;---- 
                           ; 6 words = 12 bytes
Thought ya had me there for a minuite. :P
Best regards,
DT

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Fri Jan 19, 2007 12:45 am

Hi Darrel,

My understanding of P18's is that all instructions are 2 bytes. The difference is that a few instructions, including MOVFF, are 2 machine cycles each. The Program Clock only works in 2's with P18 with the LSB ignored. Your previous ASM should be 10 bytes and this one is 8 bytes. The machine cycles for both are identical though.

That's so....isn't it? :?

User avatar
Darrel Taylor
Posts: 29
Joined: Wed Oct 04, 2006 4:44 pm
Location: California

Post by Darrel Taylor » Fri Jan 19, 2007 3:32 am

MOST of the instructions only use 1 word, but there are a few that take 2.

CALL
GOTO
LFSR
MOVFF

You can see it in the Instruction Set section of the datasheet, where it lists both WORDs and Cycles for each opcode.
Best regards,
DT

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sun Jan 21, 2007 3:07 pm

I checked it out and you're correct. Those 4 instructions are 2 words each. Thanks for keeping me on the right track here.

Post Reply