module svaletlb title 'Output-Coded State Machine for Sunnyvale Traffic Lights' "SVALETLB device 'P16V8R'; " Input and output pins CLOCK, !OE pin 1, 11; NSCAR, EWCAR, TMSHORT, TMLONG pin 2, 3, 8, 9; NSRED, NSYELLOW, NSGREEN pin 19, 18, 17 istype 'reg'; EWRED, EWYELLOW, EWGREEN pin 16, 15, 14 istype 'reg'; TMRESET_L, XTRA pin 13, 12 istype 'reg'; " Definitions LSTATE = [NSRED,NSYELLOW,NSGREEN,EWRED,EWYELLOW,EWGREEN,XTRA]; " State vars NSGO = [ 0, 0, 1, 1, 0, 0, 0]; " States NSWAIT = [ 0, 1, 0, 1, 0, 0, 0]; NSWAIT2 = [ 0, 1, 0, 1, 0, 0, 1]; NSDELAY = [ 1, 0, 0, 1, 0, 0, 0]; EWGO = [ 1, 0, 0, 0, 0, 1, 0]; EWWAIT = [ 1, 0, 0, 0, 1, 0, 0]; EWWAIT2 = [ 1, 0, 0, 0, 1, 0, 1]; EWDELAY = [ 1, 0, 0, 1, 0, 0, 1]; state_diagram LSTATE state NSGO: " North-south green IF (!TMSHORT) THEN NSGO " Minimum green is 5 seconds. ELSE IF (TMLONG) THEN NSWAIT " Maximum green is 5 minutes. ELSE IF (EWCAR & !NSCAR) " If E-W car is waiting and no one THEN NSGO " is coming N-S, make E-W wait! ELSE IF (EWCAR & NSCAR) " Cars coming in both directions? THEN NSWAIT " Thrash! ELSE IF (!NSCAR) " Nobody coming N-S and not timed out? THEN NSGO " Keep N-S green. ELSE NSWAIT; " Else let E-W have it. state NSWAIT: GOTO NSWAIT2; " Yellow light is on for two ticks for safety. state NSWAIT2: GOTO NSDELAY; " (Drivers go 70 mph to catch this turkey green!) state NSDELAY: GOTO EWGO; " Red in both directions for added safety. state EWGO: " East-west green; states defined analogous to N-S IF (!TMSHORT) THEN EWGO ELSE IF (TMLONG) THEN EWWAIT ELSE IF (NSCAR & !EWCAR) THEN EWGO ELSE IF (NSCAR & EWCAR) THEN EWWAIT ELSE IF (!EWCAR) THEN EWGO ELSE EWWAIT; state EWWAIT: GOTO EWWAIT2; state EWWAIT2: GOTO EWDELAY; state EWDELAY: GOTO NSGO; equations LSTATE.CLK = CLOCK; TMRESET_L.CLK = CLOCK; !TMRESET_L := (LSTATE == NSWAIT2) " Reset the timer when going into + (LSTATE == EWWAIT2); " state NSDELAY or state EWDELAY. end svaletlb