       ORG    0100H
;
TRAIN  XRA    A      ;ONCE UPON A TIME,
       OUT    0FEH   ;IN A CURIOUS LITTLE
       LXI    H,SHED  ;PLACE THERE WAS -
       LXI    B,2048  ;..A TINY TRAIN
EMPTY  MVI    M,20H   ;..AND IT STAYED IN A
       INX    H       ;..TINY SHED
       DCX    B       ;..THAT WAS ALL EMPTY
       XRA    A
       XRA    B
       JNZ    EMPTY
       LXI    H,CLOUD   ;..EXCEPT FOR A HUGE
       LXI    D,1
       MVI    B,14   ;B
       CALL   SMO1    ;I
       LXI    D,43     ;L
       MVI    B,12      ;L
       CALL   SMO1       ;O
       LXI    D,48        ;W
       MVI    B,9          ;Y
       CALL   SMO1
       LXI    D,52           ;C
       MVI    B,4             ;L
       CALL   SMO1             ;O
       LXI    D,58              ;U
       MVI    B,2                ;D
       CALL   SMO1
       LXI    D,61                 ;O
       MVI    B,1                   ;F
       CALL   SMO1
       LXI    D,62                    ;S
       MVI    B,1                      ;M
       CALL   SMO1                      ;O
       JMP    ENGIN                      ;K
;                                         ;E
SMO1   DAD    D
SMO2   MVI    M,SMOKE  ;..COUGH
       INX    H
       DCR    B             ;..COUGH
       JNZ    SMO2
       RET           ;COMING OUT OF THE STACK
;                    ;OF
ENGIN  LXI    H,SHED+322H  ;A TINY LOCOMOTIVE
       MVI    M,16H   ;WITH A LITTLE SMOKESTACK
       LXI    H,SHED+325H  ;AND A LITTLE BELL
       MVI    M,07H
       LXI    H,SHED+327H  ;..AND A TINY DOME
       MVI    M,6EH
       LHLD   CAB1       ;..AND A CAB
       SHLD   SHED+32AH  ;WITH WINDOWS
       LHLD   CAB2       ;..SO THAT YOU COULD
       SHLD   SHED+32CH  ;SEE INTO WHERE THE
       LHLD   CAB3       ;..ENGINEER AND THE
       SHLD   SHED+32EH  ;FIREMAN SAT.
       LHLD   BOI1       ;..IT HAD A BEAUTIFUL
       SHLD   SHED+361H  ;POLISHED
       LHLD   BOI2       ;..BRASS
       SHLD   SHED+363H  ;BOILER
       LHLD   BOI3       ;..WITH
       SHLD   SHED+365H  ;THE NUMBER
       SHLD   SHED+367H  ;"99" ON THE
       LHLD   BOI4       ;..SIDE OF
       SHLD   SHED+369H  ;THE CAB
       LHLD   BOI5       ;..BUT YOU COULDN'T
       SHLD   SHED+36BH  ;SEE EITHER THE
       LHLD   BOI6       ;..ENGINEER OR THE
       SHLD   SHED+36DH  ;FIREMAN
       LHLD   FRA1       ;..THE LITTLE ENGINE
       SHLD   SHED+3A0H  ;ALSO
       LHLD   FRA2       ;..HAD A COWCATCHER
       SHLD   SHED+3A2H  ;AND LOTS OF FUNNY
       SHLD   SHED+3A4H  ;WHEELS AND THE
       SHLD   SHED+3A6H  ;THINGS THAT CONNECTED
       LHLD   FRA3       ;..AND TWO VERY TINY
       SHLD   SHED+3A8H  ;WHEELS AT THE VERY
       LHLD   FRA4       ;..VERY BACK
       SHLD   SHED+3AAH  ;ALTOGETHER IT LOOKED
       LHLD   FRA5       ;..QUITE LONELY AND YET
       SHLD   SHED+3ACH  ;IT APPEARED VERY
       LXI    B,-21H     ;..FUNNY JUST SITTING
       LXI    H,SHED+3DEH  ;THERE ON THE
RAILS  MVI    M,19H   ;..RAILS
       INX    B       ;..WITH NOTHING AT ALL
       INX    H       ;..EVER
       XRA    A       ;..TO DO
       XRA    B       ;..  W E L L !
       JNZ    RAILS   ;..LET'S RUN IT JUST FOR
;                         ;FUN
HOSTL  LXI    H,RRY  ;GET TRAIN OUT OF SHED
       XCHG          ;MOVE IT FROM SHED
       LXI    H,SHED
FIRE   MOV    A,M    ;LIGHT FIRE
       INX    H
       XCHG
       MOV    M,A    ;MOVE IT
       INX    H
       MOV    A,H
       XCHG
       CPI    0D0H   ;END OF YARD?
       JNZ    FIRE   ;NO, MAKE MORE STEAM!
       CALL   TRAVL  ;GO TAKE TRIP
       JMP    HOSTL  ;NO ROUND TRIPS, JUST DO
;                    ;AGAIN
;
TRAVL  LXI    B,64*13  ;MAKE TRIP 13 MILES
CHOO   PUSH   B        ;CHUFF ONCE
       CALL   STROK    ;MAKE FORWARD MOTION
       CALL   TURN     ;TURN WHEELS
       POP    B
       DCX    B        ;CLICK ODOMETER
       XRA    A
       XRA    B        ;13 MILES YET?
       JNZ    CHOO     ;NO
       RET             ;DO NEXT TRIP
;
STROK  LXI    H,0CC01H  ;MOV DOWN THE TRACK
       MVI    C,0D0H
COAL   MOV    A,M      ;ADD MORE COAL
       DCX    H
       MOV    M,A
       INX    H
       INX    H
       MOV    A,H
       CMP    C        ;ENOUGH COAL?
       JNZ    COAL     ;NO, PUT MORE ON!
       RET
;
TURN   LXI    H,0CC00H  ;TURN WHEELS
HISS   MOV    A,M
       INX    H
       CPI    6        ;FIND COWCATCHER
       JNZ    HISS     ;LOOK AGAIN.
FOUND  INX    H
AXLE   MOV    A,M
       CPI    11H      ;WHAT QUARTER TURN
       JNZ    NXT1
       LXI    D,147EH  ;NEXT QUARTER TURN
;
AXL1   MVI    B,7     ;DO 4 AXLES
AXL2   MOV    M,D     ;WHEELS MOVED HERE
       DCR    B       ;LAST WHEEL?
       JZ     WORK    ;I'VE BEEN WORKIN'...
       INX    H      ;..ON THE RAILROAD..
       MOV    M,E    ;(SIDE RODS MOVED HERE)
       INX    H      ;..ALL THE LIVE..
       DCR    B      ;..LONG DAY.....
       JNZ    AXL2
;
NXT1   CPI    14H     ;QUARTER TURN
       JNZ    NXT2
       LXI    D,132DH
       JMP    AXL1
NXT2   CPI    13H     ;QUARTER TURN
       JNZ    NXT3
       LXI    D,125FH
       JMP    AXL1
NXT3   CPI    12H     ;QUARTER TURN
       JNZ    AXLE
       LXI    D,112DH
       JMP    AXL1
;
WORK   LXI    B,5300
OIL    DCX    B
       IN     0FAH    ;DID THE CONDUCTOR
       ANI    RDA    ;..FLAG US DOWN
       IN     0FCH   ;CLEAR INPUT PORT
       JZ     QUIT   ;QUITTIN' TIME
       XRA    A      ;NO,    THEN HIGHBALL
       XRA    B
       JNZ    OIL    ;NO SQUEEKS, PLEASE!
       RET
;
;    PUT A JUMP TO WHERE EVER YOU NEED TO
;    TO RETURN IN THE NEXT STATION.
QUIT   JMP    0C004H ;YOUR RETURN TICKET HERE
;
RDA    EQU    01H    ;PUT YOUR DAV FLAG HERE
RRY    EQU    0CC00H   ;VDM RAM
SMOKE  EQU    6FH      ;SMOKE CHARACTOR
CAB1   DW     1001H    ;CAB DESCRIPTION
CAB2   DW     5D10H    ; "       "
CAB3   DW     207EH    ; "       "
BOI1   DW     0A28H    ;BOILER DESCRIPTION
BOI2   DW     190AH    ;  "         "
BOI3   DW     0A0AH    ;  "         "
BOI4   DW     5B0AH    ;  "         "
BOI5   DW     3939H    ;  "         "
BOI6   DW     205DH    ;  "         "
FRA1   DW     2006H    ;FRAME DESCRIPTION
FRA2   DW     2D11H    ;  "         "
FRA3   DW     2011H    ;  "         "
FRA4   DW     2D6FH    ;  "         "
FRA5   DW     206FH    ;  "         "
TIES   DW     1919H    ;TIES DESCRIPTION
SHED   NOP             ;THIS IS SHED AREA
CLOUD  EQU    $+0173H  ;BEGINNING OF CLOUD

