initial "working" version

This should print something on the UART.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2019-01-01 22:10:02 +08:00
parent 049a52022c
commit c864db3eec
33 changed files with 417 additions and 2040 deletions

6
.gitignore vendored
View File

@ -1,4 +1,8 @@
.obj/
.swp
.swo
*~
*~
/foboot.bin
/foboot.dfu
/foboot.ihex
/foboot.elf

View File

@ -28,11 +28,11 @@ CFLAGS = $(ADD_CFLAGS) \
-Wall -Wextra \
-ffunction-sections -fdata-sections -fno-common \
-fomit-frame-pointer -Os \
-flto -ffreestanding -fuse-linker-plugin \
-DGIT_VERSION=u\"$(GIT_VERSION)\" -std=gnu11
CXXFLAGS = $(CFLAGS) -std=c++11 -fno-rtti -fno-exceptions
LFLAGS = $(CFLAGS) $(ADD_LFLAGS) \
-nostartfiles \
-nostdlib \
-Wl,--gc-sections \
-Wl,--no-warn-mismatch \
-Wl,--script=$(LDSCRIPT) \
@ -40,14 +40,14 @@ LFLAGS = $(CFLAGS) $(ADD_LFLAGS) \
OBJ_DIR = .obj
CSOURCES = $(wildcard $(SRC_DIR)/*.c) $(wildcard third_party/libbase/*.c)
CPPSOURCES = $(wildcard $(SRC_DIR)/*.cpp) $(wildcard third_party/libbase/*.cpp)
ASOURCES = $(wildcard $(SRC_DIR)/*.S) $(wildcard third_party/libbase/*.S)
CSOURCES = $(wildcard $(SRC_DIR)/*.c) $(wildcard third_party/libbase/*.c) $(wildcard third_party/*.c)
CPPSOURCES = $(wildcard $(SRC_DIR)/*.cpp) $(wildcard third_party/libbase/*.cpp) $(wildcard third_party/*.cpp)
ASOURCES = $(wildcard $(SRC_DIR)/*.S) $(wildcard third_party/libbase/*.S) $(wildcard third_party/*.S)
COBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(CSOURCES:.c=.o)))
CXXOBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(CPPSOURCES:.cpp=.o)))
AOBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(ASOURCES:.S=.o)))
OBJECTS = $(COBJS) $(CXXOBJS) $(AOBJS)
VPATH = $(SRC_DIR) third_party/libbase
VPATH = $(SRC_DIR) third_party/libbase third_party
QUIET = @

View File

@ -1,6 +0,0 @@
#ifndef __ASSERT_H
#define __ASSERT_H
#define assert(x)
#endif /* __ASSERT_H */

View File

@ -1,63 +0,0 @@
#ifndef __CTYPE_H
#define __CTYPE_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* NOTE! This ctype does not handle EOF like the standard C
* library is required to.
*/
#define _U 0x01 /* upper */
#define _L 0x02 /* lower */
#define _D 0x04 /* digit */
#define _C 0x08 /* cntrl */
#define _P 0x10 /* punct */
#define _S 0x20 /* white space (space/lf/tab) */
#define _X 0x40 /* hex digit */
#define _SP 0x80 /* hard space (0x20) */
extern const unsigned char _ctype[];
#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
#define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
#define isalpha(c) ((__ismask(c)&(_U|_L)) != 0)
#define iscntrl(c) ((__ismask(c)&(_C)) != 0)
#define isdigit(c) ((__ismask(c)&(_D)) != 0)
#define isgraph(c) ((__ismask(c)&(_P|_U|_L|_D)) != 0)
#define islower(c) ((__ismask(c)&(_L)) != 0)
#define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
#define ispunct(c) ((__ismask(c)&(_P)) != 0)
/* Note: isspace() must return false for %NUL-terminator */
#define isspace(c) ((__ismask(c)&(_S)) != 0)
#define isupper(c) ((__ismask(c)&(_U)) != 0)
#define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
#define isascii(c) (((unsigned char)(c))<=0x7f)
#define toascii(c) (((unsigned char)(c))&0x7f)
static inline unsigned char __tolower(unsigned char c)
{
if (isupper(c))
c -= 'A'-'a';
return c;
}
static inline unsigned char __toupper(unsigned char c)
{
if (islower(c))
c -= 'a'-'A';
return c;
}
#define tolower(c) __tolower(c)
#define toupper(c) __toupper(c)
#ifdef __cplusplus
}
#endif
#endif /* __CTYPE_H */

View File

@ -1,30 +0,0 @@
#ifndef __ENDIAN_H
#define __ENDIAN_H
#ifdef __cplusplus
extern "C" {
#endif
#define __LITTLE_ENDIAN 0
#define __BIG_ENDIAN 1
#define __BYTE_ORDER __BIG_ENDIAN
static inline unsigned int le32toh(unsigned int val)
{
return (val & 0xff) << 24 |
(val & 0xff00) << 8 |
(val & 0xff0000) >> 8 |
(val & 0xff000000) >> 24;
}
static inline unsigned short le16toh(unsigned short val)
{
return (val & 0xff) << 8 |
(val & 0xff00) >> 8;
}
#ifdef __cplusplus
}
#endif
#endif /* __ENDIAN_H */

View File

@ -1,261 +0,0 @@
#ifndef __ERRNO_H
#define __ERRNO_H
#ifdef __cplusplus
extern "C" {
#endif
extern int errno;
#define EPERM 1
#define EPERM_STR "Operation not permitted"
#define ENOENT 2
#define ENOENT_STR "No such file or directory"
#define ESRCH 3
#define ESRCH_STR "No such process"
#define EINTR 4
#define EINTR_STR "Interrupted system call"
#define EIO 5
#define EIO_STR "I/O error"
#define ENXIO 6
#define ENXIO_STR "No such device or address"
#define E2BIG 7
#define E2BIG_STR "Arg list too long"
#define ENOEXEC 8
#define ENOEXEC_STR "Exec format error"
#define EBADF 9
#define EBADF_STR "Bad file number"
#define ECHILD 10
#define ECHILD_STR "No child processes"
#define EAGAIN 11
#define EWOULDBLOCK EAGAIN
#define EAGAIN_STR "Try again"
#define ENOMEM 12
#define ENOMEM_STR "Out of memory"
#define EACCES 13
#define EACCES_STR "Permission denied"
#define EFAULT 14
#define EFAULT_STR "Bad address"
#define ENOTBLK 15
#define ENOTBLK_STR "Block device required"
#define EBUSY 16
#define EBUSY_STR "Device or resource busy"
#define EEXIST 17
#define EEXIST_STR "File exists"
#define EXDEV 18
#define EXDEV_STR "Cross-device link"
#define ENODEV 19
#define ENODEV_STR "No such device"
#define ENOTDIR 20
#define ENOTDIR_STR "Not a directory"
#define EISDIR 21
#define EISDIR_STR "Is a directory"
#define EINVAL 22
#define EINVAL_STR "Invalid argument"
#define ENFILE 23
#define ENFILE_STR "File table overflow"
#define EMFILE 24
#define EMFILE_STR "Too many open files"
#define ENOTTY 25
#define ENOTTY_STR "Not a typewriter"
#define ETXTBSY 26
#define ETXTBSY_STR "Text file busy"
#define EFBIG 27
#define EFBIG_STR "File too large"
#define ENOSPC 28
#define ENOSPC_STR "No space left on device"
#define ESPIPE 29
#define ESPIPE_STR "Illegal seek"
#define EROFS 30
#define EROFS_STR "Read-only file system"
#define EMLINK 31
#define EMLINK_STR "Too many links"
#define EPIPE 32
#define EPIPE_STR "Broken pipe"
#define EDOM 33
#define EDOM_STR "Math argument out of domain of func"
#define ERANGE 34
#define ERANGE_STR "Math result not representable"
#define EDEADLK 35
#define EDEADLOCK EDEADLK
#define EDEADLK_STR "Resource deadlock would occur"
#define ENAMETOOLONG 36
#define ENAMETOOLONG_STR "File name too long"
#define ENOLCK 37
#define ENOLCK_STR "No record locks available"
#define ENOSYS 38
#define ENOSYS_STR "Function not implemented"
#define ENOTEMPTY 39
#define ENOTEMPTY_STR "Directory not empty"
#define ELOOP 40
#define ELOOP_STR "Too many symbolic links encountered"
#define ENOMSG 42
#define ENOMSG_STR "No message of desired type"
#define EIDRM 43
#define EIDRM_STR "Identifier removed"
#define ECHRNG 44
#define ECHRNG_STR "Channel number out of range"
#define EL2NSYNC 45
#define EL2NSYNC_STR "Level 2 not synchronized"
#define EL3HLT 46
#define EL3HLT_STR "Level 3 halted"
#define EL3RST 47
#define EL3RST_STR "Level 3 reset"
#define ELNRNG 48
#define ELNRNG_STR "Link number out of range"
#define EUNATCH 49
#define EUNATCH_STR "Protocol driver not attached"
#define ENOCSI 50
#define ENOCSI_STR "No CSI structure available"
#define EL2HLT 51
#define EL2HLT_STR "Level 2 halted"
#define EBADE 52
#define EBADE_STR "Invalid exchange"
#define EBADR 53
#define EBADR_STR "Invalid request descriptor"
#define EXFULL 54
#define EXFULL_STR "Exchange full"
#define ENOANO 55
#define ENOANO_STR "No anode"
#define EBADRQC 56
#define EBADRQC_STR "Invalid request code"
#define EBADSLT 57
#define EBADSLT_STR "Invalid slot"
#define EBFONT 59
#define EBFONT_STR "Bad font file format"
#define ENOSTR 60
#define ENOSTR_STR "Device not a stream"
#define ENODATA 61
#define ENODATA_STR "No data available"
#define ETIME 62
#define ETIME_STR "Timer expired"
#define ENOSR 63
#define ENOSR_STR "Out of streams resources"
#define ENONET 64
#define ENONET_STR "Machine is not on the network"
#define ENOPKG 65
#define ENOPKG_STR "Package not installed"
#define EREMOTE 66
#define EREMOTE_STR "Object is remote"
#define ENOLINK 67
#define ENOLINK_STR "Link has been severed"
#define EADV 68
#define EADV_STR "Advertise error"
#define ESRMNT 69
#define ESRMNT_STR "Srmount error"
#define ECOMM 70
#define ECOMM_STR "Communication error on send"
#define EPROTO 71
#define EPROTO_STR "Protocol error"
#define EMULTIHOP 72
#define EMULTIHOP_STR "Multihop attempted"
#define EDOTDOT 73
#define EDOTDOT_STR "RFS specific error"
#define EBADMSG 74
#define EBADMSG_STR "Not a data message"
#define EOVERFLOW 75
#define EOVERFLOW_STR "Value too large for defined data type"
#define ENOTUNIQ 76
#define ENOTUNIQ_STR "Name not unique on network"
#define EBADFD 77
#define EBADFD_STR "File descriptor in bad state"
#define EREMCHG 78
#define EREMCHG_STR "Remote address changed"
#define ELIBACC 79
#define ELIBACC_STR "Can not access a needed shared library"
#define ELIBBAD 80
#define ELIBBAD_STR "Accessing a corrupted shared library"
#define ELIBSCN 81
#define ELIBSCN_STR ".lib section in a.out corrupted"
#define ELIBMAX 82
#define ELIBMAX_STR "Attempting to link in too many shared libraries"
#define ELIBEXEC 83
#define ELIBEXEC_STR "Cannot exec a shared library directly"
#define EILSEQ 84
#define EILSEQ_STR "Illegal byte sequence"
#define ERESTART 85
#define ERESTART_STR "Interrupted system call should be restarted"
#define ESTRPIPE 86
#define ESTRPIPE_STR "Streams pipe error"
#define EUSERS 87
#define EUSERS_STR "Too many users"
#define ENOTSOCK 88
#define ENOTSOCK_STR "Socket operation on non-socket"
#define EDESTADDRREQ 89
#define EDESTADDRREQ_STR "Destination address required"
#define EMSGSIZE 90
#define EMSGSIZE_STR "Message too long"
#define EPROTOTYPE 91
#define EPROTOTYPE_STR "Protocol wrong type for socket"
#define ENOPROTOOPT 92
#define ENOPROTOOPT_STR "Protocol not available"
#define EPROTONOSUPPORT 93
#define EPROTONOSUPPORT_STR "Protocol not supported"
#define ESOCKTNOSUPPORT 94
#define ESOCKTNOSUPPORT_STR "Socket type not supported"
#define EOPNOTSUPP 95
#define EOPNOTSUPP_STR "Operation not supported on transport endpoint"
#define EPFNOSUPPORT 96
#define EPFNOSUPPORT_STR "Protocol family not supported"
#define EAFNOSUPPORT 97
#define EAFNOSUPPORT_STR "Address family not supported by protocol"
#define EADDRINUSE 98
#define EADDRINUSE_STR "Address already in use"
#define EADDRNOTAVAIL 99
#define EADDRNOTAVAIL_STR "Cannot assign requested address"
#define ENETDOWN 100
#define ENETDOWN_STR "Network is down"
#define ENETUNREACH 101
#define ENETUNREACH_STR "Network is unreachable"
#define ENETRESET 102
#define ENETRESET_STR "Network dropped connection because of reset"
#define ECONNABORTED 103
#define ECONNABORTED_STR "Software caused connection abort"
#define ECONNRESET 104
#define ECONNRESET_STR "Connection reset by peer"
#define ENOBUFS 105
#define ENOBUFS_STR "No buffer space available"
#define EISCONN 106
#define EISCONN_STR "Transport endpoint is already connected"
#define ENOTCONN 107
#define ENOTCONN_STR "Transport endpoint is not connected"
#define ESHUTDOWN 108
#define ESHUTDOWN_STR "Cannot send after transport endpoint shutdown"
#define ETOOMANYREFS 109
#define ETOOMANYREFS_STR "Too many references: cannot splice"
#define ETIMEDOUT 110
#define ETIMEDOUT_STR "Connection timed out"
#define ECONNREFUSED 111
#define ECONNREFUSED_STR "Connection refused"
#define EHOSTDOWN 112
#define EHOSTDOWN_STR "Host is down"
#define EHOSTUNREACH 113
#define EHOSTUNREACH_STR "No route to host"
#define EALREADY 114
#define EALREADY_STR "Operation already in progress"
#define EINPROGRESS 115
#define EINPROGRESS_STR "Operation now in progress"
#define ESTALE 116
#define ESTALE_STR "Stale NFS file handle"
#define EUCLEAN 117
#define EUCLEAN_STR "Structure needs cleaning"
#define ENOTNAM 118
#define ENOTNAM_STR "Not a XENIX named type file"
#define ENAVAIL 119
#define ENAVAIL_STR "No XENIX semaphores available"
#define EISNAM 120
#define EISNAM_STR "Is a named type file"
#define EREMOTEIO 121
#define EREMOTEIO_STR "Remote I/O error"
#define EDQUOT 122
#define EDQUOT_STR "Quota exceeded"
#define ENOMEDIUM 123
#define ENOMEDIUM_STR "No medium found"
#define EMEDIUMTYPE 124
#define EMEDIUMTYPE_STR "Wrong medium type"
#ifdef __cplusplus
}
#endif
#endif /* __ERRNO_H */

View File

@ -1,216 +0,0 @@
/* @(#)fdlibm.h 1.5 04/04/22 */
/*
* ====================================================
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
*
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
/* Sometimes it's necessary to define __LITTLE_ENDIAN explicitly
but these catch some common cases. */
#if defined(i386) || defined(i486) || \
defined(intel) || defined(x86) || defined(i86pc) || \
defined(__alpha) || defined(__osf__)
#define __LITTLE_ENDIAN
#endif
#ifdef __LITTLE_ENDIAN
#define __HI(x) *(1+(int*)&x)
#define __LO(x) *(int*)&x
#define __HIp(x) *(1+(int*)x)
#define __LOp(x) *(int*)x
#else
#define __HI(x) *(int*)&x
#define __LO(x) *(1+(int*)&x)
#define __HIp(x) *(int*)x
#define __LOp(x) *(1+(int*)x)
#endif
#ifdef __STDC__
#define __P(p) p
#else
#define __P(p) ()
#endif
/*
* ANSI/POSIX
*/
extern int signgam;
#define MAXFLOAT ((float)3.40282346638528860e+38)
enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix};
#define _LIB_VERSION_TYPE enum fdversion
#define _LIB_VERSION _fdlib_version
/* if global variable _LIB_VERSION is not desirable, one may
* change the following to be a constant by:
* #define _LIB_VERSION_TYPE const enum version
* In that case, after one initializes the value _LIB_VERSION (see
* s_lib_version.c) during compile time, it cannot be modified
* in the middle of a program
*/
extern _LIB_VERSION_TYPE _LIB_VERSION;
#define _IEEE_ fdlibm_ieee
#define _SVID_ fdlibm_svid
#define _XOPEN_ fdlibm_xopen
#define _POSIX_ fdlibm_posix
struct exception {
int type;
char *name;
double arg1;
double arg2;
double retval;
};
#define HUGE MAXFLOAT
/*
* set X_TLOSS = pi*2**52, which is possibly defined in <values.h>
* (one may replace the following line by "#include <values.h>")
*/
#define X_TLOSS 1.41484755040568800000e+16
#define DOMAIN 1
#define SING 2
#define OVERFLOW 3
#define UNDERFLOW 4
#define TLOSS 5
#define PLOSS 6
/*
* ANSI/POSIX
*/
extern double acos __P((double));
extern double asin __P((double));
extern double atan __P((double));
extern double atan2 __P((double, double));
extern double cos __P((double));
extern double sin __P((double));
extern double tan __P((double));
extern double cosh __P((double));
extern double sinh __P((double));
extern double tanh __P((double));
extern double exp __P((double));
extern double frexp __P((double, int *));
extern double ldexp __P((double, int));
extern double log __P((double));
extern double log10 __P((double));
extern double modf __P((double, double *));
extern double pow __P((double, double));
extern double sqrt __P((double));
extern double ceil __P((double));
extern double fabs __P((double));
extern double floor __P((double));
extern double fmod __P((double, double));
extern double erf __P((double));
extern double erfc __P((double));
extern double gamma __P((double));
extern double hypot __P((double, double));
extern int isnan __P((double));
extern int finite __P((double));
extern double j0 __P((double));
extern double j1 __P((double));
extern double jn __P((int, double));
extern double lgamma __P((double));
extern double y0 __P((double));
extern double y1 __P((double));
extern double yn __P((int, double));
extern double acosh __P((double));
extern double asinh __P((double));
extern double atanh __P((double));
extern double cbrt __P((double));
extern double logb __P((double));
extern double nextafter __P((double, double));
extern double remainder __P((double, double));
#ifdef _SCALB_INT
extern double scalb __P((double, int));
#else
extern double scalb __P((double, double));
#endif
extern int matherr __P((struct exception *));
/*
* IEEE Test Vector
*/
extern double significand __P((double));
/*
* Functions callable from C, intended to support IEEE arithmetic.
*/
extern double copysign __P((double, double));
extern int ilogb __P((double));
extern double rint __P((double));
extern double scalbn __P((double, int));
/*
* BSD math library entry points
*/
extern double expm1 __P((double));
extern double log1p __P((double));
/*
* Reentrant version of gamma & lgamma; passes signgam back by reference
* as the second argument; user must allocate space for signgam.
*/
#ifdef _REENTRANT
extern double gamma_r __P((double, int *));
extern double lgamma_r __P((double, int *));
#endif /* _REENTRANT */
/* ieee style elementary functions */
extern double __ieee754_sqrt __P((double));
extern double __ieee754_acos __P((double));
extern double __ieee754_acosh __P((double));
extern double __ieee754_log __P((double));
extern double __ieee754_atanh __P((double));
extern double __ieee754_asin __P((double));
extern double __ieee754_atan2 __P((double,double));
extern double __ieee754_exp __P((double));
extern double __ieee754_cosh __P((double));
extern double __ieee754_fmod __P((double,double));
extern double __ieee754_pow __P((double,double));
extern double __ieee754_lgamma_r __P((double,int *));
extern double __ieee754_gamma_r __P((double,int *));
extern double __ieee754_lgamma __P((double));
extern double __ieee754_gamma __P((double));
extern double __ieee754_log10 __P((double));
extern double __ieee754_sinh __P((double));
extern double __ieee754_hypot __P((double,double));
extern double __ieee754_j0 __P((double));
extern double __ieee754_j1 __P((double));
extern double __ieee754_y0 __P((double));
extern double __ieee754_y1 __P((double));
extern double __ieee754_jn __P((int,double));
extern double __ieee754_yn __P((int,double));
extern double __ieee754_remainder __P((double,double));
extern int __ieee754_rem_pio2 __P((double,double*));
#ifdef _SCALB_INT
extern double __ieee754_scalb __P((double,int));
#else
extern double __ieee754_scalb __P((double,double));
#endif
/* fdlibm kernel function */
extern double __kernel_standard __P((double,double,int));
extern double __kernel_sin __P((double,double,int));
extern double __kernel_cos __P((double,double));
extern double __kernel_tan __P((double,double,int));
extern int __kernel_rem_pio2 __P((double*,double*,int,int,int,const int*));

View File

@ -1,58 +0,0 @@
#ifndef __FLOAT_H
#define __FLOAT_H
#ifdef __cplusplus
extern "C" {
#endif
#define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
#define FLT_ROUNDS (__builtin_flt_rounds())
#define FLT_RADIX __FLT_RADIX__
#define FLT_MANT_DIG __FLT_MANT_DIG__
#define DBL_MANT_DIG __DBL_MANT_DIG__
#define LDBL_MANT_DIG __LDBL_MANT_DIG__
#define DECIMAL_DIG __DECIMAL_DIG__
#define FLT_DIG __FLT_DIG__
#define DBL_DIG __DBL_DIG__
#define LDBL_DIG __LDBL_DIG__
#define FLT_MIN_EXP __FLT_MIN_EXP__
#define DBL_MIN_EXP __DBL_MIN_EXP__
#define LDBL_MIN_EXP __LDBL_MIN_EXP__
#define FLT_MIN_10_EXP __FLT_MIN_10_EXP__
#define DBL_MIN_10_EXP __DBL_MIN_10_EXP__
#define LDBL_MIN_10_EXP __LDBL_MIN_10_EXP__
#define FLT_MAX_EXP __FLT_MAX_EXP__
#define DBL_MAX_EXP __DBL_MAX_EXP__
#define LDBL_MAX_EXP __LDBL_MAX_EXP__
#define FLT_MAX_10_EXP __FLT_MAX_10_EXP__
#define DBL_MAX_10_EXP __DBL_MAX_10_EXP__
#define LDBL_MAX_10_EXP __LDBL_MAX_10_EXP__
#define FLT_MAX __FLT_MAX__
#define DBL_MAX __DBL_MAX__
#define LDBL_MAX __LDBL_MAX__
#define FLT_EPSILON __FLT_EPSILON__
#define DBL_EPSILON __DBL_EPSILON__
#define LDBL_EPSILON __LDBL_EPSILON__
#define FLT_MIN __FLT_MIN__
#define DBL_MIN __DBL_MIN__
#define LDBL_MIN __LDBL_MIN__
#define FLT_TRUE_MIN __FLT_DENORM_MIN__
#define DBL_TRUE_MIN __DBL_DENORM_MIN__
#define LDBL_TRUE_MIN __LDBL_DENORM_MIN__
#ifdef __cplusplus
}
#endif
#endif /* __FLOAT_H */

View File

@ -1,11 +0,0 @@
#ifndef __HW_ETHMAC_MEM_H
#define __HW_ETHMAC_MEM_H
#include <generated/mem.h>
#define ETHMAC_RX0_BASE ETHMAC_BASE
#define ETHMAC_RX1_BASE (ETHMAC_BASE+0x0800)
#define ETHMAC_TX0_BASE (ETHMAC_BASE+0x1000)
#define ETHMAC_TX1_BASE (ETHMAC_BASE+0x1800)
#endif

View File

@ -1,15 +0,0 @@
#ifndef __ID_H
#define __ID_H
#ifdef __cplusplus
extern "C" {
#endif
#define IDENT_SIZE 256
void get_ident(char *ident);
#ifdef __cplusplus
}
#endif
#endif /* __ID_H */

View File

@ -1,52 +0,0 @@
/* Copyright © 2005-2014 Rich Felker, et al.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __INET_H
#define __INET_H
#include <stdint.h>
static __inline uint16_t __bswap_16(uint16_t __x)
{
return (__x<<8) | (__x>>8);
}
static __inline uint32_t __bswap_32(uint32_t __x)
{
return (__x>>24) | ((__x>>8)&0xff00) | ((__x<<8)&0xff0000) | (__x<<24);
}
static __inline uint64_t __bswap_64(uint64_t __x)
{
return (__bswap_32(__x)+(0ULL<<32)) | __bswap_32(__x>>32);
}
#define bswap_16(x) __bswap_16(x)
#define bswap_32(x) __bswap_32(x)
#define bswap_64(x) __bswap_64(x)
uint16_t htons(uint16_t n);
uint32_t htonl(uint32_t n);
uint16_t ntohs(uint16_t n);
uint32_t ntohl(uint32_t n);
#endif /* __INET_H */

View File

@ -1,26 +0,0 @@
#ifndef __LIMITS_H
#define __LIMITS_H
#ifdef __cplusplus
extern "C" {
#endif
#define ULONG_MAX 0xffffffff
#define UINT_MAX 0xffffffff
#define INT_MIN 0x80000000
#define INT_MAX 0x7fffffff
#define USHRT_MAX 0xffff
#define SHRT_MIN 0x8000
#define SHRT_MAX 0x7fff
#define UCHAR_MAX 0xff
#define CHAR_BIT 8
#ifdef __cplusplus
}
#endif
#endif /* __LIMITS_H */

View File

@ -1,14 +0,0 @@
#ifndef __MATH_H
#define __MATH_H
#ifdef __cplusplus
extern "C" {
#endif
#include <fdlibm/fdlibm.h>
#ifdef __cplusplus
}
#endif
#endif /* __MATH_H */

124
include/printf.h Normal file
View File

@ -0,0 +1,124 @@
/*
File: printf.h
Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
----------------------------------------------------------------------
This library is realy just two files: 'printf.h' and 'printf.c'.
They provide a simple and small (+200 loc) printf functionality to
be used in embedded systems.
I've found them so usefull in debugging that I do not bother with a
debugger at all.
They are distributed in source form, so to use them, just compile them
into your project.
Two printf variants are provided: printf and sprintf.
The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
Zero padding and field width are also supported.
If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
long specifier is also
supported. Note that this will pull in some long math routines (pun intended!)
and thus make your executable noticably longer.
The memory foot print of course depends on the target cpu, compiler and
compiler options, but a rough guestimate (based on a H8S target) is about
1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
Not too bad. Your milage may vary. By hacking the source code you can
get rid of some hunred bytes, I'm sure, but personally I feel the balance of
functionality and flexibility versus code size is close to optimal for
many embedded systems.
To use the printf you need to supply your own character output function,
something like :
void putc ( void* p, char c)
{
while (!SERIAL_PORT_EMPTY) ;
SERIAL_PORT_TX_REGISTER = c;
}
Before you can call printf you need to initialize it to use your
character output function with something like:
init_printf(NULL,putc);
Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
the NULL (or any pointer) you pass into the 'init_printf' will eventually be
passed to your 'putc' routine. This allows you to pass some storage space (or
anything realy) to the character output function, if necessary.
This is not often needed but it was implemented like that because it made
implementing the sprintf function so neat (look at the source code).
The code is re-entrant, except for the 'init_printf' function, so it
is safe to call it from interupts too, although this may result in mixed output.
If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
The printf and sprintf functions are actually macros that translate to
'tfp_printf' and 'tfp_sprintf'. This makes it possible
to use them along with 'stdio.h' printf's in a single source file.
You just need to undef the names before you include the 'stdio.h'.
Note that these are not function like macros, so if you have variables
or struct members with these names, things will explode in your face.
Without variadic macros this is the best we can do to wrap these
fucnction. If it is a problem just give up the macros and use the
functions directly or rename them.
For further details see source code.
regs Kusti, 23.10.2004
*/
#ifndef __TFP_PRINTF__
#define __TFP_PRINTF__
#include <stdarg.h>
void init_printf(void* putp,void (*putf) (void*,char));
void tfp_printf(char *fmt, ...);
void tfp_sprintf(char* s,char *fmt, ...);
void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va);
#define printf tfp_printf
#define sprintf tfp_sprintf
#endif

View File

@ -1,27 +0,0 @@
#ifndef __PTHREAD_H
#define __PTHREAD_H
typedef int pthread_rwlock_t;
#define PTHREAD_RWLOCK_INITIALIZER 0
#ifdef __cplusplus
extern "C" {
#endif
inline int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
{ return 0; }
inline int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
{ return 0; }
inline int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
{ return 0; }
inline int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
{ return 0; }
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{ return 0; }
#ifdef __cplusplus
}
#endif
#endif /* __PTHREAD_H */

View File

@ -1,25 +0,0 @@
#ifndef __STDARG_H
#define __STDARG_H
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#define va_start(v, l) __builtin_va_start((v), l)
#define va_arg(ap, type) __builtin_va_arg((ap), type)
#define va_copy(aq, ap) __builtin_va_copy((aq), (ap))
#define va_end(ap) __builtin_va_end(ap)
#define va_list __builtin_va_list
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
int vsprintf(char *buf, const char *fmt, va_list args);
int vprintf(const char *format, va_list ap);
#ifdef __cplusplus
}
#endif
#endif /* __STDARG_H */

View File

@ -1,8 +0,0 @@
#ifndef __STDBOOL_H
#define __STDBOOL_H
#define bool _Bool
#define true 1
#define false 0
#endif /* __STDBOOL_H */

View File

@ -1,23 +0,0 @@
#ifndef __STDDEF_H
#define __STDDEF_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
typedef unsigned long size_t;
typedef long ptrdiff_t;
#define offsetof(type, member) __builtin_offsetof(type, member)
#ifdef __cplusplus
}
#endif
#endif /* __STDDEF_H */

View File

@ -1,34 +0,0 @@
#ifndef __STDINT_H
#define __STDINT_H
#ifdef __cplusplus
extern "C" {
#endif
typedef int intptr_t;
typedef unsigned int uintptr_t;
typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
typedef long long int64_t;
typedef int int32_t;
typedef short int16_t;
typedef char int8_t;
#define __int_c_join(a, b) a ## b
#define __int_c(v, suffix) __int_c_join(v, suffix)
#define __uint_c(v, suffix) __int_c_join(v##U, suffix)
#define INT64_C(v) __int_c(v, LL)
#define UINT64_C(v) __uint_c(v, LL)
#define INT32_C(v) v
#define UINT32_C(v) v##U
#ifdef __cplusplus
}
#endif
#endif /* __STDINT_H */

View File

@ -1,77 +0,0 @@
#ifndef __STDIO_H
#define __STDIO_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
int putchar(int c);
int puts(const char *s);
int snprintf(char *buf, size_t size, const char *fmt, ...);
int scnprintf(char *buf, size_t size, const char *fmt, ...);
int sprintf(char *buf, const char *fmt, ...);
int printf(const char *fmt, ...);
/* Not sure this belongs here... */
typedef long long loff_t;
typedef long off_t;
typedef int mode_t;
typedef int dev_t;
/*
* Note: this library does not provide FILE operations.
* User code must implement them.
*/
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
#ifndef EOF
#define EOF -1
#endif
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
typedef int FILE;
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
int fprintf(FILE *stream, const char *format, ...);
int fflush(FILE *stream);
FILE *fopen(const char *path, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);
char *fgets(char *s, int size, FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int getc(FILE *stream);
int fputc(int c, FILE *stream);
int ferror(FILE *stream);
int feof(FILE *stream);
int fclose(FILE *fp);
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
#ifdef __cplusplus
}
#endif
#endif /* __STDIO_H */

View File

@ -1,15 +0,0 @@
#ifndef __TIME_H
#define __TIME_H
#ifdef __cplusplus
extern "C" {
#endif
void time_init(void);
int elapsed(int *last_event, int period);
#ifdef __cplusplus
}
#endif
#endif /* __TIME_H */

View File

@ -20,6 +20,7 @@ SECTIONS
_frodata = .;
*(.rodata .rodata.* .gnu.linkonce.r.*)
*(.rodata1)
*(.srodata)
_erodata = .;
} > rom

View File

@ -1,9 +1,43 @@
#include <stdio.h>
#include <printf.h>
#include <uart.h>
#include <irq.h>
void main(int argc, char **argv) {
#include <generated/csr.h>
void isr(void) {
unsigned int irqs;
irqs = irq_pending() & irq_getmask();
if(irqs & (1 << UART_INTERRUPT))
uart_isr();
}
static void rv_putchar(void *ignored, char c) {
(void)ignored;
uart_write(c);
}
static void init(void) {
irq_setmask(0);
irq_setie(1);
uart_init();
init_printf(NULL, rv_putchar);
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
init();
int i = 0;
printf("Hello, world!\n");
while (1) {
printf("10 PRINT HELLO, WORLD\n");
printf("20 GOTO 10\n");
printf("i: %d\n", i++);
}
return 0;
}

View File

@ -1,85 +0,0 @@
#include <uart.h>
#include <console.h>
#include <stdio.h>
#include <stdarg.h>
FILE *stdin, *stdout, *stderr;
static console_write_hook write_hook;
static console_read_hook read_hook;
static console_read_nonblock_hook read_nonblock_hook;
void console_set_write_hook(console_write_hook h)
{
write_hook = h;
}
void console_set_read_hook(console_read_hook r, console_read_nonblock_hook rn)
{
read_hook = r;
read_nonblock_hook = rn;
}
int putchar(int c)
{
uart_write(c);
if(write_hook != NULL)
write_hook(c);
return c;
}
char readchar(void)
{
while(1) {
if(uart_read_nonblock())
return uart_read();
if((read_nonblock_hook != NULL) && read_nonblock_hook())
return read_hook();
}
}
int readchar_nonblock(void)
{
return (uart_read_nonblock()
|| ((read_nonblock_hook != NULL) && read_nonblock_hook()));
}
int puts(const char *s)
{
while(*s) {
putchar(*s);
s++;
}
putchar('\n');
return 1;
}
void putsnonl(const char *s)
{
while(*s) {
putchar(*s);
s++;
}
}
#define PRINTF_BUFFER_SIZE 256
int vprintf(const char *fmt, va_list args)
{
int len;
char outbuf[PRINTF_BUFFER_SIZE];
len = vscnprintf(outbuf, sizeof(outbuf), fmt, args);
outbuf[len] = 0;
putsnonl(outbuf);
return len;
}
int printf(const char *fmt, ...)
{
int len;
va_list args;
va_start(args, fmt);
len = vprintf(fmt, args);
va_end(args);
return len;
}

View File

@ -83,7 +83,8 @@ unsigned int crc32(const unsigned char *buffer, unsigned int len)
}
#else
unsigned int crc32(const unsigned char *message, unsigned int len) {
int i, j;
int j;
unsigned int i;
unsigned int byte, crc, mask;
i = 0;

View File

@ -1,208 +0,0 @@
#include <string.h>
#include <errno.h>
int errno;
/************************************************************************
* Based on: lib/string/lib_strerror.c
*
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
************************************************************************/
struct errno_strmap_s
{
int errnum;
char *str;
};
/* This table maps all error numbers to descriptive strings.
* The only assumption that the code makes with regard to this
* this table is that it is order by error number.
*
* The size of this table is quite large. Its size can be
* reduced by eliminating some of the more obscure error
* strings.
*/
struct errno_strmap_s g_errnomap[] =
{
{ EPERM, EPERM_STR },
{ ENOENT, ENOENT_STR },
{ ESRCH, ESRCH_STR },
{ EINTR, EINTR_STR },
{ EIO, EIO_STR },
{ ENXIO, ENXIO_STR },
{ E2BIG, E2BIG_STR },
{ ENOEXEC, ENOEXEC_STR },
{ EBADF, EBADF_STR },
{ ECHILD, ECHILD_STR },
{ EAGAIN, EAGAIN_STR },
{ ENOMEM, ENOMEM_STR },
{ EACCES, EACCES_STR },
{ EFAULT, EFAULT_STR },
{ ENOTBLK, ENOTBLK_STR },
{ EBUSY, EBUSY_STR },
{ EEXIST, EEXIST_STR },
{ EXDEV, EXDEV_STR },
{ ENODEV, ENODEV_STR },
{ ENOTDIR, ENOTDIR_STR },
{ EISDIR, EISDIR_STR },
{ EINVAL, EINVAL_STR },
{ ENFILE, ENFILE_STR },
{ EMFILE, EMFILE_STR },
{ ENOTTY, ENOTTY_STR },
{ ETXTBSY, ETXTBSY_STR },
{ EFBIG, EFBIG_STR },
{ ENOSPC, ENOSPC_STR },
{ ESPIPE, ESPIPE_STR },
{ EROFS, EROFS_STR },
{ EMLINK, EMLINK_STR },
{ EPIPE, EPIPE_STR },
{ EDOM, EDOM_STR },
{ ERANGE, ERANGE_STR },
{ EDEADLK, EDEADLK_STR },
{ ENAMETOOLONG, ENAMETOOLONG_STR },
{ ENOLCK, ENOLCK_STR },
{ ENOSYS, ENOSYS_STR },
{ ENOTEMPTY, ENOTEMPTY_STR },
{ ELOOP, ELOOP_STR },
{ ENOMSG, ENOMSG_STR },
{ EIDRM, EIDRM_STR },
{ ECHRNG, ECHRNG_STR },
{ EL2NSYNC, EL2NSYNC_STR },
{ EL3HLT, EL3HLT_STR },
{ EL3RST, EL3RST_STR },
{ ELNRNG, ELNRNG_STR },
{ EUNATCH, EUNATCH_STR },
{ ENOCSI, ENOCSI_STR },
{ EL2HLT, EL2HLT_STR },
{ EBADE, EBADE_STR },
{ EBADR, EBADR_STR },
{ EXFULL, EXFULL_STR },
{ ENOANO, ENOANO_STR },
{ EBADRQC, EBADRQC_STR },
{ EBADSLT, EBADSLT_STR },
{ EBFONT, EBFONT_STR },
{ ENOSTR, ENOSTR_STR },
{ ENODATA, ENODATA_STR },
{ ETIME, ETIME_STR },
{ ENOSR, ENOSR_STR },
{ ENONET, ENONET_STR },
{ ENOPKG, ENOPKG_STR },
{ EREMOTE, EREMOTE_STR },
{ ENOLINK, ENOLINK_STR },
{ EADV, EADV_STR },
{ ESRMNT, ESRMNT_STR },
{ ECOMM, ECOMM_STR },
{ EPROTO, EPROTO_STR },
{ EMULTIHOP, EMULTIHOP_STR },
{ EDOTDOT, EDOTDOT_STR },
{ EBADMSG, EBADMSG_STR },
{ EOVERFLOW, EOVERFLOW_STR },
{ ENOTUNIQ, ENOTUNIQ_STR },
{ EBADFD, EBADFD_STR },
{ EREMCHG, EREMCHG_STR },
{ ELIBACC, ELIBACC_STR },
{ ELIBBAD, ELIBBAD_STR },
{ ELIBSCN, ELIBSCN_STR },
{ ELIBMAX, ELIBMAX_STR },
{ ELIBEXEC, ELIBEXEC_STR },
{ EILSEQ, EILSEQ_STR },
{ ERESTART, ERESTART_STR },
{ ESTRPIPE, ESTRPIPE_STR },
{ EUSERS, EUSERS_STR },
{ ENOTSOCK, ENOTSOCK_STR },
{ EDESTADDRREQ, EDESTADDRREQ_STR },
{ EMSGSIZE, EMSGSIZE_STR },
{ EPROTOTYPE, EPROTOTYPE_STR },
{ ENOPROTOOPT, ENOPROTOOPT_STR },
{ EPROTONOSUPPORT, EPROTONOSUPPORT_STR },
{ ESOCKTNOSUPPORT, ESOCKTNOSUPPORT_STR },
{ EOPNOTSUPP, EOPNOTSUPP_STR },
{ EPFNOSUPPORT, EPFNOSUPPORT_STR },
{ EAFNOSUPPORT, EAFNOSUPPORT_STR },
{ EADDRINUSE, EADDRINUSE_STR },
{ EADDRNOTAVAIL, EADDRNOTAVAIL_STR },
{ ENETDOWN, ENETDOWN_STR },
{ ENETUNREACH, ENETUNREACH_STR },
{ ENETRESET, ENETRESET_STR },
{ ECONNABORTED, ECONNABORTED_STR },
{ ECONNRESET, ECONNRESET_STR },
{ ENOBUFS, ENOBUFS_STR },
{ EISCONN, EISCONN_STR },
{ ENOTCONN, ENOTCONN_STR },
{ ESHUTDOWN, ESHUTDOWN_STR },
{ ETOOMANYREFS, ETOOMANYREFS_STR },
{ ETIMEDOUT, ETIMEDOUT_STR },
{ ECONNREFUSED, ECONNREFUSED_STR },
{ EHOSTDOWN, EHOSTDOWN_STR },
{ EHOSTUNREACH, EHOSTUNREACH_STR },
{ EALREADY, EALREADY_STR },
{ EINPROGRESS, EINPROGRESS_STR },
{ ESTALE, ESTALE_STR },
{ EUCLEAN, EUCLEAN_STR },
{ ENOTNAM, ENOTNAM_STR },
{ ENAVAIL, ENAVAIL_STR },
{ EISNAM, EISNAM_STR },
{ EREMOTEIO, EREMOTEIO_STR },
{ EDQUOT, EDQUOT_STR },
{ ENOMEDIUM, ENOMEDIUM_STR },
{ EMEDIUMTYPE, EMEDIUMTYPE_STR }
};
#define NERRNO_STRS (sizeof(g_errnomap) / sizeof(struct errno_strmap_s))
char *strerror(int errnum)
{
int ndxlow = 0;
int ndxhi = NERRNO_STRS - 1;
int ndxmid;
do
{
ndxmid = (ndxlow + ndxhi) >> 1;
if (errnum > g_errnomap[ndxmid].errnum)
{
ndxlow = ndxmid + 1;
}
else if (errnum < g_errnomap[ndxmid].errnum)
{
ndxhi = ndxmid - 1;
}
else
{
return g_errnomap[ndxmid].str;
}
}
while (ndxlow <= ndxhi);
return "Unknown error";
}

View File

@ -1,211 +0,0 @@
#include <generated/csr.h>
#include <stdio.h>
#include <stdarg.h>
void isr(void);
#ifdef __or1k__
#include <hw/flags.h>
#define EXTERNAL_IRQ 0x8
static void emerg_printf(const char *fmt, ...)
{
char buf[512];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
char *p = buf;
while(*p) {
while(uart_txfull_read());
uart_rxtx_write(*p++);
}
}
static char emerg_getc(void)
{
while(uart_rxempty_read());
char c = uart_rxtx_read();
uart_ev_pending_write(UART_EV_RX);
return c;
}
static const char hex[] = "0123456789abcdef";
static void gdb_send(const char *txbuf)
{
unsigned char cksum = 0;
const char *p = txbuf;
while(*p) cksum += *p++;
emerg_printf("+$%s#%c%c", txbuf, hex[cksum >> 4], hex[cksum & 0xf]);
}
static void gdb_recv(char *rxbuf, size_t size)
{
size_t pos = (size_t)-1;
for(;;) {
char c = emerg_getc();
if(c == '$')
pos = 0;
else if(c == '#')
return;
else if(pos < size - 1) {
rxbuf[pos++] = c;
rxbuf[pos] = 0;
}
}
}
static void gdb_stub(unsigned long pc, unsigned long sr,
unsigned long r1, unsigned long *regs)
{
gdb_send("S05");
char buf[385];
for(;;) {
gdb_recv(buf, sizeof(buf));
switch(buf[0]) {
case '?': {
snprintf(buf, sizeof(buf), "S05");
break;
}
case 'g': {
snprintf(buf, sizeof(buf),
"%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
"%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
"%08x%08x%08x",
0, r1, regs[2], regs[3], regs[4], regs[5], regs[6], regs[7],
regs[8], regs[9], regs[10], regs[11], regs[12], regs[13], regs[14], regs[15],
regs[16], regs[17], regs[18], regs[19], regs[20], regs[21], regs[22], regs[23],
regs[24], regs[25], regs[26], regs[27], regs[28], regs[29], regs[30], regs[31],
pc-4, pc, sr);
break;
}
case 'm': {
unsigned long addr, len;
char *endptr = &buf[0];
addr = strtoul(endptr + 1, &endptr, 16);
len = strtoul(endptr + 1, &endptr, 16);
unsigned char *ptr = (unsigned char *)addr;
if(len > sizeof(buf) / 2) len = sizeof(buf) / 2;
for(size_t i = 0; i < len; i++) {
buf[i*2 ] = hex[ptr[i] >> 4];
buf[i*2+1] = hex[ptr[i] & 15];
buf[i*2+2] = 0;
}
break;
}
case 'p': {
unsigned long reg, value;
char *endptr = &buf[0];
reg = strtoul(endptr + 1, &endptr, 16);
if(reg == 0)
value = 0;
else if(reg == 1)
value = r1;
else if(reg >= 2 && reg <= 31)
value = regs[reg];
else if(reg == 33)
value = pc;
else if(reg == 34)
value = sr;
else {
snprintf(buf, sizeof(buf), "E01");
break;
}
snprintf(buf, sizeof(buf), "%08x", value);
break;
}
case 'P': {
unsigned long reg, value;
char *endptr = &buf[0];
reg = strtoul(endptr + 1, &endptr, 16);
value = strtoul(endptr + 1, &endptr, 16);
if(reg == 0)
/* ignore */;
else if(reg == 1)
r1 = value;
else if(reg >= 2 && reg <= 31)
regs[reg] = value;
else if(reg == 33)
pc = value;
else if(reg == 34)
sr = value;
else {
snprintf(buf, sizeof(buf), "E01");
break;
}
snprintf(buf, sizeof(buf), "OK");
break;
}
case 'c': {
if(buf[1] != '\0') {
snprintf(buf, sizeof(buf), "E01");
break;
}
return;
}
default:
snprintf(buf, sizeof(buf), "");
break;
}
do {
gdb_send(buf);
} while(emerg_getc() == '-');
}
}
void exception_handler(unsigned long vect, unsigned long *regs,
unsigned long pc, unsigned long ea, unsigned long sr);
void exception_handler(unsigned long vect, unsigned long *regs,
unsigned long pc, unsigned long ea, unsigned long sr)
{
if(vect == EXTERNAL_IRQ) {
isr();
} else {
emerg_printf("\n *** Unhandled exception %d *** \n", vect);
emerg_printf(" pc %08x sr %08x ea %08x\n",
pc, sr, ea);
unsigned long r1 = (unsigned long)regs + 4*32;
regs -= 2;
emerg_printf(" r0 %08x r1 %08x r2 %08x r3 %08x\n",
0, r1, regs[2], regs[3]);
emerg_printf(" r4 %08x r5 %08x r6 %08x r7 %08x\n",
regs[4], regs[5], regs[6], regs[7]);
emerg_printf(" r8 %08x r9 %08x r10 %08x r11 %08x\n",
regs[8], regs[9], regs[10], regs[11]);
emerg_printf(" r12 %08x r13 %08x r14 %08x r15 %08x\n",
regs[12], regs[13], regs[14], regs[15]);
emerg_printf(" r16 %08x r17 %08x r18 %08x r19 %08x\n",
regs[16], regs[17], regs[18], regs[19]);
emerg_printf(" r20 %08x r21 %08x r22 %08x r23 %08x\n",
regs[20], regs[21], regs[22], regs[23]);
emerg_printf(" r24 %08x r25 %08x r26 %08x r27 %08x\n",
regs[24], regs[25], regs[26], regs[27]);
emerg_printf(" r28 %08x r29 %08x r30 %08x r31 %08x\n",
regs[28], regs[29], regs[30], regs[31]);
emerg_printf(" stack:\n");
unsigned long *sp = (unsigned long *)r1;
for(unsigned long spoff = 0; spoff < 16; spoff += 4) {
emerg_printf(" %08x:", &sp[spoff]);
for(unsigned long spoff2 = 0; spoff2 < 4; spoff2++) {
emerg_printf(" %08x", sp[spoff + spoff2]);
}
emerg_printf("\n");
}
emerg_printf(" waiting for gdb... ");
gdb_stub(pc, sr, r1, regs);
}
}
#endif

View File

@ -1,20 +0,0 @@
#include <generated/csr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <id.h>
void get_ident(char *ident)
{
#ifdef CSR_IDENTIFIER_MEM_BASE
int len, i;
len = MMPTR(CSR_IDENTIFIER_MEM_BASE);
for(i=0;i<len;i++)
ident[i] = MMPTR(CSR_IDENTIFIER_MEM_BASE + 4*i);
ident[i] = 0;
#else
ident[0] = 0;
#endif
}

View File

@ -1,141 +0,0 @@
#include <generated/csr.h>
#if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE)
#include <spiflash.h>
#define PAGE_PROGRAM_CMD 0x02
#define WRDI_CMD 0x04
#define RDSR_CMD 0x05
#define WREN_CMD 0x06
#define SE_CMD 0xd8
#define BITBANG_CLK (1 << 1)
#define BITBANG_CS_N (1 << 2)
#define BITBANG_DQ_INPUT (1 << 3)
#define SR_WIP 1
static void flash_write_byte(unsigned char b);
static void flash_write_addr(unsigned int addr);
static void wait_for_device_ready(void);
#define min(a,b) (a>b?b:a)
static void flash_write_byte(unsigned char b)
{
int i;
spiflash_bitbang_write(0); // ~CS_N ~CLK
for(i = 0; i < 8; i++, b <<= 1) {
spiflash_bitbang_write((b & 0x80) >> 7);
spiflash_bitbang_write(((b & 0x80) >> 7) | BITBANG_CLK);
}
spiflash_bitbang_write(0); // ~CS_N ~CLK
}
static void flash_write_addr(unsigned int addr)
{
int i;
spiflash_bitbang_write(0);
for(i = 0; i < 24; i++, addr <<= 1) {
spiflash_bitbang_write((addr & 0x800000) >> 23);
spiflash_bitbang_write(((addr & 0x800000) >> 23) | BITBANG_CLK);
}
spiflash_bitbang_write(0);
}
static void wait_for_device_ready(void)
{
unsigned char sr;
unsigned char i;
do {
sr = 0;
flash_write_byte(RDSR_CMD);
spiflash_bitbang_write(BITBANG_DQ_INPUT);
for(i = 0; i < 8; i++) {
sr <<= 1;
spiflash_bitbang_write(BITBANG_CLK | BITBANG_DQ_INPUT);
sr |= spiflash_miso_read();
spiflash_bitbang_write(0 | BITBANG_DQ_INPUT);
}
spiflash_bitbang_write(0);
spiflash_bitbang_write(BITBANG_CS_N);
} while(sr & SR_WIP);
}
void erase_flash_sector(unsigned int addr)
{
unsigned int sector_addr = addr & ~(SPIFLASH_SECTOR_SIZE - 1);
spiflash_bitbang_en_write(1);
wait_for_device_ready();
flash_write_byte(WREN_CMD);
spiflash_bitbang_write(BITBANG_CS_N);
flash_write_byte(SE_CMD);
flash_write_addr(sector_addr);
spiflash_bitbang_write(BITBANG_CS_N);
wait_for_device_ready();
spiflash_bitbang_en_write(0);
}
void write_to_flash_page(unsigned int addr, const unsigned char *c, unsigned int len)
{
unsigned int i;
if(len > SPIFLASH_PAGE_SIZE)
len = SPIFLASH_PAGE_SIZE;
spiflash_bitbang_en_write(1);
wait_for_device_ready();
flash_write_byte(WREN_CMD);
spiflash_bitbang_write(BITBANG_CS_N);
flash_write_byte(PAGE_PROGRAM_CMD);
flash_write_addr((unsigned int)addr);
for(i = 0; i < len; i++)
flash_write_byte(*c++);
spiflash_bitbang_write(BITBANG_CS_N);
spiflash_bitbang_write(0);
wait_for_device_ready();
spiflash_bitbang_en_write(0);
}
#define SPIFLASH_PAGE_MASK (SPIFLASH_PAGE_SIZE - 1)
void write_to_flash(unsigned int addr, const unsigned char *c, unsigned int len)
{
unsigned int written = 0;
if(addr & SPIFLASH_PAGE_MASK) {
written = min(SPIFLASH_PAGE_SIZE - (addr & SPIFLASH_PAGE_MASK), len);
write_to_flash_page(addr, c, written);
c += written;
addr += written;
len -= written;
}
while(len > 0) {
written = min(len, SPIFLASH_PAGE_SIZE);
write_to_flash_page(addr, c, written);
c += written;
addr += written;
len -= written;
}
}
#endif /* CSR_SPIFLASH_BASE && SPIFLASH_PAGE_SIZE */

View File

@ -1,61 +0,0 @@
/****************************************************************************
* libc/string/lib_strcasecmp.c
*
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
/****************************************************************************
* Included Files
*****************************************************************************/
#include <string.h>
#include <ctype.h>
/****************************************************************************
* Public Functions
*****************************************************************************/
int strcasecmp(const char *cs, const char *ct)
{
int result;
for (;;)
{
if ((result = (int)toupper(*cs) - (int)toupper(*ct)) != 0 || !*cs)
{
break;
}
cs++;
ct++;
}
return result;
}

View File

@ -1,234 +0,0 @@
/****************************************************************************
* lib/string/lib_strtod.c
* Convert string to double
*
* Copyright (C) 2002 Michael Ringgaard. All rights reserved.
* Copyright (C) 2006-2007 H. Peter Anvin.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
/* These are predefined with GCC, but could be issues for other compilers. If
* not defined, an arbitrary big number is put in for now. These should be
* added to nuttx/compiler for your compiler.
*/
#if !defined(__DBL_MIN_EXP__) || !defined(__DBL_MAX_EXP__)
# ifdef CONFIG_CPP_HAVE_WARNING
# warning "Size of exponent is unknown"
# endif
# undef __DBL_MIN_EXP__
# define __DBL_MIN_EXP__ (-1021)
# undef __DBL_MAX_EXP__
# define __DBL_MAX_EXP__ (1024)
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
static inline int is_real(double x)
{
const double infinite = 1.0/0.0;
return (x < infinite) && (x >= -infinite);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/***************************************************(************************
* Name: strtod
*
* Description:
* Convert a string to a double value
*
****************************************************************************/
double strtod(const char *str, char **endptr)
{
double number;
int exponent;
int negative;
char *p = (char *) str;
double p10;
int n;
int num_digits;
int num_decimals;
const double infinite = 1.0/0.0;
/* Skip leading whitespace */
while (isspace(*p))
{
p++;
}
/* Handle optional sign */
negative = 0;
switch (*p)
{
case '-':
negative = 1; /* Fall through to increment position */
case '+':
p++;
}
number = 0.;
exponent = 0;
num_digits = 0;
num_decimals = 0;
/* Process string of digits */
while (isdigit(*p))
{
number = number * 10. + (*p - '0');
p++;
num_digits++;
}
/* Process decimal part */
if (*p == '.')
{
p++;
while (isdigit(*p))
{
number = number * 10. + (*p - '0');
p++;
num_digits++;
num_decimals++;
}
exponent -= num_decimals;
}
if (num_digits == 0)
{
errno = ERANGE;
return 0.0;
}
/* Correct for sign */
if (negative)
{
number = -number;
}
/* Process an exponent string */
if (*p == 'e' || *p == 'E')
{
/* Handle optional sign */
negative = 0;
switch(*++p)
{
case '-':
negative = 1; /* Fall through to increment pos */
case '+':
p++;
}
/* Process string of digits */
n = 0;
while (isdigit(*p))
{
n = n * 10 + (*p - '0');
p++;
}
if (negative)
{
exponent -= n;
}
else
{
exponent += n;
}
}
if (exponent < __DBL_MIN_EXP__ ||
exponent > __DBL_MAX_EXP__)
{
errno = ERANGE;
return infinite;
}
/* Scale the result */
p10 = 10.;
n = exponent;
if (n < 0) n = -n;
while (n)
{
if (n & 1)
{
if (exponent < 0)
{
number /= p10;
}
else
{
number *= p10;
}
}
n >>= 1;
p10 *= p10;
}
if (!is_real(number))
{
errno = ERANGE;
}
if (endptr)
{
*endptr = p;
}
return number;
}

View File

@ -1,111 +0,0 @@
#include <irq.h>
#include <uart.h>
#ifdef __or1k__
#include <spr-defs.h>
#endif
#if defined (__vexriscv__)
#include <csr-defs.h>
#endif
#include <system.h>
#include <generated/mem.h>
#include <generated/csr.h>
void flush_cpu_icache(void)
{
#if defined (__lm32__)
asm volatile(
"wcsr ICC, r0\n"
"nop\n"
"nop\n"
"nop\n"
"nop\n"
);
#elif defined (__or1k__)
unsigned long iccfgr;
unsigned long cache_set_size;
unsigned long cache_ways;
unsigned long cache_block_size;
unsigned long cache_size;
int i;
iccfgr = mfspr(SPR_ICCFGR);
cache_ways = 1 << (iccfgr & SPR_ICCFGR_NCW);
cache_set_size = 1 << ((iccfgr & SPR_ICCFGR_NCS) >> 3);
cache_block_size = (iccfgr & SPR_ICCFGR_CBS) ? 32 : 16;
cache_size = cache_set_size * cache_ways * cache_block_size;
for (i = 0; i < cache_size; i += cache_block_size)
mtspr(SPR_ICBIR, i);
#elif defined (__picorv32__)
/* no instruction cache */
asm volatile("nop");
#elif defined (__vexriscv__)
asm volatile(
".word(0x400F)\n"
"nop\n"
"nop\n"
"nop\n"
"nop\n"
"nop\n"
);
#elif defined (__minerva__)
/* no instruction cache */
asm volatile("nop");
#else
#error Unsupported architecture
#endif
}
void flush_cpu_dcache(void)
{
#if defined (__lm32__)
asm volatile(
"wcsr DCC, r0\n"
"nop\n"
);
#elif defined (__or1k__)
unsigned long dccfgr;
unsigned long cache_set_size;
unsigned long cache_ways;
unsigned long cache_block_size;
unsigned long cache_size;
int i;
dccfgr = mfspr(SPR_DCCFGR);
cache_ways = 1 << (dccfgr & SPR_ICCFGR_NCW);
cache_set_size = 1 << ((dccfgr & SPR_DCCFGR_NCS) >> 3);
cache_block_size = (dccfgr & SPR_DCCFGR_CBS) ? 32 : 16;
cache_size = cache_set_size * cache_ways * cache_block_size;
for (i = 0; i < cache_size; i += cache_block_size)
mtspr(SPR_DCBIR, i);
#elif defined (__picorv32__)
/* no data cache */
asm volatile("nop");
#elif defined (__vexriscv__)
unsigned long cache_info;
asm volatile ("csrr %0, %1" : "=r"(cache_info) : "i"(CSR_DCACHE_INFO));
unsigned long cache_way_size = cache_info & 0xFFFFF;
unsigned long cache_line_size = (cache_info >> 20) & 0xFFF;
for(register unsigned long idx = 0;idx < cache_way_size;idx += cache_line_size){
asm volatile("mv x10, %0 \n .word(0b01110000000001010101000000001111)"::"r"(idx));
}
#elif defined (__minerva__)
/* no data cache */
asm volatile("nop");
#else
#error Unsupported architecture
#endif
}
#ifdef L2_SIZE
void flush_l2_cache(void)
{
unsigned int i;
for(i=0;i<2*L2_SIZE/4;i++) {
((volatile unsigned int *) MAIN_RAM_BASE)[i];
}
}
#endif

245
third_party/printf.c vendored Normal file
View File

@ -0,0 +1,245 @@
/*
* Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#include "printf.h"
typedef void (*putcf) (void*,char);
static putcf stdout_putf;
static void* stdout_putp;
#ifdef PRINTF_LONG_SUPPORT
static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf)
{
int n=0;
unsigned int d=1;
while (num/d >= base)
d*=base;
while (d!=0) {
int dgt = num / d;
num%=d;
d/=base;
if (n || dgt>0|| d==0) {
*bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
++n;
}
}
*bf=0;
}
static void li2a (long num, char * bf)
{
if (num<0) {
num=-num;
*bf++ = '-';
}
uli2a(num,10,0,bf);
}
#endif
static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
{
int n=0;
unsigned int d=1;
while (num/d >= base)
d*=base;
while (d!=0) {
int dgt = num / d;
num%= d;
d/=base;
if (n || dgt>0 || d==0) {
*bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
++n;
}
}
*bf=0;
}
static void i2a (int num, char * bf)
{
if (num<0) {
num=-num;
*bf++ = '-';
}
ui2a(num,10,0,bf);
}
static int a2d(char ch)
{
if (ch>='0' && ch<='9')
return ch-'0';
else if (ch>='a' && ch<='f')
return ch-'a'+10;
else if (ch>='A' && ch<='F')
return ch-'A'+10;
else return -1;
}
static char a2i(char ch, char** src,int base,int* nump)
{
char* p= *src;
int num=0;
int digit;
while ((digit=a2d(ch))>=0) {
if (digit>base) break;
num=num*base+digit;
ch=*p++;
}
*src=p;
*nump=num;
return ch;
}
static void putchw(void* putp,putcf putf,int n, char z, char* bf)
{
char fc=z? '0' : ' ';
char ch;
char* p=bf;
while (*p++ && n > 0)
n--;
while (n-- > 0)
putf(putp,fc);
while ((ch= *bf++))
putf(putp,ch);
}
void tfp_format(void* putp,putcf putf,char *fmt, va_list va)
{
char bf[12];
char ch;
while ((ch=*(fmt++))) {
if (ch!='%')
putf(putp,ch);
else {
char lz=0;
#ifdef PRINTF_LONG_SUPPORT
char lng=0;
#endif
int w=0;
ch=*(fmt++);
if (ch=='0') {
ch=*(fmt++);
lz=1;
}
if (ch>='0' && ch<='9') {
ch=a2i(ch,&fmt,10,&w);
}
#ifdef PRINTF_LONG_SUPPORT
if (ch=='l') {
ch=*(fmt++);
lng=1;
}
#endif
switch (ch) {
case 0:
goto abort;
case 'u' : {
#ifdef PRINTF_LONG_SUPPORT
if (lng)
uli2a(va_arg(va, unsigned long int),10,0,bf);
else
#endif
ui2a(va_arg(va, unsigned int),10,0,bf);
putchw(putp,putf,w,lz,bf);
break;
}
case 'd' : {
#ifdef PRINTF_LONG_SUPPORT
if (lng)
li2a(va_arg(va, unsigned long int),bf);
else
#endif
i2a(va_arg(va, int),bf);
putchw(putp,putf,w,lz,bf);
break;
}
case 'x': case 'X' :
#ifdef PRINTF_LONG_SUPPORT
if (lng)
uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
else
#endif
ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
putchw(putp,putf,w,lz,bf);
break;
case 'c' :
putf(putp,(char)(va_arg(va, int)));
break;
case 's' :
putchw(putp,putf,w,0,va_arg(va, char*));
break;
case '%' :
putf(putp,ch);
default:
break;
}
}
}
abort:;
}
void init_printf(void* putp,void (*putf) (void*,char))
{
stdout_putf=putf;
stdout_putp=putp;
}
void tfp_printf(char *fmt, ...)
{
va_list va;
va_start(va,fmt);
tfp_format(stdout_putp,stdout_putf,fmt,va);
va_end(va);
}
static void putcp(void* p,char c)
{
*(*((char**)p))++ = c;
}
void tfp_sprintf(char* s,char *fmt, ...)
{
va_list va;
va_start(va,fmt);
tfp_format(&s,putcp,fmt,va);
putcp(&s,0);
va_end(va);
}