Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/cd-freebsd.c
2 views
1
/* Copyright 2004-2005 Theo Berkau
2
Copyright 2004-2006 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 <sys/cdrio.h>
27
#include <unistd.h>
28
#include <string.h>
29
#include <stdio.h>
30
31
#include "cdbase.h"
32
#include "debug.h"
33
34
static int FreeBSDCDInit(const char *);
35
static void FreeBSDCDDeInit(void);
36
static s32 FreeBSDCDReadTOC(u32 *);
37
static int FreeBSDCDGetStatus(void);
38
static int FreeBSDCDReadSectorFAD(u32, void *);
39
static void FreeBSDCDReadAheadFAD(u32);
40
41
CDInterface ArchCD = {
42
CDCORE_ARCH,
43
"FreeBSD CD Drive",
44
FreeBSDCDInit,
45
FreeBSDCDDeInit,
46
FreeBSDCDGetStatus,
47
FreeBSDCDReadTOC,
48
FreeBSDCDReadSectorFAD,
49
FreeBSDCDReadAheadFAD,
50
};
51
52
static int hCDROM;
53
54
static int FreeBSDCDInit(const char * cdrom_name) {
55
int bsize = 2352;
56
57
if ((hCDROM = open(cdrom_name, O_RDONLY | O_NONBLOCK)) == -1) {
58
LOG("CDInit (%s) failed\n", cdrom_name);
59
return -1;
60
}
61
62
if (ioctl (hCDROM, CDRIOCSETBLOCKSIZE, &bsize) == -1) {
63
return -1;
64
}
65
66
LOG("CDInit (%s) OK\n", cdrom_name);
67
return 0;
68
}
69
70
static void FreeBSDCDDeInit(void) {
71
if (hCDROM != -1) {
72
close(hCDROM);
73
}
74
75
LOG("CDDeInit OK\n");
76
}
77
78
79
static s32 FreeBSDCDReadTOC(u32 * TOC)
80
{
81
int success;
82
struct ioc_toc_header ctTOC;
83
struct ioc_read_toc_single_entry ctTOCent;
84
int i, j;
85
int add150 = 0;
86
87
ctTOCent.address_format = CD_LBA_FORMAT;
88
89
if (hCDROM != -1)
90
{
91
memset(TOC, 0xFF, 0xCC * 2);
92
memset(&ctTOC, 0xFF, sizeof(struct ioc_toc_header));
93
94
if (ioctl(hCDROM, CDIOREADTOCHEADER, &ctTOC) == -1) {
95
return 0;
96
}
97
98
ctTOCent.track = ctTOC.starting_track;
99
ioctl(hCDROM, CDIOREADTOCENTRY, &ctTOCent);
100
if (ctTOCent.entry.addr.lba == 0) add150 = 150;
101
TOC[0] = ((ctTOCent.entry.control << 28) |
102
(ctTOCent.entry.addr_type << 24) |
103
ctTOCent.entry.addr.lba + add150);
104
105
// convert TOC to saturn format
106
for (i = ctTOC.starting_track + 1; i <= ctTOC.ending_track; i++)
107
{
108
ctTOCent.track = i;
109
ioctl(hCDROM, CDIOREADTOCENTRY, &ctTOCent);
110
TOC[i - 1] = (ctTOCent.entry.control << 28) |
111
(ctTOCent.entry.addr_type << 24) |
112
(ctTOCent.entry.addr.lba + add150);
113
}
114
115
// Do First, Last, and Lead out sections here
116
117
ctTOCent.track = ctTOC.starting_track;
118
ioctl(hCDROM, CDIOREADTOCENTRY, &ctTOCent);
119
TOC[99] = (ctTOCent.entry.control << 28) |
120
(ctTOCent.entry.addr_type << 24) |
121
(ctTOC.starting_track << 16);
122
123
ctTOCent.track = ctTOC.ending_track;
124
ioctl(hCDROM, CDIOREADTOCENTRY, &ctTOCent);
125
TOC[100] = (ctTOCent.entry.control << 28) |
126
(ctTOCent.entry.addr_type << 24) |
127
(ctTOC.starting_track << 16);
128
129
ctTOCent.track = 0xAA;
130
ioctl(hCDROM, CDIOREADTOCENTRY, &ctTOCent);
131
TOC[101] = (ctTOCent.entry.control << 28) |
132
(ctTOCent.entry.addr_type << 24) |
133
(ctTOCent.entry.addr.lba + add150);
134
135
return (0xCC * 2);
136
}
137
138
return 0;
139
}
140
141
static int FreeBSDCDGetStatus(void) {
142
// 0 - CD Present, disc spinning
143
// 1 - CD Present, disc not spinning
144
// 2 - CD not present
145
// 3 - Tray open
146
// see ../windows/cd.cc for more info
147
148
return 0;
149
}
150
151
static int FreeBSDCDReadSectorFAD(u32 FAD, void *buffer) {
152
if (hCDROM != -1)
153
{
154
lseek(hCDROM, (FAD - 150) * 2352, SEEK_SET);
155
read(hCDROM, buffer, 2352);
156
157
return 1;
158
}
159
160
return 0;
161
}
162
163
static void FreeBSDCDReadAheadFAD(UNUSED u32 FAD)
164
{
165
// No-op
166
}
167
168