DPRG
DPRG List  



DPRG: Poke

Subject: DPRG: Poke
From: MIKE MCCARTY -- 93789 JMCCARTY at DEVEL.USA.ALCATEL.COM
Date: Mon Aug 16 18:30:50 CDT 1999

I have a question which I was reminded about on looking at the code for
the Ming pair.

Why do so many robotics C programmers insist on using peek() and
poke()? Those look like ugly hangovers from the bad old BASIC days.
Neither is necessary nor desireable in a C environment.

I see this code:

	poke(0x3c, 1); /* Disable pcode character receive mode*/
	poke(0x102b,0x33); /*Set serial baud rate to 1200*/
	poke(0x102d,0x04); /*Enable receiver*/

		...

	while (!(peek(0x102e)&0x20)) /*Loop until character is received*/
		;


The terrible in-line constants aside, subroutine calls are unnecessary
and undesirable.

Ideally, in an include file, but I'll do it in-line here

	#define DUNNO			(*((unsigned char *)0x003C))
	#define BAUD			(*((unsigned char *)0x102B))
	#define SERIAL_CONTROL_2	(*((unsigned char *)0x102D))
	#define SERIAL_STATUS		(*((unsigned char *)0x102E))

	#define	READ_READY		0x20

	#define SCP1			0x20
	#define SCP0			0x10

	#define SCR2			0x04
	#define	SCR1			0x02
	#define	SCR0			0x01

	#define	BAUD_1200		(SCP1|SCP0|SCR1|SCR0)
	#define RECEIVE_ENABLE		0x04

	...

	DUNNO = 1;	/* Use an equate for the data, too! */
	BAUD = BAUD_1200;
	SERIAL_CONTROL_2 = RECEIVE_ENABLE;

	...

	while (!(SERIAL_STATUS & READ_READY))
		;

Suddenly the code doesn't need comments for one thing. For another, it
runs faster as it makes no subroutine calls. This should result in
direct data stores and reads. Reading the values works just as one
would expect, also. One can do this:

	PLACE |= SOME_BIT;

etc.

Now I agree that for *this* code, the speed and size difference is
minute. But the advantages in readability are pretty great. And for code
which must be reasonably fast, the direct data stores will be much
better than pushes on the stack, loads from them, and subroutine call
and return overhead.

So tell me, why do roboticists program like this?

------------------------------

More information about the DPRG mailing list