Network operations - CPU specific

The file NETCODE.FTH contains code that will show significant benefit from being coded, or is sensitive to the byte order of the underlying CPU.

The code in this file is used by PowerNet v3 onwards, and should only be compiled once if both PowerNet and NETBOOT.FTH are both compiled.

Network order (big-endian) operations

Note that these functions have to be capable of fetching 32 bit cells from 16 bit aligned addresses, not just from 32 bit aligned addresses.

Note also that these routines assume a byte-addressed CPU.

: w@(n)         \ addr -- u16
Network order 16 bit fetch.

: w!(n)         \ u16 addr --
Network order 16 bit store. Must write MSB first if writing to some hardware

: @(n)          \ addr -- u32
Network order 32 bit fetch.

: !(n)          \ u32 addr --
Network order 32 bit store.

: w,(n)         \ w --
Network order W,

: ,(n)          \ x --
Network order version of , (comma).

: w,(n)         \ w --
Network order W,

: ,(n)          \ x --
Network order version of , (comma).

Internet checksum

Because this code is used so much in the system, it is worth coding it for maximum efficiency. One of the following equates may be defined (any value) in the control file to compile CPU specific code for the Internet checksum:

CPU=Cortex

Cortex v7, e.g. M3, but not Cortex-M0.

CPU=ARM

All ARM variants using the 32 bit ISA

CPU=H8/300H

Hitachi H8/300H and H8S

CPU=68K

Motorola 68xxx CPUs

CPU=386

Intel 386 and above (i32)

If no CPU specific equate is defined, the high level code will be used.

If the checksum words have been defined previously, this section of code is ignored.

[defined] CPU=Cortex [if]
If this equate is defined (any value will do), a Cortex (not M0) machine code IP checksum routine will be used.

code (cksum)    \ cksum addr len -- cksum'
ARM IP Checksum routine. This word is safe against alignment issues as the network order word fetch is done as two byte fetches.

[defined] CPU=ARM [if]
If this equate is defined (any value will do), an ARM machine code IP checksum routine will be used.

code (cksum)    \ cksum addr len -- cksum'
ARM IP Checksum routine. This word is safe against alignment issues as the network order word fetch is done as two byte fetches.

[defined] CPU=H8/300H [if]
If this equate is defined (any value will do), an H8/300H or H8S machine code IP checksum routine will be used.

[defined] CPU=68K [defined] CPU=Coldfire  or [if]
If this equate is defined (any value will do), a 68xxx/Coldfire machine code IP checksum routine will be used.

[defined] CPU=386 [if]
If this equate is defined (any value will do), a i386+ machine code IP checksum routine will be used. N.B. This code has not been tested!

: (cksum)       \ cksum addr len -- cksum'
Compute the partial internet checksum of a memory area. This is a high level implementation that can be used by any CPU. The word (CKSUM) is useful when calculating header checksums, and avoids the need to create the pseudoheaders described in the TCP/IP literature.

: cksum         \ addr len -- cksum ; computed as 16 bit ints
Compute the internet checksum of a memory area. This a high level implementation that can be used by any CPU.

STRUCT /pseudohdr       \ -- size
The pseudoheader structure for checksum calculation. This word is for illustration and is not compiled.

: PHcksum       \ *hdr len ipsrc ipdest ipproto -- cksum
A generic TCP/UDP checksum function. The parameters supplied are used to form a 'pseudoheader' from which the acual checksum is generated or checked.