1 /*******************************************************************************
2   CDTEST - Yabause Peripheral interface tester
3 
4   (c) Copyright 2014 Theo Berkau(cwx@cyberwarriorx.com)
5 
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or
9   (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 
20 *******************************************************************************/
21 
22 // This program is designed to be linked with any port's per*.c file.
23 // example: gcc perdx.c tools\pertest.c -o pertest.exe
24 
25 // Once it's compiled, run it with the peripheral core indexh as your argument
26 // example: pertest 0
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "../core.h"
32 #include "../cdbase.h"
33 #include "../m68kcore.h"
34 #include "../peripheral.h"
35 #include "../sh2core.h"
36 #include "../scsp.h"
37 #include "../vdp1.h"
38 #ifdef __linux__
39 #include "../perlinuxjoy.h"
40 #endif
41 #include "../persdljoy.h"
42 #ifdef __APPLE__
43 #include "../permacjoy.h"
44 #endif
45 #ifdef HAVE_DIRECTINPUT
46 #include "../perdx.h"
47 #endif
48 
49 #define PROG_NAME "PERTEST"
50 #define VER_NAME "1.0"
51 #define COPYRIGHT_YEAR "2014"
52 
53 int testspassed=0;
54 
55 PerInterface_struct *PERCoreList[] = {
56 	&PERDummy,
57 #ifdef __linux__
58 	&PERLinuxJoy,
59 #endif
60 #ifdef HAVE_LIBSDL
61 	&PERSDLJoy,
62 #endif
63 #ifdef __APPLE__
64 	&PERMacJoy,
65 #endif
66 #ifdef HAVE_DIRECTINPUT
67 	&PERDIRECTX,
68 #endif
69 	NULL
70 };
71 
72 PerInterface_struct *CurPer;
73 
74 // Unused functions and variables
75 SH2Interface_struct *SH2CoreList[] = {	NULL };
76 
77 VideoInterface_struct *VIDCoreList[] = { NULL };
78 
79 SoundInterface_struct *SNDCoreList[] = { NULL };
80 
81 M68K_struct * M68KCoreList[] = { NULL };
82 
83 CDInterface *CDCoreList[] = { NULL };
84 
YuiErrorMsg(const char * string)85 void YuiErrorMsg(const char *string) { }
86 
YuiSwapBuffers()87 void YuiSwapBuffers() { }
88 
89 #ifdef HAVE_DIRECTINPUT
DXGetWindow()90 HWND DXGetWindow()
91 {
92 	return HWND_DESKTOP;
93 }
94 #endif
95 
96 //////////////////////////////////////////////////////////////////////////////
97 
ProgramUsage()98 void ProgramUsage()
99 {
100    printf("%s v%s - by Cyber Warrior X (c)%s\n", PROG_NAME, VER_NAME, COPYRIGHT_YEAR);
101    printf("usage: %s <core index as specified in pertest.c>\n", PROG_NAME);
102    exit (1);
103 }
104 
105 //////////////////////////////////////////////////////////////////////////////
106 
cleanup(void)107 void cleanup(void)
108 {
109 	if (CurPer)
110 	{
111       CurPer->DeInit();
112       testspassed++;
113 		printf("Test Score: %d/11 \n", testspassed);
114 	}
115 }
116 
117 //////////////////////////////////////////////////////////////////////////////
118 
TestInput(const char * msg,u32 flags,u64 timeout,u64 tickfreq)119 int TestInput(const char *msg, u32 flags, u64 timeout, u64 tickfreq)
120 {
121 	u64 time1, time2;
122 	u32 id;
123 	u32 startid;
124 
125 	startid = CurPer->Scan(flags);
126 	printf("%s Timeout in %d seconds\n", msg, timeout);
127 	time1 = YabauseGetTicks();
128 	time2 = YabauseGetTicks();
129 	timeout = timeout * tickfreq;
130 	while ((id = CurPer->Scan(flags)) == 0 && (time2-time1) < timeout)
131 		time2 = YabauseGetTicks();
132 
133 	if (startid != 0)
134 	{
135 		printf("Data present before press. result code: %08X", startid);
136 		return 0;
137 	}
138 	if (id != 0)
139 	{
140 	   printf("result code: %08X", id);
141 		return 1;
142 	}
143 	else
144 		return 0;
145 }
146 
147 //////////////////////////////////////////////////////////////////////////////
148 
GetTickFreq()149 u64 GetTickFreq()
150 {
151 	u64 tickfreq;
152 #ifdef WIN32
153 	QueryPerformanceFrequency((LARGE_INTEGER *)&tickfreq);
154 #elif defined(_arch_dreamcast)
155 	tickfreq = 1000;
156 #elif defined(GEKKO)
157 	tickfreq = secs_to_ticks(1);
158 #elif defined(PSP)
159 	tickfreq = 1000000;
160 #elif defined(HAVE_GETTIMEOFDAY)
161 	tickfreq = 1000000;
162 #elif defined(HAVE_LIBSDL)
163 	tickfreq = 1000;
164 #endif
165 	return tickfreq;
166 }
167 
168 //////////////////////////////////////////////////////////////////////////////
169 
main(int argc,char * argv[])170 int main(int argc, char *argv[])
171 {
172    char *cdrom_name = NULL;
173    u32 f_size=0;
174    int per_index=0;
175 	u64 tickfreq;
176 
177 	tickfreq = GetTickFreq();
178    atexit(cleanup);
179 
180 #ifndef _arch_dreamcast
181    if (argc != 2)
182    {
183       ProgramUsage();
184    }
185 
186    printf("%s v%s - by Cyber Warrior X(c)%s\n", PROG_NAME, VER_NAME, COPYRIGHT_YEAR);
187 
188    per_index = atoi(argv[1]);
189 #endif
190 
191 	if (per_index < 0 || per_index >= (sizeof(PERCoreList) / sizeof(PerInterface_struct *)))
192 	{
193 		printf("peripheral core index out of range\n");
194 		exit(1);
195 	}
196 
197 	CurPer = PERCoreList[per_index];
198 	printf("Testing %s\n", CurPer->Name);
199 
200    if (CurPer->Init() != 0)
201    {
202       printf("PerInit error: Unable to initialize peripheral core\n");
203       exit(1);
204    }
205    else testspassed++;
206 
207 	testspassed += TestInput("Press a button on a gamepad/joystick...", PERSF_BUTTON, 10, tickfreq);
208 	testspassed += TestInput("Press a key on the keyboard...", PERSF_KEY, 10, tickfreq);
209 	testspassed += TestInput("Move d-pad/stick on a gamepad/joystick...", PERSF_AXIS|PERSF_BUTTON, 10, tickfreq);
210 	testspassed += TestInput("Move mouse...", PERSF_MOUSEMOVE, 10, tickfreq);
211 }
212 
213 //////////////////////////////////////////////////////////////////////////////
214