1 /*
2  * catweaselmkiii-beos-drv.c - BeOS specific cw3 driver.
3  *
4  * Written by
5  *  Marco van den Heuvel <blackystardust68@yahoo.com>
6  *
7  * This file is part of VICE, the Versatile Commodore Emulator.
8  * See README for copyright notice.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  *  02111-1307  USA.
24  *
25  */
26 
27 /* Tested and confirmed working on:
28 
29  - BeOS Max V4b1 x86
30  - Zeta 1.21
31  */
32 
33 #include "vice.h"
34 
35 #ifdef BEOS_COMPILE
36 
37 #ifdef HAVE_CATWEASELMKIII
38 
39 #include <OS.h>
40 
41 #include "catweaselmkiii.h"
42 #include "io-access.h"
43 #include "log.h"
44 #include "pci-beos-drv.h"
45 #include "types.h"
46 
47 #define CW_SID_DAT 0xd8
48 #define CW_SID_CMD 0xdc
49 
50 #define MAXSID 1
51 
52 static int sids_found = -1;
53 static int base = -1;
54 
55 /* input/output functions */
cw_outb(uint16_t port,uint8_t val)56 static void cw_outb(uint16_t port, uint8_t val)
57 {
58     io_access_store_byte(port, val);
59 }
60 
cw_inb(uint16_t port)61 static uint8_t cw_inb(uint16_t port)
62 {
63     return io_access_read_byte(port);
64 }
65 
catweaselmkiii_drv_read(uint16_t addr,int chipno)66 int catweaselmkiii_drv_read(uint16_t addr, int chipno)
67 {
68     unsigned char cmd;
69 
70     if (chipno < MAXSID && base != -1 && addr < 0x20) {
71         cmd = (addr & 0x1f) | 0x20;
72         if (catweaselmkiii_get_ntsc()) {
73             cmd |= 0x40;
74         }
75         cw_outb(base + CW_SID_CMD, cmd);
76         snooze(1);
77         return cw_inb(base + CW_SID_DAT);
78     }
79     return 0;
80 }
81 
catweaselmkiii_drv_store(uint16_t addr,uint8_t outval,int chipno)82 void catweaselmkiii_drv_store(uint16_t addr, uint8_t outval, int chipno)
83 {
84     unsigned char cmd;
85 
86     if (chipno < MAXSID && base != -1 && addr < 0x20) {
87         cmd = addr & 0x1f;
88         if (catweaselmkiii_get_ntsc()) {
89             cmd |= 0x40;
90         }
91         cw_outb(base + CW_SID_DAT, outval);
92         cw_outb(base + CW_SID_CMD, cmd);
93         snooze(1);
94     }
95 }
96 
detect_sid(void)97 static int detect_sid(void)
98 {
99     int i;
100 
101     for (i = 0x18; i >= 0; --i) {
102         catweaselmkiii_drv_store((uint16_t)i, 0, 0);
103     }
104 
105     catweaselmkiii_drv_store(0x12, 0xff, 0);
106 
107     for (i = 0; i < 100; ++i) {
108         if (catweaselmkiii_drv_read(0x1b, 0)) {
109             return 0;
110         }
111     }
112 
113     catweaselmkiii_drv_store(0x0e, 0xff, 0);
114     catweaselmkiii_drv_store(0x0f, 0xff, 0);
115     catweaselmkiii_drv_store(0x12, 0x20, 0);
116 
117     for (i = 0; i < 100; ++i) {
118         if (catweaselmkiii_drv_read(0x1b, 0)) {
119             return 1;
120         }
121     }
122     return 0;
123 }
124 
catweaselmkiii_drv_open(void)125 int catweaselmkiii_drv_open(void)
126 {
127     int i;
128     uint32_t b1 = 0;
129     uint32_t b2 = 0;
130 
131     if (!sids_found) {
132         return -1;
133     }
134 
135     if (sids_found > 0) {
136         return 0;
137     }
138 
139     sids_found = 0;
140 
141     log_message(LOG_DEFAULT, "Detecting PCI CatWeasel boards.");
142 
143     if (io_access_init() < 0) {
144         log_message(LOG_DEFAULT, "Cannot get access to $%X.", base);
145         return -1;
146     }
147 
148     i = pci_get_base(0xe159, 0x0001, &b1, &b2);
149 
150     if (i < 0) {
151         log_message(LOG_DEFAULT, "No PCI CatWeasel found.");
152         io_access_shutdown();
153         return -1;
154     }
155 
156     base = b1 & 0xfffc;
157 
158 
159     log_message(LOG_DEFAULT, "PCI CatWeasel board found at $%04X.", base);
160 
161     if (detect_sid()) {
162         sids_found++;
163     }
164 
165     if (!sids_found) {
166         log_message(LOG_DEFAULT, "No PCI CatWeasel found.");
167         io_access_shutdown();
168         return -1;
169     }
170 
171     log_message(LOG_DEFAULT, "PCI CatWeasel: opened, found %d SIDs.", sids_found);
172 
173     return 0;
174 }
175 
catweaselmkiii_drv_close(void)176 int catweaselmkiii_drv_close(void)
177 {
178     io_access_shutdown();
179 
180     base = -1;
181 
182     sids_found = -1;
183 
184     log_message(LOG_DEFAULT, "PCI CatWeasel: closed");
185 
186     return 0;
187 }
188 
catweaselmkiii_drv_available(void)189 int catweaselmkiii_drv_available(void)
190 {
191     return sids_found;
192 }
193 
catweaselmkiii_drv_set_machine_parameter(long cycles_per_sec)194 void catweaselmkiii_drv_set_machine_parameter(long cycles_per_sec)
195 {
196 }
197 #endif
198 #endif
199