[sdiy] Pointers in C

Paul Maddox yo at vacoloco.net
Sat Dec 31 18:06:54 CET 2011


Olivier,

 actually that's a good point, I haven't...

 Looks like it's fairly optimal here ;

;//*****************************************************************
;// Put Address on Address Bus pins
;void SetAddress(unsigned char voice, unsigned int address)
; 0000 0112 {
_SetAddress:
; 0000 0113 unsigned char LSByte = 0;
; 0000 0114 unsigned char MSByte = 0;
; 0000 0115 
; 0000 0116 SPCR = 0x00;                // SPI disabled
	ST   -Y,R27
	ST   -Y,R26
	ST   -Y,R17
	ST   -Y,R16
;	voice -> Y+4
;	address -> Y+2
;	LSByte -> R17
;	MSByte -> R16
	LDI  R17,0
	LDI  R16,0
	LDI  R30,LOW(0)
	OUT  0x2C,R30
; 0000 0117 SPSR = 0x00;
	OUT  0x2D,R30
; 0000 0118 DDRB = 0xFF;                // PORTB output
	LDI  R30,LOW(255)
	OUT  0x4,R30
; 0000 0119 
; 0000 011A LSByte = address;           // lower byte of address (bits 0 - 7)
	LDD  R17,Y+2
; 0000 011B address >>= 8;
	LDD  R30,Y+3
	ANDI R31,HIGH(0x0)
	STD  Y+2,R30
	STD  Y+2+1,R31
; 0000 011C 
; 0000 011D MSByte = address;           // next byte of address (bits 8 - 15)
	LDD  R16,Y+2
; 0000 011E MSByte &= 0x7F;             // mask off MSBit
	ANDI R16,LOW(127)
; 0000 011F 
; 0000 0120 PORTA = LSByte;             // put address on pins
	OUT  0x2,R17
; 0000 0121 PORTB = MSByte;             // put address on pins
	OUT  0x5,R16

however I notice that where I'm doing some additions, it's repeatedly doing a call to a function ;
;VoiceAcc[0] += VoicePTC[0];
	LDS  R30,_VoicePTC
	LDS  R26,_VoiceAcc
	LDS  R27,_VoiceAcc+1
	LDS  R24,_VoiceAcc+2
	LDS  R25,_VoiceAcc+3
	CLR  R31
	CLR  R22
	CLR  R23
	CALL __ADDD12
	STS  _VoiceAcc,R30
	STS  _VoiceAcc+1,R31
	STS  _VoiceAcc+2,R22
	STS  _VoiceAcc+3,R23

A "call" command eats four clock cycles, the function "__ADDD12:" is quite efficient ;
__ADDD12:
	ADD  R30,R26
	ADC  R31,R27
	ADC  R22,R24
	ADC  R23,R25
	RET

but the call and the ret are a very expensive way, in terms of clock cycles, of doing this.... 

Maybe time to switch to assembly language and forget C.
irritating.

Paul

On 31 Dec 2011, at 14:55, Olivier Gillet wrote:

>> I'm currently using a shift/mask approach and it's taking up valuable clock cycles.
> 
> Have you checked the generated code? There are many places in the
> Shruthi-1 code where I do things like:
> 
> uint8_t result = (a_16_bits_word >> 8) & 0xff;
> 
> And gcc is always smart enough to compile this into a direct byte /
> register transfer ; not the shift and mask. Even better, when
> a_16_bits_word is the result of a complex sequence of operations, gcc
> removes dead code that has no influence on the MSByte.
> 
> Olivier

______________________________________
VacoLoco

http://VacoLoco.net







More information about the Synth-diy mailing list