1(*
2  $Id: gba_helper.inc 14344 2009-12-06 19:37:58Z Legolas $
3  ------------------------------------------------------------------------------
4  This lib is a raw porting of tonclib library for gba (you can find it at
5  http://user.chem.tue.nl/jakvijn/index.htm).
6
7  As this is a direct port from c, I'm pretty sure that something could not work
8  as you expect. I am even more sure that this code could be written better, so
9  if you think that I have made some mistakes or you have some better
10  implemented functions, let me know [francky74 (at) gmail (dot) com]
11  Enjoy!
12
13  Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
14  (http://www.freepascal.org)
15
16  Copyright (C) 2006  Francesco Lombardi
17
18  This library is free software; you can redistribute it and/or
19  modify it under the terms of the GNU Lesser General Public
20  License as published by the Free Software Foundation; either
21  version 2.1 of the License, or (at your option) any later version.
22
23  This library is distributed in the hope that it will be useful,
24  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  Lesser General Public License for more details.
27
28  You should have received a copy of the GNU Lesser General Public
29  License along with this library; if not, write to the Free Software
30  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
31
32  ------------------------------------------------------------------------------
33
34
35  Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
36  (http://www.freepascal.org)
37
38  Copyright (C) 2006  Francesco Lombardi
39  Check http://sourceforge.net/projects/libndsfpc for updates
40
41  ------------------------------------------------------------------------------
42
43  $Log$
44
45
46*)
47
48
49{$ifdef GBA_INTERFACE}
50{$H+}
51    (*
52procedure memset16(dest: pointer; hw: word; hwcount: dword); cdecl; external;
53procedure memcpy16(dest: pointer; const src: pointer; hwcount: dword); cdecl; external;
54
55procedure memset32(dest: pointer; wd: dword; wcount: dword); cdecl; external;
56procedure memcpy32(dest: pointer; const src: pointer; wcount: dword); cdecl; external;
57
58function printf(format: Pchar; args: array of const): longint; cdecl; external;
59function printf(format: Pchar): longint; cdecl; varargs; external;
60function sprintf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
61function sprintf(s: Pchar; format: Pchar): longint; varargs; cdecl; external;
62function iprintf(format: Pchar; args: array of const): longint; cdecl; external;
63function iprintf(format: Pchar): longint; varargs; cdecl; external;
64function scanf(format: Pchar; args: array of const): longint; cdecl; external;
65function scanf(format: Pchar): longint; cdecl; varargs; external;
66function sscanf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
67function sscanf(s: Pchar; format: Pchar): longint; cdecl; varargs; external;
68
69function malloc(size: integer): pointer; cdecl; external;
70function realloc(ptr: pointer; size: integer): pointer; cdecl; external;
71procedure free(ptr: pointer); cdecl; external;
72function memcpy(dest: pointer; src: pointer; n: integer): pointer; cdecl; external;
73
74
75type
76  _FILE = record
77    firstCluster: cuint;
78    length: cuint;
79    curPos: cuint;
80    curClus: cuint;			// Current cluster to read from
81    curSect: integer;			// Current sector within cluster
82    curByte: integer;			// Current byte within sector
83    readBuffer: array [0..511] of byte;	// Buffer used for unaligned reads
84    appClus: cuint;			// Cluster to append to
85    appSect: integer;			// Sector within cluster for appending
86    appByte: integer;			// Byte within sector for appending
87    read: boolean;	// Can read from file
88    write: boolean;	// Can write to file
89    append: boolean;// Can append to file
90    inUse: boolean;	// This file is open
91    dirEntSector: cuint;	// The sector where the directory entry is stored
92    dirEntOffset: integer;	// The offset within the directory sector
93  end;
94  P_FILE = ^_FILE;
95
96const
97   SEEK_SET = 0;
98   SEEK_CUR = 1;
99   SEEK_END = 2;
100
101function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
102function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
103function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
104function ftell(stream: P_FILE): longint; cdecl; external;
105function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
106function fclose(stream: P_FILE): longint; cdecl; external;
107
108type
109  DIR_ITER = record
110    device: cint;
111    dirStruct: pointer;
112  end;
113  PDIR_ITER = ^DIR_ITER;
114
115  stat = packed record
116    st_dev: qword;
117    __pad1: word;
118    __align_pad1: word;
119    st_ino: dword;
120    st_mode : dword;
121    st_nlink : dword;
122    st_uid : dword;
123    st_gid : dword;
124    st_rdev : qword;
125    __pad2 : word;
126    __align_pad2 : word;
127    st_size : longint;
128    st_blksize : longint;
129    st_blocks : longint;
130    st_atime : longint;
131    __unused1 : dword;
132    st_mtime : longint;
133    __unused2 : dword;
134    st_ctime : longint;
135    __unused3 : dword;
136    __unused4 : dword;
137    __unused5 : dword;
138  end;
139  Pstat = ^stat;
140
141const
142  S_IFMT        = $F000;
143  S_IFDIR       = $4000;
144  S_IFCHR       = $2000;
145  S_IFBLK       = $6000;
146  S_IFREG       = $8000;
147  S_IFIFO       = $1000;
148  S_IFLNK       = $A000;
149  S_IFSOCK      = $C000;
150
151  S_ISUID       = $800;
152  S_ISGID       = $400;
153  S_ISVTX       = $200;
154  S_IREAD       = $100;
155  S_IWRITE      = $80;
156  S_IEXEC       = $40;
157
158function diropen(const path: pchar): PDIR_ITER; cdecl; external;
159function dirreset(dirState: PDIR_ITER): cint; cdecl; external;
160function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): cint; cdecl; external;
161function dirclose(dirState: PDIR_ITER): cint; cdecl; external;
162  *)
163procedure DebugPrint(s: string); assembler; inline;
164function GBAIntToStr(i: integer): string;
165
166{$endif GBA_INTERFACE}
167
168
169{$ifdef GBA_IMPLEMENTATION}
170
171// memory handling routines
172// these are in ASM and optimized; use when possible
173{$l core_asm.o}
174
175procedure DebugPrint(s: string); assembler; inline;
176asm
177  mov r0,s
178  swi #0xff0000
179end['r0'];
180
181function GBAIntToStr(i: integer): string;
182var
183  s: string;
184begin
185  str(i, s);
186  GBAIntToStr := s;
187end;
188
189
190
191{$endif GBA_IMPLEMENTATION}
192