; Parity Error Detection Simulation ; written by Teresa Carrigan, 2004 globals [ from-number from-string number-of-digits start-x myDigits praise digits1 save-base base step error-detected?] breeds [ digit arrow ones ] arrow-own [ state ] patches-own [ name ] ; runs the program when it is first loaded to startup setup end ; initializes variables to setup locals [here-x current n] ca set praise ["Awesome!" "You got it!" "Right!" "Correct!" "Perfect!" "Bravo!" "Splendid!"] set digits1 [ "0" "1" ] set base 2 set save-base base set myDigits [] set n 0 set start-x 9 set here-x 7 set current 1 set n 0 repeat 2 [ set myDigits lput ( item n digits1 ) myDigits set n (n + 1) ] set n 1 set number-of-digits 8 ; initialize binary number set from-number "" repeat number-of-digits [ set from-number (word from-number (random-one-of myDigits)) ] ; create explanation bar at bottom ask patches with [ pycor < -3 ] [set pcolor blue] ask patch-at 6 -4 [ set plabel (word add-space from-number " was sent using " parity " parity.") set plabel-color white set name 1 ] ask patch-at 6 -5 [ set plabel-color white set plabel "Do we detect an error in transmission?" set name 2 ] set step 1 setup-bits from-number setup-arrow end to step1 explain 1 "Ignore the 0 bits." explain 2 "" ask digit with [ label = "1 " ] [ hatch 1 [ set breed ones set shape "circle" set color yellow set size 2 set label-color black ] ] ask ones [ set heading 180 repeat 4 [ fd 0.5 wait slow-motion ] ] set step step + 1 end to step2 ifelse count ones with [ color = yellow ] = 0 [ set step step + 1] [ ask arrow [ showturtle ifelse any? other-ones-here [ ask other-ones-here [ set color red ] if count ones with [color = red] = 2 [ explain 2 "Dropping a pair of ones." ask ones with [color = red] [ die ] wait slow-motion ] fd 1 ] [ explain 2 "" fd 1 ] ] ] end ; read the digits, storing it as a string ; adds a space in the number so it can be read easier to-report add-space [ 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 ; this is for the bottom explanation to explain [ which what ] ask patches with [ name = which ] [ set plabel what ] end ; setup the red arrow to setup-arrow locals [here-y ] ask random-one-of digit [ set here-y ycor ] ; create red arrow cct-arrow 1 [ hideturtle setxy start-x ( here-y - 2) set heading -90 set color red set shape "arrow" set size 2 set state 0 ] end ; setup the bits to setup-bits [ unsigned ] locals [ here-x here-y] set here-x ( start-x - 2) set here-y 2 repeat number-of-digits [ cct-digit 1 [ setxy here-x here-y set shape "circle" set color white set size 2 set label-color black set label (word (last unsigned) " ") ] ; end cct digits set unsigned but-last unsigned set here-x (here-x - 2) wait slow-motion ] end to step3 locals [ here-parity ] ask arrow [ hideturtle ] ifelse count ones = 0 [ set here-parity "even" explain 1 "No ones left, so parity is currently even." ] [ set here-parity "odd" explain 1 "A one left, so parity is currently odd." ] explain 2 "" wait slow-motion set step step + 1 end to step4 locals [ here-parity ] ifelse count ones = 0 [ set here-parity "even" ] [ set here-parity "odd" ask ones [ die ] ] ifelse here-parity = parity [ explain 2 "NO error is detected." set error-detected? false ] [ explain 2 "YES an error is detected." set error-detected? true ] wait slow-motion set step 10 end ; creates a random quiz quesiton and praise if it is right and says its wrong if it is to ask-question locals [ guess save-slow target line] set line (word add-space from-number " was sent using " parity " parity.\n") set line (word line "Do we detect an error in transmission?") set guess user-yes-or-no? line set save-slow slow-motion set slow-motion 0 while [ step < 10 ] [ go ] set slow-motion save-slow set target error-detected? ifelse guess = target [ user-message random-one-of praise ] [ set line "I'm sorry, but " ifelse target [ set line line + "YES an error was detected." ] [ set line line + "NO error was detected." ] user-message line ] end ; calls the quiz when the button is pressed to quiz locals [ save-slow ] set save-slow slow-motion set slow-motion 0 set parity random-one-of [ "even" "odd" ] without-interruption [setup ] wait .5 ask-question set slow-motion save-slow end ; flips the digits (called by the invert procedure) to flip if any? other-digit-here [ ask other-digit-here [ set color yellow ifelse first label = "0" [ set label "1 " ] [ set label "0 " ] ] ] end ; do whatever step comes next, then wait until user wants to continue to one-step locals [ which ] if step < 10 [ set which (word "step" step) run which ] end ; this is the go button, that when pressed, goes through the entire simulation to go ifelse step < 10 [ one-step wait slow-motion ] [ stop ] end to show-again locals [here-x current n] set start-x 9 set here-x 7 set current 1 set n 1 set number-of-digits 8 ask turtles [die] ask patches [ set plabel "" ] explain 1 (word add-space from-number " was sent using " parity " parity.") explain 2 "Do we detect an error in transmission?" set step 1 setup-bits from-number setup-arrow 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). Parity Error Detection 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 13 10 548 316 10 5 25.0 1 18 1 1 1 CC-WINDOW 27 459 564 629 Command Center BUTTON 13 318 76 351 NIL setup NIL 1 T OBSERVER T SLIDER 368 319 540 352 slow-motion slow-motion 0 1 0.5 0.1 1 NIL BUTTON 78 318 141 351 step one-step NIL 1 T OBSERVER T BUTTON 143 318 206 351 NIL go T 1 T OBSERVER T CHOICE 273 318 365 363 parity parity "even" "odd" 1 BUTTON 12 352 75 385 NIL quiz NIL 1 T OBSERVER T BUTTON 78 352 206 385 NIL show-again NIL 1 T OBSERVER T @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates the detection of single bit errors in bit patterns stored using either even or odd parity. HOW IT WORKS ------------ First, a random eight-bit binary number is generated. Any bit that is a 1 will hatch a yellow copy of itself. The red arrow then discards pairs of yellow 1 bits. If there are no yellow bits left over, the current parity is even; if there is a bit left over, the current parity is odd. Next, the current parity is compared to the parity used when the bit pattern was stored. If they match, then no error is detected. If they do not match, then an error was detected. HOW TO USE IT ------------- The setup button generates a random eight-bit binary number. The step button demonstrates the next step, and then stops so you can take notes. This is useful when you are first learning the method. The go button does every remaining step, at a speed determined by the slow-motion slider. This is useful when you do not need to take notes between each step. The show-again button starts the exact problem from the beginning. You may then click either the step button or the go button to see the same demonstration. The quiz button will generate a random bit pattern and ask the user if an error was detected, using a specified parity. 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.5 is a good setting for most purposes. The parity choice box specifies whether even or odd parity will be demonstrated. THINGS TO NOTICE ---------------- Any bits that are zero are always ignored. The number of bits does not change the parity either. The receiver must know not only whether the message was sent using even or odd parity, but also whether the parity bit is the first or last bit. This means that even if no error is detected, we cannot determine the data bits, because we are not told whether the parity bit was first or last. When an error is detected, there is no way to fix the error, other than to ask for the message to be sent again. THINGS TO TRY ------------- Set slow-motion to 0.5, click setup, and then click go. Set the parity to a type you wish to drill, and click setup. Work each step by hand, and then click the step button to check your answer. EXTENDING THE MODEL ------------------- Allow the user to input the starting bit pattern. NETLOGO FEATURES ---------------- one-step uses the NetLogo run command combined with a global integer variable step to run the next step, without needing nested ifelse blocks. without-interruption runs all the commands in the block without allowing other agents to "interrupt". That is, other agents are put "on hold" and do not execute any commands until the commands in the block are finished. RELATED MODELS -------------- Parity 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). Parity Error Detection 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 parity, see: Null, L. and Lobur, J. "Essentials of Computer Organization and Architecture", First Edition. Jones and Bartlett. page 65. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 arrow true 0 Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 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 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@