; Simple communication with an IDE disk drive (ATA PIO mode)
;
; This research is an original work by Paul Stoffregen, written
; in December 1999 (modified in 2005).  This article has been
; placed in the public domain for use it without any restrictions.
;
; Modified and ported to AltairDos v3.x (Orion Z80 CP/M)
;          by Serge Akimov, October 2006
;
; This code is distributed in the hope that they will be useful,
; but without any warranty; without even the implied warranty of
; merchantability or fitness for a particular purpose.
;
;------------------------------------------------------------------
; Hardware Configuration
;
;8255 chip (Default ROM-DISK ppa - port 0F500h). 
;The first three control which 8255 ports have the control signals,
;upper and lower data bytes.  
;
; C,B-data (inout),  A-ctl (out)
;   (because if ORDOS exists, its treat ROM-DISK on the same port
;    and must not send dummy "random" signals into IDE)
;
;     +------------------------------------------+
;     |+----------------------------------------+|
;     ||+--------------------------------------+||
;     |||+------------------------------------+|||
;     ||||+----------------------------------+||||
;     |||||+--------------------------------+|||||
;     ||||||+------------------------------+||||||
;     |||||||+----------------------------+|||||||    
;     ||||||||  1+-+2   555ln1   IDE      ||||||||
;82C55|||||||| +-|1o--+        +-----+    ||||||||
;--+  |||||||| | +-+  +--------|1   2|--+ ||||||||
;C8|---------------------------|3   4|--|-+|||||||
;C7|---------------------------|5   6|--|--+||||||
;C6|---------------------------|7   8|--|---+|||||
;C5|---------------------------|9  10|--|----+||||
;C4|--||||||||-----------------|11 12|--|-----+|||
;C3|--||||||||-----------------|13 14|--|------+||
;C2|--||||||||-----------------|15 16|--|-------+|
;C1|--||||||||-----------------|17 18|--|--------+
;--+  |||||||| | 13+-+12    +--|19 20|x |
;B8|--+||||||| | +-|1o--+   | -|21 22|--+
;B7|---+|||||| | | +-+  +------|23 24|--+          
;B6|----+||||| | |        +----|25 26|--+--------+
;B5|-----+|||| | |11+-+10 | | -|27 28|- |   +    |
;B4|------+||| |+|--|1o---+ | -|29 30|--+-||-+   |
;B3|-------+|| |||  +-+     | -|31 32|- | C1 |   |
;B2|--------+| |||        +----|33 34|- |    |   |  
;B1|---------+ ||| 3+-+4  |+---|35 36|--|--+ |   |
;--+           |||+-|1o--------|37 38|--|-+| |   |
;A8|-----------+||| +-+   |||+-|39 40|--+ || |   |
;A7|------------+||       |||| +-----+    || |   |
;A6|-------------+| 5+-+6 ||||            || |   |
;A5|--------------|--|1o------------------+| |   |
;A4|--------------+  +-+  ||||             | |   |
;A3|----------------------||||-------------+ |   |
;A2|----------------------+|||               |   |
;A1|-----------------------+|+-RRRR----|<|---+   |
;--+- GND                   | 470 Om   LED   |   |                    
;A10|-----------------------+----------------|---+
;C10|----------------------------------------+
;---+ Vcc
;
;
;        I D E   P i n o u t
;        ===================
;pin no:    name,state:     function:
;---------  ----------      ---------
;
;1            /RESET    Al low signal level on this pin will reset
;                out    all connected devices
;
;2,19,22      GND       ground, interconnect them all and tie to
;24,26,30               controller's ground signal
;40
;
;3,5,7,9,11   D7..D0    low data bus, 3=D7 .. 17=D0. This part of
;13,15,17       inout   the bus is used for the command and
;                       parameter transfer. It is also used for
;                       the low byte in 16-bits data transfers.
;
;4,6,8,10     D8..D15   high data bus, 4=D8 .. 18=D15. This part
;12,14,16,18    inout   of the bus is used only for the 16-bits
;                       data transfer.
;
;20           -         This pin is usually missing. It is used to
;                       prevent mis-connecting the IDE cable.
;
;21 and       /IOREADY  I do not use or connect to this pin. It is
;27                     there to slow down a controller when it is
;                       going too fast for the bus. I do not have
;                       that problem...
;
;23           /WR out   Write strobe of the bus.
;
;25           /RD out   Read strobe of the bus.
;
;28           ALE       Some relic from the XT time. I do not use
;                       it, and I'm not the only one...
;
;31           IRQ  in   Interrupt output from the IDE devices. At
;                       this moment I do not use it. This pin
;                       could be connected to a controller to
;                       generate interrupts when a command is
;                       finished. I have an inverter ready for
;                       this signal (I need a /IRQ for my
;                       controller, an IRQ is of no use to me..)
;
;32           IO16      Used in an AT interface to enable the
;                       upper data bus drivers. I do not use this
;                       signal. It is redundant anyway, the ATA-3
;                       definition has scrapped it.
;
;34           /PDIAG    Master/slave interface on the IDE bus
;                       itself. Leave it alone or suffer
;                       master/slave communications problems. Not
;                       used (or connected to ANYTHING) by me.
;
;35           A0  out   Addresses of the IDE bus. With these
;33           A1  out   you can select which register of the IDE
;36           A2  out   devices you want to communicate.
;
;37           /CS0 out  The two /CS signals of the IDE bus. Used
;38           /CS1 out  in combination with the A0 .. A2 to select
;                       the register on the IDE device to
;                       communicate with.
;
;39           /ACT  in  A low level on this pin indicates that the
;                       IDE device is busy. I have connected a LED
;                       on this pin. The real busy signal for the
;                       controller I get from the IDE status
;                       register.
;
