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

Subversion Repositories btcminer

[/] [btcminer/] [trunk/] [btcminer.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   btcminer -- BTCMiner for ZTEX USB-FPGA Modules: EZ-USB FX2 firmware
3
   Copyright (C) 2011-2012 ZTEX GmbH
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
#include[ztex-conf.h]   // Loads the configuration macros, see ztex-conf.h for the available macros
20
#include[ztex-utils.h]  // include basic functions
21
 
22
// configure endpoints 2 (used for high speed configuration)
23
EP_CONFIG(2,0,BULK,OUT,512,2);
24
 
25
// select target board
26
#ifndef[TARGET_BOARD]
27
#define[TARGET_BOARD][1.15]
28
#endif
29
 
30
#ifeq[TARGET_BOARD][1.15y]
31
IDENTITY_UFM_1_15Y(10.15.1.2,0);
32
#else
33
IDENTITY_UFM_1_15(10.13.1.1,0);
34
ENABLE_UFM_1_15X_DETECTION;
35
#define[select_num][0]
36
#endif
37
 
38
// enables high speed FPGA configuration, use EP 2
39
ENABLE_HS_FPGA_CONF(2);
40
 
41
 
42
// this product string is also used for identification by the host software
43
#define[PRODUCT_STRING]["btcminer for ZTEX FPGA Modules"]
44
 
45
#define[WATCHDOG_TIMEOUT][(300*100)]
46
 
47
#ifndef[F_M1]
48
#define[F_M1][800]
49
#endif
50
 
51
#ifndef[F_DIV]
52
#define[F_DIV][6]
53
#endif
54
 
55
#ifndef[F_MIN_MULT]
56
#define[F_MIN_MULT][13]
57
#endif
58
 
59
#ifeq[TARGET_BOARD][1.15y]
60
#define[WR_CLK][IOA0]
61
#define[RD_CLK][IOC6]
62
#define[PLL_STOP][IOC5]
63
#define[OEA_MASK][bmBIT0]
64
#define[OEC_MASK][bmBIT6 | bmBIT5]
65
#else
66
#define[WR_CLK][IOA6]
67
#define[RD_CLK][IOC1]
68
#define[PLL_STOP][IOC2]
69
#define[OEA_MASK][bmBIT6]
70
#define[OEC_MASK][bmBIT1 | bmBIT2]
71
#endif
72
 
73
 
74
// !!!!! currently NUM_NONCES must not be larger than 2 !!!!!
75
 
76
__xdata BYTE stopped[NUMBER_OF_FPGAS];
77
__xdata WORD watchdog_cnt;
78
 
79
#define[PRE_FPGA_RESET][PRE_FPGA_RESET
80
    OEC |= bmBIT4;
81
    IOC4 = 1;                   // reset clocks
82
#ifeq[TARGET_BOARD][1.15]
83
    CPUCS &= ~bmBIT1;           // stop clock
84
#endif
85
]
86
 
87
#define[POST_FPGA_CONFIG][POST_FPGA_CONFIG
88
    IOC4 = 1;
89
    PLL_STOP = 1;
90
    OEC = bmBIT0 | bmBIT2 | bmBIT4 | OEC_MASK;
91
    stopped[select_num] = 1;
92
 
93
#ifeq[TARGET_BOARD][1.15]
94
    CPUCS |= bmBIT1;            // start clock
95
    wait(20);
96
#endif
97
    IOC4 = 0;
98
 
99
    OEA = bmBIT2 | bmBIT4 | bmBIT5 | bmBIT7 | OEA_MASK;
100
    IOA = 0;
101
    OEB = 0;
102
    OED = 255;
103
 
104
#ifeq[TARGET_BOARD][1.15]
105
    if ( is_ufm_1_15x ) {
106
        OEA |= bmBIT0;
107
        IOA0 = 1;
108
    }
109
#endif
110
 
111
    wait(50);
112
 
113
    set_freq(0);
114
//    set_freq(F_MULT);
115
]
116
 
117
 
118
#ifeq[TARGET_BOARD][1.15]
119
__xdata BYTE prev_gn1, prev_gn2;
120
#else
121
__xdata BYTE OLD_IOC[NUMBER_OF_FPGAS];
122
#define[PRE_FPGA_SELECT][PRE_FPGA_SELECT
123
    OLD_IOC[prev_select_num] = IOC;
124
    IOC = OLD_IOC[select_num];
125
]
126
#endif
127
 
128
/* *********************************************************************
129
   ***** descriptor ****************************************************
130
   ********************************************************************* */
131
__code BYTE BitminerDescriptor[] =
132
{
133
    5,                          // 0, version number
134
    NUM_NONCES-1,               // 1, number of nonces - 1
135
    (OFFS_NONCES+10000)&255,    // 2, ( nonce offset + 10000 ) & 255
136
    (OFFS_NONCES+10000)>>8,     // 3, ( nonce offset + 10000 ) >> 8
137
    F_M1 & 255,                 // 4, frequency @ F_MULT=1 / 10kHz (LSB)
138
    F_M1 >> 8,                  // 5, frequency @ F_MULT=1 / 10kHz (MSB)
139
    F_MULT-1,                   // 6, frequency multiplier - 1 (default)
140
    F_MAX_MULT-1,               // 7, max frequency multiplier - 1 
141
    (HASHES_PER_CLOCK-1) & 255, // 8, (hashes_per_clck/128-1 ) & 266 
142
    (WORD)(HASHES_PER_CLOCK-1) >> 8,  // 9, (hashes_per_clck/128-1 ) >> 8 
143
    EXTRA_SOLUTIONS,            // 10, number of extra solutions
144
 
145
};
146
__code char bitfileString[] = BITFILE_STRING;
147
__code BYTE bitFileStringTerm = 0;
148
 
149
 
150
/* *********************************************************************
151
   ***** set_freq ******************************************************
152
   ********************************************************************* */
153
#define[PROGEN][IOA5]
154
#define[PROGCLK][IOA2]
155
#define[PROGDATA][IOA4]
156
 
157
void set_freq ( BYTE f ) {
158
    BYTE b,i;
159
 
160
    if ( f < F_MIN_MULT-1 )
161
        f = F_MIN_MULT-1;
162
 
163
    if ( f > F_MAX_MULT-1 )
164
        f = F_MAX_MULT-1;
165
 
166
    PROGEN = 1;
167
 
168
    PROGDATA = 1;
169
    PROGCLK = 1;
170
    PROGCLK = 0;
171
 
172
    PROGDATA = 0;
173
    PROGCLK = 1;
174
    PROGCLK = 0;
175
 
176
    b = F_DIV - 1;
177
    for ( i=0; i<8; i++ ) {
178
        PROGDATA = b & 1;
179
        PROGCLK = 1;
180
        PROGCLK = 0;
181
        b = b >> 1;
182
    }
183
 
184
    PROGEN = 0;
185
 
186
    PROGCLK = 1;
187
    PROGCLK = 0;
188
    PROGCLK = 1;
189
    PROGCLK = 0;
190
    PROGCLK = 1;
191
    PROGCLK = 0;
192
 
193
// load D
194
    PROGEN = 1;
195
 
196
    PROGDATA = 1;
197
    PROGCLK = 1;
198
    PROGCLK = 0;
199
 
200
    PROGCLK = 1;
201
    PROGCLK = 0;
202
 
203
    b = f;
204
    for ( i=0; i<8; i++ ) {
205
        PROGDATA = b & 1;
206
        PROGCLK = 1;
207
        PROGCLK = 0;
208
        b = b >> 1;
209
    }
210
 
211
    PROGEN = 0;
212
 
213
    PROGCLK = 1;
214
    PROGCLK = 0;
215
    PROGCLK = 1;
216
    PROGCLK = 0;
217
    PROGCLK = 1;
218
    PROGCLK = 0;
219
 
220
// GO
221
    PROGDATA = 0;
222
 
223
    PROGEN = 1;
224
 
225
    PROGCLK = 1;
226
    PROGCLK = 0;
227
 
228
    PROGEN = 0;
229
 
230
    _asm
231
        mov r1,#50
232
011000$:
233
        mov r2,#0
234
011001$:
235
        setb _PROGCLK
236
        clr _PROGCLK
237
        setb _PROGCLK
238
        clr _PROGCLK
239
        setb _PROGCLK
240
        clr _PROGCLK
241
        setb _PROGCLK
242
        clr _PROGCLK
243
        djnz r2, 011001$
244
        djnz r1, 011000$
245
    __endasm;
246
}
247
 
248
 
249
/* *********************************************************************
250
   ***** EP0 vendor command 0x80 ***************************************
251
   ********************************************************************* */
252
// write data to FPGA
253
void ep0_write_data () {
254
    BYTE b;
255
 
256
    IOC0 = 1;    // reset on
257
    for ( b=0; b<EP0BCL; b++ ) {
258
        IOD = EP0BUF[b];
259
        RD_CLK = !RD_CLK;
260
    }
261
    IOC0 = 0;    // reset off
262
#ifeq[TARGET_BOARD][1.15]
263
    prev_gn1 = 0;
264
    prev_gn2 = 0;
265
#endif    
266
}
267
 
268
ADD_EP0_VENDOR_COMMAND((0x80,,
269
    watchdog_cnt = 0;
270
 
271
    if ( stopped[select_num] ) {
272
        PLL_STOP = 0;
273
        wait(100);
274
        stopped[select_num]=0;
275
    }
276
,,
277
    ep0_write_data();
278
));;
279
 
280
 
281
/* *********************************************************************
282
   ***** EP0 vendor request 0x81 ***************************************
283
   ********************************************************************* */
284
// read data from FPGA
285
void ep0_read_data () {
286
    BYTE b;
287
    for ( b=0; b<ep0_payload_transfer; b++ ) {
288
        EP0BUF[b] = IOB;
289
        WR_CLK = !WR_CLK;
290
    }
291
#ifeq[TARGET_BOARD][1.15]
292
    prev_gn1 = EP0BUF[0];
293
#endif    
294
    EP0BCH = 0;
295
    EP0BCL = ep0_payload_transfer;
296
}
297
 
298
// read date from FPGA
299
ADD_EP0_VENDOR_REQUEST((0x81,,
300
    IOA7 = 1;   // write start signal
301
    IOA7 = 0;
302
    ep0_read_data ();
303
,,
304
    ep0_read_data ();
305
));;
306
 
307
 
308
/* *********************************************************************
309
   ***** EP0 vendor request 0x82 ***************************************
310
   ********************************************************************* */
311
// send descriptor
312
ADD_EP0_VENDOR_REQUEST((0x82,,
313
    MEM_COPY1(BitminerDescriptor,EP0BUF,64);
314
    EP0BCH = 0;
315
    EP0BCL = SETUPDAT[6];
316
,,
317
));;
318
 
319
 
320
/* *********************************************************************
321
   ***** EP0 vendor command 0x83 ***************************************
322
   ********************************************************************* */
323
// set frequency
324
ADD_EP0_VENDOR_COMMAND((0x83,,
325
    PLL_STOP = 1;
326
    set_freq(SETUPDAT[2]);
327
    wait(20);
328
    PLL_STOP = 0;
329
    stopped[select_num] = 0;
330
    watchdog_cnt = 0;
331
,,
332
    NOP;
333
));;
334
 
335
 
336
/* *********************************************************************
337
   ***** EP0 vendor command 0x84 ***************************************
338
   ********************************************************************* */
339
// suspend
340
ADD_EP0_VENDOR_COMMAND((0x84,,
341
    stopped[select_num] = 1;
342
    PLL_STOP = 1;
343
,,
344
    NOP;
345
));;
346
 
347
 
348
// include the main part of the firmware kit, define the descriptors, ...
349
#include[ztex.h]
350
 
351
void main(void)
352
{
353
    BYTE b, c;
354
// init everything
355
    init_USB();
356
 
357
    watchdog_cnt = 1;
358
    for ( b = 0; b<NUMBER_OF_FPGAS; b++ ) {
359
        stopped[b] = 1;
360
    }
361
 
362
#ifeq[TARGET_BOARD][1.15]
363
    c = 0;
364
    prev_gn1 = 0;
365
    prev_gn2 = 0;
366
#endif
367
 
368
    while (1) {
369
 
370
        wait(10);
371
 
372
#ifeq[TARGET_BOARD][1.15]
373
        if ( is_ufm_1_15x ) {
374
            if ( prev_gn1 != prev_gn2 ) {
375
                c = 25;
376
                prev_gn2 = prev_gn1;
377
            }
378
            IOA0 = ( stopped[0] || c>0 ) ? 1 : 0;
379
            if ( c > 0 ) c--;
380
        }
381
 
382
#endif
383
 
384
        watchdog_cnt += 1;
385
        if ( watchdog_cnt == WATCHDOG_TIMEOUT ) {
386
#ifeq[TARGET_BOARD][1.15]
387
            stopped[0] = 1;
388
            PLL_STOP = 1;
389
#else       
390
            c = select_num;
391
            for ( b = 0; b<NUMBER_OF_FPGAS; b++ ) {
392
                select_fpga(b);
393
                stopped[b] = 1;
394
                PLL_STOP = 1;
395
            }
396
            select_fpga(c);
397
#endif
398
        }
399
    }
400
}

powered by: WebSVN 2.1.0

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