; Transmission Encodings Simulation
; written by Teresa Carrigan, 2004

globals [ from-number start-x praise myDigits doneyet? number-of-digits step 
          encodingList abbrevList current-bit last-invert ]
breeds [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]
  ca
  set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ]
  set myDigits [ "0" "1" ]
  set encodingList [ "Unipolar" "Non-Return-To-Zero" "Non-Return-To-Zero-Invert" "Manchester" 
      "Return-To-Zero" "Alternate Mark Inversion" "Phase Modulation" "Frequency Modulation"
      "Modified Frequency Modulation"]
  set abbrevList [ "Unipolar" "NRZ" "NRZI" "Manchester" "RZ" "AMI" "PM" "FM" "MFM"]
  set number-of-digits 16
  set from-number ""
  ;  make a random 16-bit pattern
  repeat number-of-digits [
        set from-number (word from-number random 2)
      ]

;    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 encoding-phrase + " 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 1
    set doneyet? false
    set last-invert "-"
    step1
    step2
end

to change-encoding
  ask patches with [ pycor >= -25 and pycor <= 25]
    [ set pcolor black ]
  ask bit [ die ]
  ask signal [ die ]
  ask clock [die ]
  explain -1 encoding-phrase + " Encoding"
  explain -2 "Bits to be sent: " + add-spaces from-number
  set step 1
  set doneyet? false
  step1
  step2
end

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

to step1
  locals [ here-y offset ]
  set start-x -68
  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)
  
  explain-encoding
end

to explain-encoding1
  ;  NRZ
      explain 1 "NRZ:  To send 1, use a HIGH pulse for one time slice."
      explain 2 "To send 0, use a LOW pulse for one time slice."
end

to explain-encoding2
  ;  NRZI
     explain 1 "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 explain-encoding3
  ;  Manchester
         explain 1 "Manchester:  To send 1, transition UP in the middle."
         explain 2 "To send 0, transition DOWN in the middle of time slice."
end

to explain-encoding4
  ; RZ
    explain 1 "Return-To-Zero: To send 1, transition DOWN to zero in middle."
    explain 2 "To send 0, transition UP to zero in the middle."
end

to explain-encoding5
  ; AMI
    explain 1 "Alternate Mark Inversion: To send 0, send zero pulse."
    explain 2 "To send 1, send oppositive of last 1 sent."
end

to explain-encoding6
  ;  Manchester = PM
         explain 1 "Phase Modulation:  To send 1, transition UP in the middle."
         explain 2 "To send 0, transition DOWN in the middle of time slice."
end

to explain-encoding7
  ;  FM
  explain 1 "FM: To send 1, transition in the middle of the time slice."
  explain 2 "To send 0, no transition in the middle."
end

to explain-encoding8
  ;  MFM
  explain 1 "MFM: To send 1, transition in the middle of the time slice."
  explain 2 "To send 0, no transition in the middle."
end

to explain-encoding0
  ;  Unipolar
    explain 1 "Unipolar:  To send 1, use a HIGH pulse for one time slice."
    explain 2 "To send 0, use zero pulse for one time slice."
end

to explain-encoding
  locals [ n ]
  set n position encoding abbrevList
  run "explain-encoding" +  n
end

to step2
  locals [ here-x here-y k x offset]
  set offset 8
  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)
  ;  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 8
  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 [ n ]
  set n position encoding abbrevList
  run "send-bit" + n
end

to send-bit0
  locals [ x ]
  set x item current-bit from-number
  ; Unipolar
  ifelse x = "1"
    [  if state = "0"
          [ set heading 0
            fd 8
          ]
       set heading 90
       fd 8
       set state "+"
    ]
    [  ; x = 0
      if state = "+"
        [ set heading 180
          fd 8
        ]
      set heading 90
      fd 8
      set state "0"
    ]
end

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

  ; NRZ
  ifelse x = "1"
    [ if state = "-"
        [ set heading 0
          fd 16
          set state "+"
        ]
      set heading 90
      fd 8
    ]
    [ ;  x = "0"
      if state = "+"
        [ set heading 180
          fd 16
          set state "-"
        ]
      set heading 90
      fd 8
    ]
end

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

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

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

  ; Manchester
  ifelse x = "0"
    [  if state = "-"
          [ set heading 0
            fd 16
          ]
       set heading 90
       fd 4
       set heading 180
       fd 16
       set heading 90
       fd 4
       set state "-"
    ]
    [  ; x = "1"
      if state = "+"
          [ set heading 180
            fd 16
          ]
       set heading 90
       fd 4
       set heading 0
       fd 16
       set heading 90
       fd 4    
       set state "+"
    ]
end

to send-bit4
  locals [ x ]
  set x item current-bit from-number
  ;  RZ
  if state = "+"
    [
      set state "0"
      pu
      set heading 180
      fd 8
      pd
    ]
  ifelse x = "1"
    [  set heading 0
       fd 8
       rt 90
       fd 4
       rt 90
       fd 8
       set heading 90
       fd 4
    ]       
    [  ; x = 0
       set heading 180
       fd 8
       set heading 90
       fd 4
       set heading 0
       fd 8
       set heading 90
       fd 4
    ]
end

to send-bit5
  locals [ x ]
  set x item current-bit from-number
  ;  AMI
  if state = "+"
    [  set heading 180
       pu
       fd 8
       set state "0"
       pd
    ]
  ifelse x = "0"
    [  set heading 90
       fd 8
       set state "0"
    ]
    [  ; x = 1
        ifelse last-invert = "+"
          [  set heading 180
             fd 8
             set heading 90
             fd 8
             set heading 0
             fd 8
             set state "0"
             set last-invert "-"
          ]
          [  set heading 0
             fd 8
             set heading 90
             fd 8
             set heading 180
             fd 8
             set state "0"
             set last-invert "+"
          ]
    ]
end

;  PM = Manchester
to send-bit6
  send-bit3
end

;  FM
to send-bit7
  locals [ x ]
  set x item current-bit from-number
  ifelse x = "0"
    [  set heading 90
       fd 8
       ifelse state = "+"
         [  set heading 180
            fd 16
            set state "-"
         ]
         [  set heading 0
            fd 16
            set state "+"
         ]
    ]
    [  ; x = 1
       ifelse state = "+"
         [  set heading 90
            fd 4
            set heading 180
            fd 16
            set heading 90
            fd 4
            set heading 0
            fd 16
         ]
         [  set heading 90
            fd 4
            set heading 0
            fd 16
            set heading 90
            fd 4
            set heading 180
            fd 16
         ]
    ]
end

;  MFM
to send-bit8
  locals [ next-x x ]
  ifelse current-bit < 15
    [  set next-x item (current-bit + 1) from-number ]
    [  set next-x "1" ]
  set x item current-bit from-number
  ifelse x = "0"
    [  set heading 90
       fd 8
       if next-x = "0"
         ; only transition for consecutive zeroes
       [ifelse state = "+"
         [  set heading 180
            fd 16
            set state "-"
         ]
         [  set heading 0
            fd 16
            set state "+"
         ]
       ]
    ]
    [  ; x = 1
       ifelse state = "+"
         [  set heading 90
            fd 4
            set heading 180
            fd 16
            set heading 90
            fd 4
            set state "-"
         ]
         [  set heading 90
            fd 4
            set heading 0
            fd 16
            set heading 90
            fd 4
            set state "+"
         ]
    ]
end

to pulse
  set heading 90
  fd 4
  left 90
  fd 8
  rt 90
  fd 4
  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

to random-setup
  without-interruption [random-encoding]
  setup
end

to random-encoding
  set encoding random-one-of abbrevList
end

to-report encoding-phrase
  locals [ n ]
  set n position encoding abbrevList
  report item n encodingList
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).  Transmission Encodings 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
436
399
499
Command Center

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

SLIDER
151
379
324
412
slow-motion
slow-motion
0
1
0.3
0.1
1
seconds

BUTTON
172
345
254
378
step
one-step
NIL
1
T
OBSERVER
T

BUTTON
254
345
323
378
go
go
T
1
T
OBSERVER
T

CHOICE
452
345
664
390
encoding
encoding
"Unipolar" "NRZ" "NRZI" "Manchester" "RZ" "AMI" "PM" "FM" "MFM"
3

BUTTON
86
344
171
377
random
random-setup
NIL
1
T
OBSERVER
T

BUTTON
324
345
451
378
change encoding
change-encoding
NIL
1
T
OBSERVER
T

@#$#@#$#@
WHAT IS IT?
-----------
This model demonstrates various data transmission encodings: Unipolar, NRZ, NRZI, Manchester, RZ, AMI, PM, FM, MFM.


HOW IT WORKS
------------
A random 16-digit bit pattern is generated.  For each bit sent, both a synchronization clock pulse and the transmission waveform are displayed.  The transmission waveform will differ depending on the encoding chosen.


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

The random button selects a random encoding scheme, and then sets up a random bit pattern.

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 processes the next bit of the message to be sent.  It then stops so you can take notes.  This is useful when you are first learning an encoding method.

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 change encoding button resets the screen without changing the bit pattern.  This allows you to select a different encoding scheme for the same bit pattern.

The encoding choice box allows you to select the encoding scheme to be demonstrated.  Refer to a textbook or lecture notes for details on the various encoding schemes.

THINGS TO NOTICE
----------------
Some encoding schemes are prone to loss of synchronization.  That is, some bit patterns will produce an encoding where the signal stays at the same voltage level for multiple time slices.  Which encoding schemes might have this problem?  Which never do?

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.  Which encoding schemes might have this problem?  Which never do?

Which encoding schemes use the fewest number of transitions?  Which use the greatest?

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.

Add more encodings: Differential Manchester, BnZS, 2B1Q, MLT-3, 8B/10B, 8B/6T.

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

NETLOGO FEATURES
----------------
explain-encoding and send-bit use the NetLogo run command combined with an integer variable to run the appropriate procedures, without needing nested ifelse blocks.  

The go procedure uses "forever" on the button, and an ifelse in the procedure instead of a while loop.  When the while loop would have finished, the else branch is executed: "stop", which halts the forever execution of "go".

RELATED MODELS
--------------
Run-Length-Limited Encoding Simulation, 4B/5B 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). Transmission Encodings 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 Transmission Encodings, see one of the following textbooks:
[1]Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition, Jones and Bartlett, pages 67-73. 
[2] Forouzan, B. "Data Communications and Networking", Third Edition, McGrawHill, pages 89-94.

@#$#@#$#@
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
@#$#@#$#@
@#$#@#$#@
@#$#@#$#@
