-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos.c
227 lines (187 loc) · 4.14 KB
/
os.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* UAE - The Un*x Amiga Emulator
*
* OS specific functions
*
* (c) 1995 Bernd Schmidt
*/
#include <stdio.h>
#ifdef __unix
#include <values.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include "config.h"
#include "amiga.h"
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "os.h"
#ifdef LINUX_JOYSTICK
#include <linux/joystick.h>
static int js0;
static bool joystickpresent = false;
struct JS_DATA_TYPE jscal;
void read_joystick(UWORD *dir, bool *button)
{
static int minx = MAXINT, maxx = MININT,
miny = MAXINT, maxy = MININT;
bool left = false, right = false, top = false, bot = false;
struct JS_DATA_TYPE buffer;
int len;
*dir = 0;
*button = false;
if (!joystickpresent)
return;
len = read(js0, &buffer, sizeof(buffer));
if (len != sizeof(buffer))
return;
if (buffer.x < minx) minx = buffer.x;
if (buffer.y < miny) miny = buffer.y;
if (buffer.x > maxx) maxx = buffer.x;
if (buffer.y > maxy) maxy = buffer.y;
if (buffer.x < (minx + (maxx-minx)/3))
left = true;
else if (buffer.x > (minx + 2*(maxx-minx)/3))
right = true;
if (buffer.y < (miny + (maxy-miny)/3))
top = true;
else if (buffer.y > (miny + 2*(maxy-miny)/3))
bot = true;
if (left) top = !top;
if (right) bot = !bot;
*dir = bot | (right << 1) | (top << 8) | (left << 9);
*button = (buffer.buttons & 3) != 0;
}
void init_joystick(void)
{
js0 = open("/dev/js0", O_RDONLY);
if (js0 < 0)
return;
joystickpresent = true;
}
void close_joystick(void)
{
if (joystickpresent)
close(js0);
}
#else
void read_joystick(UWORD *dir, bool *button)
{
*dir = 0;
*button = false;
}
void init_joystick(void)
{
}
void close_joystick(void)
{
}
#endif
CPTR audlc[4], audpt[4];
UWORD audvol[4], audper[4], audlen[4], audwlen[4];
int audwper[4];
UWORD auddat[4];
int audsnum[4];
/* Audio states. This is not an exact representation of the Audio State Machine
* (HRM p. 166), but a simplification. To be honest, I don't completely
* understand that picture yet.
*/
int audst[4];
#ifdef LINUX_SOUND
#include <sys/soundcard.h>
static const int n_frames = 10;
/* The buffer is too large... */
static UWORD buffer[44100], *bufpt;
static BYTE snddata[4];
static int frames = 0;
static int smplcnt = 0;
static int sfd;
static bool have_sound;
bool init_sound (void)
{
int tmp;
sfd = open ("/dev/dsp", O_WRONLY);
have_sound = !(sfd < 0);
if (!have_sound) {
return false;
}
tmp = 16;
ioctl(sfd, SNDCTL_DSP_SAMPLESIZE, &tmp);
tmp = 0;
ioctl(sfd, SNDCTL_DSP_STEREO, &tmp);
tmp = 44100;
ioctl(sfd, SNDCTL_DSP_SPEED, &tmp);
audst[0] = audst[1] = audst[2] = audst[3] = 0;
bufpt = buffer;
frames = n_frames;
smplcnt = 0;
return true;
}
static void channel_reload (int c)
{
audst[c] = 1;
audpt[c] = audlc[c];
audwper[c] = 0;
audwlen[c] = audlen[c];
audsnum[c] = 1;
}
void do_sound (void)
{
smplcnt -= 227;
while (smplcnt < 0) {
int i;
smplcnt += 80;
for(i = 0; i < 4; i++) {
if (dmaen (1<<i)) {
if (audst[i] == 0) {
/* DMA was turned on for this channel */
channel_reload (i);
continue;
}
if (audwper[i] <= 0) {
audwper[i] += audper[i];
if (audst[i] == 1) {
/* Starting a sample, cause interrupt */
put_word (0xDFF09C, 0x8000 | (0x80 << i));
audst[i] = 2;
}
audsnum[i] ^= 1;
if (audsnum[i] == 0) {
auddat[i] = get_word (audpt[i]);
audpt[i] += 2;
audwlen[i]--;
if (audwlen[i] == 0) {
channel_reload (i);
}
}
}
snddata[i] = audsnum[i] ? auddat[i] : auddat[i] >> 8;
audwper[i] -= 80;
} else
audst[i] = snddata[i] = 0;
}
*bufpt++ = (snddata[0] * audvol[0] / 2 + snddata[1] * audvol[1] / 2
+ snddata[2] * audvol[2] / 2 + snddata[3] * audvol[3] / 2);
}
}
void flush_sound (void)
{
if (--frames == 0) {
write (sfd, buffer, 2*(bufpt - buffer));
bufpt = buffer;
frames = n_frames;
}
}
#else
bool init_sound (void)
{
return true;
}
void do_sound (void)
{
}
void flush_sound (void)
{
}
#endif