これまでの プログラムをまとめてみました。
コメントが少ないですが、過去のブログを参考にしてくださいねwww

プログラムの中で、1部モニタのサブルーチンをコール(RS232Cでの表示関係)していますので、指定のモニタ(v0.72)以外ではコール先アドレスを変更する必要があります。

モニタ上でLコマンドにて、アッセンブルしたHEXファイルをロードして、jコマンドで0x1000番地から実行した結果を合わせて載せています。

プログラム(ちょっと長いです^^;)

;;
;; i/o port on spi >> sd-card
;;
;; Z8S180 cpu
;; 0000h -- ffffh
;; External clock 16MHz
;; run on monitor v0.72
;;
;;
;;  i/o address
;;      INPUT-PORT    0x84(r)
;;      OUTPUT-PORT   0x84(w)
;;
;; assembler
;;  program 0x1000
;;  data    0x3000
;;
;; assemblers  ASxxxx and ASlink V5.20
;; file name spi.asm
;; $ asz80 -l -s -o spi.asm
;; $ aslink -i spi
;;
;; load hex to monitor v0.72   l command
;; j command  0x1000 program go
;;

;; z180 MPU (zilog Z8S18033VSG)

.z180

;; dely timing set in 100uS
D100U  .equ    6      ;clok 16Mhz*2 monitor v0.72

;; monitor subroutine call
hex_a_disp      .equ    0x0406  ;monitor v0.72
putchar         .equ    0x0453  ;monitor v0.72
msgout          .equ    0x043f  ;monitor v0.72

;;spi i/o port setting
INPUTP  .equ    0x84            ;;74HC541
OUTPUTP .equ    0x84            ;;74HC574

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

;INPUT-PORT
DO      .equ    7               ;0b10000000  D7

;; start program

.area   TEST (ABS)

.org    0x1000

start_spi:
push    af
ld      b,4             ;4ms dely
call    delym
xor     a
set     S_CS,a          ;CS="H"
set     S_DI,a          ;DI="H"
out0    (OUTPUTP),a
ld      b,80            ;80 clok output
call    loopspii
pop     af
xor     a
out     (OUTPUTP),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

call    cmd17_out
call    cmd24_out
call    cmd17_out

jp      0x0100          ;;monitor jump

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
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

call    cmd58_out

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
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
cmd16_out:
call    dummy_data
ld      hl,cmd16
call    cmd_out
cmd16_loop:
call    r1_resp
cp      0x00
jr      nz,cmd16_loop

cmd17_out:
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?
jp      nz,init_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
ld      hl,sd_buff              ;read data buff address
cmd17_r:
call    resp                    ;read data
ld      (hl),a
inc     hl
djnz    cmd17_r

ld      b,0x00                  ;read data size 256byte
ld      hl,sd_buff+0x100        ;read data buff address 257-512byte
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

ld      hl,sd_buff              ;memory dump address

dump_hl512b:                            ;memory dump 512b
ld      a,0x0d
call    putchar

call    hl_disp256              ;256bye dump
call    putchar
call    hl_disp256              ;256bye dump
call    putchar
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

dump_msg:
.str    "addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F"
.db     0x0d
.db     0x00

cmd24_out:
call    dummy_data
ld      hl,cmd24
call    cmd_out
call    r1_resp
cp      0x00
jr      nz,init_err
cmd24_st:
ld      d,0xfe
call    spi_8bit

ld      b,0x00
cmd24_w:
ld      a,b
dec     a
ld      d,a
call    spi_8bit
djnz    cmd24_w

ld      b,0x00
cmd24_w2:
ld      a,b
dec     a
ld      d,a
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,init_err
cmd24_wch:
call    r1_resp
cp      0x00
jr      z,cmd24_wch
call    dummy_data

ret

init_err:
halt

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
cmd58_r_d:
ld      hl,cmd58_msg
call    msgout
pop     af
push    af
call    hex_a_disp
ld      hl,r7
ld      b,4
cmd58_r_d_loop:
ld      a,(hl)
call    hex_a_disp
inc     hl
djnz    cmd58_r_d_loop
ld      a,0x0d
call    putchar
pop     af
ret
cmd58_msg:
.str    "CMD58:"
.db     0x00

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

loopspii:
res     S_CLK,a         ;CLK    L
out0    (OUTPUTP),a
nop
nop
nop
nop
nop
nop
nop
nop

set     S_CLK,a         ;CLK    H
out0    (OUTPUTP),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
out0    (OUTPUTP),a
nop
nop
nop
nop
nop
nop
set     S_CLK,a         ;CLK    H
out0    (OUTPUTP),a
in0     a,(INPUTP)
rlca
rl      e
djnz    loopspi
xor     a
out0    (OUTPUTP),a
pop     bc
ret

delym:
push    bc
delyml: call    dely1m
djnz    delyml
pop     bc
ret

dely1m:                                 ;; 1mS dely
push    bc
call    dely500u
call    dely500u
pop     bc
ret

dely500u:                               ;; 500uS dely
push    bc
ld      b,5
dd5:    call dely100u
djnz    dd5
pop     bc
ret

dely100u:                               ;; 100uS dely
push    bc
ld      b,D100U
l100u:  djnz    l100u
pop     bc
ret

;       .org    0x2000  ;; data

cmd0:                           ;;48bit(6byte) Fixed length
.db     0x40            ;;command index
.db     0x00            ;;argument
.db     0x00            ;;argument
.db     0x00            ;;argument
.db     0x00            ;;argument
.db     0x95            ;;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
cmd17:
.db     0x51
.db     0x00
.db     0x00
.db     0x10
.db     0x00
.db     0xff

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

r7:
.ds     4

.org 0x3000
sd_buff:
.ds     512

.end

モニタ上での実行結果です。

>j 1000 Yes:
CMD0:01
CMD8:01000001AA
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:01
CMD55:01
ACMD41:00
CMD58:0080FF8000

addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
3000  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3010  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3020  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3030  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3040  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3050  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3060  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3070  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3080  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3090  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
30A0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
30B0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
30C0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
30D0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
30E0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
30F0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
3100  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3110  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3120  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3130  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3140  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3150  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3160  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3170  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3180  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
3190  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
31A0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
31B0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
31C0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
31D0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
31E0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
31F0  FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF

addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
3000  FF FE FD FC FB FA F9 F8 F7 F6 F5 F4 F3 F2 F1 F0
3010  EF EE ED EC EB EA E9 E8 E7 E6 E5 E4 E3 E2 E1 E0
3020  DF DE DD DC DB DA D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
3030  CF CE CD CC CB CA C9 C8 C7 C6 C5 C4 C3 C2 C1 C0
3040  BF BE BD BC BB BA B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
3050  AF AE AD AC AB AA A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
3060  9F 9E 9D 9C 9B 9A 99 98 97 96 95 94 93 92 91 90
3070  8F 8E 8D 8C 8B 8A 89 88 87 86 85 84 83 82 81 80
3080  7F 7E 7D 7C 7B 7A 79 78 77 76 75 74 73 72 71 70
3090  6F 6E 6D 6C 6B 6A 69 68 67 66 65 64 63 62 61 60
30A0  5F 5E 5D 5C 5B 5A 59 58 57 56 55 54 53 52 51 50
30B0  4F 4E 4D 4C 4B 4A 49 48 47 46 45 44 43 42 41 40
30C0  3F 3E 3D 3C 3B 3A 39 38 37 36 35 34 33 32 31 30
30D0  2F 2E 2D 2C 2B 2A 29 28 27 26 25 24 23 22 21 20
30E0  1F 1E 1D 1C 1B 1A 19 18 17 16 15 14 13 12 11 10
30F0  0F 0E 0D 0C 0B 0A 09 08 07 06 05 04 03 02 01 00

addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F
3100  FF FE FD FC FB FA F9 F8 F7 F6 F5 F4 F3 F2 F1 F0
3110  EF EE ED EC EB EA E9 E8 E7 E6 E5 E4 E3 E2 E1 E0
3120  DF DE DD DC DB DA D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
3130  CF CE CD CC CB CA C9 C8 C7 C6 C5 C4 C3 C2 C1 C0
3140  BF BE BD BC BB BA B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
3150  AF AE AD AC AB AA A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
3160  9F 9E 9D 9C 9B 9A 99 98 97 96 95 94 93 92 91 90
3170  8F 8E 8D 8C 8B 8A 89 88 87 86 85 84 83 82 81 80
3180  7F 7E 7D 7C 7B 7A 79 78 77 76 75 74 73 72 71 70
3190  6F 6E 6D 6C 6B 6A 69 68 67 66 65 64 63 62 61 60
31A0  5F 5E 5D 5C 5B 5A 59 58 57 56 55 54 53 52 51 50
31B0  4F 4E 4D 4C 4B 4A 49 48 47 46 45 44 43 42 41 40
31C0  3F 3E 3D 3C 3B 3A 39 38 37 36 35 34 33 32 31 30
31D0  2F 2E 2D 2C 2B 2A 29 28 27 26 25 24 23 22 21 20
31E0  1F 1E 1D 1C 1B 1A 19 18 17 16 15 14 13 12 11 10
31F0  0F 0E 0D 0C 0B 0A 09 08 07 06 05 04 03 02 01 00

Monitor Z180 CPU ver0.72  by Pinecone 2017/3/20

20CBR:20 CBAR:00
clock:1/1 wait memory:0 wait i/o:1
monitor area:0100-05D5
おすすめの記事