; 4B/5B Encoding Simulation
; written by Teresa Carrigan, 2004

globals [ from-number save-from-number from-5B-number start-x praise myDigits doneyet? 
          number-of-digits step current-bit last-invert from-list to-list ]
breeds [digit clock signal bit]
patches-own [ name ]
signal-own [ state ]
bit-own [ bit-number]


;  runs setup when program is first loaded
to startup
  setup
end

;  initializes variables
to setup
  locals [ n these-bits]
  ca
  set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ]
  populate-from-list
  populate-to-list
  set myDigits [ "0" "1" ]
  set from-number ""
  set from-5B-number ""

  ;  make a random 16-bit pattern
  repeat 16
    [ set from-number (word from-number random-one-of myDigits) ]
  set number-of-digits length from-number
  set save-from-number from-number
  
;    create explanation bars at top and bottom
    ask patches with [ pycor < -25 or pycor > 25]
        [ set pcolor blue ]
    ask patch-at 25 34
      [ set plabel "4B/5B Encoding" 
        set plabel-color white
        set name -1
      ]
    ask patch-at 25 28
      [ set plabel "number here"
        set plabel-color white
        set name -2
      ]
    ask patch-at 45 -30
        [ set plabel ""
          set plabel-color white
          set name 1
        ]
    ask patch-at 45 -35
        [ set plabel "" 
          set name 2
        ]
    
    explain -2 "Bits to be sent: " + add-spaces from-number
    set step 0
    set doneyet? false
    set last-invert "-"
end

to restart
  ask patches with [ pycor >= -25 and pycor <= 25]
    [ set pcolor black 
      set plabel ""
      set name ""
    ]
  ask patches with [ pycor < -25 ]
    [ set plabel "" ]
  ask bit [ die ]
  ask signal [ die ]
  ask clock [die ]
  ask digit [ die ]
  set from-number save-from-number
  set from-5B-number ""
  explain -2 "Bits to be sent: " + add-spaces from-number
  set step 0
  set doneyet? false
end

to explain [ which what ]
  ask patches with [ name = which ]
    [  set plabel what ]
end

to populate-from-list
  locals [ current ]
  set from-list [ "0000" ]
  set current "0000"
  while [ current != "1111" ]
    [ set current (get-next current) 
      set from-list lput current from-list 
    ]
end

to-report get-next [ current ]
  locals [ here-x addend carry total this-digit this-add result ]
  set here-x 3
  set addend "0001"
  set carry 0
  set result ""
  repeat 4
    [  set this-digit read-from-string item here-x current
       set this-add read-from-string item here-x addend
       set total carry + this-digit + this-add
       set carry 0
       if total > 1
         [  set total total - 2
            set carry 1
         ]
       set result (word total result)
       set here-x (here-x - 1)
    ]
  report result   
end

to populate-to-list
  set to-list [ "11110" "01001" "10100" "10101" "01010" "01011" "01110" "01111" 
                "10010" "10011" "10110" "10111" "11010" "11011" "11100" "11101" ]
end

to-report lookup [ what ]
  locals [  n]
  set n position what from-list
  report item n to-list
end

to update-5B [ current-from numbits ]
  locals [ current-to here-y ]
  set current-to lookup current-from
  repeat numbits
    [ set from-number but-first from-number ]
  explain 2 current-from + " maps to " + current-to
  set from-5B-number (from-5B-number + current-to)
  ;  make current-from ball
  ask patches with [ name = "Msg Bits" ]
    [ set here-y pycor ]
  ask patches with [ name = "unproc" ]
    [ set plabel from-number ]
  cct-digit 1
    [  set label current-from
       set label-color white
       setxy start-x here-y
    ]
  cct-digit 1
    [  set label current-to
       set label-color cyan
       setxy start-x (here-y - 15)
    ]
  set start-x (start-x + 25)
end

to setup-display
  locals [ here-y offset ]
  set start-x -44
  set here-y 15
  set offset 15
  ask patch-at start-x  here-y
    [ set plabel "Unprocessed Bits"
      set plabel-color yellow
    ]
  ask patch-at (start-x + 2 * (length from-number)) here-y
    [ set plabel from-number
      set plabel-color yellow
      set name "unproc"
    ]
  set here-y (here-y - offset)
  ask patch-at start-x here-y
    [  set plabel "Msg Bits"
       set name "Msg Bits"
       set plabel-color white
    ]
  set here-y (here-y - offset)
  ask patch-at start-x here-y
    [  set plabel "4B/5B Bits"
       set name "5B Bits"
       set plabel-color cyan
    ]
  set start-x (start-x + 25)
end

to take-down-display
  ask patches with [ pycor >= -25 and pycor <= 25]
    [ set pcolor black 
      set plabel ""
      set name ""
    ]
  ask digit [die ]
  explain -2 "4B/5B Bits to be sent: " + add-spaces from-5B-number

end

to step0
  locals [ current-from current-to ]
  explain 1 "First we convert each set of 4 bits to its 4B/5B pattern."
  if from-5B-number = ""
    [ setup-display ]
  ifelse  length from-number > 1
    [  set current-from substring from-number 0 2
       ifelse member? current-from from-list
         [  update-5B current-from 2 ]
         [  set current-from substring from-number 0 3
            ifelse member? current-from from-list
              [  update-5B current-from 3 ]
              [  ; assume 4 bit match will be found
                 set current-from substring from-number 0 4
                 update-5B current-from 4
              ]
         ]
    ]
    [  set step (step + 1)
    ]

end

to step1
  locals [ here-y offset ]
  take-down-display
  set start-x -64
  set here-y 20
  set offset 15
  ask patch-at start-x here-y
    [  set plabel "Bits"
       set name "Bits"
    ]
  ask patch-at start-x 0
    [  set plabel "Signal"
       set name "Signal"
    ]
  ask patch-at start-x (-1 * here-y)
    [ set plabel "Clock"
      set name "Clock"
    ]

  set start-x start-x + 8

  ask patch-at (start-x) (offset / 2)
    [ set plabel "+" ]
  ask patch-at (start-x) 0
    [ set plabel "0" ]
  ask patch-at (start-x) (-1 * offset / 2)
    [ set plabel "-" ]
    
  set start-x start-x + 8
  set step (step + 1)
end

to explain-encoding
     explain 1 "Use NRZI:  To send 1, transition in the middle of the time slice."
     explain 2 "To send 0, stay at same level for one time slice."
end

to step2
  locals [ here-x here-y k x offset]
  set offset 6
  set current-bit 0
  set here-y pycor-of random-one-of patches with [ name = "Signal" ]
  cct-signal 1
    [  set shape "circle"
       set color cyan
       setxy start-x here-y + 8
       set state "+"
       pd
    ]
  set here-y pycor-of random-one-of patches with [ name = "Clock" ]
  cct-clock 1
    [  set shape "circle"
       set color yellow
       setxy start-x here-y - 4
       pd
    ]
  set step (step + 1)
  set from-number from-5B-number
  explain-encoding
  
  ;  create bits along top row
  set here-y pycor-of random-one-of patches with [ name = "Bits" ]
  set k 0
  set here-x (start-x + offset / 2)
  while [ k < length from-number ]
    [ cct-bit 1
        [  set shape "box"
           set color black
           set label-color white
           set x item k from-number
           set label x
           set bit-number k
           setxy  here-x here-y
        ]
      set here-x (here-x + offset)
      set k (k + 1)
      wait slow-motion / 5
    ]
 
end

to show-current-bit
  ask bit with [ bit-number = current-bit ]
    [ set label-color cyan ]
end

to step3
  locals [ x here-y offset]
  set offset 6
  set here-y pycor-of random-one-of patches with [ name = "Bits" ]
  ifelse (not doneyet?) and (current-bit < length from-number)
    [
      show-current-bit
      ask clock [ pulse ]
      ask signal [ send-bit ]
      wait slow-motion
      set current-bit current-bit + 1
      set start-x start-x + offset
    ]
    [
      set doneyet? true
    ]
end

to send-bit
  locals [ x ]
  set x item current-bit from-number

  ; NRZI
  ifelse x = "0"
    [  set heading 90
       fd 6
    ]
    [  ;  x = "1"
       set heading 90
       fd 3
       ifelse state = "+"
         [ set heading 180 
           set state "-"
         ]
         [ set heading 0 
           set state "+"
         ]
       fd 16
       set heading 90
       fd 3
    ]
  
end


to pulse
  set heading 90
  fd 3
  left 90
  fd 8
  rt 90
  fd 3
  rt 90
  fd 8
end

;  do a single step (whichever comes next)
to one-step
  locals [  which-step]
  set which-step "step" + step
  if ( step < 8)
    [ run which-step ]
end


;  return randomly "0" or "1"
to-report random-bit
  locals [ ch ]
  ifelse random 100 < 50
    [ set ch "0" ]
    [ set ch "1" ]
  report ch
end

;  show remaining steps
to go
  locals [  which-step]
  set which-step "step" + step
  ifelse not doneyet? 
    [ run which-step
      set which-step "step" + step
      wait slow-motion
    ]
    [ stop
    ]
end

;  add a space every four bits, so the user won't make copy errors
to-report add-spaces [ number ]
  locals [ save k ]
  set save ""
  set k 0
  while [ (length number) > 0 ]
    [
      set save (word last number save )
      set number butlast number
      set k (k + 1)
      if (k = 4) and (length number > 0)
        [ set save (word " " save )
          set k 0
        ]
    ]
  set number save  
  report number
end

; *** NetLogo Model Copyright Notice ***
;
; Copyright 2004 by Teresa W. Carrigan.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from Teresa W. Carrigan.
; Contact Teresa W. Carrigan for appropriate licenses for redistribution 
; for profit.
;
; To refer to this model in academic publications, please use:
; Carrigan, T. (2004).  4B/5B Encoding Simulation model.
; Blackburn College, Carlinville IL.
;
; In other publications, please use:
; Copyright 2004 by Teresa W. Carrigan.  All rights reserved.  
;
; *** End of NetLogo Model Copyright Notice ***
@#$#@#$#@
GRAPHICS-WINDOW
3
10
665
341
81
37
4.0
1
18
1
1
1

CC-WINDOW
18
388
399
499
Command Center

BUTTON
3
344
86
377
NIL
setup
NIL
1
T
OBSERVER
T

SLIDER
370
345
543
378
slow-motion
slow-motion
0
1
0.3
0.1
1
seconds

BUTTON
87
344
169
377
step
one-step
NIL
1
T
OBSERVER
T

BUTTON
169
344
238
377
go
go
T
1
T
OBSERVER
T

BUTTON
239
344
366
377
show-again
restart
NIL
1
T
OBSERVER
T

@#$#@#$#@
WHAT IS IT?
-----------
This model demonstrates the 4B/5B transmission encoding method.  4B/5B is designed to limit the number of consecutive zeroes, and is used by 100Base-FX networks to achieve a 100Mbps data transmission rate.


HOW IT WORKS
------------
A random 16-bit pattern is generated. This is the data that we want to transmit on our 100Mbps network. Starting from the left-most bit, each set of 4 bits is replaced by its 5-bit 4B/5B equivalent. The 4B/5B encoding of the data is then transmitted over the network using NRZI transmission encoding. For each bit sent, both a synchronization clock pulse and the transmission waveform are displayed. 


HOW TO USE IT
-------------
The setup button generates a random bit pattern, and initializes variables.

The slow-motion slider is an easy way to adjust the speed of the display. Set it to zero if you want to show the final result as quickly as possible. 0.3 is a good setting for most purposes. 

The step button demonstrates the next step of the method, and then stops so you can take notes. The first few steps convert the next set of 4 bits into 4B/5B encoding. Once the full message is encoded, each step processes the next bit of the encoded message. Taking notes after each step is useful when you are first learning 4B/5B or NRZI. 

The go button processes the remaining bits, at a speed determined by the slow-motion slider.  This is useful when you do not need to take notes between each step, or do not wish to press the step button sixteen times to get an answer.  If you want to pause the demonstration, simply click the go button a second time and it will stop after it finishes the current step.  You may then click go a third time to resume.

The show-again button starts the exact same data bit pattern from the beginning. 

THINGS TO NOTICE
----------------
Some encoding schemes are prone to loss of synchronization due to too many time slices at the same voltage level. What is the greatest number of time slices that can be produced by 4B/5B-encoding? 

If the average voltage is not zero, then there is a DC component to the signal. This can cause signal distortion and possibly even damage equipment. Does 4B/5B have this problem? 

A magnetic disk can store more bits per inch if there are fewer transitions between high and low signal (positive and negative magnetic charge). How many transitions does 4B/5B usually have, compared to other encoding schemes?  Would this make a suitable encoding for use with magnetic disks, or should it be restricted to use with networks?


THINGS TO TRY
-------------
Set slow-motion to 0.3, click random, and then click go.

Set the encoding choice box to the type of encoding you want to drill, then click setup.  Attempt one bit at a time on paper, and then click the step button to check that you did that bit correctly.

EXTENDING THE MODEL
-------------------
Allow the user to input a starting bit pattern.

Modify to use 8B/10B or 8B/6T encoding.  These encodings allow even faster data transmission rates.

Modify so that the signal is generated first, and then translated into bits.

NETLOGO FEATURES
----------------
Extensive use is made of NetLogo commands substring and member? in translating the original data bit pattern to the RLL encoded bit pattern. 


RELATED MODELS
--------------
Transmission Encodings, Run-Length-Limited Encoding Simulation


CREDITS AND REFERENCES
----------------------
This model was written by Teresa W. Carrigan, 2004.  

Permission to use, modify or redistribute this model is hereby granted,
provided that both of the following requirements are followed:
	a) this copyright notice is included.
	b) this model will not be redistributed for profit without permission
	   from Teresa W. Carrigan.
Contact Teresa W. Carrigan for appropriate licenses for redistribution 
for profit.

To refer to this model in academic publications, please use: 
Carrigan, T. (2004). 4B/5B Encoding Simulation model.
Blackburn College, Carlinville, IL.

In other publications, please use: 
Copyright 2004 by Teresa W. Carrigan.  All rights reserved.  

FOR MORE INFORMATION
--------------------
For more information on 4B/5B Transmission Encoding, see:
Forouzan, B. "Data Communications and Networking", Third Edition, McGrawHill, pages 95-98; 348-349.


@#$#@#$#@
default
true
0
Polygon -7566196 true true 150 5 40 250 150 205 260 250

box
true
0
Polygon -7566196 true true 45 255 255 255 255 45 45 45

circle
false
0
Circle -7566196 true true 35 35 230

@#$#@#$#@
NetLogo 2.0.1
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
