; Run-Length-Limited Encoding Simulation
; written by Teresa Carrigan, 2004

globals [ from-number save-from-number from-RLL-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]
  ca
  set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ]
  set from-list [ "10" "11" "000" "010" "011" "0010" "0011" ]
  set to-list [ "0100" "1000" "000100" "100100" "001000" "00100100" "00001000" ]
  set myDigits [ "0" "1" ]
  set from-number ""
  set from-RLL-number ""

  ;  make a random 16-bit pattern
  while [ length from-number < 7 ]
    [ set from-number (word from-number random-one-of from-list) ]
  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 "Run-Length-Limited 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-RLL-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-report lookup [ what ]
  locals [  n]
  set n position what from-list
  report item n to-list
end

to update-RLL [ 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-RLL-number (from-RLL-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 + 25) 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 "RLL Bits"
       set name "RLL 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 "RLL Bits to be sent: " + add-spaces from-RLL-number

end

to step0
  locals [ current-from current-to ]
  explain 1 "First we convert the bits to RLL(2,7) patterns."
  if from-RLL-number = ""
    [ setup-display ]
  ifelse  length from-number > 1
    [  set current-from substring from-number 0 2
       ifelse member? current-from from-list
         [  update-RLL current-from 2 ]
         [  set current-from substring from-number 0 3
            ifelse member? current-from from-list
              [  update-RLL current-from 3 ]
              [  ; assume 4 bit match will be found
                 set current-from substring from-number 0 4
                 update-RLL 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 "RLL 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-RLL-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).  Run-Length-Limited Encoding Simulation
; 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
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 Run-Length-Limited encoding method.  RLL is designed to limit the number of consecutive zeroes, and is frequently used to store data on magnetic disks.


HOW IT WORKS
------------
A random bit pattern is generated, from seven to ten bits long.  This is the data that we want to store on the magnetic disk.  

Starting from the left-most bit, we find a bit pattern (two to four bits) that has an RLL encoding.  10 becomes 0100, 11 becomes 1000, 000 becomes 000100, 010 becomes 100100, 011 becomes 001000, 0010 becomes 00100100, and 0011 becomes 00001000.  This is repeated until the entire data bit pattern is encoded.  

The RLL encoding of the data is then sent to the magnetic disk 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 for the chosen encoding.

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 find the next set of data bits that can be encoded into an RLL bit pattern.  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 RLL 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 RLL-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 RLL 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 RLL usually have, compared to other encoding schemes?

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

Click setup.  Attempt one step at a time on paper, and then click the step button to check that you did that the step correctly.

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

Modify so that the signal is generated first, and then translated into the original data 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, 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). Run-Length-Limited Encoding Simulation
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 Run-Length-Limited Encoding, see:
Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition, Jones and Bartlett, pages 71-73. 

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