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

From: Dave Dribin <ddribin1gmail.com>
Date: 2010-12-06 06:57:28
Hello,

I'm trying to replicate this bit of assembly code (used on an NES) in C:

lda $2002 ; Read PPU status to reset the high/low latch
lda #$20 ; Write the high byte of $2000
sta $2006 ;  .
lda #$00 ; Write the low byte of $2000
sta $2006 ;  .

The addresses $2002 and $2006 correspond to memory mapped registers.
THus, even though the result of the first read is ignore, it still
needs to be performed to put the device in the correct state.  Here's
my attempt at the C version:

#define _MMBYTE(_A_) (*((volatile uint8_t *)(_A_)))
#define IO_Write8(_A_, _V_) (_MMBYTE(_A_)) = (_V_);
#define IO_Read8(_A_) (_MMBYTE(_A_))

void foo(void)
{
  IO_Read8(0x2002);
  IO_Write8(0x2006, 0x20);
  IO_Write8(0x2006, 0x00);
}

Unfortunately, the IO_Read8() is being optimized out when compiled with -Os:

000000r 1               ;
---------------------------------------------------------------
000000r 1               ; void __near__ foo (void)
000000r 1               ;
---------------------------------------------------------------
000000r 1
000000r 1               .segment "CODE"
000000r 1
000000r 1               .proc _foo: near
000000r 1
000000r 1               .segment "CODE"
000000r 1
000000r 1               ;
000000r 1               ; IO_Write8(0x2006, 0x20);
000000r 1               ;
000000r 1  A9 20         lda     #$20
000002r 1  8D 06 20     sta     $2006
000005r 1               ;
000005r 1               ; IO_Write8(0x2006, 0x00);
000005r 1               ;
000005r 1  A9 00         lda     #$00
000007r 1  8D 06 20     sta     $2006
00000Ar 1               ;
00000Ar 1               ; }
00000Ar 1               ;
00000Ar 1  60           rts
00000Br 1
00000Br 1               .endproc

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?

Thanks!

-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 06:57:35 2010

This archive was generated by hypermail 2.1.8 : 2010-12-06 06:57:38 CET