Z80(KL5C80B12C)でも、Z80(Z80+SIO+PIO)同様に、64K化モニタに移行するtコマンドとCP/Mのシステムをブートするためのbコマンドを実装したいと思います。

Bコマンドについては、Z80(Z80+SIO+PIO)同様、SDCARDに実装するCP/Mシステムができた時点でコマンドを実行することができます。
Bコマンドはそれまでちょっとだけおあずけです。

tコマンド及びbコマンドは前々回Z80(Z80+8255+8255)とほぼ一緒なので、実装方法は、そちらを読んでいただくとして、説明は割愛いたします。

ソースプログラムとHEXファイルは下記のようになっています。
次回は、CP/Mのファイルシステムを作成したいと思います。

; KL5C80B12 monitor ver0.72(SPI+boot) by Pinecone 2020/11/02
;
; KL5C80B12 cpu
; rom 0000h -- 7fffh
; ram 8000h -- ffffh(MMU)
; clock 10MHz(ext.clock 20MHz)


; KL5C80B12 serial communication
; extemal Clock 614.4KHz(4.9152MHz TC74HC4040 1/8 Frequency division)
; Band Rate:38400  8bits 1Stopbits none Parity (81N)

 
; assembler
;  program start 0000H
;  main          0100H
;
; assemblers  ASxxxx and ASlink V5.10
; file name KL5C80_moni072_spi_boot.asm
; $ asz80 -l -s -o KL5C80_moni072_spi_boot.asm
; $ aslink -i KL5C80_moni072_spi_boot
; windows10 rom-writer TL866-2 plus KL5C80_moni072_spi_boot.ihx
;

        .z80
 
;;KL5C80 System control register

SCR0	.equ	0x3a	
SCR1	.equ	0x3b

;;KL5C80 Memory management unit

BBR1	.equ	0x00
BR1	.equ	0x01
BBR2	.equ	0x02
BR2	.equ	0x03
BBR3	.equ	0x04
BR3	.equ	0x05
BBR4	.equ	0x06
BR4	.equ	0x07

;;KL5C80 UART 

UART_D	.equ	0x38
UART_C	.equ	0x39

;;KL5C80 Parallel port B

PP2D	.equ	0x30
PP3D	.equ	0x31
PP4D	.equ	0x32
PPBC	.equ	0x33

	;OUTPUT-PORT
S_DI 	.equ 7 ;0b10000000 D7
S_CS 	.equ 6 ;0b01000000 D6
S_CLK 	.equ 5 ;0b00100000 D5

	;INPUT-PORT
DO 	.equ 0 ;0b00000001 D0

temp_addr       .equ    0x8000  ;moni move temprary address
ROM_RAM_SW_PORT	.equ	0x84	;D-FF rom/ram sw port
ON_RAM		.equ	0x03	;D0 0:ROM 1:RAM 
ON_ROM		.equ	0x00	;D1 0:LED_ROM 1:RAM_LED

       .area TEST(ABS)
 
        .org    0x0000
        jp      start
 
        .org    0x100

start:				;cold start
	ld	a,0b00000001	;RxC,TxC Port set
	out	(SCR0),a
	ld	a,0b11001111	;NMI,BREQ,BACK,HALT,M1 set Active mode : 0 wait set
	out	(SCR1),a

	ld	a,0b00000010	;Parallel port B 2x P0-P3 input P4-P7 OutPut Mode
	out	(PPBC),a
				;;normal mood extmem0 0x00000-0x1ffff
				;;            extmem1 0xe0000-0xfffff
	ld	a,0x3f		;Built-in ram non set (0xfe00-0xffff >> no ram)
	out	(BBR4),a
	ld	a,0x1f		;MMU 0x8000-0xffff setting
	out	(BBR3),a	;0xe0000-0xeffff >> 0x8000-0xffff
	ld	a,0xd8		;BR3-0xD8  BBR3-0x1f
	out	(BR3),a
        ld      sp,0xffff       ;stack pointer set
        call    com_init        ;serial channel initialize

ram_start:                          
        ld      hl,msg_op       ;opening message display
        call    msgout
prompt: ld      a,">"           ;main  routine
        call    putchar         ;prompt display
        call    getchar
        call    putchar
        cp      "?"             ;help ?
        call    z,help
        cp      "d"             ;memory display?
        call    z,dump
        cp      "m"             ;memory rewrite ?
        call    z,memory
        cp      "l"             ;hex file load?
        call    z,loadhex       
        cp      "j"             ;address jump?
        call    z,jump
        cp	"i"		;i/o register rewite
        call	z,ioreg
        cp	"c"
        call	z,calling	;address call
        cp      "t"
        call    z,trance        ;moni 64Kmemory trance
        cp      "a"
        call    z,spi_card      ;read/write spi sd-card
	cp	"b"		
	call	z,cpm_boot	;cpm system boot
        cp      0x0d
        jr	z,prompt
        cp 	0x00
        jr	z,prompt
        call	cr
        jr      prompt

help:   ld      hl,help_msg     ;help ?
        call    msgout
        ret
cr:
        ld	a,0x0d
        call	putchar
        ret
crret_t:
        call    cr
        ret
trance: 
        call    call_yes_msg
        call    getchar
        cp      "y"
        jr      nz,crret_t
        call    cr

				;ram    0x8000 to 0xffff
        ld      hl,0x0000       ;moni   copy at temp_addr
        ld      de,temp_addr
        ld      bc,prog_end
        ldir
        
        ld      hl,move_prog+temp_addr
        jp      (hl)            ;temprary address jump
;
move_prog:
        ld	a,ON_RAM        ;ram 0x0000 to 0x7fff setting
        out     (ROM_RAM_SW_PORT),a
        ld      hl,temp_addr    ;moni moveing at ram 0x0000 to prog_end
        ld      de,0x0000
        ld      bc,prog_end
        ldir
        ld      hl,go_moni
        jp      (hl)            ;ram monitor jump
;
go_moni:
	call	cr
	ld	hl,ram_msg
	call	msgout
	ld	sp,0xffff
        jp      ram_start

jump:                           ;address jump?
        ld      a," "
        call    putchar
        call    input_hl        ;dump address input
        cp      0x00
        jr      nz,crret
        ld      a," "
        call    putchar
        ld      a,"y"
        call    putchar
        ld      a,":"

        call    putchar
        call    getchar
        cp      "y"
        jr      nz,crret
        call    cr
        jp      (HL)       
crret:
        call    cr
        ret

calling:                         ;address call?
        ld      a," "
        call    putchar
        call    input_hl       	 ;call address input
        cp      0x00
        jr      nz,crret
        ld      a," "
        call    putchar
        ld      a,"y"
        call    putchar
        ld      a,":"
        call    putchar
        call    getchar
        cp      "y"
        jr      nz,crret
        call	cr
        ld	de,callret
        push	de
        jp	(HL)
callret:
        ret


loadhex:
        call    loadhexline     ;load 1line Hex file
        cp      0xff            ;hex file read err
        jr      z,loadhexerr
        cp      0x01            ;hex file end?
        jr      nz,loadhex
        ld      a,0x0d
        call    putchar
        ld      a,"O"
        call    putchar
        ld      a,"K"
        call    putchar
        ld      a,0x0d
        call    putchar
        ret

loadhexerr:
        ld      a,0x0d          ;hex file read skip
        call    putchar
        ld      a,"E"
        call    putchar
        ld      a,"R"
        call    putchar
        ld      a,"R"
        call    putchar
        ld      a,0x0d
        call    putchar
        ret
        
loadhexline:
        ld      a,0x0d
        call    putchar
        ld      d,0x00          ;checksum count clr
        call    getchar         ;record mark is ":"
        cp      ":"
        jr      nz,loadhexlineerr
        call    input_l         ;data size 1byte
        ld      b,l             ;checksum = checksum + data size
        ld      a,d
        add     b
        ld      d,a
        call    input_hl        ;offset address 2byte
        push    hl
        pop     ix              ;offset address HLreg >> IXreg
        ld      a,d             ;checksum = checksum + address Low      
        add     l               
        add     h               ;checksum = checksum + address Hi
        ld      d,a
        call    input_l         ;record type
        ld      a,l
        cp      0x00            ;data record
        jr      nz,checkrecord

        ;; *** data record job ****
data_hex:
        call    input_l         ;read hex 1byte in Lreg(hex)
        ld      (ix),l          ;offset address < hex data
        inc     ix              ;offset address +1
        ld      a,l
        add     d               ;cheksum Dreg = Dreg + Lreg
        ld      d,a
        djnz    data_hex        ;Breg(data size) == 0?
        call    input_l         ;chechsum OK?
        ld      a,d
        add     l
        cp      0x00
        jr      nz,loadhexlineerr
        call    getchar
        cp      0x0a            ;load 1line hex LF?
        jr      nz,loadhexlineerr
        ld      a,0x00
        ret

checkrecord:                    ;end record ?
        cp      0x01
        jr      nz,loadhexlineerr
        call    input_l
        ld      a,l
        cp      0xff
        jr      nz,loadhexlineerr
        ld      a,0x01
        ret

loadhexlineerr:
        ld      a,0xff          ;read record err (no support)
        ret     

dump:                           ;memory dump
        ld      a," "
        call    putchar
        call    input_hl        ;dump address input
        cp      0x00
        jr      z,dump1
        push    iy              ;memory dump address set
        pop     hl
dump1:
        call    dump_hl         ;memory dump
        push    hl              ;memory address IY set
        pop     iy
        ret

memory:                         ;memory change
        ld      a," "           ;space
        call    putchar         
        call    input_hl        ;change memory address 
        cp      0x00
        jr      z,memory1       ;no address hex code 
        ld      a,0x0d
        call    putchar
        ret
memory1:
        ld      a," "           ;current memory display
        call    putchar
        ld      a,(HL)
        call    hex_a_disp
        ld      a," "
        call    putchar
        push    hl
        call    input_l         ;new memory conntents input
        cp      0x00
        jr      z,memory2
        pop     hl
        xor	a
        ret
memory2:
        ld      c,l             ;memory contents update
        pop     hl
        ld      (hl),c
        ld      a,0x0d
        call    putchar
        ret

ioreg:                         ;ioreg change
        ld      a," "           ;space
        call    putchar
        call    input_l        ;change i/o register
        cp      0x00
        jr      z,ioreg1       ;no address hex code
        ld      a,0x0d
        call    putchar
        ret
ioreg1:
        ld      a," "           ;current i/o address display
        call    putchar
        ld	b,0x00
        ld	c,l
        in      a,(c)
        call    hex_a_disp
        ld      a," "
        call    putchar
        call    input_l         ;new i/o conntents address input
        cp      0x00
        jr      z,ioreg2
        xor	a
        ret
ioreg2:
        ld      a,l             ;new i/o address update
        out     (c),a
        ld      a,0x0d
        call    putchar
        ret

call_yes_msg:
        push    hl
        ld      hl,yes_msg
        call    msgout
        pop     hl
        ret

cpm_boot:
	ld	hl,boot_yes_msg
	call 	msgout   
        call    getchar
        cp      "y"
        ret      nz

	call	cr
	call	cmd17_24_copy		;command rom >> ram area copy
        call    spi_card_init		;sdcard initialize

	ld	hl,0x0000		;CP/M system block 0x0000 start 
	ld	(rw_block_no),hl
	ld	hl,0x000b		;ccp+bdos >0x0b (512byte * 0x000b)
	ld	(rw_block),hl
	ld	hl,0xdc00		;62K cp/m ccp top addr >> 0xdc00
	ld	(rw_addr),hl
	call	spi_read_go
	cp	0x01
	ret	z
	ld	hl,0x000b		;CP/M bios block 0x000b start 
	ld	(rw_block_no),hl
	ld	hl,0x0004		;bios >0x04 (512byte * 4 bios area)
	ld	(rw_block),hl
	ld	hl,0xf200		;62K cp/m bios top addr >> 0xf200
	ld	(rw_addr),hl
	call	spi_read_go
	cp	0x01
	ret	z
	ld	hl,cpm_boot_msg
	call	msgout

        ld      hl,boot_move_prog       ;64K swiching ram progrum  copy at temp_addr
        ld      de,temp_addr		;transfer boot_move_prog ~ boot_move_end >>temp_addr(0x8000)
        ld      bc,boot_move_end
        ldir
        
        ld      hl,temp_addr
        jp      (hl)            	;temprary address jump(0x8000)
;
boot_move_prog:				;transfer address (temp_addr:0x8000)
        ld	a,ON_RAM        	;ram 0x0000 to 0x7fff setting
        out     (ROM_RAM_SW_PORT),a
	nop
	nop
	jp	0xf200			;62K CP/M bios top address
boot_move_end:

spi_read_go:
	ld	hl,(rw_block_no)
 
        ld      a,(r7)
        bit     6,a
        jp      z,spi_read_SD_ver2
        ld      a,h                   ;SDHC
        ld      (cmd18+03),a
        ld      a,l
        ld      (cmd18+04),a
        jr      spi_read01
spi_read_SD_ver2:                        ;Ver2
        ld      a,h
        ld      (cmd18+02),a
        ld      a,l
        ld      (cmd18+03),a    


spi_read01: 

        call    dummy_data
        ld      hl,cmd18
        call    cmd_out
        call    r1_resp
        cp      0x00
        jp      nz,cmd18_err

sector_read_loop:
cmd18_st:
        call    r1_resp                 ;read data start response?
        cp      0xfe                    ;data start byte 0xfe?
        jr nz,  cmd18_st


	ld	hl,(rw_addr)

        ld      b,0x00
cmd18_r:
	call	resp				;read data
	ld	(hl),a
	inc	hl
        djnz    cmd18_r

;	2st dummy read
 
        ld      b,0x00
cmd18_r2st:					;2st 256byte write
	call	resp
	ld	(hl),a
	inc	hl
        djnz    cmd18_r2st

	ld	(rw_addr),hl

        call    resp                    ;crc Skipping read
        call    resp                    ;crc Skipping read

	ld	hl,(rw_block)
	dec	hl
	ld	(rw_block),hl
	ld	a,h			;HL = 0?
	or	l
	jr	nz,sector_read_loop

        ld      d,0xff                  ;dummy data 0xff(cs low)
        call    spi_8bit


       	ld      hl,cmd12		;stop cmd
        call    cmd_out                 ;cmd12 Issue
	ld	d,0xff
	call	spi_8bit
        call    r1_resp
        cp      0x00                    ;response is 0x00 read mode OK?
        jr      nz,cmd18_err

cmd18_wch:
	call	resp
	cp	0x00
	jr	z,cmd18_wch

	call	set_cs

	xor	a
	ret


;; 1 sector write 


cmd18_err: 
cmd18_err2:
	ld	hl,cmd25_err_msg
	call    msgout
	call	set_cs
	ld	a,0x01
	ret


spi_card:
        call    getchar
        cp      "i"
        call    z,spi_init
        cp      "r"
        call    z,spi_read
        cp      "w"
        call    z,spi_write
        ret

spi_read:
        ld      hl,read_block_no_msg
        call    msgout
        call    input_hl
        cp      0x00
        jr      z,spi_read1
        call    cr
        xor   a
        ret
spi_read1:
        ld      a,(r7)
        bit     6,a
        jp      z,read_SD_ver2
        ld      a,h                    ;SDHC
        ld      (cmd17+03),a
        ld      a,l
        ld      (cmd17+04),a
        jr      spi_read2
read_SD_ver2:
        ld      a,h                    ;Ver2
        ld      (cmd17+02),a
        ld      a,l
        ld      (cmd17+03),a
spi_read2:
        ld      hl,save_addr_msg
        call    msgout
        call    input_hl
        cp      0x00
        jr      z,cmd17_out
        call    cr
        xor     a
        ret

cmd17_out:
        call    call_yes_msg
        call    getchar
        cp      "y"
        jp      nz,spi_ret

        push    hl
        call    dummy_data              ;dummy data(0xff) out
        ld      hl,cmd17
        call    cmd_out                 ;cmd17 Issue
        call    r1_resp
        cp      0x00                    ;response is 0x00 read mode OK?
        jr      nz,cmd17_err
cmd17_st:
        call    r1_resp                 ;read data start response?
        cp      0xfe                    ;data start byte 0xfe?
        jr nz,  cmd17_st
        ld      b,0x00                  ;read data size 256byte
        pop        hl           ;read data buff address
cmd17_r:
        call    resp                    ;read data
        ld      (hl),a
        inc     hl
        djnz    cmd17_r

        ld      b,0x00

cmd17_r2:
        call    resp
        ld      (hl),a                  ;read data
        inc     hl
        djnz    cmd17_r2

        call    resp                    ;crc Skipping read
        call    resp                    ;crc Skipping read
        ld      d,0xff                  ;dummy data 0xff(cs low)
        call    spi_8bit

                ;memory dump address

        ld      bc,0x0200
        sbc     hl,bc
                                ;memory dump 512b
        ld      a,0x0d
        call    putchar
        
  
        call    hl_disp256              ;256bye dump 
        call    putchar
        call    hl_disp256              ;256bye dump
        call    putchar

        call	set_cs		          ;CS="H"
        xor        a
        ret

cmd17_err:
	pop	hl
	ld	hl,cmd17_err_msg
	call    msgout
        call	set_cs		          ;CS="H"
	ret

hl_disp256:
        push    bc
        ld      b,0x00                  ;dump data size 256bye
        push    hl
        ld      hl,dump_msg             ;dump address msg output
        call    msgout
        pop     hl
        ld      a,h                     ;dump address HLreg
        call    hex_a_disp              ;dump address output
        ld      a,l
        call    hex_a_disp
        ld      a," "
        call    putchar
        call    putchar
hl_disp256_loop:
        ld      a,(HL)                  ;dump addres data (HL)
        inc     HL
        call    hex_a_disp
        ld      a,b
        cp      0x01                    ;end dump size?
        jr      z,hl_disp256_loop1
        ld      a,b
        and     0x0f
        cp      0x01                    ;0x?0-0x?f is 0x01(1line disp end?)
        jr      z,hl_disp256_cr
        ld      a," "                   ;space Insert
        call    putchar
        jr      hl_disp256_loop1
hl_disp256_cr:
        ld      a,0x0d
        call    putchar
        ld      a,h                     ;dump address HLreg
        call    hex_a_disp              ;dump address output
        ld      a,l
        call    hex_a_disp
        ld      a," "
        call    putchar
        call    putchar
hl_disp256_loop1:

        djnz    hl_disp256_loop

        ld      a,0x0d
        call    putchar
        pop     bc
        ret

spi_write:

        ld      hl,write_block_no_msg
        call    msgout
        call    input_hl
        cp      0x00
        jr      z,spi_write1
        call    cr
        xor     a
        ret
spi_write1:
        ld      a,(r7)
        bit     6,a
        jp      z,write_SD_ver2
        ld      a,h                   ;SDHC
        ld      (cmd24+03),a
        ld      a,l
        ld      (cmd24+04),a
        jr      spi_write2
write_SD_ver2:                        ;Ver2
        ld      a,h
        ld      (cmd24+02),a
        ld      a,l
        ld      (cmd24+03),a    


spi_write2:
        ld      hl,write_addr_msg
        call    msgout
        call    input_hl
        cp      0x00
        jr      z,cmd24_out
        call    cr
        xor     a
        ret

cmd24_out:       
        call    call_yes_msg
        call    getchar
        cp      "y"
        jp      nz,spi_ret
        push    hl
        call    dummy_data
        ld      hl,cmd24
        call    cmd_out
        call    r1_resp
        cp      0x00
        jp      nz,cmd24_err
        ld      d,0xfe
        call    spi_8bit
        pop     hl
        ld      b,0x00
cmd24_w:
        ld      d,(hl)
        inc     hl
        call    spi_8bit
        djnz    cmd24_w
 
        ld      b,0x00
cmd24_w2:
        ld      d,(hl)
        inc     hl  
        call    spi_8bit
        djnz    cmd24_w2
        ld      d,0xff
        call    spi_8bit    ;crc
        call    spi_8bit    ;crc
        call    r1_resp
        and     0x0f
        cp      0x05
        jr      nz,cmd24_err2
cmd24_wch:
        call    resp
        cp      0x00
        jr      z,cmd24_wch
        call    dummy_data
 
        ld      bc,0x0200
        sbc     hl,bc

                                ;memory dump 512b
        ld      a,0x0d
        call    putchar
        
  
        call    hl_disp256              ;256bye dump 
        call    putchar
        call    hl_disp256              ;256bye dump
        call    putchar

        call	set_cs		          ;CS="H"
        xor     a
        ret

cmd24_err:
	pop	hl
cmd24_err2:
	ld	hl,cmd24_err_msg
	call    msgout
        call	set_cs		          ;CS="H"
	ret



spi_init:
	call	cmd17_24_copy

        ld      hl,spi_init_msg
        call    msgout
        call    call_yes_msg
        call    getchar
        cp      "y"
        jp      nz,spi_ret
        call    cr
        call    spi_card_init
        ld      hl,ok_msg
        call    msgout
        ret

cmd17_24_copy:
	ld	b,0x06
	ld	hl,m_cmd17
	ld	de,cmd17
cmd17_copy:
	ld	a,(HL)
	ld	(DE),a
	inc	hl
	inc	de
	djnz	cmd17_copy

	ld	b,0x06
	ld	hl,m_cmd24
	ld	de,cmd24
cmd24_copy:
	ld	a,(HL)
	ld	(DE),a
	inc	hl
	inc	de
	djnz	cmd24_copy

	ld	b,0x06
	ld	hl,m_cmd18
	ld	de,cmd18
cmd18_copy:
	ld	a,(HL)
	ld	(DE),a
	inc	hl
	inc	de
	djnz	cmd18_copy

	ld	b,0x06
	ld	hl,m_cmd12
	ld	de,cmd12
cmd12_copy:
	ld	a,(HL)
	ld	(DE),a
	inc	hl
	inc	de
	djnz	cmd12_copy
	ret

spi_card_init:
        xor     a
        set     S_CS,a          ;CS="H" 
        set     S_DI,a          ;DI="H"
        out    (PP2D),a
        ld      b,80            ;80 clok output
        call    loopspii

        xor     a
        out     (PP2D),a     ;CS="L" DI="L" CLK="L"

        call    cmd0_out
        cp      0x01
        jp      nz,init_err
        
        call    cmd8_out
        cp      0x01
        jp      nz,init_err
        call    cmd55_out
        cp      0x01
        jp      nz,init_err
        call    acmd41_out
        call    cmd58_out
        ld      a,(r7)
        bit     7,a
        jp      z,init_err
        bit     6,a
        jr      z,SD_ver2
        ld      hl,sdhc_msg
init_msg:
        call    msgout
	call	set_cs
        ret
SD_ver2:
        ld      hl,sd_ver2_msg
	call	set_cs
        jr      init_msg

;cmd0_out: 
;        ld      hl,cmd0
;        call    cmd_out
;        call    r1_resp
;        ret
;
cmd0_out: 
        ld      hl,cmd0
        call    cmd_out
        call    r1_resp
        push    af
        ld      hl,cmd0_msg
        call    msgout
        pop     af
        push    af
        call    hex_a_disp
        ld      a,0x0d
        call    putchar
        pop     af
        ret
cmd0_msg:
        .str    "CMD0:"
        .db     0x00

        
;cmd8_out:
;        call    dummy_data
;        ld      hl,cmd8
;        call    cmd_out
;        call    r1_resp
;        cp      0x01
;        ret     nz
;        ld      b,4
;        ld      hl,r7
;cmd8_r:
;        call    resp
;        ld      (hl),a
;        inc     hl
;        djnz    cmd8_r
;        ld      a,0x01
;        ret
;
cmd8_out:
        call    dummy_data
        ld      hl,cmd8
        call    cmd_out
        call    r1_resp
        cp      0x01
        ret     nz
        ld      b,4
        ld      hl,r7
cmd8_r:
        call    resp
        ld      (hl),a
        inc     hl
        djnz    cmd8_r
cmd8_r_d:
        ld      hl,cmd8_msg
        call    msgout
        ld      a,0x01
        call    hex_a_disp
        ld      hl,r7
        ld      b,4
cmd8_r_d_loop:
        ld      a,(hl)
        call    hex_a_disp
        inc     hl      
        djnz    cmd8_r_d_loop           
        ld      a,0x0d
        call    putchar
        ld      a,0x01
        ret
cmd8_msg:
        .str    "CMD8:"
        .db     0x00

;cmd55_out:
;        call    dummy_data
;        ld      hl,cmd55
;        call    cmd_out
;        call    r1_resp
;        ret
;
cmd55_out:
        call    dummy_data
        ld      hl,cmd55
        call    cmd_out
        call    r1_resp
        push    af
        ld      hl,cmd55_msg
        call    msgout
        pop     af
        push    af
        call    hex_a_disp
        ld      a,0x0d
        call    putchar
        pop     af
        ret
cmd55_msg:
        .str    "CMD55:"
        .db     0x00

;acmd41_out:
;        call    dummy_data
;        ld      hl,acmd41               ;sd-card Initialize
;        call    cmd_out
;        call    r1_resp
;        cp      0x00
;        ret     z
;        call    cmd55_out
;        jr      acmd41_out
;
acmd41_out:
        call    dummy_data
        ld      hl,acmd41               ;sd-card Initialize
        call    cmd_out
        call    r1_resp
        cp      0x00
        push    af
        ld      hl,acmd41_msg
        call    msgout
        pop     af
        push    af
        call    hex_a_disp
        ld      a,0x0d
        call    putchar
        pop     af
        ret     z
        call    cmd55_out
        jr      acmd41_out
acmd41_msg:
        .str    "ACMD41:"
        .db     0x00


spi_ret:
        call    cr
        ret

init_err:
        ld      hl,spi_init_err_msg
        call    msgout
        call    cr
        jp      ram_start      


cmd58_out:
        call    dummy_data
        ld      hl,cmd58
        call    cmd_out
        call    r1_resp
        push    af
        ld      b,4
        ld      hl,r7
cmd58_r:
        call    resp
        ld      (hl),a
        inc     hl
        djnz    cmd58_r
        pop     af
        ret

dummy_data:
        xor     a
        set     S_CS,a          ;CS="H"
        set     S_DI,a          ;DI="H"
        out    (PP2D),a
        ld      b,8            ;1clok output
        call    loopspii
        xor     a
	set     S_CS,a          ;CS="H"	
	set	S_DI,a		;DI="H"
        out    (PP2D),a
        ret

loopspii:
        res     S_CLK,a         ;CLK    L
        out    (PP2D),a
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop

        set     S_CLK,a         ;CLK    H
        out    (PP2D),a
        nop
        nop
        nop
        nop
        nop
        djnz    loopspii
        ret
r1_resp:
        call    resp
        cp      0xff
        jr      z,r1_resp
        ret

resp:
        ld      d,0xff  
        call    spi_8bit
        ld      a,e
        ret

cmd_out:                        ;command out
        push    hl
        push    bc
        ld      b,0x06          ;command line word count
cmd_out1:
        ld      d,(hl)          ;command line area
        call    spi_8bit
        inc     hl
        djnz    cmd_out1
        pop     bc
        pop     hl
        ret

spi_8bit:                       ;outdata Dreg,inputdata Ereg
        push    bc
        ld      c,0x00
        ld      b,0x08        
loopspi:
        xor     a
        rlc     d
        rra
        res     S_CLK,a         ;CLK    L
        out    (PP2D),a
        nop
        nop
        nop
        nop
        nop
        nop
        set     S_CLK,a         ;CLK    H
        out    (PP2D),a
        in     a,(PP2D)
        rrca
        rl      e
        djnz    loopspi
        xor     a
	set	S_DI,a		;DI  H
        out    (PP2D),a
        pop     bc
        ret

set_cs:
	xor	a
	set     S_CS,a          ;CS="H"
	set	S_DI,a		;DI="H"
	out    (PP2D),a
        ret


input_l:                        ;input Lreg hex (1byte)
        push    bc
        ld      b,0x02
        jr      input_loop

input_hl:                       ;input HLreg hex (2byte)
        push    bc
        ld      b,0x04
input_loop:
        ld      hl,0x0000       ;HL reg cler
        jr      input_hl2
shift_hl:
        add     hl,hl           ;HL reg shift left 4bit
        add     hl,hl
        add     hl,hl
        add     hl,hl
input_hl2:
        call    getchar         ;input ascii hex 1char
        call    putchar
        call    ascii_hex       ;ascii hex 1char >> Areg
        cp      0xff
        jr      z,input_hl_err
        and     0x0f            ;Areg low order 4bit >> Lreg
        or      l
        ld      l,a
        djnz    shift_hl
input_hl_end:
        pop     bc
        xor     a               ;OK >> Areg 0x00
        ret
input_hl_err:
        ld      a,0xff          ;NG >> Areg 0xff
        pop     bc
        ret

dump_hl:                        ;memory dump 1line(+0 -- +f)
        ld      a,0x0d
        call    putchar
        push    hl
        ld      hl,dump_msg     ;dump address msg output
        call    msgout
        pop     hl

        ld      a,h             ;dump address HLreg
        call    hex_a_disp      ;dump address output
        ld      a,l
        call    hex_a_disp
        ld      a," "
        call    putchar
        call    putchar
        ld      b,0x10          ;address --> address +0f
hl_disp:
        ld      a,(HL)          ;dump addres data (HL)
        inc     HL
        call    hex_a_disp
        ld      a," "
        call    putchar
        djnz    hl_disp

        ld      a,0x0d
        call    putchar
        ret

hex_a_disp:                     ;Areg(HEX) >> putchar(areg)
        push    af
        rrca                    ;7-4bit Right shift
        rrca
        rrca
        rrca
        call    hex_ascii       ;3-0bit Hex >> Hex ascii(0-F)
        call    putchar         ;7-4bit putchar
        pop     af
        call    hex_ascii       ;3-0bit Hex >> Hex ascii(0-F)
        call    putchar         ;3-0bit putchar
        ret


ascii_hex:
        sub     0x30
        jr      c,err_hex
        cp      0x0A            ;0-9
        jr      c,hex_1_9
        sub     0x11
        cp      0x06            ;A-F
        jr      c,hex_A_F
        sub     0x20
        cp      0x06            ;a-f
        jr      nc,err_hex
hex_A_F:
        add     0x0a
hex_1_9:
        ret
err_hex:ld      a,0xff
        ret

hex_ascii:                      ;Areg(HEX) >> Areg(ascii code)  
        and     a,0x0f
        cp      0x0a
        jr      c,ascii0_9
        add     0x37
        ret
ascii0_9:
        add     0x30
        ret

                        
        
msgout:
        ld      a,(hl)          ;(HL)reg is output disp at 0x00
        cp      0x00
        ret     z
        call    putchar
        inc     hl
        jr      msgout


getchar:
	in	a,(UART_C)	;Rx status in
	bit	1,a		;RxRdy ?
	jr	z,getchar
	in	a,(UART_D)	;Rx data in
	ret

putchar:push	af
putchar01:
	in	a,(UART_C)	;Tx status in
	bit	0,a		;TxRdy ?
	jr	z,putchar01
	pop	af
	out	(UART_D),a	;Tx data out
	ret

;

com_init:
	ld	a,0b01001110	;8bit NonParity 1StopBit 1/16Portrait
	out	(UART_C),a
	ld	a,0b00000101	;Rx or Tx Enable
	out	(UART_C),a	
	ret 



msg_op: .str    "Monitor KL5C80B12 CPU ver0.72(SPI+boot) by Pinecone 2020/11/02"
        .db     0x0d
        .db     0x00
help_msg:
        .db     0x0d    
        .str    "?  Help"
        .db     0x0d
        .str    "d  memory dump"
        .db     0x0d
        .str    "m  memory change   address old new"
        .db     0x0d
        .str    "l  hex file load"
        .db     0x0d
        .str    "j  jump"
        .db     0x0d
        .str	"i  i/o register    i/o-address old new"
        .db	0x0d
        .str	"c  call"
        .db	0x0d
        .str    "t  ram area trance monitor jump"
        .db     0x0d
        .str    "b  CP/M system boot at SDCARD"
        .db     0x0d
       	.str  	"ai spi(sd-card) Initialization"
        .db   	0x0d
        .str  	"ar spi(sd-card) read  (512byte/block)  0x0000-0xffff set"
        .db  	0x0d
        .str 	"aw spi(sd-card) write (512byte/block)  0x0000-0xffff set"
        .db  	0x0d 	
        .db     0x00

dump_msg:
        .str    "addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F"
        .db     0x0d
        .db     0x00
yes_msg:
        .str    " Yes:"
        .db     0x00
ok_msg:
        .str    "OK"
        .db     0x0d
        .db     0x00
err_msg:
        .str    "ERR"
        .db     0x0d
        .db     0x00
read_block_no_msg:
        .str    " read block No:"
        .db     0x00
write_block_no_msg:
        .str    " write block No:"
        .db     0x00
save_addr_msg:
        .str    " save addr:"
        .db     0x00
write_addr_msg:
        .str    " write addr:"
        .db     0x00
spi_init_err_msg:
        .str    "Spi Init Err"
        .db     0x0d
        .db     0x00    
cmd17_err_msg:
        .str    "cmd17 Err"
        .db     0x0d
        .db     0x00  
cmd24_err_msg:
        .str    "cmd24 Err"
        .db     0x0d
        .db     0x00
cmd25_err_msg:
        .str    "cmd25 Err"
        .db     0x0d
        .db     0x00 
sdhc_msg:
        .str    ":SDHC"
        .db     0x0d
        .db     0x00    
sd_ver2_msg:
        .str    ":SD Ver2"
        .db     0x0d
        .db     0x00
spi_init_msg:
        .str    " spi(sd-card) Initialize :"
        .db     0x00 
ram_msg:
	.str	"RAM area switching monitor"
	.db     0x0d
	.db	0x00
boot_yes_msg:
	.db	0x0d
	.str	"CP/M boot ? yes:"
	.db	0x00
cpm_boot_msg:
	.str	"CP/M boot ...."
	.db     0x0d
	.db	0x00
cmd0:                           ;;48bit(6byte) Fixed length 
        .db     0x40            ;;command index 
        .db     0x00            ;;argument
        .db     0x00            ;;argument
        .db     0x00            ;;argument
        .db     0x00            ;;argument
        .db     0x95            ;;CRC  
;cmd1:
;       .db     0x41            ;;command init
;       .db     0x00
;       .db     0x00
;       .db     0x00
;       .db     0x00
;       .db     0xf9            ;;CRC
cmd8:
        .db     0x48
        .db     0x00
        .db     0x00
        .db     0x01
        .db     0xaa
        .db     0x87
acmd41:
        .db     0x69
        .db     0x40
        .db     0xff
        .db     0x80
        .db     0x00
        .db     0xff
cmd58:
        .db     0x7a
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0xff

cmd55:
        .db     0x77
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0xff

cmd16:
        .db     0x50
        .db     0x00
        .db     0x00
        .db     0x02
        .db     0x00
        .db     0xff
m_cmd17:
        .db     0x51
        .db     0x00
        .db     0x00
        .db     0x02
        .db     0x00
        .db     0xff
m_cmd18:
        .db     0x52
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0xff
m_cmd12:
        .db     0x4c
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0xff
m_cmd24:
        .db     0x58
        .db     0x00
        .db     0x00
        .db     0x02
        .db     0x00
        .db     0xff
prog_end:
	.org	0xff00
cmd17:
        .db     0x51
        .db     0x00
        .db     0x00
        .db     0x02
        .db     0x00
        .db     0xff
cmd18:
        .db     0x52
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0xff
cmd12:
        .db     0x4c
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0x00
        .db     0xff

cmd24:
        .db     0x58
        .db     0x00
        .db     0x00
        .db     0x02
        .db     0x00
        .db     0xff

r7:
        .ds     4 
rw_block_no:
	.ds	2
rw_block:
	.ds	2
rw_addr:
	.ds	2  

HEXファイルです。

:03000000C3000139
:200100003E01D33A3ECFD33B3E02D3333E3FD3063E1FD3043ED8D30531FFFFCD8A08219378
:2001200008CD6C083E3ECD7F08CD7608CD7F08FE3FCC6E01FE64CCA502FE6DCCBB02FE6C5C
:20014000CC1202FE6ACCBD01FE69CCEF02FE63CCE701FE74CC7F01FE61CC1E04FE62CC2E2F
:2001600003FE0D28BFFE0028BBCD750118B621D308CD6C08C93E0DCD7F08C9CD7501C9CD51
:200180002503CD7608FE7920F2CD7501210000110080017B0BEDB0219B81E93E03D384216B
:2001A0000080110000017B0BEDB021AE01E9CD750121010BCD6C0831FFFFC31E013E20CDE4
:2001C0007F08CDDC07FE00201A3E20CD7F083E79CD7F083E3ACD7F08CD7608FE792004CD74
:2001E0007501E9CD7501C93E20CD7F08CDDC07FE0020F03E20CD7F083E79CD7F083E3ACD22
:200200007F08CD7608FE7920DACD7501111102D5E9C9CD4C02FEFF2819FE0120F53E0DCD28
:200220007F083E4FCD7F083E4BCD7F083E0DCD7F08C93E0DCD7F083E45CD7F083E52CD7FBF
:20024000083E52CD7F083E0DCD7F08C93E0DCD7F081600CD7608FE3A2048CDD707457A80C0
:2002600057CDDC07E5DDE17A858457CDD7077DFE002020CDD707DD7500DD237D825710F33E
:20028000CDD7077A85FE002019CD7608FE0A20123E00C9FE01200BCDD7077DFEFF20033E41
:2002A00001C93EFFC93E20CD7F08CDDC07FE002803FDE5E1CD0208E5FDE1C93E20CD7F080B
:2002C000CDDC07FE0028063E0DCD7F08C93E20CD7F087ECD33083E20CD7F08E5CDD707FE62
:2002E000002803E1AFC94DE1713E0DCD7F08C93E20CD7F08CDD707FE0028063E0DCD7F084B
:20030000C93E20CD7F0806004DED78CD33083E20CD7F08CDD707FE002802AFC97DED793E84
:200320000DCD7F08C9E5215D0ACD6C08E1C9211D0BCD6C08CD7608FE79C0CD7501CDFC051E
:20034000CD3506210000221CFF210B00221EFF2100DC2220FFCD9103FE01C8210B00221CFC
:20036000FF210400221EFF2100F22220FFCD9103FE01C8212F0BCD6C0821880311008001C4
:200380009103EDB0210080E93E03D3840000C300F22A1CFF3A18FFCB77CAA6037C3209FF54
:2003A0007D320AFF18087C3208FF7D3209FFCD61072106FFCD9C07CD8D07FE00C21204CD2A
:2003C0008D07FEFE20F92A20FF0600CD9507772310F90600CD9507772310F92220FFCD9564
:2003E00007CD95072A1EFF2B221EFF7CB520D016FFCDAA07210CFFCD9C0716FFCDAA07CD2C
:200400008D07FE00200CCD9507FE0028F9CDCF07AFC921CA0ACD6C08CDCF073E01C9CD7657
:2004200008FE69CCDB05FE72CC3104FE77CC2705C9216C0ACD6C08CDDC07FE002805CD7505
:2004400001AFC93A18FFCB77CA55047C3203FF7D3204FF18087C3202FF7D3203FF218D0AD3
:20046000CD6C08CDDC07FE002805CD7501AFC9CD2503CD7608FE79C23607E5CD61072100B4
:20048000FFCD9C07CD8D07FE002040CD8D07FEFE20F90600E1CD9507772310F90600CD955D
:2004A00007772310F9CD9507CD950716FFCDAA07010002ED423E0DCD7F08CDD604CD7F0861
:2004C000CDD604CD7F08CDCF07AFC9E121B40ACD6C08CDCF07C9C50600E521260ACD6C0857
:2004E000E17CCD33087DCD33083E20CD7F08CD7F087E23CD330878FE01282378E60FFE0135
:2005000028073E20CD7F0818153E0DCD7F087CCD33087DCD33083E20CD7F08CD7F0810D13E
:200520003E0DCD7F08C1C9217C0ACD6C08CDDC07FE002805CD7501AFC93A18FFCB77CA4B6C
:20054000057C3215FF7D3216FF18087C3214FF7D3215FF21990ACD6C08CDDC07FE0028058C
:20056000CD7501AFC9CD2503CD7608FE79C23607E5CD61072112FFCD9C07CD8D07FE00C22D
:20058000D00516FECDAA07E106005623CDAA0710F906005623CDAA0710F916FFCDAA07CDA7
:2005A000AA07CD8D07E60FFE052026CD9507FE0028F9CD6107010002ED423E0DCD7F08CD90
:2005C000D604CD7F08CDD604CD7F08CDCF07AFC9E121BF0ACD6C08CDCF07C9CDFC0521E68A
:2005E0000ACD6C08CD2503CD7608FE79C23607CD7501CD350621630ACD6C08C90606216387
:200600000B1100FF7E12231310FA060621750B1112FF7E12231310FA060621690B1106FF99
:200620007E12231310FA0606216F0B110CFF7E12231310FAC9AFCBF7CBFFD3300650CD75B8
:2006400007AFD330CD8006FE01C23A07CDA206FE01C23A07CDE206FE01C23A07CD0807CDB5
:2006600046073A18FFCB7FCA3A07CB77280A21D50ACD6C08CDCF07C921DC0ACDCF0718F1B3
:20068000213F0BCD9C07CD8D07F5219C06CD6C08F1F5CD33083E0DCD7F08F1C9434D4430DA
:2006A0003A00CD610721450BCD9C07CD8D07FE01C006042118FFCD9507772310F921DC0679
:2006C000CD6C083E01CD33082118FF06047ECD33082310F93E0DCD7F083E01C9434D4438EB
:2006E0003A00CD610721570BCD9C07CD8D07F5210107CD6C08F1F5CD33083E0DCD7F08F15A
:20070000C9434D4435353A00CD6107214B0BCD9C07CD8D07FE00F5212E07CD6C08F1F5CDDE
:2007200033083E0DCD7F08F1C8CDE20618DA41434D4434313A00CD7501C921A60ACD6C08AD
:20074000CD7501C31E01CD610721510BCD9C07CD8D07F506042118FFCD9507772310F9F1BD
:20076000C9AFCBF7CBFFD3300608CD7507AFCBF7CBFFD330C9CBAFD3300000000000000097
:2007800000CBEFD330000000000010E9C9CD9507FEFF28F9C916FFCDAA077BC9E5C50606FD
:2007A00056CDAA072310F9C1E1C9C50E000608AFCB021FCBAFD330000000000000CBEFD348
:2007C00030DB300FCB1310E7AFCBFFD330C1C9AFCBF7CBFFD330C9C506021803C506042115
:2007E0000000180429292929CD7608CD7F08CD4608FEFF2809E60FB56F10E9C1AFC93EFFC9
:20080000C1C93E0DCD7F08E521260ACD6C08E17CCD33087DCD33083E20CD7F08CD7F080642
:20082000107E23CD33083E20CD7F0810F43E0DCD7F08C9F50F0F0F0FCD6008CD7F08F1CD69
:200840006008CD7F08C9D6303813FE0A380ED611FE063806D620FE063003C60AC93EFFC97F
:20086000E60FFE0A3803C637C9C630C97EFE00C8CD7F082318F6DB39CB4F28FADB38C9F5D1
:20088000DB39CB4728FAF1D338C93E4ED3393E05D339C94D6F6E69746F72204B4C3543384C
:2008A000304231322043505520766572302E3732285350492B626F6F742920627920506937
:2008C0006E65636F6E6520323032302F31312F30320D000D3F202048656C700D6420206D5A
:2008E000656D6F72792064756D700D6D20206D656D6F7279206368616E67652020206164F8
:200900006472657373206F6C64206E65770D6C20206865782066696C65206C6F61640D6AF8
:2009200020206A756D700D692020692F6F20726567697374657220202020692F6F2D616400
:200940006472657373206F6C64206E65770D63202063616C6C0D74202072616D2061726508
:2009600061207472616E6365206D6F6E69746F72206A756D700D62202043502F4D207379AB
:200980007374656D20626F6F74206174205344434152440D6169207370692873642D6361D1
:2009A00072642920496E697469616C697A6174696F6E0D6172207370692873642D636172A2
:2009C00064292072656164202028353132627974652F626C6F636B2920203078303030300F
:2009E0002D307866666666207365740D6177207370692873642D63617264292077726974F8
:200A0000652028353132627974652F626C6F636B2920203078303030302D30786666666665
:200A2000207365740D006164647220202B30202B31202B32202B33202B34202B35202B3610
:200A4000202B37202B38202B39202B41202B42202B43202B44202B45202B460D00205965CB
:200A6000733A004F4B0D004552520D00207265616420626C6F636B204E6F3A0020777269BC
:200A8000746520626C6F636B204E6F3A00207361766520616464723A00207772697465200C
:200AA000616464723A0053706920496E6974204572720D00636D643137204572720D0063D6
:200AC0006D643234204572720D00636D643235204572720D003A534448430D003A5344203E
:200AE000566572320D00207370692873642D636172642920496E697469616C697A65203AA3
:200B00000052414D206172656120737769746368696E67206D6F6E69746F720D000D435077
:200B20002F4D20626F6F74203F207965733A0043502F4D20626F6F74202E2E2E2E0D004053
:200B4000000000009548000001AA876940FF8000FF7A00000000FF7700000000FF50000020
:1B0B60000200FF5100000200FF5200000000FF4C00000000FF5800000200FF32
:18FF00005100000200FF5200000000FF4C00000000FF5800000200FFA2
:00000001FF
コメントを残す

KL5C80B12C, monitor, SPI, Z80の関連記事
  • Z80(KL5C80B12C)に萌えたい。5-BIOS完成
  • Z80(KL5C80B12C)に萌えたい。4-64K(+BOOT)モニタ編
  • Z80(KL5C80B12C)に萌えたい。3-最初のモニタ編
  • Z80(KL5C80B12C)に萌えたい 。 2-ROM確認編
  • Z80(KL5C80B12C)に萌えたい 。 1-ハード準備編
おすすめの記事
Z80
前回のI/Oポートに SPI接続できるSDカードを接続したいと思います。 SDカード自体が3.3V動作なので、電圧変換が必要になります。 変...
Z80
最近のZ80はどうなってるんでしょう ちょっと くぐって見ましたwww eZ80が最新バージョンのようですね(eZ80Acclaim Plu...