Re: [cc65] How to force a read from a memory mapped register w/o volatile

From: Dave Dribin <ddribin1gmail.com>
Date: 2010-12-06 16:49:08
On Mon, Dec 6, 2010 at 9:09 AM, Ullrich von Bassewitz <uz@musoftware.de> wrote:
>
> Hi!
>
> On Sun, Dec 05, 2010 at 11:57:28PM -0600, Dave Dribin wrote:
>> Typically the 'volatile' keyword tells the compiler not to perform
>> this optimization.  Since the 'volatile' keyword is parsed, but has no
>> effect, how do I go about doing this with C in cc65?
>
> There are workarounds, but none of them is really great:
>
> 1. Write the function in assembler

I tried using inline assembler, and it still got optimized out:

    __asm__ ("lda %w", 0x2002);
    IO_Write8(0x2006, 0x20);
    IO_Write8(0x2006, 0x00);

> 2. Disable optimizations for the function that contains the statement.

Hrm... that's not ideal. It generates extraneous code with ldx #$00:

000000r 1               .segment	"CODE"
000000r 1
000000r 1               ;
000000r 1               ; IO_Read8(0x2002);
000000r 1               ;
000000r 1  A2 00        	ldx     #$00
000002r 1  AD 02 20     	lda     $2002
000005r 1               ;
000005r 1               ; IO_Write8(0x2006, 0x20);
000005r 1               ;
000005r 1  A2 00        	ldx     #$00
000007r 1  A9 20        	lda     #$20
000009r 1  8D 06 20     	sta     $2006
00000Cr 1               ;
00000Cr 1               ; IO_Write8(0x2006, 0x00);
00000Cr 1               ;
00000Cr 1  A2 00        	ldx     #$00
00000Er 1  A9 00        	lda     #$00
000010r 1  8D 06 20     	sta     $2006
000013r 1               ;
000013r 1               ; }
000013r 1               ;
000013r 1  60           	rts
000014r 1
000014r 1               .endproc


> 3. Use the value in some way to trick the compiler.

I've tried a few things, and none of them are working:


    { uint8_t dummy = IO_Read8(0x2002); }
    IO_Write8(0x2006, 0x20);
    IO_Write8(0x2006, 0x00);

Produces:

000000r 1               ; { uint8_t dummy = IO_Read8(0x2002); }
000000r 1               ;
000000r 1  AD 02 20     	lda     $2002
000003r 1  20 rr rr     	jsr     pusha
000006r 1  20 rr rr     	jsr     incsp1

Any other ideas?  As a future work around, perhaps we could be able to
disable optimizations on a block level:


void foo(void)
{
#pragma optimize(push, off)
    {
        IO_Read8(0x2002);
    }
#pragma optimize(pop)

    IO_Write8(0x2006, 0x20);
    IO_Write8(0x2006, 0x00);
}

-Dave
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Mon Dec 6 16:49:14 2010

This archive was generated by hypermail 2.1.8 : 2010-12-06 16:49:17 CET