Re: [cc65] Finished product: FAST ShellSort demo for c64 (working :) ) - feedback?

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-03-05 17:48:42


On Wed, Mar 05, 2003 at 04:57:23PM +0100, Spiro Trikaliotis wrote:
> Although not really necessary, I suggest using a void parameter instead of
> an empty parameter list. In C++, this is the same, but it is not in C. In
> C, it means the function can take any parameter, which seems not to be the
> thing you meant, did you?

Explicitly declaring an empty parameter list as "void" gives better code.

> > //bank out BASIC interpreter
> > a = PEEK(1);
> > POKE(1,a&254);
>
> I rather would code it something like:
>
> unsigned char *pP6510IO = 1;
>
> and then
>
> *pP6510IO &= 0xFE;
>
> This looks much more "C-like" (or "C++-like") to me.

The problem with the code is not how it is written, but that banking out the
BASIC interpreter is not needed and may actually crash the program after
banking it in again. This is described in internal.txt: The startup code will
already disable the BASIC interpreter, so if a program banks it in, code
residing under the ROM may become unreachable; the program may crash when such
code is called later.

> > unsigned long stopwatch()
> > {
> > unsigned long sw_time = 0;
> >
> > sw_time += ((unsigned long)PEEK(160)) * 0x10000;
> > sw_time += ((unsigned long)PEEK(161)) * 0x100;
> > sw_time += (unsigned long)PEEK(162);
> >
> > return sw_time;
> > }
>
> Is the counter really in this order (160 = high byte, 162 = low byte)?
> If yes, what about:
>
> unsigned char *pTimer = 160;
>
> sw_time = (((unsigned long)pTimer[0]) << 16)
>         | (((unsigned long)pTimer[1]) << 8)
>         |                  pTimer[2];
>
> On many architectures, this results in much faster code. I'm not sure
> about CC65 (haven't tested it).

Both code snippets have the same problem: If an interrupt occurs between the
fetch of the three bytes, and if this interrupt causes an overflow, the value
read will be wrong. The chance for this to happen is slightly higher than
1/256 or 0.4%. If one *has* to read memory locations that are modified inside
an interrupt handler, it is necessary to disable interrupts.

So: Use the clock() function. Using clock() will also mean that the program
will run on other platforms.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-03-05 17:48:53 CET