From 9e19c95d8efc78a22a32b4a77f9c4a0b4e64125c Mon Sep 17 00:00:00 2001 From: Dave Chapman Date: Thu, 6 Oct 2005 19:27:43 +0000 Subject: Replace SWAB16 and SWAB32 with a comprehensive set of byte-swap macros - letoh16, letoh32, htole16, htole32, betoh16, betoh32, htobe16 and htobe32 git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7584 a1c6a512-1295-4272-9138-f99709370657 --- firmware/drivers/ata.c | 6 ++--- firmware/drivers/fat.c | 56 ++++++++++++++++++++++----------------------- firmware/export/system.h | 59 ++++++++++++++++++++++++++++++++++++++++-------- firmware/pcm_record.c | 6 ++--- 4 files changed, 83 insertions(+), 44 deletions(-) (limited to 'firmware') diff --git a/firmware/drivers/ata.c b/firmware/drivers/ata.c index 61365094bc..475b1257a6 100644 --- a/firmware/drivers/ata.c +++ b/firmware/drivers/ata.c @@ -362,7 +362,7 @@ static void copy_read_sectors(unsigned char* buf, int wordcount) { /* loop compiles to 7 assembler instructions */ /* takes 12 clock cycles (2 pipeline stalls, 1 wait) */ #ifdef SWAP_WORDS - *wbuf = SWAB16(ATA_DATA); + *wbuf = letoh16(ATA_DATA); #else *wbuf = ATA_DATA; #endif @@ -677,7 +677,7 @@ static void copy_write_sectors(const unsigned char* buf, int wordcount) #ifdef SWAP_WORDS /* loop compiles to 6 assembler instructions */ /* takes 10 clock cycles (2 pipeline stalls) */ - SET_16BITREG(ATA_DATA, SWAB16(*wbuf)); + SET_16BITREG(ATA_DATA, htole16(*wbuf)); #else SET_16BITREG(ATA_DATA, *wbuf); #endif @@ -1270,7 +1270,7 @@ static int identify(void) #ifdef SWAP_WORDS identify_info[i] = ATA_DATA; #else - identify_info[i] = SWAB16(ATA_DATA); + identify_info[i] = letoh16(ATA_DATA); #endif } diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index e3e05f8523..5d430dd24e 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c @@ -534,7 +534,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume)) if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */ break; - if (SWAB16(fat[j]) == 0x0000) { + if (letoh16(fat[j]) == 0x0000) { free++; if ( fat_bpb->fsinfo.nextfree == 0xffffffff ) fat_bpb->fsinfo.nextfree = c; @@ -553,7 +553,7 @@ void fat_recalc_free(IF_MV_NONVOID(int volume)) if ( c > fat_bpb->dataclusters+1 ) /* nr 0 is unused */ break; - if (!(SWAB32(fat[j]) & 0x0fffffff)) { + if (!(letoh32(fat[j]) & 0x0fffffff)) { free++; if ( fat_bpb->fsinfo.nextfree == 0xffffffff ) fat_bpb->fsinfo.nextfree = c; @@ -740,7 +740,7 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon break; for (j = 0; j < CLUSTERS_PER_FAT16_SECTOR; j++) { int k = (j + offset) % CLUSTERS_PER_FAT16_SECTOR; - if (SWAB16(fat[k]) == 0x0000) { + if (letoh16(fat[k]) == 0x0000) { unsigned int c = nr * CLUSTERS_PER_FAT16_SECTOR + k; /* Ignore the reserved clusters 0 & 1, and also cluster numbers out of bounds */ @@ -768,7 +768,7 @@ static unsigned long find_free_cluster(IF_MV2(struct bpb* fat_bpb,) unsigned lon break; for (j = 0; j < CLUSTERS_PER_FAT_SECTOR; j++) { int k = (j + offset) % CLUSTERS_PER_FAT_SECTOR; - if (!(SWAB32(fat[k]) & 0x0fffffff)) { + if (!(letoh32(fat[k]) & 0x0fffffff)) { unsigned long c = nr * CLUSTERS_PER_FAT_SECTOR + k; /* Ignore the reserved clusters 0 & 1, and also cluster numbers out of bounds */ @@ -819,17 +819,17 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) } if ( val ) { - if (SWAB16(sec[offset]) == 0x0000 && fat_bpb->fsinfo.freecount > 0) + if (htole16(sec[offset]) == 0x0000 && fat_bpb->fsinfo.freecount > 0) fat_bpb->fsinfo.freecount--; } else { - if (SWAB16(sec[offset])) + if (htole16(sec[offset])) fat_bpb->fsinfo.freecount++; } LDEBUGF("update_fat_entry: %d free clusters\n", fat_bpb->fsinfo.freecount); - sec[offset] = SWAB16(val); + sec[offset] = htole16(val); } else #endif /* #ifdef HAVE_FAT16SUPPORT */ @@ -854,20 +854,20 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) } if ( val ) { - if (!(SWAB32(sec[offset]) & 0x0fffffff) && + if (!(htole32(sec[offset]) & 0x0fffffff) && fat_bpb->fsinfo.freecount > 0) fat_bpb->fsinfo.freecount--; } else { - if (SWAB32(sec[offset]) & 0x0fffffff) + if (htole32(sec[offset]) & 0x0fffffff) fat_bpb->fsinfo.freecount++; } LDEBUGF("update_fat_entry: %ld free clusters\n", fat_bpb->fsinfo.freecount); /* don't change top 4 bits */ - sec[offset] &= SWAB32(0xf0000000); - sec[offset] |= SWAB32(val & 0x0fffffff); + sec[offset] &= htole32(0xf0000000); + sec[offset] |= htole32(val & 0x0fffffff); } return 0; @@ -892,7 +892,7 @@ static long read_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry) return -1; } - return SWAB16(sec[offset]); + return letoh16(sec[offset]); } else #endif /* #ifdef HAVE_FAT16SUPPORT */ @@ -908,7 +908,7 @@ static long read_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry) return -1; } - return SWAB32(sec[offset]) & 0x0fffffff; + return letoh32(sec[offset]) & 0x0fffffff; } } @@ -960,10 +960,10 @@ static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb)) return rc * 10 - 1; } intptr = (long*)&(fsinfo[FSINFO_FREECOUNT]); - *intptr = SWAB32(fat_bpb->fsinfo.freecount); + *intptr = htole32(fat_bpb->fsinfo.freecount); intptr = (long*)&(fsinfo[FSINFO_NEXTFREE]); - *intptr = SWAB32(fat_bpb->fsinfo.nextfree); + *intptr = htole32(fat_bpb->fsinfo.nextfree); rc = ata_write_sectors(IF_MV2(fat_bpb->drive,) fat_bpb->startsector + fat_bpb->bpb_fsinfo,1,fsinfo); @@ -1212,11 +1212,11 @@ static int write_long_name(struct fat_file* file, fat_time(&date, &time, &tenth); entry[FATDIR_CRTTIMETENTH] = tenth; - *(unsigned short*)(entry + FATDIR_CRTTIME) = SWAB16(time); - *(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); - *(unsigned short*)(entry + FATDIR_CRTDATE) = SWAB16(date); - *(unsigned short*)(entry + FATDIR_WRTDATE) = SWAB16(date); - *(unsigned short*)(entry + FATDIR_LSTACCDATE) = SWAB16(date); + *(unsigned short*)(entry + FATDIR_CRTTIME) = htole16(time); + *(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time); + *(unsigned short*)(entry + FATDIR_CRTDATE) = htole16(date); + *(unsigned short*)(entry + FATDIR_WRTDATE) = htole16(date); + *(unsigned short*)(entry + FATDIR_LSTACCDATE) = htole16(date); } idx++; nameidx -= NAME_BYTES_PER_ENTRY; @@ -1520,13 +1520,13 @@ static int update_short_entry( struct fat_file* file, long size, int attr ) entry[FATDIR_ATTR] = attr & 0xFF; clusptr = (short*)(entry + FATDIR_FSTCLUSHI); - *clusptr = SWAB16(file->firstcluster >> 16); + *clusptr = htole16(file->firstcluster >> 16); clusptr = (short*)(entry + FATDIR_FSTCLUSLO); - *clusptr = SWAB16(file->firstcluster & 0xffff); + *clusptr = htole16(file->firstcluster & 0xffff); sizeptr = (long*)(entry + FATDIR_FILESIZE); - *sizeptr = SWAB32(size); + *sizeptr = htole32(size); { #ifdef HAVE_RTC @@ -1534,13 +1534,13 @@ static int update_short_entry( struct fat_file* file, long size, int attr ) unsigned short date = 0; #else /* get old time to increment from */ - unsigned short time = SWAB16(*(unsigned short*)(entry + FATDIR_WRTTIME)); - unsigned short date = SWAB16(*(unsigned short*)(entry + FATDIR_WRTDATE)); + unsigned short time = htole16(*(unsigned short*)(entry + FATDIR_WRTTIME)); + unsigned short date = htole16(*(unsigned short*)(entry + FATDIR_WRTDATE)); #endif fat_time(&date, &time, NULL); - *(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); - *(unsigned short*)(entry + FATDIR_WRTDATE) = SWAB16(date); - *(unsigned short*)(entry + FATDIR_LSTACCDATE) = SWAB16(date); + *(unsigned short*)(entry + FATDIR_WRTTIME) = htole16(time); + *(unsigned short*)(entry + FATDIR_WRTDATE) = htole16(date); + *(unsigned short*)(entry + FATDIR_LSTACCDATE) = htole16(date); } rc = fat_seek( &dir, sector ); diff --git a/firmware/export/system.h b/firmware/export/system.h index c2246e1e70..313a9a8e4c 100644 --- a/firmware/export/system.h +++ b/firmware/export/system.h @@ -56,10 +56,26 @@ void cpu_idle_mode(bool on_off); #endif #ifdef ROCKBOX_LITTLE_ENDIAN -#define SWAB16(x) (x) -#define SWAB32(x) (x) +#define letoh16(x) (x) +#define letoh32(x) (x) +#define htole16(x) (x) +#define htole32(x) (x) +#define betoh16(x) swap16(x) +#define betoh32(x) swap32(x) +#define htobe16(x) swap16(x) +#define htobe32(x) swap32(x) +#else +#define letoh16(x) swap16(x) +#define letoh32(x) swap32(x) +#define htole16(x) swap16(x) +#define htole32(x) swap32(x) +#define betoh16(x) (x) +#define betoh32(x) (x) +#define htobe16(x) (x) +#define htobe32(x) (x) #endif + #define nop \ asm volatile ("nop") @@ -162,7 +178,7 @@ static inline int set_irq_level(int level) return i; } -static inline unsigned short SWAB16(unsigned short value) +static inline unsigned short swap16(unsigned short value) /* result[15..8] = value[ 7..0]; result[ 7..0] = value[15..8]; @@ -184,7 +200,7 @@ static inline unsigned long SWAW32(unsigned long value) return result; } -static inline unsigned long SWAB32(unsigned long value) +static inline unsigned long swap32(unsigned long value) /* result[31..24] = value[ 7.. 0]; result[23..16] = value[15.. 8]; @@ -212,7 +228,7 @@ static inline int set_irq_level(int level) return oldlevel; } -static inline unsigned short SWAB16(unsigned short value) +static inline unsigned short swap16(unsigned short value) /* result[15..8] = value[ 7..0]; result[ 7..0] = value[15..8]; @@ -231,7 +247,7 @@ static inline unsigned long SWAW32(unsigned long value) return value; } -static inline unsigned long SWAB32(unsigned long value) +static inline unsigned long swap32(unsigned long value) /* result[31..24] = value[ 7.. 0]; result[23..16] = value[15.. 8]; @@ -299,7 +315,7 @@ static inline int set_irq_level(int level) return result; } -static inline unsigned short SWAB16(unsigned short value) +static inline unsigned short swap16(unsigned short value) /* result[15..8] = value[ 7..0]; result[ 7..0] = value[15..8]; @@ -308,7 +324,7 @@ static inline unsigned short SWAB16(unsigned short value) return (value >> 8) | (value << 8); } -static inline unsigned long SWAB32(unsigned long value) +static inline unsigned long swap32(unsigned long value) /* result[31..24] = value[ 7.. 0]; result[23..16] = value[15.. 8]; @@ -316,8 +332,8 @@ static inline unsigned long SWAB32(unsigned long value) result[ 7.. 0] = value[31..24]; */ { - unsigned long hi = SWAB16(value >> 16); - unsigned long lo = SWAB16(value & 0xffff); + unsigned long hi = swap16(value >> 16); + unsigned long lo = swap16(value & 0xffff); return (lo << 16) | hi; } @@ -338,6 +354,29 @@ static inline unsigned long SWAB32(unsigned long value) #endif #else +static inline unsigned short swap16(unsigned short value) + /* + result[15..8] = value[ 7..0]; + result[ 7..0] = value[15..8]; + */ +{ + return (value >> 8) | (value << 8); +} + +static inline unsigned long swap32(unsigned long value) + /* + result[31..24] = value[ 7.. 0]; + result[23..16] = value[15.. 8]; + result[15.. 8] = value[23..16]; + result[ 7.. 0] = value[31..24]; + */ +{ + unsigned long hi = swap16(value >> 16); + unsigned long lo = swap16(value & 0xffff); + return (lo << 16) | hi; +} + + #define invalidate_icache() #endif diff --git a/firmware/pcm_record.c b/firmware/pcm_record.c index 6eb7bbcba8..cafbd71754 100644 --- a/firmware/pcm_record.c +++ b/firmware/pcm_record.c @@ -354,7 +354,7 @@ void pcmrec_callback(bool flush) for (i=0; i