OpenCores
URL https://opencores.org/ocsvn/aoocs/aoocs/trunk

Subversion Repositories aoocs

[/] [aoocs/] [trunk/] [tests/] [ocs_floppy.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
#include <cstdio>
2
#include <cstdlib>
3
#include <cstring>
4
#include <errno.h>
5
#include <poll.h>
6
#include <unistd.h>
7
 
8
//****************************************************************************** E-UAE floppy read
9
typedef unsigned int uae_u32;
10
typedef unsigned char uae_u8;
11
typedef unsigned short uae_u16;
12
 
13
unsigned char mfm_uae[544*2*11];
14
int mfm_uae_index = 0;
15
static void mfmcode (uae_u16 *mfm, unsigned int words) {
16
    uae_u32 lastword = 0;
17
    while (words--) {
18
    uae_u32 v = *mfm;
19
    uae_u32 lv = (lastword << 16) | v;
20
    uae_u32 nlv = 0x55555555 & ~lv;
21
    uae_u32 mfmbits = (nlv << 1) & (nlv >> 1);
22
    *mfm++ = v | mfmbits;
23
    lastword = v;
24
    }
25
}
26
 
27
static void decode_amigados () {
28
    /* Normal AmigaDOS format track */
29
    unsigned int tr = 0;
30
    unsigned int sec;
31
    int len = 11 * 544 + 0;
32
 
33
    for (sec = 0; sec < 11; sec++) {
34
        uae_u8 secbuf[544];
35
        uae_u16 mfmbuf[544];
36
        int i;
37
        uae_u32 deven, dodd;
38
        uae_u32 hck = 0, dck = 0;
39
 
40
        secbuf[0] = secbuf[1] = 0x00;
41
        secbuf[2] = secbuf[3] = 0xa1;
42
        secbuf[4] = 0xff;
43
        secbuf[5] = tr;
44
        secbuf[6] = sec;
45
        secbuf[7] = 11 - sec;
46
 
47
        for (i = 8; i < 24; i++)
48
        secbuf[i] = 0;
49
 
50
//memset(secbuf+32, 0x00, 512);
51
srand(1);
52
for(int i=0; i<512; i++) secbuf[32+i] = rand();
53
 
54
        mfmbuf[0] = mfmbuf[1] = 0xaaaa;
55
        mfmbuf[2] = mfmbuf[3] = 0x4489;
56
 
57
        deven = ((secbuf[4] << 24) | (secbuf[5] << 16)
58
             | (secbuf[6] << 8) | (secbuf[7]));
59
        dodd = deven >> 1;
60
        deven &= 0x55555555;
61
        dodd &= 0x55555555;
62
 
63
        mfmbuf[4] = dodd >> 16;
64
        mfmbuf[5] = dodd;
65
        mfmbuf[6] = deven >> 16;
66
        mfmbuf[7] = deven;
67
 
68
        for (i = 8; i < 48; i++)
69
        mfmbuf[i] = 0xaaaa;
70
    for (i = 0; i < 512; i += 4) {
71
        deven = ((secbuf[i + 32] << 24) | (secbuf[i + 33] << 16)
72
             | (secbuf[i + 34] << 8) | (secbuf[i + 35]));
73
        dodd = deven >> 1;
74
        deven &= 0x55555555;
75
        dodd &= 0x55555555;
76
        mfmbuf[(i >> 1) + 32] = dodd >> 16;
77
        mfmbuf[(i >> 1) + 33] = dodd;
78
        mfmbuf[(i >> 1) + 256 + 32] = deven >> 16;
79
        mfmbuf[(i >> 1) + 256 + 33] = deven;
80
    }
81
 
82
    for (i = 4; i < 24; i += 2)
83
        hck ^= (mfmbuf[i] << 16) | mfmbuf[i + 1];
84
 
85
        deven = dodd = hck;
86
        dodd >>= 1;
87
        mfmbuf[24] = dodd >> 16;
88
        mfmbuf[25] = dodd;
89
        mfmbuf[26] = deven >> 16;
90
        mfmbuf[27] = deven;
91
 
92
        for (i = 32; i < 544; i += 2)
93
        dck ^= (mfmbuf[i] << 16) | mfmbuf[i + 1];
94
 
95
        deven = dodd = dck;
96
        dodd >>= 1;
97
        mfmbuf[28] = dodd >> 16;
98
        mfmbuf[29] = dodd;
99
        mfmbuf[30] = deven >> 16;
100
        mfmbuf[31] = deven;
101
 
102
        /*
103
        for(int ii=0; ii<544; ii++) {
104
            if((ii%16) == 0) printf("\n");
105
            printf("%04x, ", mfmbuf[ii]);
106
        }
107
        printf("\n");
108
        printf("mfmcode\n");
109
        */
110
        mfmcode (mfmbuf + 4, 544 - 4);
111
        /*
112
        for(int ii=0; ii<544; ii++) {
113
            if((ii%16) == 0) printf("\n");
114
            printf("%04x, ", mfmbuf[ii]);
115
        }
116
        printf("\n");
117
        exit(-1);
118
        */
119
 
120
        for (i = 0; i < 544; i++) {
121
            mfm_uae[mfm_uae_index+2*i+0] = mfmbuf[i] >> 8;
122
            mfm_uae[mfm_uae_index+2*i+1] = mfmbuf[i];
123
        }
124
        mfm_uae_index += 544*2;
125
    }
126
}
127
 
128
 
129
//****************************************************************************** E-UAE floppy read
130
 
131
int ans_to_tb[2];
132
int ans_from_tb[2];
133
int pid;
134
static void ans_prepare_pipe() {
135
    /* Pipe. */
136
    if(pipe(ans_to_tb) == -1) {
137
        printf("Error: to pipe() failed.\n");
138
        exit(-1);
139
    }
140
    if(pipe(ans_from_tb) == -1) {
141
        printf("Error: from pipe() failed.\n");
142
        exit(-1);
143
    }
144
 
145
    /* Fork */
146
    if((pid = fork()) == -1) {
147
        printf("Error: fork() failed.\n");
148
        exit(-1);
149
    }
150
 
151
    /* Child */
152
    if(pid == 0) {
153
        close(0);
154
        dup2(ans_to_tb[0], 0);
155
        close(ans_to_tb[0]);
156
 
157
        close(1);
158
        dup2(ans_from_tb[1], 1);
159
        close(ans_from_tb[1]);
160
 
161
        /* Execute */
162
        int result;
163
        result = execl("/home/alek/aktualne/aoOCS/aoOCS/tb_ocs_floppy", "/home/alek/aktualne/aoOCS/aoOCS/tb_ocs_floppy", (char *)0);
164
        printf("EXECL failed: %d, %s\n", result, strerror(errno));
165
        exit(-1);
166
    }
167
}
168
enum port_t {
169
    REGISTER,
170
    FL_MTR_N,
171
    FL_SEL_N,
172
    FL_SIDE_N,
173
    FL_DIR,
174
    FL_STEP_N,
175
    STEP
176
};
177
static void ans_write_register(enum port_t port, unsigned int adr, unsigned short val) {
178
    if(pid == 0) {
179
        ans_prepare_pipe();
180
    }
181
    char line[256];
182
    if(port == REGISTER) {
183
        if( (adr % 4) == 1 || (adr % 4) == 3 ) {
184
            printf("Error: unaligned write register: %08h\n", adr);
185
            exit(-1);
186
        }
187
        int sel = 0, value = 0;
188
        if( (adr % 4) == 0 ) { sel = 0xc; value = val << 16; }
189
        if( (adr % 4) == 2 ) { sel = 0x3; value = val; }
190
 
191
        sprintf(line, "write register: adr=%x, sel=%x, val=%x\n", adr, sel, value);
192
    }
193
    else if(port == FL_MTR_N)   sprintf(line, "fl_mtr_n=%x\n", val);
194
    else if(port == FL_SEL_N)   sprintf(line, "fl_sel_n=%x\n", val);
195
    else if(port == FL_SIDE_N)  sprintf(line, "fl_side_n=%x\n", val);
196
    else if(port == FL_DIR)     sprintf(line, "fl_dir=%x\n", val);
197
    else if(port == FL_STEP_N)  sprintf(line, "fl_step_n=%x\n", val);
198
    else if(port == STEP)       sprintf(line, "step=%x\n", val);
199
 
200
    printf("WRITING: %s\n", line);
201
    if( write(ans_to_tb[1], line, strlen(line) ) != (int)strlen(line) ) {
202
        printf("Error writing to tb.\n");
203
        exit(-1);
204
    }
205
}
206
 
207
unsigned char mfm[544*2*11];
208
unsigned int lget(unsigned int adr) {
209
    printf("lget: %08x\n", adr);
210
 
211
    if(adr == 0x10001000) return 2; // sd status read
212
 
213
    if(adr >= 0x1008D800 && adr <= 0x1008D800 + 11*512 - 4) {
214
        unsigned char buf[512];
215
        srand(1);
216
        for(int i=0; i<512; i++) buf[i] = rand();
217
 
218
        adr -= 0x1008D800;
219
        adr %= 512;
220
        return buf[adr+3] | (buf[adr+2] << 8) | (buf[adr+1] << 16) | (buf[adr+0] << 24);
221
    }
222
 
223
    if(adr >= 0x1008EE00 && adr <= 0x1008EE00 + sizeof(mfm) - 4) {
224
        adr -= 0x1008EE00;
225
        unsigned int val = 0;
226
        val |= mfm[adr+0]<<24;
227
        val |= mfm[adr+1]<<16;
228
        val |= mfm[adr+2]<<8;
229
        val |= mfm[adr+3]<<0;
230
        return val;
231
    }
232
 
233
    getchar();
234
    exit(-1);
235
    return 0;
236
}
237
 
238
unsigned char mfm_dma[12700];
239
void wput(unsigned int adr, unsigned short val) {
240
    printf("wput: %08x <- %04hx\n", adr, val);
241
 
242
    if(adr >= 0x800 && adr <= 0x800 + sizeof(mfm_dma) -2) {
243
        adr -= 0x800;
244
        mfm_dma[adr+0] = val>>8;
245
        mfm_dma[adr+1] = val;
246
        return;
247
    }
248
    getchar();
249
    exit(-1);
250
}
251
 
252
void lput(unsigned int adr, unsigned int val) {
253
    printf("lput: %08x <- %08x\n", adr, val);
254
 
255
    if(adr == 0x10001000 || adr == 0x10001004 || adr == 0x10001008 || adr == 0x1000100C) return;
256
 
257
    if(adr >= 0x1008EE00 && adr <= 0x1008EE00 + sizeof(mfm) - 4) {
258
        adr -= 0x1008EE00;
259
        mfm[adr+0] = val>>24;
260
        mfm[adr+1] = val>>16;
261
        mfm[adr+2] = val>>8;
262
        mfm[adr+3] = val;
263
        return;
264
    }
265
    getchar();
266
    exit(-1);
267
}
268
void print_mfm() {
269
    for(int i=0; i<sizeof(mfm); i++) {
270
        if((i%32) == 0) printf("\n");
271
        printf("%02x, ", mfm[i]);
272
    }
273
    printf("\n");
274
    printf("MFM UAE\n");
275
    decode_amigados();
276
    for(int i=0; i<sizeof(mfm_uae); i++) {
277
        if((i%32) == 0) printf("\n");
278
        printf("%02x, ", mfm_uae[i]);
279
    }
280
    printf("\n");
281
}
282
 
283
bool poll_tb() {
284
    struct pollfd p;
285
    p.fd = ans_from_tb[0];
286
    p.events = POLLIN;
287
    int result = poll(&p, 1, 100);
288
    if(result == -1) {
289
        printf("poll error: %d\n", result);
290
        exit(-1);
291
    }
292
    if(result == 0) return false;
293
    return true;
294
}
295
 
296
int main(int argc, char **argv) {
297
    memset(mfm, 0xFF, sizeof(mfm));
298
    memset(mfm_dma, 0xFF, sizeof(mfm_dma));
299
 
300
    if(pid == 0) {
301
        ans_prepare_pipe();
302
    }
303
 
304
    char line[256];
305
 
306
    int result;
307
    unsigned int adr;
308
    unsigned int sel;
309
    unsigned int val;
310
 
311
    ans_write_register(FL_MTR_N, 0, 0);
312
    ans_write_register(FL_SIDE_N, 0, 1);
313
    ans_write_register(FL_SEL_N, 0, 0xE);
314
 
315
    ans_write_register(REGISTER, 0x022, 0x800);
316
    ans_write_register(REGISTER, 0x024, 0x8000 | (12600/2));
317
    ans_write_register(REGISTER, 0x024, 0x8000 | (12600/2));
318
    ans_write_register(REGISTER, 0x07E, 0x4489);
319
 
320
    int count=0, step_count=0;
321
    while(1) {
322
        count++;
323
 
324
        if(count == 120000) {
325
            print_mfm();
326
 
327
            printf("DMA\n");
328
            printf("aa, aa, aa, aa, 44, 89, ");
329
            for(int i=6; i<sizeof(mfm_dma)+6; i++) {
330
                if((i%32) == 0) printf("\n");
331
                printf("%02hhx, ", mfm_dma[i-6]);
332
            }
333
            exit(0);
334
        }
335
 
336
        memset(line, 0, sizeof(line));
337
        int i=0;
338
        while(1) {
339
            if(poll_tb() == false) {
340
                ans_write_register(STEP, 0, 1);
341
                step_count = 0;
342
                continue;
343
            }
344
 
345
            result = read(ans_from_tb[0], line+i, 1);
346
            if(result == -1) {
347
                printf("Error: read == -1\n");
348
                exit(-1);
349
            }
350
            if(line[i] == '\n') {
351
                line[i] = '\0';
352
                break;
353
            }
354
            i++;
355
        }
356
printf("GOT: %s\n", line);
357
 
358
        if(strcmp(line, "done") == 0) {
359
            printf("READ: done\n");
360
            print_mfm();
361
            exit(0);
362
        }
363
        else if(sscanf(line, "read memory: adr=%x", &adr) == 1) {
364
            //printf("READ: read memory: adr=%x\n", adr);
365
 
366
            sprintf(line, "memory: adr=%x, val=%x\n", adr, lget(adr));
367
            printf("WRITING: %s\n", line);
368
            if( write(ans_to_tb[1], line, strlen(line)) != (int)strlen(line) ) {
369
                printf("Error writing to tb.\n");
370
                exit(-1);
371
            }
372
            printf("done\n");
373
        }
374
        else if(sscanf(line, "write memory: adr=%x, sel=%x, val=%x", &adr, &sel, &val) == 3) {
375
            if(sel == 0xF) lput(adr, val);
376
            else {
377
                if((sel&0xC) == 0xC) wput(adr, (val>>16)&0xFFFF);
378
                if((sel&0x3) == 0x3) wput(adr+2, val&0xFFFF);
379
            }
380
 
381
            if(sel != 0xF && sel != 0xC && sel != 0x3) {
382
                printf("Error: unknown sel: %x\n", sel);
383
                exit(-1);
384
            }
385
            //printf("READ: write memory: adr=%x, sel=%x, val=%x\n", adr, sel, val);
386
        }
387
        if(step_count < 5) {
388
            ans_write_register(STEP, 0, 1);
389
            step_count++;
390
        }
391
 
392
        printf("count: %d\n", count);
393
    }
394
 
395
 
396
    return 0;
397
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.