Subversion Repositories pentevo

Rev

Rev 796 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #pragma once
  2.  
  3. const int Z80FQ = 3500000; // todo: #define as (conf.frame*conf.intfq)
  4. const int FDD_RPS = 5; // rotation speed
  5.  
  6. const int MAX_TRACK_LEN = 6250;
  7. const int MAX_CYLS = 86;            // don't load images with so many tracks
  8. const int MAX_PHYS_CYL = 86;        // don't seek over it
  9. const int MAX_SEC = 256;
  10.  
  11. struct SECHDR
  12. {
  13.    unsigned char c,s,n,l;
  14.    unsigned short crc; // CRC чруюыютър ёхъЄюЁр
  15.  
  16.    // Їыруш crc чюэ√ рфЁхёр ш фрээ√ї:
  17.    // ╧Ёш ЇюЁьрЄшЁютрэшш:
  18.    //   0 - ухэхЁшЁєхЄё  яЁртшы№эюх чэрўхэшх crc
  19.    //   1 - чряшё№ crc шч crc(фы  рфЁхёр)/crcd(фы  фрээ√ї)
  20.    //   2 - ю°шсюўэ√щ crc (ухэхЁшЁєхЄё  шэтхЁёшхщ яЁртшы№эюую crc))
  21.    // ╧Ёш ўЄхэшш (ЇєэъЎш  seek єёЄрэртыштрхЄ яюы  c1 ш c2):
  22.    //   0 - ЁрёёўшЄрээюх crc эх ёютярфрхЄ ё єърчрээ√ь т чруюыютъх (тючтЁр∙рхЄё  crc error)
  23.    //   1 - ЁрёёўшЄрээюх crc ёютярфрхЄ ё єърчрээ√ь т чруюыютъх
  24.    unsigned char c1, c2;
  25.    u8 *data; // ╙ърчрЄхы№ эр фрээ√х ёхъЄюЁр тэєЄЁш ЄЁ¤ър
  26.    u8 *id; // ╙ърчрЄхы№ эр чруюыютюъ ёхъЄюЁр тэєЄЁш ЄЁ¤ър
  27.    u8 *wp; // ╙ърчрЄхы№ эр сшЄютє■ ърЁЄє ёсющэ√ї срщЄют ёхъЄюЁр тэєЄЁш ЄЁ¤ър (шёяюы№чєхЄё  Єюы№ъю яЁш чруЁєчъх)
  28.    unsigned wp_start; // ═юьхЁ яхЁтюую сшЄр т ърЁЄх ёсющэ√ї срщЄют (юЄэюёшЄхы№эю эрўрыр ЄЁ¤ър) фы  фрээюую ёхъЄюЁр
  29.    unsigned datlen; // ╨рчьхЁ ёхъЄюЁр т срщЄрї
  30.    unsigned crcd;        // used to load specific CRC from FDI-file
  31. };
  32.  
  33. enum SEEK_MODE { JUST_SEEK = 0, LOAD_SECTORS = 1 };
  34.  
  35. static inline bool test_bit(const u8 *data, unsigned bit)
  36. {
  37.     return (data[bit >> 3] & (1U << (bit & 7))) != 0;
  38. }
  39.  
  40. static inline void set_bit(u8 *data, unsigned bit)
  41. {
  42.     data[bit >> 3] |= (1U << (bit & 7));
  43. }
  44.  
  45. static inline void clr_bit(u8 *data, unsigned bit)
  46. {
  47.     data[bit >> 3] &= ~(1U << (bit & 7));
  48. }
  49.  
  50.  
  51. struct TRKCACHE
  52. {
  53.    // cached track position
  54.    struct FDD *drive;
  55.    unsigned cyl, side;
  56.  
  57.    // generic track data
  58.    unsigned trklen;
  59.     // pointer to data inside UDI
  60.    u8 *trkd; // фрээ√х (ьюцхЄ с√Є№ NULL, хёыш ЄЁ¤ъ схч фрээ√ї)
  61.    u8 *trki; // сшЄютр  ърЁЄр ёшэїЁюшьяєы№ёют
  62.    u8 *trkwp; // сшЄютр  ърЁЄр ёсющэ√ї срщЄют
  63.    unsigned ts_byte;                 // cpu.t per byte
  64.    SEEK_MODE sf;                     // flag: is sectors filled
  65.    unsigned s;                       // no. of sectors
  66.  
  67.    // sectors on track
  68.    SECHDR hdr[MAX_SEC];
  69.  
  70.    void set_i(unsigned pos) { set_bit(trki, pos); }
  71.    void clr_i(unsigned pos) { clr_bit(trki, pos); }
  72.    bool test_i(unsigned pos) { return test_bit(trki, pos); }
  73.  
  74.    void set_wp(unsigned pos) { set_bit(trkwp, pos); }
  75.    bool test_wp(unsigned pos) { return test_bit(trkwp, pos); }
  76.  
  77.    void write(unsigned pos, unsigned char byte, u8 index)
  78.    {
  79.        if(!trkd)
  80.            return;
  81.  
  82.        trkd[pos] = byte;
  83.        if (index)
  84.            set_i(pos);
  85.        else
  86.            clr_i(pos);
  87.    }
  88.  
  89.    void seek(FDD *d, unsigned cyl, unsigned side, SEEK_MODE fs);
  90.    void format(); // before use, call seek(d,c,s,JUST_SEEK), set s and hdr[]
  91.    unsigned write_sector(unsigned sec, unsigned char *data); // call seek(d,c,s,LOAD_SECTORS)
  92.    const SECHDR *get_sector(unsigned sec) const; // before use, call fill(d,c,s,LOAD_SECTORS)
  93.  
  94.    void dump();
  95.    void clear()
  96.    {
  97.        drive = nullptr;
  98.        trkd = nullptr;
  99.        ts_byte = Z80FQ/(MAX_TRACK_LEN * FDD_RPS);
  100.    }
  101.    TRKCACHE() { clear(); }
  102. };
  103.  
  104.  
  105. struct FDD
  106. {
  107.    u8 Id;
  108.    // drive data
  109.  
  110.    __int64 motor;       // 0 - not spinning, >0 - time when it'll stop
  111.    unsigned char track; // head position
  112.  
  113.    // disk data
  114.  
  115.    unsigned char *rawdata;              // used in VirtualAlloc/VirtualFree
  116.    unsigned rawsize;
  117.  
  118.    // ═рўры№эюх ўшёыю фюЁюцхъ (яЁш чруЁєчъх юсЁрчр шыш яЁш ёючфрэшш яєёЄюую фшёър), ьюцхЄ с√Є№ єтхышўхэю фю MAX_CYLS
  119.    // яєЄхь ЇюЁрЄшЁютрэш  фюяюыэшЄхы№э√ї фюЁюцхъ єЄшышЄрьш Єшяр ADS ш яюфюсэ√ї
  120.    unsigned cyls;
  121.    unsigned sides;
  122.    unsigned trklen[MAX_CYLS][2];
  123.    u8 *trkd[MAX_CYLS][2]; // фрээ√х
  124.    u8 *trki[MAX_CYLS][2]; // сшЄют√х ърЁЄ√ ёшэїЁюшьяєы№ёют
  125.    u8 *trkwp[MAX_CYLS][2]; // сшЄют√х ърЁЄ√ ёсющэ√ї срщЄют
  126.    unsigned char optype; // bits: 0-not modified, 1-write sector, 2-format track
  127.    unsigned char snaptype;
  128.  
  129.    TRKCACHE t; // used in read/write image
  130.    char name[0x200];
  131.    char dsc[0x200];
  132.  
  133.    char test();
  134.    void free();
  135.    int index();
  136.  
  137.    void format_trd(unsigned CylCnt); // ╚ёяюы№чєхЄё  Єюы№ъю фы  wldr_trd
  138.    void emptydisk(unsigned FreeSecCnt); // ╚ёяюы№чєхЄё  Єюы№ъю фы  wldr_trd
  139.    int addfile(unsigned char *hdr, unsigned char *data); // ╚ёяюы№чєхЄё  Єюы№ъю фы  wldr_trd
  140.    void addboot(); // ╚ёяюы№чєхЄё  Єюы№ъю фы  wldr_trd
  141.  
  142.    void newdisk(unsigned cyls, unsigned sides);
  143.  
  144.    int read(unsigned char snType);
  145.  
  146.    int read_scl();
  147.    int read_hob();
  148.    int read_trd();
  149.    int write_trd(FILE *ff);
  150.    int read_fdi();
  151.    int write_fdi(FILE *ff);
  152.    int read_td0();
  153.    int write_td0(FILE *ff);
  154.    int read_udi();
  155.    int write_udi(FILE *ff);
  156.  
  157.    void format_isd();
  158.    int read_isd();
  159.    int write_isd(FILE *ff);
  160.  
  161.    void format_pro();
  162.    int read_pro();
  163.    int write_pro(FILE *ff);
  164.  
  165.    ~FDD() { free(); }
  166. };
  167.  
  168.  
  169. struct WD1793
  170. {
  171.    enum WDSTATE
  172.    {
  173.       S_IDLE = 0,
  174.       S_WAIT,
  175.  
  176.       S_DELAY_BEFORE_CMD,
  177.       S_CMD_RW,
  178.       S_FOUND_NEXT_ID,
  179.       S_RDSEC,
  180.       S_READ,
  181.       S_WRSEC,
  182.       S_WRITE,
  183.       S_WRTRACK,
  184.       S_WR_TRACK_DATA,
  185.  
  186.       S_TYPE1_CMD,
  187.       S_STEP,
  188.       S_SEEKSTART,
  189.       S_RESTORE,
  190.       S_SEEK,
  191.       S_VERIFY,
  192.       S_VERIFY2,
  193.  
  194.       S_WAIT_HLT,
  195.       S_WAIT_HLT_RW,
  196.  
  197.       S_EJECT1,
  198.       S_EJECT2
  199.    };
  200.  
  201.    __int64 next, time;
  202.    __int64 idx_tmo;
  203.  
  204.    FDD *seldrive;
  205.    unsigned tshift;
  206.  
  207.    WDSTATE state, state2;
  208.  
  209.    unsigned char cmd;
  210.    unsigned char data, track, sector;
  211.    unsigned char rqs, status;
  212.    u8 sign_status; // ┬эх°эшх ёшуэры√ (яюър Єюы№ъю HLD)
  213.  
  214.    unsigned drive, side;                // update this with changing 'system'
  215.  
  216.    signed char stepdirection;
  217.    unsigned char system;                // beta128 system register
  218.  
  219.    unsigned idx_cnt; // idx counter
  220.  
  221.    // read/write sector(s) data
  222.    __int64 end_waiting_am;
  223.    unsigned foundid;                    // index in trkcache.hdr for next encountered ID and bytes before this ID
  224.    unsigned rwptr, rwlen;
  225.  
  226.    // format track data
  227.    unsigned start_crc;
  228.  
  229.    enum CMDBITS
  230.    {
  231.       CMD_SEEK_RATE     = 0x03,
  232.       CMD_SEEK_VERIFY   = 0x04,
  233.       CMD_SEEK_HEADLOAD = 0x08,
  234.       CMD_SEEK_TRKUPD   = 0x10,
  235.       CMD_SEEK_DIR      = 0x20,
  236.  
  237.       CMD_WRITE_DEL     = 0x01,
  238.       CMD_SIDE_CMP_FLAG = 0x02,
  239.       CMD_DELAY         = 0x04,
  240.       CMD_SIDE          = 0x08,
  241.       CMD_SIDE_SHIFT    = 3,
  242.       CMD_MULTIPLE      = 0x10
  243.    };
  244.  
  245.    enum BETA_STATUS
  246.    {
  247.       DRQ   = 0x40,
  248.       INTRQ = 0x80
  249.    };
  250.  
  251.    enum WD_STATUS
  252.    {
  253.       WDS_BUSY      = 0x01,
  254.       WDS_INDEX     = 0x02,
  255.       WDS_DRQ       = 0x02,
  256.       WDS_TRK00     = 0x04,
  257.       WDS_LOST      = 0x04,
  258.       WDS_CRCERR    = 0x08,
  259.       WDS_NOTFOUND  = 0x10,
  260.       WDS_SEEKERR   = 0x10,
  261.       WDS_RECORDT   = 0x20,
  262.       WDS_HEADL     = 0x20,
  263.       WDS_WRFAULT   = 0x20,
  264.       WDS_WRITEP    = 0x40,
  265.       WDS_NOTRDY    = 0x80
  266.    };
  267.  
  268.    enum WD_SYS
  269.    {
  270.       SYS_HLT       = 0x08
  271.    };
  272.  
  273.    enum WD_SIG
  274.    {
  275.        SIG_HLD      = 0x01
  276.    };
  277.  
  278.    unsigned char in(unsigned char port);
  279.    void out(unsigned char port, unsigned char val);
  280.    u8 RdStatus();
  281.  
  282.    void process();
  283.    void find_marker();
  284.    char notready();
  285.    void load();
  286.    void getindex();
  287.    void trdos_traps();
  288.  
  289. //   TRKCACHE trkcache;
  290.    FDD fdd[4];
  291.  
  292.    bool EjectPending;
  293.    void Eject(unsigned Drive);
  294.  
  295.    WD1793()
  296.    {
  297.        for(unsigned i = 0; i < 4; i++) // [vv] ─ы  єфюсёЄтр юЄырфъш
  298.            fdd[i].Id = u8(i);
  299.        seldrive = &fdd[0];
  300.        idx_cnt = 0;
  301.        idx_tmo = LLONG_MAX;
  302.        sign_status = 0;
  303.        tshift = 0;
  304.        EjectPending = false;
  305.    }
  306. };
  307.