Răsfoiți Sursa

2.325u-modules: using 1024x768 now

rain 5 ani în urmă
părinte
comite
c3e371954f

+ 1 - 0
LINES

@@ -13,4 +13,5 @@
 2021-7-04 2.035: 2035 lines, 25 files, rain
 2021-7-04 2.154: 2154 lines, 25 files, rain
 2021-7-04 2.164: 2164 lines, 25 files, rain
+2021-7-08 2.325: 2325 lines, 26 files, rain
 

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 OBJS = boot/bootsect 
-KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o font.o
-KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c 
+KOBJS = init/head.o init/main.o kernel/int.o kernel/trap.o kernel/graphic.o kernel/vsprintf.o font.o 
+KSRCS = init/head.S init/main.c kernel/int.c kernel/trap.S kernel/graphic.c kernel/vsprintf.c 
 
 all: Makefile
 	make -C tools/

+ 4 - 0
README.md

@@ -65,3 +65,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 2.164u-int:  
 &nbsp;&nbsp;&nbsp;&nbsp;now interrupt handlers and head.S can use print now. 
 
+2.325u-modules:  
+&nbsp;&nbsp;&nbsp;&nbsp;now we can use 1024x768 VBE and vsprintf. this is a big change. 
+&nbsp;&nbsp;&nbsp;&nbsp;NOTE that interrupts can also work now, but only divide-error and syscall. 
+

+ 29 - 3
boot/boot.asm

@@ -24,7 +24,12 @@ cyldsneed equ 10
 
 ; yeah, it's really 0x7c00 because I don't want to use other memory. 
 
-BOOTDRIVER equ 0x7c00
+BOOTDRIVER 	equ 0x7c00
+VMODE	 	equ 0x7c01
+SCREENX 	equ 0x7c02
+SCREENY 	equ 0x7c04
+FB_ADDR 	equ 0x7c06
+
 
 _start:
 	xor ax, ax
@@ -46,11 +51,32 @@ _start:
 	call read_it
 
 .set_vga:
-	mov ax, 0x0013
+	; get mode info
+	mov ax, 0x4f01
+	mov cx, 0x4118
+	mov di, 0x7000
+
 	int 0x10
 
-	jmp 0x0000:0x7e00
+die:	jc die
+
+	mov al,[es:di]
+	mov [VMODE], al
+
+	mov ax, [es:di + 18]
+	mov [SCREENX], ax
 
+	mov ax, [es:di + 20]
+	mov [SCREENY], ax
+
+	mov eax, [es:di + 40]
+	mov [FB_ADDR], eax
+
+	mov ax, 0x4f02
+	mov bx, 0x4118
+	int 0x10
+
+	jmp 0x0000:0x7e00
 
 
 print:

+ 18 - 0
boot/loader.asm

@@ -112,6 +112,24 @@ init_prot:
 	mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
 ;	mov dword [0x0000200c], 0x00000000
 
+	; get frame buffer address and map it here. 
+
+	mov eax, [0x7c06]
+
+	; eax &= PAGE_SIZE
+	and eax, ~(1048576 * 2 - 1)
+	or eax, PAGE_KERNEL
+
+	mov dword [0x00002010], eax
+; 	mov dword [0x00002014], 0x00000000
+
+	add eax, 1048576 * 2
+	mov dword [0x00002018], eax
+
+	add eax, 1048576 * 2
+	mov dword [0x00002020], eax
+
+
 .enable_long_mode:
 	lgdt [gdt64_desc]
 

+ 2 - 2
include/arch/io.h

@@ -23,8 +23,8 @@
 
 
 
-#ifndef INCLUDED_IO_H
-#define INCLUDED_IO_H
+#ifndef _IO_H
+#define _IO_H
 
 #include <kernel/types.h>
 

+ 2 - 2
include/arch/sys.h

@@ -23,8 +23,8 @@
 
 
 
-#ifndef INCLUDED_SYS_H
-#define INCLUDED_SYS_H
+#ifndef _SYS_H
+#define _SYS_H
 
 #include <kernel/types.h>
 

+ 0 - 64
include/kernel/args.h

@@ -1,64 +0,0 @@
-/* Copyright (C) 2021 Rain */
-
-/* This file is part of XNIX. */
-
-/* 
-  XNIX is free software: you can redistribute it and/or modify 
-  it under the terms of the GNU General Public License as published by 
-  the Free Software Foundation, either version 3 of the License, or 
-  (at your option) and later version. 
-*/
-
-/*
-  XNIX is distributed in the hope that it will be useful, 
-  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-  GNU General Public License for more details. 
-*/
-
-/*
-  You should have received a copy of the GNU General Public License 
-   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-*/
-
-
-
-
-#ifndef INCLUDED_ARGS_H
-#define INCLUDED_ARGS_H
-
-#include <kernel/types.h>
-#include <arch/sys.h>
-
-/*
- * arguments in 64 bit: 
- * %rdi, %rsi, %rdx, %rcx, %r8, %9, and stack. 
- * */
-
-#define get_arg(type, argno) ({		\
-	__uint64_t __res;			\
-	if (argno < 7) {			\
-		if (argno == 1) 		\
-			__res = get_rdi();	\
-		else if (argno == 2)		\
-			__res = get_rsi();	\
-		else if (argno == 3)		\
-			__res = get_rdx();	\
-		else if (argno == 4)		\
-			__res = get_rcx();	\
-		else if (argno == 5)		\
-			__res = get_r8();	\
-		else if (argno == 6)		\
-			__res = get_r9();	\
-	}					\
-	else {					\
-		argno -= 6;			\
-		__uint64_t *p = (__uint64_t *) 	\
-				get_rsp();	\
-		__res = p[argno];		\
-	}					\
-	(type) __res;				\
-})
-
-#endif
-

+ 52 - 78
include/kernel/errno.h

@@ -1,80 +1,54 @@
-/* Copyright (C) 2021 Rain */
-
-/* This file is part of XNIX. */
-
-/* 
-  XNIX is free software: you can redistribute it and/or modify 
-  it under the terms of the GNU General Public License as published by 
-  the Free Software Foundation, either version 3 of the License, or 
-  (at your option) and later version. 
-*/
-
-/*
-  XNIX is distributed in the hope that it will be useful, 
-  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
-  GNU General Public License for more details. 
-*/
-
-/*
-  You should have received a copy of the GNU General Public License 
-   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
-*/
-
-
-
-
-#ifndef INCLUDED_ERRNO_H
-#define INCLUDED_ERRNO_H
-
-#include <kernel/types.h>
-
-typedef __uint32_t errno_t;
-
-
-/* error in kernel */
-#define E_KERROR 0x20000000
-
-/* error io port */
-#define E_IOERROR 0x00000001 | E_KERROR
-
-/* no enough memory for this call */
-#define E_NOMEM	  0x00000002 | E_KERROR
-
-/* call interrupted by signal */
-#define E_SIGNAL  0x00000003 | E_KERROR
-
-/* kernel not supported */
-#define E_NOSUPP  0x00000004 | E_KERROR
-
-
-/* no permision */
-#define E_DENIED 0x40000000
-
-/* need root to call this */
-#define E_NDROOT 0x00000001 | E_DENIED
-
-/* no io passport */
-#define E_IODND  0x00000002 | E_DENIED
-
-/* need hardware super 0 */
-#define E_HARDSP 0x00000003 | E_DENIED
-
-/* hardware no support */
-#define E_HNOSUP 0x00000004 | E_DENIED
-
-/* user error (caller) */
-#define E_UERROR 0x80000000
-
-
-/* syntax error for this call (invalid argument) */
-#define E_SYNERR 0x00000001 | E_UERROR
-
-/* error because address is invalid */
-#define E_ADDRE  0x00000002 | E_UERROR
-
-/* call out of range */
-#define E_OUTRAG 0x00000003 | E_UERROR
+#ifndef _ERRNO_H
+#define _ERRNO_H
+
+/* yeah, yeah, you know. this copies from linux 0.01 :-) */
+
+/* this is a real joke. */
+
+extern int errno;
+
+typedef int errno_t;
+
+
+#define ERROR		99
+#define EPERM		 1
+#define ENOENT		 2
+#define ESRCH		 3
+#define EINTR		 4
+#define EIO		 5
+#define ENXIO		 6
+#define E2BIG		 7
+#define ENOEXEC		 8
+#define EBADF		 9
+#define ECHILD		10
+#define EAGAIN		11
+#define ENOMEM		12
+#define EACCES		13
+#define EFAULT		14
+#define ENOTBLK		15
+#define EBUSY		16
+#define EEXIST		17
+#define EXDEV		18
+#define ENODEV		19
+#define ENOTDIR		20
+#define EISDIR		21
+#define EINVAL		22
+#define ENFILE		23
+#define EMFILE		24
+#define ENOTTY		25
+#define ETXTBSY		26
+#define EFBIG		27
+#define ENOSPC		28
+#define ESPIPE		29
+#define EROFS		30
+#define EMLINK		31
+#define EPIPE		32
+#define EDOM		33
+#define ERANGE		34
+#define EDEADLK		35
+#define ENAMETOOLONG	36
+#define ENOLCK		37
+#define ENOSYS		38
+#define ENOTEMPTY	39
 
 #endif
-

+ 9 - 9
include/kernel/graphic.h

@@ -24,16 +24,17 @@
 
 
 
-#ifndef INCLUDED_GRAPHIC_H
-#define INCLUDED_GRAPHIC_H
+#ifndef _GRAPHIC_H
+#define _GRAPHIC_H
 
 
 #include <kernel/types.h>
 
 
-void putblock(__uint8_t, int, int, int, int);
-void putc(char, __uint8_t, __uint8_t);
-void print(__uint8_t, __uint8_t, char *);
+void putblock(__uint32_t, int, int, int, int);
+void putc(char, __uint32_t, __uint32_t);
+void print(__uint32_t, __uint32_t, char *);
+int printk(__uint32_t, __uint32_t, char *, ...);
 
 
 /* 
@@ -76,12 +77,11 @@ extern __uint8_t *font_end;
 extern __uint64_t font_size;
 
 
-
-#define FB_ADDR ((char *) 0xa0000)
+#define FB_ADDR ((__uint32_t *) 0x400000)
 
 /* x and y in this vga mode */
-#define VGA_X   320
-#define VGA_Y   200
+#define VGA_X   (bootarg->vga_x)
+#define VGA_Y   (bootarg->vga_y)
 
 /* this is default value */
 

+ 16 - 2
include/kernel/init.h

@@ -24,8 +24,8 @@
 
 
 
-#ifndef INCLUDED_INIT_H
-#define INCLUDED_INIT_H
+#ifndef _INIT_H
+#define _INIT_H
 
 #include <kernel/types.h>
 #include <kernel/errno.h>
@@ -39,6 +39,20 @@
 
 
 
+struct bootarg {
+	__uint8_t boot_driver;
+	__uint8_t vbe_mode;
+	__uint16_t vga_x;
+	__uint16_t vga_y;
+
+	/* yes, just 32 bytes this pointer is */
+	__uint32_t fb_addr;
+} __attribute__((packed));
+
+/* init/main.c */
+extern struct bootarg *bootarg;
+
+
 struct inode_desc {
 	/* this struct is a general descriptor, so we must know
 	 * how big is it. */

+ 2 - 2
include/kernel/types.h

@@ -24,8 +24,8 @@
 
 
 
-#ifndef INCLUDED_TYPES_H
-#define INCLUDED_TYPES_H
+#ifndef _TYPES_H
+#define _TYPES_H
 
 #define NULL (void *) 0
 

+ 17 - 2
include/modules/tty.h → include/kernel/vsprintf.h

@@ -24,7 +24,22 @@
 
 
 
-#ifndef INCLUDED_TTY_H
-#define INCLUDED_TTY_H
+#ifndef _VSPRINTF_H
+#define _VSPRINTF_H
+
+#include <kernel/types.h>
+#include <kernel/errno.h>
+
+#include <stdarg.h>
+
+/* decl that function vsprintf we're going to use */
+
+/* kernel/vsprintf.c */
+
+int vsprintf(char *, char *, va_list);
+int itos(char *, unsigned long, int, int, int);
+
+
 
 #endif
+

+ 2 - 2
include/lib/bitman.h

@@ -24,8 +24,8 @@
 
 
 
-#ifndef INCLUDED_BITMAN_H
-#define INCLUDED_BITMAN_H
+#ifndef _BITMAN_H
+#define _BITMAN_H
 
 struct bitmap_manager {
 	unsigned char *bitmap;

+ 2 - 2
include/lib/bitmap.h

@@ -24,8 +24,8 @@
 
 
 
-#ifndef INCLUDED_BITMAP_H
-#define INCLUDED_BITMAP_H
+#ifndef _BITMAP_H
+#define _BITMAP_H
 
 extern char *bitmap_init(unsigned int size);
 extern void bitmap_set(unsigned char *bitmap, unsigned int pos, unsigned char value);

+ 5 - 2
include/lib/rlib.h

@@ -24,8 +24,11 @@
 
 
 
-#ifndef INCLUDED_RLIB_H
-#define INCLUDED_RLIB_H
+#ifndef _RLIB_H
+#define _RLIB_H
+
+
+/* I think you are saying "xxxx" to me. I hear that. :-( */
 
 #include <lib/bitmap.h>
 #include <lib/bitman.h>

+ 4 - 37
include/lib/string.h

@@ -24,10 +24,12 @@
 
 
 
-#ifndef INCLUDED_STRING_H
-#define INCLUDED_STRING_H
+#ifndef _STRING_H
+#define _STRING_H
 
 
+/* now you're saying loudly... */
+
 
 /* copy src to dest for n bytes */
 #define memcpy(dest, src, n) ({ \
@@ -51,40 +53,5 @@
 })
 
 
-/* set n to src for n words (n must be power of 2) */
-#define memsetw(dest, c, n) ({ \
-	__asm__ ( \
-		"cld\n\t" \
-		"rep stosw\n\t" \
-		:: "c" (n >> 1), "D" (dest), "a" (c) \
-	); \
-	dest; \
-})
-
-
-/* set n to src for n dwords (n must be power of 4) */
-#define memsetl(dest, c, n) ({ \
-	__asm__ ( \
-		"cld\n\t" \
-		"rep stosl\n\t" \
-		:: "c" (n >> 2), "D" (dest), "a" (c) \
-	); \
-	dest; \
-})
-
-
-/* set n to src for n quads (n must be power of 8) */
-#define memsetq(dest, c, n) ({ \
-	__asm__ ( \
-		"cld\n\t" \
-		"rep stosq\n\t" \
-		:: "c" (n >> 3), "D" (dest), "a" (c) \
-	); \
-	dest; \
-})
-
-
-
-
 #endif
 

+ 0 - 1
init/head.S

@@ -89,7 +89,6 @@ rep_set_idt:
 	jnz rep_set_idt
 
 to_init:
-
 	/* we cannot use far-jmp in long mode. */
 
 	/* 

+ 9 - 2
init/main.c

@@ -27,12 +27,17 @@
 #include <kernel/errno.h>
 #include <kernel/types.h>
 
-#include <modules/tty.h>
 #include <kernel/graphic.h>
+#include <kernel/vsprintf.h>
 
 #include <arch/io.h>
 #include <arch/sys.h>
 #include <arch/x86/int.h>
+#include <arch/x86/paging.h>
+
+
+/* we save boot-time arguments at that address where boot was. */
+struct bootarg *bootarg = (struct bootarg *) 0x7c00;
 
 
 /* 
@@ -45,7 +50,9 @@
 int init(void) {
 	init_int();
 
-	print(0x0f, 0x00, "XNIX kernel is starting...\n");
+	print(0x00ffffff, 0x00, "XNIX kernel is starting...\n");
+
+	printk(0x00ffffff, 0x00, "hello, can you see this? %d, and this? %q\n", 123, 0x0123456789abcdef);
 
 	return 0;
 }

+ 1 - 1
kernel/Makefile

@@ -1,5 +1,5 @@
 CFLAGS = -mcmodel=large -fno-builtin -m64 -I ../include/ -Wall -Werror -g
-all: int.o trap.o graphic.o 
+all: int.o trap.o graphic.o vsprintf.o
 
 
 %.o: %.S

+ 40 - 18
kernel/graphic.c

@@ -28,26 +28,40 @@
 #include <kernel/errno.h>
 
 #include <kernel/graphic.h>
+#include <kernel/vsprintf.h>
 
+#include <stdarg.h>
 
-void putblock(__uint8_t color, int xs, int xe, int ys, int ye) {
-	int x, y;
 
-	char *fb = FB_ADDR;
+int printk(__uint32_t fg, __uint32_t bg, char *s, ...) {
+	va_list args;
+	int i;
 
-	for (y = ys; y <= ye; y++) {
-		for (x = xs; x <= xe; x++) 
-			fb[y * VGA_X + x] = color;
-	}
-	
-	return;
+	/* I think 256 is enough in kernel */
+	/* too long, return it. */
+
+	if (s[255]) 
+		return 0;
+
+	static char buffer[256] = {0};
+
+	va_start(args, s);
+
+	/* it maybe overflow, but that's in .data section and we did check. */
+
+	i = vsprintf(buffer, s, args);
+	print(fg, bg, buffer);
+
+	va_end(args);
+
+	return i;
 }
 
 
 static int x = 0, y = 0;
 
 
-void putc(char c, __uint8_t fg, __uint8_t bg) {
+void putc(char c, __uint32_t fg, __uint32_t bg) {
 	if (c == '\n') {
 		/* it's '\n', but we use '\r\n' */
 		x = 0;
@@ -79,23 +93,31 @@ void putc(char c, __uint8_t fg, __uint8_t bg) {
 
 	/* okay, now we put it on the screen */
 
-	int i, j;
-	char *p, d, mask;
+	int i;
+	int mask;
+	char d;
+
+	for (i = 0; i < 16; i++) {
+		__uint32_t *addr;
+		int j;
+
+		addr = FB_ADDR + VGA_X * (y + i) + x;
+		mask = 0x0100;
 
-	for (i = 0; i < psf->height; i++) {
-		p = FB_ADDR + (y + i) * VGA_X + x;
 		d = glyph[i];
 
 		for (j = 0; j < 8; j++) {
-			mask = 0x80 >> j;
+			mask >>= 1;
 
 			if (d & mask) 
-				p[j] = fg;
+				addr[j] = fg;
+			
 			else 
-				p[j] = bg;
+				addr[j] = bg;
 		}
 	}
 
+
 	x += 8;
 
 	if (x >= VGA_X) {
@@ -109,7 +131,7 @@ void putc(char c, __uint8_t fg, __uint8_t bg) {
 }
 
 
-void print(__uint8_t fg, __uint8_t bg, char *s) {
+void print(__uint32_t fg, __uint32_t bg, char *s) {
 	for (; *s; s++) 
 		putc(*s, fg, bg);
 

+ 0 - 2
kernel/int.c

@@ -34,8 +34,6 @@
 #include <arch/io.h>
 #include <arch/sys.h>
 
-#include <modules/tty.h>
-
 
 int init_int(void) {
 	/* init PIC */

+ 182 - 0
kernel/vsprintf.c

@@ -0,0 +1,182 @@
+/* Copyright (C) 2021 Rain */
+
+/* This file is part of XNIX. */
+
+/* 
+  XNIX is free software: you can redistribute it and/or modify 
+  it under the terms of the GNU General Public License as published by 
+  the Free Software Foundation, either version 3 of the License, or 
+  (at your option) and later version. 
+*/
+
+/*
+  XNIX is distributed in the hope that it will be useful, 
+  but WITHOUT ANY WARRANTY; without even the implied warranty of 
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+  GNU General Public License for more details. 
+*/
+
+/*
+  You should have received a copy of the GNU General Public License 
+   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
+*/
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/errno.h>
+#include <kernel/types.h>
+
+#include <kernel/graphic.h>
+#include <kernel/vsprintf.h>
+
+#include <arch/x86/int.h>
+#include <arch/io.h>
+#include <arch/sys.h>
+
+#include <stdarg.h>
+
+
+
+/* 
+ * vsprintf: a function that uses to format strings. 
+ *
+ * buf:  (write only) buffer for output. 
+ * fmt:  formater (read only)
+ * args: arguments
+ *
+ * SUPPORTS:
+ *	STANDARD FORMATERS: 
+ * 		%c: a character
+ * 		%d: a 32-bit 10-based signed   int number. 
+ * 		%l: a 64-bit 10-based signed   long number. 
+ *	 	%u: a 64-bit 10-based unsigned long number. 
+ * 		%x: a 64-bit 16-based unsigned long number. 
+ * 		%o: a 64-bit  8-based unsigned long number. 
+ *
+ *	OTHERS: 
+ *	8 :	%b: a 16-based unsigned char  number. 
+ *	16:	%w: a 16-based unsigned short number. 
+ *	32:	%d: (defined)
+ *	64:	%q: a 16-based unsigned long  number. 
+ * */
+
+int vsprintf(char *buf, char *fmt, va_list args) {
+	/* ri: real index in buf */
+	__uint64_t i, ri;
+	char state, c;
+
+	state = 0;
+
+	for (i = 0, ri = 0; (c = fmt[i]); i++) {
+		if (!state) 
+			if (c == '%') 
+				state = 1;
+			else 
+				buf[ri++] = c;
+
+		else {
+			if (c == 'c') 
+				/* gcc compiler makes a char (that 1 byte) to 
+				 * a int (4 bytes) in stack and registers because
+				 * many registers haven't got a byte-size piece. 
+				 * and sp must be 8-bytes aligned in long-mode, 
+				 * 4-bytes in protected-mode. 
+				 * */
+				buf[ri++] = va_arg(args, int);
+
+			else if (c == 's') {
+				/* it's not so difficult, I think */
+
+				/* get pointer and save in s */
+				char *s = va_arg(args, char *);
+
+				while (*s) {
+					buf[ri++] = *s;
+					s++;
+				}
+			}
+
+			else if (c == 'd') 
+				ri += itos(buf + ri, va_arg(args, int), 10, 1, 0);
+
+			else if (c == 'l') 
+				ri += itos(buf + ri, va_arg(args, long), 10, 1, 0);
+
+			else if (c == 'u') 
+				ri += itos(buf + ri, va_arg(args, unsigned long), 10, 1, 0);
+
+			else if (c == 'x') 
+				ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 0);
+
+			else if (c == 'o') 
+				ri += itos(buf + ri, va_arg(args, unsigned long), 8, 0, 0);
+
+			else if (c == 'b') 
+				ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
+
+			else if (c == 'w') 
+				ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
+
+			else if (c == 'q') 
+				ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 1);
+
+			else {
+				/* unsupported format, we should put '%' on screen too */
+				buf[ri++] = '%';
+				buf[ri++] = c;
+			}
+
+			state = 0;
+		}
+	}
+
+	return ri;
+}
+
+
+
+int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
+	static char digits[] = "0123456789abcdef";
+
+	char tbuf[32];
+	int i, neg, j;
+
+	__uint64_t x;
+
+	if (sign && n < 0) {
+		neg = 1;
+		x = -n;
+	}
+
+	else {
+		neg = 0;
+		x = n;
+	}
+
+	i = 0;
+
+	do {
+		tbuf[i++] = digits[x % base];
+	} while (x /= base);
+
+	if (withzero && (base == 8 || base == 16)) {
+		/* fill zero */
+		int i;
+
+		for (i = 0; i < 32; i++) 
+			if (!tbuf[i]) 
+				tbuf[i] = '0';
+	}
+
+	if (neg) 
+		tbuf[i++] = '-';
+
+	j = i;
+
+	while (--j >= 0) 
+		buffer[i - j - 1] = tbuf[j];
+
+	return i;
+}