; Driver area locations
drvrq     EQU &FFE8
cfgbyt    EQU &FFE9
trkrq     EQU &FFEA
secrq     EQU &FFEC
dmarq     EQU &FFEE

doCFG     EQU &FFF0
readsec   EQU &FFF3
writeSec  EQU &FFF6

drive     EQU 0
partition EQU &18
track     EQU 2519       ; 2046 blocks = 8184K, + 6.5K CPM = 8190.5K used
                         ; track 2159 = 8185.75k into the disc, therefor 2045 
                         ; blocks would have to be in use to create a conflict.
                         ; save files require 139 bytes, so 2 CPM sectors each
                         ; so 20 required out of 26 available


.showDiscMenu
CALL testFlight          ; will not return if test fails
; setup the base driver info
  ld   hl,track
  ld   (trkrq),hl
  ld   a,drive
  ld   (drvrq),a
  ld   a,partition
  ld   (cfgbyt),a
  DI
  call doCFG
  EI
CALL prepareSaveData
LD   HL,commander
LD   B,&00
.LA315
LD   A,(HL)
OR   A
JR   Z,LA31D
INC  B
INC  HL
JR   LA315


.LA31D
LD   A,B
LD   (comLength),A
CALL resetView
LD   IX,ColourDiscMenu
RST  &28                ; block fill using table at IX
LD   HL,TdiscMenu
RST  &20                ; print string(s) at given position
CALL printName
LD   BC,&1706
CALL csrBC
LD   HL,commander
RST  &08                ; print it
.invalidResponse
LD   A,(optionsActive)
OR   A
JR   Z,LA348
XOR  A
LD   (optionsActive),A
LD   A,"1"              ; set the default option
JR   LA35C
 
.LA348
LD   HL,Toption
RST  &20                ; print string(s) at given position
.LA34C
CALL waitForKey

CP   "1"                ; shorter version of the code
JR   LO,LA34C
CP   "6"
JR   HS,LA34C 

CALL clearLower
.LA35C
SUB  "1"                ; convert "1" to "5" to 0 to 4 binary
LD   C,A
LD   B,&00
LD   HL,discOptionTable
ADD  HL,BC
ADD  HL,BC              ; point to one of the 5 routine start addr
LD   A,(HL)
INC  HL
LD   H,(HL)
LD   L,A                ; address formed
JP   (HL)               ; and enter


.getName
LD   HL,TyourName
RST  &20                ; print string(s) at given position
LD   DE,&415A
LD   C,&0A
JP   inputString
 
.loadCommander
LD   HL,TloadNumber
CALL getNumber          
JP   NC,invalidResponse ; out of range number if NC
LD   HL,Tloading
RST  &08                ; print it
CALL discLoadCommander  
JP   NZ,discErrored
LD   HL,FDB
LD   DE,commander
LD   BC,&000B
LDIR
LD   HL,FDB+11
LD   DE,ComanderData
LD   BC,&0080
LDIR
CALL storeLoadedData
JP   showDiscMenu
 
.catalogueDisc
CALL resetView
LD   IX,colourCatalogue
CALL restart28
LD   HL,Tcatalogue
RST  &20                ; print string(s) at given position
CALL readSavedNames     ; read the (hidden) commander files and put the names at FDB
; JP   C,discErrored    ; read names does not error, but returns a blank list instead 
LD   HL,FDB
LD   A,&01
.LA3C1
PUSH AF
PUSH HL
PUSH HL
PUSH AF
ADD  A,&04
LD   C,A
LD   B,&0E              ; column 14
CALL csrBC              ; move the cursor down for each of the 10 save slots
POP  AF
LD   C,A
LD   HL,&0000
LD   A,&82
RST  &18                ; Print Number                ; convert slot number to decimal and print ?
LD   A,&16              ; column 22
CALL tabA
POP  HL                 ; get the pointer to the name back
RST  &08                ; print it
POP  HL
LD   DE,&000B           ; 11 character names
ADD  HL,DE
POP  AF                 ; get the count back
INC  A
CP   &0B                ; stop if passes 10
JR   NZ,LA3C1
CALL pressAkey
JP   showDiscMenu
 
; read the file names of any saved commanders to the FDB buffer

.readSavedNames
LD   DE,FDB             ; buffer address
LD   HL,track
ld   (trkrq),hl
LD   A,1                ; file number 1 to 10
.LA3FC
PUSH AF                 ; save file number
PUSH DE                 ; save buffer
add  a,a                ; 2 sectors per save
dec  a                  ; 1 3 5 etc
ld   h,0
ld   l,a                ; zero extend A into HL
ld   (secrq),hl
ld   (dmarq),de
DI
call readsec
EI
POP  DE                 ; retrieve buffer
LD   HL,&000B           ; step over 11 charactrer file name
ADD  HL,DE              ; add to the buffer
EX   DE,HL              ; and back to DE for the next commander name
POP  AF
INC  A                  ; next file name
CP   11
JR   LO,LA3FC           ; repeat 
RET
 
.saveCommander
CALL getName
JP   NC,invalidResponse
LD   A,(keyBuffer)
OR   A
JR   Z,LA433
LD   HL,keyBuffer
LD   DE,commander
LD   BC,&000B
LDIR
.LA433
LD   HL,TsaveNumber
CALL getNumber
JP   NC,showDiscMenu
LD   HL,Tsaving
RST  &08                ; print it
LD   HL,commander
RST  &08                ; print it
CALL discSaveCommander
JP   NZ,discErrored
JP   showDiscMenu
 
.loadDefault
LD   HL,TareYouSure
RST  &08                ; print it
CALL QyesNo
CALL clearLower
CP   "Y"
JP   NZ,invalidResponse           ; must have said N then ...
CALL initState
CALL storeLoadedData
JP   showDiscMenu
 
.initState
LD   HL,DefaultComanderData
LD   DE,ComanderData
LD   BC,&0080
LDIR
LD   HL,defaultCommander
LD   DE,commander
LD   BC,&000B
LDIR
LD   A,&07
LD   (comLength),A           ;7 letters in JAMESON ?
RET
 
.prepareSaveData
LD   HL,datafileObjects
LD   DE,ComanderData
.LA489
LD   C,(HL)
INC  HL
LD   A,(HL)
INC  HL
OR   A
RET  Z
LD   B,A
LD   A,(HL)
INC  HL
PUSH HL
LD   H,B
LD   L,C
LD   C,A
LD   B,&00
LDIR
POP  HL
JR   LA489
 
.storeLoadedData
LD   HL,datafileObjects
LD   DE,ComanderData
.LA4A3
LD   C,(HL)         ; read destination address
INC  HL
LD   A,(HL)
INC  HL
OR   A              ; exit if zero end marker found
JR   Z,LA4BA        ; file loaded, exit via setting the current sytem
LD   B,A            ; address in BC
LD   A,(HL)         ; count in A
INC  HL
PUSH HL
EX   DE,HL          ; the file just loaded becomes the source
LD   D,B
LD   E,C            ; transfer destination
LD   C,A            ; count zero extended to BC
LD   B,&00
LDIR                ; block move
EX   DE,HL          ; data file pointer back in DE
POP  HL             ; restore object list
JR   LA4A3
 
.LA4BA
LD   A,(currentSystem)
LD   (hyperSystem),A
CALL L4BA8
JP   doHomeCursor
 
.discLoadCommander
LD   HL,FDB
LD   (DMArq),HL
LD   HL,track
ld   (trkrq),HL
DI
call readsec            ; sector has been set up by 1-10 code
LD   HL,(secrq)
inc  HL
LD   (secrq),HL         ; bump the sector by 1
LD   HL,FDB+128         ; bump the address by 128
LD   (DMArq),HL
call readsec            ; now get sector+1
EI
and  a                  ; 0 is good!
RET
 
.discSaveCommander
LD   HL,commander       ; move the commander data to the FDB
LD   DE,FDB
LD   BC,&000B           ; commander name first
LDIR
LD   HL,ComanderData    ; then the data
LD   C,&80
LDIR
LD   HL,FDB
LD   (DMArq),HL
LD   HL,track
ld   (trkrq),HL
DI
call writesec           ; sector has been set up by 1-10 code
LD   HL,(secrq)
inc  HL
LD   (secrq),HL         ; bump the sector by 1
LD   HL,FDB+128         ; bump the address by 128
LD   (DMArq),HL
call writesec           ; now write sector+1

LD   HL,FDB+1024
LD   (DMArq),HL
LD   HL,0
ld   (trkrq),HL
call readsec            ; force a flush of the disc cache
EI
and  a                  ; 0 is good!
RET
 
.discErrored
CALL doBeep
CALL clearLower
LD   HL,TdiscError
CALL momentaryPrint
JP   showDiscMenu


; get a number 1-10
; MTX version also setup disc sector
.getNumber
LD   (tempHL),HL
.LA577
RST  &20                ; print string(s) at given position
RST  &30                ; string input 
RET  NC
CCF
RET  Z
EX   DE,HL
LD   HL,-11
ADD  HL,DE
JR   C,over10
EX   DE,HL
add  hl,hl              ; each save file is 2 sectors
dec  hl                 ; 2-20 now  1 to 19 as sector count is 1 based
ld   (secrq),hl         ; save the sector
SCF
RET
 
.over10
CALL doBeep
LD   HL,Tselect1_10
CALL momentaryPrint
LD   HL,(tempHL)
JR   LA577
 

.datafileObjects
DW   LEB66
DB   &01
DW   galaxyInfo
DB   &06
DW   currentSystem
DB   &01
DW   gameFlagBits
DB   &01
DW   credits
DB   &03
DW   last
DB   &01
DW   LE600
DB   &04
DW   hyperFuel
DB   &14
DW   cargoSpace
DB   &48
DW   naughtynes
DB   &01
DW   currentScore
DB   &03
DW   Rating
DB   &01
DW   LE600
DB   &01
DW   LEFA3
DB   &01
DW   LE930
DB   &01
DW   LE93C
DB   &01
DW   MissionStatus
DB   &01
DW   LEFA2
DB   &01
DW   LE93B
DB   &01
DW   timeSec
DB   &04
DW   &0000

.TdiscMenu
DB   &05,&04
DZ   "1. Load new Commander"
DB   &05,&06
DZ   "2. Save Commander"
DB   &05,&08
DZ   "3. Catalog disk"
DB   &05,&0A
DZ   "4. Default JAMESON"
DB   &05,&0C
DZ   "5. Exit"
DB   &FF

.ColourDiscMenu
DB   &05,&01,&20,&01,&91
DB   &05,&04,&02,&09,&D1
DB   &08,&04,&19,&09,&21
DB   &FF

.Tcatalogue
DB   &0F,&01
DZ   "Disk Catalog"
DB   &0C,&03
DZ   "Game No.  Commander"
DB   &FF


.colourCatalogue
DB   &05,&01,&20,&01,&91
DB   &05,&03,&20,&01,&D1
DB   &05,&05,&20,&0A,&71
DB   &FF


.Toption
DB   &05,&16
DZ   "Select option 1 to 5"
DB   &FF

.TareYouSure
DC   "Are you sure"

.TyourName
DB   &05,&16
DZ   "Commander's name ? "
DB   &FF

.Tsaving
DS   "Saving Commander "
DB   &80
.Tloading
DC   "Loading .."
.TdiscError
DC   "Disk I/O error!"

.TsaveNumber
DB   &05,&16
DZ   "Save game no. 1-10"
DB   &FF

.TloadNumber
DB   &05,&16
DZ   "Load game no. 1-10"
DB   &FF


.Tselect1_10
DC   "Please select from 1 to 10."

.discOptionTable
DW   loadCommander
DW   saveCommander
DW   catalogueDisc
DW   loadDefault
DW   statusPage


end