;--------------------------------------------------------------------------------------------------------
;-
;- Name:            rfs_bank11.asm
;- Created:         July 2019
;- Author(s):       Philip Smart
;- Description:     Sharp MZ series Rom Filing System.
;-                  This assembly language program is written to utilise the banked flashroms added with
;-                  the MZ-80A RFS hardware upgrade.
;-
;- Credits:         
;- Copyright:       (c) 2018-2026 Philip Smart <philip.smart@net2net.org>
;-
;- History:         July 2019 - Merged 2 utilities to create this compilation.
;-                  May 2020  - Bank switch changes with release of v2 pcb with coded latch. The coded
;-                              latch adds additional instruction overhead as the control latches share
;-                              the same address space as the Flash RAMS thus the extra hardware to
;-                              only enable the control registers if a fixed number of reads is made
;-                              into the upper 8 bytes which normally wouldnt occur. Caveat - ensure
;-                              that no loop instruction is ever placed into EFF8H - EFFFH.
;-                  Aug 2023  - Updates to make RFS run under the SFD700 Floppy Disk Interface board.
;-                              UROM remains the same, a 2K paged ROM, MROM is located at F000 when
;-                              RFS is built for the SFD700.
;-                  Mar 2026  - 4 additional banks added, help screen moved to bank 11.
;-
;--------------------------------------------------------------------------------------------------------
;- This source file is free software: you can redistribute it and-or modify
;- it under the terms of the GNU General Public License as published
;- by the Free Software Foundation, either version 3 of the License, or
;- (at your option) any later version.
;-
;- This source file is distributed in the hope that it will be useful,
;- but WITHOUT ANY WARRANTY; without even the implied warranty of
;- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;- GNU General Public License for more details.
;-
;- You should have received a copy of the GNU General Public License
;- along with this program.  If not, see <http://www.gnu.org/licenses/>.
;--------------------------------------------------------------------------------------------------------

            ;===========================================================
            ;
            ; USER ROM BANK 11 - 
            ;
            ;===========================================================
            ORG     UROMADDR

            ;--------------------------------
            ; Common code spanning all banks.
            ;--------------------------------
            NOP
            HWSELROM2                                                        ; Select the first ROM page.
            ;
            ; No mans land... this should have switched to Bank 0 and at this point there is a jump to 00000H.
            JP      00000H                                                   ; This is for safety!!


            ;------------------------------------------------------------------------------------------
            ; Bank switching code, allows a call to code in another bank.
            ; This code is duplicated in each bank such that a bank switch doesnt affect logic flow.
            ;------------------------------------------------------------------------------------------            
            ALIGN_NOPS UROMBSTBL
            ;
BKSW11to0:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK0                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to1:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK1                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to2:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK2                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to3:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK3                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to4:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK4                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to5:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK5                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to6:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK6                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to7:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK7                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to8:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK8                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to9:  PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK9                                              ; Required bank to call.
            JR      BKSW11_0
BKSW11to10: PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK10                                             ; Required bank to call.
            JR      BKSW11_0
BKSW11to11: PUSH    AF
            LD      A, ROMBANK11                                             ; Calling bank (ie. us).
            PUSH    AF
            LD      A, ROMBANK11                                             ; Required bank to call.
            ;
BKSW11_0:   PUSH    HL                                                       ; Place function to call on stack
            LD      HL, BKSWRET11                                            ; Place bank switchers return address on stack.
            EX      (SP),HL
            LD      (TMPSTACKP),SP                                           ; Save the stack pointer as some old code corrupts it.
            BNKSWSEL
            JP      (HL)                                                     ; Jump to required function.
BKSWRET11:  POP     AF                                                       ; Get bank which called us.
            BNKSWSELRET
            POP     AF
            RET  

            ;-------------------------------------------------------------------------------
            ; HELP SCREEN - Moved from bank 6 to free message space.
            ;
            ; This bank contains the HELP command handler, PRTSTR routine,
            ; PRINTASCII character mapper, ATBL mapping table, and the help
            ; text itself. All self-contained so no cross-bank calls needed.
            ;-------------------------------------------------------------------------------

            ; Simple help screen to display commands.
HELP:       CALL    NL
            LD      DE, HELPSCR
            CALL    PRTSTR11
            RET

            ; Print string routine with full screen pause for help text.
            ; String is NULL terminated, CR (0DH) triggers line counting and pause.
PRTSTR11:   PUSH    AF
            PUSH    BC
            PUSH    DE
            LD      A,0
            LD      (TMPLINECNT),A
PRTST11A:   LD      A,(DE)
            CP      000H                                                 ; NULL terminates the string.
            JR      Z,PRTST11E
            CP      00DH                                                 ; CR triggers line count.
            JR      Z,PRTST11C
PRTST11B:   CALL    PRNTASC11
            INC     DE
            JR      PRTST11A
PRTST11C:   PUSH    AF
            LD      A,(TMPLINECNT)
            CP      24
            JR      Z,PRTST11F
            INC     A
PRTST11D:   LD      (TMPLINECNT),A
            POP     AF
            JR      PRTST11B
PRTST11F:   CALL    GETKY
            CP      ' '
            JR      NZ,PRTST11F
            XOR     A
            JR      PRTST11D
PRTST11E:   POP     DE
            POP     BC
            POP     AF
            RET

            ; Print a true ASCII character using the ATBL mapping table.
            ; Input: A = ASCII character (0-127). >= 128 calls Sharp PRNT directly.
PRNTASC11:  PUSH    HL
            PUSH    BC
            CP      080H
            JR      NC,PRNTASC11B
            CP      00DH                                                 ; CR: don't map, call Sharp directly.
            JR      Z,PRNTASC11B
            LD      HL,ATBL11
            LD      C,A
            LD      B,0
            ADD     HL,BC
            LD      A,(HL)
            CALL    ?DSP
PRNTASC11A: POP     BC
            POP     HL
            RET
PRNTASC11B: CALL    PRNT
            JR      PRNTASC11A

            ; TRUE ASCII TO DISPLAY CODE TABLE (128 bytes, same as bank 6 ATBL)
ATBL11:     DB      0CCH   ; NUL '\0' (null character)
            DB      0E0H   ; SOH (start of heading)
            DB      0F2H   ; STX (start of text)
            DB      0F3H   ; ETX (end of text)
            DB      0CEH   ; EOT (end of transmission)
            DB      0CFH   ; ENQ (enquiry)
            DB      0F6H   ; ACK (acknowledge)
            DB      0F7H   ; BEL '\a' (bell)
            DB      0F8H   ; BS  '\b' (backspace)
            DB      0F9H   ; HT  '\t' (horizontal tab)
            DB      0FAH   ; LF  '\n' (new line)
            DB      0FBH   ; VT  '\v' (vertical tab)
            DB      0FCH   ; FF  '\f' (form feed)
            DB      0FDH   ; CR  '\r' (carriage ret)
            DB      0FEH   ; SO  (shift out)
            DB      0FFH   ; SI  (shift in)
            DB      0E1H   ; DLE (data link escape)
            DB      0C1H   ; DC1 (device control 1)
            DB      0C2H   ; DC2 (device control 2)
            DB      0C3H   ; DC3 (device control 3)
            DB      0C4H   ; DC4 (device control 4)
            DB      0C5H   ; NAK (negative ack.)
            DB      0C6H   ; SYN (synchronous idle)
            DB      0E2H   ; ETB (end of trans. blk)
            DB      0E3H   ; CAN (cancel)
            DB      0E4H   ; EM  (end of medium)
            DB      0E5H   ; SUB (substitute)
            DB      0E6H   ; ESC (escape)
            DB      0EBH   ; FS  (file separator)
            DB      0EEH   ; GS  (group separator)
            DB      0EFH   ; RS  (record separator)
            DB      0F4H   ; US  (unit separator)
            DB      000H   ; SPACE
            DB      061H   ; !
            DB      062H   ; "
            DB      063H   ; #
            DB      064H   ; $
            DB      065H   ; %
            DB      066H   ; &
            DB      067H   ; '
            DB      068H   ; (
            DB      069H   ; )
            DB      06BH   ; *
            DB      06AH   ; +
            DB      02FH   ; ,
            DB      02AH   ; -
            DB      02EH   ; .
            DB      02DH   ; /
            DB      020H   ; 0
            DB      021H   ; 1
            DB      022H   ; 2
            DB      023H   ; 3
            DB      024H   ; 4
            DB      025H   ; 5
            DB      026H   ; 6
            DB      027H   ; 7
            DB      028H   ; 8
            DB      029H   ; 9
            DB      04FH   ; :
            DB      02CH   ; ;
            DB      051H   ; <
            DB      02BH   ; =
            DB      057H   ; >
            DB      049H   ; ?
            DB      055H   ; @
            DB      001H   ; A
            DB      002H   ; B
            DB      003H   ; C
            DB      004H   ; D
            DB      005H   ; E
            DB      006H   ; F
            DB      007H   ; G
            DB      008H   ; H
            DB      009H   ; I
            DB      00AH   ; J
            DB      00BH   ; K
            DB      00CH   ; L
            DB      00DH   ; M
            DB      00EH   ; N
            DB      00FH   ; O
            DB      010H   ; P
            DB      011H   ; Q
            DB      012H   ; R
            DB      013H   ; S
            DB      014H   ; T
            DB      015H   ; U
            DB      016H   ; V
            DB      017H   ; W
            DB      018H   ; X
            DB      019H   ; Y
            DB      01AH   ; Z
            DB      052H   ; [
            DB      059H   ; \  '\\'
            DB      054H   ; ]
            DB      0BEH   ; ^
            DB      03CH   ; _
            DB      0C7H   ; `
            DB      081H   ; a
            DB      082H   ; b
            DB      083H   ; c
            DB      084H   ; d
            DB      085H   ; e
            DB      086H   ; f
            DB      087H   ; g
            DB      088H   ; h
            DB      089H   ; i
            DB      08AH   ; j
            DB      08BH   ; k
            DB      08CH   ; l
            DB      08DH   ; m
            DB      08EH   ; n
            DB      08FH   ; o
            DB      090H   ; p
            DB      091H   ; q
            DB      092H   ; r
            DB      093H   ; s
            DB      094H   ; t
            DB      095H   ; u
            DB      096H   ; v
            DB      097H   ; w
            DB      098H   ; x
            DB      099H   ; y
            DB      09AH   ; z
            DB      0BCH   ; {
            DB      080H   ; |
            DB      040H   ; }
            DB      0A5H   ; ~
            DB      0C0H   ; DEL

            ; Help text.
            ;        1                                      40
HELPSCR:    IF      BUILD_ROMDISK+BUILD_PICOZ80 = 1
              DB    "0..9  - select RFS Drive",                             00DH
              IF    BUILD_ROMDISK = 1
               DB   "40    - 40 col mode",                                  00DH
               DB   "80    - 80 col mode",                                  00DH
              ENDIF
              DB    "ASMXXXX assemble into dest XXXX",                      00DH
              DB    "B     - toggle bell",                                  00DH
              DB    "BASIC - load BASIC SA-5510",                           00DH
              DB    "C     - clear mem $1200-$D000",                        00DH
              DB    "CPXXXXYYYYZZZZ - copy mem X->Y size Z",                00DH
              DB    "CPM   - load CPM",                                     00DH
              DB    "DUC[H|T]FN-dump sd file,H=hex+hdr,T=txt",              00DH
              DB    "DASMXXXX[YYYY] - disassemble X to Y",                  00DH
              DB    "DXXXX[YYYY] - dump mem X -> Y",                        00DH
              DB    "EC[FN]- erase file, FN=No, or Filename",               00DH
              DB    "FD/FL - fd dir/boot",                                  00DH
              DB    "f     - boot orig fd rom",                             00DH
              DB    "H     - help screen",                                  00DH
              DB    "IN[port,..] - read I/O port(s)",                       00DH
              DB    "IR/IC - rfs rom/sd card directory",                    00DH
              DB    "JXXXX - jump to addr X",                               00DH
              DB    "LT[FN]- load tape, FN=Filename",                       00DH
              DB    "LR[FN]- load rom, FN=No. or Filename",                 00DH
              DB    "LC[NX][FN]- load sd, FN=No or Filename",               00DH
              DB    "      - NX = no exec",                                 00DH
              DB    "MXXXX - edit mem at X",                                00DH
              DB    "OUT[port:val,..]-write I/O port(s)",                   00DH
              DB    "P     - test printer",                                 00DH
              DB    "QD/QL - QD dir/boot",                                  00DH
              DB    "R     - test RAM",                                     00DH
              DB    "SD2T  - copy sd to tape",                              00DH
              DB    "ST[XXXXYYYYZZZZ] - save mem to tape",                  00DH
              DB    "SC[XXXXYYYYZZZZ] - save mem to sd",                    00DH
              DB    "        X=start,Y=end,Z=exec",                         00DH
              DB    "T     - test timer",                                   00DH
              DB    "T2SD  - copy tape to sd",                              00DH
              DB    "V     - verify tape save",                             00DH
              DB    000H
            ENDIF
            IF      BUILD_SFD700 = 1
              DB    "ASMXXXX assemble into dest X",                         00DH
              DB    "B     - toggle keyboard bell.",                        00DH
              DB    "BASIC - Load BASIC SA-5510.",                          00DH
              DB    "C     - clear memory $1200-$D000.",                    00DH
              DB    "CPXXXXYYYYZZZZ - copy memory",                         00DH
              DB    "        X=src,Y=dst,Z=size",                           00DH
             ;DB    "CPM   - Load CPM.",                                    00DH
              DB    "DUC[H|T]FN-dump sd file,H=hex+hdr,T=txt",              00DH
              DB    "DXXXX[YYYY] - dump mem X to Y.",                       00DH
              DB    "DASMXXXX[YYYY]",                                       00DH
              DB    "        disassemble X to Y",                           00DH
              DB    "FD/FL - fd dir/boot",                                  00DH
              DB    "H     - this help screen.",                            00DH
              DB    "IR    - rfs rom dir listing.",                         00DH
              DB    "JXXXX - jump to location X.",                          00DH
              DB    "LT[FN]- load tape, FN=Filename",                       00DH
              DB    "LR[FN]- load rom, FN=No. or Filename",                 00DH
              DB    "      - add NX for no exec, ie.LRNX.",                 00DH
              DB    "MXXXX - edit memory starting at X.",                   00DH
              DB    "P     - test printer.",                                00DH
              DB    "QD/QL - QD dir/boot",                                  00DH
              DB    "R     - test dram memory.",                            00DH
              DB    "SD2T  - copy sd card to tape.",                        00DH
              DB    "ST[XXXXYYYYZZZZ] - save mem to tape.",                 00DH
              DB    "SC[XXXXYYYYZZZZ] - save mem to card.",                 00DH
              DB    "        X=start,Y=end,Z=exec",                         00DH
              DB    "T     - test timer.",                                  00DH
              DB    "T2SD  - copy tape to sd card.",                        00DH
              DB    "V     - verify tape save.",                            00DH
              DB    000H
            ENDIF

            ;-------------------------------------------------------------------------------
            ; END OF HELP SCREEN
            ;-------------------------------------------------------------------------------

            ; RomDisk - Pad to EFFF boundary.
            IF      BUILD_ROMDISK+BUILD_PICOZ80 = 1
              ALIGN 0EFF8h
              ORG   0EFF8h
              DB    0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh,0FFh            
            ENDIF
            ; SFD700 - Pad to 10000H
            IF      BUILD_SFD700 = 1
              ALIGN 10000H
            ENDIF
