*
* Dungeon Master
*

	mx	%00
	rel
	dsk	MAIN.L
	lst	off

	use	MAIN.E
	use	MAIN.X
	
	use	4/Event.Macs
	use	4/Locator.Macs
	use	4/Mem.Macs
	use	4/Misc.Macs
	use	4/Qd.Macs
	use	4/SANE.Macs
	use	4/Sch.Macs
	use	4/Util.Macs
	
         PHA
         SEP   #$30
         LDA   #^L020000
         PHA
         PLB
         REP   #$30
         PLA
         STA   L020000
         TDC
         STA   L020004
         TSC
         STA   L020006
         JMPL  L0106D6

L010019  PHD
         TSC
         SEC
         SBC   #$00FB
         TCD
         ADC   #$00DC
         TCS
         LDA   #L0423DC
         STA   $F2
         LDA   #^L0423DC
         STA   $F4
         LDA   #$2000
         STA   $EE
         LDA   #$00E1
         STA   $F0
         LDA   #$00A0
         STA   $E6
         LDA   #$FA00
         STA   $EC
         SEP   #$30
         STZ   $DE
         BRA   L010050
L010048  REP   #$30
         INC   $F2
         BNE   L010050
         INC   $F4
L010050  SEP   #$30
         LDA   [$F2]
         BMI   L010097
         PHA
         AND   #$0F
         STA   $DF
         ASL
         ASL
         ASL
         ASL
         TAX
         PLA
         AND   #$70
         LSR
         LSR
         LSR
         LSR
         TAY
L010068  BIT   $DE
         BMI   L010071
         TXA
         STA   [$EE]
         BRA   L010081
L010071  LDA   $DF
         ORA   [$EE]
         STA   [$EE]
         INC   $EE
         BNE   L010081
         INC   $EF
         BNE   L010081
         INC   $F0
L010081  LDA   $DE
         EOR   #$80
         STA   $DE
         REP   #$30
         DEC   $EC
         SEP   #$30
         BNE   L010092
         BRL   L010252
L010092  DEY
         BPL   L010068
         BRA   L010048
L010097  PHA
         BIT   #$40
         PHP
         REP   #$30
         INC   $F2
         BNE   L0100A3
         INC   $F4
L0100A3  SEP   #$30
         LDA   [$F2]
         STA   $E8
         STZ   $E9
         PLP
         BEQ   L0100BE
         STA   $E9
         REP   #$30
         INC   $F2
         BNE   L0100B8
         INC   $F4
L0100B8  SEP   #$30
         LDA   [$F2]
         STA   $E8
L0100BE  REP   #$30
         INC   $E8
         SEP   #$30
         PLA
         PHA
         AND   #$30
         BEQ   L0100D5
         CMP   #$30
         BNE   L0100D1
         BRL   L010179
L0100D1  PLA
         BRL   L010048
L0100D5  PLA
         AND   #$0F
         STA   $DF
         ASL
         ASL
         ASL
         ASL
         ORA   $DF
         STA   $EA
         STA   $EB
         BIT   $DE
         BPL   L010109
         LDA   $DF
         ORA   [$EE]
         STA   [$EE]
         INC   $EE
         BNE   L0100F8
         INC   $EF
         BNE   L0100F8
         INC   $F0
L0100F8  LDA   $DE
         EOR   #$80
         STA   $DE
         REP   #$30
         DEC   $EC
         BNE   L010107
         BRL   L010252
L010107  DEC   $E8
L010109  REP   #$30
         STZ   $E4
         LDA   $E8
         LSR
         ROR   $E4
         LSR
         ROR   $E4
         CMP   #$0000
         BEQ   L010131
         TAX
         LDA   $EA
         LDY   #$0000
L010120  STA   [$EE],Y
         INY
         INY
         DEX
         BNE   L010120
         CLC
         TYA
         ADC   $EE
         STA   $EE
         BCC   L010131
         INC   $F0
L010131  SEP   #$30
         BIT   $E5
         BPL   L010145
         LDA   $EA
         STA   [$EE]
         REP   #$30
         INC   $EE
         BNE   L010143
         INC   $F0
L010143  SEP   #$30
L010145  BIT   $E5
         BVC   L010166
         LDA   $DF
         ASL
         ASL
         ASL
         ASL
         STA   [$EE]
         LDA   $DE
         EOR   #$80
         STA   $DE
         REP   #$30
         DEC   $EC
         SEP   #$30
         BNE   L010162
         BRL   L010252
L010162  REP   #$30
         DEC   $E8
L010166  REP   #$30
         SEC
         LDA   $EC
         SBC   $E8
         STA   $EC
         SEP   #$30
         BEQ   L010176
         BRL   L010048
L010176  BRL   L010252
L010179  PLA
         AND   #$0F
         STA   $EA
         REP   #$30
         SEC
         LDA   $EE
         SBC   $E6
         STA   $E0
         LDA   $F0
         BCS   L01018C
         DEC
L01018C  STA   $E2
         SEP   #$30
         BIT   $DE
         BPL   L0101C5
         LDA   [$E0]
         AND   #$0F
         ORA   [$EE]
         STA   [$EE]
         INC   $EE
         BNE   L0101A6
         INC   $EF
         BNE   L0101A6
         INC   $F0
L0101A6  LDA   $DE
         EOR   #$80
         STA   $DE
         REP   #$30
         DEC   $EC
         BNE   L0101B5
         BRL   L010252
L0101B5  DEC   $E8
         SEP   #$30
         BEQ   L01021F
         INC   $E0
         BNE   L0101C5
         INC   $E1
         BNE   L0101C5
         INC   $E2
L0101C5  REP   #$30
         STZ   $E4
         LDA   $E8
         LSR
         ROR   $E4
         LSR
         ROR   $E4
         TAX
         LDY   #$0000
         CMP   #$0000
         BEQ   L0101E3
L0101DA  LDA   [$E0],Y
         STA   [$EE],Y
         INY
         INY
         DEX
         BNE   L0101DA
L0101E3  BIT   $E4
         BPL   L0101F0
         SEP   #$20
         LDA   [$E0],Y
         STA   [$EE],Y
         INY
         REP   #$30
L0101F0  CLC
         TYA
         ADC   $EE
         STA   $EE
         BCC   L0101FA
         INC   $F0
L0101FA  CLC
         TYA
         ADC   $E0
         STA   $E0
         BCC   L010204
         INC   $E2
L010204  BIT   $E4
         BVC   L010216
         SEP   #$30
         LDA   [$E0]
         AND   #$F0
         STA   [$EE]
         LDA   #$80
         STA   $DE
         REP   #$30
L010216  SEC
         LDA   $EC
         SBC   $E8
         STA   $EC
         SEP   #$30
L01021F  LDA   $EA
         STA   $DF
         BIT   $DE
         BMI   L010231
         LDA   $DF
         ASL
         ASL
         ASL
         ASL
         STA   [$EE]
         BRA   L010241
L010231  LDA   $DF
         ORA   [$EE]
         STA   [$EE]
         INC   $EE
         BNE   L010241
         INC   $EF
         BNE   L010241
         INC   $F0
L010241  LDA   $DE
         EOR   #$80
         STA   $DE
         REP   #$30
         DEC   $EC
         SEP   #$30
         BEQ   L010252
         BRL   L010048
L010252  REP   #$30
         TSC
         CLC
         ADC   #$001E
         TCS
         PLD
         RTL

L01025C  PHD
         TSC
         SEC
         SBC   #$00FB
         TCD
         ADC   #$00EA
         TCS
         PHX
         PHA
         PEA   $0006
         _GetAddr
         BCS   L010278
         LDA   #$0000
L010278  STAL  L020002
         PLA
         PLX
         STX   $F0
         STA   $EE
         LDA   #L040010
         LDX   #^L040010
         STX   $F4
         STA   $F2
         LDA   #$2377
         STA   $EC
         SEP   #$30
L010293  LDA   SOUNDCTL
         BMI   L010293
         LDA   #$2F
         STA   SOUNDCTL
         LDA   #$A0
         STA   SOUNDADRL
         LDA   #$03
         STA   SOUNDDATA
         STA   SOUNDDATA
         LDA   #$6F
         STA   SOUNDCTL
         STZ   SOUNDADRL
         STZ   SOUNDADRH
L0102B5  LDA   [$F2]
         STA   SOUNDDATA
         REP   #$30
         INC   $F2
         BNE   L0102C2
         INC   $F4
L0102C2  DEC   $EC
         SEP   #$30
         BNE   L0102B5
         LDA   #$2F
         STA   SOUNDCTL
         LDA   #$E1
         STA   SOUNDADRL
         LDA   #$FF
         STA   SOUNDDATA
         LDA   #$80
         STA   SOUNDADRL
         STZ   SOUNDDATA
         STZ   SOUNDDATA
         LDA   #$C0
         STA   SOUNDADRL
         LDA   #$36
         STA   SOUNDDATA
         STA   SOUNDDATA
         LDA   #$40
         STA   SOUNDADRL
         LDA   [$EE]
         AND   #$0F
         TAX
         LDAL  L040000,X
         STA   SOUNDDATA
         STA   SOUNDDATA
         LDA   #$00
         STA   SOUNDADRL
         LDA   #$E0
         STA   SOUNDDATA
         STA   SOUNDDATA
         LDA   #$20
         STA   SOUNDADRL
         LDA   #$00
         STA   SOUNDDATA
         STA   SOUNDDATA
         LDA   #$A0
         STA   SOUNDADRL
         LDA   #$02
         STA   SOUNDDATA
         LDA   #$12
         STA   SOUNDDATA
         REP   #$30
         TSC
         CLC
         ADC   #$0010
         TCS
         PLD
         RTL

L010336  PHD
         TSC
         SEC
         SBC   #$00F7
         TCD
         ADC   #$00F0
         TCS
         SEP   #$30
         LDA   NEWVIDEO
         AND   #$1F
         ORA   #$40
         STA   NEWVIDEO
         REP   #$30
         LDA   L020002
         PHA
         PEI   $FF
         PEI   $FD
         _SysFailMgr
         BCS   L010363
         LDA   #$0000
L010363  STAL  L020002
L010367  BRA   L010367
         TSC
         CLC
         ADC   #$0006
         TCS
         PLD
         RTL

L010371  PHD
         TSC
         SEC
         SBC   #$00FB
         TCD
         ADC   #$00EC
         TCS
         _TLStartUp
         BCS   L010388
         LDA   #$0000
L010388  STAL  L020002
         PHA
         _MMStartUp
         BCS   L010399
         LDA   #$0000
L010399  STAL  L020002
         PLA
         STA   $F0
         _MTStartUp
         BCS   L0103AC
         LDA   #$0000
L0103AC  STAL  L020002
         LDX   #$0010
         LDA   #$0000
         PHX
         PHA
         PHX
         PHA
         _TotalMem
         BCS   L0103C6
         LDA   #$0000
L0103C6  STAL  L020002
         PLA
         PLX
         JSL   L010798
         BEQ   L0103E0
         BCC   L0103E0
         PEA   ^L042CEF
         PEA   L042CEF
         JSL   L010336
         PLY
         PLY

L0103E0

* Here was the bad block protection
* We can keep the following code

         PEA   $0019
         PEA   ^L042D13   ; Gort, Klaatu Barada Nickto
         PEA   L042D13
         PEA   ^L042DD2
         PEA   L042DD2
         JSL   L0108A1
         PLY
         PLY
         PLY
         PLY
         PLY
         CMP   #$0000
         BNE   L010466

L01045A  PEA   ^L042D2F   ; Disk damaged
         PEA   L042D2F
         JSL   L010336
         PLY
         PLY

L010466  _SchStartUp
         BCS   L010472
         LDA   #$0000
L010472  STAL  L020002
         PHA
         _SANEStatus
         BCS   L010483
         LDA   #$0000
L010483  STAL  L020002
         PLA
         CMP   #$0000
         BNE   L0104F3
         PHX
         PHA
         PEA   $0000
         PEA   $0100
         PHA
         PEA   $1F00
         _GetNewID
         BCS   L0104A5
         LDA   #$0000
L0104A5  STAL  L020002
         PEA   $C015
         PEA   $0000
         PEA   $0800
         _NewHandle
         BCS   L0104BE
         LDA   #$0000
L0104BE  STAL  L020002
         PLA
         PLX
         STX   $F4
         STA   $F2
         STX   $F6
         ORA   $F6
         BNE   L0104DA
         PEA   ^L042D52
         PEA   L042D52
         JSL   L010336
         PLY
         PLY
L0104DA  LDY   #$0002
         LDA   [$F2],Y
         TAX
         LDA   [$F2]
         PHA
         _SANEStartUp
         BCS   L0104EF
         LDA   #$0000
L0104EF  STAL  L020002
L0104F3  PHA
         _EMStatus
         BCS   L010500
         LDA   #$0000
L010500  STAL  L020002
         PLA
         CMP   #$0000
         BNE   L010585
         PHX
         PHA
         PEA   $0000
         PEA   $0100
         PHA
         PEA   $1F00
         _GetNewID
         BCS   L010522
         LDA   #$0000
L010522  STAL  L020002
         PLA
         STA   $EE
         PHA
         PEA   $C015
         PEA   $0000
         PEA   $0900
         _NewHandle
         BCS   L01053F
         LDA   #$0000
L01053F  STAL  L020002
         PLA
         PLX
         STX   $F4
         STA   $F2
         STX   $F6
         ORA   $F6
         BNE   L01055B
         PEA   ^L042D78
         PEA   L042D78
         JSL   L010336
         PLY
         PLY
L01055B  LDY   #$0002
         LDA   [$F2],Y
         TAX
         LDA   [$F2]
         PHA
         PEA   $0000
         PEA   $0000
         PEA   $013F
         PEA   $0000
         PEA   $00C7
         PEI   $EE
         _EMStartUp
         BCS   L010581
         LDA   #$0000
L010581  STAL  L020002
L010585  PHA
         _QDStatus
         BCS   L010592
         LDA   #$0000
L010592  STAL  L020002
         PLA
         CMP   #$0000
         BNE   L01060E
         PHX
         PHA
         PEA   $0000
         PEA   $0400
         PHA
         PEA   $1F00
         _GetNewID
         BCS   L0105B4
         LDA   #$0000
L0105B4  STAL  L020002
         PLA
         STA   $EE
         PHA
         PEA   $C015
         PEA   $0000
         PEA   $0A00
         _NewHandle
         BCS   L0105D1
         LDA   #$0000
L0105D1  STAL  L020002
         PLA
         PLX
         STX   $F4
         STA   $F2
         STX   $F6
         ORA   $F6
         BNE   L0105ED
         PEA   ^L042DA7
         PEA   L042DA7
         JSL   L010336
         PLY
         PLY
L0105ED  LDY   #$0002
         LDA   [$F2],Y
         TAX
         LDA   [$F2]
         PHA
         PEA   $0000
         PEA   $0140
         PEI   $EE
         _QDStartUp
         BCS   L01060A
         LDA   #$0000
L01060A  STAL  L020002
L01060E  SEP   #$30
         LDA   CLOCKCTL
         AND   #$F0
         ORA   #$02
         STA   CLOCKCTL
         LDX   #$1F
L01061C  TXA
         AND   #$01
         BNE   L010625
         LDA   #$09
         BRA   L010627
L010625  LDA   #$00
L010627  STAL  $E19E00,X
         STAL  $E19E20,X
         DEX
         BPL   L01061C
         LDX   #$00
L010634  CPX   #$1D
         BCC   L010642
         CPX   #$A8
         BCC   L01063E
         BNE   L010642
L01063E  LDA   #$00
         BRA   L010644
L010642  LDA   #$01
L010644  STAL  $E19D00,X
         INX
         CPX   #$C8
         BCC   L010634
         REP   #$30
         PEI   $F0
         _MMShutDown
         BCS   L01065D
         LDA   #$0000
L01065D  STAL  L020002
         TSC
         CLC
         ADC   #$000E
         TCS
         PLD
         RTL

L010669  PHD
         TSC
         SEC
         SBC   #$00FB
         TCD
         ADC   #$00F0
         TCS
         PHX
         PHA
         _GetTick
         BCS   L010682
         LDA   #$0000
L010682  STAL  L020002
         PLA
         PLX
         STX   $F4
         STA   $F2
L01068C  LDX   $F4
         LDA   $F2
         PHX
         PHA
         PHX
         PHA
         _GetTick
         BCS   L0106A0
         LDA   #$0000
L0106A0  STAL  L020002
         PLA
         PLX
         JSL   L010798
         BNE   L0106AE
         BRA   L01068C
L0106AE  TSC
         CLC
         ADC   #$000A
         TCS
         PLD
         RTL

L0106B6  PHD
         TSC
         SEC
         SBC   #$00F9
         TCD
         ADC   #$00F2
         TCS
L0106C1  LDA   $FF
         DEC   $FF
         TAY
         BEQ   L0106CE
         JSL   L010669
         BRA   L0106C1
L0106CE  TSC
         CLC
         ADC   #$0006
         TCS
         PLD
         RTL

*
* ENTRY POINT
*

L0106D6  PHD
         TSC
         SEC
         SBC   #$00FB
         TCD
         ADC   #$00F0
         TCS
         JSL   L010371
         JSL   L010019
         PEA   $003C
         JSL   L0106B6
         PLY
         JSL   L01025C
         LDA   #L042387
         STA   $F2
         LDA   #^L042387
         STA   $F4
         LDY   #$0000
         SEP   #$30
L010704  LDA   [$F2],Y
         BEQ   L01073E
         CMP   #$10
         BCC   L010723
         PHY
         SEC
         SBC   #$0F
         REP   #$30
         AND   #$00FF
         PHA
         JSL   L0106B6
         PLA
         SEP   #$30
         PLY
         INY
         INY
L010720  INY
         BRA   L010704
L010723  ASL
         TAX
         INY
         LDA   [$F2],Y
         STAL  $E19E00,X
         STAL  $E19E20,X
         INX
         INY
         LDA   [$F2],Y
         STAL  $E19E00,X
         STAL  $E19E20,X
         BRA   L010720
L01073E  REP   #$30
         PEA   ^L042CDE
         PEA   L042CDE
         JSL   L0107C3
         PLY
         PLY
         STAL  L042FE8
         PEA   ^L042CDE
         PEA   L042CDE
         PEA   ^L042FEA
         PEA   L042FEA
         JSL   L010853
         PLY
         PLY
         PLY
         PLY
         LDA   #$0002
         STAL  L04306A
         LDA   #L042FE8
         LDX   #^L042FE8
         PHA
         TXA
         STAL  L04306E
         PLA
         STAL  L04306C
         LDA   #$0000
         STAL  L043070
         PEA   $2029
         PEA   ^L04306A
         PEA   L04306A
         JSL   L010815
         TSC
         CLC
         ADC   #$000A
         TCS
         PLD
         RTL

L010798  PHX
         PHA
         LDA   $08,S
         TAX
         LDA   $0A,S
         TAY
         LDA   $06,S
         STA   $0A,S
         LDA   $04,S
         STA   $08,S
         TXA
         SEC
         SBC   $01,S
         BNE   L0107B3
         TYA
         SBC   $03,S
         BRA   L0107B8
L0107B3  TYA
         SBC   $03,S
         REP   #$02
L0107B8  PHP
         LDA   $00,S
         STA   $08,S
         PLY
         PLY
         PLY
         PLY
         PLP
         RTL

L0107C3  PHD
         TSC
         SEC
         SBC   #$00F7
         TCD
         ADC   #$00EC
         TCS
         LDX   $FF
         LDA   $FD
         CLC
         ADC   #$0001
         PHA
         TXA
         ADC   #$0000
         TAX
         PLA
         STX   $F0
         STA   $EE
L0107E1  LDX   $FF
         LDA   $FD
         INC   $FD
         BNE   L0107EB
         INC   $FF
L0107EB  STA   $F2
         STX   $F4
         LDA   [$F2]
         AND   #$00FF
         CMP   #$0000
         BEQ   L0107FB
         BRA   L0107E1
L0107FB  LDX   $F0
         LDA   $EE
         PHX
         PHA
         LDX   $FF
         LDA   $FD
         JSL   L010949
         PLA
         PLX
         TAY
         TDC
         CLC
         ADC   #$00F7
         TCS
         TYA
         PLD
         RTL

L010815  PHB
         PLA
         XBA
         STAL  L010850
         PLA
         STAL  L010850+1
         PLA
         STAL  L010836
         PLA
         STAL  L010836+2
         PLA
         STAL  L010834
         JSL   GSOS
L010834  DW    $0000
L010836  ADRL  $00000000
         BCS   L01083F
         LDA   #$0000
L01083F  STAL  L020002
         LDAL  L010850+1
         PHA
         PHB
         LDAL  L010850
         STA   $01,S
         RTL

L010850  ADR   $000000

L010853  PHD
         TSC
         SEC
         SBC   #$00F3
         TCD
         ADC   #$00E8
         TCS
         LDX   $FB
         LDA   $F9
         STX   $EC
         STA   $EA
L010866  LDX   $FF
         LDA   $FD
         INC   $FD
         BNE   L010870
         INC   $FF
L010870  STA   $EE
         STX   $F0
         LDA   [$EE]
         PHA
         LDX   $FB
         LDA   $F9
         INC   $F9
         BNE   L010881
         INC   $FB
L010881  STA   $EE
         STX   $F0
         PLA
         SEP   #$20
         STA   [$EE]
         REP   #$20
         AND   #$00FF
         BEQ   L010893
         BRA   L010866
L010893  LDX   $EC
         LDA   $EA
         TAY
         TDC
         CLC
         ADC   #$00F3
         TCS
         TYA
         PLD
         RTL

L0108A1  PHD
         TSC
         SEC
         SBC   #$00F1
         TCD
         ADC   #$00EA
         TCS
         LDA   $F7
         CMP   $FB
         BNE   L0108BE
         LDA   $F9
         CMP   $FD
         BNE   L0108BE
         LDA   #$0000
         BRL   L01093F
L0108BE  DEC   $FF
         LDA   $FF
         SEC
         SBC   #$0000
         BVS   L0108CB
         EOR   #$8000
L0108CB  BPL   L01090C
         LDA   [$F7]
         AND   #$00FF
         PHA
         LDX   $FD
         LDA   $FB
         INC   $FB
         BNE   L0108DD
         INC   $FD
L0108DD  STA   $EC
         STX   $EE
         LDA   [$EC]
         AND   #$00FF
         PLX
         STX   $EC
         CMP   $EC
         BNE   L01090C
         LDX   $F9
         LDA   $F7
         INC   $F7
         BNE   L0108F7
         INC   $F9
L0108F7  STA   $EC
         STX   $EE
         LDA   [$EC]
         AND   #$00FF
         CMP   #$0000
         BNE   L01090A
         LDA   #$0000
         BRA   L01093F
L01090A  BRA   L0108BE
L01090C  LDA   $FF
         SEC
         SBC   #$0000
         BVS   L010917
         EOR   #$8000
L010917  BMI   L01091E
         LDA   #$0000
         BRA   L01093F
L01091E  LDA   $FB
         BNE   L010924
         DEC   $FD
L010924  DEC   $FB
         LDX   $FD
         LDA   $FB
         STA   $EC
         STX   $EE
         LDA   [$EC]
         AND   #$00FF
         PHA
         LDA   [$F7]
         AND   #$00FF
         PLX
         STX   $F0
         SEC
         SBC   $F0
L01093F  TAY
         TDC
         CLC
         ADC   #$00F1
         TCS
         TYA
         PLD
         RTL

L010949  SEC
         SBC   $04,S
         STA   $04,S
         TXA
         SBC   $06,S
         STA   $06,S
         RTL

