count EQU $1004 ; Put count in port B dir EQU $1003 ; Put dir in port C start: CLRA ; Set the current state to S0. output: STAA count ; Output current state # as count. JSR delay ; Delay so we can see what's happening loop: TST dir ; Test the "dir" input BEQ down ; Go down if dir=0 up: CMPA #5 ; Already 5? BEQ loop ; If so, no change. INCA ; Increment state number. BRA output ; Update output and repeat. down: TST count ; Check current count BEQ loop ; If count=0, keep it. DECA ; Decrement state number. BRA output ; Update output and repeat. delay: LDX #1000 dloop: DEX BNE dloop RTS