2 * Mostly done after the Scitech Bios emulation
3 * Written by Hans-Jörg Frieden
4 * Hyperion Entertainment
11 #define PRINTF(fmt, args...) printf(fmt, ## args)
13 #define PRINTF(fmt, args...)
16 #define BIOS_SEG 0xFFF0
17 #define PCIBIOS_SUCCESSFUL 0
18 #define PCIBIOS_DEVICE_NOT_FOUND 0x86
20 typedef unsigned char UBYTE;
21 typedef unsigned short UWORD;
22 typedef unsigned long ULONG;
28 static inline UBYTE read_byte(volatile UBYTE* from)
31 asm volatile ("lbz %0,%1\n eieio" : "=r" (x) : "m" (*from));
35 static inline void write_byte(volatile UBYTE *to, int x)
37 asm volatile ("stb %1,%0\n eieio" : "=m" (*to) : "r" (x));
40 static inline UWORD read_word_little(volatile UWORD *from)
43 asm volatile ("lhbrx %0,0,%1\n eieio" : "=r" (x) : "r" (from), "m" (*from));
47 static inline UWORD read_word_big(volatile UWORD *from)
50 asm volatile ("lhz %0,%1\n eieio" : "=r" (x) : "m" (*from));
54 static inline void write_word_little(volatile UWORD *to, int x)
56 asm volatile ("sthbrx %1,0,%2\n eieio" : "=m" (*to) : "r" (x), "r" (to));
59 static inline void write_word_big(volatile UWORD *to, int x)
61 asm volatile ("sth %1,%0\n eieio" : "=m" (*to) : "r" (x));
64 static inline ULONG read_long_little(volatile ULONG *from)
67 asm volatile ("lwbrx %0,0,%1\n eieio" : "=r" (x) : "r" (from), "m"(*from));
71 static inline ULONG read_long_big(volatile ULONG *from)
74 asm volatile ("lwz %0,%1\n eieio" : "=r" (x) : "m" (*from));
78 static inline void write_long_little(volatile ULONG *to, ULONG x)
80 asm volatile ("stwbrx %1,0,%2\n eieio" : "=m" (*to) : "r" (x), "r" (to));
83 static inline void write_long_big(volatile ULONG *to, ULONG x)
85 asm volatile ("stw %1,%0\n eieio" : "=m" (*to) : "r" (x));
88 #define port_to_mem(from) (0xFE000000|(from))
89 #define in_byte(from) read_byte( (UBYTE *)port_to_mem(from))
90 #define in_word(from) read_word_little((UWORD *)port_to_mem(from))
91 #define in_long(from) read_long_little((ULONG *)port_to_mem(from))
92 #define out_byte(to, val) write_byte((UBYTE *)port_to_mem(to), val)
93 #define out_word(to, val) write_word_little((UWORD *)port_to_mem(to), val)
94 #define out_long(to, val) write_long_little((ULONG *)port_to_mem(to), val)
96 static void X86API undefined_intr(int intno)
98 extern u16 A1_rdw(u32 addr);
99 if (A1_rdw(intno * 4 + 2) == BIOS_SEG)
101 PRINTF("Undefined interrupt %xh called AX = %xh, BX = %xh, CX = %xh, DX = %xh\n",
102 intno, M.x86.R_AX, M.x86.R_BX, M.x86.R_CX, M.x86.R_DX);
107 PRINTF("Calling interrupt %xh, AL=%xh, AH=%xh\n", intno, M.x86.R_AL, M.x86.R_AH);
108 X86EMU_prepareForInt(intno);
112 static void X86API int42(int intno);
113 static void X86API int15(int intno);
115 static void X86API int10(int intno)
117 if (A1_rdw(intno*4+2) == BIOS_SEG)
121 PRINTF("int10: branching to %04X:%04X, AL=%xh, AH=%xh\n", A1_rdw(intno*4+2), A1_rdw(intno*4),
122 M.x86.R_AL, M.x86.R_AH);
123 X86EMU_prepareForInt(intno);
127 static void X86API int1A(int intno)
133 case 0xB101: /* PCI Bios Present? */
135 M.x86.R_EDX = 0x20494350;
140 case 0xB102: /* Find device */
141 device = mypci_find_device(M.x86.R_DX, M.x86.R_CX, M.x86.R_SI);
144 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
145 M.x86.R_BH = mypci_bus(device);
146 M.x86.R_BL = mypci_devfn(device);
150 M.x86.R_AH = PCIBIOS_DEVICE_NOT_FOUND;
152 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
154 case 0xB103: /* Find PCI class code */
155 M.x86.R_AH = PCIBIOS_DEVICE_NOT_FOUND;
156 /*printf("Find by class not yet implmented"); */
157 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
159 case 0xB108: /* read config byte */
160 M.x86.R_CL = mypci_read_cfg_byte(M.x86.R_BH, M.x86.R_BL, M.x86.R_DI);
161 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
162 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
163 /*printf("read_config_byte %x,%x,%x -> %x\n", M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, */
166 case 0xB109: /* read config word */
167 M.x86.R_CX = mypci_read_cfg_word(M.x86.R_BH, M.x86.R_BL, M.x86.R_DI);
168 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
169 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
170 /*printf("read_config_word %x,%x,%x -> %x\n", M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, */
173 case 0xB10A: /* read config dword */
174 M.x86.R_ECX = mypci_read_cfg_long(M.x86.R_BH, M.x86.R_BL, M.x86.R_DI);
175 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
176 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
177 /*printf("read_config_long %x,%x,%x -> %x\n", M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, */
180 case 0xB10B: /* write config byte */
181 mypci_write_cfg_byte(M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, M.x86.R_CL);
182 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
183 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
184 /*printf("write_config_byte %x,%x,%x <- %x\n", M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, */
187 case 0xB10C: /* write config word */
188 mypci_write_cfg_word(M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, M.x86.R_CX);
189 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
190 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
191 /*printf("write_config_word %x,%x,%x <- %x\n", M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, */
194 case 0xB10D: /* write config dword */
195 mypci_write_cfg_long(M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, M.x86.R_ECX);
196 M.x86.R_AH = PCIBIOS_SUCCESSFUL;
197 CONDITIONAL_SET_FLAG((M.x86.R_AH != PCIBIOS_SUCCESSFUL), F_CF);
198 /*printf("write_config_long %x,%x,%x <- %x\n", M.x86.R_BH, M.x86.R_BL, M.x86.R_DI, */
202 PRINTF("BIOS int %xh: Unknown function AX=%04xh\n", intno, M.x86.R_AX);
210 X86EMU_intrFuncs bios_intr_tab[256];
212 for (i=0; i<256; i++)
214 write_long_little(M.mem_base+i*4, BIOS_SEG<<16);
215 bios_intr_tab[i] = undefined_intr;
218 bios_intr_tab[0x10] = int10;
219 bios_intr_tab[0x1A] = int1A;
220 bios_intr_tab[0x42] = int42;
221 bios_intr_tab[0x15] = int15;
223 bios_intr_tab[0x6D] = int42;
225 X86EMU_setupIntrFuncs(bios_intr_tab);
229 unsigned char setup_40x25[] =
231 0x38, 0x28, 0x2d, 0x0a, 0x1f, 6, 0x19,
232 0x1c, 2, 7, 6, 7, 0, 0, 0, 0
235 unsigned char setup_80x25[] =
237 0x71, 0x50, 0x5a, 0x0a, 0x1f, 6, 0x19,
238 0x1c, 2, 7, 6, 7, 0, 0, 0, 0
241 unsigned char setup_graphics[] =
243 0x38, 0x28, 0x20, 0x0a, 0x7f, 6, 0x64,
244 0x70, 2, 1, 6, 7, 0, 0, 0, 0
247 unsigned char setup_bw[] =
249 0x61, 0x50, 0x52, 0x0f, 0x19, 6, 0x19,
250 0x19, 2, 0x0d, 0x0b, 0x0c, 0, 0, 0, 0
253 unsigned char * setup_modes[] =
255 setup_40x25, /* mode 0: 40x25 bw text */
256 setup_40x25, /* mode 1: 40x25 col text */
257 setup_80x25, /* mode 2: 80x25 bw text */
258 setup_80x25, /* mode 3: 80x25 col text */
259 setup_graphics, /* mode 4: 320x200 col graphics */
260 setup_graphics, /* mode 5: 320x200 bw graphics */
261 setup_graphics, /* mode 6: 640x200 bw graphics */
262 setup_bw /* mode 7: 80x25 mono text */
265 unsigned int setup_cols[] =
267 40, 40, 80, 80, 40, 40, 80, 80
270 unsigned char setup_modesets[] =
272 0x2C, 0x28, 0x2D, 0x29, 0x2A, 0x2E, 0x1E, 0x29
275 unsigned int setup_bufsize[] =
277 2048, 2048, 4096, 2096, 16384, 16384, 16384, 4096
280 void bios_set_mode(int mode)
283 unsigned char mode_set = setup_modesets[mode]; /* Control register value */
284 unsigned char *setup_regs = setup_modes[mode]; /* Register 3D4 Array */
286 /* Switch video off */
287 out_byte(0x3D8, mode_set & 0x37);
289 /* Set up parameters at 3D4h */
292 out_byte(0x3D4, (unsigned char)i);
293 out_byte(0x3D5, *setup_regs);
298 out_byte(0x3D8, mode_set);
301 if (mode == 6) out_byte(0x3D9, 0x3F);
302 else out_byte(0x3D9, 0x30);
305 static void bios_print_string(void)
307 extern void video_bios_print_string(char *string, int x, int y, int attr, int count);
308 char *s = (char *)(M.x86.R_ES<<4) + M.x86.R_BP;
310 if (M.x86.R_AL & 0x02) attr = - 1;
311 else attr = M.x86.R_BL;
312 video_bios_print_string(s, M.x86.R_DH, M.x86.R_DL, attr, M.x86.R_CX);
315 static void X86API int42(int intno)
320 bios_set_mode(M.x86.R_AL);
326 PRINTF("Warning: VIDEO BIOS interrupt %xh unimplemented function %xh, AL = %xh\n",
327 intno, M.x86.R_AH, M.x86.R_AL);
331 static void X86API int15(int intno)
333 PRINTF("Called interrupt 15h: AX = %xh, BX = %xh, CX = %xh, DX = %xh\n",
334 M.x86.R_AX, M.x86.R_BX, M.x86.R_CX, M.x86.R_DX);