0

I am wondering if there is a better way to write this:

void readtcp_c(unsigned char c)
{
    volatile char *a;
    volatile int *p;
    volatile int *q;

    a = (char *)APPLE_REG_A;    // a = memory mapped address for REG A
    *a = c + 128;               // store c + 128 in REG A

    p = (int *)APPLE_SUB;       // p = address of 6502 sub
    *p = TCP_WRITE;             // store TCP entry point in address p

    p = (int *)SOFTCARD;        // p = address of softcard address
    q = (int *)*p;              // q = softcard address
    *q = 0;                     // write 0 to softcard address
}

IANS, I have to read/write to specific addresses. 'a' is simply a write to a memory mapped register (6502). The next two lines are similar, except that I am writing the address of a 6502 entry point to be used later. The last 3 lines is where I have to write a 0 to the address stored in SOFTCARD. Writing to this address triggers the call to the aforementioned entry point.

It's the last 3 lines that I think can be shorter. Perhaps not. If the other pairs can be written as a single line that'd be great too. The code works and compiles (sdcc) without error or warning.

Thanks.

Update: I guess I could replace:

    p = (int *)SOFTCARD;            // p = address of softcard address
    q = (int *)*p;                  // q = softcard address
    *q = 0;                         // write 0 to softcard address

with:

    p = (int *)*(int *)SOFTCARD;
    *p = 0;

It compiles without warning and runs. But is it readable? Thanks again.

datajerk
  • 185
  • 7
  • Apple? 6502? Register A? Softcard? Please, for the love of all that's holy, please tell me you're joking :-) Is someone actually still using these machines? – paxdiablo Jun 25 '11 at 23:50

2 Answers2

1

This should do it. I just replaced the variables by their definition after adding volatile (and also a pair of parenthesis)

void readtcp_c(unsigned char c)
{
    *((volatile char *)APPLE_REG_A) = c + 128;
    *((volatile int *)APPLE_SUB) = TCP_WRITE;
    *((volatile int *)*((volatile int *)SOFTCARD)) = 0;
}

The compiler is perfectly capable of generating good code from your first version though. I like your step-by-step code better.

pmg
  • 106,608
  • 13
  • 126
  • 198
0

You should be able to rewrite all the assignments as

*(char *)APPLE_REG_A = c + 128;
*(int *)APPLE_SUB = TCP_WRITE;
**(int **)SOFTCARD = 0;
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72