Compare commits
3 Commits
22470604a3
...
c4394a2faf
| Author | SHA1 | Date | |
|---|---|---|---|
| c4394a2faf | |||
| e7cfcb27df | |||
| aa55602b33 |
34
Makefile
Normal file
34
Makefile
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
CC=gcc
|
||||||
|
CFLAGS= -g3 -O0 -Wall -Wextra
|
||||||
|
INCLUDE_DIR=/usr/local/include/
|
||||||
|
LIB_DIR=/usr/local/lib
|
||||||
|
INCLUDE_PATH=-I $(INCLUDE_DIR)
|
||||||
|
LIB_PATH=-L $(LIB_DIR)
|
||||||
|
LIBS=
|
||||||
|
|
||||||
|
all: testbench
|
||||||
|
|
||||||
|
testbench: testbench.c bit.o
|
||||||
|
$(CC) $^ $(CFLAGS) $(INCLUDE_PATH) $(LIB_PATH) $(LIBS) -o $@
|
||||||
|
|
||||||
|
libbit.a: bit.o
|
||||||
|
ar rcs $@ $^
|
||||||
|
|
||||||
|
bit.o: bit.c bit.h
|
||||||
|
$(CC) -c $< $(CFLAGS) $(INCLUDE_PATH) -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf ./*.o ./*.a testbench
|
||||||
|
|
||||||
|
install: libbit.a
|
||||||
|
sudo cp libbit.a $(LIB_DIR)
|
||||||
|
sudo cp bit.h $(INCLUDE_DIR)
|
||||||
|
|
||||||
|
.PHONY: all clean install
|
||||||
|
|
||||||
|
# Variables
|
||||||
|
# $^ Will expand to be all the sensitivity list
|
||||||
|
# $< Will expand to be the frist file in sensitivity list
|
||||||
|
# $@ Will expand to be the target name (the left side of the ":" )
|
||||||
|
# -c gcc will compile but not try and link
|
||||||
122
bit.c
Normal file
122
bit.c
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/* SPDX-License-Identifier: Apache-2.0 */
|
||||||
|
|
||||||
|
/* INCLUDES ==================================================================*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
//#include <stdlib.h>
|
||||||
|
|
||||||
|
/* printf()
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* bswap_32(x)
|
||||||
|
*/
|
||||||
|
#include <byteswap.h>
|
||||||
|
|
||||||
|
#include "bit.h"
|
||||||
|
|
||||||
|
/* MACROS ====================================================================*/
|
||||||
|
|
||||||
|
/* ENUMERATIONS ==============================================================*/
|
||||||
|
|
||||||
|
/* STRUCTS ===================================================================*/
|
||||||
|
|
||||||
|
/* GLOBAL VARIABLES ==========================================================*/
|
||||||
|
|
||||||
|
/* PROTOTYPES ================================================================*/
|
||||||
|
|
||||||
|
/* FUNCTIONS =================================================================*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print a unsigned char buffer
|
||||||
|
*/
|
||||||
|
void bit_print_buffer_u8(void *buf, unsigned long len, unsigned long width, int print_header)
|
||||||
|
{
|
||||||
|
unsigned long i, j, k, rows;
|
||||||
|
__u8 *ptr;
|
||||||
|
|
||||||
|
/* STEP 1: Verify Inputs */
|
||||||
|
if ( buf == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ( len == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ( width == 0 )
|
||||||
|
width = BIT_DEFAULT_WIDTH;
|
||||||
|
|
||||||
|
if ( width < BIT_MIN_WIDTH )
|
||||||
|
width = BIT_MIN_WIDTH;
|
||||||
|
|
||||||
|
ptr = (__u8*) buf;
|
||||||
|
|
||||||
|
/* Compute the number of rows to print */
|
||||||
|
rows = len / width;
|
||||||
|
if ( (len % width) > 0)
|
||||||
|
rows++;
|
||||||
|
|
||||||
|
/* Print index '0x0000: ' */
|
||||||
|
if (print_header) {
|
||||||
|
printf(" ");
|
||||||
|
for ( i = 0 ; i < width ; i++ )
|
||||||
|
printf("%02lu ", i);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
k = 0;
|
||||||
|
for ( i = 0 ; i < rows ; i++ ) {
|
||||||
|
printf("0x%08lx: ", i * width);
|
||||||
|
for ( j = 0 ; j < width ; j++ ) {
|
||||||
|
if (k >= len)
|
||||||
|
break;
|
||||||
|
|
||||||
|
printf("%02x ", ptr[i*width + j]);
|
||||||
|
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print a unsigned long buffer
|
||||||
|
*/
|
||||||
|
void bit_print_buffer_u32(void *buf, unsigned long len, int print_header, int byteswap)
|
||||||
|
{
|
||||||
|
unsigned long i, rows;
|
||||||
|
__u32 *ptr;
|
||||||
|
|
||||||
|
/* STEP 1: Verify Inputs */
|
||||||
|
if ( buf == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ( len == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
ptr = (__u32*) buf;
|
||||||
|
|
||||||
|
/* Compute the number of rows to print */
|
||||||
|
rows = len;
|
||||||
|
|
||||||
|
/* Print index '0x0000: ' */
|
||||||
|
if (print_header) {
|
||||||
|
printf(" ");
|
||||||
|
for ( i = 0 ; i < sizeof(__u32) ; i++ )
|
||||||
|
printf("%02lu", i);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Print data */
|
||||||
|
for ( i = 0 ; i < rows ; i++ ) {
|
||||||
|
if (byteswap)
|
||||||
|
printf("0x%08lx: %08x\n", i, bswap_32(ptr[i]));
|
||||||
|
else
|
||||||
|
printf("0x%08lx: %08x\n", i, ptr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
39
bit.h
Normal file
39
bit.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/* SPDX-License-Identifier: Apache-2.0 */
|
||||||
|
|
||||||
|
#ifndef _BIT_H
|
||||||
|
#define _BIT_H
|
||||||
|
|
||||||
|
/* INCLUDES ==================================================================*/
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
|
/* MACROS ====================================================================*/
|
||||||
|
|
||||||
|
//#define BIT_VERBOSE
|
||||||
|
#define BIT_DEFAULT_WIDTH 16
|
||||||
|
#define BIT_MIN_WIDTH 4
|
||||||
|
|
||||||
|
#ifdef BIT_VERBOSE
|
||||||
|
#define VERBOSE(v, m, f, t) ({ if(v) printf("%d:%s %s\n", t, f, m ); })
|
||||||
|
#define VERBOSE_INT(v, m, f, t, i) ({ if(v) printf("%d:%s %s %d\n", t, f, m, i); })
|
||||||
|
#define VERBOSE_STR(v, m, f, t, s) ({ if(v) printf("%d:%s %s %s\n", t, f, m, s); })
|
||||||
|
#else
|
||||||
|
#define VERBOSE(v, m, f, t)
|
||||||
|
#define VERBOSE_INT(v, m, f, t, i)
|
||||||
|
#define VERBOSE_STR(v, m, f, t, s)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ENUMERATIONS ==============================================================*/
|
||||||
|
|
||||||
|
/* STRUCTS ===================================================================*/
|
||||||
|
|
||||||
|
/* GLOBAL VARIABLES ==========================================================*/
|
||||||
|
|
||||||
|
/* PROTOTYPES ================================================================*/
|
||||||
|
|
||||||
|
void bit_print_buffer_u8(void *buf, unsigned long len, unsigned long width, int print_header);
|
||||||
|
void bit_print_buffer_u32(void *buf, unsigned long len, int print_header, int byteswap);
|
||||||
|
|
||||||
|
/* INLINE FUNCTIONS ==========================================================*/
|
||||||
|
|
||||||
|
#endif //ifndef _BIT_H
|
||||||
49
testbench.c
Normal file
49
testbench.c
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/* SPDX-License-Identifier: Apache-2.0 */
|
||||||
|
/* INCLUDES ==================================================================*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <linux/types.h>
|
||||||
|
|
||||||
|
#include "bit.h"
|
||||||
|
|
||||||
|
/* MACROS ====================================================================*/
|
||||||
|
|
||||||
|
/* ENUMERATIONS ==============================================================*/
|
||||||
|
|
||||||
|
/* STRUCTS ===================================================================*/
|
||||||
|
|
||||||
|
/* GLOBAL VARIABLES ==========================================================*/
|
||||||
|
|
||||||
|
/* PROTOTYPES ================================================================*/
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
__u8 *u8;
|
||||||
|
__u32 *u32;
|
||||||
|
|
||||||
|
/* Allocate memory */
|
||||||
|
u8 = (__u8*) malloc(1024);
|
||||||
|
|
||||||
|
/* Cast variables */
|
||||||
|
u32 = (__u32*) u8;
|
||||||
|
|
||||||
|
/* Fill with data */
|
||||||
|
for ( i = 0 ; i < 1024 ; i ++ )
|
||||||
|
u8[i] = i;
|
||||||
|
|
||||||
|
printf("bit_print_buffer_u8() ------------------\n");
|
||||||
|
bit_print_buffer_u8(u8, 1024, 16, 1);
|
||||||
|
|
||||||
|
//printf("bit_print_buffer_u16() ------------------\n");
|
||||||
|
//bit_print_buffer_u8(u8, 1024/2, 16, 1);
|
||||||
|
|
||||||
|
printf("bit_print_buffer_u32() ------------------\n");
|
||||||
|
bit_print_buffer_u32(u32, 1024/4, 1, 1);
|
||||||
|
|
||||||
|
free(u8);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user