Commit f583b797 authored by Libretro-Admin's avatar Libretro-Admin
Browse files

Cleanups

parent 22f70ac8
###
### Include this file in the first line of your Makefile when compiling
### EMULib application for FreeBSD, NetBSD, OpenBSD, Linux, or any other
### generic Unix flavor.
###
include ../../EMULib/Rules.gcc
CFLAGS += -I$(EMULIB)/Unix -I/usr/X11R6/include
DEFINES+= -DUNIX -DMITSHM -DBPS16
LIBS += -lX11 -lXext
OBJECTS+= $(EMUUNIX)
BASEDIR = ../..
EMULIB = $(BASEDIR)/EMULib
LIBARM = $(BASEDIR)/ARM
LIBZ80 = $(BASEDIR)/Z80
LIB6502 = $(BASEDIR)/M6502
LIB68K = $(BASEDIR)/M68000
LIBGBZ = $(BASEDIR)/GBZ80
PRIVATE = $(BASEDIR)/Private
CXX ?= g++
CC ?= gcc
CPP ?= cpp
MOC ?= moc
SDT2GAS ?= perl $(EMULIB)/Tools/sdt2gas.pl
CFLAGS += -I. -I.. -I$(EMULIB)
DEFINES += -DZLIB
LIBS += -lz
# EMULib sound, console, and other utility functions
OBJECTS = $(EMULIB)/EMULib.o $(EMULIB)/Sound.o \
$(EMULIB)/Image.o $(EMULIB)/Console.o \
$(EMULIB)/Record.o $(EMULIB)/NetPlay.o \
$(EMULIB)/Touch.o
# Platform-specific EMULib files
EMUUNIX = $(EMULIB)/Unix/LibUnix.o $(EMULIB)/Unix/SndUnix.o \
$(EMULIB)/Unix/NetUnix.o
EMUMAEMO= $(EMULIB)/Maemo/LibMaemo.o $(EMULIB)/Unix/GConf.o \
$(EMULIB)/Unix/NetUnix.o $(EMULIB)/LibARM-BPP16.o
EMUMEEGO= $(EMULIB)/Meego/LibMeego.o $(EMULIB)/Unix/GConf.o \
$(EMULIB)/Unix/NetUnix.o $(EMULIB)/LibARM-BPP16.o
EMUSTMP = $(EMULIB)/STMP3700/LibSTMP3700.o $(EMULIB)/Unix/SndALSA.o \
$(EMULIB)/LibARM-BPP16.o $(EMULIB)/LibARM-BPP32.o
EMUNXC = $(EMULIB)/NXC2600/LibNXC2600.o $(EMULIB)/Unix/SndSDL.o
# SHA1 computation library
SHA1 = $(EMULIB)/SHA1.o
# CRC32 computation library
CRC32 = $(EMULIB)/CRC32.o
# .IPS file patch library
IPS = $(EMULIB)/IPS.o
# .INI file access library
INIFILE = $(EMULIB)/INIFile.o
# Record/replay library
RECORD = $(EMULIB)/Record.o
# Cheat code hunting library
HUNT = $(EMULIB)/Hunt.o
# 720kB floppy utility library
FLOPPY = $(EMULIB)/Floppy.o
# Disk image utility library
FDIDISK = $(EMULIB)/FDIDisk.o
# CPU emulation libraries
ARM = $(LIBARM)/ARM.o $(LIBARM)/ConDebug.o
Z80 = $(LIBZ80)/Z80.o $(LIBZ80)/ConDebug.o
GBZ80 = $(LIBGBZ)/Z80.o $(LIBGBZ)/ConDebug.o
M6502 = $(LIB6502)/M6502.o $(LIB6502)/ConDebug.o
M68000 = $(LIB68K)/M68000.o $(LIB68K)/ConDebug.o
# Optimized CPU emulation
ARMONARM = $(ARM) \
$(LIBARM)/ARMonARM/OptARM16.o \
$(LIBARM)/ARMonARM/OptARM32.o
M6502ONARM = $(M6502) $(LIB6502)/M6502-ARM.o
# TMS9918/9928/9929 VDP emulation
TMS9918 = $(EMULIB)/TMS9918.o $(EMULIB)/DRV9918.o
# Disk controller emulation libraries
WD1793 = $(EMULIB)/WD1793.o
UPD765 = $(EMULIB)/uPD765.o
WD2793 = $(WD1793)
I8272 = $(UPD765)
# C93xx/C94xx EEPROM chips
C93XX = $(EMULIB)/C93XX.o
# Intel 8255 PPI chip
I8255 = $(EMULIB)/I8255.o
# Sound chip emulation libraries
AY8910 = $(EMULIB)/AY8910.o
SN76489 = $(EMULIB)/SN76489.o
SAA1099 = $(EMULIB)/SAA1099.o
YM2413 = $(EMULIB)/YM2413.o
SCC = $(EMULIB)/SCC.o
GBSND = $(PRIVATE)/GBSND.o
NESSND = $(PRIVATE)/NESSND.o
# Cartridge parsing utilities
NESCART = $(EMULIB)/NESCarts.o
GBCART = $(EMULIB)/GBCarts.o
GBACART = $(EMULIB)/GBACarts.o
DSCART = $(EMULIB)/DSCarts.o
%.o: %.c
$(CC) $(CFLAGS) $(DEFINES) -c -o $@ $<
%.o: %.cc
$(CC) $(CFLAGS) $(DEFINES) -c -o $@ $<
%.o: %.cpp
$(CXX) $(CFLAGS) $(DEFINES) -c -o $@ $<
%.o: %.s
$(CC) $(CFLAGS) $(DEFINES) -c -o $@ $<
%.s: %.asm
$(SDT2GAS) $< >$@
%.moc: %.h
$(MOC) $(DEFINES) $< >$@
This diff is collapsed.
/** EMULib Emulation Library *********************************/
/** **/
/** LibUnix.h **/
/** **/
/** This file contains Unix-dependent definitions and **/
/** declarations for the emulation library. **/
/** **/
/** Copyright (C) Marat Fayzullin 1996-2016 **/
/** You are not allowed to distribute this software **/
/** commercially. Please, notify me, if you make any **/
/** changes to this file. **/
/*************************************************************/
#ifndef LIBUNIX_H
#define LIBUNIX_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#ifdef MITSHM
#include <X11/extensions/XShm.h>
#endif
/* X11 defines "Status" to be "int" but this may shadow some */
/* of our application variables! */
#undef Status
#ifdef __cplusplus
extern "C" {
#endif
#define SND_CHANNELS 16 /* Number of sound channels */
#define SND_BITS 8
#define SND_BUFSIZE (1<<SND_BITS)
/** PIXEL() **************************************************/
/** Unix may use multiple pixel formats. **/
/*************************************************************/
#if defined(BPP32) || defined(BPP24)
#define PIXEL(R,G,B) (pixel)(((int)R<<16)|((int)G<<8)|B)
#define PIXEL2MONO(P) (((P>>16)&0xFF)+((P>>8)&0xFF)+(P&0xFF))/3)
#define RMASK 0xFF0000
#define GMASK 0x00FF00
#define BMASK 0x0000FF
#elif defined(BPP16)
#define PIXEL(R,G,B) (pixel)(((31*(R)/255)<<11)|((63*(G)/255)<<5)|(31*(B)/255))
#define PIXEL2MONO(P) (522*(((P)&31)+(((P)>>5)&63)+(((P)>>11)&31))>>8)
#define RMASK 0xF800
#define GMASK 0x07E0
#define BMASK 0x001F
#elif defined(BPP8)
#define PIXEL(R,G,B) (pixel)(((7*(R)/255)<<5)|((7*(G)/255)<<2)|(3*(B)/255))
#define PIXEL2MONO(P) (3264*((((P)<<1)&7)+(((P)>>2)&7)+(((P)>>5)&7))>>8)
#define RMASK 0xE0
#define GMASK 0x1C
#define BMASK 0x03
#endif
int ARGC;
char **ARGV;
/** InitUnix() ***********************************************/
/** Initialize Unix/X11 resources and set initial window. **/
/** title and dimensions. **/
/*************************************************************/
int InitUnix(const char *Title,int Width,int Height);
/** TrashUnix() **********************************************/
/** Free resources allocated in InitUnix() **/
/*************************************************************/
void TrashUnix(void);
/** InitAudio() **********************************************/
/** Initialize sound. Returns rate (Hz) on success, else 0. **/
/** Rate=0 to skip initialization (will be silent). **/
/*************************************************************/
unsigned int InitAudio(unsigned int Rate,unsigned int Latency);
/** TrashAudio() *********************************************/
/** Free resources allocated by InitAudio(). **/
/*************************************************************/
void TrashAudio(void);
/** PauseAudio() *********************************************/
/** Pause/resume audio playback. **/
/*************************************************************/
int PauseAudio(int Switch);
/** X11Window() **********************************************/
/** Open a window of a given size with a given title. **/
/*************************************************************/
Window X11Window(const char *Title,int Width,int Height);
/** X11GetColor **********************************************/
/** Get pixel for the current screen depth based on the RGB **/
/** values. **/
/*************************************************************/
unsigned int X11GetColor(unsigned char R,unsigned char G,unsigned char B);
#ifdef __cplusplus
}
#endif
#endif /* LIBUNIX_H */
<
/** EMULib Emulation Library *********************************/
/** **/
/** NetUnix.c **/
/** **/
/** This file contains standard communication routines for **/
/** Unix using TCP/IP via sockets. **/
/** **/
/** Copyright (C) Marat Fayzullin 1997-2016 **/
/** You are not allowed to distribute this software **/
/** commercially. Please, notify me, if you make any **/
/** changes to this file. **/
/*************************************************************/
#include "EMULib.h"
#include "NetPlay.h"
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <pthread.h>
#ifdef ANDROID
#define puts LOGI
#define printf LOGI
#endif
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
static volatile int IsServer = 0;
static volatile int Socket = -1;
static volatile int Blocking = 1;
static volatile int UseUDP = 0;
static volatile pthread_t Thr = 0;
static struct sockaddr_in PeerAddr;
/** ThrHandler() *********************************************/
/** This is the thread function responsible for asyncronous **/
/** connection process. **/
/*************************************************************/
static void *ThrHandler(void *Arg)
{
void **Args = (void **)Arg;
char *Server = (char *)Args[0];
unsigned int Port = (unsigned int)Args[1];
/* Try connecting */
NETConnect(Server,Port);
/* No longer need strdup()ed server name */
if(Server) free(Server);
/* Thread done */
Thr = 0;
return(0);
}
/** NETConnected() *******************************************/
/** Returns NET_SERVER, NET_CLIENT, or NET_OFF. **/
/*************************************************************/
int NETConnected(void)
{ return(Socket<0? NET_OFF:IsServer? NET_SERVER:NET_CLIENT); }
/** NETMyName() **********************************************/
/** Returns local hostname/address or 0 on failure. **/
/*************************************************************/
const char *NETMyName(char *Buffer,int MaxChars)
{
struct hostent *Host;
/* Have to have enough characters */
if(MaxChars<16) return(0);
/* Get local hostname */
gethostname(Buffer,MaxChars);
/* Look up address */
if(!(Host=gethostbyname(Buffer))) return(0);
/* Must have an address */
if(!Host->h_addr_list||!Host->h_addr_list[0]) return(0);
/* Copy address */
sprintf(Buffer,"%d.%d.%d.%d",
Host->h_addr_list[0][0],
Host->h_addr_list[0][1],
Host->h_addr_list[0][2],
Host->h_addr_list[0][3]
);
return(Buffer);
}
/** NETConnectAsync() ****************************************/
/** Asynchronous version of NETConnect(). **/
/*************************************************************/
int NETConnectAsync(const char *Server,unsigned short Port)
{
static void *Args[2];
/* Thread already exists, nothing to do */
if(Thr) return(1);
/* Close existing network connection */
NETClose();
/* Set up arguments */
Args[0] = (void *)(Server? strdup(Server):0);
Args[1] = (void *)(unsigned int)Port;
if(!Args[0]&&Server) return(0);
/* Create connection thread */
return(!!pthread_create((pthread_t *)&Thr,0,ThrHandler,Args));
}
/** UDPConnect() *********************************************/
/** Connects to Server:Port. When Server=0, becomes server, **/
/** waits at for an incoming request, and connects. Returns **/
/** the NetPlay connection status, like NETConnected(). **/
/*************************************************************/
int UDPConnect(const char *Server,unsigned short Port)
{
struct sockaddr_in MyAddr;
struct hostent *Host;
int SSocket,J;
pthread_t T;
/* Store current thread ID (or 0 if called synchronously) */
T = Thr;
/* Close existing network connection */
if(!T) NETClose();
/* Create UDP socket */
if((SSocket=socket(AF_INET,SOCK_DGRAM,0))<0) return(NET_OFF);
/* Set fields of the address structure */
memset(&MyAddr,0,sizeof(MyAddr));
MyAddr.sin_family = AF_INET;
MyAddr.sin_port = htons(Port);
MyAddr.sin_addr.s_addr = htonl(INADDR_ANY);
memcpy(&PeerAddr,&MyAddr,sizeof(PeerAddr));
/* If the server name is given, we first try to */
/* connect as a client. */
if(Server)
{
printf("UDP-CLIENT: Looking up '%s'...\n",Server);
/* Look up server address */
if(!(Host=gethostbyname(Server))) { close(SSocket);return(NET_OFF); }
/* Set server address */
memcpy(&PeerAddr.sin_addr,Host->h_addr,Host->h_length);
/* Bind socket */
if(bind(SSocket,(struct sockaddr *)&MyAddr,sizeof(MyAddr))<0)
{ close(SSocket);return(NET_OFF); }
printf("UDP-CLIENT: Client established.\n");
}
else
{
printf("UDP-SERVER: Becoming server...\n");
/* Accepting messages from any address */
PeerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
/* Bind socket */
if(bind(SSocket,(struct sockaddr *)&MyAddr,sizeof(MyAddr))<0)
{ close(SSocket);return(NET_OFF); }
printf("UDP-SERVER: Server established.\n");
}
/* Make communication socket blocking/non-blocking */
J=!Blocking;
if(ioctl(SSocket,FIONBIO,&J)<0) { close(SSocket);return(NET_OFF); }
/* Success */
IsServer = !Server;
UseUDP = 1;
Socket = SSocket;
return(Server? NET_SERVER:NET_CLIENT);
}
/** NETConnect() *********************************************/
/** Connects to Server:Port. When Server=0, becomes server, **/
/** waits at for an incoming request, and connects. Returns **/
/** the NetPlay connection status, like NETConnected(). **/
/*************************************************************/
int NETConnect(const char *Server,unsigned short Port)
{
struct sockaddr_in Addr;
struct hostent *Host;
struct timeval TV;
int AddrLength,LSocket,SSocket;
unsigned long J;
pthread_t T;
fd_set FDs;
/* Store current thread ID (or 0 if called synchronously) */
T = Thr;
/* Close existing network connection */
if(!T) NETClose();
/* Clear the address structure */
memset(&Addr,0,sizeof(Addr));
/* If the server name is given, we first try to */
/* connect as a client. */
if(Server)
{
printf("NET-CLIENT: Connecting to '%s'...\n",Server);
/* Look up server address */
if(!(Host=gethostbyname(Server))) return(NET_OFF);
printf("NET-CLIENT: Got server's IP address...\n");
/* Set fields of the address structure */
memcpy(&Addr.sin_addr,Host->h_addr,Host->h_length);
Addr.sin_family = AF_INET;
Addr.sin_port = htons(Port);
/* Create a socket */
if((SSocket=socket(AF_INET,SOCK_STREAM,0))<0) return(NET_OFF);
printf("NET-CLIENT: Created socket...\n");
/* Connecting... */
if(connect(SSocket,(struct sockaddr *)&Addr,sizeof(Addr))>=0)
{
printf("NET-CLIENT: Connected to the server...\n");
/* Make communication socket blocking/non-blocking */
J=!Blocking;
if(ioctl(SSocket,FIONBIO,&J)<0)
{ close(SSocket);return(NET_OFF); }
/* Disable Nagle algorithm */
J=1;
setsockopt(SSocket,SOL_TCP,TCP_NODELAY,&J,sizeof(J));
/* Succesfully connected as client */
IsServer = 0;
UseUDP = 0;
Socket = SSocket;
printf("NET-CLIENT: Connected to '%s'.\n",Server);
return(NET_CLIENT);
}
printf("NET-CLIENT: Failed connecting to '%s'.\n",Server);
/* Failed to connect as a client */
close(SSocket);
#ifdef ANDROID
/* Do not try becoming server! */
return(NET_OFF);
#endif
}
/* Connection as client either failed or hasn't */
/* been attempted at all. Becoming a server and */
/* waiting for connection request. */
printf("NET-SERVER: Becoming server...\n");
/* Set fields of the address structure */
Addr.sin_addr.s_addr = htonl(INADDR_ANY);
Addr.sin_family = AF_INET;
Addr.sin_port = htons(Port);
/* Create a listening socket */
if((LSocket=socket(AF_INET,SOCK_STREAM,0))<0) return(NET_OFF);
printf("NET-SERVER: Created socket...\n");
/* Bind listening socket */
if(bind(LSocket,(struct sockaddr *)&Addr,sizeof(Addr))<0)
{ close(LSocket);return(NET_OFF); }
printf("NET-SERVER: Bound socket...\n");
/* Listen for one client */
if(listen(LSocket,1)<0)
{ close(LSocket);return(NET_OFF); }
/* We will need address length */
AddrLength=sizeof(Addr);
printf("NET-SERVER: Accepting calls...\n");
/* No sockets yet */
FD_ZERO(&FDs);
#ifdef ANDROID
/* Accepting calls... */
for(SSocket=-1;(SSocket<0)&&VideoImg&&(T==Thr);)
{
/* Prepare data for select() */
FD_SET(LSocket,&FDs);
TV.tv_sec = 1;
TV.tv_usec = 0;
/* Listen and accept connection */
if(select(LSocket+1,&FDs,0,0,&TV)>0)
SSocket=accept(LSocket,(struct sockaddr *)&Addr,&AddrLength);
}
#else
/* Accepting calls... */
for(SSocket=-1,GetKey();(SSocket<0)&&VideoImg&&!GetKey()&&(T==Thr);)
{
/* Prepare data for select() */
FD_SET(LSocket,&FDs);
TV.tv_sec = 0;
TV.tv_usec = 100000;
/* Make sure UI is still running */
ProcessEvents(0);
/* Listen and accept connection */
if(select(LSocket+1,&FDs,0,0,&TV)>0)
SSocket=accept(LSocket,(struct sockaddr *)&Addr,&AddrLength);
}
#endif
printf("NET-SERVER: Client %s...\n",SSocket>=0? "connected":"failed to connect");
/* Done listening */
close(LSocket);
/* Client failed to connect */
if(SSocket<0) return(NET_OFF);
/* Make communication socket blocking/non-blocking */
J=!Blocking;
if(ioctl(SSocket,FIONBIO,&J)<0)
{ close(SSocket);return(NET_OFF); }
/* Disable Nagle algorithm */
J=1;
setsockopt(SSocket,SOL_TCP,TCP_NODELAY,&J,sizeof(J));
/* Client connected succesfully */
IsServer = 1;
UseUDP = 0;
Socket = SSocket;
printf("NET-SERVER: Client connected.\n");
return(NET_SERVER);
}
/** NETClose() ***********************************************/
/** Closes connection open with NETConnect(). **/
/*************************************************************/
void NETClose(void)
{
pthread_t T = Thr;
/* If there is a connection thread running, stop it */
if(T) { Thr=0;pthread_join(T,0); }
if(Socket>=0) close(Socket);
Socket = -1;
IsServer = 0;
}
/** NETBlock() ***********************************************/
/** Makes NETSend()/NETRecv() blocking or not blocking. **/
/*************************************************************/
int NETBlock(int Switch)
{
unsigned long J;
/* Toggle blocking if requested */
if(Switch==NET_TOGGLE) Switch=!Blocking;
/* If switching blocking... */
if((Switch==NET_OFF)||(Switch==NET_ON))
{
J=!Switch;
if((Socket<0)||(ioctl(Socket,FIONBIO,&J)>=0)) Blocking=Switch;
}
/* Return blocking state */
return(Blocking);
}
/** NETSend() ************************************************/
/** Send N bytes. Returns number of bytes sent or 0. **/
/*************************************************************/
int NETSend(const char *Out,int N)
{
int J,I;