; -----------------------------------------------------------------------------
; The Hobbit - Second Stage Loader (c) Melbourne House
; reverse engineered by Eelco Huininga
; DOS Filename   : O.BOOT
; Acorn filename : O.BOOT
; Load address   : FFFF05D1
; Exec address   : FFFF05D1
; Length         : 000002D2
; -----------------------------------------------------------------------------

; -----------------------------------------------------------------------------
; The Hobbit - Basic Disc Operating System calls
; -----------------------------------------------------------------------------
readdata           = &0491
writedata          = &049F
identifydisc       = &058E

; -----------------------------------------------------------------------------
; OS call addresses
; -----------------------------------------------------------------------------
osrdch             = &FFE0                       ; OSRDCH Read character
osasci             = &FFE3                       ; OSASCI Write character &0D=LF
oswrch             = &FFEE                       ; OSWRCH Write character
osbyte             = &FFF4                       ; OSBYTE Various OS routines

; -----------------------------------------------------------------------------
; Start of code
; -----------------------------------------------------------------------------
.
05D1  20 45 07      E.   JSR loadbdos
05D4  58           X     CLI
05D5  20 72 07      r.   JSR check4080
05D8  20 8E 05      ..   JSR identifydisc
05DB  20 9E 07      ..   JSR showscreen
05DE  20 57 08      W.   JSR getresponse
05E1  F0 2E        ..    BEQ loadcover           ; Jump when RETURN was pressed
05E3  A9 00        ..    LDA #&00                ; Sector &00
05E5  85 B4        ..    STA &B4
05E7  A9 0E        ..    LDA #&0E                ; Track &0E
05E9  85 B2        ..    STA &B2
05EB  A9 BA        ..    LDA #&BA                ; Length &08BA
05ED  85 88        ..    STA &88
05EF  A9 08        ..    LDA #&08
05F1  85 89        ..    STA &89
05F3  A9 03        ..    LDA #&03                ; Destination address &0D03
05F5  85 8A        ..    STA &8A
05F7  A9 0D        ..    LDA #&0D
05F9  85 8B        ..    STA &8B
05FB  20 91 04      ..   JSR readdata
05FE  90 0E        ..    BCC &060E
.
0600  00           .     BRK                     ; Generate error
0601  01           .     EQUB &01                ; Error code &01
0602  42 4F 4F 54  BOOT  EQUS "BOOT-F-Can"       ; Error message
      2D 46 2D 43  -F-C
      61 6E        an
060C  0D           .     EQUB &0D
060D  00           .     EQUB &00
.
060E  4C 03 0D     L..   JMP &0D03               ; Jump to the prepare utility

.loadcover
0611  A9 0C        ..    LDA #&0C                ; VDU 12 (Clear text area)
0613  20 EE FF      ..   JSR oswrch
0616  A9 0F        ..    LDA #&0F                ; Track &0F
0618  85 B2        ..    STA &B2
061A  A9 00        ..    LDA #&00                ; Sector &00
061C  85 B4        ..    STA &B4
061E  A9 00        ..    LDA #&00                ; Destination address &7C00
0620  85 8A        ..    STA &8A
0622  A9 7C        .|    LDA #&7C
0624  85 8B        ..    STA &8B
0626  A9 00        ..    LDA #&00                ; Length &0400
0628  85 88        ..    STA &88
062A  A9 04        ..    LDA #&04
062C  85 89        ..    STA &89
062E  20 91 04      ..   JSR readdata
0631  90 27        .'    BCC loaddb
.
0633  00           .     BRK                     ; Generate error
0634  01           .     EQUB &01                ; Error code &01
0635  42 4F 4F 54  BOOT  EQUS "BOOT-F-Error during cover page load"
      2D 46 2D 45  -F-E
      72 72 6F 72  rror
      20 64 75 72   dur
      69 6E 67 20  ing 
      63 6F 76 65  cove
      72 20 70 61  r pa
      67 65 20 6C  ge l
      6F 61 64     oad
0658  0D           .     EQUB &0D
0659  00           .     EQUB &00

.loaddb
065A  A9 02        ..    LDA #&02                ; Track &02
065C  85 B2        ..    STA &B2
065E  A9 00        ..    LDA #&00                ; Sector &00
0660  85 B4        ..    STA &B4
0662  A9 03        ..    LDA #&03                ; Destination address &0D03
0664  85 8A        ..    STA &8A
0666  A9 0D        ..    LDA #&0D
0668  85 8B        ..    STA &8B
066A  A9 AC        ..    LDA #&AC                ; Length &22AC
066C  85 88        ..    STA &88
066E  A9 22        ."    LDA #&22
0670  85 89        ..    STA &89
0672  20 91 04      ..   JSR readdata
0675  90 24        .$    BCC loadmain
.
0677  00           .     BRK                     ; Generate error
0678  01           .     EQUB &01                ; Error code &01
0679  42 4F 4F 54  BOOT  EQUS "BOOT-F-Read error during db load"
      2D 46 2D 52  -F-R
      65 61 64 20  ead 
      65 72 72 6F  erro
      72 20 64 75  r du
      72 69 6E 67  ring
      20 64 62 20   db 
      6C 6F 61 64  load
0699  0D           .     EQUB &0D
069A  00           .     EQUB &00

.loadmain
069B  A9 06        ..    LDA #&06                 ; Track &06
069D  85 B2        ..    STA &B2
069F  A9 00        ..    LDA #&00                 ; Sector &00
06A1  85 B4        ..    STA &B4
06A3  A9 B0        ..    LDA #&B0                 ; Destination address &2FB0
06A5  85 8A        ..    STA &8A
06A7  A9 2F        ./    LDA #&2F
06A9  85 8B        ..    STA &8B
06AB  A9 4F        .O    LDA #&4F                 ; Length &4C4F
06AD  85 88        ..    STA &88
06AF  A9 4C        .L    LDA #&4C
06B1  85 89        ..    STA &89
06B3  20 91 04      ..   JSR readdata
06B6  90 26        .&    BCC loadjmptabl
.
06B8  00           .     BRK                     ; Generate error
06B9  01           .     EQUB &01                ; Error code &01
06BA  42 4F 4F 54  BOOT  EQUS "BOOT-F-Read error during main load"
      2D 46 2D 52  -F-R
      65 61 64 20  ead 
      65 72 72 6F  erro
      72 20 64 75  r du
      72 69 6E 67  ring
      20 6D 61 69   mai
      6E 20 6C 6F  n lo
      61 64        ad
06DC  0D           .     EQUB &0D
06DD  00           .     EQUB &00

.loadjmptabl
06DE  A9 01        ..    LDA #&01                ; Track &01
06E0  85 B2        ..    STA &B2
06E2  A9 09        ..    LDA #&09                ; Sector &09
06E4  85 B4        ..    STA &B4
06E6  A9 B3        ..    LDA #&B3                ; Destination address &2FB3
06E8  85 8A        ..    STA &8A
06EA  A9 2F        ./    LDA #&2F
06EC  85 8B        ..    STA &8B
06EE  A9 2C        .,    LDA #&2C                ; Length &002C
06F0  85 88        ..    STA &88
06F2  A9 00        ..    LDA #&00
06F4  85 89        ..    STA &89
06F6  20 91 04      ..   JSR readdata
06F9  90 29        .)    BCC loadrun
.
06FB  00           .     BRK                     ; Generate error
06FC  01           .     EQUB &01                ; Error code &01
06FD  42 4F 4F 54  BOOT  EQUS "BOOT-F-Read error during jmptabl load"
      2D 46 2D 52  -F-R
      65 61 64 20  ead 
      65 72 72 6F  erro
      72 20 64 75  r du
      72 69 6E 67  ring
      20 6A 6D 70   jmp
      74 61 62 6C  tabl
      20 6C 6F 61   loa
      64           d
0722  0D           .     EQUB &0D
0723  00           .     EQUB &00

.loadrun
0724  A9 05        ..    LDA #&05                ; Push &05D1 to the stack
0725  48           H     PHA
0727  A9 D1        ..    LDA #&D1
0729  48           H     PHA
072A  A9 01        ..    LDA #&01                ; Track &01
072C  85 B2        ..    STA &B2
072E  A9 00        ..    LDA #&00                ; Sector &00
0730  85 B4        ..    STA &B4
0732  A9 D1        ..    LDA #&D1                ; Destination address &05D1
0734  85 8A        ..    STA &8A
0736  A9 05        ..    LDA #&05
0738  85 8B        ..    STA &8B
073A  A9 21        .!    LDA #&21                ; Length &0721
073C  85 88        ..    STA &88
073E  A9 07        ..    LDA #&07
0740  85 89        ..    STA &89
0742  4C 91 04     L..   JMP &0491               ; Load the final code, and jump to &05D2

.loadbdos
0745  A9 00        ..    LDA #&00
0747  85 06        ..    STA &06
0749  A9 00        ..    LDA #&00
074B  8D CE 02     ...   STA &02CE
074E  A2 58        .X    LDX #&58
0750  A0 07        ..    LDY #&07
0752  20 F7 FF      ..   JSR oscli               ; *LOAD O.BDOS
0755  4C 65 07     Le.   JMP &0765
.loadbdosstring
0758  2A 4C 4F 41  *LOA  EQUS "*LOAD O.BDOS"
      44 20 4F 2E  D O.
      42 44 4F 53  BDOS
0764  0D           .     EQUB &0D
.
0765  A9 D2        ..    LDA #&D2                 ; OSBYTE &D2 (*FX 210): Read/write sound supression status
0767  A2 01        ..    LDX #&01                 ; Disable sound
0769  20 F4 FF      ..   JSR osbyte
076C  A9 40        .@    LDA #&40                 ; RTI
076E  8D 00 0D     ...   STA &0D00
0771  60           `     RTS

; -----------------------------------------------------------------------------
; Check wether the disc drive is 40 or 80 tracks. Returns with A=&04 and &B1=&00 for 40 tracks and A=&08 and &B1=&01 for 80 tracks
; -----------------------------------------------------------------------------
.check4080
0772  A9 10        ..    LDA #&10                 ; Track &10
0774  85 B2        ..    STA &B2
0776  A9 00        ..    LDA #&00                 ; Sector &00
0778  85 B4        ..    STA &B4
077A  85 B0        ..    STA &B0
077C  85 B1        ..    STA &B1
077E  85 89        ..    STA &89                  ; Length &0001
0780  A9 01        ..    LDA #&01
0782  85 88        ..    STA &88
0784  A9 B0        ..    LDA #&B0                 ; Destination address &2FB0
0786  85 8A        ..    STA &8A
0788  A9 2F        ./    LDA #&2F
078A  85 8B        ..    STA &8B
078C  20 91 04      ..   JSR readdata             ; Load 1 byte from track &10 sector &00
078F  90 04        ..    BCC &0795                ; Jump if read was succesfull
0791  A9 01        ..    LDA #&01
0793  85 B1        ..    STA &B1
.
0795  A9 04        ..    LDA #&04                 ; 40 tracks
0797  A4 B1        ..    LDY &B1
0799  F0 01        ..    BEQ &079C
079B  0A           .     ASL A                    ; 80 tracks
.
079C  18           .     CLC
079D  60           `     RTS

; -----------------------------------------------------------------------------
; Show the main screen
; -----------------------------------------------------------------------------
.showscreen
079E  20 81 08      ..   JSR printmessage
.
07A1  16 07        ..    ASL &07,X
07A3  1E 57 65     .We   ASL &6557,Y
07A6  6C 63 6F     lco   JMP (&6F63)
07A9  6D 65 20     me    ADC &2065
07AC  74           t     ...
07AD  6F           o     ...
07AE  20 74 68      th   JSR &6874
07B1  65 20        e     ADC &20
07B3  48           H     PHA
07B4  4F           O     ...
07B5  42           B     ...
07B6  42           B     ...
07B7  49 54        IT    EOR #&54
07B9  2E 0D 0D     ...   ROL &0D0D
07BC  0D 20 20     .     ORA &2020
07BF  3C           <     ...
07C0  52           R     ...
07C1  45 54        ET    EOR &54
07C3  55 52        UR    EOR &52,X
07C5  4E 3E 20     N>    LSR &203E
07C8  2D 20 52     - R   AND &5220
07CB  75 6E        un    ADC &6E,X
07CD  20 48 4F      HO   JSR &4F48
07D0  42           B     ...
07D1  42           B     ...
07D2  49 54        IT    EOR #&54
07D4  20 50 52      PR   JSR &5250
07D7  4F           O     ...
07D8  47           G     ...
07D9  52           R     ...
07DA  41 4D        AM    EOR (&4D,X)
07DC  0D 0D 20     ..    ORA &200D
07DF  20 3C 54      <T   JSR &543C
07E2  41 42        AB    EOR (&42,X)
07E4  3E 20 20     >     ROL &2020,X
07E7  20 20 2D       -   JSR &2D20
07EA  20 52 75      Ru   JSR &7552
07ED  6E 20 50     n P   ROR &5020
07F0  52           R     ...
07F1  45 50        EP    EOR &50
07F3  41 52        AR    EOR (&52,X)
07F5  45 20        E     EOR &20
07F7  55 54        UT    EOR &54,X
07F9  49 4C        IL    EOR #&4C
07FB  49 54        IT    EOR #&54
07FD  59 0D 0D     Y..   EOR &0D0D,Y
0800  0D 4E 4F     .NO   ORA &4F4E
0803  54           T     ...
0804  45 3A        E:    EOR &3A
0806  2D 20 59     - Y   AND &5920
0809  6F           o     ...
080A  75 20        u     ADC &20,X
080C  4D 55 53     MUS   EOR &5355
080F  54           T     ...
0810  20 70 72      pr   JSR &7270
0813  65 70        ep    ADC &70
0815  61 72        ar    ADC (&72,X)
0817  65 20        e     ADC &20
0819  72           r     ...
081A  75 6E        un    ADC &6E,X
081C  74           t     ...
081D  69 6D        im    ADC #&6D
081F  65 0D        e.    ADC &0D
0821  20 20 20           JSR &2020
0824  20 20 20           JSR &2020
0827  20 64 69      di   JSR &6964
082A  73           s     ...
082B  6B           k     ...
082C  73           s     ...
082D  20 62 65      be   JSR &6562
0830  66 6F        fo    ROR &6F
0832  72           r     ...
0833  65 20        e     ADC &20
0835  79 6F 75     you   ADC &756F,Y
0838  20 63 61      ca   JSR &6163
083B  6E 20 70     n p   ROR &7020
083E  6C 61 79     lay   JMP (&7961)
0841  0D 20 20     .     ORA &2020
0844  20 20 20           JSR &2020
0847  20 20 54       T   JSR &5420
084A  48           H     PHA
084B  45 20        E     EOR &20
084D  48           H     PHA
084E  4F           O     ...
084F  42           B     ...
0850  42           B     ...
0851  49 54        IT    EOR #&54
0853  2E 0D 00     ...   ROL &000D
.
0856  60           `     RTS

; -----------------------------------------------------------------------------
; Get response from user
; -----------------------------------------------------------------------------
.getresponse
0857  20 81 08      ..   JSR printmessage
.
085A  50 6C        Pl    BVC &08C8
085C  65 61        ea    ADC &61
085E  73           s     ...
085F  65 20        e     ADC &20
0861  65 6E        en    ADC &6E
0863  74           t     ...
0864  65 72        er    ADC &72
0866  20 63 68      ch   JSR &6863
0869  6F           o     ...
086A  69 63        ic    ADC #&63
086C  65 3A        e:    ADC &3A
086E  0D 00
.
0870  20 E0 FF      ..   JSR osrdch               ; Read character
0873  C9 0D        ..    CMP #&0D                 ; Is it ENTER?
0875  F0 07        ..    BEQ &087E
0877  C9 09        ..    CMP #&09                 ; Is it TAB?
0879  D0 DC        ..    BNE &0857
087B  A9 01        ..    LDA #&01                 ; Return &01 when TAB was pressed
087D  60           `     RTS
.
087E  A9 00        ..    LDA #&00                 ; Return &00 when RETURN was pressed
0880  60           `     RTS

; -----------------------------------------------------------------------------
; Print message
; -----------------------------------------------------------------------------
.printmessage
0881  68           h     PLA
0882  85 00        ..    STA &00
0884  68           h     PLA
0885  85 01        ..    STA &01
0887  A0 00        ..    LDY #&00
.
0889  20 9C 08      ..   JSR &089C
088C  B1 00        ..    LDA (&00),Y
088E  F0 06        ..    BEQ &0896
0890  20 E3 FF      ..   JSR osasci
0893  4C 89 08     L..   JMP &0889
.
0896  20 9C 08      ..   JSR &089C
0899  6C 00 00     l..   JMP (&0000)              ; Return to main code
.
089C  E6 00        ..    INC &00
089E  D0 02        ..    BNE &08A2
08A0  E6 01        ..    INC &01
.
08A2  60           `     RTS
