From: Erik M. <er...@us...> - 2001-12-16 18:23:40
|
Update of /cvsroot/blob/blob/src/blob In directory usw-pr-cvs1:/tmp/cvs-serv11239/src/blob Modified Files: debug.c Log Message: This has been sitting in my local CVS sandbox for too long: - Make all functions static - Remove debug.h - Add proper alignment checks for all functions - Remove redundant barrier()s - Improve help texts - Remove address wizardry from Poke() (Poke() and Peek() now use the same address as one would expect) Index: debug.c =================================================================== RCS file: /cvsroot/blob/blob/src/blob/debug.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- debug.c 2001/12/08 07:16:47 1.6 +++ debug.c 2001/12/16 18:23:37 1.7 @@ -36,13 +36,12 @@ #include <blob/serial.h> #include <blob/command.h> -#include <blob/debug.h> /********************************************************************** * defines */ -#define CHKMEM_DEBUG 1 +#define CHKMEM_DEBUG BLOB_DEBUG /* this will send a cold shiver through erik's spine ... */ #define ERR( x ) { ret = x; goto DONE; } @@ -51,39 +50,11 @@ #define MEM( x ) (*((u32 *)x)) /********************************************************************** - * program globals - */ - -static char memcpyhelp[] = "memcpy [source] [dest] [len]\n" -"copy memory blocks\n"; - -static char pokehelp[] = "poke address value\n"; - -static char peekhelp[] = "peek address\n"; - -static char bitchghelp[] = "bitchg address value {and|or|xor|set|clear}\n"; - - -/********************************************************************** - * module globals - */ - -/********************************************************************** * prototypes */ static void perror( int errno, char *func ); -/********************************************************************** - * exported functions - */ - -/* Commandlist stuff */ -__commandlist(CmdMemcpy, "memcpy", memcpyhelp); -__commandlist(Poke, "poke", pokehelp ); -__commandlist(Peek, "peek", peekhelp ); -__commandlist(BitChange, "bitchg", bitchghelp ); - /********************************************************************* * CmdMemcpy * @@ -93,7 +64,7 @@ * Command wrapper for memcpy utility function. * */ -int CmdMemcpy( int argc, char *argv[] ) +static int CmdMemcpy( int argc, char *argv[] ) { int ret = 0; u32 src = 0L; @@ -111,6 +82,10 @@ ret = strtou32( argv[3], &len ); if ( ret < 0 ) ERR( -EINVAL ); + /* check alignment of src and dest */ + if((src & 0x03) || (dest & 0x03)) + ERR(-EALIGN); + /* counted in words */ if ( len & 0x00000003 ) { len = ( len >> 2 ) + 1; @@ -138,6 +113,11 @@ return ret; } +static char memcpyhelp[] = "memcpy src dst len\n" +"copy len bytes from src to dst\n"; +__commandlist(CmdMemcpy, "memcpy", memcpyhelp); + + /********************************************************************* * Poke * @@ -147,12 +127,12 @@ * Poke values to memory * */ -int Poke( int argc, char *argv[] ) +static int Poke( int argc, char *argv[] ) { int ret = 0; u32 address; u32 value; - char type = 'l'; + char type = 'w'; if ( argc < 3 ) ERR( -EINVAL ); @@ -173,29 +153,53 @@ SerialOutputHex(value); SerialOutputString(" type="); SerialOutputByte(type); - SerialOutputString("\n"); + SerialOutputByte('\n'); #endif -#define mem_b(adr) ( ((volatile unsigned char*)0)[adr] ) -#define mem_w(adr) ( ((volatile unsigned short int*)0)[(adr)/2] ) -#define mem_dw(adr) ( ((volatile unsigned long*)(0)) [(adr)/4] ) + /* check memory alignment */ + switch(type | 0x20) { + case 'b': + /* bytes don't have alignment restrictions */ + break; + + case 'h': + /* half word accesses should be aligned on half words */ + if(address & 0x01) + ERR(-EALIGN); + + break; + + case 'w': + /* word accesses should be aligned on word boundaries */ + if(address & 0x03) + ERR(-EALIGN); + + break; + default: + /* hmm, invalid type */ + ERR( -EINVAL ); + break; + } + + /* write the value to memory */ switch( type | 0x20 ) { - case 'b': - mem_b( address ) = value & 0xff; - //(*((u8 *)address)) = value & 0xff; - break; - case 'w': - mem_w( address ) = value & 0xffff; - //(*((u16 *)address)) = value & 0xffff; - break; - case 'l': - mem_dw( address ) = value; - //(*((u32 *)address)) = value; - break; - default: - ERR( -EINVAL ); - break; + case 'b': + *((u8 *)address) = (u8)(value & 0xff); + break; + + case 'h': + *((u16 *)address) = (u16)(value & 0xffff); + break; + + case 'w': + *((u32 *)address) = value; + break; + + default: + /* this should not happen */ + ERR( -EINVAL ); + break; } @@ -207,6 +211,10 @@ return ret; } +static char pokehelp[] = "poke address value [b|h|w]\n" +"b = byte, h = half word, w = word (default is w)\n"; +__commandlist(Poke, "poke", pokehelp ); + /********************************************************************* * Peek * @@ -216,12 +224,12 @@ * Peeks values from memory * */ -int Peek( int argc, char *argv[] ) +static int Peek( int argc, char *argv[] ) { int ret = 0; u32 address; u32 value; - char type = 'l'; + char type = 'w'; if ( argc < 2 ) ERR( -EINVAL ); @@ -230,35 +238,60 @@ if ( argc >= 2 ) { type = argv[2][0]; - SerialOutputString("type="); - SerialOutputByte(type); - SerialOutputString("\n"); } #if CHKMEM_DEBUG SerialOutputString("adr=0x"); SerialOutputHex(address); - SerialOutputString("\n"); + SerialOutputString(" type="); + SerialOutputByte(type); + SerialOutputByte('\n'); #endif - value = 0; + /* check memory alignment */ + switch(type | 0x20) { + case 'b': + /* bytes don't have alignment restrictions */ + break; + + case 'h': + /* half word accesses should be aligned on half words */ + if(address & 0x01) + ERR(-EALIGN); + + break; + + case 'w': + /* word accesses should be aligned on word boundaries */ + if(address & 0x03) + ERR(-EALIGN); + + break; + + default: + /* hmm, invalid type */ + ERR( -EINVAL ); + break; + } + + /* read value from memory */ switch( type | 0x20 ) { - case 'b': - value = (*((u8 *)address)) & 0xff; - barrier(); - break; - case 'w': - value = (*((u16 *)address))& 0xffff; - barrier(); - break; - case 'l': - value = (*((u32 *)address)); - barrier(); - break; - default: - ret = -EINVAL; - goto DONE; - break; + case 'b': + value = (*((u8 *)address)) & 0xff; + break; + + case 'h': + value = (*((u16 *)address))& 0xffff; + break; + + case 'w': + value = (*((u32 *)address)); + break; + + default: + /* this should not happen */ + ERR(-EINVAL); + break; } SerialOutputByte(type); @@ -274,6 +307,10 @@ return ret; } +static char peekhelp[] = "peek address [b|h|w]\n" +"b = byte, h = half word, w = word (default is w)\n"; +__commandlist(Peek, "peek", peekhelp ); + /********************************************************************* * dump * @@ -283,7 +320,7 @@ * dumps memory * */ -int dump( int argc, char *argv[] ) +static int dump( int argc, char *argv[] ) { int ret = 0; u32 address; @@ -303,20 +340,24 @@ if ( ret < 0 ) ERR( -EINVAL ); } else - endaddress = address + 0x80; + endaddress = address + 0x80; + + + /* check alignment of address and endaddress */ + if((address & 0x03) || (endaddress & 0x03)) + ERR(-EALIGN); + /* print it */ for ( ; address < endaddress; address += 0x10) { SerialOutputHex(address); SerialOutputString(": "); for (tmpaddress = address; tmpaddress < address + 0x10; tmpaddress += 4) { value = (*((u32 *)tmpaddress)); - barrier(); SerialOutputHex(value); SerialOutputByte(' '); } for (tmpaddress = address; tmpaddress < address + 0x10; tmpaddress++) { value = (*((u8 *)tmpaddress)) & 0xff; - barrier(); if ((value >= ' ') && (value <= '~')) SerialOutputByte(value); else @@ -332,6 +373,7 @@ } return ret; } + static char dumphelp[] = "dump address [endAddress]\n"; __commandlist(dump, "dump", dumphelp ); @@ -344,7 +386,7 @@ * Modifies bits of an given memory location * */ -int BitChange( int argc, char *argv[] ) +static int BitChange( int argc, char *argv[] ) { int ret = 0; u32 adr = 0L; @@ -358,6 +400,10 @@ ret = strtou32( argv[2], &value ); if ( ret < 0 ) ERR( -EINVAL ); + /* check memory alignment */ + if(adr &= 0x03) + ERR(-EALIGN); + SerialOutputHex( MEM( adr ) ); switch ( argv[3][0] & (~0x20) ) { @@ -390,6 +436,9 @@ } return ret; } + +static char bitchghelp[] = "bitchg address value {and|or|xor|set|clear}\n"; +__commandlist(BitChange, "bitchg", bitchghelp ); /********************************************************************** * static functions |