Skip to content

Instantly share code, notes, and snippets.

@DarkGuardsman
Created January 24, 2019 23:30
Show Gist options
  • Save DarkGuardsman/69175d07881c55b60aa95592fba4b9e4 to your computer and use it in GitHub Desktop.
Save DarkGuardsman/69175d07881c55b60aa95592fba4b9e4 to your computer and use it in GitHub Desktop.
Traffic lights for little man computer
https://en.wikipedia.org/wiki/Little_man_computer
################################
# Notes for reading this program
####
# I think in OOP style so this program will not visually look like a
# linear flow of logic. It, however, does flow in a linear way but jumps around a lot.
# At the bottom of the program is all the data, above that is what I call functions.
# These functions are reusable or single instruction sets that are called in different
# places in the program. All of these functions return to the start of the program
# Where the program then checks what is our current condition before moving forward.
# The logic behind the repeat branch checks is to allow the lights to change on the external without
# effecting the program's logic if was rebooted mid run. I know this was not needed
# but it makes the code run more effective in my mind. As well it theory this would allow
# and operator on the outside of the box to set the starting condition. Without having it
# assume that the start is always EW green.
#
### Logic Flow ###
# start -> BRANCHES -> ACTION -> LOOP delay -> start
#
### Usage ###
# Input the starting condition
# IF invalid it will assume EW green
# On turn lane change input turn condition (0, 5, 10, 15)
# IF invalid it will assume 0
#
##################################
################################
#Load data from input for signal start position
############################################
IN
STO lights
#################################
#start of branching statements
#Each statement calls logic at the bottom
#Then returns to the start label
#################################
start LDA lights ;Load light data, store after each switch
#IF EastWestGreen
SUB rrgr ;subtract number for zero check
BRZ eastWestYellow ;Switch lights EW to yellow
ADD rrgr ;restore by adding what was subtracted
#IF EastWestYellow
SUB rryr ;subtract number for zero check
BRZ switchns ;Move to detech turn logic
ADD rryr ;restore by adding what was subtracted
#IF North South Green
SUB ggrr ;subtract number for zero check
BRZ northSouthYellow ;Switch NS yellow
ADD ggrr ;restore by adding what was subtracted
#IF North South Yellow
SUB yyrr ;subtract number for zero check
BRZ eastWestGreen ;Switch EW green
ADD yyrr ;restore by adding what was subtracted
#IF bothTurnsGreen
SUB rrrg ;subtract number for zero check
BRZ bothTurnsYellow ;Switch Both turns yellow
ADD rrrg ;restore by adding what was subtracted
#IF BothTurnsyellow
SUB rrry ;subtract number for zero check
BRZ northSouthGreen ;Switch NS green only, no turns
ADD rrry ;restore by adding what was subtracted
#IF northTurnGreen
SUB brrg ;subtract number for zero check
BRZ northTurnYellow ;Switch north turn to yellow
ADD brrg ;restore by adding what was subtracted
#IF northTurnYellow
SUB brry ;subtract number for zero check
BRZ northSouthGreen ;switch NS to green only, no turns
ADD brry ;restore by adding what was subtracted
#IF southTurnGreen
SUB rbrg ;subtract number for zero check
BRZ southTurnYellow ;Switch south turn to yellow
ADD rbrg ;restore by adding what was subtracted
#IF southTurnYellow
SUB rbry ;subtract number for zero check
BRZ northSouthGreen ;Switch NS to green only, no turns
ADD rbry ;restore by adding what was subtracted
BR eastWestGreen ;Invalid data was entered set EW to green as start condition
###################################################################################
### Bellow are basicly thought of as functions (public void action() in java)
### Reused to reduce # of lines of code needed, as well simplify thought process
###################################################################################
#store, Out, loop 3, back to start
outLoop3 STO lights
OUT
LDA two ;load two so we loop 3
BR loopZero
#store, Out, loop 6, back to start
outLoop6 STO lights ;save light value
OUT ;output light value
LDA five ;load give so we loop 6
BR loopZero
#counts down to zero
loopZero SUB one ;subtract one
BRP loopZero ;loop if zero ore more
BR start ;return to start if zero
#Out ggrr
northSouthGreen LDA ggrr
BR outLoop6
#Out rrgr
eastWestGreen LDA rrgr
BR outLoop6
#OUT rrrg
bothTurnsGreen LDA rrrg
BR outLoop3
#OUT brrg
northTurnGreen LDA brrg
BR outLoop3
#OUT rbrg
southTurnGreen LDA rbrg
BR outLoop3
#Out rrry
bothTurnsYellow LDA rrry
BR outLoop3
#OUT brry
northTurnYellow LDA brry
BR outLoop3
#OUT rbry
southTurnYellow LDA rbry
BR outLoop3
#OUT yyrr
northSouthYellow LDA yyrr
BR outLoop3
#OUT rryr
eastWestYellow LDA rryr
BR outLoop3
#############################
##start of ggrr switch, Load signals
############################
switchns IN
###South turn lane
SUB five
BRZ southTurnGreen ;input was 5
###North turn lane
SUB five
BRZ northTurnGreen ;input was 10
###Both turn lanes
SUB five
BRZ bothTurnsGreen ;input was 15
BR northSouthGreen ;input was 0
##################################
#Data
##################################
lights DAT 0
############################
#constants
################################
one DAT 1
two DAT 1
three DAT 3
five DAT 5
##############################
#constants signals
##################################
rrgr DAT 251 ;East West Green
rryr DAT 247 ;East, West Yellow
ggrr DAT 175 ;North South Green
yyrr DAT 95 ;North, South yellow
rbrg DAT 206 ;South, South turn green
rbry DAT 205 ;South, South turn yellow
brrg DAT 62 ;North, North Turn green
brry DAT 61 ;North, North turn yellow
rrrg DAT 254 ;Both Turns Green
rrry DAT 253 ;Both Turns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment