OpenCores
URL https://opencores.org/ocsvn/hdl-deflate/hdl-deflate/trunk

Subversion Repositories hdl-deflate

[/] [hdl-deflate/] [trunk/] [deflate.py] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tomtor
"""
2
MyHDL FPGA Deflate (de)compressor, see RFC 1950/1951
3
 
4
Copyright 2018 by Tom Vijlbrief
5
 
6
See: https://github.com/tomtor
7
 
8
This MyHDL FPGA implementation is partially inspired by the C++ implementation
9
from https://create.stephan-brumme.com/deflate-decoder
10
 
11
"""
12
 
13
from math import log2
14
 
15
from myhdl import always, block, Signal, intbv, Error, ResetSignal, \
16
    enum, always_seq, always_comb, concat, ConcatSignal, modbv
17
 
18
IDLE, RESET, WRITE, READ, STARTC, STARTD = range(6)
19
 
20 5 tomtor
COMPRESS = True
21
MATCH10 = False
22
MATCH10 = True
23
 
24
FAST = False
25
FAST = True
26
 
27 2 tomtor
CWINDOW = 32    # Search window for compression
28
 
29
OBSIZE = 8192   # Size of output buffer (BRAM)
30
IBSIZE = 4 * CWINDOW  # 2048   # Size of input buffer (LUT-RAM)
31
 
32
if OBSIZE > IBSIZE:
33 5 tomtor
    LBSIZE = int(log2(OBSIZE))
34 2 tomtor
else:
35 5 tomtor
    LBSIZE = int(log2(IBSIZE))
36 2 tomtor
 
37 5 tomtor
LIBSIZE = int(log2(IBSIZE))
38
IBS = (1 << LIBSIZE) - 1
39 2 tomtor
 
40
d_state = enum('IDLE', 'HEADER', 'BL', 'READBL', 'REPEAT', 'DISTTREE', 'INIT3',
41
               'HF1', 'HF1INIT', 'HF2', 'HF3', 'HF4', 'HF4_2', 'HF4_3',
42
               'STATIC', 'D_NEXT', 'D_NEXT_2',
43
               'D_INFLATE', 'SPREAD', 'NEXT', 'INFLATE', 'COPY', 'CSTATIC',
44
               'SEARCH', 'DISTANCE', 'CHECKSUM') # , encoding='one_hot')
45
 
46
CodeLengthOrder = (16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14,
47
                   1, 15)
48
 
49
CopyLength = (3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35,
50
              43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258)
51
 
52
ExtraLengthBits = (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
53
                   3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0)
54
 
55
CopyDistance = (1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
56
                257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193,
57
                12289, 16385, 24577)
58
 
59
ExtraDistanceBits = (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
60
 
61
 
62
@block
63
def deflate(i_mode, o_done, i_data, o_iprogress, o_oprogress, o_byte, i_addr,
64
            clk, reset):
65
 
66
    """ Deflate (de)compress
67
 
68
    Ports:
69
 
70
    """
71
 
72
    iram = [Signal(intbv()[8:]) for _ in range(IBSIZE)]
73
    oram = [Signal(intbv()[8:]) for _ in range(OBSIZE)]
74
 
75
    oaddr = Signal(intbv()[LBSIZE:])
76
    oraddr = Signal(intbv()[LBSIZE:])
77
    obyte = Signal(intbv()[8:])
78
    orbyte = Signal(intbv()[8:])
79
 
80 5 tomtor
    iraddr = Signal(intbv()[LIBSIZE:])
81
 
82 2 tomtor
    isize = Signal(intbv()[LBSIZE:])
83
    state = Signal(d_state.IDLE)
84
    method = Signal(intbv()[3:])
85
    final = Signal(bool())
86
    do_compress = Signal(bool())
87
 
88
    numLiterals = Signal(intbv()[9:])
89
    numDistance = Signal(intbv()[6:])
90
    numCodeLength = Signal(intbv()[9:])
91
    b_numCodeLength = Signal(intbv()[9:])
92
 
93
    CodeLengths = 19
94
    MaxCodeLength = 15
95
    InstantMaxBit = 10
96
    EndOfBlock = 256
97
    MaxBitLength = 288
98
    # MaxToken = 285
99
    InvalidToken = 300
100
 
101
    CODEBITS = 10 # MaxCodeLength
102
    BITBITS = 9
103
 
104
    codeLength = [Signal(intbv()[4:]) for _ in range(MaxBitLength+2)]
105
    bits = Signal(intbv()[4:])
106
    bitLengthCount = [Signal(intbv()[9:]) for _ in range(MaxCodeLength+1)]
107
    nextCode = [Signal(intbv()[CODEBITS:]) for _ in range(MaxCodeLength)]
108
    reverse = Signal(intbv()[CODEBITS:])
109
    code_bits = [Signal(intbv()[9:]) for _ in range(MaxBitLength)]
110
    distanceLength = [Signal(intbv()[4:]) for _ in range(32)]
111
 
112
    leaves = [Signal(intbv()[CODEBITS + BITBITS:]) for _ in range(512)]
113
    lwaddr = Signal(intbv()[9:])
114
    # lraddr = Signal(intbv()[9:])
115
    d_leaves = [Signal(intbv()[CODEBITS + BITBITS:]) for _ in range(128)]
116
    # rleaf = Signal(intbv()[CODEBITS + BITBITS:])
117
    wleaf = Signal(intbv()[CODEBITS + BITBITS:])
118
    leaf = Signal(intbv()[CODEBITS + BITBITS:])
119
 
120
    minBits = Signal(intbv()[5:])
121
    maxBits = Signal(intbv()[5:])
122
    d_maxBits = Signal(intbv()[5:])
123
    instantMaxBit = Signal(intbv()[InstantMaxBit:])
124
    d_instantMaxBit = Signal(intbv()[InstantMaxBit:])
125
    instantMask = Signal(intbv()[MaxCodeLength:])
126
    d_instantMask = Signal(intbv()[MaxCodeLength:])
127
    spread = Signal(intbv()[10:])
128
    step = Signal(intbv()[10:])
129
 
130
    static = Signal(bool())
131
 
132
    code = Signal(intbv()[15:])
133
    lastToken = Signal(intbv()[15:])
134
    howOften = Signal(intbv()[9:])
135
 
136
    cur_i = Signal(intbv()[LBSIZE:])
137
    spread_i = Signal(intbv()[9:])
138
    cur_HF1 = Signal(intbv()[10:])
139
    cur_static = Signal(intbv()[9:])
140
    cur_cstatic = Signal(intbv()[LBSIZE:])
141
    cur_search = Signal(intbv(min=-CWINDOW,max=IBSIZE))
142
    cur_dist = Signal(intbv(min=-CWINDOW,max=IBSIZE))
143
    # cur_next = Signal(intbv()[5:])
144
    cur_next = Signal(bool())
145
 
146
    length = Signal(intbv()[LBSIZE:])
147
    offset = Signal(intbv()[LBSIZE:])
148
 
149
    di = Signal(intbv()[LBSIZE:])
150
    old_di = Signal(intbv()[LBSIZE:])
151
    dio = Signal(intbv()[3:])
152
    do = Signal(intbv()[LBSIZE:])
153
    doo = Signal(intbv()[3:])
154
 
155
    b1 = Signal(intbv()[8:])
156
    b2 = Signal(intbv()[8:])
157
    b3 = Signal(intbv()[8:])
158
    b4 = Signal(intbv()[8:])
159 5 tomtor
    b5 = Signal(intbv()[8:])
160 2 tomtor
 
161
    b41 = ConcatSignal(b4, b3, b2, b1)
162
    b41._markUsed()
163
 
164 5 tomtor
    b14 = ConcatSignal(b1, b2, b3, b4)
165
    b14._markUsed()
166
 
167
    if MATCH10:
168
        b6 = Signal(intbv()[8:])
169
        b7 = Signal(intbv()[8:])
170
        b8 = Signal(intbv()[8:])
171
        b9 = Signal(intbv()[8:])
172
        b10 = Signal(intbv()[8:])
173
        b110 = ConcatSignal(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10)
174
        b110._markUsed()
175
    else:
176
        b6 = Signal(bool())
177
        b7 = Signal(bool())
178
        b8 = Signal(bool())
179
        b9 = Signal(bool())
180
        b10 = Signal(bool())
181
        b110 = Signal(bool())
182
 
183 2 tomtor
    nb = Signal(intbv()[3:])
184
 
185
    filled = Signal(bool())
186
 
187
    ob1 = Signal(intbv()[8:])
188
    copy1 = Signal(intbv()[8:])
189 4 tomtor
    copy2 = Signal(intbv()[8:])
190 5 tomtor
    flush = Signal(bool())
191 2 tomtor
 
192
    adler1 = Signal(intbv()[16:])
193
    adler2 = Signal(intbv()[16:])
194
    ladler1 = Signal(intbv()[16:])
195
 
196
 
197
    @always(clk.posedge)
198
    def oramwrite():
199
        oram[oaddr].next = obyte
200
        leaves[lwaddr].next = wleaf
201
 
202
    @always(clk.posedge)
203
    def oramread():
204
        orbyte.next = oram[oraddr]
205
        # rleaf.next = leaves[lraddr]
206
 
207 5 tomtor
    @block
208
    def matcher3(o_m, mi):
209
        @always_comb
210
        def logic():
211
            o_m.next = ((concat(cwindow,b1,b2) >> (8 * mi)) & 0xFFFFFF) == (b14 >> 8)
212
        return logic
213
 
214
    if FAST:
215
        smatch = [Signal(bool()) for _ in range(CWINDOW)]
216
        cwindow = Signal(modbv()[8 * CWINDOW:])
217
        matchers = [matcher3(smatch[mi], mi) for mi in range(CWINDOW)]
218
    else:
219
        cwindow = Signal(bool())
220
        smatch = [Signal(bool())]
221
 
222 2 tomtor
    @always(clk.posedge)
223
    def fill_buf():
224
        if not reset:
225
            print("FILL RESET")
226
            nb.next = 0
227
            b1.next = 0
228
            b2.next = 0
229
            b3.next = 0
230
            b4.next = 0
231 5 tomtor
            old_di.next = 0
232 2 tomtor
        else:
233
            if isize < 4:
234
                pass
235
            elif i_mode == STARTC or i_mode == STARTD:
236
                nb.next = 0
237 5 tomtor
                old_di.next = 0
238 2 tomtor
            else:
239 5 tomtor
                """
240
                if do_compress:
241
                    print("FILL", di, old_di, nb, b1, b2, b3, b4)
242
                """
243
                if FAST: # and nb == 4:
244
                    shift = (di - old_di) * 8
245
                    """
246
                    if shift != 0:
247
                        print("shift", shift, cwindow, b1, b2, b3, b4)
248
                    """
249
                    if shift <= 32:
250
                        cwindow.next = (cwindow << shift) | (b14 >> (32 - shift))
251
                    elif shift == 40:
252
                        cwindow.next = (cwindow << shift) | (b14 << 8) | b5
253
                    elif MATCH10:
254
                        cwindow.next = (cwindow << shift) | (b110 >> (80 - shift))
255
 
256
                if old_di == di:
257
                    nb.next = 4
258
                    # wtick.next = True
259
                old_di.next = di
260
 
261
                # iraddr.next = di
262 2 tomtor
                b1.next = iram[di & IBS]
263
                b2.next = iram[di+1 & IBS]
264
                b3.next = iram[di+2 & IBS]
265
                b4.next = iram[di+3 & IBS]
266 5 tomtor
                b5.next = iram[di+4 & IBS]
267
                if MATCH10:
268
                    b6.next = iram[di+5 & IBS]
269
                    b7.next = iram[di+6 & IBS]
270
                    b8.next = iram[di+7 & IBS]
271
                    b9.next = iram[di+8 & IBS]
272
                    b10.next = iram[di+9 & IBS]
273 2 tomtor
    """
274
    @always(clk.posedge)
275
    def fill_buf():
276
        if not reset:
277
            print("FILL RESET")
278
            nb.next = 0
279
            old_di.next = 0
280
            b1.next = 0
281
            b2.next = 0
282
            b3.next = 0
283
            b4.next = 0
284
        else:
285
            if isize < 4:
286
                pass
287
            elif i_mode == STARTC or i_mode == STARTD:
288
                nb.next = 0
289
            elif not filled and nb == 4 and di - old_di <= 4:
290
                delta = di - old_di
291
                if delta == 1:
292
                    # print("delta == 1")
293 5 tomtor
                    if FAST:
294
                        cwindow.next = (cwindow << 8) | b1
295 2 tomtor
                    b1.next = b2
296
                    b2.next = b3
297
                    b3.next = b4
298
                    b4.next = iram[di+3 & IBS]
299
                    # nb.next = 4
300
                elif delta == 2:
301 5 tomtor
                    print("delta == 2")
302
                    if FAST:
303
                        cwindow.next = (cwindow << 16) | (b14 >> 16)
304 2 tomtor
                    b1.next = b3
305
                    b2.next = b4
306
                    b3.next = iram[di+2 & IBS]
307
                    nb.next = 3
308
                elif delta == 3:
309 5 tomtor
                    print("delta == 3")
310
                    if FAST:
311
                        cwindow.next = (cwindow << 24) | (b14 >> 8)
312 2 tomtor
                    b1.next = b4
313
                    b2.next = iram[di+1 & IBS]
314
                    nb.next = 2
315
                elif delta == 4:
316 5 tomtor
                    print("delta == 4")
317
                    if FAST:
318
                        cwindow.next = (cwindow << 32) | (b14)
319 2 tomtor
                    b1.next = iram[di & IBS]
320
                    nb.next = 1
321
                else:
322
                    pass
323
            elif not filled or nb == 0:
324 5 tomtor
                print("nb = 0")
325 2 tomtor
                b1.next = iram[di & IBS]
326
                nb.next = 1
327
            elif not filled or nb == 1:
328 5 tomtor
                print("nb = 1")
329 2 tomtor
                b2.next = iram[di+1 & IBS]
330
                nb.next = 2
331
            elif not filled or nb == 2:
332 5 tomtor
                print("nb = 2")
333
                # raise Error("nb == 2")
334 2 tomtor
                b3.next = iram[di+2 & IBS]
335
                nb.next = 3
336
            elif not filled or nb == 3:
337 5 tomtor
                print("nb = 3")
338 2 tomtor
                b4.next = iram[di+3 & IBS]
339
                nb.next = 4
340
            else:
341
                pass
342
            old_di.next = di
343
    """
344
 
345
    def get4(boffset, width):
346
        if nb != 4:
347
            print("----NB----")
348
            raise Error("NB")
349
        return (b41 >> (dio + boffset)) & ((1 << width) - 1)
350
        # return b41[dio + boffset + width: dio + boffset]
351
 
352
    def adv(width):
353
        # print("adv", width, di, dio, do, doo)
354
        nshift = ((dio + width) >> 3)
355
        # print("nshift: ", nshift)
356
 
357
        o_iprogress.next = di
358
        dio.next = (dio + width) & 0x7
359
        di.next = di + nshift
360
 
361
        if nshift != 0:
362
            filled.next = False
363
 
364
    def put(d, width):
365
        if width > 9:
366
            raise Error("width > 9")
367
        if d > ((1 << width) - 1):
368
            raise Error("too big")
369
        # print("put:", d, width, do, doo, ob1, (ob1 | (d << doo)))
370
        return (ob1 | (d << doo)) & 0xFF
371
 
372
    def put_adv(d, width):
373
        if width > 9:
374
            raise Error("width > 9")
375
        if d > ((1 << width) - 1):
376
            raise Error("too big")
377
        # print("put_adv:", d, width, do, doo, di, dio)
378
        pshift = (doo + width) > 8
379
        # print("pshift: ", pshift)
380
        if pshift:
381
            carry = width - (8 - doo)
382
            # print("carry:", carry, d >> (8 - carry))
383
            ob1.next = d >> (width - carry)
384
        else:
385
            # print("put_adv:", ob1, d, doo)
386
            ob1.next = ob1 | (d << doo)
387
            # print("ob1.next", ob1 | (d << doo))
388
        do.next = do + pshift
389
        o_oprogress.next = do + pshift
390
        doo_next = (doo + width) & 0x7
391
        if doo_next == 0:
392
            flush.next = True
393
        doo.next = doo_next
394
 
395
    def do_flush():
396
        # print("FLUSH")
397
        flush.next = False
398
        ob1.next = 0
399
        o_oprogress.next = do + 1
400
        do.next = do + 1
401
 
402
    def rev_bits(b, nb):
403
        if b >= 1 << nb:
404
            raise Error("too few bits")
405
            print("too few bits")
406
        r = (((b >> 14) & 0x1) << 0) | (((b >> 13) & 0x1) << 1) | \
407
            (((b >> 12) & 0x1) << 2) | (((b >> 11) & 0x1) << 3) | \
408
            (((b >> 10) & 0x1) << 4) | (((b >> 9) & 0x1) << 5) | \
409
            (((b >> 8) & 0x1) << 6) | (((b >> 7) & 0x1) << 7) | \
410
            (((b >> 6) & 0x1) << 8) | (((b >> 5) & 0x1) << 9) | \
411
            (((b >> 4) & 0x1) << 10) | (((b >> 3) & 0x1) << 11) | \
412
            (((b >> 2) & 0x1) << 12) | (((b >> 1) & 0x1) << 13) | \
413
            (((b >> 0) & 0x1) << 14)
414
        r >>= (15 - nb)
415
        return r
416
 
417
    def makeLeaf(lcode, lbits):
418
        if lcode >= 1 << CODEBITS:
419
            raise Error("code too big")
420
        if lbits >= 1 << BITBITS:
421
            raise Error("bits too big")
422
        return (lcode << BITBITS) | lbits
423
 
424
    def get_bits(aleaf):
425
        return aleaf & ((1 << BITBITS) - 1)
426
 
427
    def get_code(aleaf):
428
        return (aleaf >> BITBITS)  # & ((1 << CODEBITS) - 1)
429
 
430
    @always(clk.posedge)
431
    def io_logic():
432
        if i_mode == WRITE:
433
 
434
            # print("WRITE:", i_addr, i_data)
435
            iram[i_addr & IBS].next = i_data
436
            isize.next = i_addr
437
 
438
        elif i_mode == READ:
439
 
440
            # o_data.next = oram[i_addr]
441
            # oraddr.next = i_addr
442
            o_byte.next = oram[i_addr]
443
 
444
        else:
445
            pass
446
 
447
    @always(clk.posedge)
448
    def logic():
449
        if not reset:
450
            print("DEFLATE RESET")
451
            state.next = d_state.IDLE
452
            o_done.next = False
453
            # oaddr.next = 0
454
            # obyte.next = 0
455
        else:
456
 
457
            if state == d_state.IDLE:
458
 
459 5 tomtor
                if COMPRESS and i_mode == STARTC:
460 2 tomtor
 
461
                    print("STARTC")
462
                    do_compress.next = True
463
                    method.next = 1
464
                    o_done.next = False
465
                    o_iprogress.next = 0
466
                    o_oprogress.next = 0
467
                    di.next = 0
468
                    dio.next = 0
469
                    do.next = 0
470
                    doo.next = 0
471
                    filled.next = True
472
                    cur_static.next = 0
473
                    state.next = d_state.STATIC
474
 
475
                elif i_mode == STARTD:
476
 
477
                    do_compress.next = False
478
                    o_done.next = False
479
                    o_iprogress.next = 0
480
                    o_oprogress.next = 0
481
                    di.next = 0
482
                    dio.next = 0
483
                    # oaddr.next = 0
484
                    do.next = 0
485
                    doo.next = 0
486
                    filled.next = True
487
                    state.next = d_state.HEADER
488
 
489
                else:
490
                    pass
491
 
492
            elif state == d_state.HEADER:
493
 
494
                if not filled:
495
                    filled.next = True
496
                elif nb < 4:
497
                    pass
498
                # Read block header
499
                elif di == 0:
500
                    #print(iram[di & IBS])
501
                    #if iram[di & IBS] == 0x78:
502
                    if b1 == 0x78:
503
                        print("deflate mode")
504
                    else:
505
                        print(di, dio, nb, b1, b2, b3, b4, isize)
506
                        raise Error("unexpected mode")
507
                        o_done.next = True
508
                        state.next = d_state.IDLE
509
                    adv(16)
510
                else:
511
                    if get4(0, 1):
512
                        print("final")
513
                        final.next = True
514
                    hm = get4(1, 2)
515
                    method.next = hm
516
                    print("method", hm)
517
                    # print(di, dio, nb, b1, b2, b3, b4, hm, isize)
518
                    if hm == 2:
519
                        state.next = d_state.BL
520
                        numCodeLength.next = 0
521
                        numLiterals.next = 0
522
                        static.next = False
523
                        adv(3)
524
                    elif hm == 1:
525
                        static.next = True
526
                        cur_static.next = 0
527
                        state.next = d_state.STATIC
528
                        adv(3)
529
                    elif hm == 0:
530
                        state.next = d_state.COPY
531
                        skip = 8 - dio
532
                        if skip <= 2:
533
                            skip = 16 - dio
534
                        length.next = get4(skip, 16)
535
                        adv(skip + 16)
536
                        cur_i.next = 0
537
                        offset.next = 7
538
                    else:
539
                        state.next = d_state.IDLE
540
                        print("Bad method")
541
                        raise Error("Bad method")
542
 
543
            elif state == d_state.CSTATIC:
544
 
545
                # print("CSTATIC", cur_i, ob1, do, doo, isize)
546
 
547
                no_adv = 0
548 5 tomtor
                if not COMPRESS:
549
                    pass
550
                elif not filled:
551 2 tomtor
                    no_adv = 1
552
                    filled.next = True
553
                elif nb < 4:
554
                    no_adv = 1
555
                    pass
556
                elif cur_cstatic == 0:
557
                    flush.next = False
558
                    ob1.next = 0
559
                    adler1.next = 1
560
                    adler2.next = 0
561
                    ladler1.next = 0
562
                    oaddr.next = 0
563
                    obyte.next = 0x78
564
                elif cur_cstatic == 1:
565
                    oaddr.next = 1
566
                    obyte.next = 0x9c
567
                    do.next = 2
568
                elif cur_cstatic == 2:
569
                    oaddr.next = do
570
                    obyte.next = put(0x3, 3)
571
                    put_adv(0x3, 3)
572
                elif flush:
573
                    print("flush", do, ob1)
574
                    no_adv = 1
575
                    oaddr.next = do
576
                    obyte.next = ob1
577
                    do_flush()
578
                elif cur_cstatic - 3 > isize:
579
                    if cur_cstatic - 3 == isize + 1:
580
                        print("Put EOF", do)
581
                        cs_i = EndOfBlock
582
                        outlen = codeLength[cs_i]
583
                        outbits = code_bits[cs_i]
584
                        print("EOF BITS:", cs_i, outlen, outbits)
585
                        oaddr.next = do
586
                        obyte.next = put(outbits, outlen)
587
                        put_adv(outbits, outlen)
588
                    elif cur_cstatic - 3 == isize + 2:
589
                        print("calc end adler")
590
                        adler2.next = (adler2 + ladler1) % 65521
591
                        if doo != 0:
592
                            oaddr.next = do
593
                            obyte.next = ob1
594
                            do.next = do + 1
595
                    elif cur_cstatic - 3 == isize + 3:
596
                        print("c1")
597
                        oaddr.next = do
598
                        obyte.next = adler2 >> 8
599
                        do.next = do + 1
600
                    elif cur_cstatic - 3 == isize + 4:
601
                        print("c2")
602
                        oaddr.next = do
603
                        obyte.next = adler2 & 0xFF
604
                        do.next = do + 1
605
                    elif cur_cstatic - 3 == isize + 5:
606
                        print("c3")
607
                        oaddr.next = do
608
                        obyte.next = adler1 >> 8
609
                        do.next = do + 1
610
                    elif cur_cstatic - 3 == isize + 6:
611
                        print("c4")
612
                        oaddr.next = do
613
                        obyte.next = adler1 & 0xFF
614
                    elif cur_cstatic - 3 == isize + 7:
615
                        print("EOF finish", do)
616
                        o_done.next = True
617
                        o_oprogress.next = do + 1
618
                        state.next = d_state.IDLE
619
                    else:
620
                        print(cur_cstatic, isize)
621
                        raise Error("???")
622
                else:
623
                    bdata = iram[di]
624
                    # Fix this when > 1 byte output:
625
                    # print("cs1", bdata)
626
                    adler1_next = (adler1 + bdata) % 65521
627
                    adler1.next = adler1_next
628
                    adler2.next = (adler2 + ladler1) % 65521
629
                    ladler1.next = adler1_next
630
                    # print("in: ", bdata, di, isize)
631
                    state.next = d_state.SEARCH
632 5 tomtor
                    cur_search.next = di - 1
633 2 tomtor
 
634
                if not no_adv:
635
                    cur_cstatic.next = cur_cstatic + 1
636
 
637
            elif state == d_state.DISTANCE:
638
 
639 5 tomtor
                if not COMPRESS:
640
                    pass
641
                elif flush:
642 2 tomtor
                    do_flush()
643
                else:
644
                    # print("DISTANCE", di, do, cur_i, cur_dist)
645
                    nextdist = CopyDistance[cur_i+1]
646
                    if nextdist > cur_dist:
647
                        copydist = CopyDistance[cur_i]
648
                        # print("Found distance", copydist)
649
                        extra_dist = cur_dist - copydist
650
                        # print("extra dist", extra_dist)
651
                        extra_bits = ExtraDistanceBits[cur_i // 2]
652
                        # print("extra bits", extra_bits)
653
                        if extra_dist > ((1 << extra_bits) - 1):
654
                            raise Error("too few extra")
655
                        # print("rev", cur_i, rev_bits(cur_i, 5))
656
                        outcode = (rev_bits(cur_i, 5) | (extra_dist << 5))
657
                        # print("outcode", outcode)
658
                        oaddr.next = do
659
                        obyte.next = put(outcode, 5 + extra_bits)
660
                        put_adv(outcode, 5 + extra_bits)
661
                        #state.next = d_state.CSTATIC
662
                        cur_i.next = di - length + 1
663
                        state.next = d_state.CHECKSUM
664
                    else:
665
                        cur_i.next = cur_i + 1
666
 
667
            elif state == d_state.CHECKSUM:
668
 
669 5 tomtor
                if not COMPRESS:
670
                    pass
671
                elif cur_i < di:
672 2 tomtor
                    # print("CHECKSUM", cur_i, di, iram[cur_i])
673
                    bdata = iram[cur_i & IBS]
674
                    adler1_next = (adler1 + bdata) % 65521
675
                    adler1.next = adler1_next
676
                    adler2.next = (adler2 + ladler1) % 65521
677
                    ladler1.next = adler1_next
678
                    cur_i.next = cur_i.next + 1
679
                else:
680
                    state.next = d_state.CSTATIC
681
 
682
            elif state == d_state.SEARCH:
683
 
684 5 tomtor
                if not COMPRESS:
685
                    pass
686
                elif not filled:
687 2 tomtor
                    filled.next = True
688
                elif nb < 4:
689
                    pass
690
                else:
691
                    if cur_search >= 0 \
692
                             and cur_search >= di - CWINDOW \
693
                             and di < isize - 3:
694 5 tomtor
 
695
                        if FAST:
696
                            found = 0
697
                            fmatch = 0
698
                            for si in range(CWINDOW):
699
                                # print("test", di, si, di - si - 1)
700
                                if smatch[si]:
701
                                    print("fmatch", si)
702
                                    fmatch = si
703
                                    found = 1
704
                                    break
705
                            if not found or di - fmatch - 1 < 0:
706
                                cur_search.next = -1
707
                                # print("NO FSEARCH")
708
                            else:
709
                                distance = fmatch + 1
710
                                # print("FSEARCH", distance)
711
                                fmatch = di - fmatch + 2
712
                                # Length is 3 code
713
                                lencode = 257
714
                                match = 3
715
 
716
                                if di < isize - 4 and \
717
                                        iram[fmatch & IBS] == b4:
718
                                    lencode = 258
719
                                    match = 4
720
                                    if di < isize - 5 and \
721
                                            iram[fmatch+1 & IBS] == b5:
722
                                        lencode = 259
723
                                        match = 5
724
                                        if MATCH10 and di < isize - 6 and \
725
                                                iram[fmatch+2 & IBS] == b6:
726
                                            lencode = 260
727
                                            match = 6
728
                                            if di < isize - 7 and \
729
                                                    iram[fmatch+3 & IBS] == b7:
730
                                                lencode = 261
731
                                                match = 7
732
                                                if di < isize - 8 and \
733
                                                        iram[fmatch+4 & IBS] == b8:
734
                                                    lencode = 262
735
                                                    match = 8
736
                                                    if di < isize - 9 and \
737
                                                            iram[fmatch+5 & IBS] == b9:
738
                                                        lencode = 263
739
                                                        match = 9
740
                                                        if di < isize - 10 and \
741
                                                                iram[fmatch+6 & IBS] == b10:
742
                                                            lencode = 264
743
                                                            match = 10
744
 
745
                                print("fast:", distance, di, isize, match)
746
                                outlen = codeLength[lencode]
747
                                outbits = code_bits[lencode]
748
                                # print("BITS:", outlen, outbits)
749
                                oaddr.next = do
750
                                obyte.next = put(outbits, outlen)
751
                                put_adv(outbits, outlen)
752
 
753
                                # distance = di - cur_search
754
                                # print("distance", distance)
755
                                cur_dist.next = distance
756
                                cur_i.next = 0
757
                                # adv(match * 8)
758
                                di.next = di + match
759
                                cur_cstatic.next = cur_cstatic + match - 1
760
                                length.next = match
761
                                state.next = d_state.DISTANCE
762
 
763
                        elif iram[cur_search & IBS] == b1 and \
764 2 tomtor
                                iram[cur_search+1 & IBS] == b2 and \
765
                                iram[cur_search+2 & IBS] == b3:
766
                            # Length is 3 code
767
                            lencode = 257
768
                            match = 3
769
 
770
                            if di < isize - 4 and \
771
                                    iram[cur_search+3 & IBS] == b4: # iram[di + 3 & IBS]:
772
                                lencode = 258
773
                                match = 4
774
                                if di < isize - 5 and \
775
                                        iram[cur_search+4 & IBS] == iram[di + 4 & IBS]:
776
                                    lencode = 259
777
                                    match = 5
778 5 tomtor
                                    if MATCH10 and di < isize - 6 and \
779 2 tomtor
                                            iram[cur_search+5 & IBS] == iram[di + 5 & IBS]:
780
                                        lencode = 260
781
                                        match = 6
782
                                        if di < isize - 7 and \
783
                                                iram[cur_search+6 & IBS] == iram[di + 6 & IBS]:
784
                                            lencode = 261
785
                                            match = 7
786
                                            if di < isize - 8 and \
787
                                                    iram[cur_search+7 & IBS] == iram[di + 7 & IBS]:
788
                                                lencode = 262
789
                                                match = 8
790
                                                if di < isize - 9 and \
791
                                                        iram[cur_search+8 & IBS] == iram[di + 8 & IBS]:
792
                                                    lencode = 263
793
                                                    match = 9
794
                                                    if di < isize - 10 and \
795
                                                            iram[cur_search+9 & IBS] == iram[di + 9 & IBS]:
796
                                                        lencode = 264
797
                                                        match = 10
798 5 tomtor
 
799 2 tomtor
                            print("found:", cur_search, di, isize, match)
800
                            outlen = codeLength[lencode]
801
                            outbits = code_bits[lencode]
802
                            # print("BITS:", outlen, outbits)
803
                            oaddr.next = do
804
                            obyte.next = put(outbits, outlen)
805
                            put_adv(outbits, outlen)
806
 
807
                            distance = di - cur_search
808
                            # print("distance", distance)
809
                            cur_dist.next = distance
810
                            cur_i.next = 0
811
                            # adv(match * 8)
812
                            di.next = di + match
813
                            cur_cstatic.next = cur_cstatic + match - 1
814
                            length.next = match
815
                            state.next = d_state.DISTANCE
816
                        else:
817
                            cur_search.next = cur_search - 1
818
                    else:
819 5 tomtor
                        # print("NO MATCH")
820 2 tomtor
                        bdata = iram[di]
821
                        # adv(8)
822
                        di.next = di + 1
823
                        outlen = codeLength[bdata]
824
                        outbits = code_bits[bdata]
825
                        # print("CBITS:", bdata, outlen, outbits)
826
                        oaddr.next = do
827
                        obyte.next = put(outbits, outlen)
828
                        put_adv(outbits, outlen)
829
                        state.next = d_state.CSTATIC
830
 
831
            elif state == d_state.STATIC:
832
 
833
                for stat_i in range(0, 144):
834
                    codeLength[stat_i].next = 8
835
                for stat_i in range(144, 256):
836
                    codeLength[stat_i].next = 9
837
                for stat_i in range(256, 280):
838
                    codeLength[stat_i].next = 7
839
                for stat_i in range(280, 288):
840
                    codeLength[stat_i].next = 8
841
                numCodeLength.next = 288
842
                cur_HF1.next = 0
843
                state.next = d_state.HF1
844
                """
845
                if cur_static < 288:
846
                    if cur_static < 144:
847
                        codeLength[cur_static].next = 8
848
                    elif cur_static < 256:
849
                        codeLength[cur_static].next = 9
850
                    elif cur_static < 280:
851
                        codeLength[cur_static].next = 7
852
                    else:
853
                        codeLength[cur_static].next = 8
854
                    cur_static.next = cur_static + 1
855
                else:
856
                    numCodeLength.next = 288
857
                    cur_HF1.next = 0
858
                    state.next = d_state.HF1
859
                """
860
 
861
            elif state == d_state.BL:
862
 
863
                if not filled:
864
                    filled.next = True
865
                elif nb < 4:
866
                    pass
867
                elif numLiterals == 0:
868
                    numLiterals.next = 257 + get4(0, 5)
869
                    print("NL:", 257 + get4(0, 5))
870
                    numDistance.next = 1 + get4(5, 5)
871
                    print("ND:", 1 + get4(5, 5))
872
                    b_numCodeLength.next = 4 + get4(10, 4)
873
                    print("NCL:", 4 + get4(10, 4))
874
                    numCodeLength.next = 0
875
                    adv(14)
876
                else:
877
                    if numCodeLength < CodeLengths:
878
                        clo_i = CodeLengthOrder[numCodeLength]
879
                        # print("CLI: ", clo_i)
880
                        if numCodeLength < b_numCodeLength:
881
                            codeLength[clo_i].next = get4(0, 3)
882
                            adv(3)
883
                        else:
884
                            # print("SKIP")
885
                            codeLength[clo_i].next = 0
886
                        numCodeLength.next = numCodeLength + 1
887
                    else:
888
                        numCodeLength.next = CodeLengths
889
                        cur_HF1.next = 0
890
                        state.next = d_state.HF1
891
 
892
            elif state == d_state.READBL:
893
 
894
                if not filled:
895
                    filled.next = True
896
                elif nb < 4:
897
                    pass
898
                elif numCodeLength < numLiterals + numDistance:
899
                    # print(numLiterals + numDistance, numCodeLength)
900
                    n_adv = 0
901
                    if code < 16:
902
                        howOften.next = 1
903
                        lastToken.next = code
904
                    elif code == 16:
905
                        howOften.next = 3 + get4(0, 2)
906
                        n_adv = 2
907
                    elif code == 17:
908
                        howOften.next = 3 + get4(0, 3)
909
                        lastToken.next = 0
910
                        n_adv = 3
911
                    elif code == 18:
912
                        howOften.next = 11 + get4(0, 7)
913
                        lastToken.next = 0
914
                        n_adv = 7
915
                    else:
916
                        raise Error("Invalid data")
917
 
918
                    # print(numCodeLength, howOften, code, di, n_adv)
919
                    if n_adv != 0:
920
                        adv(n_adv)
921
 
922
                    state.next = d_state.REPEAT
923
                else:
924
                    print("FILL UP")
925
 
926
                    for dbl_i in range(32):
927
                        dbl = 0
928
                        if dbl_i + numLiterals < numCodeLength:
929
                            dbl = int(codeLength[dbl_i + numLiterals])
930
                        # print("dbl:", dbl)
931
                        distanceLength[dbl_i].next = dbl
932
 
933
                    # print(numCodeLength, numLiterals, MaxBitLength)
934
 
935
                    cur_i.next = numLiterals
936
                    state.next = d_state.INIT3
937
 
938
            elif state == d_state.INIT3:
939
 
940
                    if cur_i < MaxBitLength:
941
                        codeLength[cur_i].next = 0
942
                        cur_i.next = cur_i + 1
943
                    else:
944
                        numCodeLength.next = MaxBitLength
945
                        method.next = 3  # Start building bit tree
946
                        cur_HF1.next = 0
947
                        state.next = d_state.HF1
948
 
949
            elif state == d_state.DISTTREE:
950
 
951
                print("DISTTREE")
952
                for dist_i in range(32):
953
                    codeLength[dist_i].next = distanceLength[dist_i]
954
                    # print(dist_i, distanceLength[dist_i])
955
                numCodeLength.next = 32
956
                method.next = 4  # Start building dist tree
957
                # cur_i.next = 0
958
                cur_HF1.next = 0
959
                state.next = d_state.HF1
960
 
961
            elif state == d_state.REPEAT:
962
 
963
                # print("HOWOFTEN: ", numCodeLength, howOften)
964
                if howOften != 0:
965
                    codeLength[numCodeLength].next = lastToken
966
                    howOften.next = howOften - 1
967
                    numCodeLength.next = numCodeLength + 1
968
                elif numCodeLength < numLiterals + numDistance:
969
                    cur_next.next = 0
970
                    state.next = d_state.NEXT
971
                else:
972
                    state.next = d_state.READBL
973
 
974
            elif state == d_state.HF1:
975
 
976
                if cur_HF1 < len(bitLengthCount):
977
                    bitLengthCount[cur_HF1].next = 0
978
                if cur_HF1 < len(d_leaves):
979
                    d_leaves[cur_HF1].next = 0
980
                if method != 4 and cur_HF1 < len(leaves):
981
                    lwaddr.next = cur_HF1
982
                    wleaf.next = 0
983
                    # leaves[cur_HF1].next = 0
984
                limit = len(leaves)
985
                if method == 4:
986
                    limit = len(d_leaves)
987
                if cur_HF1 < limit:
988
                    cur_HF1.next = cur_HF1 + 1
989
                else:
990
                    print("DID HF1 INIT")
991
                    cur_i.next = 0
992
                    state.next = d_state.HF1INIT
993
 
994
            elif state == d_state.HF1INIT:
995
                # get frequencies of each bit length and ignore 0's
996
 
997
                # print("HF1")
998
                if cur_i < numCodeLength:
999
                    j = codeLength[cur_i]
1000
                    bitLengthCount[j].next = bitLengthCount[j] + 1
1001
                    # print(cur_i, j, bitLengthCount[j] + 1)
1002
                    cur_i.next = cur_i + 1
1003
                else:
1004
                    bitLengthCount[0].next = 0
1005
                    state.next = d_state.HF2
1006
                    cur_i.next = 1
1007
                    if method == 4:
1008
                        d_maxBits.next = 0
1009
                    else:
1010
                        maxBits.next = 0
1011
                    minBits.next = MaxCodeLength
1012
 
1013
            elif state == d_state.HF2:
1014
                # shortest and longest codes
1015
 
1016
                # print("HF2")
1017
                if cur_i <= MaxCodeLength:
1018
                    if bitLengthCount[cur_i] != 0:
1019
                        if cur_i < minBits:
1020
                            minBits.next = cur_i
1021
                        if method == 4:
1022
                            if cur_i > d_maxBits:
1023
                                d_maxBits.next = cur_i
1024
                        else:
1025
                            if cur_i > maxBits:
1026
                                maxBits.next = cur_i
1027
                    cur_i.next = cur_i + 1
1028
                else:
1029
                    print(minBits, maxBits)
1030
                    t = InstantMaxBit
1031
                    if method == 4:
1032
                        if t > int(d_maxBits):
1033
                            t = int(d_maxBits)
1034
                        d_instantMaxBit.next = t
1035
                        d_instantMask.next = (1 << t) - 1
1036
                    else:
1037
                        if t > int(maxBits):
1038
                            t = int(maxBits)
1039
                        instantMaxBit.next = t
1040
                        instantMask.next = (1 << t) - 1
1041
                    print((1 << t) - 1)
1042
                    state.next = d_state.HF3
1043
                    cur_i.next = minBits
1044
                    code.next = 0
1045
                    for hf2_i in range(len(nextCode)):
1046
                        nextCode[hf2_i].next = 0
1047
                    print("to HF3")
1048
 
1049
            elif state == d_state.HF3:
1050
                # find bit code for first element of each bitLength group
1051
 
1052
                # print("HF3")
1053
                amb = maxBits
1054
                if method == 4:
1055
                    amb = d_maxBits
1056
                if cur_i <= amb:
1057
                    ncode = ((code + bitLengthCount[cur_i - 1]) << 1)
1058
                    code.next = ncode
1059
                    nextCode[cur_i].next = ncode
1060
                    # print(cur_i, ncode)
1061
                    cur_i.next = cur_i + 1
1062
                else:
1063
                    state.next = d_state.HF4
1064
                    cur_i.next = 0
1065
                    spread_i.next = 0
1066
                    print("to HF4")
1067
 
1068
            elif state == d_state.HF4_2:
1069
 
1070
                canonical = nextCode[bits]
1071
                nextCode[bits].next = nextCode[bits] + 1
1072
                if bits > MaxCodeLength:
1073
                    raise Error("too many bits: %d" % bits)
1074
                # print(canonical, bits)
1075
                reverse.next = rev_bits(canonical, bits)
1076
                # print("LEAF: ", spread_i, bits, reverse, canonical)
1077
                leaf.next = makeLeaf(spread_i, bits)
1078
                state.next = d_state.HF4_3
1079
 
1080
            elif state == d_state.HF4_3:
1081
 
1082
                if method == 4:
1083
                    d_leaves[reverse].next = leaf # makeLeaf(spread_i, bits)
1084
                    if bits <= d_instantMaxBit:
1085
                        if reverse + (1 << bits) <= d_instantMask:
1086
                            step.next = 1 << bits
1087
                            spread.next = reverse + (1 << bits)
1088
                            state.next = d_state.SPREAD
1089
                        else:
1090
                            spread_i.next = spread_i + 1
1091
                            state.next = d_state.HF4
1092
                    else:
1093
                        state.next = d_state.HF4
1094
                        spread_i.next = spread_i + 1
1095
                else:
1096
                    wleaf.next = leaf
1097
                    lwaddr.next = reverse
1098
                    # leaves[reverse].next = leaf # makeLeaf(spread_i, bits)
1099
                    code_bits[spread_i].next = reverse
1100
                    if bits <= instantMaxBit:
1101
                        if reverse + (1 << bits) <= instantMask:
1102
                            step.next = 1 << bits
1103
                            spread.next = reverse + (1 << bits)
1104
                            state.next = d_state.SPREAD
1105
                        else:
1106
                            spread_i.next = spread_i + 1
1107
                            state.next = d_state.HF4
1108
                    else:
1109
                        spread_i.next = spread_i + 1
1110
                        state.next = d_state.HF4
1111
 
1112
            elif state == d_state.HF4:
1113
                # create binary codes for each literal
1114
 
1115
                if spread_i < numCodeLength:
1116
                    bits_next = codeLength[spread_i]
1117
                    if bits_next != 0:
1118
                        bits.next = bits_next
1119
                        state.next = d_state.HF4_2
1120
                    else:
1121
                        spread_i.next = spread_i + 1
1122
                else:
1123
                    if do_compress:
1124
                        state.next = d_state.CSTATIC
1125
                        cur_cstatic.next = 0
1126
                    elif method == 3:
1127
                        state.next = d_state.DISTTREE
1128
                    elif method == 4:
1129
                        print("DEFLATE m2!")
1130
                        state.next = d_state.NEXT
1131
                    elif method == 2:
1132
                        numCodeLength.next = 0
1133
                        state.next = d_state.NEXT
1134
                    else:
1135
                        state.next = d_state.NEXT
1136
                    cur_next.next = 0
1137
                    cur_i.next = 0
1138
 
1139
            elif state == d_state.SPREAD:
1140
 
1141
                if method == 4:
1142
                    # print(spread, spread_i)
1143
                    d_leaves[spread].next = makeLeaf(
1144
                        spread_i, codeLength[spread_i])
1145
                else:
1146
                    lwaddr.next = spread
1147
                    wleaf.next = makeLeaf(spread_i, codeLength[spread_i])
1148
                    # leaves[spread].next = makeLeaf(spread_i, codeLength[spread_i])
1149
                # print("SPREAD:", spread, step, instantMask)
1150
                aim = instantMask
1151
                if method == 4:
1152
                    aim = d_instantMask
1153
                if spread > aim - step:
1154
                    spread_i.next = spread_i + 1
1155
                    state.next = d_state.HF4
1156
                else:
1157
                    spread.next = spread + step
1158
 
1159
            elif state == d_state.NEXT:
1160
 
1161
                if not filled:
1162
                    filled.next = True
1163
                elif nb < 4:
1164
                    pass
1165
                elif cur_next == 0:
1166
                    # print("INIT:", di, dio, instantMaxBit, maxBits)
1167
                    cto = get4(0, maxBits)
1168
                    cur_next.next = 1  # instantMaxBit
1169
                    mask = (1 << instantMaxBit) - 1
1170
                    # lraddr.next = (cto & mask)
1171
                    leaf.next = leaves[cto & mask]
1172
                    # print(cur_next, mask, leaf, maxBits)
1173
                else:
1174
                    if get_bits(leaf) < 1:
1175
                        print("< 1 bits: ")
1176
                        raise Error("< 1 bits: ")
1177
                    adv(get_bits(leaf))
1178
                    if get_code(leaf) == 0:
1179
                        print("leaf 0")
1180
                    code.next = get_code(leaf)
1181
                    # print("ADV:", di, get_bits(leaf), get_code(leaf))
1182
                    if method == 2:
1183
                        state.next = d_state.READBL
1184
                    else:
1185
                        state.next = d_state.INFLATE
1186
 
1187
            elif state == d_state.D_NEXT:
1188
 
1189
                if not filled:
1190
                    filled.next = True
1191
                elif nb < 4:
1192
                    pass
1193
                else:
1194
                    # print("D_INIT:", di, dio, d_instantMaxBit, d_maxBits)
1195
                    token = code - 257
1196
                    # print("token: ", token)
1197
                    extraLength = ExtraLengthBits[token]
1198
                    # print("extra length bits:", extraLength)
1199
                    cto = get4(extraLength, d_maxBits)
1200
                    mask = (1 << d_instantMaxBit) - 1
1201
                    leaf.next = d_leaves[cto & mask]
1202
                    state.next = d_state.D_NEXT_2
1203
 
1204
            elif state == d_state.D_NEXT_2:
1205
 
1206
                if get_bits(leaf) == 0:
1207
                    raise Error("0 bits")
1208
                token = code - 257
1209
                # print("E2:", token, leaf)
1210
                tlength = CopyLength[token]
1211
                # print("tlength:", tlength)
1212
                extraLength = ExtraLengthBits[token]
1213
                # print("extra length bits:", extraLength)
1214
                tlength += get4(0, extraLength)
1215
                # print("extra length:", tlength)
1216
                distanceCode = get_code(leaf)
1217
                # print("distance code:", distanceCode)
1218
                distance = CopyDistance[distanceCode]
1219
                # print("distance:", distance)
1220
                moreBits = ExtraDistanceBits[distanceCode >> 1]
1221
                # print("more bits:", moreBits)
1222
                # print("bits:", get_bits(leaf))
1223
                mored = get4(extraLength + get_bits(leaf), moreBits)
1224
                # print("mored:", mored)
1225
                distance += mored
1226
                # print("distance more:", distance)
1227
                adv(moreBits + extraLength + get_bits(leaf))
1228
                # print("offset:", do - distance)
1229
                # print("FAIL?: ", di, dio, do, b1, b2, b3, b4)
1230
                offset.next = do - distance
1231
                length.next = tlength
1232
                # cur_next.next = 0
1233
                cur_i.next = 0
1234 3 tomtor
                oraddr.next = do - distance
1235 2 tomtor
                state.next = d_state.COPY
1236
 
1237
            elif state == d_state.INFLATE:
1238
 
1239
                    if not filled:
1240
                        filled.next = True
1241
                    elif nb < 4:  # nb <= 2 or (nb == 3 and dio > 1):
1242
                        # print("EXTRA FETCH", nb, dio)
1243
                        pass  # fetch more bytes
1244 5 tomtor
                    elif di > isize:  # - 3:  # checksum is 4 bytes
1245 2 tomtor
                        state.next = d_state.IDLE
1246
                        o_done.next = True
1247
                        print("NO EOF ", di)
1248
                        raise Error("NO EOF!")
1249
                    elif code == EndOfBlock:
1250
                        print("EOF:", di, do)
1251
                        if not final:
1252
                            state.next = d_state.HEADER
1253
                        else:
1254
                            o_done.next = True
1255
                            o_oprogress.next = do
1256
                            state.next = d_state.IDLE
1257
                    else:
1258
                        if code < EndOfBlock:
1259
                            # print("B:", code, di, do)
1260
                            oaddr.next = do
1261
                            obyte.next = code
1262
                            o_oprogress.next = do + 1
1263
                            do.next = do + 1
1264
                            cur_next.next = 0
1265
                            state.next = d_state.NEXT
1266
                            # raise Error("DF!")
1267
                        elif code == InvalidToken:
1268
                            raise Error("invalid token")
1269
                        else:
1270
                            if static:
1271
                                token = code - 257
1272
                                # print("E:", token)
1273
                                tlength = CopyLength[token]
1274
                                # print("tlength", tlength)
1275
                                extraLength = ExtraLengthBits[token]
1276
                                # print("extralengthbits", extraLength)
1277
                                tlength += get4(0, extraLength)
1278
                                # print("tlength extra", tlength)
1279
                                t = get4(extraLength, 5)
1280
                                distanceCode = rev_bits(t, 5)
1281
                                # print("dcode", distanceCode)
1282
                                distance = CopyDistance[distanceCode]
1283
                                # print("distance", distance)
1284
                                moreBits = ExtraDistanceBits[distanceCode
1285
                                                                >> 1]
1286
                                distance += get4(extraLength + 5, moreBits)
1287
                                # print("distance2", distance)
1288
                                adv(extraLength + 5 + moreBits)
1289
                                # print("adv", extraLength + 5 + moreBits)
1290
                                offset.next = do - distance
1291
                                length.next = tlength
1292
                                cur_i.next = 0
1293 3 tomtor
                                oraddr.next = do - distance
1294 2 tomtor
                                state.next = d_state.COPY
1295
                            else:
1296
                                # raise Error("TO DO")
1297
                                state.next = d_state.D_NEXT
1298
                        cur_next.next = 0
1299
 
1300
            elif state == d_state.COPY:
1301
 
1302
                if not filled:
1303
                    filled.next = True
1304
                elif nb < 4:
1305
                    pass
1306
                elif method == 0:
1307
                    if cur_i < length:
1308
                        oaddr.next = do
1309
                        obyte.next = b3
1310
                        adv(8)
1311
                        cur_i.next = cur_i + 1
1312
                        do.next = do + 1
1313
                        o_oprogress.next = do + 1
1314
                    elif not final:
1315
                        state.next = d_state.HEADER
1316
                    else:
1317
                        o_oprogress.next = do # + 1
1318
                        o_done.next = True
1319
                        state.next = d_state.IDLE
1320 4 tomtor
                elif cur_i < length + 2:
1321 2 tomtor
                    oraddr.next = offset + cur_i
1322
                    if cur_i == 1:
1323
                        copy1.next = orbyte
1324 4 tomtor
                        # print("c1", cur_i, length, offset, do, orbyte)
1325
                    if cur_i == 3:
1326
                        copy2.next = orbyte
1327 2 tomtor
                    if cur_i > 1:
1328 4 tomtor
                        # Special 1 byte offset handling:
1329 2 tomtor
                        if offset + cur_i == do + 1:
1330
                            obyte.next = copy1
1331 4 tomtor
                        elif cur_i == 3 or offset + cur_i != do:
1332
                             obyte.next = orbyte
1333
                        # Special 2 byte offset handling:
1334
                        elif cur_i > 2:
1335
                            if cur_i & 1:
1336
                                obyte.next = copy2
1337
                            else:
1338
                                obyte.next = copy1
1339
                        else:  # cur_i == 2
1340
                            obyte.next = copy1
1341 2 tomtor
                        oaddr.next = do
1342
                        o_oprogress.next = do + 1
1343
                        do.next = do + 1
1344
                    cur_i.next = cur_i + 1
1345
                else:
1346
                    cur_next.next = 0
1347
                    state.next = d_state.NEXT
1348
 
1349
            else:
1350
 
1351
                print("unknown state?!")
1352
                state.next = d_state.IDLE
1353
 
1354 5 tomtor
    if FAST:
1355
        return io_logic, logic, fill_buf, oramwrite, oramread, matchers
1356
    else:
1357
        return io_logic, logic, fill_buf, oramwrite, oramread
1358 2 tomtor
 
1359
 
1360
if __name__ == "__main__":
1361
    d = deflate(Signal(intbv()[3:]), Signal(bool(0)),
1362
                Signal(intbv()[8:]), Signal(intbv()[LBSIZE:]),
1363
                Signal(intbv()[LBSIZE:]),
1364
                Signal(intbv()[8:]), Signal(intbv()[LBSIZE:]),
1365
                Signal(bool(0)), ResetSignal(1, 0, True))
1366
    d.convert()

powered by: WebSVN 2.1.0

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