1 |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: ps2_bios.c,v 1.29 2005/02/18 07:04:10 debug Exp $ |
29 |
* |
30 |
* Playstation 2 SIFBIOS emulation. |
31 |
*/ |
32 |
|
33 |
#include <stdio.h> |
34 |
#include <stdlib.h> |
35 |
#include <string.h> |
36 |
#include <sys/types.h> |
37 |
#include <sys/time.h> |
38 |
#include <sys/resource.h> |
39 |
|
40 |
#include "console.h" |
41 |
#include "cpu.h" |
42 |
#include "cpu_mips.h" |
43 |
#include "machine.h" |
44 |
#include "misc.h" |
45 |
|
46 |
|
47 |
extern int quiet_mode; |
48 |
|
49 |
|
50 |
/* |
51 |
* playstation2_sifbios_emul(): |
52 |
* |
53 |
* SIFBIOS calls: |
54 |
* |
55 |
* 0 getver() |
56 |
* 1 halt(int mode) |
57 |
* 2 setdve(int mode) |
58 |
* 3 putchar(int ch) |
59 |
* 4 getchar() |
60 |
* 16 dma_init() |
61 |
* 17 dma_exit() |
62 |
* 32 cmd_init() |
63 |
* 33 cmd_exit() |
64 |
*/ |
65 |
int playstation2_sifbios_emul(struct cpu *cpu) |
66 |
{ |
67 |
int callnr; |
68 |
|
69 |
callnr = cpu->cd.mips.gpr[MIPS_GPR_A0]; |
70 |
|
71 |
switch (callnr) { |
72 |
case 0: /* getver() */ |
73 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0x200; /* TODO */ |
74 |
break; |
75 |
case 1: /* halt(int mode) */ |
76 |
debug("[ SIFBIOS halt(0x%llx) ]\n", |
77 |
(long long)cpu->cd.mips.gpr[MIPS_GPR_A1]); |
78 |
cpu->running = 0; |
79 |
cpu->dead = 1; |
80 |
break; |
81 |
case 2: /* setdve(int mode) */ |
82 |
debug("[ SIFBIOS setdve(0x%llx) ]\n", |
83 |
(long long)cpu->cd.mips.gpr[MIPS_GPR_A1]); |
84 |
break; |
85 |
case 3: /* putchar(int ch) */ |
86 |
/* debug("[ SIFBIOS putchar(0x%x) ]\n", |
87 |
(char)cpu->cd.mips.gpr[MIPS_GPR_A1]); */ |
88 |
console_putchar(cpu->machine->main_console_handle, |
89 |
cpu->cd.mips.gpr[MIPS_GPR_A1]); |
90 |
break; |
91 |
case 4: /* getchar() */ |
92 |
/* This is untested. TODO */ |
93 |
/* debug("[ SIFBIOS getchar() ]\n"; */ |
94 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; |
95 |
if (console_charavail(cpu->machine->main_console_handle)) |
96 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = console_readchar( |
97 |
cpu->machine->main_console_handle); |
98 |
break; |
99 |
case 16: /* dma_init() */ |
100 |
debug("[ SIFBIOS dma_init() ]\n"); |
101 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; /* TODO */ |
102 |
break; |
103 |
case 17: /* dma_exit() */ |
104 |
debug("[ SIFBIOS dma_exit() ]\n"); |
105 |
break; |
106 |
case 32: /* cmd_init() */ |
107 |
debug("[ SIFBIOS cmd_init() ]\n"); |
108 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; /* TODO */ |
109 |
break; |
110 |
case 33: /* cmd_exit() */ |
111 |
debug("[ SIFBIOS cmd_exit() ]\n"); |
112 |
break; |
113 |
case 48: |
114 |
debug("[ SIFBIOS rpc_init(): TODO ]\n"); |
115 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; /* TODO */ |
116 |
break; |
117 |
case 49: |
118 |
debug("[ SIFBIOS rpc_exit(): TODO ]\n"); |
119 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; /* TODO */ |
120 |
break; |
121 |
case 64: |
122 |
fatal("[ SIFBIOS SBR_IOPH_INIT(0x%x,0x%x,0x%x): TODO ]\n", |
123 |
(int)cpu->cd.mips.gpr[MIPS_GPR_A1], |
124 |
(int)cpu->cd.mips.gpr[MIPS_GPR_A2], |
125 |
(int)cpu->cd.mips.gpr[MIPS_GPR_A3]); |
126 |
|
127 |
/* |
128 |
* This is really really ugly: TODO |
129 |
* |
130 |
* Linux and NetBSD seem to work, but it's an ugly hack. |
131 |
* This should really be a callback thingy... |
132 |
* |
133 |
* NetBSD has a done-word which should be set to 1. |
134 |
* Linux has one done-word which should be set to 1, and |
135 |
* one which should be set to 0. |
136 |
* |
137 |
* The code as it is right now probably overwrites stuff in |
138 |
* memory that shouldn't be touched. Not good. |
139 |
* |
140 |
* Linux: err = sbios_rpc(SBR_IOPH_INIT, NULL, &result); |
141 |
* err should be 0 (just as NetBSD), |
142 |
* and result should be set to 0 as well. |
143 |
*/ |
144 |
{ |
145 |
uint32_t tmpaddr; |
146 |
|
147 |
tmpaddr = load_32bit_word(cpu, |
148 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 0); |
149 |
fatal(" +0: %08x\n", tmpaddr); |
150 |
tmpaddr = load_32bit_word(cpu, |
151 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 4); |
152 |
fatal(" +4: %08x\n", tmpaddr); |
153 |
tmpaddr = load_32bit_word(cpu, |
154 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 8); |
155 |
fatal(" +8: %08x\n", tmpaddr); |
156 |
tmpaddr = load_32bit_word(cpu, |
157 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 12); |
158 |
fatal(" +12: %08x\n", tmpaddr); |
159 |
|
160 |
/* TODO: This is probably netbsd specific */ |
161 |
tmpaddr = load_32bit_word(cpu, |
162 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 12); |
163 |
fatal("tmpaddr 1 = 0x%08x\n", tmpaddr); |
164 |
|
165 |
/* "done" word for NetBSD: */ |
166 |
store_32bit_word(cpu, tmpaddr, 1); |
167 |
/* "done" word A for Linux: */ |
168 |
store_32bit_word(cpu, tmpaddr + 4, 1); |
169 |
/* "done" word B for Linux: */ |
170 |
store_32bit_word(cpu, |
171 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 0, 0); |
172 |
} |
173 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; |
174 |
break; |
175 |
case 65: |
176 |
fatal("[ SIFBIOS alloc iop heap(0x%x) ]\n", |
177 |
(int)cpu->cd.mips.gpr[MIPS_GPR_A1]); |
178 |
|
179 |
/* |
180 |
* Linux uses this to allocate "heap" for the OHCI USB |
181 |
* controller. |
182 |
* |
183 |
* TODO: This naïve implementation does not allow for a |
184 |
* "free iop heap" function: :-/ |
185 |
*/ |
186 |
|
187 |
{ |
188 |
uint32_t tmpaddr; |
189 |
static uint32_t return_addr = 0x1000; |
190 |
/* 0xbc000000; */ |
191 |
uint32_t size; |
192 |
|
193 |
tmpaddr = load_32bit_word(cpu, |
194 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 0); |
195 |
fatal(" +0: %08x (result should be placed here)\n", |
196 |
tmpaddr); |
197 |
tmpaddr = load_32bit_word(cpu, |
198 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 4); |
199 |
fatal(" +4: %08x (*arg)\n", tmpaddr); |
200 |
size = load_32bit_word(cpu, tmpaddr + 0); |
201 |
fatal(" size = %08x\n", size); |
202 |
tmpaddr = load_32bit_word(cpu, |
203 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 8); |
204 |
fatal(" +8: %08x (*func (void *, int))\n", tmpaddr); |
205 |
tmpaddr = load_32bit_word(cpu, |
206 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 12); |
207 |
fatal(" +12: %08x (*para)\n", tmpaddr); |
208 |
|
209 |
/* TODO: This is probably netbsd specific */ |
210 |
tmpaddr = load_32bit_word(cpu, |
211 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 12); |
212 |
fatal("tmpaddr 1 = 0x%08x\n", tmpaddr); |
213 |
|
214 |
/* "done" word for NetBSD: */ |
215 |
store_32bit_word(cpu, tmpaddr, 1); |
216 |
/* "done" word A for Linux: */ |
217 |
store_32bit_word(cpu, tmpaddr + 4, 1); |
218 |
|
219 |
/* Result: */ |
220 |
store_32bit_word(cpu, |
221 |
cpu->cd.mips.gpr[MIPS_GPR_A1] + 0, return_addr); |
222 |
|
223 |
return_addr += size; |
224 |
/* Round up to next page: */ |
225 |
return_addr += 4095; |
226 |
return_addr &= ~4095; |
227 |
} |
228 |
cpu->cd.mips.gpr[MIPS_GPR_V0] = 0; |
229 |
break; |
230 |
default: |
231 |
quiet_mode = 0; |
232 |
cpu_register_dump(cpu->machine, cpu, 1, 0x1); |
233 |
printf("\n"); |
234 |
fatal("Playstation 2 SIFBIOS emulation: " |
235 |
"unimplemented call nr 0x%x\n", callnr); |
236 |
cpu->running = 0; |
237 |
cpu->dead = 1; |
238 |
} |
239 |
|
240 |
return 1; |
241 |
} |
242 |
|