gvanem / Watt-32

Watt-32 TCP/IP library and samples.
https://www.watt-32.net/
18 stars 8 forks source link

Avoid 64-bit division in 8254 timer #110

Closed jwt27 closed 7 months ago

jwt27 commented 7 months ago

As I mentioned in #99, the 64-bit divisions can be avoided by using NTP format as intermediate between 8254-ticks and timeval. This can be implemented right now since microsec_clock() isn't used anywhere else. Also fixed is the ~134ms jump at midnight.

On my Pentium 3, the speed difference is not very substantial:

Before: avg. 2347-2388 cycles (3129-3184ns) per call
After : avg.      2195 cycles (     2927ns) per call

Only ~8% faster. But slower CPUs will see much more improvement.

Fun fact: the "before" number is variable because gcc does 64-bit division by shift-and-subtract, it gets a little slower for larger values. So it was fastest just after midnight.

jwt27 commented 7 months ago

About 22% faster with a 200 MHz cpu:

Before: avg. 963-1008 cycles (4815-5040ns) per call
After : avg.      789 cycles (     3945ns) per call

And it gets better the slower you go.

gvanem commented 7 months ago

Nice. Have you kept this slow PC since the 1990-ies or is it a VM or something?

PS. I have installed DOSBox-X and played around with a bit. But I'm struggling to get the built-in NE2000.COM packet-driver to work. This would a nice addition to the AppVeyor jobs if possible; a VM inside another VM (!).

jwt27 commented 7 months ago

Yeah I always kept at least one old PC around. And collected a few boxes full of parts over the years.

DOSBox-X needs winpcap, I think. I have used the NE2000 driver with it before but I can't seem to make it work now either.

gvanem commented 7 months ago

Merged. Thanks again!

Lethja commented 7 months ago

PS. I have installed DOSBox-X and played around with a bit. But I'm struggling to get the built-in NE2000.COM packet-driver to work. This would a nice addition to the AppVeyor jobs if possible; a VM inside another VM (!).

DOSBox-X needs winpcap, I think. I have used the NE2000 driver with it before but I can't seem to make it work now either.

Here's my working Dosbox-X configuration on a Linux host (automatically chosen when placed at ~/.config/dosbox-x/dosbox-x.conf):

Autostart

Disable file dialog when started without any arguments

[dosbox]
#  working directory option: Select an option for DOSBox-X's working directory when it runs.
#                              autoprompt: DOSBox-X will auto-decide whether to prompt for a working directory.
#                              config: DOSBox-X will use the primary config file directory as the working directory.
#                              custom: Specify a working directory via the "working directory default" option.
#                              default: Similar to autoprompt, but DOSBox-X will ask whether to save the selected folder.
#                              force: Similar to "custom", while overriding -defaultdir command-line option if used.
#                              noprompt: DOSBox-X uses the current directory and never prompts for a working directory.
#                              program: DOSBox-X will use the DOSBox-X program directory as the working directory.
#                              prompt: DOSBox-X will always ask the user to select a working directory when it runs.
#                              userconfig: DOSBox-X will use its user configuration directory as the working directory.
#                              Possible values: autoprompt, config, custom, default, force, noprompt, program, prompt, userconfig.
working directory option = noprompt

NE2000

This example is Linux but Windows is similar.

[ne2000]
#  ne2000: Enable NE2000 Ethernet emulation. Either pcap or slirp backend can be used, switchable via "backend" option.
#            Settings for the pcap and slirp backends can be found in the [ethernet, pcap] and [ethernet, slirp] sections.
#            Once properly set, load the NE2000 packet driver inside DOSBox-X with base address and interrupt specified below.
# nicbase: The base address of the NE2000 board.
#  nicirq: The interrupt it uses. Note serial2 uses IRQ3 as default.
# macaddr: The MAC address the emulator will use for its network adapter.
#            If you have multiple DOSBox-Xes running on the same network,
#            this has to be changed for each. AC:DE:48 is an address range reserved for
#            private use, so modify the last three number blocks, e.g. AC:DE:48:88:99:AB.
#            Default setting is 'random' which randomly choses a MAC address.
# backend: The backend (either pcap or slirp is supported) used for the NE2000 Ethernet emulation.
#            If set to "auto", then "slirp" is selected when available, otherwise "pcap" is selected when available.
#            NE2000 Ethernet emulation will be disabled if no backend is available (or the specified backend if unavailable).
#            Possible values: pcap, slirp, nothing, auto, none.
ne2000  = true
nicbase = 300
nicirq  = 9
macaddr = random
backend = pcap

[ethernet, pcap]
# realnic: Specifies which of your host network interfaces is used for pcap.
#            Write 'list' here to see the list of devices from the Help
#            menu ('List network interfaces') or from the Status Window.
#            Then make your choice and put either the interface number
#            (e.g. 2) or a part of your adapters name (e.g. VIA here).
# timeout: Specifies the read timeout for the device in milliseconds for the pcap backend, or the default value will be used.
realnic = enp4s0
timeout = default

[ethernet, slirp]
#    ipv4_network: The IPv4 network the guest and host services are on.
#    ipv4_netmask: The netmask for the IPv4 network.
#       ipv4_host: The address of the guest on the IPv4 network.
# ipv4_nameserver: The address of the nameserver service provided by the host on the IPv4 network.
# ipv4_dhcp_start: The start address used for DHCP by the host services on the IPv4 network.
#
# Advanced options (see full configuration reference file [dosbox-x.reference.full.conf] for more details):
# -> restricted; disable_host_loopback; mtu; mru; tcp_port_forwards; udp_port_forwards
#
ipv4_network    = 10.0.2.0
ipv4_netmask    = 255.255.255.0
ipv4_host       = 10.0.2.2
ipv4_nameserver = 10.0.2.3
ipv4_dhcp_start = 10.0.2.15

PCap on Linux

The user must be a member of the appropriate group (often called pcap or wireshark) and the executable binary (dosbox-x) needs to have pcap permissions set on it

# usermod -a -G pcap $(whoami)
# setcap cap_net_raw,cap_net_admin=eip $(which dosbox-x)`

In practice only Ethernet adapters support pcap (no wireless adapters)

Autoexec

You can write your autoexec entries directly in the configuration file

[autoexec]
# Lines in this section will be run at startup.
# You can put your MOUNT lines here.
@echo off
keyb dv
mount C /home/<REDACTED>/Vm/Dosbox-x
C:
set dosdir=C:\DOS
set path=%path%;%dosdir%\BIN
alias upx=%dosdir%\PROGS\UPX\UPX.EXE
%dosdir%\DRIVERS\CRYNWR\NE2000.COM 0x60
@echo on