Compare commits

...

2 Commits

Author SHA1 Message Date
Virgil Dupras
5219887ad7 sdc: implement _crc16 in z80 2020-04-24 20:46:01 -04:00
Virgil Dupras
8d78ca9dac tests: add test_sdc
The SD card driver is a bit too slow to be bearable. I'll write
_crc16 in z80 and see how it goes.
2020-04-24 18:23:29 -04:00
7 changed files with 56 additions and 57 deletions

View File

@ -10,4 +10,5 @@
0xe6 OP2n ANDn, 0xe6 OP2n ANDn,
0xf6 OP2n ORn, 0xf6 OP2n ORn,
0xd6 OP2n SUBn, 0xd6 OP2n SUBn,
0xee OP2n XORn,

23
blk/373
View File

@ -1,9 +1,16 @@
CODE _sdcSel CODE _sdcSel SDC_CSLOW OUTnA, ;CODE
SDC_CSLOW OUTnA, CODE _sdcDesel SDC_CSHIGH OUTnA, ;CODE
( Computes n into crc c with polynomial 0x1021 )
CODE _crc16 ( c n -- c )
HL POPqq, ( n ) DE POPqq, ( c )
A L LDrr, D XORr, D A LDrr,
B 8 LDrn,
BEGIN,
E SLAr, D RLr,
IFC, ( msb is set, apply polynomial )
A D LDrr, 0x10 XORn, D A LDrr,
A E LDrr, 0x21 XORn, E A LDrr,
THEN,
DJNZ, AGAIN,
DE PUSHqq,
;CODE ;CODE
CODE _sdcDesel
SDC_CSHIGH OUTnA,
;CODE

12
blk/377
View File

@ -1,15 +1,3 @@
( c n -- c )
( Computes n into crc c with polynomial 0x1021 )
: _crc16
SWAP DUP 256 / ( n c c>>8 )
ROT XOR ( c x )
DUP 16 / XOR ( c x^x>>4 )
SWAP 256 * ( x c<<8 )
OVER 4096 * XOR ( x c^x<<12 )
OVER 32 * XOR ( x c^x<<5 )
XOR ( c )
;
( send-and-crc7 ) ( send-and-crc7 )
( n c -- c ) ( n c -- c )
: _s+crc SWAP DUP _sdcSR DROP _crc7 ; : _s+crc SWAP DUP _sdcSR DROP _crc7 ;

28
blk/380
View File

@ -1,16 +1,16 @@
: _err _sdcDesel ABORT" SDerr" ; : _err _sdcDesel ABORT" SDerr" ;
( Initialize a SD card. This should be called at least 1ms ( Tight definition ahead, pre-comment.
after the powering up of the card. )
: SDC$ Initialize a SD card. This should be called at least 1ms
( Wake the SD card up. After power up, a SD card has to receive after the powering up of the card. We begin by waking up the
at least 74 dummy clocks with CS and DI high. We send 80. ) SD card. After power up, a SD card has to receive at least
10 0 DO _idle DROP LOOP 74 dummy clocks with CS and DI high. We send 80.
( call cmd0 and expect a 0x01 response (card idle) Then send cmd0 for a maximum of 10 times, success is when
this should be called multiple times. we're actually we get 0x01. Then comes the CMD8. We send it with a 0x01aa
expected to. let's call this for a maximum of 10 times. ) argument and expect a 0x01aa argument back, along with a
0 ( dummy ) 0x01 R1 response. After that, we need to repeatedly run
10 0 DO ( r ) CMD55+CMD41 (0x40000000) until the card goes out of idle
DROP 0x40 0 0 SDCMDR1 ( CMD0 ) mode, that is, when it stops sending us 0x01 response and
DUP 0x01 = IF LEAVE THEN send us 0x00 instead. Any other response means that
LOOP 0x01 = NOT IF _err THEN ( cont. ) initialization failed. )

26
blk/381
View File

@ -1,10 +1,16 @@
( Then comes the CMD8. We send it with a 0x01aa argument and : SDC$
expect a 0x01aa argument back, along with a 0x01 R1 10 0 DO _idle DROP LOOP
response. ) 0 ( dummy ) 10 0 DO ( r )
0x48 0 0x1aa ( CMD8 ) DROP 0x40 0 0 SDCMDR1 ( CMD0 )
SDCMDR7 ( r arg1 arg2 ) DUP 0x01 = IF LEAVE THEN
0x1aa = NOT IF _err THEN ( arg2 check ) LOOP 0x01 = NOT IF _err THEN
0 = NOT IF _err THEN ( arg1 check ) 0x48 0 0x1aa ( CMD8 ) SDCMDR7 ( r arg1 arg2 )
0x01 = NOT IF _err THEN ( r check ) 0x1aa = NOT IF _err THEN ( arg2 check )
0 = NOT IF _err THEN ( arg1 check )
( cont. ) 0x01 = NOT IF _err THEN ( r check )
BEGIN
0x77 0 0 SDCMDR1 ( CMD55 )
0x01 = NOT IF _err THEN
0x69 0x4000 0x0000 SDCMDR1 ( CMD41 )
DUP 0x01 > IF _err THEN
NOT UNTIL ; ( out of idle mode, success! )

13
blk/382
View File

@ -1,13 +0,0 @@
( Now we need to repeatedly run CMD55+CMD41 (0x40000000)
until the card goes out of idle mode, that is, when it stops
sending us 0x01 response and send us 0x00 instead. Any other
response means that initialization failed. )
BEGIN
0x77 0 0 SDCMDR1 ( CMD55 )
0x01 = NOT IF _err THEN
0x69 0x4000 0x0000 SDCMDR1 ( CMD41 )
DUP 0x01 > IF _err THEN
NOT UNTIL
( Out of idle mode! Success! )
;

10
tests/forth/test_sdc.fs Normal file
View File

@ -0,0 +1,10 @@
212 LOAD ( z80a )
: SDC_SPI 4 ;
: SDC_CSLOW 5 ;
: SDC_CSHIGH 6 ;
372 LOAD ( sdc.z80 )
374 LOAD ( sdc.fs )
0x0000 0x00 _crc16 0x0000 #eq
0x0000 0x01 _crc16 0x1021 #eq
0x5678 0x34 _crc16 0x34e4 #eq