--- vl.c.orig	Wed May  3 15:32:58 2006
+++ vl.c	Fri May  5 22:24:31 2006
@@ -43,7 +43,8 @@
 #include <netdb.h>
 #ifdef _BSD
 #include <sys/stat.h>
-#ifndef __APPLE__
+#include <net/if.h>
+#if !defined(__APPLE__) && !defined(__OpenBSD__)
 #include <libutil.h>
 #endif
 #else
@@ -130,6 +131,7 @@ QEMUTimer *gui_timer;
 int vm_running;
 int rtc_utc = 1;
 int cirrus_vga_enabled = 1;
+int nic_pcnet = 0;
 #ifdef TARGET_SPARC
 int graphic_width = 1024;
 int graphic_height = 768;
@@ -292,7 +294,7 @@ void isa_unassign_ioport(int start, int 
 
 /***********************************************************/
 
-void pstrcpy(char *buf, int buf_size, const char *str)
+void pstrcpy(char *buf, size_t buf_size, const char *str)
 {
     int c;
     char *q = buf;
@@ -310,7 +312,7 @@ void pstrcpy(char *buf, int buf_size, co
 }
 
 /* strcat and truncate. */
-char *pstrcat(char *buf, int buf_size, const char *s)
+char *pstrcat(char *buf, size_t buf_size, const char *s)
 {
     int len;
     len = strlen(buf);
@@ -561,7 +563,23 @@ int64_t cpu_get_real_ticks(void)
 }
 
 #else
-#error unsupported CPU
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
+int64_t cpu_get_real_ticks(void)
+{
+    struct timeval tv;
+    static int64_t i = 0;
+    int64_t j;
+
+    gettimeofday(&tv, NULL);
+    do {
+	j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+    } while (i == j);
+    i = j;
+    return j;
+}
 #endif
 
 static int64_t cpu_ticks_prev;
@@ -2201,7 +2219,7 @@ static int parse_macaddr(uint8_t *macadd
     return 0;
 }
 
-static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
+static int get_str_sep(char *buf, size_t buf_size, const char **pp, int sep)
 {
     const char *p, *p1;
     int len;
@@ -2560,11 +2578,85 @@ static int tap_open(char *ifname, int if
     char *dev;
     struct stat s;
 
+    /* If the device was specified on the command line, use it */
+    if (ifname[0]) {
+	fd = open(ifname, O_RDWR);
+	if (fd < 0) {
+	   fprintf(stderr, "warning: could not open %s: no virtual network emulation\n", ifname);
+	     return -1;
+	}
+    } else {
+#ifdef __OpenBSD__
+    struct ifreq ifr;
+    int i = 0, enoentcount = 0, err = 0, sock;
+    char dname[100], iname[100];
+
+    bzero(&ifr, sizeof(ifr));
+    if (ifname != NULL && ifname[0] != '\0') {
+        snprintf(dname, sizeof(dname), "/dev/%s", ifname);
+        strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+        fd = open(dname, O_RDWR);
+    } else {
+        for (; i != -1; i++) {
+    	    snprintf(dname, sizeof dname, "/dev/tun%d", i);
+            bzero(&ifr.ifr_name, sizeof(ifr.ifr_name));
+	    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", i);
+	    fd = open(dname, O_RDWR);
+	    if (fd >= 0)
+	        break;
+	    else if (errno != ENOENT || ++enoentcount > 3) {
+                if (errno != EBUSY) {
+                    err = errno;
+                    break;
+                }
+            } else
+                err = errno;
+        }
+    }
+    if (fd < 0) {
+	fprintf(stderr, "warning: could not open %s (%s): no virtual "
+	    "network emulation\n", dname, strerror(err));
+	return -1;
+    }
+
+    /* Set the tunnel device operation mode */
+    if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
+        close(fd);
+        return -1;
+    }
+
+    /* Get interface flags */
+    if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
+        close(fd);
+        close(sock);
+        return -1;
+    }
+
+    /* Set interface mode */
+    ifr.ifr_flags &= ~IFF_UP;
+    ifr.ifr_flags |= IFF_LINK0;
+    if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
+        close(fd);
+        close(sock);
+        return -1;
+    }
+
+    /* Bring interface up */
+    ifr.ifr_flags |= IFF_UP;
+    if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
+        close(fd);
+        close(sock);
+        return -1;
+    }
+
+#else
     fd = open("/dev/tap", O_RDWR);
     if (fd < 0) {
         fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
         return -1;
     }
+#endif
+    }
 
     fstat(fd, &s);
     dev = devname(s.st_rdev, S_IFCHR);
@@ -3065,7 +3157,8 @@ static int net_socket_mcast_init(VLANSta
 
 }
 
-static int get_param_value(char *buf, int buf_size,
+static int get_param_value(char *, size_t, const char *, const char *);
+static int get_param_value(char *buf, size_t buf_size,
                            const char *tag, const char *str)
 {
     const char *p;
@@ -3190,17 +3283,21 @@ int net_client_init(const char *str)
         char ifname[64];
         char setup_script[1024];
         int fd;
+	bzero(&ifname,sizeof(ifname));
+	bzero(&setup_script,sizeof(setup_script));
         if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
             fd = strtol(buf, NULL, 0);
             ret = -1;
             if (net_tap_fd_init(vlan, fd))
                 ret = 0;
         } else {
-            get_param_value(ifname, sizeof(ifname), "ifname", p);
             if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
                 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
             }
-            ret = net_tap_init(vlan, ifname, setup_script);
+            if (get_param_value(ifname, sizeof(ifname), "ifname", p) == 0)
+		ret = net_tap_init(vlan, NULL, setup_script);
+	    else
+            	ret = net_tap_init(vlan, ifname, setup_script);
         }
     } else
 #endif
@@ -4623,6 +4720,7 @@ void help(void)
 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
            "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
 #endif
+           "-nic-pcnet     simulate an AMD PC-Net PCI ethernet adaptor\n"
            "\n"
            "Network options:\n"
            "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
@@ -4735,6 +4833,7 @@ enum {
     QEMU_OPTION_audio_help,
     QEMU_OPTION_soundhw,
 #endif
+    QEMU_OPTION_nic_pcnet,
 
     QEMU_OPTION_net,
     QEMU_OPTION_tftp,
@@ -4841,6 +4940,7 @@ const QEMUOption qemu_options[] = {
     
     /* temporary options */
     { "usb", 0, QEMU_OPTION_usb },
+    { "nic-pcnet", 0, QEMU_OPTION_nic_pcnet },
     { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
     { NULL },
 };
@@ -5101,7 +5201,7 @@ int main(int argc, char **argv)
         serial_devices[i][0] = '\0';
     serial_device_index = 0;
     
-    pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
+    pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null");
     for(i = 1; i < MAX_PARALLEL_PORTS; i++)
         parallel_devices[i][0] = '\0';
     parallel_device_index = 0;
@@ -5261,6 +5361,9 @@ int main(int argc, char **argv)
                         optarg);
                 nb_net_clients++;
                 break;
+            case QEMU_OPTION_nic_pcnet:
+		nic_pcnet = 1;
+		break;
 #ifdef CONFIG_SLIRP
             case QEMU_OPTION_tftp:
 		tftp_prefix = optarg;
