summaryrefslogtreecommitdiff
path: root/libpsx/include
diff options
context:
space:
mode:
authorXavi Del Campo <xavi.dcr@tutanota.com>2020-01-31 10:32:23 +0100
committerXavi Del Campo <xavi.dcr@tutanota.com>2020-01-31 10:32:23 +0100
commit7c24e9a9b02b04dcaf9507acb94091ea70a2c02d (patch)
treec28d0748652ad4b4222309e46e6cfc82c0906220 /libpsx/include
parenta2b7b6bb1cc2f4a3258b7b2dbc92399d151f864d (diff)
downloadpsxsdk-7c24e9a9b02b04dcaf9507acb94091ea70a2c02d.tar.gz
Imported pristine psxsdk-20190410 from official repo
Diffstat (limited to 'libpsx/include')
-rw-r--r--libpsx/include/bitstring.h78
-rw-r--r--libpsx/include/ctype.h25
-rw-r--r--libpsx/include/errno.h167
-rw-r--r--libpsx/include/fcntl.h72
-rw-r--r--libpsx/include/inttypes.h68
-rw-r--r--libpsx/include/memcard.h74
-rw-r--r--libpsx/include/psx.h406
-rw-r--r--libpsx/include/psxbios.h215
-rw-r--r--libpsx/include/psxcdrom.h82
-rw-r--r--libpsx/include/psxgpu.h1142
-rw-r--r--libpsx/include/psxgte.h196
-rw-r--r--libpsx/include/psxpad.h178
-rw-r--r--libpsx/include/psxsio.h121
-rw-r--r--libpsx/include/psxspu.h225
-rw-r--r--libpsx/include/psxutil.h20
-rw-r--r--libpsx/include/runexe.h17
-rw-r--r--libpsx/include/search.h14
-rw-r--r--libpsx/include/stdint.h14
-rw-r--r--libpsx/include/stdio.h170
-rw-r--r--libpsx/include/stdlib.h66
-rw-r--r--libpsx/include/string.h64
-rw-r--r--libpsx/include/strings.h37
-rw-r--r--libpsx/include/sys/stat.h19
-rw-r--r--libpsx/include/sys/types.h11
-rw-r--r--libpsx/include/types.h10
-rw-r--r--libpsx/include/unistd.h22
26 files changed, 3513 insertions, 0 deletions
diff --git a/libpsx/include/bitstring.h b/libpsx/include/bitstring.h
new file mode 100644
index 0000000..a9b4c32
--- /dev/null
+++ b/libpsx/include/bitstring.h
@@ -0,0 +1,78 @@
+/**
+ * PSXSDK
+ *
+ * bitstring.h
+ *
+ * Implementation of bitstring.h, a family of macros found in *BSD
+ * to manipulate bit strings
+ */
+
+#ifndef _BITSTRING_H
+#define _BITSTRING_H
+
+#include <stdlib.h>
+
+typedef unsigned char bitstr_t;
+
+#define bitstr_size(nbits) \
+ ( (nbits + 7) >> 3 )
+
+#define bit_alloc(nbits) \
+ calloc( bitstr_size(nbits) , 1)
+
+#define bit_decl(name, nbits) \
+ name[ bitstr_size(nbits) ]
+
+#define bit_clear(name, bit) \
+ name[ bit >> 3 ] &= ~(1 << (bit & 7) )
+
+#define bit_set(name, bit) \
+ name[ bit >> 3] |= 1 << (bit & 7)
+
+#define bit_nclear(name, start, stop) \
+ do{ \
+ int _b__;\
+ for(_b__ = start; _b__ <= stop; _b__++) \
+ bit_clear(name, _b__);\
+ }while(0)
+
+#define bit_nset(name, start, stop) \
+ do{ \
+ int _b__;\
+ for(_b__ = start; _b__ <= stop; _b__++) \
+ bit_set(name, _b__); \
+ }while(0)
+
+#define bit_test(name, bit) \
+ ( name[bit >> 3] & (1 << (bit & 7) ) )
+
+#define bit_ffs(name, nbits, value) \
+ do{\
+ int _b__;\
+ for(_b__ = 0; _b__ < nbits; _b__++) \
+ {\
+ printf("bb = %d\n", _b__);\
+ if( bit_test(name, _b__) ) \
+ {\
+ *(value) = _b__; \
+ break; \
+ }\
+ }\
+ if( _b__ == nbits ) *(value) = -1;\
+ }while(0)
+
+#define bit_ffc(name, nbits, value) \
+ do{\
+ int _b__;\
+ for(_b__ = 0; _b__ < nbits; _b__++) \
+ {\
+ if( !bit_test(name, _b__) ) \
+ {\
+ *(value) = _b__; \
+ break; \
+ }\
+ }\
+ if( _b__ == nbits ) *(value) = -1;\
+ }while(0)
+
+#endif
diff --git a/libpsx/include/ctype.h b/libpsx/include/ctype.h
new file mode 100644
index 0000000..209e175
--- /dev/null
+++ b/libpsx/include/ctype.h
@@ -0,0 +1,25 @@
+/*
+ * ctype.h
+ *
+ * PSXSDK
+ */
+
+#ifndef _CTYPE_H
+#define _CTYPE_H
+
+int isupper(int c);
+int islower(int c);
+int isdigit(int c);
+int isxdigit(int c);
+int isalpha(int c);
+int isalnum(int c);
+int isspace(int c);
+int isprint(int c);
+int isgraph(int c);
+int iscntrl(int c);
+int isblank(int c);
+int toupper(int c);
+int tolower(int c);
+
+#endif
+
diff --git a/libpsx/include/errno.h b/libpsx/include/errno.h
new file mode 100644
index 0000000..cfaa8fd
--- /dev/null
+++ b/libpsx/include/errno.h
@@ -0,0 +1,167 @@
+/*
+ * errno.h
+ *
+ * PSXSDK
+ */
+
+#ifndef _ERRNO_H
+#define _ERRNO_H
+
+// There is no real support for errno, this include exists
+// mostly to make things compile
+
+/*
+ * Declarations from NetBSD's /usr/include/sys/errno.h
+ * BSD license
+ */
+
+#ifndef __IN_LIBPSX
+extern int errno;
+#endif
+
+#define EPERM 1 /* Operation not permitted */
+#define ENOENT 2 /* No such file or directory */
+#define ESRCH 3 /* No such process */
+#define EINTR 4 /* Interrupted system call */
+#define EIO 5 /* Input/output error */
+#define ENXIO 6 /* Device not configured */
+#define E2BIG 7 /* Argument list too long */
+#define ENOEXEC 8 /* Exec format error */
+#define EBADF 9 /* Bad file descriptor */
+#define ECHILD 10 /* No child processes */
+#define EDEADLK 11 /* Resource deadlock avoided */
+ /* 11 was EAGAIN */
+#define ENOMEM 12 /* Cannot allocate memory */
+#define EACCES 13 /* Permission denied */
+#define EFAULT 14 /* Bad address */
+#define ENOTBLK 15 /* Block device required */
+#define EBUSY 16 /* Device busy */
+#define EEXIST 17 /* File exists */
+#define EXDEV 18 /* Cross-device link */
+#define ENODEV 19 /* Operation not supported by device */
+#define ENOTDIR 20 /* Not a directory */
+#define EISDIR 21 /* Is a directory */
+#define EINVAL 22 /* Invalid argument */
+#define ENFILE 23 /* Too many open files in system */
+#define EMFILE 24 /* Too many open files */
+#define ENOTTY 25 /* Inappropriate ioctl for device */
+#define ETXTBSY 26 /* Text file busy */
+#define EFBIG 27 /* File too large */
+#define ENOSPC 28 /* No space left on device */
+#define ESPIPE 29 /* Illegal seek */
+#define EROFS 30 /* Read-only file system */
+#define EMLINK 31 /* Too many links */
+#define EPIPE 32 /* Broken pipe */
+
+/* math software */
+#define EDOM 33 /* Numerical argument out of domain */
+#define ERANGE 34 /* Result too large or too small */
+
+/* non-blocking and interrupt i/o */
+#define EAGAIN 35 /* Resource temporarily unavailable */
+#define EWOULDBLOCK EAGAIN /* Operation would block */
+#define EINPROGRESS 36 /* Operation now in progress */
+#define EALREADY 37 /* Operation already in progress */
+
+/* ipc/network software -- argument errors */
+#define ENOTSOCK 38 /* Socket operation on non-socket */
+#define EDESTADDRREQ 39 /* Destination address required */
+#define EMSGSIZE 40 /* Message too long */
+#define EPROTOTYPE 41 /* Protocol wrong type for socket */
+#define ENOPROTOOPT 42 /* Protocol option not available */
+#define EPROTONOSUPPORT 43 /* Protocol not supported */
+#define ESOCKTNOSUPPORT 44 /* Socket type not supported */
+#define EOPNOTSUPP 45 /* Operation not supported */
+#define EPFNOSUPPORT 46 /* Protocol family not supported */
+#define EAFNOSUPPORT 47 /* Address family not supported by protocol family */
+#define EADDRINUSE 48 /* Address already in use */
+#define EADDRNOTAVAIL 49 /* Can't assign requested address */
+
+/* ipc/network software -- operational errors */
+#define ENETDOWN 50 /* Network is down */
+#define ENETUNREACH 51 /* Network is unreachable */
+#define ENETRESET 52 /* Network dropped connection on reset */
+#define ECONNABORTED 53 /* Software caused connection abort */
+#define ECONNRESET 54 /* Connection reset by peer */
+#define ENOBUFS 55 /* No buffer space available */
+#define EISCONN 56 /* Socket is already connected */
+#define ENOTCONN 57 /* Socket is not connected */
+#define ESHUTDOWN 58 /* Can't send after socket shutdown */
+#define ETOOMANYREFS 59 /* Too many references: can't splice */
+#define ETIMEDOUT 60 /* Operation timed out */
+#define ECONNREFUSED 61 /* Connection refused */
+
+#define ELOOP 62 /* Too many levels of symbolic links */
+#define ENAMETOOLONG 63 /* File name too long */
+
+/* should be rearranged */
+#define EHOSTDOWN 64 /* Host is down */
+#define EHOSTUNREACH 65 /* No route to host */
+#define ENOTEMPTY 66 /* Directory not empty */
+
+/* quotas & mush */
+#define EPROCLIM 67 /* Too many processes */
+#define EUSERS 68 /* Too many users */
+#define EDQUOT 69 /* Disc quota exceeded */
+
+/* Network File System */
+#define ESTALE 70 /* Stale NFS file handle */
+#define EREMOTE 71 /* Too many levels of remote in path */
+#define EBADRPC 72 /* RPC struct is bad */
+#define ERPCMISMATCH 73 /* RPC version wrong */
+#define EPROGUNAVAIL 74 /* RPC prog. not avail */
+#define EPROGMISMATCH 75 /* Program version wrong */
+#define EPROCUNAVAIL 76 /* Bad procedure for program */
+
+#define ENOLCK 77 /* No locks available */
+#define ENOSYS 78 /* Function not implemented */
+
+#define EFTYPE 79 /* Inappropriate file type or format */
+#define EAUTH 80 /* Authentication error */
+#define ENEEDAUTH 81 /* Need authenticator */
+
+/* SystemV IPC */
+#define EIDRM 82 /* Identifier removed */
+#define ENOMSG 83 /* No message of desired type */
+#define EOVERFLOW 84 /* Value too large to be stored in data type */
+
+/* Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 */
+#define EILSEQ 85 /* Illegal byte sequence */
+
+/* From IEEE Std 1003.1-2001 */
+/* Base, Realtime, Threads or Thread Priority Scheduling option errors */
+#define ENOTSUP 86 /* Not supported */
+
+/* Realtime option errors */
+#define ECANCELED 87 /* Operation canceled */
+
+/* Realtime, XSI STREAMS option errors */
+#define EBADMSG 88 /* Bad or Corrupt message */
+
+/* XSI STREAMS option errors */
+#define ENODATA 89 /* No message available */
+#define ENOSR 90 /* No STREAM resources */
+#define ENOSTR 91 /* Not a STREAM */
+#define ETIME 92 /* STREAM ioctl timeout */
+
+/* File system extended attribute errors */
+#define ENOATTR 93 /* Attribute not found */
+
+/* Realtime, XSI STREAMS option errors */
+#define EMULTIHOP 94 /* Multihop attempted */
+#define ENOLINK 95 /* Link has been severed */
+#define EPROTO 96 /* Protocol error */
+
+#define ELAST 96 /* Must equal largest errno */
+
+#ifdef _KERNEL
+/* pseudo-errors returned inside kernel to modify return to process */
+#define EJUSTRETURN -2 /* don't modify regs, just return */
+#define ERESTART -3 /* restart syscall */
+#define EPASSTHROUGH -4 /* ioctl not handled by this layer */
+#define EDUPFD -5 /* Dup given fd */
+#define EMOVEFD -6 /* Move given fd */
+#endif
+
+#endif
+
diff --git a/libpsx/include/fcntl.h b/libpsx/include/fcntl.h
new file mode 100644
index 0000000..d51faa1
--- /dev/null
+++ b/libpsx/include/fcntl.h
@@ -0,0 +1,72 @@
+/*
+ * fcntl.h
+ *
+ * File control
+ *
+ * PSXSDK
+ */
+
+#ifndef _FCNTL_H
+#define _FCNTL_H
+
+#define O_RDONLY 1
+#define O_WRONLY 2
+#define O_NONBLOCK 4
+/** Read locked, not shared. */
+#define O_RDLOCK 16
+/** Write locked, not shared. */
+#define O_WRLOCK 32
+#define O_RDWR (O_RDONLY | O_WRONLY)
+#define O_APPEND 256
+#define O_CREAT 512
+#define O_TRUNC 1024
+/** Obviously, O_EXCL has no effect in the PSXSDK. */
+#define O_EXCL 0
+
+/** The following are PlayStation BIOS extensions. */
+
+/** Set to scanning type. Real purpose unknown. */
+#define O_SCAN 4096
+/** Setup for remote command entry. Real purpose unknown. */
+#define O_RCOM 8192
+/** No buffering and console interrupt */
+#define O_NOBUF 16384
+/** Asynchronous I/O mode */
+#define O_NOWAIT 32768
+/** Asynchronous I/O mode, alias */
+#define O_ASYNC O_NOWAIT
+
+/**
+ * These are standard C library file I/O functions provided by the PSX BIOS.
+ * Filenames have to be specified in this way:
+ * [device]:[filename]
+ *
+ * Where device specifies the device the file is on:
+ * "tty:" Console
+ * "cdrom:" CD-ROM
+ * "buXX:" Memory cards
+ *
+ * When using cdrom: as device, append file version (;1) to filename
+ * Example: cdrom:README.TXT;1
+ *
+ * Subdirectory paths have to be specified with backslashes (\),
+ * like MS-DOS. Read and write operations can be carried only in blocks.
+ * Blocks are 2048 bytes for the CD-ROM device, and 128 bytes for memory cards.
+ */
+
+// The third argument (file mode) makes no sense in PSXSDK,
+// and will be ignored.
+
+#define open(filename, flags, ...) \
+ open(filename, flags)
+
+/** In previous versions, the second argument for open()
+ was named `mode'. That was incorrect; now it is correctly named `flags'.
+*/
+
+int open(const char *filename, int flags);
+int read(int d, void *buf, int nbytes);
+int close(int d);
+int lseek(int fildes, int offset, int whence);
+
+#endif
diff --git a/libpsx/include/inttypes.h b/libpsx/include/inttypes.h
new file mode 100644
index 0000000..831c6ed
--- /dev/null
+++ b/libpsx/include/inttypes.h
@@ -0,0 +1,68 @@
+/* inttypes.h */
+
+#ifndef _INTTYPES_H
+#define _INTTYPES_H
+
+#include <stdint.h>
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+typedef unsigned long long uint64_t;
+
+typedef unsigned char uint_least8_t;
+typedef unsigned short uint_least16_t;
+typedef unsigned int uint_least32_t;
+typedef unsigned long long uint_least64_t;
+
+typedef unsigned char uint_fast8_t;
+typedef unsigned short uint_fast16_t;
+typedef unsigned int uint_fast32_t;
+typedef unsigned long long uint_fast64_t;
+
+typedef unsigned long long uintmax_t;
+typedef unsigned int uintptr_t;
+
+typedef signed char int8_t;
+typedef short int16_t;
+typedef int int32_t;
+typedef long long int64_t;
+
+typedef signed char int_least8_t;
+typedef short int_least16_t;
+typedef int int_least32_t;
+typedef long long int_least64_t;
+
+typedef char int_fast8_t;
+typedef short int_fast16_t;
+typedef int int_fast32_t;
+typedef long long int_fast64_t;
+
+typedef long long intmax_t;
+
+typedef long intptr_t;
+
+#define PRIu8 "hhu"
+#define PRIu16 "hu"
+#define PRIu32 "u"
+#define PRIu64 "llu"
+
+#define PRIs8 "hhd"
+#define PRIs16 "hd"
+#define PRIs32 "d"
+#define PRIs64 "lld"
+
+#define PRIuLEAST8 "hhu"
+#define PRIuLEAST16 "hu"
+#define PRIuLEAST32 "u"
+#define PRIuLEAST64 "llu"
+
+#define PRIuFAST8 "hhu"
+#define PRIuFAST16 "hu"
+#define PRIuFAST32 "u"
+#define PRIuFAST64 "llu"
+
+#define PRIuMAX "llu"
+#define PRIuPTR "lu"
+
+#endif
diff --git a/libpsx/include/memcard.h b/libpsx/include/memcard.h
new file mode 100644
index 0000000..11b443f
--- /dev/null
+++ b/libpsx/include/memcard.h
@@ -0,0 +1,74 @@
+#ifndef _PSXSDK_MEMCARD_H
+#define _PSXSDK_MEMCARD_H
+
+/*Card connection related*/
+enum memcard_status
+{
+ /** Memory card is connected */
+ MEMCARD_CONNECTED = 1,
+ /** Memory card is formatted */
+ MEMCARD_FORMATTED = 2
+};
+
+/**
+ * Results of memory card operations
+ */
+
+enum memcard_operation_result
+{
+ /** Operation was successful - good result */
+ MEMCARD_RW_GOOD = 0x47,
+ /** Bad checksum - bad result */
+ MEMCARD_RW_BADCHECK = 0x4E,
+ /** Bad sector - bad result */
+ MEMCARD_RW_BADSECTOR = 0xFF
+};
+
+/*Card block type*/
+/*#define BLOCK_FORMATTED 0x000000A0
+#define BLOCK_INITIAL 0x00000051
+#define BLOCK_MIDLINK 0x00000052
+#define BLOCK_ENDLINK 0x00000053
+#define BLOCK_DEL_INITIAL 0x000000A1
+#define BLOCK_DEL_MIDLINK 0x000000A2
+#define BLOCK_DEL_ENDLINK 0x000000A3*/
+
+/*128 byte card directory*/
+/*typedef struct{
+ long BlockType;
+ long FileSize;
+ short NextBlock;
+ char FileName[20];
+ char Reserved[97];
+ char XorChecksum;
+}CARDDIR;*/
+
+/**
+ * Reads a 128-byte sector from a memory card.
+ * @param card_slot Memory card slot (0 = first slot, 1 = second slot)
+ * @param sector Sector number (0-511)
+ * @param buffer Pointer to data buffer in which data will be stored
+ * @return Result of operation (possible values in memcard_operation_result enum)
+ */
+
+unsigned char McReadSector(int card_slot, int sector, unsigned char *buffer);
+
+/**
+ * Writes a 128-byte sector to a memory card.
+ * @param card_slot Memory card slot (0 = first slot, 1 = second slot)
+ * @param sector Sector number (0-511)
+ * @param buffer Pointer to data buffer containing data to write
+ * @return Result of operation (possible values in memcard_operation_result enum)
+ */
+
+unsigned char McWriteSector(int card_slot, int sector, unsigned char *buffer);
+
+/**
+ * Get memory card status
+ * @param card_slot Memory card slot (0 = first slot, 1 = second slot)
+ * @return Bitmask for current memory card status (flags in memcard_status enum)
+ */
+
+unsigned int McGetStatus(int card_slot);
+
+#endif
diff --git a/libpsx/include/psx.h b/libpsx/include/psx.h
new file mode 100644
index 0000000..881291b
--- /dev/null
+++ b/libpsx/include/psx.h
@@ -0,0 +1,406 @@
+/*
+ * PSXSDK Library include
+ */
+
+#ifndef _PSX_H
+#define _PSX_H
+
+#include <stdarg.h>
+
+#ifndef true
+#define true 1
+#endif
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef false
+#define false 0
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#define IPENDING *((volatile unsigned int*)0x1f801070)
+#define IMASK *((volatile unsigned int*)0x1f801074)
+#define RCNT_COUNT(x) *((volatile unsigned int*)(0x1f801100 + (x<<4)))
+#define RCNT_MODE(x) *((volatile unsigned int*)(0x1f801104 + (x<<4)))
+#define RCNT_TARGET(x) *((volatile unsigned int*)(0x1f801108 + (x<<4)))
+
+/**
+ * PSXSDK version information in hexadecimal format.
+ *
+ * An explanation of version numbers and how they relate to
+ * releases:
+ * + PSXSDK_VERSION undefined
+ * - PSXSDK 0.1
+ * + 0.1.99 (0x0199)
+ * - From PSXSDK releases 2012-03-03 up to 2013-01-14.
+ * + 0.2.99 (0x0299)
+ * - PSXSDK 2013-05-14
+ * + 0.3.99 (0x0399)
+ * - PSXSDK 2013-11-09
+ * + 0.4.99 (0x0499)
+ * - PSXSDK 2014-04-22
+ * + 0.5.99 (0x0599)
+ * - PSXSDK 2015-07-29
+ * + 0.6.00 (0x0600)
+ * - PSXSDK 2016-06-03
+ * + 0.6.1 (0x0601)
+ * - PSXSDK 2018-01-10
+ * + 0.6.2 (0x0602)
+ * - PSXSDK 2019-04-10
+ */
+
+#define PSXSDK_VERSION 0x0602
+
+/**
+ * PSXSDK version information in string format
+ */
+
+#define PSXSDK_VERSION_STRING "0.6.2"
+
+/**
+ * PSXSDK version date (BCD YYYY-MM-DD)
+ */
+
+#define PSXSDK_VERSION_DATE 0x20190410
+
+
+/*
+ * Include some GCC builtin includes
+ */
+
+#ifndef _PSXSDK_WRAPPER
+
+#include <psxbios.h>
+
+#endif
+
+#include <psxgpu.h>
+#include <memcard.h>
+
+#include <psxpad.h>
+#include <psxspu.h>
+#include <psxcdrom.h>
+#include <psxsio.h>
+//#include <adpcm.h>
+#include <psxgte.h>
+
+/**
+ * Scratch pad - unused data cache, that can be used as "fast RAM"
+ */
+
+extern unsigned char __scratchpad[1024];
+
+/**
+ * Coprocessor 0 register numbers
+ */
+
+enum cop0_register_numbers
+{
+ /** Contains the last invalid program address which caused a trap.
+
+ It is set by address errors of all kinds. */
+ COP0_BADVADDR = 8,
+ /** CPU mode flags (status register) */
+ COP0_SR = 12,
+ /** Describes the most recently recognized exception. */
+ COP0_CAUSE = 13,
+ /** Return address from trap */
+ COP0_EPC = 14,
+ /** COP0 type and revision level */
+ COP0_PRID = 15,
+};
+
+/**
+ * Root counter specifications
+ */
+
+enum psx_rcnt_specs
+{
+ /** Pixel clock*/
+ RCntCNT0 = 0xf2000000,
+ /** Horizontal sync*/
+ RCntCNT1 = 0xf2000001,
+ /** System clock / 8 */
+ RCntCNT2 = 0xf2000002,
+ /** VSync (VBlank) */
+ RCntCNT3 = 0xf2000003,
+};
+
+/**
+ * Root counter modes
+ */
+
+enum psx_rcnt_modes
+{
+ /** Interrupt mode */
+ RCntIntr = 0x1000,
+ /** Ignore target and count to 65535 (hex: 0xFFFF) */
+ RCntNotar = 0x0100,
+ /** Timer stop mode */
+ RCntStop = 0x0010,
+ /** System Clock mode */
+ RCntSC = 0x0001,
+};
+
+struct psx_info
+{
+ struct kernel
+ {
+ const char *version; // Kernel version
+ int year; // Kernel year
+ int month; // Kernel month
+ int day; // Kernel day
+ }kernel;
+
+ struct system
+ {
+ int memory; // RAM memory size
+ }system;
+};
+
+/**
+ * Initialize library
+ */
+
+void PSX_Init(void);
+
+/**
+ * Flags for PSX_Init.
+ */
+
+enum psx_init_flags
+{
+ /** PSX_INIT_CD - Initialize CDROM filesystem */
+ PSX_INIT_CD = 1,
+ /** PSX_INIT_SAVESTATE - Save BIOS state before initializing the library */
+ PSX_INIT_SAVESTATE = 2,
+ /** PSX_INIT_NOBIOS - Remove control from the BIOS and let PSXSDK be in complete control */
+ PSX_INIT_NOBIOS = 4,
+};
+
+/**
+ * Deinitialize library
+ */
+
+void PSX_DeInit(void);
+
+/**
+ * Initialize library (extended version)
+ *
+ * flags contains a bitmask specifying which flags are enabled
+ *
+ * PSX_Init() is the same as PSX_InitEx(PSX_INIT_CD)
+ * @param flags Flag bitmask (flags are to be OR'd)
+ */
+
+void PSX_InitEx(unsigned int flags);
+
+/**
+ * Reads the status of the buttons of the two joypads.
+ *
+ * Takes two pointers for 16-bit bitmasks, one for the first player's joypad,
+ * and one for the second one. Their bits are updated to show which buttons were pressed.
+ *
+ * This function only supplies basic functionality, adequate for a normal digital pad.
+ *
+ * If more advanced functionality is desired, use PSX_PollPad() instead of this function.
+ * @attention Note that some joypads, like the official ones from Sony, do not like to be polled more than
+ * once every 1/60th of a second and if this limitation is not considered the data
+ * they return is undefined. Other joypads do not have this limitation but in any case err on the safe side.
+ * @param padbuf Pointer to 16-bit variable where bitmask for pad #1 will be stored.
+ * If NULL is passed, this argument is ignored.
+ * @param padbuf2 Pointer to 16-bit variable where bitmask for pad #2 will be stored
+ * If NULL is passed, this argument is ignored.
+ */
+void PSX_ReadPad(unsigned short *padbuf, unsigned short *padbuf2);
+
+/**
+ * Polls a joypad for information.
+ * @attention Note that some joypads, like the official ones from Sony, do not like to be polled more than
+ * once every 1/60th of a second and if this limitation is not considered the data
+ * they return is undefined. Other joypads do not have this limitation but in any case err on the safe side.
+ * @param pad_num Number of the pad to poll (0 = pad #1, 1 = pad #2, etc.)
+ * @param pad_state Pointer to a psx_pad_state structure in which to store information for the pad.
+ */
+
+void PSX_PollPad(int pad_num, psx_pad_state *pad_state);
+
+/**
+ * Takes a pointer to a struct psx_info structure, and fills it
+ * with information about the PlayStation on which the program is running.
+ * PS-OS kernel build date and version are reported among other things.
+ * @param info Pointer to struct psx_info structure which will be filled.
+ */
+void PSX_GetSysInfo(struct psx_info *info);
+
+/**
+ * Gets Coprocessor 0 status register
+ * @return Value of Coprocessor 0 status register
+ */
+
+unsigned int get_cop0_status(void);
+
+/**
+ * Sets Coprocessor 0 status register
+ * @param sreg New value of Coprocessor 0 status register
+ * @return [[ check: maybe this is a void function ]]
+ */
+
+unsigned int set_cop0_status(unsigned int sreg);
+
+/**
+ * Gets the contents of the program counter when the
+ * last exception happened.
+ * @return Value of the program counter at the time of the last exception
+ */
+
+unsigned int get_cop0_epc(void);
+
+/**
+ * Get value of specified Coprocessor 0 register
+ * @param register_num Number of Coprocessor 0 register whose value must be retrieved
+ * @return Value of specified Coprocessor 0 register
+ */
+
+unsigned int get_cop0_register(unsigned char register_num);
+
+/**
+ * Set value of specified Coprocessor 0 register
+ * @param register_num Number of Coprocessor 0 register whose value must be set
+ * @param value New value of specified Coprocessor 0 register
+ */
+
+void set_cop0_register(unsigned char register_num, unsigned int value);
+
+/**
+ * Get value of the specified (data) register of a specified coprocessor
+ * @param cop_num Coprocessor number
+ * @param register_num Number of coprocessor register whose value must be retrieved
+ * @return Value of specified coprocessor register
+ */
+
+unsigned int get_cop_register(unsigned char cop_num,
+ unsigned char register_num);
+
+
+/**
+ * Get value of the specified control register of a specified coprocessor
+ * @param cop_num Coprocessor number
+ * @param register_num Number of coprocessor register whose value must be retrieved
+ * @return Value of specified coprocessor register
+ */
+
+unsigned int get_cop_ctrl_register(unsigned char cop_num,
+ unsigned char register_num);
+
+/**
+ * Set value of the specified (data) register of a specified coprocessor
+ * @param cop_num Coprocessor number
+ * @param register_num Number of Coprocessor 0 register whose value must be set
+ * @param value New value of specified Coprocessor 0 register
+ */
+
+ void set_cop_register(unsigned char cop_num,
+ unsigned char register_num, unsigned int value);
+
+/**
+ * Set value of the specified control register of a specified coprocessor
+ * @param cop_num Coprocessor number
+ * @param register_num Number of Coprocessor 0 register whose value must be set
+ * @param value New value of specified Coprocessor 0 register
+ */
+
+ void set_cop_ctrl_register(unsigned char cop_num,
+ unsigned char register_num, unsigned int value);
+
+/**
+ * Make the specified coprocessor run the specified instruction
+ * @param operation Operation number for the instruction
+ */
+
+void run_cop_instruction(unsigned char cop_num,
+ unsigned int operation);
+
+// Root counter functions
+
+/**
+ * Set root counter (documentation TO DO)
+ * @param spec Spec
+ * @param target Target
+ * @param mode mode
+ */
+
+int SetRCnt(int spec, unsigned short target, unsigned int mode);
+
+/**
+ * Get root counter (documentation TO DO)
+ * @param spec Spec
+ * @return TO DO
+ */
+
+int GetRCnt(int spec);
+
+/**
+ * Start root counter (documentation TO DO)
+ * @param spec Spec
+ * @return TO DO
+ */
+
+int StartRCnt(int spec);
+
+/**
+ * Stop root counter (documentation TO DO)
+ * @param spec Spec
+ * @return TO DO
+ */
+
+int StopRCnt(int spec);
+
+/**
+ * Restores BIOS state to the one prior the initialization of the library.
+ * This function can only be used if PSX_InitEx() was called with the
+ * PSX_INIT_SAVESTATE flag.
+ * It is usually called by PSX_DeInit() automatically, so unless
+ * you know what you are doing, you do not need to call it yourself.
+ * @return 1 on success, 0 on failure, such as the fact that PSX_InitEx() wasn't called
+ * with the PSX_INIT_SAVESTATE flag.
+ */
+
+int PSX_RestoreBiosState(void);
+
+/**
+ * Gets the bitmask for the flags passed to PSX_InitEx()
+ * @return Flag bitmask
+ */
+
+unsigned int PSX_GetInitFlags(void);
+
+/**
+ * Sets an handler function for the VBlank interrupt.
+ * Used for simple, inaccurate timing - the handler function gets called 60 times a second
+ * in NTSC video mode and 50 times a second in PAL video mode.
+ *
+ * While most games use the VBlank interrupt for timing as they don't require high precision and
+ * have mechanisms to keep up with the different speed in PAL or NTSC video mode, for precise
+ * timing VBlank is inadequate. It is better to look at root counters if you desire precision.
+ *
+ * If there is already a VBlank handler set, this function replaces the current handler with the specified one.
+ * @param h Pointer to (new) VBlank handler function
+ */
+
+void SetVBlankHandler(void (*h)());
+
+/**
+ * Removes a previously set VBlank handler.
+ *
+ * If SetVBlankHandler() was not called before, calling this function has no effect.
+ */
+
+void RemoveVBlankHandler(void);
+
+#endif
diff --git a/libpsx/include/psxbios.h b/libpsx/include/psxbios.h
new file mode 100644
index 0000000..9275393
--- /dev/null
+++ b/libpsx/include/psxbios.h
@@ -0,0 +1,215 @@
+/*
+ * PSXSDK: Bios functions
+ */
+
+#ifndef _PSXBIOS_H
+#define _PSXBIOS_H
+
+/* Joypad functions */
+
+extern void PAD_init(unsigned long mode, unsigned long *pad_buf);
+extern int PAD_dr(void);
+
+/* ROM information functions */
+
+/**
+ * Returns PSX kernel date.
+ * @return Kernel date n 0xYYYYMMDD BCD format.
+ */
+
+unsigned long GetKernelDate(void);
+
+/**
+ * Returns a pointer to a zero-terminated
+ * string which contains the kernel ROM version.
+ * @return Pointer to a zero-terminated string which contains the kernel ROM version.
+ */
+
+const char *GetKernelRomVersion(void);
+
+/**
+ * Returns a pointer to a zero-terminated
+ * string which contains the system ROM version.
+ * @return Zero-terminated string which contains the system ROM version.
+ */
+
+const char *GetSystemRomVersion(void);
+
+/**
+ * GetRamSize() should return size of RAM in bytes.
+ * It doesn't seem to work most times. On SCPH1001, it returns 0.
+ * On SCPH1000, it returns 2 (which is the number of megabytes of RAM
+ * the PSX has.)
+ * @return Size of RAM in bytes.
+ */
+
+unsigned int GetRamSize(void);
+
+/* Interrupt/Exception functions */
+
+/*void Exception();*/
+
+/**
+ * Enters a critical section.
+ */
+
+void EnterCriticalSection(void);
+
+/**
+ * Exits a critical section.
+ */
+
+void ExitCriticalSection(void);
+
+void SysEnqIntRP(int index, unsigned int *buf);
+void SysDeqIntRP(int index, unsigned int *buf);
+
+void ResetEntryInt(void);
+
+
+/**
+ * Directory entry
+ */
+struct DIRENTRY
+{
+ /** Filename */
+ char name[20];
+ /** Attributes */
+ unsigned int attr;
+ /** File size in bytes */
+ int size;
+ /** Pointer to next file entry */
+ struct DIRENTRY *next;
+ /** System reserved */
+ unsigned char system[8];
+};
+
+/**
+ * Gets information about the first file which
+ * matches the pattern. ? and * wildcards can be used.
+ * Characters after * are ignored.
+ * @param name File name string
+ * @param dirent Pointer to a struct DIRENTRY object.
+ * @return dirent on success, NULL on failure.
+ */
+
+struct DIRENTRY *firstfile(const char *name, struct DIRENTRY *dirent);
+
+/**
+ * Finds a file with the same conditions as the previous call to firstfile().
+ * If a corresponding file is found, file information is stored
+ * to the structure pointed to by dir.
+ *
+ * @param dir Pointer to a struct DIRENTRY object.
+ * @return dir on success, NULL on failure.
+ */
+
+struct DIRENTRY *nextfile(struct DIRENTRY *dir);
+
+/**
+ * Gets the file size of the file named "name".
+ * It is actually just a wrapper around firstfile.
+ * It rounds the file size to the block size (2048).
+ * @param name FIle name string
+ * @return File size in bytes, rounded.
+ */
+
+int get_file_size(const char *name);
+
+/**
+ * This function is like get_file_size() but doesn't round
+ * the file size to the block size.
+ * @param name File name string
+ * @return File size in bytes, unrounded.
+ */
+
+int get_real_file_size(const char *name);
+
+void InitHeap(void *block , int size);
+void FlushCache(void);
+
+void SetRCntHandler(void (*callback)(), int spec, unsigned short target);
+void RemoveRCntHandler(int spec);
+
+/**
+ * Opens an event, and returns its identifier
+ * Must be executed in a critical section
+ * @param desc Numerical cause descriptor
+ * @param spec Numerical event type
+ * @param mode Numerical mode
+ * @param func Function pointer to callback function
+ * @return Numerical identifier for the event opened
+ */
+
+int OpenEvent(
+ int desc, // Cause descriptor
+ int spec, // Event type
+ int mode, // Mode
+ int *(*func)(void) // Pointer to callback function
+);
+
+/**
+ * Enables an event by its identifier returned by OpenEvent()
+ * @param event Numerical event identifier
+ * @return ???
+ */
+
+int EnableEvent(unsigned int event);
+
+/**
+ * Closes an event by its identifier
+ * @param event Numerical event identifier
+ * @return ???
+ */
+
+int CloseEvent(unsigned int event);
+
+/**
+ * Disables an event by its identifier
+ * @param event Numerical event identifier
+ * @return ???
+ */
+
+int DisableEvent(unsigned int event);
+
+/**
+ * Generates an event. This must be executed in a critical section.
+ * If the event to deliver is set to generate an interrupt, the handler function is called.
+ * @param ev1 Numerical cause descriptor
+ * @param ev2 Numerical event class
+ * @return ???
+ */
+
+int DeliverEvent(unsigned int ev1, // Cause descriptor
+ int ev2); // Event class
+
+/**
+ * Checks if the event specified by its identifier has occured
+ * @param event Numerical event identifier
+ * @return 1 if the event has occured, 0 if it has not
+ */
+
+int TestEvent(unsigned int event);
+
+/**
+ * Waits until the event specified by identifier occurs.
+ * @param event Numerical event identifier
+ * @return 1 on success, 0 on failure.
+ */
+
+int WaitEvent(unsigned int event);
+
+/**
+ * Replaces the executable image in memory with the one
+ * contained in another executable file in PSX-EXE format.
+ * WARNING: Does not work right now.
+ *
+ * Most likely you want PSX_RunExe()
+ * @param name Path name of PSX-EXE executable
+ * @param argc Number of arguments
+ * @param argv Pointer to an array of string pointers for each argument
+ */
+
+void LoadExec(char *name, int argc, char **argv);
+
+#endif
diff --git a/libpsx/include/psxcdrom.h b/libpsx/include/psxcdrom.h
new file mode 100644
index 0000000..2f18e68
--- /dev/null
+++ b/libpsx/include/psxcdrom.h
@@ -0,0 +1,82 @@
+#ifndef _PSXCDROM_H
+#define _PSXCDROM_H
+
+#define CDSTATUS_PLAY 0x80
+#define CDSTATUS_SEEK 0x40
+#define CDSTATUS_SHELLOPEN 0x10
+
+// Command names
+
+enum
+{
+ CdlSync = 0,
+ CdlNop = 1, CdlGetstat = 1,
+ CdlSetloc = 2,
+ CdlPlay = 3,
+ CdlForward = 4,
+ CdlBackward = 5,
+ CdlReadN = 6,
+ CdlStandby = 7,
+ CdlStop = 8,
+ CdlPause = 9,
+ CdlInit = 10,
+ CdlMute = 11,
+ CdlDemute = 12,
+ CdlSetfilter = 13,
+ CdlSetmode = 14,
+ CdlSetparam = 15,
+ CdlGetlocL = 16,
+ CdlGetlocP = 17,
+ CdlCmd18 = 18,
+ CdlGetTN = 19,
+ CdlGetTD = 20,
+ CdlSeekL = 21,
+ CdlSeekP = 22,
+ CdlCmd23 = 23,
+ CdlCmd24 = 24,
+ CdlTest = 25,
+ CdlID = 26,
+ CdlReadS = 27,
+ CdlReset = 28,
+ CdlCmd29 = 29,
+ CdlReadTOC = 30
+};
+
+/*
+ * Send a low-level CDROM command
+ * cmd = command number
+ * num = number of arguments
+ * ... = arguments
+ */
+
+void CdSendCommand(int cmd, int num, ...);
+
+/**
+ * Reads the results of a low-level CDROM command
+ *
+ * @param out Pointer to array of chars where the output will be stored
+ * @param max Maximum number of bytes to store
+ *
+ * Return value: number of results.
+ */
+
+int CdReadResults(unsigned char *out, int max);
+
+/**
+ * Gets CDROM drive status
+ * @return CDROM drive status bitmask
+ */
+
+int CdGetStatus(void);
+
+/**
+ * Play an Audio CD track
+ * @return 1 on success, 0 on failure
+ */
+
+int CdPlayTrack(unsigned int track);
+
+unsigned char CdRamRead(unsigned short addr);
+
+
+#endif
diff --git a/libpsx/include/psxgpu.h b/libpsx/include/psxgpu.h
new file mode 100644
index 0000000..373b2d6
--- /dev/null
+++ b/libpsx/include/psxgpu.h
@@ -0,0 +1,1142 @@
+#ifndef _PSXGPU_H
+#define _PSXGPU_H
+
+#define GPU_DATA_PORT_ADDR 0x1f801810
+#define GPU_CONTROL_PORT_ADDR 0x1f801814
+#define GPU_DATA_PORT *((volatile unsigned int*)GPU_DATA_PORT_ADDR)
+#define GPU_CONTROL_PORT *((volatile unsigned int*)GPU_CONTROL_PORT_ADDR)
+
+#define DPCR *((volatile unsigned int*)0x1f8010f0)
+#define D2_MADR *((volatile unsigned int*)0x1f8010a0)
+#define D2_BCR *((volatile unsigned int*)0x1f8010a4)
+#define D2_CHCR *((volatile unsigned int*)0x1f8010a8)
+
+#define get_clutid(cx, cy) (((cx&0x3ff)>>4)|((cy&0x1ff)<<6))
+
+/**
+ * Initializes the GPU. Same as GsInitEx(0)
+ *
+ * The display is left disabled.
+ * You can enable it with GsEnableDisplay() or more preferably with GsSetVideoMode()
+ */
+
+void GsInit(void);
+
+/**
+ * Initializes the GPU.
+ *
+ * The display is left disabled.
+ * You can enable it with GsEnableDisplay() or more preferably with GsSetVideoMode()
+ * @param flags Flag bitmask
+ */
+
+void GsInitEx(unsigned int flags);
+
+/**
+ * Resets the GPU
+ */
+
+void GsReset(void);
+
+/**
+ * Enables the display (i.e. generation of video output).
+ *
+ * Unless you are doing low-level GPU programming, you don't need to call this function;
+ * the display is enabled automatically by GsSetVideoMode().
+ * @param enable If TRUE (>=1) the display will be enabled, if FALSE (== 0) it will be disabled
+ */
+
+void GsEnableDisplay(int enable);
+
+/**
+ * Sets a video mode and enables the display. It does so in a quicker way
+ * than GsSetVideoModeEx(), which wants more arguments but offers greater control
+ * @param width Width
+ + 640, 384, 320 and 256 are supported by the PlayStation hardware.
+ * @param height Height
+ + 480 and 240 are supported by the PlayStation hardware.
+ * You probably want to use GsSetVideoModeEx() if you set
+ * 480 here, because this function doesn't enable interlacing.
+ * @param video_mode Video mode
+ + VMODE_NTSC for NTSC, VMODE_PAL for PAL
+ * @return 1 on success, 0 on failure (such as unavailable video mode)
+ */
+
+int GsSetVideoMode(int width, int height, int video_mode);
+
+/**
+ * Works like GsSetVideoMode() but offers more control.
+ * Interlaced, 24-bit RGB, and "reverse" flags can be specified.
+ * @param width Width
+ + 640, 384, 320 and 256 are supported by the PlayStation hardware.
+ * @param height Height
+ + 480 and 240 are supported by the PlayStation hardware.
+ * @param video_mode Video mode
+ + VMODE_NTSC for NTSC, VMODE_PAL for PAL
+ * @param rgb24 24-bit RGB mode
+ + If TRUE (!=0) enables the very rarely used 24-bit color mode. Unless you're very creative, don't enable this.
+ * @param inter Interlacing
+ + If TRUE (!=0) enables interlacing. If you set the height paramater to 480, and you want to display your program on
+ a real television screen, you must enable this.
+ * @param reverse Reverse??
+ + The function of this is not really known. Stay safe and set this to FALSE (0).
+ * @return 1 on success, 0 on failure (such as unavailable video mode)
+ */
+
+int GsSetVideoModeEx(int width, int height, int video_mode, int rgb24, int inter, int reverse);
+
+/**
+ * Assigns the internal pointer to the primitive list to the desired one,
+ * and resets the linked list position counter.
+ * The memory address specified by your pointer has to have enough space free to contain all
+ * the packets which you want to send.
+ * @param listptr Pointer to primitive list
+ */
+
+void GsSetList(unsigned int *listptr);
+
+/**
+ * Assigns the internal pointer to the primitive list to the desired one,
+ * an sets the linked list position counter to the specified value.
+ * The memory address specified by your pointer has to have enough space free to contain all
+ * the packets which you want to send, and you must ensure that the specified position
+ * is not out of bounds.
+ * @param listptr Pointer to primitive list
+ * @param listpos List position
+ */
+
+void GsSetListEx(unsigned int *listptr, unsigned int listpos);
+
+/**
+ * Draws the primitive list.
+ *
+ * This also has the effect of resetting the current drawing list position.
+ */
+
+void GsDrawList(void);
+
+/**
+ * Draws the primitive list using port I/O access.
+ *
+ * GsDrawList() uses DMA to transfer the primitive data in the linked list to the GPU.
+ *
+ * This function which is of main interest to low-level programmers, uses GPU port I/O access instead,
+ * and it is slower (as reads and writes must be done by the main CPU), but works in all situations,
+ * even when you can't get DMA working.
+ *
+ * This function due to its nature is blocking, and always waits for completion.
+ */
+
+void GsDrawListPIO(void);
+
+/**
+ * Makes non-blocking gpu functions like GsDrawList()
+ * wait for completion. Removes the need to use GsIsDrawing()/GsIsWorking()
+ */
+
+void GsSetAutoWait(void);
+
+/** Monochrome 3 point polygon */
+
+typedef struct
+{
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** X Coordinates for vertexes */
+ short x[3];
+ /** Y Coordinates for vertexes */
+ short y[3];
+ /** Attribute bitmask */
+ unsigned int attribute;
+}GsPoly3;
+
+/** Monochrome 4 point polygon */
+
+typedef struct
+{
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** X Coordinates for vertexes */
+ short x[4];
+ /** Y Coordinates for vertexes */
+ short y[4];
+ /** Attribute */
+ unsigned int attribute;
+}GsPoly4;
+
+/** Textured 3 point polygon */
+
+typedef struct
+{
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** X Coordinates for vertexes */
+ short x[3];
+ /** Y Coordinates for vertexes */
+ short y[3];
+ /** X Texture offsets for vertexes */
+ unsigned char u[3];
+ /** Y Texture offset for vertexes */
+ unsigned char v[3];
+ /** CLUT X coordinate */
+ short cx;
+ /** CLUT Y coordinate */
+ short cy;
+ /** Attribute */
+ unsigned int attribute;
+ /** Texture page */
+ unsigned char tpage;
+}GsTPoly3;
+
+/** Textured 4 point polygon */
+
+typedef struct
+{
+ /** X Coordinates for vertexes */
+ short x[4];
+ /** Y Coordinates for vertexes */
+ short y[4];
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** CLUT X coordinate */
+ short cx;
+ /** CLUT Y coordinate */
+ short cy;
+ /** Texture page */
+ unsigned char tpage;
+ /** Horizontal texture offset */
+ unsigned char u[4];
+ /** Vertical texture offset */
+ unsigned char v[4];
+ /** Attribute */
+ unsigned int attribute;
+}GsTPoly4;
+
+/** Graduated 3 point polygon */
+
+typedef struct
+{
+ /** Red color components (0-255) */
+ unsigned char r[3];
+ /** Green color components (0-255) */
+ unsigned char g[3];
+ /** Blue color components (0-255) */
+ unsigned char b[3];
+ /** X Coordinates for vertexes */
+ short x[3];
+ /** Y Coordinates for vertexes */
+ short y[3];
+ /** Attribute */
+ unsigned int attribute;
+}GsGPoly3;
+
+/** Graduated 4 point polygon */
+
+typedef struct
+{
+ /** Red color components (0-255) */
+ unsigned char r[4];
+ /** Green color components (0-255) */
+ unsigned char g[4];
+ /** Blue color components (0-255) */
+ unsigned char b[4];
+ /** X Coordinates for vertexes */
+ short x[4];
+ /** Y Coordinates for vertexes */
+ short y[4];
+ /** Attribute */
+ unsigned int attribute;
+}GsGPoly4;
+
+/** Graduated textured 3 point polygon */
+
+typedef struct
+{
+ /** Red color components (0-255) */
+ unsigned char r[3];
+ /** Green color components (0-255) */
+ unsigned char g[3];
+ /** Blue color components (0-255) */
+ unsigned char b[3];
+ /** X Coordinates for vertexes */
+ short x[3];
+ /** Y Coordinates for vertexes */
+ short y[3];
+ /** CLUT X coordinate */
+ short cx;
+ /** CLUT Y coordinate */
+ short cy;
+ /** Texture page */
+ unsigned char tpage;
+ /** Horizontal texture offset */
+ unsigned char u[3];
+ /** Vertical texture offset */
+ unsigned char v[3];
+ /** Attribute */
+ unsigned int attribute;
+}GsGTPoly3;
+
+/** Graduated textured 4 point polygon */
+
+typedef struct
+{
+ /** Red color components (0-255) */
+ unsigned char r[4];
+ /** Green color components (0-255) */
+ unsigned char g[4];
+ /** Blue color components (0-255) */
+ unsigned char b[4];
+ /** X Coordinates for vertexes */
+ short x[4];
+ /** Y Coordinates for vertexes */
+ short y[4];
+ /** CLUT X coordinate */
+ short cx;
+ /** CLUT Y coordinate */
+ short cy;
+ /** Texture page */
+ unsigned char tpage;
+ /** Horizontal texture offset */
+ unsigned char u[4];
+ /** Vertical texture offset */
+ unsigned char v[4];
+ /** Attribute */
+ unsigned int attribute;
+}GsGTPoly4;
+
+/** Monochrome line */
+
+typedef struct
+{
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** X Coordinates for points */
+ short x[2];
+ /** Y Coordinates for points */
+ short y[2];
+ /** Attribute */
+ unsigned int attribute;
+}GsLine;
+
+/** Dot (pixel) */
+
+typedef struct
+{
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** X coordinate */
+ short x;
+ /** Y coordinate */
+ short y;
+ /** Attribute */
+ unsigned int attribute;
+}GsDot;
+
+/** Graduated line */
+
+typedef struct
+{
+ /** Red color components (0-255) */
+ unsigned char r[2];
+ /** Green color components (0-255) */
+ unsigned char g[2];
+ /** Blue color components (0-255) */
+ unsigned char b[2];
+ /** X coordinates for points */
+ short x[2];
+ /** Y coordinates for points */
+ short y[2];
+ /** Attribute */
+ unsigned int attribute;
+}GsGLine;
+
+/** Sprite */
+
+typedef struct
+{
+ short x, y; /* X, Y positions */
+ unsigned char u, v; /* Offset into texture page of sprite image data */
+ short w, h; /* Width and height of sprite */
+ short cx, cy; /* Color look up table (palette) X, Y positions */
+ unsigned char r, g, b; /* Luminosity of color components - 128 is normal luminosity */
+ unsigned char tpage; /* Texture page */
+ unsigned int attribute; /* Attribute */
+
+ /* Scaling? These are only candidates...
+
+ scalex:
+ Denotes horizontal scaling
+
+ 0 = true size (unmodified)
+ 1 = true size (*1)
+ 2 = double size (*2)
+ 3 = triple size (*3)
+ ...
+
+ -1 = true size (/1)
+ -2 = half size (/2)
+ -3 = one-third size (/3)
+ ...
+
+ scaley:
+ Denotes vertical scaling
+
+ *** The behaviour below was introduced in PSXSDK 0.5
+
+ If scalex > 8,
+ resulting width will be (original_width * scalex) / 4096
+ scalex = 4096 (SCALE_ONE) (original width), scalex = 2048 (half width), etc.
+ If scaley > 8,
+ resulting height will be (original_height * scaley) / 4096
+ works like scalex but affects height
+ */
+ int scalex, scaley;
+
+ int rotate; // Rotation angle - Fixed point notation, 4096 = 1 degree
+ int mx, my; // Coordinates of rotation center - relative to coordinates of sprite
+}GsSprite;
+
+/** Rectangle */
+
+typedef struct
+{
+
+ short x, y;
+ short w, h;
+ unsigned char r, g, b;
+ unsigned int attribute; /* Attribute */
+}GsRectangle;
+
+typedef struct
+{
+ /** Number of points */
+ unsigned int npoints;
+ /** Red color component (0-255) */
+ unsigned char r;
+ /** Green color component (0-255) */
+ unsigned char g;
+ /** Blue color component (0-255) */
+ unsigned char b;
+ /** X Coordinates for points */
+ short *x;
+ /** Y Coordinates for points */
+ short *y;
+ /** Attribute */
+ unsigned int attribute;
+}GsPolyLine;
+
+typedef struct
+{
+ /** Number of points */
+ unsigned int npoints;
+ /** Red color components (0-255) */
+ unsigned char *r;
+ /** Green color components (0-255) */
+ unsigned char *g;
+ /** Blue color components (0-255) */
+ unsigned char *b;
+ /** X Coordinates for points */
+ short *x;
+ /** Y Coordinates for points */
+ short *y;
+ /** Attribute */
+ unsigned int attribute;
+}GsGPolyLine;
+
+/** Map */
+
+//typedef struct
+//{
+// short x, y; /* X, Y positions */
+// unsigned char u, v; /* Offset into texture page of sprite image data */
+// short w, h; /* Width and height of tilemap */
+// short l; /* Length of tilemap line */
+// short cx, cy; /* Color look up table (palette) X, Y positions */
+// unsigned char r, g, b; /* Luminosity of color components - 128 is normal luminosity */
+// unsigned char tpage; /* Texture page */
+// unsigned int attribute; /* Attribute */
+//
+// unsigned short tmw, tmh; /* Map texture width and height */
+// unsigned char tw, th; /* Map tile width and height */
+//
+// unsigned char tsize; /* Size of tile in map (1 = 8-bit, 2 = 16-bit, 4 = 32-bit) */
+//
+// unsigned int tmask; /* Inverted mask for tile number */
+//
+// void *data; /* Pointer to beginning of map data */
+//}GsMap;
+
+/** Texture color modes. */
+
+enum psx_gpu_texmodes
+{
+ /** 4-bit color mode */
+ COLORMODE_4BPP,
+ /** 8-bit color mode */
+ COLORMODE_8BPP,
+ /** 16-bit color mode */
+ COLORMODE_16BPP,
+ /** 24-bit color mode */
+ COLORMODE_24BPP
+};
+
+/**
+ * This is the luminance factor with which images
+ * are drawn as they are stored. (i.e. without applying lighting)
+ *
+ * NORMAL_LUMINOSITY (sic) is kept for backward compatibility,
+ * but "luminosity" is an incorrect term here.
+ */
+
+#define NORMAL_LUMINANCE 128
+#define NORMAL_LUMINOSITY NORMAL_LUMINANCE
+
+/**
+ * Macro to specify texture color mode, takes a value from psx_gpu_texmodes
+ */
+#define COLORMODE(x) x&3
+/**
+ * Macro to specify translucency/semitransparency mode, where x can be a value from 0 to 3.
+ *
+ * If a pixel in image data to be drawn has the STP bit set, semitransparency
+ * processing is enabled for that pixel.
+ *
+ * When the color is black (RGB=0,0,0) STP is processed differently from when it is not
+ * black.
+ *
+ * The table below shows the differences:
+ *
+ * Color | STP bit | Processing off | Processing on
+ * ----- | ---- | ----- | ----
+ * Black | 0 | Transparent | Transparent
+ * Black | 1 | Non-transparent |Non-Transparent
+ * Not black | 0 | Non-Transparent |Non-Transparent
+ * Not black | 1 | Non-Transparent |Transparent
+ *
+ * If the image pixel is semi-transparent (STP bit set) and not black, the formulas for the
+ * final pixel color are the following:
+ *
+ * Mode | From framebuffer pixel| From image pixel
+ * ---- | --- | -----
+ * 0 | +50% | +50%
+ * 1 | +100% | +100%
+ * 2 | +100% | -100%
+ * 3 | +100% | +25%
+ *
+ * The final color component values do not underflow or overflow, they simply saturate at 0 or 255.
+ */
+#define TRANS_MODE(x) ((x&3)<<2)
+/**
+ * Enable semi-transparency processing for the primitive.
+ */
+#define ENABLE_TRANS (1<<4)
+/**
+ * Enable horizontal flipping for the primitive.
+ */
+#define H_FLIP (1<<5)
+/**
+ * Enable vertical flipping for the primitive.
+ */
+#define V_FLIP (1<<6)
+
+/**
+ * Drawing environment.
+ */
+
+typedef struct
+{
+ /**
+ * Dithering enabled flag.
+ */
+ unsigned char dither;
+ /**
+ * Enable drawing on display area flag.
+ *
+ * If this flag is enabled, drawing on display area is allowed.
+ *
+ * Usually you enable this flag when you do not want to use double buffering.
+ */
+ unsigned char draw_on_display;
+
+ /**
+ * Drawing area X start coordinate in framebuffer
+ */
+ short x;
+ /**
+ * Drawing area Y start coordinate in framebuffer
+ */
+ short y;
+ /**
+ * Drawing area width.
+ */
+ short w;
+ /**
+ * Drawing area height.
+ */
+ short h;
+
+ /*
+ * Drawing offset
+ */
+ //short off_x, off_y;
+
+ /**
+ * Masking settings (can also be set with GsSetMasking())
+ */
+
+ /**
+ * Do not draw over pixels which have their mask bit set
+ */
+ unsigned char ignore_mask;
+ /**
+ * If this is set, every pixel drawn will have the mask bit set
+ */
+ unsigned char set_mask;
+}GsDrawEnv;
+
+/**
+ * Display environment.
+ *
+ * The width and height of the display area are those of the currently set video mode.
+ */
+
+typedef struct
+{
+ /** Display area X start coordinate in framebuffer. */
+ short x;
+ /** Display area Y start coordinate in framebuffer. */
+ short y;
+}GsDispEnv;
+
+/**
+ * Image
+ * @brief This structure describes a TIM image
+ */
+
+typedef struct
+{
+
+
+ /** Pixel (color) mode. 0 = 4bpp, 1 = 8bpp, 2 = 16bpp, 3 = 24bpp */
+ int pmode;
+ /** Reports whether this image has a Color Look Up Table. 1 if there's a CLUT, 0 otherwise. */
+ int has_clut;
+ /** X coordinate of CLUT in framebuffer */
+ int clut_x;
+ /** Y coordinate of CLUT in framebuffer */
+ int clut_y;
+ /** Width of CLUT in framebuffer */
+ int clut_w;
+ /** Height of CLUT in framebuffer */
+ int clut_h;
+ /** X coordinate of image in framebuffer */
+ int x;
+ /** Y coordinate of image in framebuffer */
+ int y;
+ /** Width of image in framebuffer */
+ int w;
+ /** Height of image in framebuffer */
+ int h;
+ /** Pointer to CLUT data */
+ void *clut_data;
+ /** Pointer to image data */
+ void *data;
+}GsImage;
+
+/**
+ * Adds a monochrome 3 point polygon to the packet list
+ * @param poly3 Pointer to structure for monochrome 3 point polygon
+ */
+
+void GsSortPoly3(GsPoly3 *poly3);
+
+/**
+ * Adds a monochrome 4 point polygon to the packet list
+ * @param poly4 Pointer to structure for monochrome 4 point polygon
+ */
+
+void GsSortPoly4(GsPoly4 *poly4);
+
+/**
+ * Adds a textured 3 point polygon to the packet list
+ * @param tpoly3 Pointer to structure for textured 3 point polygon
+ */
+
+void GsSortTPoly3(GsTPoly3 *tpoly3);
+
+/**
+ * Adds a textured 4 point polygon to the packet list
+ * @param tpoly4 Pointer to structure for textured 4 point polygon
+ */
+
+void GsSortTPoly4(GsTPoly4 *tpoly4);
+
+/**
+ * Adds a gradated 3 point polygon to the packet list
+ * @param poly3 Pointer to structure for gradated 3 point polygon
+ */
+
+void GsSortGPoly3(GsGPoly3 *poly3);
+
+/**
+ * Adds a gradated 4 point polygon to the packet list
+ * @param poly4 Pointer to structure for gradated 4 point polygon
+ */
+
+void GsSortGPoly4(GsGPoly4 *poly4);
+
+/**
+ * Adds a gradated textured 3 point polygon to the packet list
+ * @param tpoly3 Pointer to structure for textured 3 point polygon
+ */
+
+void GsSortGTPoly3(GsGTPoly3 *tpoly3);
+
+/**
+ * Adds a gradated 4 point polygon to the packet list
+ * @param tpoly4 Pointer to structure for textured 4 point polygon
+ */
+
+void GsSortGTPoly4(GsGTPoly4 *tpoly4);
+
+/**
+ * Adds a monochrome line to the packet list
+ * @param line Pointer to structure for monochrome line
+ */
+
+void GsSortLine(GsLine *line);
+
+/**
+ * Adds a gradated line to the packet list
+ * @param line Pointer to structure for gradated line
+ */
+
+void GsSortGLine(GsGLine *line);
+
+/**
+ * Adds a dot (pixel) to the packet list
+ * @param dot Pointer to structure for dot
+ */
+
+void GsSortDot(GsDot *dot);
+
+/**
+ * Adds a sprite to the packet list
+ * @param sprite Pointer to structure for sprite
+ */
+
+void GsSortSprite(GsSprite *sprite);
+
+/**
+ * Always adds a REAL sprite to the packet list
+ *
+ * + GsSortSprite() on the other hand checks for scaling and flipping, which
+ * are not supported by the "sprite" primitive on the PlayStation, but instead
+ * are done by using a textured 4-point polygon accordingly.
+ * GsSortSprite() only uses the sprite primitive when all the attributes
+ * can be done with a "sprite" primitive as well.
+ *
+ * @param sprite Pointer to structure for sprite
+ */
+
+void GsSortSimpleSprite(GsSprite *sprite);
+
+/**
+ * Adds a rectangle to the packet list
+ * @param rectangle Pointer to structure for rectangle
+ */
+
+void GsSortRectangle(GsRectangle *rectangle);
+
+/**
+ * Moves image data from a part of the framebuffer to another.
+ * Actually it does a copy.
+ * @param src_x Top-left X coordinate of source area
+ * @param src_y Top-left Y coordinate of source area
+ * @param dst_x Top-left X coordinate of destination area
+ * @param dst_y Top-left Y coordinate of destination area
+ * @param w Width of area
+ * @param h Height of area
+ */
+
+void MoveImage(int src_x, int src_y, int dst_x, int dst_y, int w, int h);
+
+/**
+ * Loads image data into framebuffer memory.
+ * @param img Pointer to raw image data
+ * @param x Top-left X coordinate of destination area
+ * @param y Top-left Y coordinate of destination area
+ * @param w Width of image data
+ * @param h Height of image data
+ */
+
+void LoadImage(void *img, int x, int y, int w, int h);
+
+/**
+ * Draws a rectangle in the framebuffer, without considering drawing
+ * and display environments (i.e. it does so in an absolute way)
+ * @param x Top-left X coordinate of area
+ * @param y Top-left Y coordinate of area
+ * @param w Area width
+ * @param h Area height
+ * @param r Red (0 - 255)
+ * @param g Green (0 - 255)
+ * @param b Blue (0 - 255)
+ */
+
+void DrawFBRect(int x, int y, int w, int h, int r, int g, int b);
+
+/**
+ * Set drawing environment
+ * @param drawenv Pointer to drawing environment structure
+ */
+
+void GsSetDrawEnv(GsDrawEnv *drawenv);
+
+/**
+ * Set display environment
+ * @param dispenv Pointer to display environment structure
+ */
+
+void GsSetDispEnv(GsDispEnv *dispenv);
+
+
+
+/* If this flag is set, pixels drawn have MSB set */
+#define MASK_SET 1
+/* If this flag is set, pixels aren't drawn over pixels with MSB set */
+#define MASK_IGNORE 2
+
+ /**
+ * Sets masking settings
+ * @param flag Bitwise flags
+ */
+
+void GsSetMasking(unsigned char flag);
+
+/**
+ * Return pointer position in linked list
+ * @return Pointer position in linked list
+ */
+
+unsigned int GsListPos(void);
+
+/**
+ * Three functions which send data to the control port and to the data port
+ */
+
+void gpu_ctrl(unsigned int command, unsigned int param);
+void gpu_data(unsigned int data);
+void gpu_data_ctrl(unsigned int command, unsigned int param);
+
+/**
+ * Puts information about a TIM image passed in a buffer in a GsImage structure.
+ */
+
+int GsImageFromTim(GsImage *image, void *timdata);
+
+/**
+ * Uploads an image described by a GsImage structure to video memory.
+ */
+
+void GsUploadImage(GsImage *image);
+
+/**
+ * Fills a GsSprite structure with information from an image described
+ * by a GsImage structure, then optionally uploads data to video memory.
+ * Sprite coordinates are set to 0.
+ */
+
+int GsSpriteFromImage(GsSprite *sprite, GsImage *image, int do_upload);
+
+/**
+ * Checks if the GPU is drawing
+ * @return 1 if GPU is drawing, 0 otherwise
+ */
+
+int GsIsDrawing(void);
+
+/**
+ * Checks if the GPU is working. Alias of GsIsDrawing()
+ * @return 1 if GPU is working, 0 otherwise.
+ */
+
+int GsIsWorking(void); // Alias of GsIsDrawing()
+
+/**
+ * Clear Video RAM
+ */
+
+void GsClearMem(void);
+
+/**
+ * Loads the built-in 8x8 font in Video RAM at the specified coordinates
+ *
+ *
+ * Specifying cx and cy as -1 will not load the black&white CLUT to
+ * video memory. Use GsSetFont() to specify clut x and clut y in that case.
+ *
+ * The font occupies a space of 16x128 pixels in 16-bit mode,
+ * and 64x128 in 4-bit mode.
+* @param fb_x X coordinate in framebuffer
+ * @param fb_y Y coordinate in framebuffer
+ * @param cx X coordinate of black/white CLUT in framebuffer
+ * @param cy Y coordinate of black/white CLUT in framebuffer
+ */
+
+void GsLoadFont(int fb_x, int fb_y, int cx, int cy);
+
+/**
+ * Prints string using 8x8 font at screen coordinates x, y
+ * @param x X coordinate
+ * @param y Y coordinate
+ * @param fmt format (like *printf())
+ * @return Position identifier.
+ */
+
+unsigned int GsPrintFont(int x, int y, const char *fmt, ...);
+
+/**
+ * Prints string using 8x8 font at screen coordinates x, y
+ * Apart from using a variable argument list, this function
+ * is identical to GsPrintFont()
+ * @param x X coordinate
+ * @param y Y coordinate
+ * @param fmt format (like *printf())
+ * @param ap Variable argument list
+ * @return Position identifier.
+ */
+
+unsigned int GsVPrintFont(int x, int y, const char *fmt, va_list ap);
+
+/**
+ * Change font coordinates without reloading it
+ */
+
+void GsSetFont(int fb_x, int fb_y, int cx, int cy);
+
+/**
+ * This is a function that sets an internal variable
+ * flags should be specified as an OR mask
+ *
+ * to set the wrap and scale attributes, with X scaling factor 2 and Y scaling
+ * factor 3, for example:
+ * GsSetFontAttrib(PRFONT_WRAP | PRFONT_SCALE
+ * | PRFONT_SCALEX(2) | PRFONT_SCALEY(3));
+ *
+ * to remove all attributes (normal printing):
+ *
+ * GsSetFontAttrib(0);
+ *
+ * PRFONT_WRAP can't coexist with PRFONT_CENTER or PRFONT_RIGHT
+ * PRFONT_CENTER and PRFONT_RIGHT are justifying attributes and as such
+ * they are mutually exclusive - they cannot coexist with each other.
+ * Specifying both will give priority to PRFONT_CENTER.
+ *
+ * Attribute list:
+ *
+ * PRFONT_WRAP - Wrap to next row when the text would fall off the screen
+ * Ideal for debug
+ * PRFONT_SCALE - Enable font scaling
+ * PRFONT_SCALEX(i) - Specifies the factor "i" as the X scaling factor
+ * This has the same meaning as a sprite's scaling factor
+ * PRFONT_SCALEY(i) - Specify the factor "i" as the Y scaling factor
+ * This has the same meaning as a sprite's scaling factor
+ * PRFONT_RIGHT - Justifies text to the right
+ * PRFONT_CENTER - Justifies text at the center
+ * PRFONT_RL(f) - Luminance factor for the red component
+ * PRFONT_GL(f) - Luminance factor for the green component
+ * PRFONT_BL(f) - Luminance factor for the blue component
+*/
+
+void GsSetFontAttrib(unsigned int flags);
+
+/**
+ * Sets drawing environment
+ * Enables drawing on the display area, disables dithering and disables all masking flags by default
+ * @param x Top-left X coordinate of framebuffer area to use for drawing
+ * @param y Top-left Y coordinate of framebuffer area to use for drawing
+ * @param w Width of area
+ * @param h Height of area
+ */
+
+void GsSetDrawEnvSimple(int x, int y, int w, int h);
+
+/**
+ * Sets display environment
+ * @param x Top-left X coordinate of framebuffer area to display
+ * @param y Top-left Y coordinate of framebuffer area to display
+ */
+
+void GsSetDispEnvSimple(int x, int y);
+
+/**
+ Television video modes.
+*/
+enum psx_gpu_vmodes
+{
+ /** NTSC video mode, 60Hz */
+ VMODE_NTSC,
+ /** PAL video mode, 50Hz */
+ VMODE_PAL
+};
+
+/** One scaling unit. */
+
+#define SCALE_ONE 4096
+
+/** One rotation unit. */
+
+#define ROTATE_ONE 4096
+
+// GsSetFontAttrib() Attribute Flags
+
+#define PRFONT_WRAP 1
+#define PRFONT_CENTER 2
+#define PRFONT_RIGHT 4
+#define PRFONT_SCALE 8
+#define PRFONT_UNIXLF 16
+#define PRFONT_COLOR 32
+
+// These below are not really attributes... but use them as if they were.
+
+unsigned int PRFONT_SCALEX(int i);
+unsigned int PRFONT_SCALEY(int i);
+unsigned int PRFONT_RL(unsigned char f);
+unsigned int PRFONT_GL(unsigned char f);
+unsigned int PRFONT_BL(unsigned char f);
+
+// Use this to get the final X and Y positions from the return value
+// of GsPrintFont(). Especially useful after wrapping.
+
+#define prfont_get_fx(i) ((short)(i & 0xffff))
+#define prfont_get_fy(i) ((short)((i >> 16) & 0xffff))
+
+// Width and height of the screen in the current video mode
+
+/** This global variable reports the current screen width, as set by GsSetVideoMode(). Do not modify. */
+extern unsigned short GsScreenW;
+/** This global variable reports the current screen height, as set by GsSetVideoMode(). Do not modify. */
+extern unsigned short GsScreenH;
+/** This global variable reports the current screen color mode (PAL or NTSC), as set by GsSetVideoMode().
+ You can use the values in the psx_gpu_vmodes enum to evaluate this. Do not modify. */
+extern unsigned char GsScreenM; // Current video mode
+
+/** This global variable reports the width of the current drawing environment. */
+extern unsigned short GsCurDrawEnvW;
+/** This global variable reports the height of the current drawing environment. */
+extern unsigned short GsCurDrawEnvH;
+
+/**
+ * Clear the entire drawing area with specified color
+ * @param r Red (0 - 255)
+ * @param g Green (0 - 255)
+ * @param b Blue (0 - 255)
+ */
+
+void GsSortCls(int r, int g, int b);
+
+/**
+ @attention Macros by their definition do not like being passed invalid values.
+*/
+
+/**
+ * Macro to get a texture page number from a coordinate in VRAM
+ * @param x X coordinate (0-1023)
+ * @param y Y coordinate (0-511)
+ */
+
+#define gs_get_tpage_num(x,y) ((x/64)+((y/256)*16))
+
+/**
+ * Macro to get an horizontal texture offset from a X coordinate in VRAM
+ * @param x X coordinate (0-1023)
+ */
+
+#define gs_get_tpage_u(x) (x&0x3f)
+
+/**
+ * Macro to get a vertical texture offset from a Y coordinate in VRAM
+ * @param y Y coordinate (0-511)
+ */
+
+#define gs_get_tpage_v(y) (y&0xff)
+
+/**
+ * This macro converts a RGB 888 color to PlayStation color format.
+ * @param r Red component (0-255)
+ * @param g Green component (0-255)
+ * @param b Blue component (0-255)
+ */
+
+#define gs_rgb_to_psx(r, g, b) ((r>>3)|((g>>3)<<5)|((b>>3)<<10))
+
+/**
+ * This macro converts a RGB 888 color (a color whose color components have an intensity between 0 and 255)
+ * to PlayStation color format, considering an alpha bit. If the alpha bit is non-zero, the STP bit in the color
+ * is set and will be considered for PlayStation translucency effects.
+ * @param r Red component (0-255)
+ * @param g Green component (0-255)
+ * @param b Blue component (0-255)
+ * @param a Alpha bit (STP/semitransparency bit)
+ */
+
+#define gs_rgba_to_psx(r, g, b, a) ((r>>3)|((g>>3)<<5)|((b>>3)<<10)|(a==0?0:1))
+
+/**
+ * This function can rotate a vector about the X, Y and Z axes
+ * If you don't want to rotate a vector about an axis, pass 0 as angle to that axis
+ *
+ * It is correct to pass the same argument to v and n, as calculations are first done in an internal buffer
+ * and then stored in the output array
+ *
+ * @param x_a Number of degrees (0-359) to which the vector should be rotated about the X axis
+ * @param y_a Number of degrees (0-359) to which the vector should be rotated about the Y axis
+ * @param z_a Number of degrees (0-359) to which the vector should be rotated about the Z axis
+ * @param v Pointer to an array of coordinates for the source vector
+ * @param n Pointer to destination array for the coordinates of the rotated vector
+ */
+
+void GsRotateVector(int x_a, int y_a, int z_a, double *v, double *n);
+
+/**
+ * Adds a monochrome polyline to the packet list
+ * @param line Pointer to structure for monochrome polyline
+ */
+
+void GsSortPolyLine(GsPolyLine *line);
+
+/**
+ * Adds a gradated polyline to the packet list
+ * @param line Pointer to structure for monochrome line
+ */
+
+void GsSortGPolyLine(GsGPolyLine *line);
+
+//void GsSortSimpleMap(GsMap *map);
+
+#endif
diff --git a/libpsx/include/psxgte.h b/libpsx/include/psxgte.h
new file mode 100644
index 0000000..24574b9
--- /dev/null
+++ b/libpsx/include/psxgte.h
@@ -0,0 +1,196 @@
+#ifndef _PSXGTE_H
+#define _PSXGTE_H
+
+/** GTE operations */
+
+enum gte_operations
+{
+ /** Perspective transformation */
+ GTE_OP_RTPS = 0x0180001,
+ /** Perspective Transformation on 3 points */
+ GTE_OP_RTPT = 0x0280030,
+ /** Multiply vector by matrix and vector addition */
+ GTE_OP_MVMVA = 0x0400012,
+ /** Depth Cue Color light */
+ GTE_OP_DCPL = 0x0680029,
+ /** Depth Cueing */
+ GTE_OP_DPCS = 0x0780010,
+ /** Interpolation of a vector and far color vector */
+ GTE_OP_INTPL = 0x0980011,
+ /** Square vector */
+ GTE_OP_SQR = 0x0A00428,
+ /** Normal color single vector */
+ GTE_OP_NCS = 0x0C8041E,
+ /** Normal color three vectors */
+ GTE_OP_NCT = 0x0D80420,
+ /** Normal color depth cue single vector */
+ GTE_OP_NCDS = 0x0E80413,
+ /** Normal color depth cue three vectors */
+ GTE_OP_NCDT = 0x0F80416,
+ /** Depth Cueing */
+ GTE_OP_DPCT = 0x0F8002A,
+ /** Normal Color Color single vector */
+ GTE_OP_NCCS = 0x108041B,
+ /** Normal Color Color three vectors */
+ GTE_OP_NCCT = 0x118043F,
+ /** Color Depth Cue */
+ GTE_OP_CDP = 0x1280414,
+ /** Color Color */
+ GTE_OP_CC = 0x138041C,
+ /** Normal clipping */
+ GTE_OP_NCLIP = 0x1400006,
+ /** Average of three Z values */
+ GTE_OP_AVSZ3 = 0x158002D,
+ /** Average of four Z values */
+ GTE_OP_AVSZ4 = 0x168002E,
+ /** Outer product of 2 vectors */
+ GTE_OP_OP = 0x170000C,
+ /** General purpose interpolation */
+ GTE_OP_GPF = 0x190003D,
+ /** General purpose interpolation */
+ GTE_OP_GPL = 0x1A0003E
+};
+
+/** GTE error flags, grab from cop2 register 63 */
+
+enum gte_error_flags
+{
+ GTE_FLAG_MAC1_OVF_POS = 0x40000000,
+ GTE_FLAG_MAC2_OVF_POS = 0x20000000,
+ GTE_FLAG_MAC3_OVF_POS = 0x10000000,
+
+ GTE_FLAG_MAC1_OVF_NEG = 0x08000000,
+ GTE_FLAG_MAC2_OVF_NEG = 0x04000000,
+ GTE_FLAG_MAC3_OVF_NEG = 0x02000000,
+
+ GTE_FLAG_IR1_SATURATED = 0x01000000,
+ GTE_FLAG_IR2_SATURATED = 0x00800000,
+ GTE_FLAG_IR3_SATURATED = 0x00400000,
+
+ GTE_FLAG_COL_FIFO_R_SATURATED = 0x00200000,
+ GTE_FLAG_COL_FIFO_G_SATURATED = 0x00100000,
+ GTE_FLAG_COL_FIFO_B_SATURATED = 0x00080000,
+
+ GTE_FLAG_SZ3_OTZ_SATURATED = 0x00040000,
+
+ GTE_FLAG_DIV_OVF_SATURATED = 0x00020000,
+
+ GTE_FLAG_MAC0_OVF_POS = 0x00010000,
+ GTE_FLAG_MAC0_OVF_NEG = 0x00008000,
+
+ GTE_FLAG_SX2_SATURATED = 0x00004000,
+ GTE_FLAG_SY2_SATURATED = 0x00002000,
+
+ GTE_FLAG_IR0_SATURATED = 0x00001000
+};
+
+/** GTE data registers */
+
+enum gte_data_registers
+{
+ /** Vector 0 X and Y */
+ GTE_R_VXY0 = 0,
+ /** Vector 0 Z */
+ GTE_R_VZ0 = 1,
+ /** Vector 1 X and Y */
+ GTE_R_VXY1 = 2,
+ /** Vector 1 Z */
+ GTE_R_VZ1 = 3,
+ /** Vector 2 X and Y */
+ GTE_R_VXY2 = 4,
+ /** Vector 2 Z */
+ GTE_R_VZ2 = 5,
+ /** RGB value */
+ GTE_R_RGB = 6,
+ /** Z Average value */
+ GTE_R_OTZ = 7,
+ /** Intermediate value 0 */
+ GTE_R_IR0 = 8,
+ /** Intermediate value 1 */
+ GTE_R_IR1 = 9,
+ /** Intermediate value 2 */
+ GTE_R_IR2 = 10,
+ /** Intermediate value 3 */
+ GTE_R_IR3 = 11,
+ /** Screen XY coordinates 0 FIFO */
+ GTE_R_SXY0 = 12,
+ /** Screen XY coordinates 1 FIFO */
+ GTE_R_SXY1 = 13,
+ /** Screen XY coordinates 2 FIFO */
+ GTE_R_SXY2 = 14,
+ /** Screen XY coordinates P FIFO */
+ GTE_R_SXYP = 15,
+ /** Screen Z 0 FIFO */
+ GTE_R_SZ0 = 16,
+ /** Screen Z 1 FIFO */
+ GTE_R_SZ1 = 17,
+ /** Screen Z 2 FIFO */
+ GTE_R_SZ2 = 18,
+ /** Screen Z 3 FIFO */
+ GTE_R_SZ3 = 19,
+ /** Characteristic color 0 FIFO */
+ GTE_R_RGB0 = 20,
+ /** Characteristic color 1 FIFO */
+ GTE_R_RGB1 = 21,
+ /** Characteristic color 2 FIFO */
+ GTE_R_RGB2 = 22,
+ /** Sum of products value 0 */
+ GTE_R_MAC0 = 24,
+ /** Sum of products value 1 */
+ GTE_R_MAC1 = 25,
+ /** Sum of products value 2 */
+ GTE_R_MAC2 = 26,
+ /** Sum of products value 3 */
+ GTE_R_MAC3 = 27,
+ /** IRGB ?? */
+ GTE_R_IRGB = 28,
+ /** ORGB ?? */
+ GTE_R_ORGB = 29,
+ /** Leading zero count source data. */
+ GTE_R_LZCS = 30,
+ /** Leading zero count result */
+ GTE_R_LZCR = 31
+};
+
+/** GTE control registers */
+
+enum gte_control_registers
+{
+ GTE_R_R11R12 = 0,
+ GTE_R_R13R21 = 1,
+ GTE_R_R22R23 = 2,
+ GTE_R_R31R32 = 3,
+ GTE_R_R33 = 4,
+ GTE_R_TRX = 5,
+ GTE_R_TRY = 6,
+ GTE_R_TRZ = 7,
+
+ GTE_R_L11L12 = 8,
+ GTE_R_L13L21 = 9,
+ GTE_R_L22L23 = 10,
+ GTE_R_L31L32 = 11,
+ GTE_R_L33 = 12,
+ GTE_R_RBK = 13,
+ GTE_R_GBK = 14,
+ GTE_R_BBK = 15,
+
+ GTE_R_LR1LR2 = 16,
+ GTE_R_LR3LG1 = 17,
+ GTE_R_LG2LG3 = 18,
+ GTE_R_LB1LB2 = 19,
+ GTE_R_LB3 = 20,
+ GTE_R_RFC = 21,
+ GTE_R_GFC = 22,
+ GTE_R_BFC = 23,
+
+ GTE_R_OFX = 24,
+ GTE_R_OFY = 25,
+ GTE_R_H = 26,
+ GTE_R_DQA = 27,
+ GTE_R_DQB = 28,
+ GTE_R_ZSF3 = 29,
+ GTE_R_ZSF4 = 30,
+ GTE_R_FLAG = 31
+};
+
+#endif
diff --git a/libpsx/include/psxpad.h b/libpsx/include/psxpad.h
new file mode 100644
index 0000000..3f96861
--- /dev/null
+++ b/libpsx/include/psxpad.h
@@ -0,0 +1,178 @@
+#ifndef _PSXPAD_H
+#define _PSXPAD_H
+
+/* Pad bits defines. */
+
+#define PAD_LEFT (1<<15)
+#define PAD_RIGHT (1<<13)
+#define PAD_UP (1<<12)
+#define PAD_DOWN (1<<14)
+#define PAD_L2 1
+#define PAD_R2 (1<<1)
+#define PAD_L1 (1<<2)
+#define PAD_R1 (1<<3)
+#define PAD_TRIANGLE (1<<4)
+#define PAD_CIRCLE (1<<5)
+#define PAD_CROSS (1<<6)
+#define PAD_SQUARE (1<<7)
+#define PAD_SELECT (1<<8)
+#define PAD_LANALOGB (1<<9)
+#define PAD_RANALOGB (1<<10)
+#define PAD_START (1<<11)
+
+#define PAD_READ_RAW_SIZE 21
+
+/**
+ * These values below are to be used for evalauting the type field of the
+ * psx_pad_state structure
+ */
+
+enum psx_pad_types
+{
+ /** No pad connected. */
+ PADTYPE_NONE,
+ /** Normal pad. */
+ PADTYPE_NORMALPAD,
+ /** Analog joystick, the early ones with analog. */
+ PADTYPE_ANALOGJOY,
+ /** Analog pad, the Dual Shock controller. */
+ PADTYPE_ANALOGPAD,
+ /** Namco NeGcon. Many steering wheels implement this protocol as well. */
+ PADTYPE_NEGCON, // Namco NeGcon
+ /** Konami Justifier gun controller. Many third party gun controllers implement this protocol. */
+ PADTYPE_KONAMIGUN,
+ /** Unknown pad type. */
+ PADTYPE_UNKNOWN
+};
+
+/**
+ * This structure contains the state of the pad (game controller) after
+ * polling it with PSX_PollPad()
+ */
+
+typedef struct
+{
+ /** Status. 0 on success, 255 on failure. */
+ unsigned char status;
+ /** Id. Bits 7-4 indicate the type. Bits 3-0 indicate the number of words
+ in the raw packet returned by the controller. */
+ unsigned char id;
+ /** Type of pad. To be evaluated with the types in the psx_pad_types enum */
+ unsigned char type;
+ /** Button bitmask. To be checked by AND'ing with the defines in
+ psxpad.h for buttons. If a bit is set for a button, it is pressed.
+ Checking the pad type to use this bitmask is not necessary at all,
+ and if button emulations are set up, this may not represent the
+ buttons actually pressed. Also reliable when type is PADTYPE_UNKNOWN */
+ unsigned short buttons;
+
+ /** Extra data for non-normal controllers.
+ You should check the value of the type field before accessing any of this data
+ */
+
+ union extra
+ {
+ /** Data for analog joysticks.
+ * @attention Due to the poor calibration of the analog sticks, it is recommended
+ * that you do not assume "left" for X values lower than zero and
+ * "right" for X values higher than zero.
+ *
+ * @attention It is recommended to pick a value, for instance 64, and assume
+ * "left" for values lower than -64, "right" for values higher than 64,
+ * and no movement for values between -64 and 64.
+ *
+ * @attention The same is valid for Y values, "up" and "down".
+ **/
+
+ struct analogJoy
+ {
+ /** X coordinates for the left analog stick and the right analog stick
+ * @par Value
+ * Totally left: -128, totally right: 127
+ */
+ signed char x[2];
+ /** Y coordinates for the left analog stick and the right analog stick
+ * @par Value
+ * Totally up: -128, totally down: 127
+ */
+ signed char y[2];
+ }analogJoy;
+
+ /** Data for analog joypads (controller).
+ * @attention Due to the poor calibration of the analog sticks, it is recommended
+ * that you do not assume "left" for X values lower than zero and
+ * "right" for X values higher than zero.
+ *
+ * @attention It is recommended to pick a value, for instance 64, and assume
+ * "left" for values lower than -64, "right" for values higher than 64,
+ * and no movement for values between -64 and 64.
+ *
+ * @attention The same is valid for Y values, "up" and "down".
+ **/
+
+ struct analogPad
+ {
+ /** X coordinates for the left analog stick and the right analog stick
+ * @par Value
+ * Totally left: -128, totally right: 127
+ */
+ signed char x[2];
+ /** Y coordinates for the left analog stick and the right analog stick
+ * @par Value
+ * Totally up: -128, totally down: 127
+ */
+ signed char y[2];
+ }analogPad;
+
+ /** Data for Namco NeGcon and steering wheels using its protocol.
+ * Many steering wheels use this protocol or can switch to it if desired.
+ */
+
+ struct negCon
+ {
+ /**
+ * Steering wheel position.
+ *
+ * Unlike analog sticks, the steering is accurate and this value is reliable.
+ *
+ * @par Value
+ * When steering left it is lower than zero, when steering right it is higher than zero
+ * and when not steering at all it is zero.
+ */
+
+ signed char steering;
+
+ /**
+ * Pressure for button I (1).
+ * @par Value
+ * 0 = not pressed, 255 = maximum pressure
+ */
+
+ unsigned char one;
+
+ /**
+ * Pressure for button II (2).
+ * @par Value
+ * 0 = not pressed, 255 = maximum pressure
+ */
+
+ unsigned char two;
+
+ /**
+ * Pressure for "L" shoulder button.
+ * @par Value
+ * 0 = not pressed, 255 = maximum pressure
+ */
+
+ unsigned char shoulder;
+ }negCon;
+ }extra;
+}psx_pad_state;
+
+void QueryPAD(int pad_n, unsigned char *in, unsigned char *out, int len);
+void pad_read_raw(int pad_n, unsigned char *arr);
+void pad_escape_mode(int pad_n, int enable);
+void pad_enable_vibration(int pad_n);
+void pad_set_vibration(int pad_n, unsigned char small, unsigned char big);
+
+#endif
diff --git a/libpsx/include/psxsio.h b/libpsx/include/psxsio.h
new file mode 100644
index 0000000..9756f1d
--- /dev/null
+++ b/libpsx/include/psxsio.h
@@ -0,0 +1,121 @@
+#ifndef _PSXSIO_H
+#define _PSXSIO_H
+
+/** Bitrate reload factors */
+
+enum sio_reload_factors
+{
+ /** STOP */
+ SIO_REL_STOP = 0,
+ /** MUL1 */
+ SIO_REL_MUL1 = 1,
+ /** MUL16 */
+ SIO_REL_MUL16 = 2,
+ /** MUL64 */
+ SIO_REL_MUL64 = 3
+};
+
+/** Character (data) length settings */
+
+enum sio_data_len
+{
+ /** Data Length = 5 bits */
+ SIO_DATA_LEN_5 = 0,
+ /** Data Length = 6 bits */
+ SIO_DATA_LEN_6 = 1,
+ /** Data Length = 7 bits */
+ SIO_DATA_LEN_7 = 2,
+ /** Data Length = 8 bits */
+ SIO_DATA_LEN_8 = 3
+};
+
+/** Stop bit length settings */
+
+enum sio_stop_bit
+{
+ /** Length = 1 bit */
+ SIO_STOP_BIT_1 = 1,
+ /** Length = 1.5 bits */
+ SIO_STOP_BIT_1_5 = 2,
+ /** Length = 2 bits */
+ SIO_STOP_BIT_2 = 3
+};
+
+/** Parity settings */
+
+enum sio_parity
+{
+ /** No parity */
+ SIO_PARITY_NONE = 0,
+ /** Odd parity */
+ SIO_PARITY_ODD = 1,
+ /** Even parity */
+ SIO_PARITY_EVEN = 3
+};
+
+/** SIO FIFO Buffer (TX/RX) Register [Read/Write] */
+#define SIO_TX_RX *((volatile unsigned char*)0x1F801050)
+/** SIO Status Register [Read Only] */
+#define SIO_STAT *((volatile unsigned short*)0x1F801054)
+/** SIO Mode Register [Read/Write] */
+#define SIO_MODE *((volatile unsigned short*)0x1F801058)
+/** SIO Control Register [Read/Write] */
+#define SIO_CTRL *((volatile unsigned short*)0x1F80105A)
+/** SIO Baud Rate Register [Read/Write] */
+#define SIO_BPSV *((volatile unsigned short*)0x1F80105E)
+
+
+/**
+ * Initialize SIO communication at the specified bitrate (baud rate).
+ * Mode is 8N1. (Data Length = 8 bit, No parity, Stop bit Length = 1 bit)
+ * @param bitrate Bitrate (baud rate)
+ */
+void SIOStart(int bitrate);
+
+/**
+ * Same as SIOStart() but with more control.
+ * IMPORTANT: Must use defined macros.
+ * For example setting datalength to 5 should be done with "SIO_DATA_LEN_5"
+ * and not by simply passing 5 as an argument.
+ *
+ * @param bitrate Bit rate (baud rate)
+ * @param datalength Character (data) length
+ * @param parity Parity
+ * @param stopbit Stop bit length
+ */
+void SIOStartEx(int bitrate, int datalength, int parity, int stopbit);
+
+/**
+ * Shuts down SIO communication.
+ */
+void SIOStop(void);
+
+/**
+ * Read a single byte from the input buffer.
+ * @return Data byte from input buffer
+ */
+unsigned char SIOReadByte(void);
+
+/**
+ * Send a single byte to the output buffer.
+ * @param data Byte to send
+ */
+void SIOSendByte(unsigned char data);
+
+/**
+ * Check if any data is waiting in the input buffer.
+ * Must be used when fetching data otherwise incorrect data could be read (usually 0x00).
+ * @return Non-zero if there is data waiting in the input buffer, zero otherwise.
+ */
+int SIOCheckInBuffer(void);
+
+/**
+ * Check if port is ready to send data (previous operation finished).
+ * Must be used when sending data as the output buffer is only 2 bytes long.
+ * @return Non-zero if port is ready to send data, zero otherwise.
+ */
+int SIOCheckOutBuffer(void);
+
+
+
+#endif
diff --git a/libpsx/include/psxspu.h b/libpsx/include/psxspu.h
new file mode 100644
index 0000000..0de3656
--- /dev/null
+++ b/libpsx/include/psxspu.h
@@ -0,0 +1,225 @@
+#ifndef _SPU_H
+#define _SPU_H
+
+#define SPU_ADDR *((volatile unsigned short*)0x1f801da6)
+#define SPU_DATA *((volatile unsigned short*)0x1f801da8)
+#define SPU_CONTROL *((volatile unsigned short*)0x1f801daa)
+#define SPU_STATUS *((volatile unsigned short*)0x1f801dac)
+#define SPU_STATUS2 *((volatile unsigned short*)0x1f801dae)
+#define SPU_MVOL_L *((volatile unsigned short*)0x1f801d80)
+#define SPU_MVOL_R *((volatile unsigned short*)0x1f801d82)
+#define SPU_REVERB_L *((volatile unsigned short*)0x1f801d84)
+#define SPU_REVERB_R *((volatile unsigned short*)0x1f801d86)
+#define SPU_KEY_ON1 *((volatile unsigned short*)0x1f801d88)
+#define SPU_KEY_ON2 *((volatile unsigned short*)0x1f801d8a)
+#define SPU_KEY_OFF1 *((volatile unsigned short*)0x1f801d8c)
+#define SPU_KEY_OFF2 *((volatile unsigned short*)0x1f801d8e)
+#define SPU_KEY_FM_MODE1 *((volatile unsigned short*)0x1f801d90)
+#define SPU_KEY_FM_MODE2 *((volatile unsigned short*)0x1f801d92)
+#define SPU_KEY_NOISE_MODE1 *((volatile unsigned short*)0x1f801d94)
+#define SPU_KEY_NOISE_MODE2 *((volatile unsigned short*)0x1f801d96)
+#define SPU_KEY_REVERB_MODE1 *((volatile unsigned short*)0x1f801d98)
+#define SPU_KEY_REVERB_MODE2 *((volatile unsigned short*)0x1f801d9a)
+#define SPU_CD_MVOL_L *((volatile unsigned short*)0x1f801db0)
+#define SPU_CD_MVOL_R *((volatile unsigned short*)0x1f801db2)
+#define SPU_EXT_VOL_L *((volatile unsigned short*)0x1f801db4)
+#define SPU_EXT_VOL_R *((volatile unsigned short*)0x1f801db6)
+#define SPU_REVERB_WORK_ADDR *((volatile unsigned short*)0x1f801da2)
+#define SPU_VOICE_BASE_ADDR(x) (0x1f801c00 + (x<<4))
+
+/** Start address of sound data in Sound RAM */
+#define SPU_DATA_BASE_ADDR 0x1010
+/** Maximum volume. */
+#define SPU_MAXVOL 0x3FFF
+
+/** VAG file */
+
+typedef struct
+{
+ /** Version. */
+ unsigned int version;
+ /** Data size. */
+ unsigned int data_size;
+ /** Sample rate. */
+ unsigned int sample_rate;
+ /** Name */
+ unsigned char name[16];
+ /** Pointer to sound data */
+ void *data;
+ /** Address in Sound RAM where the sound data was uploaded */
+ unsigned int spu_addr;
+ /** [Runtime] Voice this VAG is currently being played on */
+ char cur_voice;
+}SsVag;
+
+/**
+ * Set voice volume.
+ * @param voice Voice number (0-23)
+ * @param left Left channel volume
+ * @param right Right channel volume
+ */
+
+void SsVoiceVol(int voice, unsigned short left, unsigned short right);
+
+/**
+ * Set voice pitch.
+ * @param voice Voice
+ * @param pitch Pitch.
+ */
+
+void SsVoicePitch(int voice, unsigned short pitch);
+
+
+/**
+ * Set start Sound RAM address for voice.
+ * @param voice Voice
+ * @param addr Start address in Sound RAM (multiplier of 8)
+ */
+
+void SsVoiceStartAddr(int voice, unsigned int addr);
+
+/**
+ * Set ADSR level for voice
+ * @param voice Voice
+ * @param level ADSR level
+ * @param rate ADSR rate
+ */
+
+void SsVoiceADSRRaw(int voice, unsigned short level, unsigned short rate);
+
+/**
+ * Set repeat address for voice
+ * @param voice Voice
+ * @param addr Address in Sound RAM (multiplier of 8)
+ */
+
+void SsVoiceRepeatAddr(int voice, unsigned int addr);
+
+/**
+ * Set a voice to 'on'. This has the effect of playing the sound specified for the voice.
+ * @param voice Voice
+ */
+
+void SsKeyOn(int voice);
+
+/**
+ * Set a voice to 'off'. This stops the sound specified for the voice.
+ * @param voice Voice
+ */
+
+void SsKeyOff(int voice);
+
+/**
+ * Set the voices specified by the bitmask to 'on'. Like SsKeyOn()
+ * @param mask Bitmask
+ */
+
+void SsKeyOnMask(int mask);
+
+/**
+ * Set the voices specified by the bitmask to 'off'. Like SsKeyOff()
+ * @param mask Bitmask
+ */
+
+void SsKeyOffMask(int mask);
+
+/**
+ * Wait for the SPU to be ready.
+ */
+
+void SsWait(void);
+
+/**
+ * Intialize the SPU.
+ */
+
+void SsInit(void);
+
+/**
+ * Uploads sound data in PSX ADPCM format to Sound RAM.
+ * @param addr Pointer to PSX ADPCM sound data in main RAM
+ * @param size Sound data size
+ * @param spu_addr Destination address in Sound RAM (multiplier of 8).
+ */
+
+void SsUpload(void *addr, int size, int spu_addr);
+
+/**
+ * Converts a sampling rate in hertz to PlayStation pitch rate used by the SPU.
+ * @param hz Sampling rate in hertz.
+ * @return PlayStation pitch rate
+ */
+
+unsigned short SsFreqToPitch(int hz);
+
+/**
+ * Reads information from a buffer containg a VAG file and stores it inside a SsVag structure.
+ * @param vag Pointer to structure in which to store information.
+ * @param data Pointer to VAG file data
+ */
+
+int SsReadVag(SsVag *vag, void *data);
+
+/**
+ * Uploads the sound data specified by a SsVag structure to the specified address in Sound RAM.
+ * The SsVag structure can then be used for playing with SsPlayVag()
+ * @param vag Pointer to SsVag structure
+ * @param spu_addr Destination address in Sound RAM (multiplier of 8)
+ */
+
+void SsUploadVagEx(SsVag *vag, int spu_addr);
+
+
+/**
+ * Uploads the sound data specified by a SsVag structure to Sound RAM, beginning from the
+ * base of usable Sound RAM and continuing from there, in an automatic fashion.
+ * @param vag Pointer to SsVag structure
+ */
+
+void SsUploadVag(SsVag *vag);
+
+/**
+ * Plays the sound specified by the SsVag structure at specified voice and volume.
+ * @param vag Pointer to SsVag structure
+ * @param voice Voice
+ * @param vl Left channel volume
+ * @param vr Right channel volume
+ */
+
+void SsPlayVag(SsVag *vag, unsigned char voice, unsigned short vl,
+ unsigned short vr);
+
+/**
+ * Stops the sound specified by the SsVag structure
+ * @param vag Pointer to SsVag structure
+ */
+
+void SsStopVag(SsVag *vag);
+
+/**
+ * Tell SsUploadVag() to start uploading from the base of usable Sound RAM again.
+ */
+
+void SsResetVagAddr(void);
+
+/**
+ * Enable CD Audio.
+ */
+
+void SsEnableCd(void);
+
+/**
+ * Enable External audio. (???)
+ */
+
+void SsEnableExt(void);
+
+/**
+ * Set CD Audio volume.
+ * @param left Left channel volume
+ * @param right Right channel volume
+ */
+
+void SsCdVol(unsigned short left, unsigned short right);
+
+#endif
diff --git a/libpsx/include/psxutil.h b/libpsx/include/psxutil.h
new file mode 100644
index 0000000..0dba29e
--- /dev/null
+++ b/libpsx/include/psxutil.h
@@ -0,0 +1,20 @@
+// psxutil.h
+
+#ifndef _PSXUTIL_H
+#define _PSXUTIL_H
+
+/**
+ * Given a button bitmask, returns the name for the buttons
+ * that the bitmask reports activated.
+ * If more than one button is found in the bitmask, it is specified by the symbol &
+ * followed by the other button name and so on...
+ *
+ * @param button Button bitmask as returned by PSX_ReadPad()
+ * @param out Pointer to a string, to which the button names will be output
+ * @param out_len The maximum length of the string
+ * @return The pointer to passed as the "out" parameter
+ */
+
+char *PSX_GetButtonName(unsigned short button, char *out, unsigned int out_len);
+
+#endif
diff --git a/libpsx/include/runexe.h b/libpsx/include/runexe.h
new file mode 100644
index 0000000..c42c9ee
--- /dev/null
+++ b/libpsx/include/runexe.h
@@ -0,0 +1,17 @@
+#ifndef _PSXSDK_RUNEXE_H
+#define _PSXSDK_RUNEXE_H
+
+/**
+ * Replaces running executable code with code from another executable
+ * stored in a memory buffer and starts executing at the starting point for the new code.
+ * The executable must be in PSX-EXE format (such as generated by elf2exe).
+ * Moreover, PSX_InitEx() must have been initialized by specifying the PSX_INIT_SAVESTATE flag.
+ * @param exeBuffer Pointer to memory buffer where executable is stored
+ * @return On success this function has the effect of making the PlayStation
+ * run another executable, so obviously this function will never return in that case.
+ * This function will only return on failure, where it will return 0.
+ */
+
+int PSX_RunExe(void *exeBuffer);
+
+#endif
diff --git a/libpsx/include/search.h b/libpsx/include/search.h
new file mode 100644
index 0000000..7f8574e
--- /dev/null
+++ b/libpsx/include/search.h
@@ -0,0 +1,14 @@
+/*
+ * search.h
+ *
+ * PSXSDK
+ */
+
+#ifndef _SEARCH_H
+#define _SEARCH_H
+
+void *lsearch(void *key , void *base , int belp , int width , int (*cmp)(void * , void *));
+void *bsearch(void *key , void *base , int nel , int size , int (*cmp)(void * , void *));
+
+#endif
+
diff --git a/libpsx/include/stdint.h b/libpsx/include/stdint.h
new file mode 100644
index 0000000..e6bb9d2
--- /dev/null
+++ b/libpsx/include/stdint.h
@@ -0,0 +1,14 @@
+/* stdint.h */
+
+#ifndef _STDINT_H
+#define _STDINT_H
+
+#include <inttypes.h>
+
+// Added ifndef or otherwise GCC 4.8 would complain
+
+#ifndef __PTRDIFF_TYPE__
+typedef unsigned int ptrdiff_t;
+#endif
+
+#endif
diff --git a/libpsx/include/stdio.h b/libpsx/include/stdio.h
new file mode 100644
index 0000000..1bf7a99
--- /dev/null
+++ b/libpsx/include/stdio.h
@@ -0,0 +1,170 @@
+/*
+ * stdio.h implementation for PSXSDK
+ */
+
+#ifndef _STDIO_H
+#define _STDIO_H
+
+#ifdef _PSXSDK_WRAPPER
+
+/*
+ * Dirty hack...
+ */
+
+#include "/usr/include/stdio.h"
+
+#else
+
+#include <types.h>
+
+#include <stdarg.h>
+#include <stdbool.h>
+
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+
+#define EOF -1
+
+/* NULL */
+#ifndef NULL
+#define NULL (void*)0
+#endif
+
+enum stdio_directions
+{
+ STDIO_DIRECTION_BIOS,
+ STDIO_DIRECTION_SIO
+};
+
+enum file_devices
+{
+ FDEV_UNKNOWN,
+ FDEV_CDROM,
+ FDEV_MEMCARD,
+ FDEV_CONSOLE
+};
+
+extern int __stdio_direction;
+
+/**
+ * File stream
+ */
+
+typedef struct
+{
+ /** File descriptor, as returned by open() */
+ int fildes;
+ /** Current file position */
+ unsigned int pos;
+ /** File access mode */
+ unsigned int mode;
+ /** Device ID */
+ unsigned int dev;
+ /** Size in bytes */
+ unsigned int size;
+ /** Used internally by fopen(), 0 if free, 1 if occupied */
+ unsigned int used;
+ /** End-of-File marker */
+ unsigned int eof;
+ /** Error marker */
+ unsigned int error;
+}FILE;
+
+/* Console functions */
+
+int putchar(int c);
+int puts(const char *str);
+
+/**
+ * BIOS printf() implementation. Does not support floating point.
+ * NOTE: when redirect_stdio_to_sio() is used, PSXSDK's internal implementation is used instead.
+ */
+
+extern int printf(const char *format, ...);
+
+
+#ifdef __IN_LIBPSX
+
+// Only for code in libpsx
+
+// If PSXSDK_DEBUG is defined, dprintf() calls are turned into printf() calls
+// otherwise they are left out
+
+#ifdef PSXSDK_DEBUG
+ #define dprintf printf
+#else
+ #define dprintf(fmt, ...)
+#endif
+
+#endif
+
+int vsnprintf(char *string, size_t size, const char *fmt, va_list ap);
+int vsprintf(char *string, const char *fmt, va_list ap);
+int sprintf(char *string, const char *fmt, ...);
+int snprintf(char *string, size_t size, const char *fmt, ...);
+int vprintf(char *fmt, va_list ap);
+
+FILE *fdopen(int fildes, const char *mode);
+FILE *fopen(const char *path, const char *mode);
+int fclose(FILE *stream);
+int fread(void *ptr, int size, int nmemb, FILE *f);
+int fwrite(void *ptr, int size, int nmemb, FILE *f);
+
+int fgetc(FILE *f);
+int ftell(FILE *f);
+int fseek(FILE *f, int offset, int whence);
+
+int fputs(const char *str, FILE *stream);
+void clearerr(FILE *stream);
+int feof(FILE *stream);
+int ferror(FILE *stream);
+int fileno(FILE *stream);
+
+#define getc(f) fgetc(f)
+
+int rename(const char *oldname, const char *newname);
+int remove(const char *filename);
+
+#ifndef __cplusplus
+// Define delete(x) to be remove(x) only when compiling plain C.
+#define delete(x) remove(x)
+#endif
+
+/**
+ * Redirects STDIO to SIO (serial port)
+ */
+
+void redirect_stdio_to_sio(void);
+
+/**
+ * Sets whether a carriage return must be written before a line feed.
+ * In simpler words, whether '\n' must be translated to a '\r\n' sequence.
+ * If you come from the Unix world, you most likely want to set this.
+ *
+ * @param setting New status of the setting (0 = disabled, 1 = enabled)
+ */
+
+void sio_stdio_mapcr(unsigned int setting);
+
+/**
+ * scanf and friends
+ */
+
+int vsscanf(const char *str, const char *fmt, va_list ap);
+int sscanf(const char *str, const char *fmt, ...);
+
+
+/**
+ * STDIO for SIO
+ */
+
+int sio_putchar(int c);
+int sio_puts(const char *str);
+int sio_printf(const char *fmt, ...);
+int sio_vprintf(const char *fmt, va_list ap);
+
+#endif
+
+#endif
+
diff --git a/libpsx/include/stdlib.h b/libpsx/include/stdlib.h
new file mode 100644
index 0000000..0242213
--- /dev/null
+++ b/libpsx/include/stdlib.h
@@ -0,0 +1,66 @@
+/*
+ * stdlib.h
+ *
+ * Standard library functions
+ *
+ * PSXSDK
+ */
+
+#ifndef _STDLIB_H
+#define _STDLIB_H
+
+typedef unsigned int size_t;
+typedef signed int ssize_t;
+
+/* Conversion functions */
+
+int atoi(const char *s);
+long atol(const char *s);
+char *itoa(int value, char *str, int base);
+char *ltoa(long value, char *str, int base);
+char *lltoa(long long value, char *str, int base);
+char *utoa(unsigned int value, char *str, int base);
+char *ultoa(unsigned long value, char *str, int base);
+char *ulltoa(unsigned long long value, char *str, int base);
+//extern char atob(char *s); // Is this right?
+
+
+// Random number functions
+
+#define RAND_MAX 0x7fffffff
+
+int rand(void);
+void srand(unsigned int seed);
+
+// Quick sort
+
+void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
+
+// Memory allocation functions
+
+//#warning "malloc() family of functions NEEDS MORE TESTING"
+
+void *malloc(size_t size);
+void free(void *buf);
+void *calloc(size_t number, size_t size);
+void *realloc(void *buf , size_t n);
+
+int abs(int x);
+long long strtoll(const char *nptr, char **endptr, int base);
+long strtol(const char *nptr, char **endptr, int base);
+double strtod(const char *nptr, char **endptr);
+long double strtold(const char *nptr, char **endptr);
+float strtof(const char *nptr, char **endptr);
+
+// Misc
+void abort(void);
+void exit(int status);
+void call_atexit_callbacks(void);
+
+// Program return codes
+
+#define EXIT_SUCCESS 0
+#define EXIT_FAILURE 1
+
+#endif
+
diff --git a/libpsx/include/string.h b/libpsx/include/string.h
new file mode 100644
index 0000000..5956b0d
--- /dev/null
+++ b/libpsx/include/string.h
@@ -0,0 +1,64 @@
+/*
+ * string.h
+ *
+ * Prototypes for string functions of the C library
+ *
+ * PSXSDK
+ */
+
+// NOTE: The BIOS was found to be unreliable for many functions,
+// so it is not used anymore for the libc.
+
+#ifndef _STRING_H
+#define _STRING_H
+
+#include <strings.h> // for backwards compatibility
+#include <types.h>
+
+char *strcat(char *s , const char *append);
+char *strncat(char *s , const char *append, size_t n);
+int strcmp(const char *dst , const char *src);
+int strncmp(const char *dst , const char *src , size_t len);
+int stricmp(const char *s1, const char *s2); // alias of strcasecmp
+int strnicmp(const char *s1, const char *s2, size_t len); // alias of strncasecmp
+char *strcpy(char *dst , const char *src);
+char *strncpy(char *dst , const char *src , size_t n);
+int strlen(const char *s);
+int strnlen(const char *s, size_t maxlen);
+char *strchr(const char *s , int c);
+char *strrchr(const char *s , int c);
+char *strpbrk(const char *dst , const char *src);
+int strspn(const char *s , const char *charset);
+int strcspn(const char *s , const char *charset);
+char *strsep(char **stringp, const char *delim);
+char *strtok(char *str, const char *sep);
+char *strstr(const char *big , const char *little);
+char *strcasestr(const char *big, const char *little);
+char *strlwr(char *string);
+char *strupr(char *string);
+char *strdup(const char *str);
+char *strndup(const char *str, size_t len);
+int strlcpy(char *dst, const char *src, size_t size);
+int strlcat(char *dst, const char *src, size_t size);
+int strcoll(const char *dst , const char *src);
+
+char *strerror(int errnum);
+int strerror_r(int errnum, char *strerrbuf, size_t buflen);
+
+void *memset(void *dst , char c , size_t n);
+void *memmove(void *dst , const void *src , size_t n);
+int memcmp(const void *b1 , const void *b2 , size_t n);
+void *memchr(void *s , int c , size_t n);
+void *memrchr(void *b, int c, size_t len);
+void *memcpy(void *dst , const void *src , size_t len);
+void *memccpy(void *dst, const void *src, int c, size_t len);
+void *memmem(const void *big, size_t big_len, const void *little, size_t little_len);
+
+// ffsl() and ffsll() are glibc extensions, and are in string.h
+// instead of strings.h (like ffs()) for some reason..
+
+int ffsl(long value);
+int ffsll(long long value);
+
+#endif
+
diff --git a/libpsx/include/strings.h b/libpsx/include/strings.h
new file mode 100644
index 0000000..7e9082f
--- /dev/null
+++ b/libpsx/include/strings.h
@@ -0,0 +1,37 @@
+/*
+ * strings.h
+ *
+ * PSXSDK
+ */
+
+#ifndef _STRINGS_H
+#define _STRINGS_H
+
+#include <string.h>
+#include <types.h>
+
+#define bcopy(src,dst,len) memmove(dst,src,len)
+#define bzero(ptr, len) memset(ptr, 0, len)
+#define bcmp(b1,b2,len) memcmp(b1,b2,len)
+#define index(s, c) strchr(s, c)
+#define rindex(s, c) strrchr(s, c)
+
+int ffs(int value);
+char *index(const char *s, int c);
+char *rindex(const char *s, int c);
+
+/**
+ * Returns the number of bits set in the value
+ * @param value Value
+ * @return Number of bits set in value
+ */
+
+unsigned int popcount(unsigned int value);
+unsigned int popcountl(unsigned long value);
+unsigned int popcountll(unsigned long long value);
+unsigned int popcount32(uint32_t value);
+unsigned int popcount64(uint64_t value);
+int strcasecmp(const char *s1, const char *s2);
+int strncasecmp(const char *s1, const char *s2, size_t len);
+
+#endif
diff --git a/libpsx/include/sys/stat.h b/libpsx/include/sys/stat.h
new file mode 100644
index 0000000..5b0d7ca
--- /dev/null
+++ b/libpsx/include/sys/stat.h
@@ -0,0 +1,19 @@
+/**
+ * PSXSDK
+ *
+ * sys/stat.h
+ */
+
+#ifndef _SYS_STAT_H
+#define _SYS_STAT_H
+
+struct stat
+{
+ unsigned int st_size;
+ unsigned int st_blksize;
+ unsigned int st_blocks;
+};
+
+int stat(const char *path, struct stat *sb);
+
+#endif
diff --git a/libpsx/include/sys/types.h b/libpsx/include/sys/types.h
new file mode 100644
index 0000000..2d6df2a
--- /dev/null
+++ b/libpsx/include/sys/types.h
@@ -0,0 +1,11 @@
+#ifndef _SYS_TYPES_H
+#define _SYS_TYPES_H
+
+#include <types.h>
+
+typedef unsigned char u_char;
+typedef unsigned short u_short;
+typedef unsigned int u_int;
+typedef unsigned long u_long;
+
+#endif
diff --git a/libpsx/include/types.h b/libpsx/include/types.h
new file mode 100644
index 0000000..395dac4
--- /dev/null
+++ b/libpsx/include/types.h
@@ -0,0 +1,10 @@
+#ifndef _TYPES_H
+#define _TYPES_H
+
+typedef unsigned int size_t;
+typedef signed int ssize_t;
+
+#include <inttypes.h>
+#include <sys/types.h>
+
+#endif
diff --git a/libpsx/include/unistd.h b/libpsx/include/unistd.h
new file mode 100644
index 0000000..3ae6aa3
--- /dev/null
+++ b/libpsx/include/unistd.h
@@ -0,0 +1,22 @@
+/*
+ * unistd.h
+ *
+ * PSXSDK
+ */
+
+#ifndef _UNISTD_H
+#define _UNISTD_H
+
+#include <types.h>
+
+int lseek(int fd, int off, int whence);
+int read(int fd, void *buf, int nbytes);
+int write(int fd, void *buf, int nbytes);
+int close(int fd);
+int cd(char *dirname);
+
+void swab(const void *src, void *dst, ssize_t len);
+
+#define chdir(dirname) cd(dirname)
+
+#endif