root / vl.c @ 4021dab0
History | View | Annotate | Download (93.2 kB)
1 | 0824d6fc | bellard | /*
|
---|---|---|---|
2 | 1df912cf | bellard | * QEMU PC System Emulator
|
3 | 0824d6fc | bellard | *
|
4 | 1df912cf | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 0824d6fc | bellard | *
|
6 | 1df912cf | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 1df912cf | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 1df912cf | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 1df912cf | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 1df912cf | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 1df912cf | bellard | * furnished to do so, subject to the following conditions:
|
12 | 1df912cf | bellard | *
|
13 | 1df912cf | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 1df912cf | bellard | * all copies or substantial portions of the Software.
|
15 | 1df912cf | bellard | *
|
16 | 1df912cf | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 1df912cf | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 1df912cf | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 1df912cf | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 1df912cf | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 1df912cf | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 1df912cf | bellard | * THE SOFTWARE.
|
23 | 0824d6fc | bellard | */
|
24 | 0824d6fc | bellard | #include <stdlib.h> |
25 | 0824d6fc | bellard | #include <stdio.h> |
26 | 1df912cf | bellard | #include <stdarg.h> |
27 | 0824d6fc | bellard | #include <string.h> |
28 | 0824d6fc | bellard | #include <getopt.h> |
29 | 0824d6fc | bellard | #include <inttypes.h> |
30 | 0824d6fc | bellard | #include <unistd.h> |
31 | 0824d6fc | bellard | #include <sys/mman.h> |
32 | 0824d6fc | bellard | #include <fcntl.h> |
33 | 0824d6fc | bellard | #include <signal.h> |
34 | 0824d6fc | bellard | #include <time.h> |
35 | 0824d6fc | bellard | #include <sys/time.h> |
36 | 0824d6fc | bellard | #include <malloc.h> |
37 | 0824d6fc | bellard | #include <termios.h> |
38 | 0824d6fc | bellard | #include <sys/poll.h> |
39 | 0824d6fc | bellard | #include <errno.h> |
40 | f1510b2c | bellard | #include <sys/wait.h> |
41 | f1510b2c | bellard | |
42 | f1510b2c | bellard | #include <sys/ioctl.h> |
43 | f1510b2c | bellard | #include <sys/socket.h> |
44 | f1510b2c | bellard | #include <linux/if.h> |
45 | f1510b2c | bellard | #include <linux/if_tun.h> |
46 | 0824d6fc | bellard | |
47 | 0824d6fc | bellard | #include "cpu-i386.h" |
48 | 0824d6fc | bellard | #include "disas.h" |
49 | fc01f7e7 | bellard | #include "thunk.h" |
50 | fc01f7e7 | bellard | |
51 | fc01f7e7 | bellard | #include "vl.h" |
52 | 0824d6fc | bellard | |
53 | 0824d6fc | bellard | #define DEBUG_LOGFILE "/tmp/vl.log" |
54 | f1510b2c | bellard | #define DEFAULT_NETWORK_SCRIPT "/etc/vl-ifup" |
55 | 330d0414 | bellard | #define BIOS_FILENAME "bios.bin" |
56 | 330d0414 | bellard | #define VGABIOS_FILENAME "vgabios.bin" |
57 | f1510b2c | bellard | |
58 | 0824d6fc | bellard | //#define DEBUG_UNUSED_IOPORT
|
59 | 330d0414 | bellard | |
60 | c9159e53 | bellard | //#define DEBUG_IRQ_LATENCY
|
61 | 0824d6fc | bellard | |
62 | 330d0414 | bellard | /* output Bochs bios info messages */
|
63 | 330d0414 | bellard | //#define DEBUG_BIOS
|
64 | 330d0414 | bellard | |
65 | 330d0414 | bellard | /* debug IDE devices */
|
66 | 330d0414 | bellard | //#define DEBUG_IDE
|
67 | 330d0414 | bellard | |
68 | 330d0414 | bellard | /* debug PIC */
|
69 | 330d0414 | bellard | //#define DEBUG_PIC
|
70 | 330d0414 | bellard | |
71 | 330d0414 | bellard | /* debug NE2000 card */
|
72 | 330d0414 | bellard | //#define DEBUG_NE2000
|
73 | 330d0414 | bellard | |
74 | 330d0414 | bellard | /* debug PC keyboard */
|
75 | 330d0414 | bellard | //#define DEBUG_KBD
|
76 | 330d0414 | bellard | |
77 | 7916e224 | bellard | #define PHYS_RAM_BASE 0xac000000 |
78 | 7916e224 | bellard | #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) |
79 | 7916e224 | bellard | |
80 | 0824d6fc | bellard | #define KERNEL_LOAD_ADDR 0x00100000 |
81 | 0824d6fc | bellard | #define INITRD_LOAD_ADDR 0x00400000 |
82 | 0824d6fc | bellard | #define KERNEL_PARAMS_ADDR 0x00090000 |
83 | 0824d6fc | bellard | |
84 | 33e3963e | bellard | #define MAX_DISKS 2 |
85 | 33e3963e | bellard | |
86 | 0824d6fc | bellard | /* from plex86 (BSD license) */
|
87 | 0824d6fc | bellard | struct __attribute__ ((packed)) linux_params {
|
88 | 0824d6fc | bellard | // For 0x00..0x3f, see 'struct screen_info' in linux/include/linux/tty.h.
|
89 | 0824d6fc | bellard | // I just padded out the VESA parts, rather than define them.
|
90 | 0824d6fc | bellard | |
91 | 0824d6fc | bellard | /* 0x000 */ uint8_t orig_x;
|
92 | 0824d6fc | bellard | /* 0x001 */ uint8_t orig_y;
|
93 | 0824d6fc | bellard | /* 0x002 */ uint16_t ext_mem_k;
|
94 | 0824d6fc | bellard | /* 0x004 */ uint16_t orig_video_page;
|
95 | 0824d6fc | bellard | /* 0x006 */ uint8_t orig_video_mode;
|
96 | 0824d6fc | bellard | /* 0x007 */ uint8_t orig_video_cols;
|
97 | 0824d6fc | bellard | /* 0x008 */ uint16_t unused1;
|
98 | 0824d6fc | bellard | /* 0x00a */ uint16_t orig_video_ega_bx;
|
99 | 0824d6fc | bellard | /* 0x00c */ uint16_t unused2;
|
100 | 0824d6fc | bellard | /* 0x00e */ uint8_t orig_video_lines;
|
101 | 0824d6fc | bellard | /* 0x00f */ uint8_t orig_video_isVGA;
|
102 | 0824d6fc | bellard | /* 0x010 */ uint16_t orig_video_points;
|
103 | 0824d6fc | bellard | /* 0x012 */ uint8_t pad0[0x20 - 0x12]; // VESA info. |
104 | 0824d6fc | bellard | /* 0x020 */ uint16_t cl_magic; // Commandline magic number (0xA33F) |
105 | 0824d6fc | bellard | /* 0x022 */ uint16_t cl_offset; // Commandline offset. Address of commandline |
106 | 0824d6fc | bellard | // is calculated as 0x90000 + cl_offset, bu
|
107 | 0824d6fc | bellard | // only if cl_magic == 0xA33F.
|
108 | 0824d6fc | bellard | /* 0x024 */ uint8_t pad1[0x40 - 0x24]; // VESA info. |
109 | 0824d6fc | bellard | |
110 | 0824d6fc | bellard | /* 0x040 */ uint8_t apm_bios_info[20]; // struct apm_bios_info |
111 | 0824d6fc | bellard | /* 0x054 */ uint8_t pad2[0x80 - 0x54]; |
112 | 0824d6fc | bellard | |
113 | 0824d6fc | bellard | // Following 2 from 'struct drive_info_struct' in drivers/block/cciss.h.
|
114 | 0824d6fc | bellard | // Might be truncated?
|
115 | 0824d6fc | bellard | /* 0x080 */ uint8_t hd0_info[16]; // hd0-disk-parameter from intvector 0x41 |
116 | 0824d6fc | bellard | /* 0x090 */ uint8_t hd1_info[16]; // hd1-disk-parameter from intvector 0x46 |
117 | 0824d6fc | bellard | |
118 | 0824d6fc | bellard | // System description table truncated to 16 bytes
|
119 | 0824d6fc | bellard | // From 'struct sys_desc_table_struct' in linux/arch/i386/kernel/setup.c.
|
120 | 0824d6fc | bellard | /* 0x0a0 */ uint16_t sys_description_len;
|
121 | 0824d6fc | bellard | /* 0x0a2 */ uint8_t sys_description_table[14]; |
122 | 0824d6fc | bellard | // [0] machine id
|
123 | 0824d6fc | bellard | // [1] machine submodel id
|
124 | 0824d6fc | bellard | // [2] BIOS revision
|
125 | 0824d6fc | bellard | // [3] bit1: MCA bus
|
126 | 0824d6fc | bellard | |
127 | 0824d6fc | bellard | /* 0x0b0 */ uint8_t pad3[0x1e0 - 0xb0]; |
128 | 0824d6fc | bellard | /* 0x1e0 */ uint32_t alt_mem_k;
|
129 | 0824d6fc | bellard | /* 0x1e4 */ uint8_t pad4[4]; |
130 | 0824d6fc | bellard | /* 0x1e8 */ uint8_t e820map_entries;
|
131 | 0824d6fc | bellard | /* 0x1e9 */ uint8_t eddbuf_entries; // EDD_NR |
132 | 0824d6fc | bellard | /* 0x1ea */ uint8_t pad5[0x1f1 - 0x1ea]; |
133 | 0824d6fc | bellard | /* 0x1f1 */ uint8_t setup_sects; // size of setup.S, number of sectors |
134 | 0824d6fc | bellard | /* 0x1f2 */ uint16_t mount_root_rdonly; // MOUNT_ROOT_RDONLY (if !=0) |
135 | 0824d6fc | bellard | /* 0x1f4 */ uint16_t sys_size; // size of compressed kernel-part in the |
136 | 0824d6fc | bellard | // (b)zImage-file (in 16 byte units, rounded up)
|
137 | 0824d6fc | bellard | /* 0x1f6 */ uint16_t swap_dev; // (unused AFAIK) |
138 | 0824d6fc | bellard | /* 0x1f8 */ uint16_t ramdisk_flags;
|
139 | 0824d6fc | bellard | /* 0x1fa */ uint16_t vga_mode; // (old one) |
140 | 0824d6fc | bellard | /* 0x1fc */ uint16_t orig_root_dev; // (high=Major, low=minor) |
141 | 0824d6fc | bellard | /* 0x1fe */ uint8_t pad6[1]; |
142 | 0824d6fc | bellard | /* 0x1ff */ uint8_t aux_device_info;
|
143 | 0824d6fc | bellard | /* 0x200 */ uint16_t jump_setup; // Jump to start of setup code, |
144 | 0824d6fc | bellard | // aka "reserved" field.
|
145 | 0824d6fc | bellard | /* 0x202 */ uint8_t setup_signature[4]; // Signature for SETUP-header, ="HdrS" |
146 | 0824d6fc | bellard | /* 0x206 */ uint16_t header_format_version; // Version number of header format; |
147 | 0824d6fc | bellard | /* 0x208 */ uint8_t setup_S_temp0[8]; // Used by setup.S for communication with |
148 | 0824d6fc | bellard | // boot loaders, look there.
|
149 | 0824d6fc | bellard | /* 0x210 */ uint8_t loader_type;
|
150 | 0824d6fc | bellard | // 0 for old one.
|
151 | 0824d6fc | bellard | // else 0xTV:
|
152 | 0824d6fc | bellard | // T=0: LILO
|
153 | 0824d6fc | bellard | // T=1: Loadlin
|
154 | 0824d6fc | bellard | // T=2: bootsect-loader
|
155 | 0824d6fc | bellard | // T=3: SYSLINUX
|
156 | 0824d6fc | bellard | // T=4: ETHERBOOT
|
157 | 0824d6fc | bellard | // V=version
|
158 | 0824d6fc | bellard | /* 0x211 */ uint8_t loadflags;
|
159 | 0824d6fc | bellard | // bit0 = 1: kernel is loaded high (bzImage)
|
160 | 0824d6fc | bellard | // bit7 = 1: Heap and pointer (see below) set by boot
|
161 | 0824d6fc | bellard | // loader.
|
162 | 0824d6fc | bellard | /* 0x212 */ uint16_t setup_S_temp1;
|
163 | 0824d6fc | bellard | /* 0x214 */ uint32_t kernel_start;
|
164 | 0824d6fc | bellard | /* 0x218 */ uint32_t initrd_start;
|
165 | 0824d6fc | bellard | /* 0x21c */ uint32_t initrd_size;
|
166 | 0824d6fc | bellard | /* 0x220 */ uint8_t setup_S_temp2[4]; |
167 | 0824d6fc | bellard | /* 0x224 */ uint16_t setup_S_heap_end_pointer;
|
168 | 0824d6fc | bellard | /* 0x226 */ uint8_t pad7[0x2d0 - 0x226]; |
169 | 0824d6fc | bellard | |
170 | 0824d6fc | bellard | /* 0x2d0 : Int 15, ax=e820 memory map. */
|
171 | 0824d6fc | bellard | // (linux/include/asm-i386/e820.h, 'struct e820entry')
|
172 | 0824d6fc | bellard | #define E820MAX 32 |
173 | 0824d6fc | bellard | #define E820_RAM 1 |
174 | 0824d6fc | bellard | #define E820_RESERVED 2 |
175 | 0824d6fc | bellard | #define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */ |
176 | 0824d6fc | bellard | #define E820_NVS 4 |
177 | 0824d6fc | bellard | struct {
|
178 | 0824d6fc | bellard | uint64_t addr; |
179 | 0824d6fc | bellard | uint64_t size; |
180 | 0824d6fc | bellard | uint32_t type; |
181 | 0824d6fc | bellard | } e820map[E820MAX]; |
182 | 0824d6fc | bellard | |
183 | 0824d6fc | bellard | /* 0x550 */ uint8_t pad8[0x600 - 0x550]; |
184 | 0824d6fc | bellard | |
185 | 0824d6fc | bellard | // BIOS Enhanced Disk Drive Services.
|
186 | 0824d6fc | bellard | // (From linux/include/asm-i386/edd.h, 'struct edd_info')
|
187 | 0824d6fc | bellard | // Each 'struct edd_info is 78 bytes, times a max of 6 structs in array.
|
188 | 0824d6fc | bellard | /* 0x600 */ uint8_t eddbuf[0x7d4 - 0x600]; |
189 | 0824d6fc | bellard | |
190 | 0824d6fc | bellard | /* 0x7d4 */ uint8_t pad9[0x800 - 0x7d4]; |
191 | 0824d6fc | bellard | /* 0x800 */ uint8_t commandline[0x800]; |
192 | 0824d6fc | bellard | |
193 | 0824d6fc | bellard | /* 0x1000 */
|
194 | 0824d6fc | bellard | uint64_t gdt_table[256];
|
195 | 0824d6fc | bellard | uint64_t idt_table[48];
|
196 | 0824d6fc | bellard | }; |
197 | 0824d6fc | bellard | |
198 | 0824d6fc | bellard | #define KERNEL_CS 0x10 |
199 | 0824d6fc | bellard | #define KERNEL_DS 0x18 |
200 | 0824d6fc | bellard | |
201 | 0824d6fc | bellard | typedef void (IOPortWriteFunc)(CPUX86State *env, uint32_t address, uint32_t data); |
202 | 0824d6fc | bellard | typedef uint32_t (IOPortReadFunc)(CPUX86State *env, uint32_t address);
|
203 | 0824d6fc | bellard | |
204 | fc01f7e7 | bellard | #define MAX_IOPORTS 4096 |
205 | 0824d6fc | bellard | |
206 | 330d0414 | bellard | static const char *interp_prefix = CONFIG_QEMU_PREFIX; |
207 | 0824d6fc | bellard | char phys_ram_file[1024]; |
208 | 0824d6fc | bellard | CPUX86State *global_env; |
209 | 1df912cf | bellard | CPUX86State *cpu_single_env; |
210 | 0824d6fc | bellard | FILE *logfile = NULL;
|
211 | 0824d6fc | bellard | int loglevel;
|
212 | fc01f7e7 | bellard | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
213 | fc01f7e7 | bellard | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
214 | 33e3963e | bellard | BlockDriverState *bs_table[MAX_DISKS]; |
215 | 0824d6fc | bellard | |
216 | 0824d6fc | bellard | /***********************************************************/
|
217 | 0824d6fc | bellard | /* x86 io ports */
|
218 | 0824d6fc | bellard | |
219 | 0824d6fc | bellard | uint32_t default_ioport_readb(CPUX86State *env, uint32_t address) |
220 | 0824d6fc | bellard | { |
221 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
222 | 0824d6fc | bellard | fprintf(stderr, "inb: port=0x%04x\n", address);
|
223 | 0824d6fc | bellard | #endif
|
224 | fc01f7e7 | bellard | return 0xff; |
225 | 0824d6fc | bellard | } |
226 | 0824d6fc | bellard | |
227 | 0824d6fc | bellard | void default_ioport_writeb(CPUX86State *env, uint32_t address, uint32_t data)
|
228 | 0824d6fc | bellard | { |
229 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
230 | 0824d6fc | bellard | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
|
231 | 0824d6fc | bellard | #endif
|
232 | 0824d6fc | bellard | } |
233 | 0824d6fc | bellard | |
234 | 0824d6fc | bellard | /* default is to make two byte accesses */
|
235 | 0824d6fc | bellard | uint32_t default_ioport_readw(CPUX86State *env, uint32_t address) |
236 | 0824d6fc | bellard | { |
237 | 0824d6fc | bellard | uint32_t data; |
238 | 330d0414 | bellard | data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](env, address); |
239 | 330d0414 | bellard | data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](env, address + 1) << 8; |
240 | 0824d6fc | bellard | return data;
|
241 | 0824d6fc | bellard | } |
242 | 0824d6fc | bellard | |
243 | 0824d6fc | bellard | void default_ioport_writew(CPUX86State *env, uint32_t address, uint32_t data)
|
244 | 0824d6fc | bellard | { |
245 | 330d0414 | bellard | ioport_write_table[0][address & (MAX_IOPORTS - 1)](env, address, data & 0xff); |
246 | 330d0414 | bellard | ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](env, address + 1, (data >> 8) & 0xff); |
247 | 0824d6fc | bellard | } |
248 | 0824d6fc | bellard | |
249 | fc01f7e7 | bellard | uint32_t default_ioport_readl(CPUX86State *env, uint32_t address) |
250 | 0824d6fc | bellard | { |
251 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
252 | fc01f7e7 | bellard | fprintf(stderr, "inl: port=0x%04x\n", address);
|
253 | fc01f7e7 | bellard | #endif
|
254 | fc01f7e7 | bellard | return 0xffffffff; |
255 | 0824d6fc | bellard | } |
256 | 0824d6fc | bellard | |
257 | fc01f7e7 | bellard | void default_ioport_writel(CPUX86State *env, uint32_t address, uint32_t data)
|
258 | 0824d6fc | bellard | { |
259 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
260 | fc01f7e7 | bellard | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
|
261 | fc01f7e7 | bellard | #endif
|
262 | 0824d6fc | bellard | } |
263 | 0824d6fc | bellard | |
264 | fc01f7e7 | bellard | void init_ioports(void) |
265 | 0824d6fc | bellard | { |
266 | 0824d6fc | bellard | int i;
|
267 | 0824d6fc | bellard | |
268 | fc01f7e7 | bellard | for(i = 0; i < MAX_IOPORTS; i++) { |
269 | fc01f7e7 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
270 | fc01f7e7 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
271 | fc01f7e7 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
272 | fc01f7e7 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
273 | fc01f7e7 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
274 | fc01f7e7 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
275 | fc01f7e7 | bellard | } |
276 | 0824d6fc | bellard | } |
277 | 0824d6fc | bellard | |
278 | fc01f7e7 | bellard | /* size is the word size in byte */
|
279 | fc01f7e7 | bellard | int register_ioport_read(int start, int length, IOPortReadFunc *func, int size) |
280 | f1510b2c | bellard | { |
281 | fc01f7e7 | bellard | int i, bsize;
|
282 | f1510b2c | bellard | |
283 | fc01f7e7 | bellard | if (size == 1) |
284 | fc01f7e7 | bellard | bsize = 0;
|
285 | fc01f7e7 | bellard | else if (size == 2) |
286 | fc01f7e7 | bellard | bsize = 1;
|
287 | fc01f7e7 | bellard | else if (size == 4) |
288 | fc01f7e7 | bellard | bsize = 2;
|
289 | fc01f7e7 | bellard | else
|
290 | fc01f7e7 | bellard | return -1; |
291 | fc01f7e7 | bellard | for(i = start; i < start + length; i += size)
|
292 | fc01f7e7 | bellard | ioport_read_table[bsize][i] = func; |
293 | f1510b2c | bellard | return 0; |
294 | f1510b2c | bellard | } |
295 | f1510b2c | bellard | |
296 | fc01f7e7 | bellard | /* size is the word size in byte */
|
297 | fc01f7e7 | bellard | int register_ioport_write(int start, int length, IOPortWriteFunc *func, int size) |
298 | f1510b2c | bellard | { |
299 | fc01f7e7 | bellard | int i, bsize;
|
300 | f1510b2c | bellard | |
301 | fc01f7e7 | bellard | if (size == 1) |
302 | fc01f7e7 | bellard | bsize = 0;
|
303 | fc01f7e7 | bellard | else if (size == 2) |
304 | fc01f7e7 | bellard | bsize = 1;
|
305 | fc01f7e7 | bellard | else if (size == 4) |
306 | fc01f7e7 | bellard | bsize = 2;
|
307 | fc01f7e7 | bellard | else
|
308 | fc01f7e7 | bellard | return -1; |
309 | fc01f7e7 | bellard | for(i = start; i < start + length; i += size)
|
310 | fc01f7e7 | bellard | ioport_write_table[bsize][i] = func; |
311 | f1510b2c | bellard | return 0; |
312 | f1510b2c | bellard | } |
313 | f1510b2c | bellard | |
314 | 0824d6fc | bellard | void pstrcpy(char *buf, int buf_size, const char *str) |
315 | 0824d6fc | bellard | { |
316 | 0824d6fc | bellard | int c;
|
317 | 0824d6fc | bellard | char *q = buf;
|
318 | 0824d6fc | bellard | |
319 | 0824d6fc | bellard | if (buf_size <= 0) |
320 | 0824d6fc | bellard | return;
|
321 | 0824d6fc | bellard | |
322 | 0824d6fc | bellard | for(;;) {
|
323 | 0824d6fc | bellard | c = *str++; |
324 | 0824d6fc | bellard | if (c == 0 || q >= buf + buf_size - 1) |
325 | 0824d6fc | bellard | break;
|
326 | 0824d6fc | bellard | *q++ = c; |
327 | 0824d6fc | bellard | } |
328 | 0824d6fc | bellard | *q = '\0';
|
329 | 0824d6fc | bellard | } |
330 | 0824d6fc | bellard | |
331 | 0824d6fc | bellard | /* strcat and truncate. */
|
332 | 0824d6fc | bellard | char *pstrcat(char *buf, int buf_size, const char *s) |
333 | 0824d6fc | bellard | { |
334 | 0824d6fc | bellard | int len;
|
335 | 0824d6fc | bellard | len = strlen(buf); |
336 | 0824d6fc | bellard | if (len < buf_size)
|
337 | 0824d6fc | bellard | pstrcpy(buf + len, buf_size - len, s); |
338 | 0824d6fc | bellard | return buf;
|
339 | 0824d6fc | bellard | } |
340 | 0824d6fc | bellard | |
341 | 0824d6fc | bellard | int load_kernel(const char *filename, uint8_t *addr) |
342 | 0824d6fc | bellard | { |
343 | 0824d6fc | bellard | int fd, size, setup_sects;
|
344 | 0824d6fc | bellard | uint8_t bootsect[512];
|
345 | 0824d6fc | bellard | |
346 | 0824d6fc | bellard | fd = open(filename, O_RDONLY); |
347 | 0824d6fc | bellard | if (fd < 0) |
348 | 0824d6fc | bellard | return -1; |
349 | 0824d6fc | bellard | if (read(fd, bootsect, 512) != 512) |
350 | 0824d6fc | bellard | goto fail;
|
351 | 0824d6fc | bellard | setup_sects = bootsect[0x1F1];
|
352 | 0824d6fc | bellard | if (!setup_sects)
|
353 | 0824d6fc | bellard | setup_sects = 4;
|
354 | 0824d6fc | bellard | /* skip 16 bit setup code */
|
355 | 0824d6fc | bellard | lseek(fd, (setup_sects + 1) * 512, SEEK_SET); |
356 | 0824d6fc | bellard | size = read(fd, addr, 16 * 1024 * 1024); |
357 | 0824d6fc | bellard | if (size < 0) |
358 | 0824d6fc | bellard | goto fail;
|
359 | 0824d6fc | bellard | close(fd); |
360 | 0824d6fc | bellard | return size;
|
361 | 0824d6fc | bellard | fail:
|
362 | 0824d6fc | bellard | close(fd); |
363 | 0824d6fc | bellard | return -1; |
364 | 0824d6fc | bellard | } |
365 | 0824d6fc | bellard | |
366 | 0824d6fc | bellard | /* return the size or -1 if error */
|
367 | 0824d6fc | bellard | int load_image(const char *filename, uint8_t *addr) |
368 | 0824d6fc | bellard | { |
369 | 0824d6fc | bellard | int fd, size;
|
370 | 0824d6fc | bellard | fd = open(filename, O_RDONLY); |
371 | 0824d6fc | bellard | if (fd < 0) |
372 | 0824d6fc | bellard | return -1; |
373 | 0824d6fc | bellard | size = lseek(fd, 0, SEEK_END);
|
374 | 0824d6fc | bellard | lseek(fd, 0, SEEK_SET);
|
375 | 0824d6fc | bellard | if (read(fd, addr, size) != size) {
|
376 | 0824d6fc | bellard | close(fd); |
377 | 0824d6fc | bellard | return -1; |
378 | 0824d6fc | bellard | } |
379 | 0824d6fc | bellard | close(fd); |
380 | 0824d6fc | bellard | return size;
|
381 | 0824d6fc | bellard | } |
382 | 0824d6fc | bellard | |
383 | 0824d6fc | bellard | void cpu_x86_outb(CPUX86State *env, int addr, int val) |
384 | 0824d6fc | bellard | { |
385 | fc01f7e7 | bellard | ioport_write_table[0][addr & (MAX_IOPORTS - 1)](env, addr, val); |
386 | 0824d6fc | bellard | } |
387 | 0824d6fc | bellard | |
388 | 0824d6fc | bellard | void cpu_x86_outw(CPUX86State *env, int addr, int val) |
389 | 0824d6fc | bellard | { |
390 | fc01f7e7 | bellard | ioport_write_table[1][addr & (MAX_IOPORTS - 1)](env, addr, val); |
391 | 0824d6fc | bellard | } |
392 | 0824d6fc | bellard | |
393 | 0824d6fc | bellard | void cpu_x86_outl(CPUX86State *env, int addr, int val) |
394 | 0824d6fc | bellard | { |
395 | fc01f7e7 | bellard | ioport_write_table[2][addr & (MAX_IOPORTS - 1)](env, addr, val); |
396 | 0824d6fc | bellard | } |
397 | 0824d6fc | bellard | |
398 | 0824d6fc | bellard | int cpu_x86_inb(CPUX86State *env, int addr) |
399 | 0824d6fc | bellard | { |
400 | fc01f7e7 | bellard | return ioport_read_table[0][addr & (MAX_IOPORTS - 1)](env, addr); |
401 | 0824d6fc | bellard | } |
402 | 0824d6fc | bellard | |
403 | 0824d6fc | bellard | int cpu_x86_inw(CPUX86State *env, int addr) |
404 | 0824d6fc | bellard | { |
405 | fc01f7e7 | bellard | return ioport_read_table[1][addr & (MAX_IOPORTS - 1)](env, addr); |
406 | 0824d6fc | bellard | } |
407 | 0824d6fc | bellard | |
408 | 0824d6fc | bellard | int cpu_x86_inl(CPUX86State *env, int addr) |
409 | 0824d6fc | bellard | { |
410 | fc01f7e7 | bellard | return ioport_read_table[2][addr & (MAX_IOPORTS - 1)](env, addr); |
411 | 0824d6fc | bellard | } |
412 | 0824d6fc | bellard | |
413 | 0824d6fc | bellard | /***********************************************************/
|
414 | 0824d6fc | bellard | void ioport80_write(CPUX86State *env, uint32_t addr, uint32_t data)
|
415 | 0824d6fc | bellard | { |
416 | 0824d6fc | bellard | } |
417 | 0824d6fc | bellard | |
418 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
419 | 0824d6fc | bellard | { |
420 | 0824d6fc | bellard | va_list ap; |
421 | 0824d6fc | bellard | |
422 | 0824d6fc | bellard | va_start(ap, fmt); |
423 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
424 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
425 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
426 | 0824d6fc | bellard | #ifdef TARGET_I386
|
427 | 0824d6fc | bellard | cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP); |
428 | 0824d6fc | bellard | #endif
|
429 | 0824d6fc | bellard | va_end(ap); |
430 | 0824d6fc | bellard | abort(); |
431 | 0824d6fc | bellard | } |
432 | 0824d6fc | bellard | |
433 | 0824d6fc | bellard | /***********************************************************/
|
434 | 0824d6fc | bellard | /* vga emulation */
|
435 | 0824d6fc | bellard | static uint8_t vga_index;
|
436 | 0824d6fc | bellard | static uint8_t vga_regs[256]; |
437 | 0824d6fc | bellard | static int last_cursor_pos; |
438 | 0824d6fc | bellard | |
439 | 0824d6fc | bellard | void update_console_messages(void) |
440 | 0824d6fc | bellard | { |
441 | 0824d6fc | bellard | int c, i, cursor_pos, eol;
|
442 | 0824d6fc | bellard | |
443 | 0824d6fc | bellard | cursor_pos = vga_regs[0x0f] | (vga_regs[0x0e] << 8); |
444 | 0824d6fc | bellard | eol = 0;
|
445 | 0824d6fc | bellard | for(i = last_cursor_pos; i < cursor_pos; i++) {
|
446 | 0824d6fc | bellard | c = phys_ram_base[0xb8000 + (i) * 2]; |
447 | 0824d6fc | bellard | if (c >= ' ') { |
448 | 0824d6fc | bellard | putchar(c); |
449 | 0824d6fc | bellard | eol = 0;
|
450 | 0824d6fc | bellard | } else {
|
451 | 0824d6fc | bellard | if (!eol)
|
452 | 0824d6fc | bellard | putchar('\n');
|
453 | 0824d6fc | bellard | eol = 1;
|
454 | 0824d6fc | bellard | } |
455 | 0824d6fc | bellard | } |
456 | 0824d6fc | bellard | fflush(stdout); |
457 | 0824d6fc | bellard | last_cursor_pos = cursor_pos; |
458 | 0824d6fc | bellard | } |
459 | 0824d6fc | bellard | |
460 | 0824d6fc | bellard | /* just to see first Linux console messages, we intercept cursor position */
|
461 | 0824d6fc | bellard | void vga_ioport_write(CPUX86State *env, uint32_t addr, uint32_t data)
|
462 | 0824d6fc | bellard | { |
463 | 0824d6fc | bellard | switch(addr) {
|
464 | 0824d6fc | bellard | case 0x3d4: |
465 | 0824d6fc | bellard | vga_index = data; |
466 | 0824d6fc | bellard | break;
|
467 | 0824d6fc | bellard | case 0x3d5: |
468 | 0824d6fc | bellard | vga_regs[vga_index] = data; |
469 | 0824d6fc | bellard | if (vga_index == 0x0f) |
470 | 0824d6fc | bellard | update_console_messages(); |
471 | 0824d6fc | bellard | break;
|
472 | 0824d6fc | bellard | } |
473 | 0824d6fc | bellard | |
474 | 0824d6fc | bellard | } |
475 | 0824d6fc | bellard | |
476 | 0824d6fc | bellard | /***********************************************************/
|
477 | 0824d6fc | bellard | /* cmos emulation */
|
478 | 0824d6fc | bellard | |
479 | 0824d6fc | bellard | #define RTC_SECONDS 0 |
480 | 0824d6fc | bellard | #define RTC_SECONDS_ALARM 1 |
481 | 0824d6fc | bellard | #define RTC_MINUTES 2 |
482 | 0824d6fc | bellard | #define RTC_MINUTES_ALARM 3 |
483 | 0824d6fc | bellard | #define RTC_HOURS 4 |
484 | 0824d6fc | bellard | #define RTC_HOURS_ALARM 5 |
485 | 0824d6fc | bellard | #define RTC_ALARM_DONT_CARE 0xC0 |
486 | 0824d6fc | bellard | |
487 | 0824d6fc | bellard | #define RTC_DAY_OF_WEEK 6 |
488 | 0824d6fc | bellard | #define RTC_DAY_OF_MONTH 7 |
489 | 0824d6fc | bellard | #define RTC_MONTH 8 |
490 | 0824d6fc | bellard | #define RTC_YEAR 9 |
491 | 0824d6fc | bellard | |
492 | 0824d6fc | bellard | #define RTC_REG_A 10 |
493 | 0824d6fc | bellard | #define RTC_REG_B 11 |
494 | 0824d6fc | bellard | #define RTC_REG_C 12 |
495 | 0824d6fc | bellard | #define RTC_REG_D 13 |
496 | 0824d6fc | bellard | |
497 | 0824d6fc | bellard | /* PC cmos mappings */
|
498 | 0824d6fc | bellard | #define REG_EQUIPMENT_BYTE 0x14 |
499 | 0824d6fc | bellard | |
500 | 0824d6fc | bellard | uint8_t cmos_data[128];
|
501 | 0824d6fc | bellard | uint8_t cmos_index; |
502 | 0824d6fc | bellard | |
503 | 0824d6fc | bellard | void cmos_ioport_write(CPUX86State *env, uint32_t addr, uint32_t data)
|
504 | 0824d6fc | bellard | { |
505 | 0824d6fc | bellard | if (addr == 0x70) { |
506 | 0824d6fc | bellard | cmos_index = data & 0x7f;
|
507 | 0824d6fc | bellard | } |
508 | 0824d6fc | bellard | } |
509 | 0824d6fc | bellard | |
510 | 0824d6fc | bellard | uint32_t cmos_ioport_read(CPUX86State *env, uint32_t addr) |
511 | 0824d6fc | bellard | { |
512 | 0824d6fc | bellard | int ret;
|
513 | 0824d6fc | bellard | |
514 | 0824d6fc | bellard | if (addr == 0x70) { |
515 | 0824d6fc | bellard | return 0xff; |
516 | 0824d6fc | bellard | } else {
|
517 | 0824d6fc | bellard | /* toggle update-in-progress bit for Linux (same hack as
|
518 | 0824d6fc | bellard | plex86) */
|
519 | 0824d6fc | bellard | ret = cmos_data[cmos_index]; |
520 | 0824d6fc | bellard | if (cmos_index == RTC_REG_A)
|
521 | 0824d6fc | bellard | cmos_data[RTC_REG_A] ^= 0x80;
|
522 | 0824d6fc | bellard | else if (cmos_index == RTC_REG_C) |
523 | 0824d6fc | bellard | cmos_data[RTC_REG_C] = 0x00;
|
524 | 0824d6fc | bellard | return ret;
|
525 | 0824d6fc | bellard | } |
526 | 0824d6fc | bellard | } |
527 | 0824d6fc | bellard | |
528 | 0824d6fc | bellard | |
529 | 0824d6fc | bellard | static inline int to_bcd(int a) |
530 | 0824d6fc | bellard | { |
531 | 0824d6fc | bellard | return ((a / 10) << 4) | (a % 10); |
532 | 0824d6fc | bellard | } |
533 | 0824d6fc | bellard | |
534 | 0824d6fc | bellard | void cmos_init(void) |
535 | 0824d6fc | bellard | { |
536 | 0824d6fc | bellard | struct tm *tm;
|
537 | 0824d6fc | bellard | time_t ti; |
538 | 330d0414 | bellard | int val;
|
539 | 0824d6fc | bellard | |
540 | 0824d6fc | bellard | ti = time(NULL);
|
541 | 0824d6fc | bellard | tm = gmtime(&ti); |
542 | 0824d6fc | bellard | cmos_data[RTC_SECONDS] = to_bcd(tm->tm_sec); |
543 | 0824d6fc | bellard | cmos_data[RTC_MINUTES] = to_bcd(tm->tm_min); |
544 | 0824d6fc | bellard | cmos_data[RTC_HOURS] = to_bcd(tm->tm_hour); |
545 | 0824d6fc | bellard | cmos_data[RTC_DAY_OF_WEEK] = to_bcd(tm->tm_wday); |
546 | 0824d6fc | bellard | cmos_data[RTC_DAY_OF_MONTH] = to_bcd(tm->tm_mday); |
547 | abd0aaff | bellard | cmos_data[RTC_MONTH] = to_bcd(tm->tm_mon + 1);
|
548 | 0824d6fc | bellard | cmos_data[RTC_YEAR] = to_bcd(tm->tm_year % 100);
|
549 | 0824d6fc | bellard | |
550 | 0824d6fc | bellard | cmos_data[RTC_REG_A] = 0x26;
|
551 | 0824d6fc | bellard | cmos_data[RTC_REG_B] = 0x02;
|
552 | 0824d6fc | bellard | cmos_data[RTC_REG_C] = 0x00;
|
553 | 0824d6fc | bellard | cmos_data[RTC_REG_D] = 0x80;
|
554 | 0824d6fc | bellard | |
555 | 330d0414 | bellard | /* various important CMOS locations needed by PC/Bochs bios */
|
556 | 330d0414 | bellard | |
557 | 0824d6fc | bellard | cmos_data[REG_EQUIPMENT_BYTE] = 0x02; /* FPU is there */ |
558 | 0824d6fc | bellard | |
559 | 330d0414 | bellard | /* memory size */
|
560 | 330d0414 | bellard | val = (phys_ram_size / 1024) - 1024; |
561 | 330d0414 | bellard | if (val > 65535) |
562 | 330d0414 | bellard | val = 65535;
|
563 | 330d0414 | bellard | cmos_data[0x17] = val;
|
564 | 330d0414 | bellard | cmos_data[0x18] = val >> 8; |
565 | 330d0414 | bellard | cmos_data[0x30] = val;
|
566 | 330d0414 | bellard | cmos_data[0x31] = val >> 8; |
567 | 330d0414 | bellard | |
568 | 330d0414 | bellard | val = (phys_ram_size / 65536) - ((16 * 1024 * 1024) / 65536); |
569 | 330d0414 | bellard | if (val > 65535) |
570 | 330d0414 | bellard | val = 65535;
|
571 | 330d0414 | bellard | cmos_data[0x34] = val;
|
572 | 330d0414 | bellard | cmos_data[0x35] = val >> 8; |
573 | 330d0414 | bellard | |
574 | 330d0414 | bellard | cmos_data[0x3d] = 0x02; /* hard drive boot */ |
575 | 330d0414 | bellard | |
576 | fc01f7e7 | bellard | register_ioport_write(0x70, 2, cmos_ioport_write, 1); |
577 | fc01f7e7 | bellard | register_ioport_read(0x70, 2, cmos_ioport_read, 1); |
578 | 0824d6fc | bellard | } |
579 | 0824d6fc | bellard | |
580 | 0824d6fc | bellard | /***********************************************************/
|
581 | 0824d6fc | bellard | /* 8259 pic emulation */
|
582 | 0824d6fc | bellard | |
583 | 0824d6fc | bellard | typedef struct PicState { |
584 | 0824d6fc | bellard | uint8_t last_irr; /* edge detection */
|
585 | 0824d6fc | bellard | uint8_t irr; /* interrupt request register */
|
586 | 0824d6fc | bellard | uint8_t imr; /* interrupt mask register */
|
587 | 0824d6fc | bellard | uint8_t isr; /* interrupt service register */
|
588 | 0824d6fc | bellard | uint8_t priority_add; /* used to compute irq priority */
|
589 | 0824d6fc | bellard | uint8_t irq_base; |
590 | 0824d6fc | bellard | uint8_t read_reg_select; |
591 | 0824d6fc | bellard | uint8_t special_mask; |
592 | 0824d6fc | bellard | uint8_t init_state; |
593 | 0824d6fc | bellard | uint8_t auto_eoi; |
594 | 0824d6fc | bellard | uint8_t rotate_on_autoeoi; |
595 | 0824d6fc | bellard | uint8_t init4; /* true if 4 byte init */
|
596 | 0824d6fc | bellard | } PicState; |
597 | 0824d6fc | bellard | |
598 | 0824d6fc | bellard | /* 0 is master pic, 1 is slave pic */
|
599 | 0824d6fc | bellard | PicState pics[2];
|
600 | 0824d6fc | bellard | int pic_irq_requested;
|
601 | 0824d6fc | bellard | |
602 | 0824d6fc | bellard | /* set irq level. If an edge is detected, then the IRR is set to 1 */
|
603 | 0824d6fc | bellard | static inline void pic_set_irq1(PicState *s, int irq, int level) |
604 | 0824d6fc | bellard | { |
605 | 0824d6fc | bellard | int mask;
|
606 | 0824d6fc | bellard | mask = 1 << irq;
|
607 | 0824d6fc | bellard | if (level) {
|
608 | 0824d6fc | bellard | if ((s->last_irr & mask) == 0) |
609 | 0824d6fc | bellard | s->irr |= mask; |
610 | 0824d6fc | bellard | s->last_irr |= mask; |
611 | 0824d6fc | bellard | } else {
|
612 | 0824d6fc | bellard | s->last_irr &= ~mask; |
613 | 0824d6fc | bellard | } |
614 | 0824d6fc | bellard | } |
615 | 0824d6fc | bellard | |
616 | 0824d6fc | bellard | static inline int get_priority(PicState *s, int mask) |
617 | 0824d6fc | bellard | { |
618 | 0824d6fc | bellard | int priority;
|
619 | 0824d6fc | bellard | if (mask == 0) |
620 | 0824d6fc | bellard | return -1; |
621 | 0824d6fc | bellard | priority = 7;
|
622 | 0824d6fc | bellard | while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) |
623 | 0824d6fc | bellard | priority--; |
624 | 0824d6fc | bellard | return priority;
|
625 | 0824d6fc | bellard | } |
626 | 0824d6fc | bellard | |
627 | 0824d6fc | bellard | /* return the pic wanted interrupt. return -1 if none */
|
628 | 0824d6fc | bellard | static int pic_get_irq(PicState *s) |
629 | 0824d6fc | bellard | { |
630 | 0824d6fc | bellard | int mask, cur_priority, priority;
|
631 | 0824d6fc | bellard | |
632 | 0824d6fc | bellard | mask = s->irr & ~s->imr; |
633 | 0824d6fc | bellard | priority = get_priority(s, mask); |
634 | 0824d6fc | bellard | if (priority < 0) |
635 | 0824d6fc | bellard | return -1; |
636 | 0824d6fc | bellard | /* compute current priority */
|
637 | 0824d6fc | bellard | cur_priority = get_priority(s, s->isr); |
638 | 0824d6fc | bellard | if (priority > cur_priority) {
|
639 | 0824d6fc | bellard | /* higher priority found: an irq should be generated */
|
640 | 0824d6fc | bellard | return priority;
|
641 | 0824d6fc | bellard | } else {
|
642 | 0824d6fc | bellard | return -1; |
643 | 0824d6fc | bellard | } |
644 | 0824d6fc | bellard | } |
645 | 0824d6fc | bellard | |
646 | c9159e53 | bellard | /* raise irq to CPU if necessary. must be called every time the active
|
647 | c9159e53 | bellard | irq may change */
|
648 | c9159e53 | bellard | static void pic_update_irq(void) |
649 | 0824d6fc | bellard | { |
650 | 0824d6fc | bellard | int irq2, irq;
|
651 | 0824d6fc | bellard | |
652 | 0824d6fc | bellard | /* first look at slave pic */
|
653 | 0824d6fc | bellard | irq2 = pic_get_irq(&pics[1]);
|
654 | 0824d6fc | bellard | if (irq2 >= 0) { |
655 | 0824d6fc | bellard | /* if irq request by slave pic, signal master PIC */
|
656 | 0824d6fc | bellard | pic_set_irq1(&pics[0], 2, 1); |
657 | 0824d6fc | bellard | pic_set_irq1(&pics[0], 2, 0); |
658 | 0824d6fc | bellard | } |
659 | 0824d6fc | bellard | /* look at requested irq */
|
660 | 0824d6fc | bellard | irq = pic_get_irq(&pics[0]);
|
661 | 0824d6fc | bellard | if (irq >= 0) { |
662 | 0824d6fc | bellard | if (irq == 2) { |
663 | 0824d6fc | bellard | /* from slave pic */
|
664 | 0824d6fc | bellard | pic_irq_requested = 8 + irq2;
|
665 | 0824d6fc | bellard | } else {
|
666 | 0824d6fc | bellard | /* from master pic */
|
667 | 0824d6fc | bellard | pic_irq_requested = irq; |
668 | 0824d6fc | bellard | } |
669 | c9159e53 | bellard | cpu_x86_interrupt(global_env, CPU_INTERRUPT_HARD); |
670 | 0824d6fc | bellard | } |
671 | 0824d6fc | bellard | } |
672 | 0824d6fc | bellard | |
673 | c9159e53 | bellard | #ifdef DEBUG_IRQ_LATENCY
|
674 | c9159e53 | bellard | int64_t irq_time[16];
|
675 | c9159e53 | bellard | int64_t cpu_get_ticks(void);
|
676 | c9159e53 | bellard | #endif
|
677 | b118d61e | bellard | #ifdef DEBUG_PIC
|
678 | b118d61e | bellard | int irq_level[16]; |
679 | b118d61e | bellard | #endif
|
680 | c9159e53 | bellard | |
681 | c9159e53 | bellard | void pic_set_irq(int irq, int level) |
682 | c9159e53 | bellard | { |
683 | b118d61e | bellard | #ifdef DEBUG_PIC
|
684 | b118d61e | bellard | if (level != irq_level[irq]) {
|
685 | b118d61e | bellard | printf("pic_set_irq: irq=%d level=%d\n", irq, level);
|
686 | b118d61e | bellard | irq_level[irq] = level; |
687 | b118d61e | bellard | } |
688 | b118d61e | bellard | #endif
|
689 | c9159e53 | bellard | #ifdef DEBUG_IRQ_LATENCY
|
690 | c9159e53 | bellard | if (level) {
|
691 | c9159e53 | bellard | irq_time[irq] = cpu_get_ticks(); |
692 | c9159e53 | bellard | } |
693 | c9159e53 | bellard | #endif
|
694 | c9159e53 | bellard | pic_set_irq1(&pics[irq >> 3], irq & 7, level); |
695 | c9159e53 | bellard | pic_update_irq(); |
696 | c9159e53 | bellard | } |
697 | c9159e53 | bellard | |
698 | 0824d6fc | bellard | int cpu_x86_get_pic_interrupt(CPUX86State *env)
|
699 | 0824d6fc | bellard | { |
700 | 0824d6fc | bellard | int irq, irq2, intno;
|
701 | 0824d6fc | bellard | |
702 | 0824d6fc | bellard | /* signal the pic that the irq was acked by the CPU */
|
703 | 0824d6fc | bellard | irq = pic_irq_requested; |
704 | c9159e53 | bellard | #ifdef DEBUG_IRQ_LATENCY
|
705 | c9159e53 | bellard | printf("IRQ%d latency=%Ld\n", irq, cpu_get_ticks() - irq_time[irq]);
|
706 | c9159e53 | bellard | #endif
|
707 | b118d61e | bellard | #ifdef DEBUG_PIC
|
708 | b118d61e | bellard | printf("pic_interrupt: irq=%d\n", irq);
|
709 | b118d61e | bellard | #endif
|
710 | c9159e53 | bellard | |
711 | 0824d6fc | bellard | if (irq >= 8) { |
712 | 0824d6fc | bellard | irq2 = irq & 7;
|
713 | 0824d6fc | bellard | pics[1].isr |= (1 << irq2); |
714 | 0824d6fc | bellard | pics[1].irr &= ~(1 << irq2); |
715 | 0824d6fc | bellard | irq = 2;
|
716 | 0824d6fc | bellard | intno = pics[1].irq_base + irq2;
|
717 | 0824d6fc | bellard | } else {
|
718 | 0824d6fc | bellard | intno = pics[0].irq_base + irq;
|
719 | 0824d6fc | bellard | } |
720 | 0824d6fc | bellard | pics[0].isr |= (1 << irq); |
721 | 0824d6fc | bellard | pics[0].irr &= ~(1 << irq); |
722 | 0824d6fc | bellard | return intno;
|
723 | 0824d6fc | bellard | } |
724 | 0824d6fc | bellard | |
725 | 0824d6fc | bellard | void pic_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
726 | 0824d6fc | bellard | { |
727 | 0824d6fc | bellard | PicState *s; |
728 | 0824d6fc | bellard | int priority;
|
729 | 0824d6fc | bellard | |
730 | b118d61e | bellard | #ifdef DEBUG_PIC
|
731 | b118d61e | bellard | printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
|
732 | b118d61e | bellard | #endif
|
733 | 0824d6fc | bellard | s = &pics[addr >> 7];
|
734 | 0824d6fc | bellard | addr &= 1;
|
735 | 0824d6fc | bellard | if (addr == 0) { |
736 | 0824d6fc | bellard | if (val & 0x10) { |
737 | 0824d6fc | bellard | /* init */
|
738 | 0824d6fc | bellard | memset(s, 0, sizeof(PicState)); |
739 | 0824d6fc | bellard | s->init_state = 1;
|
740 | 0824d6fc | bellard | s->init4 = val & 1;
|
741 | 0824d6fc | bellard | if (val & 0x02) |
742 | 0824d6fc | bellard | hw_error("single mode not supported");
|
743 | 0824d6fc | bellard | if (val & 0x08) |
744 | 0824d6fc | bellard | hw_error("level sensitive irq not supported");
|
745 | 0824d6fc | bellard | } else if (val & 0x08) { |
746 | 0824d6fc | bellard | if (val & 0x02) |
747 | 0824d6fc | bellard | s->read_reg_select = val & 1;
|
748 | 0824d6fc | bellard | if (val & 0x40) |
749 | 0824d6fc | bellard | s->special_mask = (val >> 5) & 1; |
750 | 0824d6fc | bellard | } else {
|
751 | 0824d6fc | bellard | switch(val) {
|
752 | 0824d6fc | bellard | case 0x00: |
753 | 0824d6fc | bellard | case 0x80: |
754 | 0824d6fc | bellard | s->rotate_on_autoeoi = val >> 7;
|
755 | 0824d6fc | bellard | break;
|
756 | 0824d6fc | bellard | case 0x20: /* end of interrupt */ |
757 | 0824d6fc | bellard | case 0xa0: |
758 | 0824d6fc | bellard | priority = get_priority(s, s->isr); |
759 | 0824d6fc | bellard | if (priority >= 0) { |
760 | 0824d6fc | bellard | s->isr &= ~(1 << ((priority + s->priority_add) & 7)); |
761 | 0824d6fc | bellard | } |
762 | 0824d6fc | bellard | if (val == 0xa0) |
763 | 0824d6fc | bellard | s->priority_add = (s->priority_add + 1) & 7; |
764 | 0824d6fc | bellard | break;
|
765 | 0824d6fc | bellard | case 0x60 ... 0x67: |
766 | 0824d6fc | bellard | priority = val & 7;
|
767 | 0824d6fc | bellard | s->isr &= ~(1 << priority);
|
768 | 0824d6fc | bellard | break;
|
769 | 0824d6fc | bellard | case 0xc0 ... 0xc7: |
770 | 0824d6fc | bellard | s->priority_add = (val + 1) & 7; |
771 | 0824d6fc | bellard | break;
|
772 | 0824d6fc | bellard | case 0xe0 ... 0xe7: |
773 | 0824d6fc | bellard | priority = val & 7;
|
774 | 0824d6fc | bellard | s->isr &= ~(1 << priority);
|
775 | 0824d6fc | bellard | s->priority_add = (priority + 1) & 7; |
776 | 0824d6fc | bellard | break;
|
777 | 0824d6fc | bellard | } |
778 | 0824d6fc | bellard | } |
779 | 0824d6fc | bellard | } else {
|
780 | 0824d6fc | bellard | switch(s->init_state) {
|
781 | 0824d6fc | bellard | case 0: |
782 | 0824d6fc | bellard | /* normal mode */
|
783 | 0824d6fc | bellard | s->imr = val; |
784 | c9159e53 | bellard | pic_update_irq(); |
785 | 0824d6fc | bellard | break;
|
786 | 0824d6fc | bellard | case 1: |
787 | 0824d6fc | bellard | s->irq_base = val & 0xf8;
|
788 | 0824d6fc | bellard | s->init_state = 2;
|
789 | 0824d6fc | bellard | break;
|
790 | 0824d6fc | bellard | case 2: |
791 | 0824d6fc | bellard | if (s->init4) {
|
792 | 0824d6fc | bellard | s->init_state = 3;
|
793 | 0824d6fc | bellard | } else {
|
794 | 0824d6fc | bellard | s->init_state = 0;
|
795 | 0824d6fc | bellard | } |
796 | 0824d6fc | bellard | break;
|
797 | 0824d6fc | bellard | case 3: |
798 | 0824d6fc | bellard | s->auto_eoi = (val >> 1) & 1; |
799 | 0824d6fc | bellard | s->init_state = 0;
|
800 | 0824d6fc | bellard | break;
|
801 | 0824d6fc | bellard | } |
802 | 0824d6fc | bellard | } |
803 | 0824d6fc | bellard | } |
804 | 0824d6fc | bellard | |
805 | b118d61e | bellard | uint32_t pic_ioport_read(CPUX86State *env, uint32_t addr1) |
806 | 0824d6fc | bellard | { |
807 | 0824d6fc | bellard | PicState *s; |
808 | b118d61e | bellard | unsigned int addr; |
809 | b118d61e | bellard | int ret;
|
810 | b118d61e | bellard | |
811 | b118d61e | bellard | addr = addr1; |
812 | 0824d6fc | bellard | s = &pics[addr >> 7];
|
813 | 0824d6fc | bellard | addr &= 1;
|
814 | 0824d6fc | bellard | if (addr == 0) { |
815 | 0824d6fc | bellard | if (s->read_reg_select)
|
816 | b118d61e | bellard | ret = s->isr; |
817 | 0824d6fc | bellard | else
|
818 | b118d61e | bellard | ret = s->irr; |
819 | 0824d6fc | bellard | } else {
|
820 | b118d61e | bellard | ret = s->imr; |
821 | 0824d6fc | bellard | } |
822 | b118d61e | bellard | #ifdef DEBUG_PIC
|
823 | b118d61e | bellard | printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
|
824 | b118d61e | bellard | #endif
|
825 | b118d61e | bellard | return ret;
|
826 | 0824d6fc | bellard | } |
827 | 0824d6fc | bellard | |
828 | 0824d6fc | bellard | void pic_init(void) |
829 | 0824d6fc | bellard | { |
830 | fc01f7e7 | bellard | register_ioport_write(0x20, 2, pic_ioport_write, 1); |
831 | fc01f7e7 | bellard | register_ioport_read(0x20, 2, pic_ioport_read, 1); |
832 | fc01f7e7 | bellard | register_ioport_write(0xa0, 2, pic_ioport_write, 1); |
833 | fc01f7e7 | bellard | register_ioport_read(0xa0, 2, pic_ioport_read, 1); |
834 | 0824d6fc | bellard | } |
835 | 0824d6fc | bellard | |
836 | 0824d6fc | bellard | /***********************************************************/
|
837 | 0824d6fc | bellard | /* 8253 PIT emulation */
|
838 | 0824d6fc | bellard | |
839 | 0824d6fc | bellard | #define PIT_FREQ 1193182 |
840 | 0824d6fc | bellard | |
841 | 0824d6fc | bellard | #define RW_STATE_LSB 0 |
842 | 0824d6fc | bellard | #define RW_STATE_MSB 1 |
843 | 0824d6fc | bellard | #define RW_STATE_WORD0 2 |
844 | 0824d6fc | bellard | #define RW_STATE_WORD1 3 |
845 | 0824d6fc | bellard | #define RW_STATE_LATCHED_WORD0 4 |
846 | 0824d6fc | bellard | #define RW_STATE_LATCHED_WORD1 5 |
847 | 0824d6fc | bellard | |
848 | 0824d6fc | bellard | typedef struct PITChannelState { |
849 | 87858c89 | bellard | int count; /* can be 65536 */ |
850 | 0824d6fc | bellard | uint16_t latched_count; |
851 | 0824d6fc | bellard | uint8_t rw_state; |
852 | 0824d6fc | bellard | uint8_t mode; |
853 | 0824d6fc | bellard | uint8_t bcd; /* not supported */
|
854 | 0824d6fc | bellard | uint8_t gate; /* timer start */
|
855 | 0824d6fc | bellard | int64_t count_load_time; |
856 | 87858c89 | bellard | int64_t count_last_edge_check_time; |
857 | 0824d6fc | bellard | } PITChannelState; |
858 | 0824d6fc | bellard | |
859 | 0824d6fc | bellard | PITChannelState pit_channels[3];
|
860 | 0824d6fc | bellard | int speaker_data_on;
|
861 | 61a2ad53 | bellard | int dummy_refresh_clock;
|
862 | 87858c89 | bellard | int pit_min_timer_count = 0; |
863 | 0824d6fc | bellard | |
864 | 0824d6fc | bellard | int64_t ticks_per_sec; |
865 | 0824d6fc | bellard | |
866 | 0824d6fc | bellard | int64_t get_clock(void)
|
867 | 0824d6fc | bellard | { |
868 | 0824d6fc | bellard | struct timeval tv;
|
869 | 0824d6fc | bellard | gettimeofday(&tv, NULL);
|
870 | 0824d6fc | bellard | return tv.tv_sec * 1000000LL + tv.tv_usec; |
871 | 0824d6fc | bellard | } |
872 | 0824d6fc | bellard | |
873 | 0824d6fc | bellard | int64_t cpu_get_ticks(void)
|
874 | 0824d6fc | bellard | { |
875 | 0824d6fc | bellard | int64_t val; |
876 | 0824d6fc | bellard | asm("rdtsc" : "=A" (val)); |
877 | 0824d6fc | bellard | return val;
|
878 | 0824d6fc | bellard | } |
879 | 0824d6fc | bellard | |
880 | 0824d6fc | bellard | void cpu_calibrate_ticks(void) |
881 | 0824d6fc | bellard | { |
882 | 0824d6fc | bellard | int64_t usec, ticks; |
883 | 0824d6fc | bellard | |
884 | 0824d6fc | bellard | usec = get_clock(); |
885 | 0824d6fc | bellard | ticks = cpu_get_ticks(); |
886 | 0824d6fc | bellard | usleep(50 * 1000); |
887 | 0824d6fc | bellard | usec = get_clock() - usec; |
888 | 0824d6fc | bellard | ticks = cpu_get_ticks() - ticks; |
889 | 0824d6fc | bellard | ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; |
890 | 0824d6fc | bellard | } |
891 | 0824d6fc | bellard | |
892 | 87858c89 | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
893 | 87858c89 | bellard | static uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
|
894 | 87858c89 | bellard | { |
895 | 87858c89 | bellard | union {
|
896 | 87858c89 | bellard | uint64_t ll; |
897 | 87858c89 | bellard | struct {
|
898 | 87858c89 | bellard | #ifdef WORDS_BIGENDIAN
|
899 | 87858c89 | bellard | uint32_t high, low; |
900 | 87858c89 | bellard | #else
|
901 | 87858c89 | bellard | uint32_t low, high; |
902 | 87858c89 | bellard | #endif
|
903 | 87858c89 | bellard | } l; |
904 | 87858c89 | bellard | } u, res; |
905 | 87858c89 | bellard | uint64_t rl, rh; |
906 | 87858c89 | bellard | |
907 | 87858c89 | bellard | u.ll = a; |
908 | 87858c89 | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
909 | 87858c89 | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
910 | 87858c89 | bellard | rh += (rl >> 32);
|
911 | 87858c89 | bellard | res.l.high = rh / c; |
912 | 87858c89 | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
913 | 87858c89 | bellard | return res.ll;
|
914 | 87858c89 | bellard | } |
915 | 87858c89 | bellard | |
916 | 0824d6fc | bellard | static int pit_get_count(PITChannelState *s) |
917 | 0824d6fc | bellard | { |
918 | 87858c89 | bellard | uint64_t d; |
919 | 0824d6fc | bellard | int counter;
|
920 | 0824d6fc | bellard | |
921 | 87858c89 | bellard | d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec); |
922 | 0824d6fc | bellard | switch(s->mode) {
|
923 | 0824d6fc | bellard | case 0: |
924 | 0824d6fc | bellard | case 1: |
925 | 0824d6fc | bellard | case 4: |
926 | 0824d6fc | bellard | case 5: |
927 | 0824d6fc | bellard | counter = (s->count - d) & 0xffff;
|
928 | 0824d6fc | bellard | break;
|
929 | 0824d6fc | bellard | default:
|
930 | 0824d6fc | bellard | counter = s->count - (d % s->count); |
931 | 0824d6fc | bellard | break;
|
932 | 0824d6fc | bellard | } |
933 | 0824d6fc | bellard | return counter;
|
934 | 0824d6fc | bellard | } |
935 | 0824d6fc | bellard | |
936 | 0824d6fc | bellard | /* get pit output bit */
|
937 | 0824d6fc | bellard | static int pit_get_out(PITChannelState *s) |
938 | 0824d6fc | bellard | { |
939 | 87858c89 | bellard | uint64_t d; |
940 | 0824d6fc | bellard | int out;
|
941 | 0824d6fc | bellard | |
942 | 87858c89 | bellard | d = muldiv64(cpu_get_ticks() - s->count_load_time, PIT_FREQ, ticks_per_sec); |
943 | 0824d6fc | bellard | switch(s->mode) {
|
944 | 0824d6fc | bellard | default:
|
945 | 0824d6fc | bellard | case 0: |
946 | 0824d6fc | bellard | out = (d >= s->count); |
947 | 0824d6fc | bellard | break;
|
948 | 0824d6fc | bellard | case 1: |
949 | 0824d6fc | bellard | out = (d < s->count); |
950 | 0824d6fc | bellard | break;
|
951 | 0824d6fc | bellard | case 2: |
952 | 0824d6fc | bellard | if ((d % s->count) == 0 && d != 0) |
953 | 0824d6fc | bellard | out = 1;
|
954 | 0824d6fc | bellard | else
|
955 | 0824d6fc | bellard | out = 0;
|
956 | 0824d6fc | bellard | break;
|
957 | 0824d6fc | bellard | case 3: |
958 | 0824d6fc | bellard | out = (d % s->count) < (s->count >> 1);
|
959 | 0824d6fc | bellard | break;
|
960 | 0824d6fc | bellard | case 4: |
961 | 0824d6fc | bellard | case 5: |
962 | 0824d6fc | bellard | out = (d == s->count); |
963 | 0824d6fc | bellard | break;
|
964 | 0824d6fc | bellard | } |
965 | 0824d6fc | bellard | return out;
|
966 | 0824d6fc | bellard | } |
967 | 0824d6fc | bellard | |
968 | 87858c89 | bellard | /* get the number of 0 to 1 transitions we had since we call this
|
969 | 87858c89 | bellard | function */
|
970 | 87858c89 | bellard | /* XXX: maybe better to use ticks precision to avoid getting edges
|
971 | 87858c89 | bellard | twice if checks are done at very small intervals */
|
972 | 87858c89 | bellard | static int pit_get_out_edges(PITChannelState *s) |
973 | 87858c89 | bellard | { |
974 | 87858c89 | bellard | uint64_t d1, d2; |
975 | 87858c89 | bellard | int64_t ticks; |
976 | 87858c89 | bellard | int ret, v;
|
977 | 87858c89 | bellard | |
978 | 87858c89 | bellard | ticks = cpu_get_ticks(); |
979 | 87858c89 | bellard | d1 = muldiv64(s->count_last_edge_check_time - s->count_load_time, |
980 | 87858c89 | bellard | PIT_FREQ, ticks_per_sec); |
981 | 87858c89 | bellard | d2 = muldiv64(ticks - s->count_load_time, |
982 | 87858c89 | bellard | PIT_FREQ, ticks_per_sec); |
983 | 87858c89 | bellard | s->count_last_edge_check_time = ticks; |
984 | 87858c89 | bellard | switch(s->mode) {
|
985 | 87858c89 | bellard | default:
|
986 | 87858c89 | bellard | case 0: |
987 | 87858c89 | bellard | if (d1 < s->count && d2 >= s->count)
|
988 | 87858c89 | bellard | ret = 1;
|
989 | 87858c89 | bellard | else
|
990 | 87858c89 | bellard | ret = 0;
|
991 | 87858c89 | bellard | break;
|
992 | 87858c89 | bellard | case 1: |
993 | 87858c89 | bellard | ret = 0;
|
994 | 87858c89 | bellard | break;
|
995 | 87858c89 | bellard | case 2: |
996 | 87858c89 | bellard | d1 /= s->count; |
997 | 87858c89 | bellard | d2 /= s->count; |
998 | 87858c89 | bellard | ret = d2 - d1; |
999 | 87858c89 | bellard | break;
|
1000 | 87858c89 | bellard | case 3: |
1001 | 87858c89 | bellard | v = s->count - (s->count >> 1);
|
1002 | 87858c89 | bellard | d1 = (d1 + v) / s->count; |
1003 | 87858c89 | bellard | d2 = (d2 + v) / s->count; |
1004 | 87858c89 | bellard | ret = d2 - d1; |
1005 | 87858c89 | bellard | break;
|
1006 | 87858c89 | bellard | case 4: |
1007 | 87858c89 | bellard | case 5: |
1008 | 87858c89 | bellard | if (d1 < s->count && d2 >= s->count)
|
1009 | 87858c89 | bellard | ret = 1;
|
1010 | 87858c89 | bellard | else
|
1011 | 87858c89 | bellard | ret = 0;
|
1012 | 87858c89 | bellard | break;
|
1013 | 87858c89 | bellard | } |
1014 | 87858c89 | bellard | return ret;
|
1015 | 87858c89 | bellard | } |
1016 | 87858c89 | bellard | |
1017 | 87858c89 | bellard | static inline void pit_load_count(PITChannelState *s, int val) |
1018 | 87858c89 | bellard | { |
1019 | 87858c89 | bellard | if (val == 0) |
1020 | 87858c89 | bellard | val = 0x10000;
|
1021 | 87858c89 | bellard | s->count_load_time = cpu_get_ticks(); |
1022 | 87858c89 | bellard | s->count_last_edge_check_time = s->count_load_time; |
1023 | 87858c89 | bellard | s->count = val; |
1024 | 87858c89 | bellard | if (s == &pit_channels[0] && val <= pit_min_timer_count) { |
1025 | 87858c89 | bellard | fprintf(stderr, |
1026 | 87858c89 | bellard | "\nWARNING: vl: on your system, accurate timer emulation is impossible if its frequency is more than %d Hz. If using a 2.5.xx Linux kernel, you must patch asm/param.h to change HZ from 1000 to 100.\n\n",
|
1027 | 87858c89 | bellard | PIT_FREQ / pit_min_timer_count); |
1028 | 87858c89 | bellard | } |
1029 | 87858c89 | bellard | } |
1030 | 87858c89 | bellard | |
1031 | 0824d6fc | bellard | void pit_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
1032 | 0824d6fc | bellard | { |
1033 | 0824d6fc | bellard | int channel, access;
|
1034 | 0824d6fc | bellard | PITChannelState *s; |
1035 | 87858c89 | bellard | |
1036 | 0824d6fc | bellard | addr &= 3;
|
1037 | 0824d6fc | bellard | if (addr == 3) { |
1038 | 0824d6fc | bellard | channel = val >> 6;
|
1039 | 0824d6fc | bellard | if (channel == 3) |
1040 | 0824d6fc | bellard | return;
|
1041 | 0824d6fc | bellard | s = &pit_channels[channel]; |
1042 | 0824d6fc | bellard | access = (val >> 4) & 3; |
1043 | 0824d6fc | bellard | switch(access) {
|
1044 | 0824d6fc | bellard | case 0: |
1045 | 0824d6fc | bellard | s->latched_count = pit_get_count(s); |
1046 | 0824d6fc | bellard | s->rw_state = RW_STATE_LATCHED_WORD0; |
1047 | 0824d6fc | bellard | break;
|
1048 | 0824d6fc | bellard | default:
|
1049 | 87858c89 | bellard | s->mode = (val >> 1) & 7; |
1050 | 87858c89 | bellard | s->bcd = val & 1;
|
1051 | 0824d6fc | bellard | s->rw_state = access - 1 + RW_STATE_LSB;
|
1052 | 0824d6fc | bellard | break;
|
1053 | 0824d6fc | bellard | } |
1054 | 0824d6fc | bellard | } else {
|
1055 | 0824d6fc | bellard | s = &pit_channels[addr]; |
1056 | 0824d6fc | bellard | switch(s->rw_state) {
|
1057 | 0824d6fc | bellard | case RW_STATE_LSB:
|
1058 | 87858c89 | bellard | pit_load_count(s, val); |
1059 | 0824d6fc | bellard | break;
|
1060 | 0824d6fc | bellard | case RW_STATE_MSB:
|
1061 | 87858c89 | bellard | pit_load_count(s, val << 8);
|
1062 | 0824d6fc | bellard | break;
|
1063 | 0824d6fc | bellard | case RW_STATE_WORD0:
|
1064 | 0824d6fc | bellard | case RW_STATE_WORD1:
|
1065 | 0824d6fc | bellard | if (s->rw_state & 1) { |
1066 | 87858c89 | bellard | pit_load_count(s, (s->latched_count & 0xff) | (val << 8)); |
1067 | 0824d6fc | bellard | } else {
|
1068 | 0824d6fc | bellard | s->latched_count = val; |
1069 | 0824d6fc | bellard | } |
1070 | 0824d6fc | bellard | s->rw_state ^= 1;
|
1071 | 0824d6fc | bellard | break;
|
1072 | 0824d6fc | bellard | } |
1073 | 0824d6fc | bellard | } |
1074 | 0824d6fc | bellard | } |
1075 | 0824d6fc | bellard | |
1076 | 0824d6fc | bellard | uint32_t pit_ioport_read(CPUX86State *env, uint32_t addr) |
1077 | 0824d6fc | bellard | { |
1078 | 0824d6fc | bellard | int ret, count;
|
1079 | 0824d6fc | bellard | PITChannelState *s; |
1080 | 0824d6fc | bellard | |
1081 | 0824d6fc | bellard | addr &= 3;
|
1082 | 0824d6fc | bellard | s = &pit_channels[addr]; |
1083 | 0824d6fc | bellard | switch(s->rw_state) {
|
1084 | 0824d6fc | bellard | case RW_STATE_LSB:
|
1085 | 0824d6fc | bellard | case RW_STATE_MSB:
|
1086 | 0824d6fc | bellard | case RW_STATE_WORD0:
|
1087 | 0824d6fc | bellard | case RW_STATE_WORD1:
|
1088 | 0824d6fc | bellard | count = pit_get_count(s); |
1089 | 0824d6fc | bellard | if (s->rw_state & 1) |
1090 | 0824d6fc | bellard | ret = (count >> 8) & 0xff; |
1091 | 0824d6fc | bellard | else
|
1092 | 0824d6fc | bellard | ret = count & 0xff;
|
1093 | 0824d6fc | bellard | if (s->rw_state & 2) |
1094 | 0824d6fc | bellard | s->rw_state ^= 1;
|
1095 | 0824d6fc | bellard | break;
|
1096 | 0824d6fc | bellard | default:
|
1097 | 0824d6fc | bellard | case RW_STATE_LATCHED_WORD0:
|
1098 | 0824d6fc | bellard | case RW_STATE_LATCHED_WORD1:
|
1099 | 0824d6fc | bellard | if (s->rw_state & 1) |
1100 | 0824d6fc | bellard | ret = s->latched_count >> 8;
|
1101 | 0824d6fc | bellard | else
|
1102 | 0824d6fc | bellard | ret = s->latched_count & 0xff;
|
1103 | 0824d6fc | bellard | s->rw_state ^= 1;
|
1104 | 0824d6fc | bellard | break;
|
1105 | 0824d6fc | bellard | } |
1106 | 0824d6fc | bellard | return ret;
|
1107 | 0824d6fc | bellard | } |
1108 | 0824d6fc | bellard | |
1109 | 0824d6fc | bellard | void speaker_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
1110 | 0824d6fc | bellard | { |
1111 | 0824d6fc | bellard | speaker_data_on = (val >> 1) & 1; |
1112 | 0824d6fc | bellard | pit_channels[2].gate = val & 1; |
1113 | 0824d6fc | bellard | } |
1114 | 0824d6fc | bellard | |
1115 | 0824d6fc | bellard | uint32_t speaker_ioport_read(CPUX86State *env, uint32_t addr) |
1116 | 0824d6fc | bellard | { |
1117 | 0824d6fc | bellard | int out;
|
1118 | 0824d6fc | bellard | out = pit_get_out(&pit_channels[2]);
|
1119 | 61a2ad53 | bellard | dummy_refresh_clock ^= 1;
|
1120 | 61a2ad53 | bellard | return (speaker_data_on << 1) | pit_channels[2].gate | (out << 5) | |
1121 | 61a2ad53 | bellard | (dummy_refresh_clock << 4);
|
1122 | 0824d6fc | bellard | } |
1123 | 0824d6fc | bellard | |
1124 | 0824d6fc | bellard | void pit_init(void) |
1125 | 0824d6fc | bellard | { |
1126 | 87858c89 | bellard | PITChannelState *s; |
1127 | 87858c89 | bellard | int i;
|
1128 | 87858c89 | bellard | |
1129 | 87858c89 | bellard | cpu_calibrate_ticks(); |
1130 | 87858c89 | bellard | |
1131 | 87858c89 | bellard | for(i = 0;i < 3; i++) { |
1132 | 87858c89 | bellard | s = &pit_channels[i]; |
1133 | 87858c89 | bellard | s->mode = 3;
|
1134 | 87858c89 | bellard | s->gate = (i != 2);
|
1135 | 87858c89 | bellard | pit_load_count(s, 0);
|
1136 | 87858c89 | bellard | } |
1137 | 87858c89 | bellard | |
1138 | fc01f7e7 | bellard | register_ioport_write(0x40, 4, pit_ioport_write, 1); |
1139 | fc01f7e7 | bellard | register_ioport_read(0x40, 3, pit_ioport_read, 1); |
1140 | 0824d6fc | bellard | |
1141 | fc01f7e7 | bellard | register_ioport_read(0x61, 1, speaker_ioport_read, 1); |
1142 | fc01f7e7 | bellard | register_ioport_write(0x61, 1, speaker_ioport_write, 1); |
1143 | 0824d6fc | bellard | } |
1144 | 0824d6fc | bellard | |
1145 | 0824d6fc | bellard | /***********************************************************/
|
1146 | 0824d6fc | bellard | /* serial port emulation */
|
1147 | 0824d6fc | bellard | |
1148 | 0824d6fc | bellard | #define UART_IRQ 4 |
1149 | 0824d6fc | bellard | |
1150 | 0824d6fc | bellard | #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */ |
1151 | 0824d6fc | bellard | |
1152 | 0824d6fc | bellard | #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */ |
1153 | 0824d6fc | bellard | #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */ |
1154 | 0824d6fc | bellard | #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */ |
1155 | 0824d6fc | bellard | #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */ |
1156 | 0824d6fc | bellard | |
1157 | 0824d6fc | bellard | #define UART_IIR_NO_INT 0x01 /* No interrupts pending */ |
1158 | 0824d6fc | bellard | #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */ |
1159 | 0824d6fc | bellard | |
1160 | 0824d6fc | bellard | #define UART_IIR_MSI 0x00 /* Modem status interrupt */ |
1161 | 0824d6fc | bellard | #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */ |
1162 | 0824d6fc | bellard | #define UART_IIR_RDI 0x04 /* Receiver data interrupt */ |
1163 | 0824d6fc | bellard | #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */ |
1164 | 0824d6fc | bellard | |
1165 | 0824d6fc | bellard | #define UART_LSR_TEMT 0x40 /* Transmitter empty */ |
1166 | 0824d6fc | bellard | #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ |
1167 | 0824d6fc | bellard | #define UART_LSR_BI 0x10 /* Break interrupt indicator */ |
1168 | 0824d6fc | bellard | #define UART_LSR_FE 0x08 /* Frame error indicator */ |
1169 | 0824d6fc | bellard | #define UART_LSR_PE 0x04 /* Parity error indicator */ |
1170 | 0824d6fc | bellard | #define UART_LSR_OE 0x02 /* Overrun error indicator */ |
1171 | 0824d6fc | bellard | #define UART_LSR_DR 0x01 /* Receiver data ready */ |
1172 | 0824d6fc | bellard | |
1173 | 0824d6fc | bellard | typedef struct SerialState { |
1174 | 0824d6fc | bellard | uint8_t divider; |
1175 | 0824d6fc | bellard | uint8_t rbr; /* receive register */
|
1176 | 0824d6fc | bellard | uint8_t ier; |
1177 | 0824d6fc | bellard | uint8_t iir; /* read only */
|
1178 | 0824d6fc | bellard | uint8_t lcr; |
1179 | 0824d6fc | bellard | uint8_t mcr; |
1180 | 0824d6fc | bellard | uint8_t lsr; /* read only */
|
1181 | 0824d6fc | bellard | uint8_t msr; |
1182 | 0824d6fc | bellard | uint8_t scr; |
1183 | 0824d6fc | bellard | } SerialState; |
1184 | 0824d6fc | bellard | |
1185 | 0824d6fc | bellard | SerialState serial_ports[1];
|
1186 | 0824d6fc | bellard | |
1187 | 0824d6fc | bellard | void serial_update_irq(void) |
1188 | 0824d6fc | bellard | { |
1189 | 0824d6fc | bellard | SerialState *s = &serial_ports[0];
|
1190 | 0824d6fc | bellard | |
1191 | 0824d6fc | bellard | if ((s->lsr & UART_LSR_DR) && (s->ier & UART_IER_RDI)) {
|
1192 | 0824d6fc | bellard | s->iir = UART_IIR_RDI; |
1193 | 0824d6fc | bellard | } else if ((s->lsr & UART_LSR_THRE) && (s->ier & UART_IER_THRI)) { |
1194 | 0824d6fc | bellard | s->iir = UART_IIR_THRI; |
1195 | 0824d6fc | bellard | } else {
|
1196 | 0824d6fc | bellard | s->iir = UART_IIR_NO_INT; |
1197 | 0824d6fc | bellard | } |
1198 | 0824d6fc | bellard | if (s->iir != UART_IIR_NO_INT) {
|
1199 | 0824d6fc | bellard | pic_set_irq(UART_IRQ, 1);
|
1200 | 0824d6fc | bellard | } else {
|
1201 | 0824d6fc | bellard | pic_set_irq(UART_IRQ, 0);
|
1202 | 0824d6fc | bellard | } |
1203 | 0824d6fc | bellard | } |
1204 | 0824d6fc | bellard | |
1205 | 0824d6fc | bellard | void serial_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
1206 | 0824d6fc | bellard | { |
1207 | 0824d6fc | bellard | SerialState *s = &serial_ports[0];
|
1208 | 0824d6fc | bellard | unsigned char ch; |
1209 | 0824d6fc | bellard | int ret;
|
1210 | 0824d6fc | bellard | |
1211 | 0824d6fc | bellard | addr &= 7;
|
1212 | 0824d6fc | bellard | switch(addr) {
|
1213 | 0824d6fc | bellard | default:
|
1214 | 0824d6fc | bellard | case 0: |
1215 | 0824d6fc | bellard | if (s->lcr & UART_LCR_DLAB) {
|
1216 | 0824d6fc | bellard | s->divider = (s->divider & 0xff00) | val;
|
1217 | 0824d6fc | bellard | } else {
|
1218 | 0824d6fc | bellard | s->lsr &= ~UART_LSR_THRE; |
1219 | 0824d6fc | bellard | serial_update_irq(); |
1220 | 0824d6fc | bellard | |
1221 | 0824d6fc | bellard | ch = val; |
1222 | 0824d6fc | bellard | do {
|
1223 | 0824d6fc | bellard | ret = write(1, &ch, 1); |
1224 | 0824d6fc | bellard | } while (ret != 1); |
1225 | 0824d6fc | bellard | s->lsr |= UART_LSR_THRE; |
1226 | 0824d6fc | bellard | s->lsr |= UART_LSR_TEMT; |
1227 | 0824d6fc | bellard | serial_update_irq(); |
1228 | 0824d6fc | bellard | } |
1229 | 0824d6fc | bellard | break;
|
1230 | 0824d6fc | bellard | case 1: |
1231 | 0824d6fc | bellard | if (s->lcr & UART_LCR_DLAB) {
|
1232 | 0824d6fc | bellard | s->divider = (s->divider & 0x00ff) | (val << 8); |
1233 | 0824d6fc | bellard | } else {
|
1234 | 0824d6fc | bellard | s->ier = val; |
1235 | 0824d6fc | bellard | serial_update_irq(); |
1236 | 0824d6fc | bellard | } |
1237 | 0824d6fc | bellard | break;
|
1238 | 0824d6fc | bellard | case 2: |
1239 | 0824d6fc | bellard | break;
|
1240 | 0824d6fc | bellard | case 3: |
1241 | 0824d6fc | bellard | s->lcr = val; |
1242 | 0824d6fc | bellard | break;
|
1243 | 0824d6fc | bellard | case 4: |
1244 | 0824d6fc | bellard | s->mcr = val; |
1245 | 0824d6fc | bellard | break;
|
1246 | 0824d6fc | bellard | case 5: |
1247 | 0824d6fc | bellard | break;
|
1248 | 0824d6fc | bellard | case 6: |
1249 | 0824d6fc | bellard | s->msr = val; |
1250 | 0824d6fc | bellard | break;
|
1251 | 0824d6fc | bellard | case 7: |
1252 | 0824d6fc | bellard | s->scr = val; |
1253 | 0824d6fc | bellard | break;
|
1254 | 0824d6fc | bellard | } |
1255 | 0824d6fc | bellard | } |
1256 | 0824d6fc | bellard | |
1257 | 0824d6fc | bellard | uint32_t serial_ioport_read(CPUX86State *env, uint32_t addr) |
1258 | 0824d6fc | bellard | { |
1259 | 0824d6fc | bellard | SerialState *s = &serial_ports[0];
|
1260 | 0824d6fc | bellard | uint32_t ret; |
1261 | 0824d6fc | bellard | |
1262 | 0824d6fc | bellard | addr &= 7;
|
1263 | 0824d6fc | bellard | switch(addr) {
|
1264 | 0824d6fc | bellard | default:
|
1265 | 0824d6fc | bellard | case 0: |
1266 | 0824d6fc | bellard | if (s->lcr & UART_LCR_DLAB) {
|
1267 | 0824d6fc | bellard | ret = s->divider & 0xff;
|
1268 | 0824d6fc | bellard | } else {
|
1269 | 0824d6fc | bellard | ret = s->rbr; |
1270 | 0824d6fc | bellard | s->lsr &= ~(UART_LSR_DR | UART_LSR_BI); |
1271 | 0824d6fc | bellard | serial_update_irq(); |
1272 | 0824d6fc | bellard | } |
1273 | 0824d6fc | bellard | break;
|
1274 | 0824d6fc | bellard | case 1: |
1275 | 0824d6fc | bellard | if (s->lcr & UART_LCR_DLAB) {
|
1276 | 0824d6fc | bellard | ret = (s->divider >> 8) & 0xff; |
1277 | 0824d6fc | bellard | } else {
|
1278 | 0824d6fc | bellard | ret = s->ier; |
1279 | 0824d6fc | bellard | } |
1280 | 0824d6fc | bellard | break;
|
1281 | 0824d6fc | bellard | case 2: |
1282 | 0824d6fc | bellard | ret = s->iir; |
1283 | 0824d6fc | bellard | break;
|
1284 | 0824d6fc | bellard | case 3: |
1285 | 0824d6fc | bellard | ret = s->lcr; |
1286 | 0824d6fc | bellard | break;
|
1287 | 0824d6fc | bellard | case 4: |
1288 | 0824d6fc | bellard | ret = s->mcr; |
1289 | 0824d6fc | bellard | break;
|
1290 | 0824d6fc | bellard | case 5: |
1291 | 0824d6fc | bellard | ret = s->lsr; |
1292 | 0824d6fc | bellard | break;
|
1293 | 0824d6fc | bellard | case 6: |
1294 | 0824d6fc | bellard | ret = s->msr; |
1295 | 0824d6fc | bellard | break;
|
1296 | 0824d6fc | bellard | case 7: |
1297 | 0824d6fc | bellard | ret = s->scr; |
1298 | 0824d6fc | bellard | break;
|
1299 | 0824d6fc | bellard | } |
1300 | 0824d6fc | bellard | return ret;
|
1301 | 0824d6fc | bellard | } |
1302 | 0824d6fc | bellard | |
1303 | 0824d6fc | bellard | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ |
1304 | 0824d6fc | bellard | static int term_got_escape; |
1305 | 0824d6fc | bellard | |
1306 | 0824d6fc | bellard | void term_print_help(void) |
1307 | 0824d6fc | bellard | { |
1308 | 0824d6fc | bellard | printf("\n"
|
1309 | 0824d6fc | bellard | "C-a h print this help\n"
|
1310 | 0824d6fc | bellard | "C-a x exit emulatior\n"
|
1311 | 33e3963e | bellard | "C-a s save disk data back to file (if -snapshot)\n"
|
1312 | 0824d6fc | bellard | "C-a b send break (magic sysrq)\n"
|
1313 | 0824d6fc | bellard | "C-a C-a send C-a\n"
|
1314 | 0824d6fc | bellard | ); |
1315 | 0824d6fc | bellard | } |
1316 | 0824d6fc | bellard | |
1317 | 0824d6fc | bellard | /* called when a char is received */
|
1318 | 0824d6fc | bellard | void serial_received_byte(SerialState *s, int ch) |
1319 | 0824d6fc | bellard | { |
1320 | 0824d6fc | bellard | if (term_got_escape) {
|
1321 | 0824d6fc | bellard | term_got_escape = 0;
|
1322 | 0824d6fc | bellard | switch(ch) {
|
1323 | 0824d6fc | bellard | case 'h': |
1324 | 0824d6fc | bellard | term_print_help(); |
1325 | 0824d6fc | bellard | break;
|
1326 | 0824d6fc | bellard | case 'x': |
1327 | 0824d6fc | bellard | exit(0);
|
1328 | 0824d6fc | bellard | break;
|
1329 | 33e3963e | bellard | case 's': |
1330 | 33e3963e | bellard | { |
1331 | 33e3963e | bellard | int i;
|
1332 | 33e3963e | bellard | for (i = 0; i < MAX_DISKS; i++) { |
1333 | 33e3963e | bellard | if (bs_table[i])
|
1334 | 33e3963e | bellard | bdrv_commit(bs_table[i]); |
1335 | 33e3963e | bellard | } |
1336 | 33e3963e | bellard | } |
1337 | 33e3963e | bellard | break;
|
1338 | 0824d6fc | bellard | case 'b': |
1339 | 0824d6fc | bellard | /* send break */
|
1340 | 0824d6fc | bellard | s->rbr = 0;
|
1341 | 0824d6fc | bellard | s->lsr |= UART_LSR_BI | UART_LSR_DR; |
1342 | 0824d6fc | bellard | serial_update_irq(); |
1343 | 0824d6fc | bellard | break;
|
1344 | 0824d6fc | bellard | case TERM_ESCAPE:
|
1345 | 0824d6fc | bellard | goto send_char;
|
1346 | 0824d6fc | bellard | } |
1347 | 0824d6fc | bellard | } else if (ch == TERM_ESCAPE) { |
1348 | 0824d6fc | bellard | term_got_escape = 1;
|
1349 | 0824d6fc | bellard | } else {
|
1350 | 0824d6fc | bellard | send_char:
|
1351 | 0824d6fc | bellard | s->rbr = ch; |
1352 | 0824d6fc | bellard | s->lsr |= UART_LSR_DR; |
1353 | 0824d6fc | bellard | serial_update_irq(); |
1354 | 0824d6fc | bellard | } |
1355 | 0824d6fc | bellard | } |
1356 | 0824d6fc | bellard | |
1357 | 0824d6fc | bellard | /* init terminal so that we can grab keys */
|
1358 | 0824d6fc | bellard | static struct termios oldtty; |
1359 | 0824d6fc | bellard | |
1360 | 0824d6fc | bellard | static void term_exit(void) |
1361 | 0824d6fc | bellard | { |
1362 | 0824d6fc | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
1363 | 0824d6fc | bellard | } |
1364 | 0824d6fc | bellard | |
1365 | 0824d6fc | bellard | static void term_init(void) |
1366 | 0824d6fc | bellard | { |
1367 | 0824d6fc | bellard | struct termios tty;
|
1368 | 0824d6fc | bellard | |
1369 | 0824d6fc | bellard | tcgetattr (0, &tty);
|
1370 | 0824d6fc | bellard | oldtty = tty; |
1371 | 0824d6fc | bellard | |
1372 | 0824d6fc | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
1373 | 0824d6fc | bellard | |INLCR|IGNCR|ICRNL|IXON); |
1374 | 0824d6fc | bellard | tty.c_oflag |= OPOST; |
1375 | 0824d6fc | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
1376 | 0824d6fc | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
1377 | 0824d6fc | bellard | tty.c_cflag |= CS8; |
1378 | 0824d6fc | bellard | tty.c_cc[VMIN] = 1;
|
1379 | 0824d6fc | bellard | tty.c_cc[VTIME] = 0;
|
1380 | 0824d6fc | bellard | |
1381 | 0824d6fc | bellard | tcsetattr (0, TCSANOW, &tty);
|
1382 | 0824d6fc | bellard | |
1383 | 0824d6fc | bellard | atexit(term_exit); |
1384 | 0824d6fc | bellard | |
1385 | 0824d6fc | bellard | fcntl(0, F_SETFL, O_NONBLOCK);
|
1386 | 0824d6fc | bellard | } |
1387 | 0824d6fc | bellard | |
1388 | 0824d6fc | bellard | void serial_init(void) |
1389 | 0824d6fc | bellard | { |
1390 | 0824d6fc | bellard | SerialState *s = &serial_ports[0];
|
1391 | 0824d6fc | bellard | |
1392 | 0824d6fc | bellard | s->lsr = UART_LSR_TEMT | UART_LSR_THRE; |
1393 | 0824d6fc | bellard | |
1394 | fc01f7e7 | bellard | register_ioport_write(0x3f8, 8, serial_ioport_write, 1); |
1395 | fc01f7e7 | bellard | register_ioport_read(0x3f8, 8, serial_ioport_read, 1); |
1396 | 0824d6fc | bellard | |
1397 | 0824d6fc | bellard | term_init(); |
1398 | 0824d6fc | bellard | } |
1399 | 0824d6fc | bellard | |
1400 | f1510b2c | bellard | /***********************************************************/
|
1401 | f1510b2c | bellard | /* ne2000 emulation */
|
1402 | f1510b2c | bellard | |
1403 | f1510b2c | bellard | #define NE2000_IOPORT 0x300 |
1404 | f1510b2c | bellard | #define NE2000_IRQ 9 |
1405 | f1510b2c | bellard | |
1406 | f1510b2c | bellard | #define MAX_ETH_FRAME_SIZE 1514 |
1407 | f1510b2c | bellard | |
1408 | f1510b2c | bellard | #define E8390_CMD 0x00 /* The command register (for all pages) */ |
1409 | f1510b2c | bellard | /* Page 0 register offsets. */
|
1410 | f1510b2c | bellard | #define EN0_CLDALO 0x01 /* Low byte of current local dma addr RD */ |
1411 | f1510b2c | bellard | #define EN0_STARTPG 0x01 /* Starting page of ring bfr WR */ |
1412 | f1510b2c | bellard | #define EN0_CLDAHI 0x02 /* High byte of current local dma addr RD */ |
1413 | f1510b2c | bellard | #define EN0_STOPPG 0x02 /* Ending page +1 of ring bfr WR */ |
1414 | f1510b2c | bellard | #define EN0_BOUNDARY 0x03 /* Boundary page of ring bfr RD WR */ |
1415 | f1510b2c | bellard | #define EN0_TSR 0x04 /* Transmit status reg RD */ |
1416 | f1510b2c | bellard | #define EN0_TPSR 0x04 /* Transmit starting page WR */ |
1417 | f1510b2c | bellard | #define EN0_NCR 0x05 /* Number of collision reg RD */ |
1418 | f1510b2c | bellard | #define EN0_TCNTLO 0x05 /* Low byte of tx byte count WR */ |
1419 | f1510b2c | bellard | #define EN0_FIFO 0x06 /* FIFO RD */ |
1420 | f1510b2c | bellard | #define EN0_TCNTHI 0x06 /* High byte of tx byte count WR */ |
1421 | f1510b2c | bellard | #define EN0_ISR 0x07 /* Interrupt status reg RD WR */ |
1422 | f1510b2c | bellard | #define EN0_CRDALO 0x08 /* low byte of current remote dma address RD */ |
1423 | f1510b2c | bellard | #define EN0_RSARLO 0x08 /* Remote start address reg 0 */ |
1424 | f1510b2c | bellard | #define EN0_CRDAHI 0x09 /* high byte, current remote dma address RD */ |
1425 | f1510b2c | bellard | #define EN0_RSARHI 0x09 /* Remote start address reg 1 */ |
1426 | f1510b2c | bellard | #define EN0_RCNTLO 0x0a /* Remote byte count reg WR */ |
1427 | f1510b2c | bellard | #define EN0_RCNTHI 0x0b /* Remote byte count reg WR */ |
1428 | f1510b2c | bellard | #define EN0_RSR 0x0c /* rx status reg RD */ |
1429 | f1510b2c | bellard | #define EN0_RXCR 0x0c /* RX configuration reg WR */ |
1430 | f1510b2c | bellard | #define EN0_TXCR 0x0d /* TX configuration reg WR */ |
1431 | f1510b2c | bellard | #define EN0_COUNTER0 0x0d /* Rcv alignment error counter RD */ |
1432 | f1510b2c | bellard | #define EN0_DCFG 0x0e /* Data configuration reg WR */ |
1433 | f1510b2c | bellard | #define EN0_COUNTER1 0x0e /* Rcv CRC error counter RD */ |
1434 | f1510b2c | bellard | #define EN0_IMR 0x0f /* Interrupt mask reg WR */ |
1435 | f1510b2c | bellard | #define EN0_COUNTER2 0x0f /* Rcv missed frame error counter RD */ |
1436 | f1510b2c | bellard | |
1437 | f1510b2c | bellard | #define EN1_PHYS 0x11 |
1438 | f1510b2c | bellard | #define EN1_CURPAG 0x17 |
1439 | f1510b2c | bellard | #define EN1_MULT 0x18 |
1440 | f1510b2c | bellard | |
1441 | f1510b2c | bellard | /* Register accessed at EN_CMD, the 8390 base addr. */
|
1442 | f1510b2c | bellard | #define E8390_STOP 0x01 /* Stop and reset the chip */ |
1443 | f1510b2c | bellard | #define E8390_START 0x02 /* Start the chip, clear reset */ |
1444 | f1510b2c | bellard | #define E8390_TRANS 0x04 /* Transmit a frame */ |
1445 | f1510b2c | bellard | #define E8390_RREAD 0x08 /* Remote read */ |
1446 | f1510b2c | bellard | #define E8390_RWRITE 0x10 /* Remote write */ |
1447 | f1510b2c | bellard | #define E8390_NODMA 0x20 /* Remote DMA */ |
1448 | f1510b2c | bellard | #define E8390_PAGE0 0x00 /* Select page chip registers */ |
1449 | f1510b2c | bellard | #define E8390_PAGE1 0x40 /* using the two high-order bits */ |
1450 | f1510b2c | bellard | #define E8390_PAGE2 0x80 /* Page 3 is invalid. */ |
1451 | f1510b2c | bellard | |
1452 | f1510b2c | bellard | /* Bits in EN0_ISR - Interrupt status register */
|
1453 | f1510b2c | bellard | #define ENISR_RX 0x01 /* Receiver, no error */ |
1454 | f1510b2c | bellard | #define ENISR_TX 0x02 /* Transmitter, no error */ |
1455 | f1510b2c | bellard | #define ENISR_RX_ERR 0x04 /* Receiver, with error */ |
1456 | f1510b2c | bellard | #define ENISR_TX_ERR 0x08 /* Transmitter, with error */ |
1457 | f1510b2c | bellard | #define ENISR_OVER 0x10 /* Receiver overwrote the ring */ |
1458 | f1510b2c | bellard | #define ENISR_COUNTERS 0x20 /* Counters need emptying */ |
1459 | f1510b2c | bellard | #define ENISR_RDC 0x40 /* remote dma complete */ |
1460 | f1510b2c | bellard | #define ENISR_RESET 0x80 /* Reset completed */ |
1461 | f1510b2c | bellard | #define ENISR_ALL 0x3f /* Interrupts we will enable */ |
1462 | f1510b2c | bellard | |
1463 | f1510b2c | bellard | /* Bits in received packet status byte and EN0_RSR*/
|
1464 | f1510b2c | bellard | #define ENRSR_RXOK 0x01 /* Received a good packet */ |
1465 | f1510b2c | bellard | #define ENRSR_CRC 0x02 /* CRC error */ |
1466 | f1510b2c | bellard | #define ENRSR_FAE 0x04 /* frame alignment error */ |
1467 | f1510b2c | bellard | #define ENRSR_FO 0x08 /* FIFO overrun */ |
1468 | f1510b2c | bellard | #define ENRSR_MPA 0x10 /* missed pkt */ |
1469 | f1510b2c | bellard | #define ENRSR_PHY 0x20 /* physical/multicast address */ |
1470 | f1510b2c | bellard | #define ENRSR_DIS 0x40 /* receiver disable. set in monitor mode */ |
1471 | f1510b2c | bellard | #define ENRSR_DEF 0x80 /* deferring */ |
1472 | f1510b2c | bellard | |
1473 | f1510b2c | bellard | /* Transmitted packet status, EN0_TSR. */
|
1474 | f1510b2c | bellard | #define ENTSR_PTX 0x01 /* Packet transmitted without error */ |
1475 | f1510b2c | bellard | #define ENTSR_ND 0x02 /* The transmit wasn't deferred. */ |
1476 | f1510b2c | bellard | #define ENTSR_COL 0x04 /* The transmit collided at least once. */ |
1477 | f1510b2c | bellard | #define ENTSR_ABT 0x08 /* The transmit collided 16 times, and was deferred. */ |
1478 | f1510b2c | bellard | #define ENTSR_CRS 0x10 /* The carrier sense was lost. */ |
1479 | f1510b2c | bellard | #define ENTSR_FU 0x20 /* A "FIFO underrun" occurred during transmit. */ |
1480 | f1510b2c | bellard | #define ENTSR_CDH 0x40 /* The collision detect "heartbeat" signal was lost. */ |
1481 | f1510b2c | bellard | #define ENTSR_OWC 0x80 /* There was an out-of-window collision. */ |
1482 | f1510b2c | bellard | |
1483 | f1510b2c | bellard | #define NE2000_MEM_SIZE 32768 |
1484 | f1510b2c | bellard | |
1485 | f1510b2c | bellard | typedef struct NE2000State { |
1486 | f1510b2c | bellard | uint8_t cmd; |
1487 | f1510b2c | bellard | uint32_t start; |
1488 | f1510b2c | bellard | uint32_t stop; |
1489 | f1510b2c | bellard | uint8_t boundary; |
1490 | f1510b2c | bellard | uint8_t tsr; |
1491 | f1510b2c | bellard | uint8_t tpsr; |
1492 | f1510b2c | bellard | uint16_t tcnt; |
1493 | f1510b2c | bellard | uint16_t rcnt; |
1494 | f1510b2c | bellard | uint32_t rsar; |
1495 | f1510b2c | bellard | uint8_t isr; |
1496 | f1510b2c | bellard | uint8_t dcfg; |
1497 | f1510b2c | bellard | uint8_t imr; |
1498 | f1510b2c | bellard | uint8_t phys[6]; /* mac address */ |
1499 | f1510b2c | bellard | uint8_t curpag; |
1500 | f1510b2c | bellard | uint8_t mult[8]; /* multicast mask array */ |
1501 | f1510b2c | bellard | uint8_t mem[NE2000_MEM_SIZE]; |
1502 | f1510b2c | bellard | } NE2000State; |
1503 | f1510b2c | bellard | |
1504 | f1510b2c | bellard | NE2000State ne2000_state; |
1505 | f1510b2c | bellard | int net_fd = -1; |
1506 | f1510b2c | bellard | char network_script[1024]; |
1507 | f1510b2c | bellard | |
1508 | f1510b2c | bellard | void ne2000_reset(void) |
1509 | f1510b2c | bellard | { |
1510 | f1510b2c | bellard | NE2000State *s = &ne2000_state; |
1511 | f1510b2c | bellard | int i;
|
1512 | f1510b2c | bellard | |
1513 | f1510b2c | bellard | s->isr = ENISR_RESET; |
1514 | f1510b2c | bellard | s->mem[0] = 0x52; |
1515 | f1510b2c | bellard | s->mem[1] = 0x54; |
1516 | f1510b2c | bellard | s->mem[2] = 0x00; |
1517 | f1510b2c | bellard | s->mem[3] = 0x12; |
1518 | f1510b2c | bellard | s->mem[4] = 0x34; |
1519 | f1510b2c | bellard | s->mem[5] = 0x56; |
1520 | f1510b2c | bellard | s->mem[14] = 0x57; |
1521 | f1510b2c | bellard | s->mem[15] = 0x57; |
1522 | f1510b2c | bellard | |
1523 | f1510b2c | bellard | /* duplicate prom data */
|
1524 | f1510b2c | bellard | for(i = 15;i >= 0; i--) { |
1525 | f1510b2c | bellard | s->mem[2 * i] = s->mem[i];
|
1526 | f1510b2c | bellard | s->mem[2 * i + 1] = s->mem[i]; |
1527 | f1510b2c | bellard | } |
1528 | f1510b2c | bellard | } |
1529 | f1510b2c | bellard | |
1530 | f1510b2c | bellard | void ne2000_update_irq(NE2000State *s)
|
1531 | f1510b2c | bellard | { |
1532 | f1510b2c | bellard | int isr;
|
1533 | f1510b2c | bellard | isr = s->isr & s->imr; |
1534 | f1510b2c | bellard | if (isr)
|
1535 | f1510b2c | bellard | pic_set_irq(NE2000_IRQ, 1);
|
1536 | f1510b2c | bellard | else
|
1537 | f1510b2c | bellard | pic_set_irq(NE2000_IRQ, 0);
|
1538 | f1510b2c | bellard | } |
1539 | f1510b2c | bellard | |
1540 | f1510b2c | bellard | int net_init(void) |
1541 | f1510b2c | bellard | { |
1542 | f1510b2c | bellard | struct ifreq ifr;
|
1543 | f1510b2c | bellard | int fd, ret, pid, status;
|
1544 | f1510b2c | bellard | |
1545 | f1510b2c | bellard | fd = open("/dev/net/tun", O_RDWR);
|
1546 | f1510b2c | bellard | if (fd < 0) { |
1547 | f1510b2c | bellard | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
1548 | f1510b2c | bellard | return -1; |
1549 | f1510b2c | bellard | } |
1550 | f1510b2c | bellard | memset(&ifr, 0, sizeof(ifr)); |
1551 | f1510b2c | bellard | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
1552 | f1510b2c | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
|
1553 | f1510b2c | bellard | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
1554 | f1510b2c | bellard | if (ret != 0) { |
1555 | f1510b2c | bellard | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
1556 | f1510b2c | bellard | close(fd); |
1557 | f1510b2c | bellard | return -1; |
1558 | f1510b2c | bellard | } |
1559 | fc01f7e7 | bellard | printf("Connected to host network interface: %s\n", ifr.ifr_name);
|
1560 | f1510b2c | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
1561 | f1510b2c | bellard | net_fd = fd; |
1562 | f1510b2c | bellard | |
1563 | f1510b2c | bellard | /* try to launch network init script */
|
1564 | f1510b2c | bellard | pid = fork(); |
1565 | f1510b2c | bellard | if (pid >= 0) { |
1566 | f1510b2c | bellard | if (pid == 0) { |
1567 | f1510b2c | bellard | execl(network_script, network_script, ifr.ifr_name, NULL);
|
1568 | f1510b2c | bellard | exit(1);
|
1569 | f1510b2c | bellard | } |
1570 | f1510b2c | bellard | while (waitpid(pid, &status, 0) != pid); |
1571 | f1510b2c | bellard | if (!WIFEXITED(status) ||
|
1572 | f1510b2c | bellard | WEXITSTATUS(status) != 0) {
|
1573 | f1510b2c | bellard | fprintf(stderr, "%s: could not launch network script for '%s'\n",
|
1574 | f1510b2c | bellard | network_script, ifr.ifr_name); |
1575 | f1510b2c | bellard | } |
1576 | f1510b2c | bellard | } |
1577 | f1510b2c | bellard | return 0; |
1578 | f1510b2c | bellard | } |
1579 | f1510b2c | bellard | |
1580 | f1510b2c | bellard | void net_send_packet(NE2000State *s, const uint8_t *buf, int size) |
1581 | f1510b2c | bellard | { |
1582 | f1510b2c | bellard | #ifdef DEBUG_NE2000
|
1583 | f1510b2c | bellard | printf("NE2000: sending packet size=%d\n", size);
|
1584 | f1510b2c | bellard | #endif
|
1585 | f1510b2c | bellard | write(net_fd, buf, size); |
1586 | f1510b2c | bellard | } |
1587 | f1510b2c | bellard | |
1588 | f1510b2c | bellard | /* return true if the NE2000 can receive more data */
|
1589 | f1510b2c | bellard | int ne2000_can_receive(NE2000State *s)
|
1590 | f1510b2c | bellard | { |
1591 | f1510b2c | bellard | int avail, index, boundary;
|
1592 | f1510b2c | bellard | |
1593 | f1510b2c | bellard | if (s->cmd & E8390_STOP)
|
1594 | f1510b2c | bellard | return 0; |
1595 | f1510b2c | bellard | index = s->curpag << 8;
|
1596 | f1510b2c | bellard | boundary = s->boundary << 8;
|
1597 | f1510b2c | bellard | if (index < boundary)
|
1598 | f1510b2c | bellard | avail = boundary - index; |
1599 | f1510b2c | bellard | else
|
1600 | f1510b2c | bellard | avail = (s->stop - s->start) - (index - boundary); |
1601 | f1510b2c | bellard | if (avail < (MAX_ETH_FRAME_SIZE + 4)) |
1602 | f1510b2c | bellard | return 0; |
1603 | f1510b2c | bellard | return 1; |
1604 | f1510b2c | bellard | } |
1605 | f1510b2c | bellard | |
1606 | f1510b2c | bellard | void ne2000_receive(NE2000State *s, uint8_t *buf, int size) |
1607 | f1510b2c | bellard | { |
1608 | f1510b2c | bellard | uint8_t *p; |
1609 | f1510b2c | bellard | int total_len, next, avail, len, index;
|
1610 | f1510b2c | bellard | |
1611 | f1510b2c | bellard | #if defined(DEBUG_NE2000)
|
1612 | f1510b2c | bellard | printf("NE2000: received len=%d\n", size);
|
1613 | f1510b2c | bellard | #endif
|
1614 | f1510b2c | bellard | |
1615 | f1510b2c | bellard | index = s->curpag << 8;
|
1616 | f1510b2c | bellard | /* 4 bytes for header */
|
1617 | f1510b2c | bellard | total_len = size + 4;
|
1618 | f1510b2c | bellard | /* address for next packet (4 bytes for CRC) */
|
1619 | f1510b2c | bellard | next = index + ((total_len + 4 + 255) & ~0xff); |
1620 | f1510b2c | bellard | if (next >= s->stop)
|
1621 | f1510b2c | bellard | next -= (s->stop - s->start); |
1622 | f1510b2c | bellard | /* prepare packet header */
|
1623 | f1510b2c | bellard | p = s->mem + index; |
1624 | f1510b2c | bellard | p[0] = ENRSR_RXOK; /* receive status */ |
1625 | f1510b2c | bellard | p[1] = next >> 8; |
1626 | f1510b2c | bellard | p[2] = total_len;
|
1627 | f1510b2c | bellard | p[3] = total_len >> 8; |
1628 | f1510b2c | bellard | index += 4;
|
1629 | f1510b2c | bellard | |
1630 | f1510b2c | bellard | /* write packet data */
|
1631 | f1510b2c | bellard | while (size > 0) { |
1632 | f1510b2c | bellard | avail = s->stop - index; |
1633 | f1510b2c | bellard | len = size; |
1634 | f1510b2c | bellard | if (len > avail)
|
1635 | f1510b2c | bellard | len = avail; |
1636 | f1510b2c | bellard | memcpy(s->mem + index, buf, len); |
1637 | f1510b2c | bellard | buf += len; |
1638 | f1510b2c | bellard | index += len; |
1639 | f1510b2c | bellard | if (index == s->stop)
|
1640 | f1510b2c | bellard | index = s->start; |
1641 | f1510b2c | bellard | size -= len; |
1642 | f1510b2c | bellard | } |
1643 | f1510b2c | bellard | s->curpag = next >> 8;
|
1644 | f1510b2c | bellard | |
1645 | f1510b2c | bellard | /* now we can signal we have receive something */
|
1646 | f1510b2c | bellard | s->isr |= ENISR_RX; |
1647 | f1510b2c | bellard | ne2000_update_irq(s); |
1648 | f1510b2c | bellard | } |
1649 | f1510b2c | bellard | |
1650 | f1510b2c | bellard | void ne2000_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
1651 | f1510b2c | bellard | { |
1652 | f1510b2c | bellard | NE2000State *s = &ne2000_state; |
1653 | f1510b2c | bellard | int offset, page;
|
1654 | f1510b2c | bellard | |
1655 | f1510b2c | bellard | addr &= 0xf;
|
1656 | f1510b2c | bellard | #ifdef DEBUG_NE2000
|
1657 | f1510b2c | bellard | printf("NE2000: write addr=0x%x val=0x%02x\n", addr, val);
|
1658 | f1510b2c | bellard | #endif
|
1659 | f1510b2c | bellard | if (addr == E8390_CMD) {
|
1660 | f1510b2c | bellard | /* control register */
|
1661 | f1510b2c | bellard | s->cmd = val; |
1662 | f1510b2c | bellard | if (val & E8390_START) {
|
1663 | f1510b2c | bellard | /* test specific case: zero length transfert */
|
1664 | f1510b2c | bellard | if ((val & (E8390_RREAD | E8390_RWRITE)) &&
|
1665 | f1510b2c | bellard | s->rcnt == 0) {
|
1666 | f1510b2c | bellard | s->isr |= ENISR_RDC; |
1667 | f1510b2c | bellard | ne2000_update_irq(s); |
1668 | f1510b2c | bellard | } |
1669 | f1510b2c | bellard | if (val & E8390_TRANS) {
|
1670 | f1510b2c | bellard | net_send_packet(s, s->mem + (s->tpsr << 8), s->tcnt);
|
1671 | f1510b2c | bellard | /* signal end of transfert */
|
1672 | f1510b2c | bellard | s->tsr = ENTSR_PTX; |
1673 | f1510b2c | bellard | s->isr |= ENISR_TX; |
1674 | f1510b2c | bellard | ne2000_update_irq(s); |
1675 | f1510b2c | bellard | } |
1676 | f1510b2c | bellard | } |
1677 | f1510b2c | bellard | } else {
|
1678 | f1510b2c | bellard | page = s->cmd >> 6;
|
1679 | f1510b2c | bellard | offset = addr | (page << 4);
|
1680 | f1510b2c | bellard | switch(offset) {
|
1681 | f1510b2c | bellard | case EN0_STARTPG:
|
1682 | f1510b2c | bellard | s->start = val << 8;
|
1683 | f1510b2c | bellard | break;
|
1684 | f1510b2c | bellard | case EN0_STOPPG:
|
1685 | f1510b2c | bellard | s->stop = val << 8;
|
1686 | f1510b2c | bellard | break;
|
1687 | f1510b2c | bellard | case EN0_BOUNDARY:
|
1688 | f1510b2c | bellard | s->boundary = val; |
1689 | f1510b2c | bellard | break;
|
1690 | f1510b2c | bellard | case EN0_IMR:
|
1691 | f1510b2c | bellard | s->imr = val; |
1692 | f1510b2c | bellard | ne2000_update_irq(s); |
1693 | f1510b2c | bellard | break;
|
1694 | f1510b2c | bellard | case EN0_TPSR:
|
1695 | f1510b2c | bellard | s->tpsr = val; |
1696 | f1510b2c | bellard | break;
|
1697 | f1510b2c | bellard | case EN0_TCNTLO:
|
1698 | f1510b2c | bellard | s->tcnt = (s->tcnt & 0xff00) | val;
|
1699 | f1510b2c | bellard | break;
|
1700 | f1510b2c | bellard | case EN0_TCNTHI:
|
1701 | f1510b2c | bellard | s->tcnt = (s->tcnt & 0x00ff) | (val << 8); |
1702 | f1510b2c | bellard | break;
|
1703 | f1510b2c | bellard | case EN0_RSARLO:
|
1704 | f1510b2c | bellard | s->rsar = (s->rsar & 0xff00) | val;
|
1705 | f1510b2c | bellard | break;
|
1706 | f1510b2c | bellard | case EN0_RSARHI:
|
1707 | f1510b2c | bellard | s->rsar = (s->rsar & 0x00ff) | (val << 8); |
1708 | f1510b2c | bellard | break;
|
1709 | f1510b2c | bellard | case EN0_RCNTLO:
|
1710 | f1510b2c | bellard | s->rcnt = (s->rcnt & 0xff00) | val;
|
1711 | f1510b2c | bellard | break;
|
1712 | f1510b2c | bellard | case EN0_RCNTHI:
|
1713 | f1510b2c | bellard | s->rcnt = (s->rcnt & 0x00ff) | (val << 8); |
1714 | f1510b2c | bellard | break;
|
1715 | f1510b2c | bellard | case EN0_DCFG:
|
1716 | f1510b2c | bellard | s->dcfg = val; |
1717 | f1510b2c | bellard | break;
|
1718 | f1510b2c | bellard | case EN0_ISR:
|
1719 | f1510b2c | bellard | s->isr &= ~val; |
1720 | f1510b2c | bellard | ne2000_update_irq(s); |
1721 | f1510b2c | bellard | break;
|
1722 | f1510b2c | bellard | case EN1_PHYS ... EN1_PHYS + 5: |
1723 | f1510b2c | bellard | s->phys[offset - EN1_PHYS] = val; |
1724 | f1510b2c | bellard | break;
|
1725 | f1510b2c | bellard | case EN1_CURPAG:
|
1726 | f1510b2c | bellard | s->curpag = val; |
1727 | f1510b2c | bellard | break;
|
1728 | f1510b2c | bellard | case EN1_MULT ... EN1_MULT + 7: |
1729 | f1510b2c | bellard | s->mult[offset - EN1_MULT] = val; |
1730 | f1510b2c | bellard | break;
|
1731 | f1510b2c | bellard | } |
1732 | f1510b2c | bellard | } |
1733 | f1510b2c | bellard | } |
1734 | f1510b2c | bellard | |
1735 | f1510b2c | bellard | uint32_t ne2000_ioport_read(CPUX86State *env, uint32_t addr) |
1736 | f1510b2c | bellard | { |
1737 | f1510b2c | bellard | NE2000State *s = &ne2000_state; |
1738 | f1510b2c | bellard | int offset, page, ret;
|
1739 | f1510b2c | bellard | |
1740 | f1510b2c | bellard | addr &= 0xf;
|
1741 | f1510b2c | bellard | if (addr == E8390_CMD) {
|
1742 | f1510b2c | bellard | ret = s->cmd; |
1743 | f1510b2c | bellard | } else {
|
1744 | f1510b2c | bellard | page = s->cmd >> 6;
|
1745 | f1510b2c | bellard | offset = addr | (page << 4);
|
1746 | f1510b2c | bellard | switch(offset) {
|
1747 | f1510b2c | bellard | case EN0_TSR:
|
1748 | f1510b2c | bellard | ret = s->tsr; |
1749 | f1510b2c | bellard | break;
|
1750 | f1510b2c | bellard | case EN0_BOUNDARY:
|
1751 | f1510b2c | bellard | ret = s->boundary; |
1752 | f1510b2c | bellard | break;
|
1753 | f1510b2c | bellard | case EN0_ISR:
|
1754 | f1510b2c | bellard | ret = s->isr; |
1755 | f1510b2c | bellard | break;
|
1756 | f1510b2c | bellard | case EN1_PHYS ... EN1_PHYS + 5: |
1757 | f1510b2c | bellard | ret = s->phys[offset - EN1_PHYS]; |
1758 | f1510b2c | bellard | break;
|
1759 | f1510b2c | bellard | case EN1_CURPAG:
|
1760 | f1510b2c | bellard | ret = s->curpag; |
1761 | f1510b2c | bellard | break;
|
1762 | f1510b2c | bellard | case EN1_MULT ... EN1_MULT + 7: |
1763 | f1510b2c | bellard | ret = s->mult[offset - EN1_MULT]; |
1764 | f1510b2c | bellard | break;
|
1765 | f1510b2c | bellard | default:
|
1766 | f1510b2c | bellard | ret = 0x00;
|
1767 | f1510b2c | bellard | break;
|
1768 | f1510b2c | bellard | } |
1769 | f1510b2c | bellard | } |
1770 | f1510b2c | bellard | #ifdef DEBUG_NE2000
|
1771 | f1510b2c | bellard | printf("NE2000: read addr=0x%x val=%02x\n", addr, ret);
|
1772 | f1510b2c | bellard | #endif
|
1773 | f1510b2c | bellard | return ret;
|
1774 | f1510b2c | bellard | } |
1775 | f1510b2c | bellard | |
1776 | f1510b2c | bellard | void ne2000_asic_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
1777 | f1510b2c | bellard | { |
1778 | f1510b2c | bellard | NE2000State *s = &ne2000_state; |
1779 | f1510b2c | bellard | uint8_t *p; |
1780 | f1510b2c | bellard | |
1781 | f1510b2c | bellard | #ifdef DEBUG_NE2000
|
1782 | f1510b2c | bellard | printf("NE2000: asic write val=0x%04x\n", val);
|
1783 | f1510b2c | bellard | #endif
|
1784 | f1510b2c | bellard | p = s->mem + s->rsar; |
1785 | f1510b2c | bellard | if (s->dcfg & 0x01) { |
1786 | f1510b2c | bellard | /* 16 bit access */
|
1787 | f1510b2c | bellard | p[0] = val;
|
1788 | f1510b2c | bellard | p[1] = val >> 8; |
1789 | f1510b2c | bellard | s->rsar += 2;
|
1790 | f1510b2c | bellard | s->rcnt -= 2;
|
1791 | f1510b2c | bellard | } else {
|
1792 | f1510b2c | bellard | /* 8 bit access */
|
1793 | f1510b2c | bellard | p[0] = val;
|
1794 | f1510b2c | bellard | s->rsar++; |
1795 | f1510b2c | bellard | s->rcnt--; |
1796 | f1510b2c | bellard | } |
1797 | f1510b2c | bellard | /* wrap */
|
1798 | f1510b2c | bellard | if (s->rsar == s->stop)
|
1799 | f1510b2c | bellard | s->rsar = s->start; |
1800 | f1510b2c | bellard | if (s->rcnt == 0) { |
1801 | f1510b2c | bellard | /* signal end of transfert */
|
1802 | f1510b2c | bellard | s->isr |= ENISR_RDC; |
1803 | f1510b2c | bellard | ne2000_update_irq(s); |
1804 | f1510b2c | bellard | } |
1805 | f1510b2c | bellard | } |
1806 | f1510b2c | bellard | |
1807 | f1510b2c | bellard | uint32_t ne2000_asic_ioport_read(CPUX86State *env, uint32_t addr) |
1808 | f1510b2c | bellard | { |
1809 | f1510b2c | bellard | NE2000State *s = &ne2000_state; |
1810 | f1510b2c | bellard | uint8_t *p; |
1811 | f1510b2c | bellard | int ret;
|
1812 | f1510b2c | bellard | |
1813 | f1510b2c | bellard | p = s->mem + s->rsar; |
1814 | f1510b2c | bellard | if (s->dcfg & 0x01) { |
1815 | f1510b2c | bellard | /* 16 bit access */
|
1816 | f1510b2c | bellard | ret = p[0] | (p[1] << 8); |
1817 | f1510b2c | bellard | s->rsar += 2;
|
1818 | f1510b2c | bellard | s->rcnt -= 2;
|
1819 | f1510b2c | bellard | } else {
|
1820 | f1510b2c | bellard | /* 8 bit access */
|
1821 | f1510b2c | bellard | ret = p[0];
|
1822 | f1510b2c | bellard | s->rsar++; |
1823 | f1510b2c | bellard | s->rcnt--; |
1824 | f1510b2c | bellard | } |
1825 | f1510b2c | bellard | /* wrap */
|
1826 | f1510b2c | bellard | if (s->rsar == s->stop)
|
1827 | f1510b2c | bellard | s->rsar = s->start; |
1828 | f1510b2c | bellard | if (s->rcnt == 0) { |
1829 | f1510b2c | bellard | /* signal end of transfert */
|
1830 | f1510b2c | bellard | s->isr |= ENISR_RDC; |
1831 | f1510b2c | bellard | ne2000_update_irq(s); |
1832 | f1510b2c | bellard | } |
1833 | f1510b2c | bellard | #ifdef DEBUG_NE2000
|
1834 | f1510b2c | bellard | printf("NE2000: asic read val=0x%04x\n", ret);
|
1835 | f1510b2c | bellard | #endif
|
1836 | f1510b2c | bellard | return ret;
|
1837 | f1510b2c | bellard | } |
1838 | f1510b2c | bellard | |
1839 | f1510b2c | bellard | void ne2000_reset_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
1840 | f1510b2c | bellard | { |
1841 | f1510b2c | bellard | /* nothing to do (end of reset pulse) */
|
1842 | f1510b2c | bellard | } |
1843 | f1510b2c | bellard | |
1844 | f1510b2c | bellard | uint32_t ne2000_reset_ioport_read(CPUX86State *env, uint32_t addr) |
1845 | f1510b2c | bellard | { |
1846 | f1510b2c | bellard | ne2000_reset(); |
1847 | f1510b2c | bellard | return 0; |
1848 | f1510b2c | bellard | } |
1849 | f1510b2c | bellard | |
1850 | f1510b2c | bellard | void ne2000_init(void) |
1851 | f1510b2c | bellard | { |
1852 | fc01f7e7 | bellard | register_ioport_write(NE2000_IOPORT, 16, ne2000_ioport_write, 1); |
1853 | fc01f7e7 | bellard | register_ioport_read(NE2000_IOPORT, 16, ne2000_ioport_read, 1); |
1854 | f1510b2c | bellard | |
1855 | fc01f7e7 | bellard | register_ioport_write(NE2000_IOPORT + 0x10, 1, ne2000_asic_ioport_write, 1); |
1856 | fc01f7e7 | bellard | register_ioport_read(NE2000_IOPORT + 0x10, 1, ne2000_asic_ioport_read, 1); |
1857 | fc01f7e7 | bellard | register_ioport_write(NE2000_IOPORT + 0x10, 2, ne2000_asic_ioport_write, 2); |
1858 | fc01f7e7 | bellard | register_ioport_read(NE2000_IOPORT + 0x10, 2, ne2000_asic_ioport_read, 2); |
1859 | f1510b2c | bellard | |
1860 | fc01f7e7 | bellard | register_ioport_write(NE2000_IOPORT + 0x1f, 1, ne2000_reset_ioport_write, 1); |
1861 | fc01f7e7 | bellard | register_ioport_read(NE2000_IOPORT + 0x1f, 1, ne2000_reset_ioport_read, 1); |
1862 | f1510b2c | bellard | ne2000_reset(); |
1863 | f1510b2c | bellard | } |
1864 | f1510b2c | bellard | |
1865 | f1510b2c | bellard | /***********************************************************/
|
1866 | fc01f7e7 | bellard | /* ide emulation */
|
1867 | fc01f7e7 | bellard | |
1868 | fc01f7e7 | bellard | /* Bits of HD_STATUS */
|
1869 | fc01f7e7 | bellard | #define ERR_STAT 0x01 |
1870 | fc01f7e7 | bellard | #define INDEX_STAT 0x02 |
1871 | fc01f7e7 | bellard | #define ECC_STAT 0x04 /* Corrected error */ |
1872 | fc01f7e7 | bellard | #define DRQ_STAT 0x08 |
1873 | fc01f7e7 | bellard | #define SEEK_STAT 0x10 |
1874 | fc01f7e7 | bellard | #define SRV_STAT 0x10 |
1875 | fc01f7e7 | bellard | #define WRERR_STAT 0x20 |
1876 | fc01f7e7 | bellard | #define READY_STAT 0x40 |
1877 | fc01f7e7 | bellard | #define BUSY_STAT 0x80 |
1878 | fc01f7e7 | bellard | |
1879 | fc01f7e7 | bellard | /* Bits for HD_ERROR */
|
1880 | fc01f7e7 | bellard | #define MARK_ERR 0x01 /* Bad address mark */ |
1881 | fc01f7e7 | bellard | #define TRK0_ERR 0x02 /* couldn't find track 0 */ |
1882 | fc01f7e7 | bellard | #define ABRT_ERR 0x04 /* Command aborted */ |
1883 | fc01f7e7 | bellard | #define MCR_ERR 0x08 /* media change request */ |
1884 | fc01f7e7 | bellard | #define ID_ERR 0x10 /* ID field not found */ |
1885 | fc01f7e7 | bellard | #define MC_ERR 0x20 /* media changed */ |
1886 | fc01f7e7 | bellard | #define ECC_ERR 0x40 /* Uncorrectable ECC error */ |
1887 | fc01f7e7 | bellard | #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */ |
1888 | fc01f7e7 | bellard | #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */ |
1889 | fc01f7e7 | bellard | |
1890 | fc01f7e7 | bellard | /* Bits of HD_NSECTOR */
|
1891 | fc01f7e7 | bellard | #define CD 0x01 |
1892 | fc01f7e7 | bellard | #define IO 0x02 |
1893 | fc01f7e7 | bellard | #define REL 0x04 |
1894 | fc01f7e7 | bellard | #define TAG_MASK 0xf8 |
1895 | fc01f7e7 | bellard | |
1896 | fc01f7e7 | bellard | #define IDE_CMD_RESET 0x04 |
1897 | fc01f7e7 | bellard | #define IDE_CMD_DISABLE_IRQ 0x02 |
1898 | fc01f7e7 | bellard | |
1899 | fc01f7e7 | bellard | /* ATA/ATAPI Commands pre T13 Spec */
|
1900 | fc01f7e7 | bellard | #define WIN_NOP 0x00 |
1901 | fc01f7e7 | bellard | /*
|
1902 | fc01f7e7 | bellard | * 0x01->0x02 Reserved
|
1903 | fc01f7e7 | bellard | */
|
1904 | fc01f7e7 | bellard | #define CFA_REQ_EXT_ERROR_CODE 0x03 /* CFA Request Extended Error Code */ |
1905 | fc01f7e7 | bellard | /*
|
1906 | fc01f7e7 | bellard | * 0x04->0x07 Reserved
|
1907 | fc01f7e7 | bellard | */
|
1908 | fc01f7e7 | bellard | #define WIN_SRST 0x08 /* ATAPI soft reset command */ |
1909 | fc01f7e7 | bellard | #define WIN_DEVICE_RESET 0x08 |
1910 | fc01f7e7 | bellard | /*
|
1911 | fc01f7e7 | bellard | * 0x09->0x0F Reserved
|
1912 | fc01f7e7 | bellard | */
|
1913 | fc01f7e7 | bellard | #define WIN_RECAL 0x10 |
1914 | fc01f7e7 | bellard | #define WIN_RESTORE WIN_RECAL
|
1915 | fc01f7e7 | bellard | /*
|
1916 | fc01f7e7 | bellard | * 0x10->0x1F Reserved
|
1917 | fc01f7e7 | bellard | */
|
1918 | fc01f7e7 | bellard | #define WIN_READ 0x20 /* 28-Bit */ |
1919 | fc01f7e7 | bellard | #define WIN_READ_ONCE 0x21 /* 28-Bit without retries */ |
1920 | fc01f7e7 | bellard | #define WIN_READ_LONG 0x22 /* 28-Bit */ |
1921 | fc01f7e7 | bellard | #define WIN_READ_LONG_ONCE 0x23 /* 28-Bit without retries */ |
1922 | fc01f7e7 | bellard | #define WIN_READ_EXT 0x24 /* 48-Bit */ |
1923 | fc01f7e7 | bellard | #define WIN_READDMA_EXT 0x25 /* 48-Bit */ |
1924 | fc01f7e7 | bellard | #define WIN_READDMA_QUEUED_EXT 0x26 /* 48-Bit */ |
1925 | fc01f7e7 | bellard | #define WIN_READ_NATIVE_MAX_EXT 0x27 /* 48-Bit */ |
1926 | fc01f7e7 | bellard | /*
|
1927 | fc01f7e7 | bellard | * 0x28
|
1928 | fc01f7e7 | bellard | */
|
1929 | fc01f7e7 | bellard | #define WIN_MULTREAD_EXT 0x29 /* 48-Bit */ |
1930 | fc01f7e7 | bellard | /*
|
1931 | fc01f7e7 | bellard | * 0x2A->0x2F Reserved
|
1932 | fc01f7e7 | bellard | */
|
1933 | fc01f7e7 | bellard | #define WIN_WRITE 0x30 /* 28-Bit */ |
1934 | fc01f7e7 | bellard | #define WIN_WRITE_ONCE 0x31 /* 28-Bit without retries */ |
1935 | fc01f7e7 | bellard | #define WIN_WRITE_LONG 0x32 /* 28-Bit */ |
1936 | fc01f7e7 | bellard | #define WIN_WRITE_LONG_ONCE 0x33 /* 28-Bit without retries */ |
1937 | fc01f7e7 | bellard | #define WIN_WRITE_EXT 0x34 /* 48-Bit */ |
1938 | fc01f7e7 | bellard | #define WIN_WRITEDMA_EXT 0x35 /* 48-Bit */ |
1939 | fc01f7e7 | bellard | #define WIN_WRITEDMA_QUEUED_EXT 0x36 /* 48-Bit */ |
1940 | fc01f7e7 | bellard | #define WIN_SET_MAX_EXT 0x37 /* 48-Bit */ |
1941 | fc01f7e7 | bellard | #define CFA_WRITE_SECT_WO_ERASE 0x38 /* CFA Write Sectors without erase */ |
1942 | fc01f7e7 | bellard | #define WIN_MULTWRITE_EXT 0x39 /* 48-Bit */ |
1943 | fc01f7e7 | bellard | /*
|
1944 | fc01f7e7 | bellard | * 0x3A->0x3B Reserved
|
1945 | fc01f7e7 | bellard | */
|
1946 | fc01f7e7 | bellard | #define WIN_WRITE_VERIFY 0x3C /* 28-Bit */ |
1947 | fc01f7e7 | bellard | /*
|
1948 | fc01f7e7 | bellard | * 0x3D->0x3F Reserved
|
1949 | fc01f7e7 | bellard | */
|
1950 | fc01f7e7 | bellard | #define WIN_VERIFY 0x40 /* 28-Bit - Read Verify Sectors */ |
1951 | fc01f7e7 | bellard | #define WIN_VERIFY_ONCE 0x41 /* 28-Bit - without retries */ |
1952 | fc01f7e7 | bellard | #define WIN_VERIFY_EXT 0x42 /* 48-Bit */ |
1953 | fc01f7e7 | bellard | /*
|
1954 | fc01f7e7 | bellard | * 0x43->0x4F Reserved
|
1955 | fc01f7e7 | bellard | */
|
1956 | fc01f7e7 | bellard | #define WIN_FORMAT 0x50 |
1957 | fc01f7e7 | bellard | /*
|
1958 | fc01f7e7 | bellard | * 0x51->0x5F Reserved
|
1959 | fc01f7e7 | bellard | */
|
1960 | fc01f7e7 | bellard | #define WIN_INIT 0x60 |
1961 | fc01f7e7 | bellard | /*
|
1962 | fc01f7e7 | bellard | * 0x61->0x5F Reserved
|
1963 | fc01f7e7 | bellard | */
|
1964 | fc01f7e7 | bellard | #define WIN_SEEK 0x70 /* 0x70-0x7F Reserved */ |
1965 | fc01f7e7 | bellard | #define CFA_TRANSLATE_SECTOR 0x87 /* CFA Translate Sector */ |
1966 | fc01f7e7 | bellard | #define WIN_DIAGNOSE 0x90 |
1967 | fc01f7e7 | bellard | #define WIN_SPECIFY 0x91 /* set drive geometry translation */ |
1968 | fc01f7e7 | bellard | #define WIN_DOWNLOAD_MICROCODE 0x92 |
1969 | fc01f7e7 | bellard | #define WIN_STANDBYNOW2 0x94 |
1970 | fc01f7e7 | bellard | #define WIN_STANDBY2 0x96 |
1971 | fc01f7e7 | bellard | #define WIN_SETIDLE2 0x97 |
1972 | fc01f7e7 | bellard | #define WIN_CHECKPOWERMODE2 0x98 |
1973 | fc01f7e7 | bellard | #define WIN_SLEEPNOW2 0x99 |
1974 | fc01f7e7 | bellard | /*
|
1975 | fc01f7e7 | bellard | * 0x9A VENDOR
|
1976 | fc01f7e7 | bellard | */
|
1977 | fc01f7e7 | bellard | #define WIN_PACKETCMD 0xA0 /* Send a packet command. */ |
1978 | fc01f7e7 | bellard | #define WIN_PIDENTIFY 0xA1 /* identify ATAPI device */ |
1979 | fc01f7e7 | bellard | #define WIN_QUEUED_SERVICE 0xA2 |
1980 | fc01f7e7 | bellard | #define WIN_SMART 0xB0 /* self-monitoring and reporting */ |
1981 | fc01f7e7 | bellard | #define CFA_ERASE_SECTORS 0xC0 |
1982 | fc01f7e7 | bellard | #define WIN_MULTREAD 0xC4 /* read sectors using multiple mode*/ |
1983 | fc01f7e7 | bellard | #define WIN_MULTWRITE 0xC5 /* write sectors using multiple mode */ |
1984 | fc01f7e7 | bellard | #define WIN_SETMULT 0xC6 /* enable/disable multiple mode */ |
1985 | fc01f7e7 | bellard | #define WIN_READDMA_QUEUED 0xC7 /* read sectors using Queued DMA transfers */ |
1986 | fc01f7e7 | bellard | #define WIN_READDMA 0xC8 /* read sectors using DMA transfers */ |
1987 | fc01f7e7 | bellard | #define WIN_READDMA_ONCE 0xC9 /* 28-Bit - without retries */ |
1988 | fc01f7e7 | bellard | #define WIN_WRITEDMA 0xCA /* write sectors using DMA transfers */ |
1989 | fc01f7e7 | bellard | #define WIN_WRITEDMA_ONCE 0xCB /* 28-Bit - without retries */ |
1990 | fc01f7e7 | bellard | #define WIN_WRITEDMA_QUEUED 0xCC /* write sectors using Queued DMA transfers */ |
1991 | fc01f7e7 | bellard | #define CFA_WRITE_MULTI_WO_ERASE 0xCD /* CFA Write multiple without erase */ |
1992 | fc01f7e7 | bellard | #define WIN_GETMEDIASTATUS 0xDA |
1993 | fc01f7e7 | bellard | #define WIN_ACKMEDIACHANGE 0xDB /* ATA-1, ATA-2 vendor */ |
1994 | fc01f7e7 | bellard | #define WIN_POSTBOOT 0xDC |
1995 | fc01f7e7 | bellard | #define WIN_PREBOOT 0xDD |
1996 | fc01f7e7 | bellard | #define WIN_DOORLOCK 0xDE /* lock door on removable drives */ |
1997 | fc01f7e7 | bellard | #define WIN_DOORUNLOCK 0xDF /* unlock door on removable drives */ |
1998 | fc01f7e7 | bellard | #define WIN_STANDBYNOW1 0xE0 |
1999 | fc01f7e7 | bellard | #define WIN_IDLEIMMEDIATE 0xE1 /* force drive to become "ready" */ |
2000 | fc01f7e7 | bellard | #define WIN_STANDBY 0xE2 /* Set device in Standby Mode */ |
2001 | fc01f7e7 | bellard | #define WIN_SETIDLE1 0xE3 |
2002 | fc01f7e7 | bellard | #define WIN_READ_BUFFER 0xE4 /* force read only 1 sector */ |
2003 | fc01f7e7 | bellard | #define WIN_CHECKPOWERMODE1 0xE5 |
2004 | fc01f7e7 | bellard | #define WIN_SLEEPNOW1 0xE6 |
2005 | fc01f7e7 | bellard | #define WIN_FLUSH_CACHE 0xE7 |
2006 | fc01f7e7 | bellard | #define WIN_WRITE_BUFFER 0xE8 /* force write only 1 sector */ |
2007 | fc01f7e7 | bellard | #define WIN_WRITE_SAME 0xE9 /* read ata-2 to use */ |
2008 | fc01f7e7 | bellard | /* SET_FEATURES 0x22 or 0xDD */
|
2009 | fc01f7e7 | bellard | #define WIN_FLUSH_CACHE_EXT 0xEA /* 48-Bit */ |
2010 | fc01f7e7 | bellard | #define WIN_IDENTIFY 0xEC /* ask drive to identify itself */ |
2011 | fc01f7e7 | bellard | #define WIN_MEDIAEJECT 0xED |
2012 | fc01f7e7 | bellard | #define WIN_IDENTIFY_DMA 0xEE /* same as WIN_IDENTIFY, but DMA */ |
2013 | fc01f7e7 | bellard | #define WIN_SETFEATURES 0xEF /* set special drive features */ |
2014 | fc01f7e7 | bellard | #define EXABYTE_ENABLE_NEST 0xF0 |
2015 | fc01f7e7 | bellard | #define WIN_SECURITY_SET_PASS 0xF1 |
2016 | fc01f7e7 | bellard | #define WIN_SECURITY_UNLOCK 0xF2 |
2017 | fc01f7e7 | bellard | #define WIN_SECURITY_ERASE_PREPARE 0xF3 |
2018 | fc01f7e7 | bellard | #define WIN_SECURITY_ERASE_UNIT 0xF4 |
2019 | fc01f7e7 | bellard | #define WIN_SECURITY_FREEZE_LOCK 0xF5 |
2020 | fc01f7e7 | bellard | #define WIN_SECURITY_DISABLE 0xF6 |
2021 | fc01f7e7 | bellard | #define WIN_READ_NATIVE_MAX 0xF8 /* return the native maximum address */ |
2022 | fc01f7e7 | bellard | #define WIN_SET_MAX 0xF9 |
2023 | fc01f7e7 | bellard | #define DISABLE_SEAGATE 0xFB |
2024 | fc01f7e7 | bellard | |
2025 | c9159e53 | bellard | /* set to 1 set disable mult support */
|
2026 | c9159e53 | bellard | #define MAX_MULT_SECTORS 8 |
2027 | fc01f7e7 | bellard | |
2028 | fc01f7e7 | bellard | struct IDEState;
|
2029 | fc01f7e7 | bellard | |
2030 | fc01f7e7 | bellard | typedef void EndTransferFunc(struct IDEState *); |
2031 | fc01f7e7 | bellard | |
2032 | fc01f7e7 | bellard | typedef struct IDEState { |
2033 | fc01f7e7 | bellard | /* ide config */
|
2034 | fc01f7e7 | bellard | int cylinders, heads, sectors;
|
2035 | fc01f7e7 | bellard | int64_t nb_sectors; |
2036 | fc01f7e7 | bellard | int mult_sectors;
|
2037 | fc01f7e7 | bellard | int irq;
|
2038 | fc01f7e7 | bellard | /* ide regs */
|
2039 | fc01f7e7 | bellard | uint8_t feature; |
2040 | fc01f7e7 | bellard | uint8_t error; |
2041 | c9159e53 | bellard | uint16_t nsector; /* 0 is 256 to ease computations */
|
2042 | fc01f7e7 | bellard | uint8_t sector; |
2043 | fc01f7e7 | bellard | uint8_t lcyl; |
2044 | fc01f7e7 | bellard | uint8_t hcyl; |
2045 | fc01f7e7 | bellard | uint8_t select; |
2046 | fc01f7e7 | bellard | uint8_t status; |
2047 | fc01f7e7 | bellard | /* 0x3f6 command, only meaningful for drive 0 */
|
2048 | fc01f7e7 | bellard | uint8_t cmd; |
2049 | fc01f7e7 | bellard | /* depends on bit 4 in select, only meaningful for drive 0 */
|
2050 | fc01f7e7 | bellard | struct IDEState *cur_drive;
|
2051 | fc01f7e7 | bellard | BlockDriverState *bs; |
2052 | c9159e53 | bellard | int req_nb_sectors; /* number of sectors per interrupt */ |
2053 | fc01f7e7 | bellard | EndTransferFunc *end_transfer_func; |
2054 | fc01f7e7 | bellard | uint8_t *data_ptr; |
2055 | fc01f7e7 | bellard | uint8_t *data_end; |
2056 | fc01f7e7 | bellard | uint8_t io_buffer[MAX_MULT_SECTORS*512 + 4]; |
2057 | fc01f7e7 | bellard | } IDEState; |
2058 | fc01f7e7 | bellard | |
2059 | fc01f7e7 | bellard | IDEState ide_state[MAX_DISKS]; |
2060 | fc01f7e7 | bellard | |
2061 | fc01f7e7 | bellard | static void padstr(char *str, const char *src, int len) |
2062 | fc01f7e7 | bellard | { |
2063 | fc01f7e7 | bellard | int i, v;
|
2064 | fc01f7e7 | bellard | for(i = 0; i < len; i++) { |
2065 | fc01f7e7 | bellard | if (*src)
|
2066 | fc01f7e7 | bellard | v = *src++; |
2067 | fc01f7e7 | bellard | else
|
2068 | fc01f7e7 | bellard | v = ' ';
|
2069 | fc01f7e7 | bellard | *(char *)((long)str ^ 1) = v; |
2070 | fc01f7e7 | bellard | str++; |
2071 | fc01f7e7 | bellard | } |
2072 | fc01f7e7 | bellard | } |
2073 | fc01f7e7 | bellard | |
2074 | fc01f7e7 | bellard | static void ide_identify(IDEState *s) |
2075 | fc01f7e7 | bellard | { |
2076 | fc01f7e7 | bellard | uint16_t *p; |
2077 | fc01f7e7 | bellard | unsigned int oldsize; |
2078 | fc01f7e7 | bellard | |
2079 | fc01f7e7 | bellard | memset(s->io_buffer, 0, 512); |
2080 | fc01f7e7 | bellard | p = (uint16_t *)s->io_buffer; |
2081 | fc01f7e7 | bellard | stw(p + 0, 0x0040); |
2082 | fc01f7e7 | bellard | stw(p + 1, s->cylinders);
|
2083 | fc01f7e7 | bellard | stw(p + 3, s->heads);
|
2084 | fc01f7e7 | bellard | stw(p + 4, 512 * s->sectors); /* sectors */ |
2085 | fc01f7e7 | bellard | stw(p + 5, 512); /* sector size */ |
2086 | fc01f7e7 | bellard | stw(p + 6, s->sectors);
|
2087 | fc01f7e7 | bellard | stw(p + 20, 3); /* buffer type */ |
2088 | fc01f7e7 | bellard | stw(p + 21, 512); /* cache size in sectors */ |
2089 | fc01f7e7 | bellard | stw(p + 22, 4); /* ecc bytes */ |
2090 | fc01f7e7 | bellard | padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40); |
2091 | c9159e53 | bellard | #if MAX_MULT_SECTORS > 1 |
2092 | c9159e53 | bellard | stw(p + 47, MAX_MULT_SECTORS);
|
2093 | c9159e53 | bellard | #endif
|
2094 | fc01f7e7 | bellard | stw(p + 48, 1); /* dword I/O */ |
2095 | fc01f7e7 | bellard | stw(p + 49, 1 << 9); /* LBA supported, no DMA */ |
2096 | fc01f7e7 | bellard | stw(p + 51, 0x200); /* PIO transfer cycle */ |
2097 | fc01f7e7 | bellard | stw(p + 52, 0x200); /* DMA transfer cycle */ |
2098 | fc01f7e7 | bellard | stw(p + 54, s->cylinders);
|
2099 | fc01f7e7 | bellard | stw(p + 55, s->heads);
|
2100 | fc01f7e7 | bellard | stw(p + 56, s->sectors);
|
2101 | fc01f7e7 | bellard | oldsize = s->cylinders * s->heads * s->sectors; |
2102 | fc01f7e7 | bellard | stw(p + 57, oldsize);
|
2103 | fc01f7e7 | bellard | stw(p + 58, oldsize >> 16); |
2104 | fc01f7e7 | bellard | if (s->mult_sectors)
|
2105 | fc01f7e7 | bellard | stw(p + 59, 0x100 | s->mult_sectors); |
2106 | fc01f7e7 | bellard | stw(p + 60, s->nb_sectors);
|
2107 | fc01f7e7 | bellard | stw(p + 61, s->nb_sectors >> 16); |
2108 | fc01f7e7 | bellard | stw(p + 80, (1 << 1) | (1 << 2)); |
2109 | fc01f7e7 | bellard | stw(p + 82, (1 << 14)); |
2110 | fc01f7e7 | bellard | stw(p + 83, (1 << 14)); |
2111 | fc01f7e7 | bellard | stw(p + 84, (1 << 14)); |
2112 | fc01f7e7 | bellard | stw(p + 85, (1 << 14)); |
2113 | fc01f7e7 | bellard | stw(p + 86, 0); |
2114 | fc01f7e7 | bellard | stw(p + 87, (1 << 14)); |
2115 | fc01f7e7 | bellard | } |
2116 | fc01f7e7 | bellard | |
2117 | fc01f7e7 | bellard | static inline void ide_abort_command(IDEState *s) |
2118 | fc01f7e7 | bellard | { |
2119 | fc01f7e7 | bellard | s->status = READY_STAT | ERR_STAT; |
2120 | fc01f7e7 | bellard | s->error = ABRT_ERR; |
2121 | fc01f7e7 | bellard | } |
2122 | fc01f7e7 | bellard | |
2123 | fc01f7e7 | bellard | static inline void ide_set_irq(IDEState *s) |
2124 | fc01f7e7 | bellard | { |
2125 | fc01f7e7 | bellard | if (!(ide_state[0].cmd & IDE_CMD_DISABLE_IRQ)) { |
2126 | fc01f7e7 | bellard | pic_set_irq(s->irq, 1);
|
2127 | fc01f7e7 | bellard | } |
2128 | fc01f7e7 | bellard | } |
2129 | fc01f7e7 | bellard | |
2130 | fc01f7e7 | bellard | /* prepare data transfer and tell what to do after */
|
2131 | fc01f7e7 | bellard | static void ide_transfer_start(IDEState *s, int size, |
2132 | fc01f7e7 | bellard | EndTransferFunc *end_transfer_func) |
2133 | fc01f7e7 | bellard | { |
2134 | fc01f7e7 | bellard | s->end_transfer_func = end_transfer_func; |
2135 | fc01f7e7 | bellard | s->data_ptr = s->io_buffer; |
2136 | fc01f7e7 | bellard | s->data_end = s->io_buffer + size; |
2137 | fc01f7e7 | bellard | s->status |= DRQ_STAT; |
2138 | fc01f7e7 | bellard | } |
2139 | fc01f7e7 | bellard | |
2140 | fc01f7e7 | bellard | static void ide_transfer_stop(IDEState *s) |
2141 | fc01f7e7 | bellard | { |
2142 | fc01f7e7 | bellard | s->end_transfer_func = ide_transfer_stop; |
2143 | fc01f7e7 | bellard | s->data_ptr = s->io_buffer; |
2144 | fc01f7e7 | bellard | s->data_end = s->io_buffer; |
2145 | fc01f7e7 | bellard | s->status &= ~DRQ_STAT; |
2146 | fc01f7e7 | bellard | } |
2147 | fc01f7e7 | bellard | |
2148 | fc01f7e7 | bellard | static int64_t ide_get_sector(IDEState *s)
|
2149 | fc01f7e7 | bellard | { |
2150 | fc01f7e7 | bellard | int64_t sector_num; |
2151 | fc01f7e7 | bellard | if (s->select & 0x40) { |
2152 | fc01f7e7 | bellard | /* lba */
|
2153 | fc01f7e7 | bellard | sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) | |
2154 | fc01f7e7 | bellard | (s->lcyl << 8) | s->sector;
|
2155 | fc01f7e7 | bellard | } else {
|
2156 | fc01f7e7 | bellard | sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
|
2157 | fc01f7e7 | bellard | (s->select & 0x0f) * s->sectors +
|
2158 | fc01f7e7 | bellard | (s->sector - 1);
|
2159 | fc01f7e7 | bellard | } |
2160 | fc01f7e7 | bellard | return sector_num;
|
2161 | fc01f7e7 | bellard | } |
2162 | fc01f7e7 | bellard | |
2163 | fc01f7e7 | bellard | static void ide_set_sector(IDEState *s, int64_t sector_num) |
2164 | fc01f7e7 | bellard | { |
2165 | fc01f7e7 | bellard | unsigned int cyl, r; |
2166 | fc01f7e7 | bellard | if (s->select & 0x40) { |
2167 | fc01f7e7 | bellard | s->select = (s->select & 0xf0) | (sector_num >> 24); |
2168 | fc01f7e7 | bellard | s->hcyl = (sector_num >> 16);
|
2169 | fc01f7e7 | bellard | s->lcyl = (sector_num >> 8);
|
2170 | fc01f7e7 | bellard | s->sector = (sector_num); |
2171 | fc01f7e7 | bellard | } else {
|
2172 | fc01f7e7 | bellard | cyl = sector_num / (s->heads * s->sectors); |
2173 | fc01f7e7 | bellard | r = sector_num % (s->heads * s->sectors); |
2174 | fc01f7e7 | bellard | s->hcyl = cyl >> 8;
|
2175 | fc01f7e7 | bellard | s->lcyl = cyl; |
2176 | fc01f7e7 | bellard | s->select = (s->select & 0xf0) | (r / s->sectors);
|
2177 | fc01f7e7 | bellard | s->sector = (r % s->sectors) + 1;
|
2178 | fc01f7e7 | bellard | } |
2179 | fc01f7e7 | bellard | } |
2180 | fc01f7e7 | bellard | |
2181 | fc01f7e7 | bellard | static void ide_sector_read(IDEState *s) |
2182 | fc01f7e7 | bellard | { |
2183 | fc01f7e7 | bellard | int64_t sector_num; |
2184 | c9159e53 | bellard | int ret, n;
|
2185 | fc01f7e7 | bellard | |
2186 | fc01f7e7 | bellard | s->status = READY_STAT | SEEK_STAT; |
2187 | fc01f7e7 | bellard | sector_num = ide_get_sector(s); |
2188 | c9159e53 | bellard | n = s->nsector; |
2189 | c9159e53 | bellard | if (n == 0) { |
2190 | fc01f7e7 | bellard | /* no more sector to read from disk */
|
2191 | fc01f7e7 | bellard | ide_transfer_stop(s); |
2192 | fc01f7e7 | bellard | } else {
|
2193 | fc01f7e7 | bellard | #if defined(DEBUG_IDE)
|
2194 | fc01f7e7 | bellard | printf("read sector=%Ld\n", sector_num);
|
2195 | fc01f7e7 | bellard | #endif
|
2196 | c9159e53 | bellard | if (n > s->req_nb_sectors)
|
2197 | c9159e53 | bellard | n = s->req_nb_sectors; |
2198 | c9159e53 | bellard | ret = bdrv_read(s->bs, sector_num, s->io_buffer, n); |
2199 | c9159e53 | bellard | ide_transfer_start(s, 512 * n, ide_sector_read);
|
2200 | fc01f7e7 | bellard | ide_set_irq(s); |
2201 | c9159e53 | bellard | ide_set_sector(s, sector_num + n); |
2202 | c9159e53 | bellard | s->nsector -= n; |
2203 | fc01f7e7 | bellard | } |
2204 | fc01f7e7 | bellard | } |
2205 | fc01f7e7 | bellard | |
2206 | fc01f7e7 | bellard | static void ide_sector_write(IDEState *s) |
2207 | fc01f7e7 | bellard | { |
2208 | fc01f7e7 | bellard | int64_t sector_num; |
2209 | c9159e53 | bellard | int ret, n, n1;
|
2210 | fc01f7e7 | bellard | |
2211 | fc01f7e7 | bellard | s->status = READY_STAT | SEEK_STAT; |
2212 | fc01f7e7 | bellard | sector_num = ide_get_sector(s); |
2213 | fc01f7e7 | bellard | #if defined(DEBUG_IDE)
|
2214 | fc01f7e7 | bellard | printf("write sector=%Ld\n", sector_num);
|
2215 | fc01f7e7 | bellard | #endif
|
2216 | c9159e53 | bellard | n = s->nsector; |
2217 | c9159e53 | bellard | if (n > s->req_nb_sectors)
|
2218 | c9159e53 | bellard | n = s->req_nb_sectors; |
2219 | c9159e53 | bellard | ret = bdrv_write(s->bs, sector_num, s->io_buffer, n); |
2220 | c9159e53 | bellard | s->nsector -= n; |
2221 | fc01f7e7 | bellard | if (s->nsector == 0) { |
2222 | fc01f7e7 | bellard | /* no more sector to write */
|
2223 | fc01f7e7 | bellard | ide_transfer_stop(s); |
2224 | fc01f7e7 | bellard | } else {
|
2225 | c9159e53 | bellard | n1 = s->nsector; |
2226 | c9159e53 | bellard | if (n1 > s->req_nb_sectors)
|
2227 | c9159e53 | bellard | n1 = s->req_nb_sectors; |
2228 | c9159e53 | bellard | ide_transfer_start(s, 512 * n1, ide_sector_write);
|
2229 | fc01f7e7 | bellard | } |
2230 | c9159e53 | bellard | ide_set_sector(s, sector_num + n); |
2231 | fc01f7e7 | bellard | ide_set_irq(s); |
2232 | fc01f7e7 | bellard | } |
2233 | fc01f7e7 | bellard | |
2234 | fc01f7e7 | bellard | void ide_ioport_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
2235 | fc01f7e7 | bellard | { |
2236 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2237 | c9159e53 | bellard | int unit, n;
|
2238 | fc01f7e7 | bellard | |
2239 | fc01f7e7 | bellard | addr &= 7;
|
2240 | fc01f7e7 | bellard | #ifdef DEBUG_IDE
|
2241 | fc01f7e7 | bellard | printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
|
2242 | fc01f7e7 | bellard | #endif
|
2243 | fc01f7e7 | bellard | switch(addr) {
|
2244 | fc01f7e7 | bellard | case 0: |
2245 | fc01f7e7 | bellard | break;
|
2246 | fc01f7e7 | bellard | case 1: |
2247 | fc01f7e7 | bellard | s->feature = val; |
2248 | fc01f7e7 | bellard | break;
|
2249 | fc01f7e7 | bellard | case 2: |
2250 | c9159e53 | bellard | if (val == 0) |
2251 | c9159e53 | bellard | val = 256;
|
2252 | fc01f7e7 | bellard | s->nsector = val; |
2253 | fc01f7e7 | bellard | break;
|
2254 | fc01f7e7 | bellard | case 3: |
2255 | fc01f7e7 | bellard | s->sector = val; |
2256 | fc01f7e7 | bellard | break;
|
2257 | fc01f7e7 | bellard | case 4: |
2258 | fc01f7e7 | bellard | s->lcyl = val; |
2259 | fc01f7e7 | bellard | break;
|
2260 | fc01f7e7 | bellard | case 5: |
2261 | fc01f7e7 | bellard | s->hcyl = val; |
2262 | fc01f7e7 | bellard | break;
|
2263 | fc01f7e7 | bellard | case 6: |
2264 | fc01f7e7 | bellard | /* select drive */
|
2265 | fc01f7e7 | bellard | unit = (val >> 4) & 1; |
2266 | fc01f7e7 | bellard | s = &ide_state[unit]; |
2267 | fc01f7e7 | bellard | ide_state[0].cur_drive = s;
|
2268 | fc01f7e7 | bellard | s->select = val; |
2269 | fc01f7e7 | bellard | break;
|
2270 | fc01f7e7 | bellard | default:
|
2271 | fc01f7e7 | bellard | case 7: |
2272 | fc01f7e7 | bellard | /* command */
|
2273 | fc01f7e7 | bellard | #if defined(DEBUG_IDE)
|
2274 | fc01f7e7 | bellard | printf("ide: CMD=%02x\n", val);
|
2275 | fc01f7e7 | bellard | #endif
|
2276 | fc01f7e7 | bellard | switch(val) {
|
2277 | fc01f7e7 | bellard | case WIN_PIDENTIFY:
|
2278 | fc01f7e7 | bellard | case WIN_IDENTIFY:
|
2279 | fc01f7e7 | bellard | if (s->bs) {
|
2280 | fc01f7e7 | bellard | ide_identify(s); |
2281 | fc01f7e7 | bellard | s->status = READY_STAT; |
2282 | fc01f7e7 | bellard | ide_transfer_start(s, 512, ide_transfer_stop);
|
2283 | fc01f7e7 | bellard | } else {
|
2284 | fc01f7e7 | bellard | ide_abort_command(s); |
2285 | fc01f7e7 | bellard | } |
2286 | fc01f7e7 | bellard | ide_set_irq(s); |
2287 | fc01f7e7 | bellard | break;
|
2288 | fc01f7e7 | bellard | case WIN_SPECIFY:
|
2289 | fc01f7e7 | bellard | case WIN_RECAL:
|
2290 | fc01f7e7 | bellard | s->status = READY_STAT; |
2291 | fc01f7e7 | bellard | ide_set_irq(s); |
2292 | fc01f7e7 | bellard | break;
|
2293 | fc01f7e7 | bellard | case WIN_SETMULT:
|
2294 | fc01f7e7 | bellard | if (s->nsector > MAX_MULT_SECTORS ||
|
2295 | fc01f7e7 | bellard | s->nsector == 0 ||
|
2296 | fc01f7e7 | bellard | (s->nsector & (s->nsector - 1)) != 0) { |
2297 | fc01f7e7 | bellard | ide_abort_command(s); |
2298 | fc01f7e7 | bellard | } else {
|
2299 | fc01f7e7 | bellard | s->mult_sectors = s->nsector; |
2300 | fc01f7e7 | bellard | s->status = READY_STAT; |
2301 | fc01f7e7 | bellard | } |
2302 | fc01f7e7 | bellard | ide_set_irq(s); |
2303 | fc01f7e7 | bellard | break;
|
2304 | fc01f7e7 | bellard | case WIN_READ:
|
2305 | fc01f7e7 | bellard | case WIN_READ_ONCE:
|
2306 | c9159e53 | bellard | s->req_nb_sectors = 1;
|
2307 | fc01f7e7 | bellard | ide_sector_read(s); |
2308 | fc01f7e7 | bellard | break;
|
2309 | fc01f7e7 | bellard | case WIN_WRITE:
|
2310 | fc01f7e7 | bellard | case WIN_WRITE_ONCE:
|
2311 | fc01f7e7 | bellard | s->status = SEEK_STAT; |
2312 | c9159e53 | bellard | s->req_nb_sectors = 1;
|
2313 | fc01f7e7 | bellard | ide_transfer_start(s, 512, ide_sector_write);
|
2314 | fc01f7e7 | bellard | break;
|
2315 | c9159e53 | bellard | case WIN_MULTREAD:
|
2316 | c9159e53 | bellard | if (!s->mult_sectors)
|
2317 | c9159e53 | bellard | goto abort_cmd;
|
2318 | c9159e53 | bellard | s->req_nb_sectors = s->mult_sectors; |
2319 | c9159e53 | bellard | ide_sector_read(s); |
2320 | c9159e53 | bellard | break;
|
2321 | c9159e53 | bellard | case WIN_MULTWRITE:
|
2322 | c9159e53 | bellard | if (!s->mult_sectors)
|
2323 | c9159e53 | bellard | goto abort_cmd;
|
2324 | c9159e53 | bellard | s->status = SEEK_STAT; |
2325 | c9159e53 | bellard | s->req_nb_sectors = s->mult_sectors; |
2326 | c9159e53 | bellard | n = s->nsector; |
2327 | c9159e53 | bellard | if (n > s->req_nb_sectors)
|
2328 | c9159e53 | bellard | n = s->req_nb_sectors; |
2329 | c9159e53 | bellard | ide_transfer_start(s, 512 * n, ide_sector_write);
|
2330 | c9159e53 | bellard | break;
|
2331 | cd4c3e88 | bellard | case WIN_READ_NATIVE_MAX:
|
2332 | cd4c3e88 | bellard | ide_set_sector(s, s->nb_sectors - 1);
|
2333 | cd4c3e88 | bellard | s->status = READY_STAT; |
2334 | cd4c3e88 | bellard | ide_set_irq(s); |
2335 | cd4c3e88 | bellard | break;
|
2336 | fc01f7e7 | bellard | default:
|
2337 | c9159e53 | bellard | abort_cmd:
|
2338 | fc01f7e7 | bellard | ide_abort_command(s); |
2339 | fc01f7e7 | bellard | ide_set_irq(s); |
2340 | fc01f7e7 | bellard | break;
|
2341 | fc01f7e7 | bellard | } |
2342 | fc01f7e7 | bellard | } |
2343 | fc01f7e7 | bellard | } |
2344 | fc01f7e7 | bellard | |
2345 | fc01f7e7 | bellard | uint32_t ide_ioport_read(CPUX86State *env, uint32_t addr) |
2346 | fc01f7e7 | bellard | { |
2347 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2348 | fc01f7e7 | bellard | int ret;
|
2349 | fc01f7e7 | bellard | |
2350 | fc01f7e7 | bellard | addr &= 7;
|
2351 | fc01f7e7 | bellard | switch(addr) {
|
2352 | fc01f7e7 | bellard | case 0: |
2353 | fc01f7e7 | bellard | ret = 0xff;
|
2354 | fc01f7e7 | bellard | break;
|
2355 | fc01f7e7 | bellard | case 1: |
2356 | fc01f7e7 | bellard | ret = s->error; |
2357 | fc01f7e7 | bellard | break;
|
2358 | fc01f7e7 | bellard | case 2: |
2359 | c9159e53 | bellard | ret = s->nsector & 0xff;
|
2360 | fc01f7e7 | bellard | break;
|
2361 | fc01f7e7 | bellard | case 3: |
2362 | fc01f7e7 | bellard | ret = s->sector; |
2363 | fc01f7e7 | bellard | break;
|
2364 | fc01f7e7 | bellard | case 4: |
2365 | fc01f7e7 | bellard | ret = s->lcyl; |
2366 | fc01f7e7 | bellard | break;
|
2367 | fc01f7e7 | bellard | case 5: |
2368 | fc01f7e7 | bellard | ret = s->hcyl; |
2369 | fc01f7e7 | bellard | break;
|
2370 | fc01f7e7 | bellard | case 6: |
2371 | fc01f7e7 | bellard | ret = s->select; |
2372 | fc01f7e7 | bellard | break;
|
2373 | fc01f7e7 | bellard | default:
|
2374 | fc01f7e7 | bellard | case 7: |
2375 | fc01f7e7 | bellard | ret = s->status; |
2376 | fc01f7e7 | bellard | pic_set_irq(s->irq, 0);
|
2377 | fc01f7e7 | bellard | break;
|
2378 | fc01f7e7 | bellard | } |
2379 | fc01f7e7 | bellard | #ifdef DEBUG_IDE
|
2380 | fc01f7e7 | bellard | printf("ide: read addr=0x%x val=%02x\n", addr, ret);
|
2381 | fc01f7e7 | bellard | #endif
|
2382 | fc01f7e7 | bellard | return ret;
|
2383 | fc01f7e7 | bellard | } |
2384 | fc01f7e7 | bellard | |
2385 | fc01f7e7 | bellard | uint32_t ide_status_read(CPUX86State *env, uint32_t addr) |
2386 | fc01f7e7 | bellard | { |
2387 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2388 | fc01f7e7 | bellard | int ret;
|
2389 | fc01f7e7 | bellard | ret = s->status; |
2390 | fc01f7e7 | bellard | #ifdef DEBUG_IDE
|
2391 | 330d0414 | bellard | printf("ide: read status val=%02x\n", ret);
|
2392 | fc01f7e7 | bellard | #endif
|
2393 | fc01f7e7 | bellard | return ret;
|
2394 | fc01f7e7 | bellard | } |
2395 | fc01f7e7 | bellard | |
2396 | fc01f7e7 | bellard | void ide_cmd_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
2397 | fc01f7e7 | bellard | { |
2398 | 330d0414 | bellard | IDEState *s; |
2399 | 330d0414 | bellard | int i;
|
2400 | 330d0414 | bellard | |
2401 | 330d0414 | bellard | #ifdef DEBUG_IDE
|
2402 | 330d0414 | bellard | printf("ide: write control val=%02x\n", val);
|
2403 | 330d0414 | bellard | #endif
|
2404 | fc01f7e7 | bellard | /* common for both drives */
|
2405 | 330d0414 | bellard | if (!(ide_state[0].cmd & IDE_CMD_RESET) && |
2406 | 330d0414 | bellard | (val & IDE_CMD_RESET)) { |
2407 | 330d0414 | bellard | /* reset low to high */
|
2408 | 330d0414 | bellard | for(i = 0;i < 2; i++) { |
2409 | 330d0414 | bellard | s = &ide_state[i]; |
2410 | 330d0414 | bellard | s->status = BUSY_STAT | SEEK_STAT; |
2411 | 330d0414 | bellard | s->error = 0x01;
|
2412 | 330d0414 | bellard | } |
2413 | 330d0414 | bellard | } else if ((ide_state[0].cmd & IDE_CMD_RESET) && |
2414 | 330d0414 | bellard | !(val & IDE_CMD_RESET)) { |
2415 | 330d0414 | bellard | /* high to low */
|
2416 | 330d0414 | bellard | for(i = 0;i < 2; i++) { |
2417 | 330d0414 | bellard | s = &ide_state[i]; |
2418 | 330d0414 | bellard | s->status = READY_STAT; |
2419 | 330d0414 | bellard | /* set hard disk drive ID */
|
2420 | 330d0414 | bellard | s->select &= 0xf0; /* clear head */ |
2421 | 330d0414 | bellard | s->nsector = 1;
|
2422 | 330d0414 | bellard | s->sector = 1;
|
2423 | 330d0414 | bellard | if (s->nb_sectors == 0) { |
2424 | 330d0414 | bellard | /* no disk present */
|
2425 | 330d0414 | bellard | s->lcyl = 0x12;
|
2426 | 330d0414 | bellard | s->hcyl = 0x34;
|
2427 | 330d0414 | bellard | } else {
|
2428 | 330d0414 | bellard | s->lcyl = 0;
|
2429 | 330d0414 | bellard | s->hcyl = 0;
|
2430 | 330d0414 | bellard | } |
2431 | 330d0414 | bellard | } |
2432 | 330d0414 | bellard | } |
2433 | 330d0414 | bellard | |
2434 | 330d0414 | bellard | ide_state[0].cmd = val;
|
2435 | fc01f7e7 | bellard | } |
2436 | fc01f7e7 | bellard | |
2437 | fc01f7e7 | bellard | void ide_data_writew(CPUX86State *env, uint32_t addr, uint32_t val)
|
2438 | fc01f7e7 | bellard | { |
2439 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2440 | fc01f7e7 | bellard | uint8_t *p; |
2441 | fc01f7e7 | bellard | |
2442 | fc01f7e7 | bellard | p = s->data_ptr; |
2443 | fc01f7e7 | bellard | *(uint16_t *)p = tswap16(val); |
2444 | fc01f7e7 | bellard | p += 2;
|
2445 | fc01f7e7 | bellard | s->data_ptr = p; |
2446 | fc01f7e7 | bellard | if (p >= s->data_end)
|
2447 | fc01f7e7 | bellard | s->end_transfer_func(s); |
2448 | fc01f7e7 | bellard | } |
2449 | fc01f7e7 | bellard | |
2450 | fc01f7e7 | bellard | uint32_t ide_data_readw(CPUX86State *env, uint32_t addr) |
2451 | fc01f7e7 | bellard | { |
2452 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2453 | fc01f7e7 | bellard | uint8_t *p; |
2454 | fc01f7e7 | bellard | int ret;
|
2455 | fc01f7e7 | bellard | |
2456 | fc01f7e7 | bellard | p = s->data_ptr; |
2457 | fc01f7e7 | bellard | ret = tswap16(*(uint16_t *)p); |
2458 | fc01f7e7 | bellard | p += 2;
|
2459 | fc01f7e7 | bellard | s->data_ptr = p; |
2460 | fc01f7e7 | bellard | if (p >= s->data_end)
|
2461 | fc01f7e7 | bellard | s->end_transfer_func(s); |
2462 | fc01f7e7 | bellard | return ret;
|
2463 | fc01f7e7 | bellard | } |
2464 | fc01f7e7 | bellard | |
2465 | fc01f7e7 | bellard | void ide_data_writel(CPUX86State *env, uint32_t addr, uint32_t val)
|
2466 | fc01f7e7 | bellard | { |
2467 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2468 | fc01f7e7 | bellard | uint8_t *p; |
2469 | fc01f7e7 | bellard | |
2470 | fc01f7e7 | bellard | p = s->data_ptr; |
2471 | fc01f7e7 | bellard | *(uint32_t *)p = tswap32(val); |
2472 | fc01f7e7 | bellard | p += 4;
|
2473 | fc01f7e7 | bellard | s->data_ptr = p; |
2474 | fc01f7e7 | bellard | if (p >= s->data_end)
|
2475 | fc01f7e7 | bellard | s->end_transfer_func(s); |
2476 | fc01f7e7 | bellard | } |
2477 | fc01f7e7 | bellard | |
2478 | fc01f7e7 | bellard | uint32_t ide_data_readl(CPUX86State *env, uint32_t addr) |
2479 | fc01f7e7 | bellard | { |
2480 | fc01f7e7 | bellard | IDEState *s = ide_state[0].cur_drive;
|
2481 | fc01f7e7 | bellard | uint8_t *p; |
2482 | fc01f7e7 | bellard | int ret;
|
2483 | fc01f7e7 | bellard | |
2484 | fc01f7e7 | bellard | p = s->data_ptr; |
2485 | fc01f7e7 | bellard | ret = tswap32(*(uint32_t *)p); |
2486 | fc01f7e7 | bellard | p += 4;
|
2487 | fc01f7e7 | bellard | s->data_ptr = p; |
2488 | fc01f7e7 | bellard | if (p >= s->data_end)
|
2489 | fc01f7e7 | bellard | s->end_transfer_func(s); |
2490 | fc01f7e7 | bellard | return ret;
|
2491 | fc01f7e7 | bellard | } |
2492 | fc01f7e7 | bellard | |
2493 | fc01f7e7 | bellard | void ide_reset(IDEState *s)
|
2494 | fc01f7e7 | bellard | { |
2495 | fc01f7e7 | bellard | s->mult_sectors = MAX_MULT_SECTORS; |
2496 | fc01f7e7 | bellard | s->status = READY_STAT; |
2497 | fc01f7e7 | bellard | s->cur_drive = s; |
2498 | fc01f7e7 | bellard | s->select = 0xa0;
|
2499 | fc01f7e7 | bellard | } |
2500 | fc01f7e7 | bellard | |
2501 | fc01f7e7 | bellard | void ide_init(void) |
2502 | fc01f7e7 | bellard | { |
2503 | fc01f7e7 | bellard | IDEState *s; |
2504 | fc01f7e7 | bellard | int i, cylinders;
|
2505 | fc01f7e7 | bellard | int64_t nb_sectors; |
2506 | fc01f7e7 | bellard | |
2507 | fc01f7e7 | bellard | for(i = 0; i < MAX_DISKS; i++) { |
2508 | fc01f7e7 | bellard | s = &ide_state[i]; |
2509 | fc01f7e7 | bellard | s->bs = bs_table[i]; |
2510 | fc01f7e7 | bellard | if (s->bs) {
|
2511 | fc01f7e7 | bellard | bdrv_get_geometry(s->bs, &nb_sectors); |
2512 | 330d0414 | bellard | if (s->cylinders == 0) { |
2513 | 330d0414 | bellard | /* if no geometry, use a LBA compatible one */
|
2514 | 330d0414 | bellard | cylinders = nb_sectors / (16 * 63); |
2515 | 330d0414 | bellard | if (cylinders > 16383) |
2516 | 330d0414 | bellard | cylinders = 16383;
|
2517 | 330d0414 | bellard | else if (cylinders < 2) |
2518 | 330d0414 | bellard | cylinders = 2;
|
2519 | 330d0414 | bellard | s->cylinders = cylinders; |
2520 | 330d0414 | bellard | s->heads = 16;
|
2521 | 330d0414 | bellard | s->sectors = 63;
|
2522 | 330d0414 | bellard | } |
2523 | fc01f7e7 | bellard | s->nb_sectors = nb_sectors; |
2524 | fc01f7e7 | bellard | } |
2525 | fc01f7e7 | bellard | s->irq = 14;
|
2526 | fc01f7e7 | bellard | ide_reset(s); |
2527 | fc01f7e7 | bellard | } |
2528 | fc01f7e7 | bellard | register_ioport_write(0x1f0, 8, ide_ioport_write, 1); |
2529 | fc01f7e7 | bellard | register_ioport_read(0x1f0, 8, ide_ioport_read, 1); |
2530 | fc01f7e7 | bellard | register_ioport_read(0x3f6, 1, ide_status_read, 1); |
2531 | fc01f7e7 | bellard | register_ioport_write(0x3f6, 1, ide_cmd_write, 1); |
2532 | fc01f7e7 | bellard | |
2533 | fc01f7e7 | bellard | /* data ports */
|
2534 | fc01f7e7 | bellard | register_ioport_write(0x1f0, 2, ide_data_writew, 2); |
2535 | fc01f7e7 | bellard | register_ioport_read(0x1f0, 2, ide_data_readw, 2); |
2536 | fc01f7e7 | bellard | register_ioport_write(0x1f0, 4, ide_data_writel, 4); |
2537 | fc01f7e7 | bellard | register_ioport_read(0x1f0, 4, ide_data_readl, 4); |
2538 | fc01f7e7 | bellard | } |
2539 | fc01f7e7 | bellard | |
2540 | fc01f7e7 | bellard | /***********************************************************/
|
2541 | 330d0414 | bellard | /* keyboard emulation */
|
2542 | 330d0414 | bellard | |
2543 | 330d0414 | bellard | /* Keyboard Controller Commands */
|
2544 | 330d0414 | bellard | #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */ |
2545 | 330d0414 | bellard | #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */ |
2546 | 330d0414 | bellard | #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */ |
2547 | 330d0414 | bellard | #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */ |
2548 | 330d0414 | bellard | #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */ |
2549 | 330d0414 | bellard | #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */ |
2550 | 330d0414 | bellard | #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */ |
2551 | 330d0414 | bellard | #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */ |
2552 | 330d0414 | bellard | #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */ |
2553 | 330d0414 | bellard | #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */ |
2554 | 330d0414 | bellard | #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */ |
2555 | 330d0414 | bellard | #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */ |
2556 | 330d0414 | bellard | #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */ |
2557 | 330d0414 | bellard | #define KBD_CCMD_WRITE_OBUF 0xD2 |
2558 | 330d0414 | bellard | #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if |
2559 | 330d0414 | bellard | initiated by the auxiliary device */
|
2560 | 330d0414 | bellard | #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */ |
2561 | 330d0414 | bellard | #define KBD_CCMD_ENABLE_A20 0xDD |
2562 | 330d0414 | bellard | #define KBD_CCMD_DISABLE_A20 0xDF |
2563 | 330d0414 | bellard | #define KBD_CCMD_RESET 0xFE |
2564 | 330d0414 | bellard | |
2565 | 330d0414 | bellard | /* Keyboard Commands */
|
2566 | 330d0414 | bellard | #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */ |
2567 | 330d0414 | bellard | #define KBD_CMD_ECHO 0xEE |
2568 | 330d0414 | bellard | #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */ |
2569 | 330d0414 | bellard | #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */ |
2570 | 330d0414 | bellard | #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */ |
2571 | 330d0414 | bellard | #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */ |
2572 | 330d0414 | bellard | #define KBD_CMD_RESET 0xFF /* Reset */ |
2573 | 330d0414 | bellard | |
2574 | 330d0414 | bellard | /* Keyboard Replies */
|
2575 | 330d0414 | bellard | #define KBD_REPLY_POR 0xAA /* Power on reset */ |
2576 | 330d0414 | bellard | #define KBD_REPLY_ACK 0xFA /* Command ACK */ |
2577 | 330d0414 | bellard | #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */ |
2578 | 330d0414 | bellard | |
2579 | 330d0414 | bellard | /* Status Register Bits */
|
2580 | 330d0414 | bellard | #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */ |
2581 | 330d0414 | bellard | #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */ |
2582 | 330d0414 | bellard | #define KBD_STAT_SELFTEST 0x04 /* Self test successful */ |
2583 | 330d0414 | bellard | #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */ |
2584 | 330d0414 | bellard | #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */ |
2585 | 330d0414 | bellard | #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
2586 | 330d0414 | bellard | #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */ |
2587 | 330d0414 | bellard | #define KBD_STAT_PERR 0x80 /* Parity error */ |
2588 | 330d0414 | bellard | |
2589 | 330d0414 | bellard | /* Controller Mode Register Bits */
|
2590 | 330d0414 | bellard | #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */ |
2591 | 330d0414 | bellard | #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */ |
2592 | 330d0414 | bellard | #define KBD_MODE_SYS 0x04 /* The system flag (?) */ |
2593 | 330d0414 | bellard | #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */ |
2594 | 330d0414 | bellard | #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */ |
2595 | 330d0414 | bellard | #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */ |
2596 | 330d0414 | bellard | #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */ |
2597 | 330d0414 | bellard | #define KBD_MODE_RFU 0x80 |
2598 | 330d0414 | bellard | |
2599 | 330d0414 | bellard | /* Mouse Commands */
|
2600 | 330d0414 | bellard | #define AUX_SET_RES 0xE8 /* Set resolution */ |
2601 | 330d0414 | bellard | #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */ |
2602 | 330d0414 | bellard | #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */ |
2603 | 330d0414 | bellard | #define AUX_GET_SCALE 0xE9 /* Get scaling factor */ |
2604 | 330d0414 | bellard | #define AUX_SET_STREAM 0xEA /* Set stream mode */ |
2605 | 330d0414 | bellard | #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */ |
2606 | 330d0414 | bellard | #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */ |
2607 | 330d0414 | bellard | #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */ |
2608 | 330d0414 | bellard | #define AUX_RESET 0xFF /* Reset aux device */ |
2609 | 330d0414 | bellard | #define AUX_ACK 0xFA /* Command byte ACK. */ |
2610 | 330d0414 | bellard | |
2611 | 330d0414 | bellard | #define KBD_QUEUE_SIZE 64 |
2612 | 330d0414 | bellard | |
2613 | 330d0414 | bellard | typedef struct { |
2614 | 330d0414 | bellard | uint8_t data[KBD_QUEUE_SIZE]; |
2615 | 330d0414 | bellard | int rptr, wptr, count;
|
2616 | 330d0414 | bellard | } KBDQueue; |
2617 | 330d0414 | bellard | |
2618 | 330d0414 | bellard | enum KBDWriteState {
|
2619 | 330d0414 | bellard | KBD_STATE_CMD = 0,
|
2620 | 330d0414 | bellard | KBD_STATE_LED, |
2621 | 330d0414 | bellard | }; |
2622 | cd4c3e88 | bellard | |
2623 | 330d0414 | bellard | typedef struct KBDState { |
2624 | 330d0414 | bellard | KBDQueue queues[2];
|
2625 | 330d0414 | bellard | uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
|
2626 | 330d0414 | bellard | uint8_t status; |
2627 | 330d0414 | bellard | uint8_t mode; |
2628 | 330d0414 | bellard | int kbd_write_cmd;
|
2629 | 330d0414 | bellard | int scan_enabled;
|
2630 | 330d0414 | bellard | } KBDState; |
2631 | 330d0414 | bellard | |
2632 | 330d0414 | bellard | KBDState kbd_state; |
2633 | cd4c3e88 | bellard | int reset_requested;
|
2634 | 330d0414 | bellard | int a20_enabled;
|
2635 | 330d0414 | bellard | |
2636 | 330d0414 | bellard | static void kbd_update_irq(KBDState *s) |
2637 | 330d0414 | bellard | { |
2638 | 330d0414 | bellard | int level;
|
2639 | 330d0414 | bellard | |
2640 | 330d0414 | bellard | level = ((s->status & KBD_STAT_OBF) && (s->mode & KBD_MODE_KBD_INT)); |
2641 | 330d0414 | bellard | pic_set_irq(1, level);
|
2642 | 330d0414 | bellard | |
2643 | 330d0414 | bellard | level = ((s->status & KBD_STAT_MOUSE_OBF) && (s->mode & KBD_MODE_MOUSE_INT)); |
2644 | 330d0414 | bellard | pic_set_irq(12, level);
|
2645 | 330d0414 | bellard | } |
2646 | 330d0414 | bellard | |
2647 | 330d0414 | bellard | static void kbd_queue(KBDState *s, int b, int aux) |
2648 | 330d0414 | bellard | { |
2649 | 330d0414 | bellard | KBDQueue *q = &kbd_state.queues[aux]; |
2650 | 330d0414 | bellard | |
2651 | 330d0414 | bellard | if (q->count >= KBD_QUEUE_SIZE)
|
2652 | 330d0414 | bellard | return;
|
2653 | 330d0414 | bellard | q->data[q->wptr] = b; |
2654 | 330d0414 | bellard | if (++q->wptr == KBD_QUEUE_SIZE)
|
2655 | 330d0414 | bellard | q->wptr = 0;
|
2656 | 330d0414 | bellard | q->count++; |
2657 | 330d0414 | bellard | s->status |= KBD_STAT_OBF; |
2658 | 330d0414 | bellard | if (aux)
|
2659 | 330d0414 | bellard | s->status |= KBD_STAT_MOUSE_OBF; |
2660 | 330d0414 | bellard | kbd_update_irq(s); |
2661 | 330d0414 | bellard | } |
2662 | cd4c3e88 | bellard | |
2663 | cd4c3e88 | bellard | uint32_t kbd_read_status(CPUX86State *env, uint32_t addr) |
2664 | cd4c3e88 | bellard | { |
2665 | 330d0414 | bellard | KBDState *s = &kbd_state; |
2666 | 330d0414 | bellard | int val;
|
2667 | 330d0414 | bellard | val = s->status; |
2668 | 330d0414 | bellard | #if defined(DEBUG_KBD) && 0 |
2669 | 330d0414 | bellard | printf("kbd: read status=0x%02x\n", val);
|
2670 | 330d0414 | bellard | #endif
|
2671 | 330d0414 | bellard | return val;
|
2672 | cd4c3e88 | bellard | } |
2673 | cd4c3e88 | bellard | |
2674 | cd4c3e88 | bellard | void kbd_write_command(CPUX86State *env, uint32_t addr, uint32_t val)
|
2675 | cd4c3e88 | bellard | { |
2676 | 330d0414 | bellard | KBDState *s = &kbd_state; |
2677 | 330d0414 | bellard | |
2678 | 330d0414 | bellard | #ifdef DEBUG_KBD
|
2679 | 330d0414 | bellard | printf("kbd: write cmd=0x%02x\n", val);
|
2680 | 330d0414 | bellard | #endif
|
2681 | cd4c3e88 | bellard | switch(val) {
|
2682 | 330d0414 | bellard | case KBD_CCMD_READ_MODE:
|
2683 | 330d0414 | bellard | kbd_queue(s, s->mode, 0);
|
2684 | 330d0414 | bellard | break;
|
2685 | 330d0414 | bellard | case KBD_CCMD_WRITE_MODE:
|
2686 | 330d0414 | bellard | case KBD_CCMD_WRITE_OBUF:
|
2687 | 330d0414 | bellard | case KBD_CCMD_WRITE_AUX_OBUF:
|
2688 | 330d0414 | bellard | case KBD_CCMD_WRITE_MOUSE:
|
2689 | 330d0414 | bellard | case KBD_CCMD_WRITE_OUTPORT:
|
2690 | 330d0414 | bellard | s->write_cmd = val; |
2691 | 330d0414 | bellard | break;
|
2692 | 330d0414 | bellard | case KBD_CCMD_MOUSE_DISABLE:
|
2693 | 330d0414 | bellard | s->mode |= KBD_MODE_DISABLE_MOUSE; |
2694 | 330d0414 | bellard | break;
|
2695 | 330d0414 | bellard | case KBD_CCMD_MOUSE_ENABLE:
|
2696 | 330d0414 | bellard | s->mode &= ~KBD_MODE_DISABLE_MOUSE; |
2697 | 330d0414 | bellard | break;
|
2698 | 330d0414 | bellard | case KBD_CCMD_TEST_MOUSE:
|
2699 | 330d0414 | bellard | kbd_queue(s, 0x00, 0); |
2700 | 330d0414 | bellard | break;
|
2701 | 330d0414 | bellard | case KBD_CCMD_SELF_TEST:
|
2702 | 330d0414 | bellard | s->status |= KBD_STAT_SELFTEST; |
2703 | 330d0414 | bellard | kbd_queue(s, 0x55, 0); |
2704 | 330d0414 | bellard | break;
|
2705 | 330d0414 | bellard | case KBD_CCMD_KBD_TEST:
|
2706 | 330d0414 | bellard | kbd_queue(s, 0x00, 0); |
2707 | 330d0414 | bellard | break;
|
2708 | 330d0414 | bellard | case KBD_CCMD_KBD_DISABLE:
|
2709 | 330d0414 | bellard | s->mode |= KBD_MODE_DISABLE_KBD; |
2710 | 330d0414 | bellard | break;
|
2711 | 330d0414 | bellard | case KBD_CCMD_KBD_ENABLE:
|
2712 | 330d0414 | bellard | s->mode &= ~KBD_MODE_DISABLE_KBD; |
2713 | 330d0414 | bellard | break;
|
2714 | 330d0414 | bellard | case KBD_CCMD_READ_INPORT:
|
2715 | 330d0414 | bellard | kbd_queue(s, 0x00, 0); |
2716 | 330d0414 | bellard | break;
|
2717 | 330d0414 | bellard | case KBD_CCMD_READ_OUTPORT:
|
2718 | 330d0414 | bellard | /* XXX: check that */
|
2719 | 330d0414 | bellard | val = 0x01 | (a20_enabled << 1); |
2720 | 330d0414 | bellard | if (s->status & KBD_STAT_OBF)
|
2721 | 330d0414 | bellard | val |= 0x10;
|
2722 | 330d0414 | bellard | if (s->status & KBD_STAT_MOUSE_OBF)
|
2723 | 330d0414 | bellard | val |= 0x20;
|
2724 | 330d0414 | bellard | kbd_queue(s, val, 0);
|
2725 | 330d0414 | bellard | break;
|
2726 | 330d0414 | bellard | case KBD_CCMD_ENABLE_A20:
|
2727 | 330d0414 | bellard | a20_enabled = 1;
|
2728 | 330d0414 | bellard | break;
|
2729 | 330d0414 | bellard | case KBD_CCMD_DISABLE_A20:
|
2730 | 330d0414 | bellard | a20_enabled = 0;
|
2731 | 330d0414 | bellard | break;
|
2732 | 330d0414 | bellard | case KBD_CCMD_RESET:
|
2733 | cd4c3e88 | bellard | reset_requested = 1;
|
2734 | cd4c3e88 | bellard | cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT); |
2735 | cd4c3e88 | bellard | break;
|
2736 | cd4c3e88 | bellard | default:
|
2737 | 330d0414 | bellard | fprintf(stderr, "vl: unsupported keyboard cmd=0x%02x\n", val);
|
2738 | 330d0414 | bellard | break;
|
2739 | 330d0414 | bellard | } |
2740 | 330d0414 | bellard | } |
2741 | 330d0414 | bellard | |
2742 | 330d0414 | bellard | uint32_t kbd_read_data(CPUX86State *env, uint32_t addr) |
2743 | 330d0414 | bellard | { |
2744 | 330d0414 | bellard | KBDState *s = &kbd_state; |
2745 | 330d0414 | bellard | KBDQueue *q; |
2746 | 330d0414 | bellard | int val;
|
2747 | 330d0414 | bellard | |
2748 | 330d0414 | bellard | q = &s->queues[1]; /* first check AUX data */ |
2749 | 330d0414 | bellard | if (q->count == 0) |
2750 | 330d0414 | bellard | q = &s->queues[0]; /* then check KBD data */ |
2751 | 330d0414 | bellard | if (q->count == 0) { |
2752 | 330d0414 | bellard | /* XXX: return something else ? */
|
2753 | 330d0414 | bellard | val = 0;
|
2754 | 330d0414 | bellard | } else {
|
2755 | 330d0414 | bellard | val = q->data[q->rptr]; |
2756 | 330d0414 | bellard | if (++q->rptr == KBD_QUEUE_SIZE)
|
2757 | 330d0414 | bellard | q->rptr = 0;
|
2758 | 330d0414 | bellard | q->count--; |
2759 | 330d0414 | bellard | } |
2760 | 330d0414 | bellard | if (s->queues[1].count == 0) { |
2761 | 330d0414 | bellard | s->status &= ~KBD_STAT_MOUSE_OBF; |
2762 | 330d0414 | bellard | if (s->queues[0].count == 0) |
2763 | 330d0414 | bellard | s->status &= ~KBD_STAT_OBF; |
2764 | 330d0414 | bellard | kbd_update_irq(s); |
2765 | 330d0414 | bellard | } |
2766 | 330d0414 | bellard | |
2767 | 330d0414 | bellard | #ifdef DEBUG_KBD
|
2768 | 330d0414 | bellard | printf("kbd: read data=0x%02x\n", val);
|
2769 | 330d0414 | bellard | #endif
|
2770 | 330d0414 | bellard | return val;
|
2771 | 330d0414 | bellard | } |
2772 | 330d0414 | bellard | |
2773 | 330d0414 | bellard | static void kbd_reset_keyboard(KBDState *s) |
2774 | 330d0414 | bellard | { |
2775 | 330d0414 | bellard | s->scan_enabled = 1;
|
2776 | 330d0414 | bellard | } |
2777 | 330d0414 | bellard | |
2778 | 330d0414 | bellard | static void kbd_write_keyboard(KBDState *s, int val) |
2779 | 330d0414 | bellard | { |
2780 | 330d0414 | bellard | switch(s->kbd_write_cmd) {
|
2781 | 330d0414 | bellard | default:
|
2782 | 330d0414 | bellard | case -1: |
2783 | 330d0414 | bellard | switch(val) {
|
2784 | 330d0414 | bellard | case 0x00: |
2785 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2786 | 330d0414 | bellard | break;
|
2787 | 330d0414 | bellard | case 0x05: |
2788 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_RESEND, 0);
|
2789 | 330d0414 | bellard | break;
|
2790 | 330d0414 | bellard | case KBD_CMD_ECHO:
|
2791 | 330d0414 | bellard | kbd_queue(s, KBD_CMD_ECHO, 0);
|
2792 | 330d0414 | bellard | break;
|
2793 | 330d0414 | bellard | case KBD_CMD_ENABLE:
|
2794 | 330d0414 | bellard | s->scan_enabled = 1;
|
2795 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2796 | 330d0414 | bellard | break;
|
2797 | 330d0414 | bellard | case KBD_CMD_SET_LEDS:
|
2798 | 330d0414 | bellard | case KBD_CMD_SET_RATE:
|
2799 | 330d0414 | bellard | s->kbd_write_cmd = val; |
2800 | 330d0414 | bellard | break;
|
2801 | 330d0414 | bellard | case KBD_CMD_RESET_DISABLE:
|
2802 | 330d0414 | bellard | kbd_reset_keyboard(s); |
2803 | 330d0414 | bellard | s->scan_enabled = 0;
|
2804 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2805 | 330d0414 | bellard | break;
|
2806 | 330d0414 | bellard | case KBD_CMD_RESET_ENABLE:
|
2807 | 330d0414 | bellard | kbd_reset_keyboard(s); |
2808 | 330d0414 | bellard | s->scan_enabled = 1;
|
2809 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2810 | 330d0414 | bellard | break;
|
2811 | 330d0414 | bellard | case KBD_CMD_RESET:
|
2812 | 330d0414 | bellard | kbd_reset_keyboard(s); |
2813 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2814 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_POR, 0);
|
2815 | 330d0414 | bellard | break;
|
2816 | 330d0414 | bellard | default:
|
2817 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2818 | 330d0414 | bellard | break;
|
2819 | 330d0414 | bellard | } |
2820 | 330d0414 | bellard | break;
|
2821 | 330d0414 | bellard | case KBD_CMD_SET_LEDS:
|
2822 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2823 | 330d0414 | bellard | break;
|
2824 | 330d0414 | bellard | case KBD_CMD_SET_RATE:
|
2825 | 330d0414 | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
2826 | 330d0414 | bellard | break;
|
2827 | 330d0414 | bellard | } |
2828 | 330d0414 | bellard | s->kbd_write_cmd = -1;
|
2829 | 330d0414 | bellard | } |
2830 | 330d0414 | bellard | |
2831 | 330d0414 | bellard | void kbd_write_data(CPUX86State *env, uint32_t addr, uint32_t val)
|
2832 | 330d0414 | bellard | { |
2833 | 330d0414 | bellard | KBDState *s = &kbd_state; |
2834 | 330d0414 | bellard | |
2835 | 330d0414 | bellard | #ifdef DEBUG_KBD
|
2836 | 330d0414 | bellard | printf("kbd: write data=0x%02x\n", val);
|
2837 | 330d0414 | bellard | #endif
|
2838 | 330d0414 | bellard | |
2839 | 330d0414 | bellard | switch(s->write_cmd) {
|
2840 | 330d0414 | bellard | case 0: |
2841 | 330d0414 | bellard | kbd_write_keyboard(s, val); |
2842 | 330d0414 | bellard | break;
|
2843 | 330d0414 | bellard | case KBD_CCMD_WRITE_MODE:
|
2844 | 330d0414 | bellard | s->mode = val; |
2845 | 330d0414 | bellard | kbd_update_irq(s); |
2846 | 330d0414 | bellard | break;
|
2847 | 330d0414 | bellard | case KBD_CCMD_WRITE_OBUF:
|
2848 | 330d0414 | bellard | kbd_queue(s, val, 0);
|
2849 | 330d0414 | bellard | break;
|
2850 | 330d0414 | bellard | case KBD_CCMD_WRITE_AUX_OBUF:
|
2851 | 330d0414 | bellard | kbd_queue(s, val, 1);
|
2852 | 330d0414 | bellard | break;
|
2853 | 330d0414 | bellard | case KBD_CCMD_WRITE_OUTPORT:
|
2854 | 330d0414 | bellard | a20_enabled = (val >> 1) & 1; |
2855 | 330d0414 | bellard | if (!(val & 1)) { |
2856 | 330d0414 | bellard | reset_requested = 1;
|
2857 | 330d0414 | bellard | cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT); |
2858 | 330d0414 | bellard | } |
2859 | 330d0414 | bellard | break;
|
2860 | 330d0414 | bellard | default:
|
2861 | cd4c3e88 | bellard | break;
|
2862 | cd4c3e88 | bellard | } |
2863 | 330d0414 | bellard | s->write_cmd = 0;
|
2864 | 330d0414 | bellard | } |
2865 | 330d0414 | bellard | |
2866 | 330d0414 | bellard | void kbd_reset(KBDState *s)
|
2867 | 330d0414 | bellard | { |
2868 | 330d0414 | bellard | KBDQueue *q; |
2869 | 330d0414 | bellard | int i;
|
2870 | 330d0414 | bellard | |
2871 | 330d0414 | bellard | s->kbd_write_cmd = -1;
|
2872 | 330d0414 | bellard | s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT; |
2873 | 330d0414 | bellard | s->status = KBD_MODE_SYS | KBD_MODE_NO_KEYLOCK; |
2874 | 330d0414 | bellard | for(i = 0; i < 2; i++) { |
2875 | 330d0414 | bellard | q = &s->queues[i]; |
2876 | 330d0414 | bellard | q->rptr = 0;
|
2877 | 330d0414 | bellard | q->wptr = 0;
|
2878 | 330d0414 | bellard | q->count = 0;
|
2879 | 330d0414 | bellard | } |
2880 | cd4c3e88 | bellard | } |
2881 | cd4c3e88 | bellard | |
2882 | cd4c3e88 | bellard | void kbd_init(void) |
2883 | cd4c3e88 | bellard | { |
2884 | 330d0414 | bellard | kbd_reset(&kbd_state); |
2885 | 330d0414 | bellard | register_ioport_read(0x60, 1, kbd_read_data, 1); |
2886 | 330d0414 | bellard | register_ioport_write(0x60, 1, kbd_write_data, 1); |
2887 | cd4c3e88 | bellard | register_ioport_read(0x64, 1, kbd_read_status, 1); |
2888 | cd4c3e88 | bellard | register_ioport_write(0x64, 1, kbd_write_command, 1); |
2889 | cd4c3e88 | bellard | } |
2890 | cd4c3e88 | bellard | |
2891 | cd4c3e88 | bellard | /***********************************************************/
|
2892 | 330d0414 | bellard | /* Bochs BIOS debug ports */
|
2893 | 330d0414 | bellard | |
2894 | 330d0414 | bellard | void bochs_bios_write(CPUX86State *env, uint32_t addr, uint32_t val)
|
2895 | 330d0414 | bellard | { |
2896 | 330d0414 | bellard | switch(addr) {
|
2897 | 330d0414 | bellard | /* Bochs BIOS messages */
|
2898 | 330d0414 | bellard | case 0x400: |
2899 | 330d0414 | bellard | case 0x401: |
2900 | 330d0414 | bellard | fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
|
2901 | 330d0414 | bellard | exit(1);
|
2902 | 330d0414 | bellard | case 0x402: |
2903 | 330d0414 | bellard | case 0x403: |
2904 | 330d0414 | bellard | #ifdef DEBUG_BIOS
|
2905 | 330d0414 | bellard | fprintf(stderr, "%c", val);
|
2906 | 330d0414 | bellard | #endif
|
2907 | 330d0414 | bellard | break;
|
2908 | 330d0414 | bellard | |
2909 | 330d0414 | bellard | /* LGPL'ed VGA BIOS messages */
|
2910 | 330d0414 | bellard | case 0x501: |
2911 | 330d0414 | bellard | case 0x502: |
2912 | 330d0414 | bellard | fprintf(stderr, "VGA BIOS panic, line %d\n", val);
|
2913 | 330d0414 | bellard | exit(1);
|
2914 | 330d0414 | bellard | case 0x500: |
2915 | 330d0414 | bellard | case 0x503: |
2916 | 330d0414 | bellard | #ifdef DEBUG_BIOS
|
2917 | 330d0414 | bellard | fprintf(stderr, "%c", val);
|
2918 | 330d0414 | bellard | #endif
|
2919 | 330d0414 | bellard | break;
|
2920 | 330d0414 | bellard | } |
2921 | 330d0414 | bellard | } |
2922 | 330d0414 | bellard | |
2923 | 330d0414 | bellard | void bochs_bios_init(void) |
2924 | 330d0414 | bellard | { |
2925 | 330d0414 | bellard | register_ioport_write(0x400, 1, bochs_bios_write, 2); |
2926 | 330d0414 | bellard | register_ioport_write(0x401, 1, bochs_bios_write, 2); |
2927 | 330d0414 | bellard | register_ioport_write(0x402, 1, bochs_bios_write, 1); |
2928 | 330d0414 | bellard | register_ioport_write(0x403, 1, bochs_bios_write, 1); |
2929 | 330d0414 | bellard | |
2930 | 330d0414 | bellard | register_ioport_write(0x501, 1, bochs_bios_write, 2); |
2931 | 330d0414 | bellard | register_ioport_write(0x502, 1, bochs_bios_write, 2); |
2932 | 330d0414 | bellard | register_ioport_write(0x500, 1, bochs_bios_write, 1); |
2933 | 330d0414 | bellard | register_ioport_write(0x503, 1, bochs_bios_write, 1); |
2934 | 330d0414 | bellard | } |
2935 | 330d0414 | bellard | |
2936 | 330d0414 | bellard | /***********************************************************/
|
2937 | 0824d6fc | bellard | /* cpu signal handler */
|
2938 | 0824d6fc | bellard | static void host_segv_handler(int host_signum, siginfo_t *info, |
2939 | 0824d6fc | bellard | void *puc)
|
2940 | 0824d6fc | bellard | { |
2941 | 0824d6fc | bellard | if (cpu_signal_handler(host_signum, info, puc))
|
2942 | 0824d6fc | bellard | return;
|
2943 | 0824d6fc | bellard | term_exit(); |
2944 | 0824d6fc | bellard | abort(); |
2945 | 0824d6fc | bellard | } |
2946 | 0824d6fc | bellard | |
2947 | 0824d6fc | bellard | static int timer_irq_pending; |
2948 | 87858c89 | bellard | static int timer_irq_count; |
2949 | 0824d6fc | bellard | |
2950 | 0824d6fc | bellard | static void host_alarm_handler(int host_signum, siginfo_t *info, |
2951 | 0824d6fc | bellard | void *puc)
|
2952 | 0824d6fc | bellard | { |
2953 | 87858c89 | bellard | /* NOTE: since usually the OS asks a 100 Hz clock, there can be
|
2954 | 87858c89 | bellard | some drift between cpu_get_ticks() and the interrupt time. So
|
2955 | 87858c89 | bellard | we queue some interrupts to avoid missing some */
|
2956 | 87858c89 | bellard | timer_irq_count += pit_get_out_edges(&pit_channels[0]);
|
2957 | 87858c89 | bellard | if (timer_irq_count) {
|
2958 | 87858c89 | bellard | if (timer_irq_count > 2) |
2959 | 87858c89 | bellard | timer_irq_count = 2;
|
2960 | 87858c89 | bellard | timer_irq_count--; |
2961 | 87858c89 | bellard | /* just exit from the cpu to have a chance to handle timers */
|
2962 | c9159e53 | bellard | cpu_x86_interrupt(global_env, CPU_INTERRUPT_EXIT); |
2963 | 87858c89 | bellard | timer_irq_pending = 1;
|
2964 | 87858c89 | bellard | } |
2965 | 0824d6fc | bellard | } |
2966 | 0824d6fc | bellard | |
2967 | 33e3963e | bellard | unsigned long mmap_addr = PHYS_RAM_BASE; |
2968 | 33e3963e | bellard | |
2969 | 33e3963e | bellard | void *get_mmap_addr(unsigned long size) |
2970 | 33e3963e | bellard | { |
2971 | 33e3963e | bellard | unsigned long addr; |
2972 | 33e3963e | bellard | addr = mmap_addr; |
2973 | 33e3963e | bellard | mmap_addr += ((size + 4095) & ~4095) + 4096; |
2974 | 33e3963e | bellard | return (void *)addr; |
2975 | 33e3963e | bellard | } |
2976 | 33e3963e | bellard | |
2977 | b4608c04 | bellard | /* main execution loop */
|
2978 | b4608c04 | bellard | |
2979 | b4608c04 | bellard | CPUState *cpu_gdbstub_get_env(void *opaque)
|
2980 | b4608c04 | bellard | { |
2981 | b4608c04 | bellard | return global_env;
|
2982 | b4608c04 | bellard | } |
2983 | b4608c04 | bellard | |
2984 | 4c3a88a2 | bellard | int main_loop(void *opaque) |
2985 | b4608c04 | bellard | { |
2986 | b4608c04 | bellard | struct pollfd ufds[2], *pf, *serial_ufd, *net_ufd, *gdb_ufd; |
2987 | b4608c04 | bellard | int ret, n, timeout;
|
2988 | b4608c04 | bellard | uint8_t ch; |
2989 | b4608c04 | bellard | CPUState *env = global_env; |
2990 | b4608c04 | bellard | |
2991 | b4608c04 | bellard | for(;;) {
|
2992 | b4608c04 | bellard | |
2993 | b4608c04 | bellard | ret = cpu_x86_exec(env); |
2994 | cd4c3e88 | bellard | if (reset_requested)
|
2995 | cd4c3e88 | bellard | break;
|
2996 | 4c3a88a2 | bellard | if (ret == EXCP_DEBUG)
|
2997 | 4c3a88a2 | bellard | return EXCP_DEBUG;
|
2998 | b4608c04 | bellard | /* if hlt instruction, we wait until the next IRQ */
|
2999 | b4608c04 | bellard | if (ret == EXCP_HLT)
|
3000 | b4608c04 | bellard | timeout = 10;
|
3001 | b4608c04 | bellard | else
|
3002 | b4608c04 | bellard | timeout = 0;
|
3003 | b4608c04 | bellard | /* poll any events */
|
3004 | b4608c04 | bellard | serial_ufd = NULL;
|
3005 | b4608c04 | bellard | pf = ufds; |
3006 | b4608c04 | bellard | if (!(serial_ports[0].lsr & UART_LSR_DR)) { |
3007 | b4608c04 | bellard | serial_ufd = pf; |
3008 | b4608c04 | bellard | pf->fd = 0;
|
3009 | b4608c04 | bellard | pf->events = POLLIN; |
3010 | b4608c04 | bellard | pf++; |
3011 | b4608c04 | bellard | } |
3012 | b4608c04 | bellard | net_ufd = NULL;
|
3013 | b4608c04 | bellard | if (net_fd > 0 && ne2000_can_receive(&ne2000_state)) { |
3014 | b4608c04 | bellard | net_ufd = pf; |
3015 | b4608c04 | bellard | pf->fd = net_fd; |
3016 | b4608c04 | bellard | pf->events = POLLIN; |
3017 | b4608c04 | bellard | pf++; |
3018 | b4608c04 | bellard | } |
3019 | b4608c04 | bellard | gdb_ufd = NULL;
|
3020 | b4608c04 | bellard | if (gdbstub_fd > 0) { |
3021 | b4608c04 | bellard | gdb_ufd = pf; |
3022 | b4608c04 | bellard | pf->fd = gdbstub_fd; |
3023 | b4608c04 | bellard | pf->events = POLLIN; |
3024 | b4608c04 | bellard | pf++; |
3025 | b4608c04 | bellard | } |
3026 | b4608c04 | bellard | |
3027 | b4608c04 | bellard | ret = poll(ufds, pf - ufds, timeout); |
3028 | b4608c04 | bellard | if (ret > 0) { |
3029 | b4608c04 | bellard | if (serial_ufd && (serial_ufd->revents & POLLIN)) {
|
3030 | b4608c04 | bellard | n = read(0, &ch, 1); |
3031 | b4608c04 | bellard | if (n == 1) { |
3032 | b4608c04 | bellard | serial_received_byte(&serial_ports[0], ch);
|
3033 | b4608c04 | bellard | } |
3034 | b4608c04 | bellard | } |
3035 | b4608c04 | bellard | if (net_ufd && (net_ufd->revents & POLLIN)) {
|
3036 | b4608c04 | bellard | uint8_t buf[MAX_ETH_FRAME_SIZE]; |
3037 | b4608c04 | bellard | |
3038 | b4608c04 | bellard | n = read(net_fd, buf, MAX_ETH_FRAME_SIZE); |
3039 | b4608c04 | bellard | if (n > 0) { |
3040 | b4608c04 | bellard | if (n < 60) { |
3041 | b4608c04 | bellard | memset(buf + n, 0, 60 - n); |
3042 | b4608c04 | bellard | n = 60;
|
3043 | b4608c04 | bellard | } |
3044 | b4608c04 | bellard | ne2000_receive(&ne2000_state, buf, n); |
3045 | b4608c04 | bellard | } |
3046 | b4608c04 | bellard | } |
3047 | b4608c04 | bellard | if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
|
3048 | b4608c04 | bellard | uint8_t buf[1];
|
3049 | b4608c04 | bellard | /* stop emulation if requested by gdb */
|
3050 | b4608c04 | bellard | n = read(gdbstub_fd, buf, 1);
|
3051 | b4608c04 | bellard | if (n == 1) |
3052 | b4608c04 | bellard | break;
|
3053 | b4608c04 | bellard | } |
3054 | b4608c04 | bellard | } |
3055 | b4608c04 | bellard | |
3056 | b4608c04 | bellard | /* timer IRQ */
|
3057 | b4608c04 | bellard | if (timer_irq_pending) {
|
3058 | b4608c04 | bellard | pic_set_irq(0, 1); |
3059 | b4608c04 | bellard | pic_set_irq(0, 0); |
3060 | b4608c04 | bellard | timer_irq_pending = 0;
|
3061 | b4608c04 | bellard | } |
3062 | b4608c04 | bellard | } |
3063 | 4c3a88a2 | bellard | return EXCP_INTERRUPT;
|
3064 | b4608c04 | bellard | } |
3065 | b4608c04 | bellard | |
3066 | 0824d6fc | bellard | void help(void) |
3067 | 0824d6fc | bellard | { |
3068 | 0824d6fc | bellard | printf("Virtual Linux version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
3069 | 330d0414 | bellard | "usage: vl [options] [bzImage [kernel parameters...]]\n"
|
3070 | 0824d6fc | bellard | "\n"
|
3071 | 0824d6fc | bellard | "'bzImage' is a Linux kernel image (PAGE_OFFSET must be defined\n"
|
3072 | 0824d6fc | bellard | "to 0x90000000 in asm/page.h and arch/i386/vmlinux.lds)\n"
|
3073 | fc01f7e7 | bellard | "\n"
|
3074 | fc01f7e7 | bellard | "General options:\n"
|
3075 | fc01f7e7 | bellard | "-initrd file use 'file' as initial ram disk\n"
|
3076 | fc01f7e7 | bellard | "-hda file use 'file' as hard disk 0 image\n"
|
3077 | fc01f7e7 | bellard | "-hdb file use 'file' as hard disk 1 image\n"
|
3078 | 33e3963e | bellard | "-snapshot write to temporary files instead of disk image files\n"
|
3079 | fc01f7e7 | bellard | "-m megs set virtual RAM size to megs MB\n"
|
3080 | fc01f7e7 | bellard | "-n script set network init script [default=%s]\n"
|
3081 | fc01f7e7 | bellard | "\n"
|
3082 | 330d0414 | bellard | "Debug/Expert options:\n"
|
3083 | fc01f7e7 | bellard | "-s wait gdb connection to port %d\n"
|
3084 | fc01f7e7 | bellard | "-p port change gdb connection port\n"
|
3085 | fc01f7e7 | bellard | "-d output log in /tmp/vl.log\n"
|
3086 | 330d0414 | bellard | "-hdachs c,h,s force hard disk 0 geometry for non LBA disk images\n"
|
3087 | 330d0414 | bellard | "-L path set the directory for the BIOS and VGA BIOS\n"
|
3088 | 0824d6fc | bellard | "\n"
|
3089 | f1510b2c | bellard | "During emulation, use C-a h to get terminal commands:\n",
|
3090 | b4608c04 | bellard | DEFAULT_NETWORK_SCRIPT, DEFAULT_GDBSTUB_PORT); |
3091 | 0824d6fc | bellard | term_print_help(); |
3092 | 0824d6fc | bellard | exit(1);
|
3093 | 0824d6fc | bellard | } |
3094 | 0824d6fc | bellard | |
3095 | fc01f7e7 | bellard | struct option long_options[] = {
|
3096 | fc01f7e7 | bellard | { "initrd", 1, NULL, 0, }, |
3097 | fc01f7e7 | bellard | { "hda", 1, NULL, 0, }, |
3098 | fc01f7e7 | bellard | { "hdb", 1, NULL, 0, }, |
3099 | 33e3963e | bellard | { "snapshot", 0, NULL, 0, }, |
3100 | 330d0414 | bellard | { "hdachs", 1, NULL, 0, }, |
3101 | fc01f7e7 | bellard | { NULL, 0, NULL, 0 }, |
3102 | fc01f7e7 | bellard | }; |
3103 | fc01f7e7 | bellard | |
3104 | 0824d6fc | bellard | int main(int argc, char **argv) |
3105 | 0824d6fc | bellard | { |
3106 | fc01f7e7 | bellard | int c, ret, initrd_size, i, use_gdbstub, gdbstub_port, long_index;
|
3107 | 330d0414 | bellard | int snapshot, linux_boot;
|
3108 | 0824d6fc | bellard | struct linux_params *params;
|
3109 | 0824d6fc | bellard | struct sigaction act;
|
3110 | 0824d6fc | bellard | struct itimerval itv;
|
3111 | 0824d6fc | bellard | CPUX86State *env; |
3112 | fc01f7e7 | bellard | const char *tmpdir, *initrd_filename; |
3113 | fc01f7e7 | bellard | const char *hd_filename[MAX_DISKS]; |
3114 | 87858c89 | bellard | |
3115 | 0824d6fc | bellard | /* we never want that malloc() uses mmap() */
|
3116 | 0824d6fc | bellard | mallopt(M_MMAP_THRESHOLD, 4096 * 1024); |
3117 | fc01f7e7 | bellard | initrd_filename = NULL;
|
3118 | fc01f7e7 | bellard | for(i = 0; i < MAX_DISKS; i++) |
3119 | fc01f7e7 | bellard | hd_filename[i] = NULL;
|
3120 | 0824d6fc | bellard | phys_ram_size = 32 * 1024 * 1024; |
3121 | f1510b2c | bellard | pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
|
3122 | b4608c04 | bellard | use_gdbstub = 0;
|
3123 | b4608c04 | bellard | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
3124 | 33e3963e | bellard | snapshot = 0;
|
3125 | 330d0414 | bellard | linux_boot = 0;
|
3126 | 0824d6fc | bellard | for(;;) {
|
3127 | 330d0414 | bellard | c = getopt_long_only(argc, argv, "hm:dn:sp:L:", long_options, &long_index);
|
3128 | 0824d6fc | bellard | if (c == -1) |
3129 | 0824d6fc | bellard | break;
|
3130 | 0824d6fc | bellard | switch(c) {
|
3131 | fc01f7e7 | bellard | case 0: |
3132 | fc01f7e7 | bellard | switch(long_index) {
|
3133 | fc01f7e7 | bellard | case 0: |
3134 | fc01f7e7 | bellard | initrd_filename = optarg; |
3135 | fc01f7e7 | bellard | break;
|
3136 | fc01f7e7 | bellard | case 1: |
3137 | fc01f7e7 | bellard | hd_filename[0] = optarg;
|
3138 | fc01f7e7 | bellard | break;
|
3139 | fc01f7e7 | bellard | case 2: |
3140 | fc01f7e7 | bellard | hd_filename[1] = optarg;
|
3141 | fc01f7e7 | bellard | break;
|
3142 | 33e3963e | bellard | case 3: |
3143 | 33e3963e | bellard | snapshot = 1;
|
3144 | 33e3963e | bellard | break;
|
3145 | 330d0414 | bellard | case 4: |
3146 | 330d0414 | bellard | { |
3147 | 330d0414 | bellard | int cyls, heads, secs;
|
3148 | 330d0414 | bellard | const char *p; |
3149 | 330d0414 | bellard | p = optarg; |
3150 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
3151 | 330d0414 | bellard | if (*p != ',') |
3152 | 330d0414 | bellard | goto chs_fail;
|
3153 | 330d0414 | bellard | p++; |
3154 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
3155 | 330d0414 | bellard | if (*p != ',') |
3156 | 330d0414 | bellard | goto chs_fail;
|
3157 | 330d0414 | bellard | p++; |
3158 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
3159 | 330d0414 | bellard | if (*p != '\0') |
3160 | 330d0414 | bellard | goto chs_fail;
|
3161 | 330d0414 | bellard | ide_state[0].cylinders = cyls;
|
3162 | 330d0414 | bellard | ide_state[0].heads = heads;
|
3163 | 330d0414 | bellard | ide_state[0].sectors = secs;
|
3164 | 330d0414 | bellard | chs_fail: ;
|
3165 | 330d0414 | bellard | } |
3166 | 330d0414 | bellard | break;
|
3167 | fc01f7e7 | bellard | } |
3168 | fc01f7e7 | bellard | break;
|
3169 | 0824d6fc | bellard | case 'h': |
3170 | 0824d6fc | bellard | help(); |
3171 | 0824d6fc | bellard | break;
|
3172 | 0824d6fc | bellard | case 'm': |
3173 | 0824d6fc | bellard | phys_ram_size = atoi(optarg) * 1024 * 1024; |
3174 | 0824d6fc | bellard | if (phys_ram_size <= 0) |
3175 | 0824d6fc | bellard | help(); |
3176 | 7916e224 | bellard | if (phys_ram_size > PHYS_RAM_MAX_SIZE) {
|
3177 | 7916e224 | bellard | fprintf(stderr, "vl: at most %d MB RAM can be simulated\n",
|
3178 | 7916e224 | bellard | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
3179 | 7916e224 | bellard | exit(1);
|
3180 | 7916e224 | bellard | } |
3181 | 0824d6fc | bellard | break;
|
3182 | 0824d6fc | bellard | case 'd': |
3183 | 0824d6fc | bellard | loglevel = 1;
|
3184 | 0824d6fc | bellard | break;
|
3185 | f1510b2c | bellard | case 'n': |
3186 | f1510b2c | bellard | pstrcpy(network_script, sizeof(network_script), optarg);
|
3187 | f1510b2c | bellard | break;
|
3188 | b4608c04 | bellard | case 's': |
3189 | b4608c04 | bellard | use_gdbstub = 1;
|
3190 | b4608c04 | bellard | break;
|
3191 | b4608c04 | bellard | case 'p': |
3192 | b4608c04 | bellard | gdbstub_port = atoi(optarg); |
3193 | b4608c04 | bellard | break;
|
3194 | 330d0414 | bellard | case 'L': |
3195 | 330d0414 | bellard | interp_prefix = optarg; |
3196 | 330d0414 | bellard | break;
|
3197 | 0824d6fc | bellard | } |
3198 | 0824d6fc | bellard | } |
3199 | 330d0414 | bellard | |
3200 | 330d0414 | bellard | linux_boot = (optind < argc); |
3201 | 330d0414 | bellard | |
3202 | 330d0414 | bellard | if (!linux_boot && hd_filename[0] == '\0') |
3203 | 0824d6fc | bellard | help(); |
3204 | 0824d6fc | bellard | |
3205 | 0824d6fc | bellard | /* init debug */
|
3206 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
3207 | 0824d6fc | bellard | if (loglevel) {
|
3208 | 0824d6fc | bellard | logfile = fopen(DEBUG_LOGFILE, "w");
|
3209 | 0824d6fc | bellard | if (!logfile) {
|
3210 | 0824d6fc | bellard | perror(DEBUG_LOGFILE); |
3211 | 0824d6fc | bellard | _exit(1);
|
3212 | 0824d6fc | bellard | } |
3213 | 0824d6fc | bellard | setvbuf(logfile, NULL, _IOLBF, 0); |
3214 | 0824d6fc | bellard | } |
3215 | 0824d6fc | bellard | |
3216 | f1510b2c | bellard | /* init network tun interface */
|
3217 | f1510b2c | bellard | net_init(); |
3218 | f1510b2c | bellard | |
3219 | 0824d6fc | bellard | /* init the memory */
|
3220 | 87858c89 | bellard | tmpdir = getenv("VLTMPDIR");
|
3221 | 87858c89 | bellard | if (!tmpdir)
|
3222 | 87858c89 | bellard | tmpdir = "/tmp";
|
3223 | 87858c89 | bellard | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); |
3224 | 0824d6fc | bellard | if (mkstemp(phys_ram_file) < 0) { |
3225 | 87858c89 | bellard | fprintf(stderr, "Could not create temporary memory file '%s'\n",
|
3226 | 87858c89 | bellard | phys_ram_file); |
3227 | 0824d6fc | bellard | exit(1);
|
3228 | 0824d6fc | bellard | } |
3229 | 0824d6fc | bellard | phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
|
3230 | 0824d6fc | bellard | if (phys_ram_fd < 0) { |
3231 | 87858c89 | bellard | fprintf(stderr, "Could not open temporary memory file '%s'\n",
|
3232 | 87858c89 | bellard | phys_ram_file); |
3233 | 0824d6fc | bellard | exit(1);
|
3234 | 0824d6fc | bellard | } |
3235 | 0824d6fc | bellard | ftruncate(phys_ram_fd, phys_ram_size); |
3236 | 0824d6fc | bellard | unlink(phys_ram_file); |
3237 | 33e3963e | bellard | phys_ram_base = mmap(get_mmap_addr(phys_ram_size), phys_ram_size, |
3238 | 0824d6fc | bellard | PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, |
3239 | 0824d6fc | bellard | phys_ram_fd, 0);
|
3240 | 0824d6fc | bellard | if (phys_ram_base == MAP_FAILED) {
|
3241 | 0824d6fc | bellard | fprintf(stderr, "Could not map physical memory\n");
|
3242 | 0824d6fc | bellard | exit(1);
|
3243 | 0824d6fc | bellard | } |
3244 | 0824d6fc | bellard | |
3245 | 33e3963e | bellard | /* open the virtual block devices */
|
3246 | 33e3963e | bellard | for(i = 0; i < MAX_DISKS; i++) { |
3247 | 33e3963e | bellard | if (hd_filename[i]) {
|
3248 | 33e3963e | bellard | bs_table[i] = bdrv_open(hd_filename[i], snapshot); |
3249 | 33e3963e | bellard | if (!bs_table[i]) {
|
3250 | 33e3963e | bellard | fprintf(stderr, "vl: could not open hard disk image '%s\n",
|
3251 | 33e3963e | bellard | hd_filename[i]); |
3252 | 33e3963e | bellard | exit(1);
|
3253 | 33e3963e | bellard | } |
3254 | 33e3963e | bellard | } |
3255 | 33e3963e | bellard | } |
3256 | 33e3963e | bellard | |
3257 | 330d0414 | bellard | /* init CPU state */
|
3258 | 330d0414 | bellard | env = cpu_init(); |
3259 | 330d0414 | bellard | global_env = env; |
3260 | 330d0414 | bellard | cpu_single_env = env; |
3261 | 330d0414 | bellard | |
3262 | 330d0414 | bellard | init_ioports(); |
3263 | 0824d6fc | bellard | |
3264 | 330d0414 | bellard | if (linux_boot) {
|
3265 | 330d0414 | bellard | /* now we can load the kernel */
|
3266 | 330d0414 | bellard | ret = load_kernel(argv[optind], phys_ram_base + KERNEL_LOAD_ADDR); |
3267 | 330d0414 | bellard | if (ret < 0) { |
3268 | 330d0414 | bellard | fprintf(stderr, "vl: could not load kernel '%s'\n", argv[optind]);
|
3269 | fc01f7e7 | bellard | exit(1);
|
3270 | fc01f7e7 | bellard | } |
3271 | 330d0414 | bellard | |
3272 | 330d0414 | bellard | /* load initrd */
|
3273 | 330d0414 | bellard | initrd_size = 0;
|
3274 | 330d0414 | bellard | if (initrd_filename) {
|
3275 | 330d0414 | bellard | initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR); |
3276 | 330d0414 | bellard | if (initrd_size < 0) { |
3277 | 330d0414 | bellard | fprintf(stderr, "vl: could not load initial ram disk '%s'\n",
|
3278 | 330d0414 | bellard | initrd_filename); |
3279 | 330d0414 | bellard | exit(1);
|
3280 | 330d0414 | bellard | } |
3281 | 330d0414 | bellard | } |
3282 | 330d0414 | bellard | |
3283 | 330d0414 | bellard | /* init kernel params */
|
3284 | 330d0414 | bellard | params = (void *)(phys_ram_base + KERNEL_PARAMS_ADDR);
|
3285 | 330d0414 | bellard | memset(params, 0, sizeof(struct linux_params)); |
3286 | 330d0414 | bellard | params->mount_root_rdonly = 0;
|
3287 | 330d0414 | bellard | params->cl_magic = 0xA33F;
|
3288 | 330d0414 | bellard | params->cl_offset = params->commandline - (uint8_t *)params; |
3289 | 330d0414 | bellard | params->alt_mem_k = (phys_ram_size / 1024) - 1024; |
3290 | 330d0414 | bellard | for(i = optind + 1; i < argc; i++) { |
3291 | 330d0414 | bellard | if (i != optind + 1) |
3292 | 330d0414 | bellard | pstrcat(params->commandline, sizeof(params->commandline), " "); |
3293 | 330d0414 | bellard | pstrcat(params->commandline, sizeof(params->commandline), argv[i]);
|
3294 | 330d0414 | bellard | } |
3295 | 330d0414 | bellard | params->loader_type = 0x01;
|
3296 | 330d0414 | bellard | if (initrd_size > 0) { |
3297 | 330d0414 | bellard | params->initrd_start = INITRD_LOAD_ADDR; |
3298 | 330d0414 | bellard | params->initrd_size = initrd_size; |
3299 | 330d0414 | bellard | } |
3300 | 330d0414 | bellard | params->orig_video_lines = 25;
|
3301 | 330d0414 | bellard | params->orig_video_cols = 80;
|
3302 | 330d0414 | bellard | |
3303 | 330d0414 | bellard | /* setup basic memory access */
|
3304 | 330d0414 | bellard | env->cr[0] = 0x00000033; |
3305 | 330d0414 | bellard | cpu_x86_init_mmu(env); |
3306 | 330d0414 | bellard | |
3307 | 330d0414 | bellard | memset(params->idt_table, 0, sizeof(params->idt_table)); |
3308 | 330d0414 | bellard | |
3309 | 330d0414 | bellard | params->gdt_table[2] = 0x00cf9a000000ffffLL; /* KERNEL_CS */ |
3310 | 330d0414 | bellard | params->gdt_table[3] = 0x00cf92000000ffffLL; /* KERNEL_DS */ |
3311 | 330d0414 | bellard | |
3312 | 330d0414 | bellard | env->idt.base = (void *)params->idt_table;
|
3313 | 330d0414 | bellard | env->idt.limit = sizeof(params->idt_table) - 1; |
3314 | 330d0414 | bellard | env->gdt.base = (void *)params->gdt_table;
|
3315 | 330d0414 | bellard | env->gdt.limit = sizeof(params->gdt_table) - 1; |
3316 | 330d0414 | bellard | |
3317 | 330d0414 | bellard | cpu_x86_load_seg(env, R_CS, KERNEL_CS); |
3318 | 330d0414 | bellard | cpu_x86_load_seg(env, R_DS, KERNEL_DS); |
3319 | 330d0414 | bellard | cpu_x86_load_seg(env, R_ES, KERNEL_DS); |
3320 | 330d0414 | bellard | cpu_x86_load_seg(env, R_SS, KERNEL_DS); |
3321 | 330d0414 | bellard | cpu_x86_load_seg(env, R_FS, KERNEL_DS); |
3322 | 330d0414 | bellard | cpu_x86_load_seg(env, R_GS, KERNEL_DS); |
3323 | 330d0414 | bellard | |
3324 | 330d0414 | bellard | env->eip = KERNEL_LOAD_ADDR; |
3325 | 330d0414 | bellard | env->regs[R_ESI] = KERNEL_PARAMS_ADDR; |
3326 | 330d0414 | bellard | env->eflags = 0x2;
|
3327 | 0824d6fc | bellard | |
3328 | 330d0414 | bellard | } else {
|
3329 | 330d0414 | bellard | char buf[1024]; |
3330 | 330d0414 | bellard | |
3331 | 330d0414 | bellard | /* RAW PC boot */
|
3332 | 330d0414 | bellard | |
3333 | 330d0414 | bellard | /* BIOS load */
|
3334 | 330d0414 | bellard | snprintf(buf, sizeof(buf), "%s/%s", interp_prefix, BIOS_FILENAME); |
3335 | 330d0414 | bellard | ret = load_image(buf, phys_ram_base + 0x000f0000);
|
3336 | 330d0414 | bellard | if (ret != 0x10000) { |
3337 | 330d0414 | bellard | fprintf(stderr, "vl: could not load PC bios '%s'\n", BIOS_FILENAME);
|
3338 | 330d0414 | bellard | exit(1);
|
3339 | 330d0414 | bellard | } |
3340 | 330d0414 | bellard | |
3341 | 330d0414 | bellard | /* VGA BIOS load */
|
3342 | 330d0414 | bellard | snprintf(buf, sizeof(buf), "%s/%s", interp_prefix, VGABIOS_FILENAME); |
3343 | 330d0414 | bellard | ret = load_image(buf, phys_ram_base + 0x000c0000);
|
3344 | 330d0414 | bellard | |
3345 | 330d0414 | bellard | /* setup basic memory access */
|
3346 | 330d0414 | bellard | env->cr[0] = 0x60000010; |
3347 | 330d0414 | bellard | cpu_x86_init_mmu(env); |
3348 | 330d0414 | bellard | |
3349 | 330d0414 | bellard | env->idt.limit = 0xffff;
|
3350 | 330d0414 | bellard | env->gdt.limit = 0xffff;
|
3351 | 330d0414 | bellard | env->ldt.limit = 0xffff;
|
3352 | 330d0414 | bellard | |
3353 | 330d0414 | bellard | /* not correct (CS base=0xffff0000) */
|
3354 | 330d0414 | bellard | cpu_x86_load_seg(env, R_CS, 0xf000);
|
3355 | 330d0414 | bellard | cpu_x86_load_seg(env, R_DS, 0);
|
3356 | 330d0414 | bellard | cpu_x86_load_seg(env, R_ES, 0);
|
3357 | 330d0414 | bellard | cpu_x86_load_seg(env, R_SS, 0);
|
3358 | 330d0414 | bellard | cpu_x86_load_seg(env, R_FS, 0);
|
3359 | 330d0414 | bellard | cpu_x86_load_seg(env, R_GS, 0);
|
3360 | 330d0414 | bellard | |
3361 | 330d0414 | bellard | env->eip = 0xfff0;
|
3362 | 330d0414 | bellard | env->regs[R_EDX] = 0x600; /* indicate P6 processor */ |
3363 | 330d0414 | bellard | |
3364 | 330d0414 | bellard | env->eflags = 0x2;
|
3365 | 330d0414 | bellard | |
3366 | 330d0414 | bellard | bochs_bios_init(); |
3367 | 0824d6fc | bellard | } |
3368 | 0824d6fc | bellard | |
3369 | 0824d6fc | bellard | /* init basic PC hardware */
|
3370 | fc01f7e7 | bellard | register_ioport_write(0x80, 1, ioport80_write, 1); |
3371 | 0824d6fc | bellard | |
3372 | fc01f7e7 | bellard | register_ioport_write(0x3d4, 2, vga_ioport_write, 1); |
3373 | 0824d6fc | bellard | |
3374 | 0824d6fc | bellard | cmos_init(); |
3375 | 0824d6fc | bellard | pic_init(); |
3376 | 0824d6fc | bellard | pit_init(); |
3377 | 0824d6fc | bellard | serial_init(); |
3378 | f1510b2c | bellard | ne2000_init(); |
3379 | fc01f7e7 | bellard | ide_init(); |
3380 | cd4c3e88 | bellard | kbd_init(); |
3381 | 0824d6fc | bellard | |
3382 | 0824d6fc | bellard | /* setup cpu signal handlers for MMU / self modifying code handling */
|
3383 | 0824d6fc | bellard | sigfillset(&act.sa_mask); |
3384 | 0824d6fc | bellard | act.sa_flags = SA_SIGINFO; |
3385 | 0824d6fc | bellard | act.sa_sigaction = host_segv_handler; |
3386 | 0824d6fc | bellard | sigaction(SIGSEGV, &act, NULL);
|
3387 | 0824d6fc | bellard | sigaction(SIGBUS, &act, NULL);
|
3388 | 0824d6fc | bellard | |
3389 | 0824d6fc | bellard | act.sa_sigaction = host_alarm_handler; |
3390 | 0824d6fc | bellard | sigaction(SIGALRM, &act, NULL);
|
3391 | 0824d6fc | bellard | |
3392 | 0824d6fc | bellard | itv.it_interval.tv_sec = 0;
|
3393 | 87858c89 | bellard | itv.it_interval.tv_usec = 1000;
|
3394 | 0824d6fc | bellard | itv.it_value.tv_sec = 0;
|
3395 | 0824d6fc | bellard | itv.it_value.tv_usec = 10 * 1000; |
3396 | 0824d6fc | bellard | setitimer(ITIMER_REAL, &itv, NULL);
|
3397 | 87858c89 | bellard | /* we probe the tick duration of the kernel to inform the user if
|
3398 | 87858c89 | bellard | the emulated kernel requested a too high timer frequency */
|
3399 | 87858c89 | bellard | getitimer(ITIMER_REAL, &itv); |
3400 | 87858c89 | bellard | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) / |
3401 | 87858c89 | bellard | 1000000;
|
3402 | b4608c04 | bellard | |
3403 | b4608c04 | bellard | if (use_gdbstub) {
|
3404 | b4608c04 | bellard | cpu_gdbstub(NULL, main_loop, gdbstub_port);
|
3405 | b4608c04 | bellard | } else {
|
3406 | b4608c04 | bellard | main_loop(NULL);
|
3407 | 0824d6fc | bellard | } |
3408 | 0824d6fc | bellard | return 0; |
3409 | 0824d6fc | bellard | } |