Stuff, again

This commit is contained in:
Julian Barathieu 2019-01-01 17:11:30 +01:00
parent ec0045a566
commit bea3e8a927
14 changed files with 305 additions and 77 deletions

View File

@ -67,21 +67,25 @@ LOBJDIR=$(OBJDIR)/$(LINXDIR)
COMMDEPS=$(INCDIR)/kaleid.h $(INCDIR)/kaldefs.h $(INCDIR)/kaltypes.h \
$(INCDIR)/kalmask.h $(INCDIR)/kalmask.h $(INCDIR)/kalassrt.h
COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c \
$(COMMDIR)/convert.c $(COMMDIR)/memory.c
COMMSRCS=$(COMMDIR)/string.c $(COMMDIR)/status.c $(COMMDIR)/rand.c \
$(COMMDIR)/convert.c $(COMMDIR)/memory.c $(COMMDIR)/arith.c
COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o \
$(COBJDIR)/convert.o $(COBJDIR)/memory.o
COMMOBJS=$(COBJDIR)/string.o $(COBJDIR)/status.o $(COBJDIR)/rand.o \
$(COBJDIR)/convert.o $(COBJDIR)/memory.o $(COBJDIR)/arith.o
common: $(COMMDEPS) $(COMMSRCS)
$(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o
$(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o
$(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o
$(KCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o
$(KCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o
$(KCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o
$(KCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o
$(KCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o
$(KCC) -c $(COMMDIR)/convert.c -o $(COBJDIR)/convert.o
CCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES)
test-common: $(COMMSRCS)
tests: $(COMMSRCS)
$(CCC) -c $(COMMDIR)/rand.c -o $(COBJDIR)/rand.o
$(CCC) -c $(COMMDIR)/arith.c -o $(COBJDIR)/arith.o
$(CCC) -c $(COMMDIR)/string.c -o $(COBJDIR)/string.o
$(CCC) -c $(COMMDIR)/status.c -o $(COBJDIR)/status.o
$(CCC) -c $(COMMDIR)/memory.c -o $(COBJDIR)/memory.o

53
src/kaleid/common/arith.c Normal file
View File

@ -0,0 +1,53 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Arithmetical functions //
//----------------------------------------------------------------------------//
// do not mask anything
#define _KALMASK_H
#include <kaleid.h>
int _osk_abs(int x)
{
return abs(x);
}
long _osk_labs(long x)
{
return labs(x);
}
int _osk_min(int x, int y)
{
return min(x, y);
}
long _osk_lmin(long x, long y)
{
return lmin(x, y);
}
int _osk_max(int x, int y)
{
return max(x, y);
}
long _osk_lmax(long x, long y)
{
return lmax(x, y);
}
div_t _osk_div(int x, int y)
{
return div(x, y);
}
ldiv_t _osk_ldiv(long x, long y)
{
return ldiv(x, y);
}

View File

@ -23,7 +23,7 @@ static inline void *_memset_internal(void *ptr, ulong uval, size_t qwords)
// aligned memory write
for (n = 0; n < qwords; n++) {
*(uptr + n) = uval;
*uptr++ = uval;
}
return ptr;
@ -54,7 +54,7 @@ void *memset(void *ptr, int val, size_t bytes)
_memset_internal(uptr, uval, qwords);
uptr += qwords * QWORD_SIZE;
uptr = (uchar *) ( (ulong)uptr / (qwords * QWORD_SIZE) );
bytes %= QWORD_SIZE;
}
@ -113,6 +113,18 @@ void *memsetw(void *ptr, int val, size_t words)
return ptr;
}
//
// Set "dwords"-many dwords starting from ptr to val
// XXX unimplemented
//
void *memsetd(void *ptr, int val, size_t dwords)
{
(void)val;
(void)dwords;
return ptr;
}
//
// Set "qwords"-many qwords starting from ptr to val
//

1
src/kaleid/common/rand.c Normal file
View File

@ -0,0 +1 @@
int rand(void) { /*STUB*/ return 0; }

View File

@ -10,7 +10,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <kaleid.h>
void *_osk_memsetw(void *, int, long);
int main(int argc, char *argv[])
{
@ -32,7 +32,7 @@ int main(int argc, char *argv[])
short *xxx = (short *)malloc(sizex * sizeof(short));
//printf("%ld\n",(ulong)xxx%8);
//memzero(xxx, sizex);
memsetw(xxx, 300, sizex);
_osk_memsetw(xxx, 300, sizex);
size_t it;
for (it = 0; it < sizex; it++) {

View File

@ -0,0 +1,23 @@
// random test file
#include <stdio.h>
int main(int argc, char *argv[])
{
long x[4];
long *ptr = &x[0];
printf("%p\n", ptr);
ptr++;
printf("%p\n", ptr);
ptr = ptr + 1;
printf("%p\n", ptr);
ptr = (long *)((long)ptr + 1);
printf("%p\n", ptr);
return 0;
}

View File

@ -11,11 +11,11 @@
#define _KALASSRT_H
//------------------------------------------//
// Useful macros //
// Macros //
//------------------------------------------//
#ifndef noreturn
#define noreturn __attribute__((noreturn))
#define noreturn __attribute__((__noreturn__))
#endif
#ifndef unlikely(x)
@ -39,22 +39,6 @@ noreturn void _assert_handler(const char *, const char *, int, const char *);
_assert_handler(#x, __FILE__, __LINE__, __func__); \
} while (0);
//
// Aliases
//
#ifndef Assert
#define Assert assert
#endif
#ifndef DosAssert
#define DosAssert assert
#endif
#ifndef KalAssert
#define KalAssert assert
#endif
//------------------------------------------//
// When not debugging //
//------------------------------------------//
@ -75,6 +59,18 @@ noreturn void _assert_handler(const char *, const char *, int, const char *);
#endif
//------------------------------------------//
// Aliases for assert() //
//------------------------------------------//
#ifndef Assert
#define Assert assert
#endif
#ifndef KalAssert
#define KalAssert assert
#endif
//------------------------------------------//
// End of <kalassrt.h> //
//------------------------------------------//

View File

@ -19,14 +19,24 @@
typedef unsigned long size_t;
#endif
#ifndef __status_t
#define __status_t
typedef signed long status_t;
#endif
#ifndef __va_list
#define __va_list
typedef __builtin_va_list va_list;
#endif
#ifndef __status_t
#define __status_t
typedef signed long status_t;
#ifndef __div_t
#define __div_t
typedef struct { int quot, rem; } div_t;
#endif
#ifndef __ldiv_t
#define __ldiv_t
typedef struct { long quot, rem; } ldiv_t;
#endif
//------------------------------------------//
@ -45,6 +55,10 @@ typedef signed long status_t;
#define va_start __builtin_va_start
#endif
#ifndef va_arg
#define va_arg __builtin_va_arg
#endif
#ifndef va_next
#define va_next __builtin_va_next
#endif
@ -57,15 +71,29 @@ typedef signed long status_t;
// Memory management utilities //
//------------------------------------------//
#if !defined(memset) && !defined(_KALMASK_H)
# define memset memsetb
#ifndef memset
#define memset memsetb
#endif
#ifndef memchr
#define memchr memchrb
#endif
void *memsetb(void *, int, size_t);
void *memsetw(void *, int, size_t);
void *memsetd(void *, int, size_t);
void *memsetq(void *, long, size_t);
void *memchrb(const void *, int, size_t);
void *memchrw(const void *, int, size_t);
void *memchrd(const void *, int, size_t);
void *memchrq(const void *, long, size_t);
void *memcpy(void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memzero(void *, size_t);
int memcmp(const void *, const void *, size_t);
//------------------------------------------//
// String manipulation utilities //
@ -86,7 +114,24 @@ int vsnprintf(char *, size_t, const char *, va_list);
// Type conversion utilities //
//------------------------------------------//
char *itoa(int, char *, int);
int *atoi(const char *);
long *atol(const char *);
char *itoa(int, char *, int);
char *ltoa(long, char *, int);
char *utoa(unsigned int, char *, int);
char *ultoa(unsigned long, char *, int);
long strtol(const char *, char **, int);
unsigned long strtoul(const char *, char **, int);
//------------------------------------------//
// RNG utilities //
//------------------------------------------//
int rand(void);
void srand(unsigned int);
//------------------------------------------//
// Diverse utilities //
@ -94,6 +139,56 @@ char *itoa(int, char *, int);
const char *describe_status(status_t) _NO_MASK;
//------------------------------------------//
// Arithmetical macros //
//------------------------------------------//
#ifndef abs
#define abs(x) ((x) < 0 ? -x : x)
#endif
#ifndef labs
#define labs(x) ((x) < 0 ? -x : x)
#endif
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef lmin
#define lmin(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef lmax
#define lmax(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef __div
#define __div
static inline div_t div(int __x, int __y)
{
div_t __res;
__res.quot = __x/__y;
__res.rem = __x%__y;
return __res;
}
#endif
#ifndef __ldiv
#define __ldiv
static inline ldiv_t ldiv(long __x, long __y)
{
ldiv_t __res;
__res.quot = __x/__y;
__res.rem = __x%__y;
return __res;
}
#endif
//------------------------------------------//
// End of <kalcrt.h> //
//------------------------------------------//

View File

@ -51,11 +51,11 @@
//------------------------------------------//
#ifndef PACKED
#define PACKED __attribute__((packed))
#define PACKED __attribute__((__packed__))
#endif
#ifndef noreturn
#define noreturn __attribute__((noreturn))
#define noreturn __attribute__((__noreturn__))
#endif
#ifndef alignof

View File

@ -20,6 +20,12 @@
# endif
#endif
#ifndef _OSK_SOURCE
# ifndef _KALMASK_H
# include <kalmask.h>
# endif
#endif
//------------------------------------------//
// Include common part of API //
//------------------------------------------//
@ -36,10 +42,6 @@
#include <kalassrt.h>
#endif
#if defined(_KALMASK_NEEDED) && !defined(_KALMASK_H)
#include <kalmask.h>
#endif
#ifndef _KALCRT_H
#include <kalcrt.h>
#endif

View File

@ -11,36 +11,77 @@
#define _KALMASK_H
//------------------------------------------//
// Not building for OS/K //
//------------------------------------------//
#ifndef _OSK_SOURCE
#define memset _osk_memsetb
#define memchr _osk_memchrb
#define memsetb _osk_memsetb
#define memsetw _osk_memsetw
#define memsetd _osk_memsetd
#define memsetq _osk_memsetq
#define memchrb _osk_memchrb
#define memchrw _osk_memchrw
#define memchrd _osk_memchrd
#define memchrq _osk_memchrq
#define memcpy _osk_memcpy
#define memmove _osk_memmove
#define memcmp _osk_memcmp
#define memzero _osk_memzero
//------------------------------------------//
# define memcpy _osk_memcpy
# define memcmp _osk_memcmp
# define memzero _osk_memzero
#define strlen _osk_strlen
#define strcpy _osk_strcpy
#define strncpy _osk_strncpy
#define strrev _osk_strrev
#define reverse _osk_reverse
#define sprintf _osk_sprintf
#define snprintf _osk_snprintf
#define vsprintf _osk_vsprintf
#define vsnprintf _osk_vsnprintf
//------------------------------------------//
# define strlen _osk_strlen
# define strcpy _osk_strcpy
# define strncpy _osk_strncpy
# define strrev _osk_strrev
# define reverse _osk_reverse
# define sprintf _osk_sprintf
# define snprintf _osk_snprintf
# define vsprintf _osk_vsprintf
# define vsnprintf _osk_vsnprintf
#define atoi _osk_atoi
#define atol _osk_atol
#define itoa _osk_itoa
#define ltoa _osk_ltoa
#define utoa _osk_utoa
#define ultoa _osk_ultoa
#define strtol _osk_strtol
#define strtoul _osk_strtoul
//------------------------------------------//
# define itoa _osk_itoa
# define atoi _osk_atoi
#define rand _osk_rand
#define srand _osk_srand
//------------------------------------------//
#define abs _osk_abs
#define labs _osk_labs
#define min _osk_min
#define lmin _osk_lmin
#define max _osk_max
#define lmax _osk_lmax
#define __div
#define __ldiv
#define div _osk_div
#define ldiv _osk_ldiv
//------------------------------------------//
// End of <kalmask.h> //
//------------------------------------------//
#endif
#endif

View File

@ -67,7 +67,7 @@ status_t ClearTerm(terminal_t *kt)
Assert(kt->kt_init == INITOK);
LockTerm(kt);
ClearTerm_Unlocked(kt);
ClearTermUnlocked(kt);
UnlockTerm(kt);
return SUCCESS;
@ -102,7 +102,7 @@ status_t PutOnTerm(terminal_t *kt, char ch)
Assert(kt->kt_init == INITOK);
LockTerm(kt);
PutOnTerm_Unlocked(kt, ch);
PutOnTermUnlocked(kt, ch);
UnlockTerm(kt);
return SUCCESS;
@ -120,7 +120,7 @@ status_t PrintOnTerm(terminal_t *kt, const char *str)
LockTerm(kt);
while (*str) {
PutOnTerm_Unlocked(kt, *str++);
PutOnTermUnlocked(kt, *str++);
}
UnlockTerm(kt);
@ -138,7 +138,7 @@ status_t PrintOnTerm(terminal_t *kt, const char *str)
// Fill terminal with spaces (UNLOCKED version)
// XXX would '\0' work too?
//
void ClearTerm_Unlocked(terminal_t *kt)
void ClearTermUnlocked(terminal_t *kt)
{
size_t i;
@ -156,7 +156,7 @@ void ClearTerm_Unlocked(terminal_t *kt)
//
// Write a single character on the terminal (UNLOCKED version)
//
void PutOnTerm_Unlocked(terminal_t *kt, char ch)
void PutOnTermUnlocked(terminal_t *kt, char ch)
{
int i;
size_t prev_row;
@ -169,19 +169,20 @@ void PutOnTerm_Unlocked(terminal_t *kt, char ch)
// later in this function we actually do the line feed
else if (ch == '\n') { kt->kt_curr_y = kt->kt_width - 1; }
// tabulations account for 4 spaces
// tabulations account for "TABSIZE" spaces
else if (ch == '\t') {
prev_row = kt->kt_curr_y;
// compiler will optimize this away
for (i = 0; i < TABSIZE; i++) {
// tabulations can't spread over two lines
if (kt->kt_curr_y == prev_row) {
PutOnTerm_Unlocked(kt, ' ');
PutOnTermUnlocked(kt, ' ');
}
}
}
else {
// actually write on the buffer
const size_t offset = ComputeOffset(kt, kt->kt_curr_x, kt->kt_curr_y);
kt->kt_buffer[offset] = ComputeEntry(ch, kt->kt_color);
}
@ -200,10 +201,10 @@ void PutOnTerm_Unlocked(terminal_t *kt, char ch)
//
// Print string on terminal (UNLOCKED version)
//
void PrintOnTerm_Unlocked(terminal_t *kt, const char *str)
void PrintOnTermUnlocked(terminal_t *kt, const char *str)
{
while (*str) {
PutOnTerm_Unlocked(kt, *str++);
PutOnTermUnlocked(kt, *str++);
}
}

View File

@ -49,10 +49,10 @@ status_t PrintOnTerm(terminal_t *, const char *);
status_t ChTermColor(terminal_t *, uchar);
#if defined(_UNLOCKED_IO)
void ClearTerm_Unlocked(terminal_t *);
void PutOnTerm_Unlocked(terminal_t *, char);
void PrintOnTerm_Unlocked(terminal_t *, const char *);
#define ChTermColor_Unlocked(kt, col) ((kt)->kt_color = col)
void ClearTermUnlocked(terminal_t *);
void PutOnTermUnlocked(terminal_t *, char);
void PrintOnTermUnlocked(terminal_t *, const char *);
#define ChTermColorUnlocked(kt, col) ((kt)->kt_color = col)
#endif
#ifndef _NO_DEBUG

View File

@ -44,22 +44,22 @@ void StartPanic(const char *str)
SetKernState(KSTATE_PANIC);
ClearTerm_Unlocked(stdout);
ClearTermUnlocked(stdout);
if (str == NULL) {
str = "(no message given)";
}
if (GetPanicStr()) {
PrintOnTerm_Unlocked(stdout, "double panic!\n");
PrintOnTermUnlocked(stdout, "double panic!\n");
HaltCPU();
}
SetPanicStr(str);
// we cannot lock anything when panicking
PrintOnTerm_Unlocked(stdout, "panic! - ");
PrintOnTerm_Unlocked(stdout, str);
PrintOnTermUnlocked(stdout, "panic! - ");
PrintOnTermUnlocked(stdout, str);
while (TRUE) {
HaltCPU();