Take the 2-minute tour ×

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.


Update: I guess I could replace:

    *q = 0;                         // write 0 to softcard address


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

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

share|improve this question
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
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
+1 for volatile (compared to the other answer) –  Seth Robertson Jun 25 '11 at 23:47
Thanks. Works perfect. –  datajerk Jun 25 '11 at 23:51
add comment

You should be able to rewrite all the assignments as

*(char *)APPLE_REG_A = c + 128;
**(int **)SOFTCARD = 0;
share|improve this answer
The last line failed to compile. Thanks for the input. –  datajerk Jun 25 '11 at 23:52
add comment

Your Answer


