Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/cd-netbsd.c
2 views
1
/* Copyright 2004-2005 Theo Berkau
2
Copyright 2004-2005 Guillaume Duhamel
3
Copyright 2005 Joost Peters
4
5
This file is part of Yabause.
6
7
Yabause is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
Yabause is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with Yabause; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21
22
#include <sys/types.h>
23
#include <sys/ioctl.h>
24
#include <fcntl.h>
25
#include <sys/cdio.h>
26
#include <unistd.h>
27
#include <string.h>
28
#include <stdio.h>
29
30
#include "cdbase.h"
31
#include "debug.h"
32
33
static int NetBSDCDInit(const char *);
34
static void NetBSDCDDeInit(void);
35
static s32 NetBSDCDReadTOC(u32 *);
36
static int NetBSDCDGetStatus(void);
37
static int NetBSDCDReadSectorFAD(u32, void *);
38
static void NetBSDCDReadAheadFAD(u32);
39
40
CDInterface ArchCD = {
41
CDCORE_ARCH,
42
"NetBSD CD Drive",
43
NetBSDCDInit,
44
NetBSDCDDeInit,
45
NetBSDCDGetStatus,
46
NetBSDCDReadTOC,
47
NetBSDCDReadSectorFAD,
48
NetBSDCDReadAheadFAD,
49
};
50
51
static int hCDROM;
52
53
static int NetBSDCDInit(const char * cdrom_name) {
54
if ((hCDROM = open(cdrom_name, O_RDONLY | O_NONBLOCK)) == -1) {
55
LOG("CDInit (%s) failed\n", cdrom_name);
56
return -1;
57
}
58
59
LOG("CDInit (%s) OK\n", cdrom_name);
60
return 0;
61
}
62
63
static void NetBSDCDDeInit(void) {
64
if (hCDROM != -1) {
65
close(hCDROM);
66
}
67
68
LOG("CDDeInit OK\n");
69
}
70
71
72
static s32 NetBSDCDReadTOC(u32 * TOC)
73
{
74
int success;
75
struct ioc_toc_header ctTOC;
76
struct ioc_read_toc_entry ctTOCent;
77
struct cd_toc_entry data;
78
int i, j;
79
int add150 = 0;
80
81
ctTOCent.address_format = CD_LBA_FORMAT;
82
ctTOCent.data_len = sizeof (struct cd_toc_entry);
83
ctTOCent.data = &data;
84
85
if (hCDROM != -1)
86
{
87
memset(TOC, 0xFF, 0xCC * 2);
88
memset(&ctTOC, 0xFF, sizeof(struct ioc_toc_header));
89
90
if (ioctl(hCDROM, CDIOREADTOCHEADER, &ctTOC) == -1) {
91
return 0;
92
}
93
94
ctTOCent.starting_track = ctTOC.starting_track;
95
ioctl(hCDROM, CDIOREADTOCENTRYS, &ctTOCent);
96
if (ctTOCent.data->addr.lba == 0) add150 = 150;
97
TOC[0] = ((ctTOCent.data->control << 28) |
98
(ctTOCent.data->addr_type << 24) |
99
ctTOCent.data->addr.lba + add150);
100
101
// convert TOC to saturn format
102
for (i = ctTOC.starting_track + 1; i <= ctTOC.ending_track; i++)
103
{
104
ctTOCent.starting_track = i;
105
ioctl(hCDROM, CDIOREADTOCENTRYS, &ctTOCent);
106
TOC[i - 1] = (ctTOCent.data->control << 28) |
107
(ctTOCent.data->addr_type << 24) |
108
(ctTOCent.data->addr.lba + add150);
109
}
110
111
// Do First, Last, and Lead out sections here
112
113
ctTOCent.starting_track = ctTOC.starting_track;
114
ioctl(hCDROM, CDIOREADTOCENTRYS, &ctTOCent);
115
TOC[99] = (ctTOCent.data->control << 28) |
116
(ctTOCent.data->addr_type << 24) |
117
(ctTOC.starting_track << 16);
118
119
ctTOCent.starting_track = ctTOC.ending_track;
120
ioctl(hCDROM, CDIOREADTOCENTRYS, &ctTOCent);
121
TOC[100] = (ctTOCent.data->control << 28) |
122
(ctTOCent.data->addr_type << 24) |
123
(ctTOC.starting_track << 16);
124
125
ctTOCent.starting_track = 0xAA;
126
ioctl(hCDROM, CDIOREADTOCENTRYS, &ctTOCent);
127
TOC[101] = (ctTOCent.data->control << 28) |
128
(ctTOCent.data->addr_type << 24) |
129
(ctTOCent.data->addr.lba + add150);
130
131
return (0xCC * 2);
132
}
133
134
return 0;
135
}
136
137
static int NetBSDCDGetStatus(void) {
138
// 0 - CD Present, disc spinning
139
// 1 - CD Present, disc not spinning
140
// 2 - CD not present
141
// 3 - Tray open
142
// see ../windows/cd.cc for more info
143
144
return 0;
145
}
146
147
static int NetBSDCDReadSectorFAD(u32 FAD, void *buffer) {
148
static const s8 syncHdr[] = {
149
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
150
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00 };
151
152
if (hCDROM != -1)
153
{
154
memcpy(buffer, syncHdr, sizeof (syncHdr));
155
lseek(hCDROM, (FAD - 150) * 2048, SEEK_SET);
156
read(hCDROM, (char *)buffer + 0x10, 2048);
157
158
return 1;
159
}
160
161
return 0;
162
}
163
164
static void NetBSDCDReadAheadFAD(UNUSED u32 FAD)
165
{
166
// No-op
167
}
168
169