|
| 1 | +; ----------------------------------------------------------------------------- |
| 2 | +; ZX0 decoder in x86 assembly. Translated from Z80 assembly. |
| 3 | +; 108 bytes |
| 4 | +; |
| 5 | +; 25/08/2022 |
| 6 | + |
| 7 | + bits 32 |
| 8 | + |
| 9 | + %ifndef BIN |
| 10 | + global dzx0_standard |
| 11 | + global _dzx0_standard |
| 12 | + %endif |
| 13 | + |
| 14 | +dzx0_standard: |
| 15 | +_dzx0_standard: |
| 16 | + pushad |
| 17 | + mov esi, [esp+32+4] ; inbuf |
| 18 | + mov edi, [esp+32+8] ; outbuf |
| 19 | + |
| 20 | + xor ecx, ecx |
| 21 | + mul ecx |
| 22 | + dec dx |
| 23 | + mov al, 0x80 |
| 24 | +dzx0s_literals: |
| 25 | + call dzx0s_elias ; obtain length |
| 26 | + rep movsb ; copy literals |
| 27 | + add al, al ; copy from last offset or new offset? |
| 28 | + jc dzx0s_new_offset |
| 29 | + call dzx0s_elias ; obtain length |
| 30 | +dzx0s_copy: |
| 31 | + push esi ; preserve offset |
| 32 | + movsx esi, dx |
| 33 | + add esi, edi ; calculate destination - offset |
| 34 | + rep movsb ; copy from offset |
| 35 | + pop esi ; restore offset |
| 36 | + add al, al ; copy from literals or new offset? |
| 37 | + jnc dzx0s_literals |
| 38 | +dzx0s_new_offset: |
| 39 | + mov cl, 0xfe ; prepare negative offset |
| 40 | + call dzx0s_elias_loop ; obtain offset MSB |
| 41 | + inc cl |
| 42 | + jz exit_depack ; check end marker |
| 43 | + mov dh, cl |
| 44 | + mov dl, [esi] ; obtain offset LSB |
| 45 | + inc esi |
| 46 | + rcr dx, 1 ; last offset bit becomes first length bit |
| 47 | + push 1 |
| 48 | + pop ecx ; obtain length |
| 49 | + jc dzx0s_skip |
| 50 | + call dzx0s_elias_backtrack |
| 51 | +dzx0s_skip: |
| 52 | + inc cx |
| 53 | + jmp dzx0s_copy |
| 54 | +dzx0s_elias: |
| 55 | + inc cl ; interlaced Elias gamma coding |
| 56 | +dzx0s_elias_loop: |
| 57 | + add al, al |
| 58 | + jnz dzx0s_elias_skip |
| 59 | + lodsb ; load another group of 8 bits |
| 60 | + adc al, al |
| 61 | +dzx0s_elias_skip: |
| 62 | + jnc dzx0s_elias_backtrack |
| 63 | + ret |
| 64 | +dzx0s_elias_backtrack: |
| 65 | + add al, al |
| 66 | + adc cx, cx |
| 67 | + jmp dzx0s_elias_loop |
| 68 | +exit_depack: |
| 69 | + sub edi, [esp+32+8] |
| 70 | + mov [esp+28], edi |
| 71 | + popad |
| 72 | + ret |
0 commit comments