summaryrefslogtreecommitdiff
path: root/doc/rfc/rfc1936.txt
blob: 479fe37059aa7e5422bbe43720cc78810fb225ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
Network Working Group                                           J. Touch
Request For Comments: 1936                                     B. Parham
Category: Informational                                              ISI
                                                              April 1996


             Implementing the Internet Checksum in Hardware

Status of This Memo

   This memo provides information for the Internet community.  This memo
   does not specify an Internet standard of any kind.  Distribution of
   this memo is unlimited.

Abstract

   This memo presents a techniques for efficiently implementing the
   Internet Checksum in hardware. It includes PLD code for programming a
   single, low cost part to perform checksumming at 1.26 Gbps.

Introduction

   The Internet Checksum is used in various Internet protocols to check
   for data corruption in headers (e.g., IP) [4] and packet bodies (e.g,
   UDP, TCP) [5][6]. Efficient software implementation of this checksum
   has been addressed in previous RFCs [1][2][3][7].

   Efficient software implementations of the Internet Checksum algorithm
   are often embedded in data copying operations ([1], Section 2). This
   copy operation is increasingly being performed by dedicated direct
   memory access (DMA) hardware. As a result, DMA hardware designs are
   beginning to incorporate dedicated hardware to compute the Internet
   Checksum during the data transfer.

   This note presents the architecture of an efficient, pipelined
   Internet Checksum mechanism, suitable for inclusion in DMA hardware
   [8]. This design can be implemented in a relatively inexpensive
   programmable logic device (PLD) (1995 cost of $40), and is capable of
   supporting 1.26 Gbps transfer rates, at 26 ns per 32-bit word.
   Appendix A provides the pseudocode for such a device. This design has
   been implemented in the PC-ATOMIC host interface hardware [8]. We
   believe this design is of general use to the Internet community.









Touch & Parham               Informational                      [Page 1]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


   The remainder of this document is organized as follows:

            Review of the Internet Checksum
            One's Complement vs. Two's Complement Addition
            Interfaces
            Summary
            Appendix A - PLD source code












































Touch & Parham               Informational                      [Page 2]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


A Review of the Internet Checksum

   The Internet Checksum is used for detecting corruption in a block of
   data [1]. It is initialized to zero, and computed as the complement
   of the ones-complement sum of the data, taken in 16-bit units. A
   subsequent checksum of the data and checksum together should generate
   a zero checksum if no errors are detected.

   The checksum allows [1]:

            - byte order "independence"
                    reordered output is equivalent to reordered input
            - 16-bit word-order independence
                    reordering 16-bit words preserves the output
            - incremental computation
            - deferred carries
            - parallel summation
                    a result of deferred carries, incremental
                    computation, and 16-bit word order independence

   This note describes an implementation that computes two partial
   checksums in parallel, over the odd and even 16-bit half-words of
   32-bit data. The result is a pair of partial checksums (odd and
   even), which can be combined, and the result inverted to generate the
   true Internet Checksum. This technique is related to the long-word
   parallel summation used in efficient software implementations [1].

            +------------------+     +------------------+
            |  high half-word  |     |  low half-word   |
            | ones-complement  |     | ones-complement  |
            | partial checksum |     | partial checksum |
            +------------------+     +------------------+
                                \   /
                                  * (ones-complement sum)
                                  |
                         +------------------+
                         | partial checksum |
                         +------------------+
                                  |
                                  * (ones-complement negative)
                                  |
                        +-------------------+
                        |       final       |
                        | Internet Checksum |
                        +-------------------+






Touch & Parham               Informational                      [Page 3]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


One's Complement vs. Two's Complement Addition

   The Internet Checksum is composed of a ones-complement lookahead
   adder and a bit-wise inverter. A ones-complement adder can be built
   either using twos-complement components, or natively.

   A twos-complement implementation of a ones-complement adder requires
   either two twos-complement adders, or two cycles per add. The sum is
   performed, then the high-bit carry-out is propagated to the carry-in,
   and a second sum is performed. (ones-complement addition is {+1s} and
   twos-complement is {+2s})

            a {+1s} b == (a {+2s} b) + carry(a {+2s} b)

            e.g.,
                    halfword16 a,b;
                    word32 c;
                    a {+1s} b == r
            such that:
                    c = a {+2s} b;                          # sum value
                    r = (c & 0xFFFF) {+2s} (c >> 16);       # sum carry

   Bits of a twos-complement lookahead adder are progressively more
   complex in carry lookahead. (OR the contents of each row, where terms
   are AND'd or XOR'd {^})

            4-bit carry-lookahead 2's complement adder:
                    a,b : input data
                    p   : carry propagate, where pi = ai*bi = (ai)(bi)
                    g   : carry generate, where gi = ai + bi

            Out0 := a0 ^ b0 ^ ci

            Out1 := a1 ^ b1 ^ (cip0     + g0)

            Out2 := a2 ^ b2 ^ (cip0p1   + g0p1   + g1)

            Out3 := a3 ^ b3 ^ (cip0p1p2 + g0p1p2 + g1p2 + g2)

            Cout := cip0p1p2p3 + g0p1p2p3 + g1p2p3 + g2p3 + g3

   The true ones-complement lookahead adder recognizes that the carry-
   wrap of the twos-complement addition is equivalent to a toroidal
   carry-lookahead. Bits of a ones-complement lookahead adder are all
   the same complexity, that of the high-bit of a twos-complement
   lookahead adder. Thus the ones-complement sum (and thus the Internet
   Checksum) is bit-position independent. We replace `ci' with the `co'
   expression and reduce. (OR terms in each row pair).



Touch & Parham               Informational                      [Page 4]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


            4-bit carry-lookahead 1's complement ring adder:

            Out0 = a0 ^ b0 ^ (g3       + g2p3     + g1p2p3   + g0p1p2p3)

            Out1 = a1 ^ b1 ^ (g3p0     + g2p3p0   + g1p2p3p0 + g0)

            Out2 = a2 ^ b2 ^ (g3p0p1   + g2p3p0p1 + g1       + g0p1)

            Out3 = a3 ^ b3 ^ (g3p0p1p2 + g2       + g1p2     + g0p1p2)

   A hardware implementation can use this toroidal design directly,
   together with conventional twos-complement fast-adder internal
   components, to perform a pipelined ones-complement adder [8].

   A VLSI implementation could use any full-lookahead adder, adapted to
   be toroidal and bit-equivalent, as above. In our PLD implementation,
   we implement the adders via 2- and 3-bit full-lookahead sub-
   components. The adder components are chained in a ring via carry bit
   registers.  This relies on delayed carry-propagation to implement a
   carry pipeline between the fast-adder stages.

            Full-lookahead adders in a toroidal pipeline

         +-+-+-+   +-+-+-+   +-+-+   +-+-+-+   +-+-+-+   +-+-+
         |i|i|i|   |i|i|i|   |i|i|   |i|i|i|   |i|i|i|   |i|i|
         |F|E|D|   |C|B|A|   |9|8|   |7|6|5|   |4|3|2|   |1|0|
         +-+-+-+   +-+-+-+   +-+-+   +-+-+-+   +-+-+-+   +-+-+
           "+"       "+"      "+"      "+"       "+"      "+"
         +-+-+-+   +-+-+-+   +-+-+   +-+-+-+   +-+-+-+   +-+-+
         |s|s|s|   |s|s|s|   |s|s|   |s|s|s|   |s|s|s|   |s|s|
         |F|E|D|   |C|B|A|   |9|8|   |7|6|5|   |4|3|2|   |1|0|
         +-+-+-+   +-+-+-+   +-+-+   +-+-+-+   +-+-+-+   +-+-+
         v     |   v     |   v   |   v     |   v     |   v   |   +--+
         |     ^   |     ^   |   ^   |     ^   |     ^   |   ^   v  |
         |      +-+       +-+     +-+       +-+       +-+     +-+   |
         |      |c|       |c|     |c|       |c|       |c|     |c|   |
         |      |5|       |4|     |3|       |2|       |1|     |0|   |
         |      +-+       +-+     +-+       +-+       +-+     +-+   |
         +----------------------------------------------------------+

   Implementation of fast-adders in PLD hardware is currently limited to
   3-bits, because an i-bit adder requires 4+2^i product terms, and
   current PLDs support only 16 product terms.  The resulting device
   takes at most 5 "idle" clock periods for the carries to propagate
   through the accumulation pipeline.






Touch & Parham               Informational                      [Page 5]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


Interfaces

   The above device has been installed in a VL-Bus PC host interface
   card [8]. It has a hardware and software interface, defined as
   follows.

 Hardware Interface

   The Internet Checksum hardware appears as a single-port 32-bit
   register, with clock and control signals [8]:

                   +----------------------+
            CLR--->|                      |
            OE---->|  32-bit register as  |
            CLK--->|  2 adjacent 16-bit   |<---/---> 32-bit data bus
            ICLK-->| ones-complement sums |
            ADD--->|                      |
                   +----------------------+

            CLR    = zero the register
            OE     = write the register onto the data bus
            CLK    = clock to cycle the pipeline operation
            ICLK   = input data latch clock
            ADD    = initiating an add of latched input data

   CLR causes the contents of the checksum register and input latch to
   be zeroed. There is no explicit load; a CLR followed by a write of
   the load value to a dummy location is equivalent.

   The OE causes the register to be written to the data bus, or tri-
   stated.

   The CLK causes the pipeline to operate. If no new input data is
   latched to be added (via ICLK, ADD), a virtual "zero" is summed into
   the register, to permit the pipeline to empty.

   The ICLK (transparently) latches the value on the data bus to be
   latched internally, to be summed into the accumulator on the next ADD
   signal. The ADD signal causes the latched input data (ICLK) to be
   accumulated into the checksum pipeline. ADD and ICLK are commonly
   tied together. One 32-bit data value can be latched and accumulated
   into the pipeline adder every 26-ns clock, assuming data is stable
   when the ADD/ICLK signal occurs.

   The internal 32-bit register is organized as two 16-bit ones-
   complement sums, over the even and odd 16-bit words of the data
   stream. To compute the Internet Checksum from this quantity, ones-
   complement add the halves together, and invert the result.



Touch & Parham               Informational                      [Page 6]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


 Software Interface

   The device is used as a memory-mapped register. The register is read
   by performing a read on its equivalent memory location.

   The device is controlled via an external memory-mapped register. Bits
   in this control register clear the device (set/clear the CLR line),
   and enable and disable the device (set/clear the ADD line). The CLR
   line can alternatively be mapped to a memory write, e.g., such that
   reading the location is a non-destructive read of the checksum
   register, and a write of any value clears the checksum register. The
   enable/disable control must be stored in an external register.

   The device is designed to operate in background during memory
   transfers (either DMA or programmed I/O). Once enabled, all transfers
   across that bus are summed into the checksum register. The checksum
   is available 5 clocks after the last enabled data accumulation. This
   delay is often hidden by memory access mechanisms and bus
   arbitration.  If required, "stall" instructions can be executed for
   the appropriate delay.

   For the following example, we assume that the device is located at
   CKSUMLOC. We assume that reading that location reads the checksum
   register, and writing any value to that location clears the register.
   The control register is located at CTLLOC, and the checksum
   enable/disable bit is CKSUMBIT, where 1 is enabled, and 0 is
   disabled.  To perform a checksum, a programmer would clear the
   register, (optionally initialize the checksum), initiate a series of
   transfers, and use the result:

            /******* initialization *******/
            *(CTLLOC) &= ~((ctlsize)(CKSUMBIT));     /* disable sum */
            (word32)(*(CKSUMLOC)) = 0;               /* clear reg   */
            *(CTLLOC) |= CKSUMBIT;                   /* enable sum  */
            { (optional) write initial value to a dummy location }

            /***** perform a transfer *****/
            { do one or more DMA or PIO transfers - read or write }

            /***** gather the results *****/
            *(CTLLOC) &= ~((ctlsize)(CKSUMBIT));     /* disable sum  */
            sum = (word32)(*(CKSUMLOC));             /* read sum     */
            sum = (sum & 0xFFFF) + (sum >> 16);      /* fold halves  */
            sum = (sum & 0xFFFF) + (sum >> 16);      /* add in carry */
            ipcksum = (halfword16)(~(sum & 0xFFFF)); /* 1's negative */






Touch & Parham               Informational                      [Page 7]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


Summary

   This note describes the design of a hardware Internet Checksum that
   can be implemented in an inexpensive PLD, achieving 1.26 Gbps. This
   design has been implemented in the PC-ATOMIC host interface hardware
   [8]. We believe this design is of general use to the Internet
   community.

Security Considerations

   Security considerations are not addressed here. The Internet Checksum
   is not intended as a security measure.

Acknowledgements

   The authors would like to thank the members of the "High-Performance
   Computing and Communications", notably Mike Carlton, and "Advanced
   Systems" Divisions at ISI for their assistance in the development of
   the hardware, and this memo.

References

   [1] Braden, R., Borman, D., and Partridge, C., "Computing the
       Internet Checksum," Network Working Group RFC-1071, ISI, Cray
       Research, and BBN Labs, Sept. 1988.

   [2] Mallory, T., and Kullberg, A., "Incremental Updating of the
       Internet Checksum," Network Working Group RFC-1141, BBN Comm.,
       Jan. 1990.

   [3] Plummer, W., "TCP Checksum Function Design," IEN-45, BBN, 1978,
       included as an appendix in RFC-1071.

   [4] Postel, Jon, "Internet Protocol," Network Working Group RFC-
       791/STD-5, ISI, Sept. 1981.

   [5] Postel, Jon, "User Datagram Protocol," Network Working Group
       RFC-768/STD-6, ISI, Aug. 1980.

   [6] Postel, Jon, "Transmission Control Protocol," Network Working
       Group RFC-793/STD-7, ISI, Sept. 1981.

   [7] Rijsinghani, A., "Computation of the Internet Checksum via
       Incremental Update," Network Working Group RFC-1624, Digital
       Equipment Corp., May 1994.

   [8] Touch, J., "PC-ATOMIC", ISI Tech. Report. SR-95-407, June 1995.




Touch & Parham               Informational                      [Page 8]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


Authors' Addresses

   Joe Touch
   University of Southern California/Information Sciences Institute
   4676 Admiralty Way
   Marina del Rey, CA 90292-6695
   USA
   Phone: +1 310-822-1511 x151
   Fax:   +1 310-823-6714
   URL:   http://www.isi.edu/~touch
   EMail: touch@isi.edu


   Bruce Parham
   University of Southern California/Information Sciences Institute
   4676 Admiralty Way
   Marina del Rey, CA 90292-6695
   USA
   Phone: +1 310-822-1511 x101
   Fax:   +1 310-823-6714
   EMail: bparham@isi.edu






























Touch & Parham               Informational                      [Page 9]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


Appendix A: PLD source code

The following is the PLD source code for an AMD MACH-435 PLD. The
MACH-435 is composed of 8 22V10-equivalent PLD blocks, connected by a
configurable internal matrix.


---- (PLD source code follows) ----

TITLE    PC-ATOMIC IP Sum Accelerator - 1-clock 2- and 3-bit 26 ns version
PATTERN  ip_sum
REVISION 1.01
AUTHOR   J. Touch & B. Parham
COMPANY  USC/ISI
DATE     06/21/94

CHIP    ip_sum          MACH435

; accumulates in 1 clock (1 level of logic)
;
; resources allocated to reduce fitting time
;
; uses an input register "dl" to latch the data bus values on rising edge
; accumulates a hi/lo ones-complement sum in register "q"
; the input and output are accessed via bidirectional pins "dq"
;
; uses 2 groups of 6 carry bit registers "cy"
;
; use 3-bit full-adders with carry lookahead (settles in 6 clocks)
; group 16 bits as      [000102 030405 0607 080910 111213 1415]
;                       [161718 192021 2223 242526 272829 3031]
;
; locking the pins down speeds up fitting and is designed to force
; 4-bit components into single "segments" of the PLD.
; we could have indicated the same thing via:
;       GROUP MACH_SEG_A        dq[6..0]
;       GROUP MACH_SEG_B        dq[14..8]
;       GROUP MACH_SEG_C        dq[22..16]
;       GROUP MACH_SEG_D        dq[30..24]

;
; control pins:
;
PIN     20      clk             ; adder clock
PIN     62      ip_add          ; add current data to sum
PIN     83      ip_sum_ena      ; output current sum
PIN     41      ip_clr          ; clear current sum
PIN 23  ip_dclk                 ; input data latch (tied to clk, or not)



Touch & Parham               Informational                     [Page 10]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; dq are data bus pins
; dl is the input register
;
PIN     [9..3]          dq[6..0] IPAIR dl[6..0]         ; IO port
PIN     [18..12]        dq[14..8] IPAIR dl[14..8]       ; IO port
PIN     [30..24]        dq[22..16] IPAIR dl[22..16]     ; IO port
PIN     [39..33]        dq[30..24] IPAIR dl[30..24]     ; IO port
PIN     ?       dq[31,23,15,7] IPAIR dl[31,23,15,7]     ; IO port

;
; q  is the partial checksum register
; dl is the input register
; dq are the data bus pins
;
NODE    ?       q[31..0] OPAIR dq[31..0]        ; internal data in reg
NODE    ?       dl[31..0] REG                   ; input reg

;
; cy are the carry register bits
;
NODE    ?       cy[31,29,26,23,21,18,15,13,10,7,5,2] REG
                                                ;1-bit internal carry bits

EQUATIONS

;
; .trst is the tri-state control, 0 means these are always inputs
;
ip_add.trst             = 0
ip_clr.trst             = 0
ip_sum_ena.trst         = 0

;
; grab data to the input register on every clock (irrelevant if invalid)
;
dl[31..0].clkf          = ip_dclk       ; grab data all the time
        ; don't use setf, rstf, or trst for dl
        ; we want dl to map to input registers, not internal cells
        ; besides, input registers don't need setf, rstf, or trst

;
; control of the checksum register
;
dq[31..0].clkf          = clk                   ; clk clocks everything
dq[31..0].setf          = gnd                   ; never preset registers
dq[31..0].rstf          = ip_clr                ; clear on reset
dq[31..0].trst          = ip_sum_ena            ; ena outputs sum - read



Touch & Parham               Informational                     [Page 11]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; control for the carry register
;
cy[31,29,26,23,21,18,15,13,10,7,5,2].clkf       = clk
cy[31,29,26,23,21,18,15,13,10,7,5,2].setf       = gnd    ; never preset
cy[31,29,26,23,21,18,15,13,10,7,5,2].rstf       = ip_clr ; clear on reset

;
; INPUT DATA LATCH
; nothing fancy here - grab all inputs when ip_add signal is high
; i.e., grab data in input register
;
dl[31..0]             := dq[31..0]

;
; COMBINATORIAL ADDER
;
; built as a series of 2-bit and 3-bit (carry-lookahead) full-adders
; with carries sent to the carry register "pipeline"
;
; sum[n] are sum bits
; cy[m] are carry bits
; ":+:" is XOR

;
; SUM[0] = (A0 :+: B0 :+: CARRY_IN)
;
; CY[0] = ((A0 * B0) + ((A0 :+: B0) * CARRY_IN))
;
; actually, the latter can be rewritten as
;
; CY[0] = ((A0 * B0) + ((A0 + B0) * CARRY_IN))
;
; because the XOR won't be invalidated by the AND case, since the
; result is always 1 from the first term then anyway
; this helps reduce the number of XOR terms required, which are
; a limited resource in PLDs
;













Touch & Parham               Informational                     [Page 12]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


; SUM THE LOW-ORDER WORD
;

;
; the first 5 bits [0..4] of the low-order word
;
q[0]    := (q[0] :+: (ip_add * dl[0]) :+: cy[15])

q[1]    := (q[1] :+: (ip_add * dl[1]) :+:
        ((ip_add *
                (q[0] * dl[0] +
                dl[0] * cy[15])) +
        (q[0] * cy[15])))

q[2]    := (q[2] :+: (ip_add * dl[2]) :+:
        ((ip_add *
                (q[1] * dl[1] +
                 q[1] *  q[0] *  dl[0] +
                dl[1] *  q[0] *  dl[0] +
                 q[1] * dl[0] * cy[15] +
                dl[1] * dl[0] * cy[15] +
                dl[1] *  q[0] * cy[15])) +
        (q[1] * q[0] * cy[15])))

cy[2]   := ((ip_add *
                (q[2] * dl[2] +
                 q[2] *  q[1] * dl[1] +
                dl[2] *  q[1] * dl[1] +
                 q[2] *  q[1] *  q[0] *  dl[0] +
                 q[2] * dl[1] *  q[0] *  dl[0] +
                dl[2] *  q[1] *  q[0] *  dl[0] +
                dl[2] * dl[1] *  q[0] *  dl[0] +
                 q[2] *  q[1] * dl[0] * cy[15] +
                 q[2] * dl[1] *  q[0] * cy[15] +
                 q[2] * dl[1] * dl[0] * cy[15] +
                dl[2] *  q[1] *  q[0] * cy[15] +
                dl[2] *  q[1] * dl[0] * cy[15] +
                dl[2] * dl[1] *  q[0] * cy[15] +
                dl[2] * dl[1] * dl[0] * cy[15])) +
        (q[2] * q[1] * q[0] * cy[15]))

q[3]    := (q[3] :+: (ip_add * dl[3]) :+: cy[2])

q[4]    := (q[4] :+: (ip_add * dl[4]) :+:
        ((ip_add *
                (q[3] * dl[3] +
                dl[3] * cy[2])) +
        (q[3] * cy[2])))



Touch & Parham               Informational                     [Page 13]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; the next 3 bits [5..7] of the low-order word
;
q[5]    := (q[5] :+: (ip_add * dl[5]) :+:
        ((ip_add *
                (q[4] * dl[4] +
                 q[4] *  q[3] * dl[3] +
                dl[4] *  q[3] * dl[3] +
                 q[4] * dl[3] * cy[2] +
                dl[4] * dl[3] * cy[2] +
                dl[4] *  q[3] * cy[2])) +
        (q[4] * q[3] * cy[2])))

cy[5]   := ((ip_add * (
                 q[5] * dl[5] +
                 q[5] *  q[4] * dl[4] +
                dl[5] *  q[4] * dl[4] +
                 q[5] *  q[4] *  q[3] * dl[3] +
                 q[5] * dl[4] *  q[3] * dl[3] +
                dl[5] *  q[4] *  q[3] * dl[3] +
                dl[5] * dl[4] *  q[3] * dl[3] +
                 q[5] *  q[4] * dl[3] * cy[2] +
                 q[5] * dl[4] *  q[3] * cy[2] +
                 q[5] * dl[4] * dl[3] * cy[2] +
                dl[5] *  q[4] *  q[3] * cy[2] +
                dl[5] *  q[4] * dl[3] * cy[2] +
                dl[5] * dl[4] *  q[3] * cy[2] +
                dl[5] * dl[4] * dl[3] * cy[2])) +
        (q[5] * q[4] * q[3] * cy[2]))

q[6]    := (q[6] :+: (ip_add * dl[6]) :+: cy[5])

q[7]    := (q[7] :+: (ip_add * dl[7]) :+:
        ((ip_add *
                (q[6] * dl[6] +
                dl[6] * cy[5])) +
        (q[6] * cy[5])))

cy[7]   := ((ip_add *
                (q[7] * dl[7] +
                 q[7] *  q[6] * dl[6] +
                dl[7] *  q[6] * dl[6] +
                 q[7] * dl[6] * cy[5] +
                dl[7] * dl[6] * cy[5] +
                dl[7] *  q[6] * cy[5])) +
        (q[7] * q[6] * cy[5]))





Touch & Parham               Informational                     [Page 14]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; the next 5 bits [8..12] of the low-order word
;
q[8]    := (q[8] :+: (ip_add * dl[8]) :+: cy[7])

q[9]    := (q[9] :+: (ip_add * dl[9]) :+:
        ((ip_add *
                (q[8] * dl[8] +
                dl[8] * cy[7])) +
        (q[8] * cy[7])))

q[10]   := (q[10] :+: (ip_add * dl[10]) :+:
        ((ip_add *
                (q[9] * dl[9] +
                 q[9] *  q[8] * dl[8] +
                dl[9] *  q[8] * dl[8] +
                 q[9] * dl[8] * cy[7] +
                dl[9] * dl[8] * cy[7] +
                dl[9] *  q[8] * cy[7])) +
        (q[9] * q[8] * cy[7])))

cy[10]  := ((ip_add *
                (q[10] * dl[10] +
                 q[10] *  q[9] * dl[9] +
                dl[10] *  q[9] * dl[9] +
                 q[10] *  q[9] *  q[8] * dl[8] +
                 q[10] * dl[9] *  q[8] * dl[8] +
                dl[10] *  q[9] *  q[8] * dl[8] +
                dl[10] * dl[9] *  q[8] * dl[8] +
                 q[10] *  q[9] * dl[8] * cy[7] +
                 q[10] * dl[9] *  q[8] * cy[7] +
                 q[10] * dl[9] * dl[8] * cy[7] +
                dl[10] *  q[9] *  q[8] * cy[7] +
                dl[10] *  q[9] * dl[8] * cy[7] +
                dl[10] * dl[9] *  q[8] * cy[7] +
                dl[10] * dl[9] * dl[8] * cy[7])) +
        (q[10] * q[9] * q[8] * cy[7]))

q[11]   := (q[11] :+: (ip_add * dl[11]) :+: cy[10])

q[12]   := (q[12] :+: (ip_add * dl[12]) :+:
        ((ip_add *
                (q[11] * dl[11] +
                dl[11] * cy[10])) +
        (q[11] * cy[10])))






Touch & Parham               Informational                     [Page 15]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; the final 3 bits [13..15] of the low-order word
;
q[13]   := (q[13] :+: (ip_add * dl[13]) :+:
        ((ip_add *
                (q[12] * dl[12] +
                 q[12] *  q[11] * dl[11] +
                dl[12] *  q[11] * dl[11] +
                 q[12] * dl[11] * cy[10] +
                dl[12] * dl[11] * cy[10] +
                dl[12] *  q[11] * cy[10])) +
        (q[12] * q[11] * cy[10])))

cy[13]  := ((ip_add * (
                 q[13] * dl[13] +
                 q[13] *  q[12] * dl[12] +
                dl[13] *  q[12] * dl[12] +
                 q[13] *  q[12] *  q[11] * dl[11] +
                 q[13] * dl[12] *  q[11] * dl[11] +
                dl[13] *  q[12] *  q[11] * dl[11] +
                dl[13] * dl[12] *  q[11] * dl[11] +
                 q[13] *  q[12] * dl[11] * cy[10] +
                 q[13] * dl[12] *  q[11] * cy[10] +
                 q[13] * dl[12] * dl[11] * cy[10] +
                dl[13] *  q[12] *  q[11] * cy[10] +
                dl[13] *  q[12] * dl[11] * cy[10] +
                dl[13] * dl[12] *  q[11] * cy[10] +
                dl[13] * dl[12] * dl[11] * cy[10])) +
        (q[13] * q[12] *  q[11] * cy[10]))

q[14]   := (q[14] :+: (ip_add * dl[14]) :+: cy[13])

q[15]   := (q[15] :+: (ip_add * dl[15]) :+:
        ((ip_add *
                (q[14] * dl[14] +
                dl[14] * cy[13])) +
        (q[14] * cy[13])))

cy[15]  := ((ip_add *
                (q[15] * dl[15] +
                 q[15] *  q[14] * dl[14] +
                dl[15] *  q[14] * dl[14] +
                 q[15] * dl[14] * cy[13] +
                dl[15] * dl[14] * cy[13] +
                dl[15] *  q[14] * cy[13])) +
        (q[15] * q[14] * cy[13]))





Touch & Parham               Informational                     [Page 16]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


; SUM THE HIGH-ORDER WORD
;

;
; the first 5 bits [16..20] of the high-order word
;
q[16]   := (q[16] :+: (ip_add * dl[16]) :+: cy[31])

q[17]   := (q[17] :+: (ip_add * dl[17]) :+:
        ((ip_add *
                (q[16] * dl[16] +
                dl[16] * cy[31])) +
        (q[16] * cy[31])))

q[18]   := (q[18] :+: (ip_add * dl[18]) :+:
        ((ip_add *
                (q[17] * dl[17] +
                 q[17] *  q[16] * dl[16] +
                dl[17] *  q[16] * dl[16] +
                 q[17] * dl[16] * cy[31] +
                dl[17] * dl[16] * cy[31] +
                dl[17] *  q[16] * cy[31])) +
        (q[17] * q[16] * cy[31])))

cy[18]  := ((ip_add *
                (q[18] * dl[18] +
                 q[18] *  q[17] * dl[17] +
                dl[18] *  q[17] * dl[17] +
                 q[18] *  q[17] *  q[16] * dl[16] +
                 q[18] * dl[17] *  q[16] * dl[16] +
                dl[18] *  q[17] *  q[16] * dl[16] +
                dl[18] * dl[17] *  q[16] * dl[16] +
                 q[18] *  q[17] * dl[16] * cy[31] +
                 q[18] * dl[17] *  q[16] * cy[31] +
                 q[18] * dl[17] * dl[16] * cy[31] +
                dl[18] *  q[17] *  q[16] * cy[31] +
                dl[18] *  q[17] * dl[16] * cy[31] +
                dl[18] * dl[17] *  q[16] * cy[31] +
                dl[18] * dl[17] * dl[16] * cy[31])) +
        (q[18] * q[17] * q[16] * cy[31]))

q[19]   := (q[19] :+: (ip_add * dl[19]) :+: cy[18])

q[20]   := (q[20] :+: (ip_add * dl[20]) :+:
        ((ip_add *
                (q[19] * dl[19] +
                dl[19] * cy[18])) +
        (q[19] * cy[18])))



Touch & Parham               Informational                     [Page 17]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; the next 3 bits [21..23] of the high-order word
;
q[21]   := (q[21] :+: (ip_add * dl[21]) :+:
        ((ip_add *
                (q[20] * dl[20] +
                 q[20] *  q[19] * dl[19] +
                dl[20] *  q[19] * dl[19] +
                 q[20] * dl[19] * cy[18] +
                dl[20] * dl[19] * cy[18] +
                dl[20] *  q[19] * cy[18])) +
        (q[20] * q[19] * cy[18])))

cy[21]  := ((ip_add * (
                 q[21] * dl[21] +
                 q[21] *  q[20] * dl[20] +
                dl[21] *  q[20] * dl[20] +
                 q[21] *  q[20] *  q[19] * dl[19] +
                 q[21] * dl[20] *  q[19] * dl[19] +
                dl[21] *  q[20] *  q[19] * dl[19] +
                dl[21] * dl[20] *  q[19] * dl[19] +
                 q[21] *  q[20] * dl[19] * cy[18] +
                 q[21] * dl[20] *  q[19] * cy[18] +
                 q[21] * dl[20] * dl[19] * cy[18] +
                dl[21] *  q[20] *  q[19] * cy[18] +
                dl[21] *  q[20] * dl[19] * cy[18] +
                dl[21] * dl[20] *  q[19] * cy[18] +
                dl[21] * dl[20] * dl[19] * cy[18])) +
        (q[21] * q[20] * q[19] * cy[18]))

q[22]   := (q[22] :+: (ip_add * dl[22]) :+: cy[21])

q[23]   := (q[23] :+: (ip_add * dl[23]) :+:
        ((ip_add *
                (q[22] * dl[22] +
                dl[22] * cy[21])) +
        (q[22] * cy[21])))

cy[23]  := ((ip_add *
                (q[23] * dl[23] +
                 q[23] *  q[22] * dl[22] +
                dl[23] *  q[22] * dl[22] +
                 q[23] * dl[22] * cy[21] +
                dl[23] * dl[22] * cy[21] +
                dl[23] *  q[22] * cy[21])) +
        (q[23] * q[22] * cy[21]))





Touch & Parham               Informational                     [Page 18]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; the next 5 bits [24..28] of the high-order word
;
q[24]   := (q[24] :+: (ip_add * dl[24]) :+: cy[23])

q[25]   := (q[25] :+: (ip_add * dl[25]) :+:
        ((ip_add *
                (q[24] * dl[24] +
                dl[24] * cy[23])) +
        (q[24] * cy[23])))

q[26]   := (q[26] :+: (ip_add * dl[26]) :+:
        ((ip_add *
                (q[25] * dl[25] +
                 q[25] *  q[24] * dl[24] +
                dl[25] *  q[24] * dl[24] +
                 q[25] * dl[24] * cy[23] +
                dl[25] * dl[24] * cy[23] +
                dl[25] *  q[24] * cy[23])) +
        (q[25] * q[24] * cy[23])))

cy[26]  := ((ip_add *
                (q[26] * dl[26] +
                 q[26] *  q[25] * dl[25] +
                dl[26] *  q[25] * dl[25] +
                 q[26] *  q[25] *  q[24] * dl[24] +
                 q[26] * dl[25] *  q[24] * dl[24] +
                dl[26] *  q[25] *  q[24] * dl[24] +
                dl[26] * dl[25] *  q[24] * dl[24] +
                 q[26] *  q[25] * dl[24] * cy[23] +
                 q[26] * dl[25] *  q[24] * cy[23] +
                 q[26] * dl[25] * dl[24] * cy[23] +
                dl[26] *  q[25] *  q[24] * cy[23] +
                dl[26] *  q[25] * dl[24] * cy[23] +
                dl[26] * dl[25] *  q[24] * cy[23] +
                dl[26] * dl[25] * dl[24] * cy[23])) +
        (q[26] * q[25] * q[24] * cy[23]))

q[27]   := (q[27] :+: (ip_add * dl[27]) :+: cy[26])

q[28]   := (q[28] :+: (ip_add * dl[28]) :+:
        ((ip_add *
                (q[27] * dl[27] +
                dl[27] * cy[26])) +
        (q[27] * cy[26])))






Touch & Parham               Informational                     [Page 19]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; the final 3 bits [29..31] of the high-order word
;
q[29]   := (q[29] :+: (ip_add * dl[29]) :+:
        ((ip_add *
                (q[28] * dl[28] +
                 q[28] *  q[27] * dl[27] +
                dl[28] *  q[27] * dl[27] +
                 q[28] * dl[27] * cy[26] +
                dl[28] * dl[27] * cy[26] +
                dl[28] *  q[27] * cy[26])) +
        (q[28] * q[27] * cy[26])))

cy[29]  := ((ip_add * (
                 q[29] * dl[29] +
                 q[29] *  q[28] * dl[28] +
                dl[29] *  q[28] * dl[28] +
                 q[29] *  q[28] *  q[27] * dl[27] +
                 q[29] * dl[28] *  q[27] * dl[27] +
                dl[29] *  q[28] *  q[27] * dl[27] +
                dl[29] * dl[28] *  q[27] * dl[27] +
                 q[29] *  q[28] * dl[27] * cy[26] +
                 q[29] * dl[28] *  q[27] * cy[26] +
                 q[29] * dl[28] * dl[27] * cy[26] +
                dl[29] *  q[28] *  q[27] * cy[26] +
                dl[29] *  q[28] * dl[27] * cy[26] +
                dl[29] * dl[28] *  q[27] * cy[26] +
                dl[29] * dl[28] * dl[27] * cy[26])) +
        (q[29] * q[28] * q[27] * cy[26]))

q[30]   := (q[30] :+: (ip_add * dl[30]) :+: cy[29])

q[31]   := (q[31] :+: (ip_add * dl[31]) :+:
        ((ip_add *
                (q[30] * dl[30] +
                dl[30] * cy[29])) +
        (q[30] * cy[29])))

cy[31]  := ((ip_add *
                (q[31] * dl[31] +
                 q[31] *  q[30] * dl[30] +
                dl[31] *  q[30] * dl[30] +
                 q[31] * dl[30] * cy[29] +
                dl[31] * dl[30] * cy[29] +
                dl[31] *  q[30] * cy[29])) +
        (q[31] * q[30] * cy[29]))





Touch & Parham               Informational                     [Page 20]
^L
RFC 1936    Implementing the Internet Checksum in Hardware    April 1996


;
; output nodes onto output pins (pending enable..)
;
dq[0]   := {q[0]}
dq[1]   := {q[1]}
dq[2]   := {q[2]}
dq[3]   := {q[3]}
dq[4]   := {q[4]}
dq[5]   := {q[5]}
dq[6]   := {q[6]}
dq[7]   := {q[7]}
dq[8]   := {q[8]}
dq[9]   := {q[9]}
dq[10]  := {q[10]}
dq[11]  := {q[11]}
dq[12]  := {q[12]}
dq[13]  := {q[13]}
dq[14]  := {q[14]}
dq[15]  := {q[15]}

dq[16]  := {q[16]}
dq[17]  := {q[17]}
dq[18]  := {q[18]}
dq[19]  := {q[19]}
dq[20]  := {q[20]}
dq[21]  := {q[21]}
dq[22]  := {q[22]}
dq[23]  := {q[23]}
dq[24]  := {q[24]}
dq[25]  := {q[25]}
dq[26]  := {q[26]}
dq[27]  := {q[27]}
dq[28]  := {q[28]}
dq[29]  := {q[29]}
dq[30]  := {q[30]}
dq[31]  := {q[31]}

;
; end.
;











Touch & Parham               Informational                     [Page 21]
^L