starting with elias-gamma

The place for codemasters or beginners to talk about programming any language for the Spectrum.
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

starting with elias-gamma

Post by C.Born »

https://en.wikipedia.org/wiki/Elias_gamma_coding

Code: Select all

  10 DEF FN b$(d)=VAL$ ((CHR$ 193+"d" AND d<=1)+(("FN b$(INT (d/2))+"+CHR$ 193+STR$ ((d/2)<>INT (d/2))) AND d>1)): DEF FN b(a$)=VAL (CHR$ 196+a$)
  20 FOR f=0 TO 255
  25 LET a$=FN b$(f)
  30 PRINT TAB 3-LEN STR$ f;f;"  ";a$;TAB 14;
  35 PRINT "0000000000"( TO LEN a$-1)+"1"+a$(2 TO )
  50 NEXT f
now its "encoded" and bigger then it was.

in assembler its a bit trickier, no slicing possible and it has to be on the bit precise
let see what i can make
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

well
i made a crash by overwriting BASIC, but how and why ???

Code: Select all


; use
; eliasg021
; pasmo -d --tap --name eliasg021 eliasg021.asm eliasg021.tap eliasg021.symbol

; cat elias.tap eliasg021.tap > eliasgamma021.tap

; bas2tap -selias -a10 eliasMCloader.bas elias.tap
;  10 DEF FN b$(d)=VAL$ ((CHR$ 193+"d" AND d<=1)+(("FN b$(INT (d/2))+"+CHR$ 193+STR$ ((d/2)<>INT (d/2))) AND d>1)): DEF FN b(a$)=VAL (CHR$ 196+a$): DEF FN a(a)=PEEK a+256*PEEK (a+1)
;  20 CLEAR 39999: LET rt=FN a(23730)+1
;  30 LOAD ""CODE rt
;  40 RANDOMIZE USR rt
;  50 FOR f=rt+493 TO rt+493+50 STEP 2
;  60 PRINT f;",";PEEK f,FN b$(FN a(f)): NEXT f


; elias-gamma uses infact BOTH big-endian AND small endian in 1 answer
; eg  0x49 = %01001001
;             76543210
;              ^
;            %0000001  +  001001
;             0123456 ... 543210  




; elias_gamma ENCoding

; https://en.wikipedia.org/wiki/Elias_gamma_coding


setchan         equ 0x1601   ; ROM openchannel routine

here equ 40000
           org here

           di
           ld de,source
           ld hl,encoding
           ld bc,endsource-source  ; length
do1        call test
           ei
           ret
           

           ld hl,encoding
           ld de,the-encoding  ; length
do2        call print
           ei
           ret



;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

test       push bc
           call elias
           pop bc

marker     inc hl
           ld (hl),255     ; temp end marker between encoded results
           inc hl
           ld (hl),255

           inc de   ; next 8 bit value source
           inc hl   ; next encode field

           dec bc
           ld a,b
           or c
           ret z
           jr test


;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
elias      ld a,(de)
           ld (hl),128

           ld b,8
count0     rrc (hl)
           sla a
           jr c,remains
           djnz count0     ; 0's counted, 2^ marker set

remains    ex af,af'
            ld a,8
            sub b

           exx
              ld hl,remain7
              and a
              jr z,setret

              ld b,a
              ld de,remain6-remain7 ; size of code block to jump over
down          add hl,de
              djnz down

setret        push hl         ; jump adres
            ex af,af'
           exx
           ret             ; is a JUMP from STACK


remain7    sla a           ;  c< xxxxxxx0
           jr nc,remain7b
           set 7,(hl)      ; 
           jr remain6
remain7b   res 7,(hl)

remain6    sla a           ;  c< xxxxxx00
           jr nc,remain6b
           set 6,(hl)      ; 
           jr remain5
remain6b   res 6,(hl)

remain5    sla a           ;  c< xxxxx000
           jr nc,remain5b
           set 5,(hl)      ; 
           jr remain4
remain5b   res 5,(hl)

remain4    sla a           ;  c< xxxx0000
           jr nc,remain4b
           set 4,(hl)      ; 
           jr remain3
remain4b   res 4,(hl)

remain3    sla a           ;  c< xxx00000
           jr nc,remain3b
           set 3,(hl)      ; 
           jr remain2
remain3b   res 3,(hl)

remain2    sla a           ;  c< xx000000
           jr nc,remain2b
           set 2,(hl)      ; 
           jr remain1
remain2b   res 2,(hl)

remain1    sla a           ;  c< x0000000
           jr nc,remain1b
           set 1,(hl)      ; 
           jr remain0
remain1b   res 1,(hl)

remain0    sla a           ;  c< 00000000
           jr nc,remain0b
           set 0,(hl)      ; 
           jr remained
remain0b   res 0,(hl)

remained   ;and a
           ;jr z,bdone
           inc hl          ; on bit0=1 encoding needs a next space (8 bit)
           ld (hl),a       ; last bits dumped on new bit7 and lower
                           ; forcing a constant 16 bit space
bdone      ret

;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_



; print 0 or 1  
print      ld a,2
           push hl
           call setchan
           pop hl

           ld a,13
           rst 16

ploop      ld c,(hl)

           ld b,8
bits       ld a,"0"
           sla c
           jr nc, prrr
           inc a

prrr       rst 16
           djnz bits

           inc hl

           bit 0,l
           jr z,countdown
           ld a,13
           rst 16

countdown  dec de
           ld a,e
           or d
           jr nz,ploop
           ret

space      equ $
spa        equ space/256
sp1        equ spa+1
sp0        equ sp1*256
space2     equ sp0-space

           defs space2,0



;---------------------------------------------------------

source
           defb 0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20,21,22,33,44,55,66,77,88,99,111,123,234,240,250,251,252,253,254
endsource  equ $

           defw 65535,65535,65535

encoding
           defs 256,0  ; reserved space

the        END here
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

NO
IT aint working, yet
but its at least working a bit, i had one stupid error , forgot exx hl
BUT my basic part has severall QUOTE signs and inside and Token will be written in txt, with bas2tap,zmakebas AND txt2emu, so i quited that for now
but i have to make it visual in memory so i will try different Next attempt
so its not working, but if you have a suggestion, you are welcome

Code: Select all

; elias_gamma ENCoding
; https://en.wikipedia.org/wiki/Elias_gamma_coding
;
; elias-gamma uses infact BOTH big-endian AND little-endian in 1 answer
; eg  0x49 = %01001001
;             76543210
;      >       ^           < points to 2^6
;      %000000 1 001001    13 bits
;       012345 6 543210


;  NOT WORKING YET !!!!
; but it has a "feel", perhaps
; it will look better if the printing is done per gamma-set
; and it will show the error IF i dont build in 100 more

; eliasg_025c
; pasmo -d --tapbas --name eliasg_025c eliasg_025c.asm eliasg_025c.tap eliasg_025c.symbol

setchan         equ 0x1601   ; ROM openchannel routine


here       equ 40000

           org here

           exx
           push hl

           ld de,source
           ld hl,encoding
           ld bc,endsource-source  ; length

do1        call list

           ld hl,encoding
           ld de,the-encoding  ; length
do2        call print

           pop hl
           exx
           ld bc,encoding
           ret


;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

list       push bc
           call elias
           pop bc

marker     inc hl
           ld (hl),255     ; temp end marker between encoded results
           inc hl
           ld (hl),255

           inc de   ; next 8 bit value source
           inc hl   ; next encode field 1 to 15 bits on 16bit

           dec bc
           ld a,b
           or c
           ret z
           jr list


;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
; rewrite off res/set b,(hl) needs pretty much double calculation in exx again
; SET and RES have -0x30 difference in code value
; set 7,(hl)  CB FE
; res 7,(hl)  CB BE
; set 6,(hl)  CB F6
; res 6,(hl)  CB B6
; set 5,(hl)  CB EE
; res 5,(hl)  CB AE
; set 4,(hl)  CB E6
; res 4,(hl)  CB A6
; set 3,(hl)  CB DE
; res 3,(hl)  CB 9E
; set 2,(hl)  CB D6
; res 2,(hl)  CB 96
; set 1,(hl)  CB CE
; res 1,(hl)  CB 8E
; set 0,(hl)  CB C6
; res 0,(hl)  CB 86


; "big endian" just as mind set
elias      ld a,(de)
           ld (hl),128      ; gamma 1st %10000000

           and a
           jr z,remained    ; elias=0 > 2^0=1, ready and jump


           ld b,8
count0     rrc (hl)         ; gamma 8th %00000001
           sla a            ; c<7-6543210-<0
           jr c,remains
           djnz count0

; 0's counted
; 2^ markbit set


; "little endian" and count back/further from 2^markbit
remains    ex af,af'
            ld a,8
            sub b                   ; b=8 a=0
                                    ; b=0 a=8
           exx                      ;   remain7 to remain6 is 0x0a difference in adress
              ld hl,remain7
              and a
              jr z,setret

              ld b,a                ; b>=1
              ld de,remain6-remain7 ; size of code block to jump over
down          add hl,de
              djnz down

setret        push hl               ; jump adres
           ex af,af'
           exx
           ret                      ; is a JUMP from STACK

remain7    sla a                    ;  c< xxxxxxx0
           jr nc,remain7b
           set 7,(hl)               ;
           jr remain6
remain7b   res 7,(hl)

remain6    sla a                    ;  c< xxxxxx00
           jr nc,remain6b
           set 6,(hl)               ;
           jr remain5
remain6b   res 6,(hl)

remain5    sla a                    ;  c< xxxxx000
           jr nc,remain5b
           set 5,(hl)               ;
           jr remain4
remain5b   res 5,(hl)

remain4    sla a                    ;  c< xxxx0000
           jr nc,remain4b
           set 4,(hl)
           jr remain3
remain4b   res 4,(hl)

remain3    sla a                    ;  c< xxx00000
           jr nc,remain3b
           set 3,(hl) 
           jr remain2
remain3b   res 3,(hl)

remain2    sla a                    ;  c< xx000000
           jr nc,remain2b
           set 2,(hl)
           jr remain1
remain2b   res 2,(hl)

remain1    sla a                    ;  c< x0000000
           jr nc,remain1b
           set 1,(hl) 
           jr remain0
remain1b   res 1,(hl)

remain0    sla a                    ;  c< 00000000
           jr nc,remain0b
           set 0,(hl)
           jr remained
remain0b   res 0,(hl)


remained   inc hl                   ; on bit0=1 encoding needs a next space (8 bit)
           ld (hl),a                ; last bits dumped on new bit7 and lower
                                    ; forcing a constant 16 bit space
           ret

;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_



; print 0 or 1  
print      ld a,2
           push hl
           push de
           call setchan
           pop de
           pop hl

           ld a,13
           rst 16

ploop      ld c,(hl)

           ld b,8
bits       ld a,48           ;"0"
           sla c             ;   c=7<6543210<0
           jr nc,pr01
           inc a             ;"1"
pr01       rst 16            ; print
           djnz bits         ; do 8 bits
           bit 0,l           ; hl = 256 aligned, l=0 at start
           jr z,nextelias    ;
           ld a,13           ; set CR
           rst 16            ; print on next line after 16bit
nextelias  inc hl            ; next byte

countdown  dec de
           ld a,e
           or d
           jr nz,ploop
           ret





;---------------------------------------------------------

source
           defb 0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20,21,22,33,44,55,66,77,88,99,111,123,234,240,250,251,252,253,254
endsource  equ $
sourcelen equ endsource-source

           defw 65535,65535,65535
space      equ $
spa        equ space/256
sp1        equ spa+1
sp0        equ sp1*256
space2     equ sp0-space

           defs space2,0        ; empty block upto 0x..00, must have an easier soltion

encoding                        ; the result, hopefully
           defs sourcelen*4 ,0  ; reserved 0 space

the        END here
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

So, there is NO elias-gamma NULL , took me some time to realise that one ...
:D :P :shock: :mrgreen: :roll: :evil: :?: :!:

now its 98,7 % or some
but it should write the encoding correct in mem, which is actualy just a shift with 8 bytes inside 16 bytes, so why dont i do that?
since i wanted to see it and print it ??
well, that seems to work now, although 1toooooooo much ...

Code: Select all

; elias_gamma ENCoding
; https://en.wikipedia.org/wiki/Elias_gamma_coding
;
; elias-gamma uses infact BOTH big-endian AND little-endian in 1 answer
; eg  0x49 = %01001001
;             76543210
;      >       ^           < points to 2^6
;      %000000 1 001001    13 bits
;       012345 6 543210

; there is NO elias-gamma NUL ??
;Number 	Binary 	γ encoding
;0 =     + 0    0       0          ; NOT CALCULATED
;1 = 2^0 + 0 	1 	1
;2 = 2^1 + 0 	10 	010

; almost working !

; eliasg_036
; pasmo -d --tapbas --name elias_036 eliasg_036.asm eliasg_036.tap eliasg_036.symbol


setchan         equ 0x1601   ; ROM openchannel routine

here       equ 40000

           org here

           exx
           push hl

           ld a,2
           call setchan            ; set SCREEN as print channel
           call printcr            ; set next line

           ld de,source
           ld hl,encoding
           ld bc,endsource-source  ; length

do1        call list

           ld hl,encoding
           ld de,the-encoding      ; length
do2        call print

           pop hl
           exx
           ld bc,encoding
           ret

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

list       push bc

           call inve_0

           call elias

           pop bc

           inc de   ; next 8 bit value source
           inc hl   ; next encode field 1 to 15 bits on 16bit

           dec bc
           ld a,b
           or c
           ret z
           jr list


;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
; rewrite off res/set b,(hl) needs pretty much double calculation in exx again
; SET and RES have -0x30 difference in code value
; set 7,(hl)  CB FE
; res 7,(hl)  CB BE
; set 6,(hl)  CB F6
; res 6,(hl)  CB B6
; set 5,(hl)  CB EE
; res 5,(hl)  CB AE
; set 4,(hl)  CB E6
; res 4,(hl)  CB A6
; set 3,(hl)  CB DE
; res 3,(hl)  CB 9E
; set 2,(hl)  CB D6
; res 2,(hl)  CB 96
; set 1,(hl)  CB CE
; res 1,(hl)  CB 8E
; set 0,(hl)  CB C6
; res 0,(hl)  CB 86




; the elias-gamma format  ; all PRINT routines should protect A by push AF
elias      ld a,(de)
           and a
              push af
              ld c,a
              call print8
              call print__
              pop af
              jr nz,el1
              call printcr

           ret z       ; if PRINT is excluded the ret Z is needed

el1        ld (hl),1           ; gamma 1st %10000000
m00        ld b,8
count0     rrc (hl)
           sla a               ; c<7-6543210-<0
           jr c,highest
           djnz count0

highest    ld c,(hl)                ; fetch "1"
m01        srl c
           call print01             ; print last bits as '0' prelead
           jr nc,m01

; printed and stored up to first gamma bit 2^b

           ld c,a
           ld a,b
           exx                      ;   remain7 to remain6 is 0x0a difference in adress
              ld hl,remain7

              ld b,a                ; b>=1
              ld a,8
              sub b
              ld b,a
              and a
              jr z,setret

              ld de,remain6-remain7 ; size of code block to jump over
down          add hl,de
              djnz down

setret        push hl               ; jump adres

           exx
           ret                      ; (pop hl) is a JUMP from STACK

remain7    sla c                    ;  c< xxxxxxx0
           jr nc,remain7b
           set 7,(hl)               ;
           jr remain7c
remain7b   res 7,(hl)
remain7c   call print01


remain6    sla c                    ;  c< xxxxxx00
           jr nc,remain6b
           set 6,(hl)               ;
           jr remain6c
remain6b   res 6,(hl)
remain6c   call print01


remain5    sla c                    ;  c< xxxxx000
           jr nc,remain5b
           set 5,(hl)               ;
           jr remain5c
remain5b   res 5,(hl)
remain5c   call print01


remain4    sla c                    ;  c< xxxx0000
           jr nc,remain4b
           set 4,(hl)
           jr remain4c
remain4b   res 4,(hl)
remain4c   call print01


remain3    sla c                    ;  c< xxx00000
           jr nc,remain3b
           set 3,(hl) 
           jr remain3c
remain3b   res 3,(hl)
remain3c   call print01


remain2    sla c                    ;  c< xx000000
           jr nc,remain2b
           set 2,(hl)
           jr remain2c
remain2b   res 2,(hl)
remain2c   call print01


remain1    sla c                    ;  c< x0000000
           jr nc,remain1b
           set 1,(hl)
           jr remain1c
remain1b   res 1,(hl)
remain1c   call print01


remain0    sla c                    ;  c< 00000000
           jr nc,remain0b
           set 0,(hl)
           jr remain0c
remain0b   res 0,(hl)
remain0c   call print01


remained           
           inc hl                   ; on bit0=1 encoding needs a next space (8 bit)
           ld (hl),c                ; last bits dumped on new bit7 and lower
                                    ; forcing a constant 16 bit space
           call print8
           call printcr
           ret


;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_

; print full byte
print8     push bc
           ld b,8
bits       sla c             ;   c=7<6543210<0
           call print01
           djnz bits         ; do 8 bits
           pop bc
           ret

INVE_1     push af
           ld a,20                  ; INVERSE
           rst 16
           ld a,1                   ; 1 = ON
           rst 16
           pop af
           ret

inve_0     push af
           ld a,20                  ; INVERSE
           rst 16
           ld a,0                   ; 0 = off
           rst 16
           pop af
           ret

prnotnul   push af
           ld a,"."
           rst 16
           pop af
           ret

print__    push af
           ld a,">"
           rst 16
           ld a," "
           rst 16
           pop af
           ret

prdot      push af
           ld a,"."
           rst 16
           pop af
           ret

printcr    push af
           ld a,13           ; set CR
pr6        rst 16            ; print on next line after 16bit
           pop af
           ret

; print 0 or 1
print01    push af
           ld a,48
           jr nc,pr01
           inc a             ;"1"
pr01       rst 16            ; print
           pop af
           ret


print      ld c,(hl)
           call print8
           bit 0,l           ; hl = 256 aligned, l=0 at start
           jr z,nextelias    ;
           ld a,13           ; set CR
           rst 16            ; print on next line after 16bit
nextelias  inc hl            ; next byte

countdown  dec de
           ld a,e
           or d
           jr nz,print
           ret





;---------------------------------------------------------

source
           defb 0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20,21,22,33,44,55,66,77,88,99,111,123,234,240,250,251,252,253,254
endsource  equ $
sourcelen equ endsource-source

           defw 65535,65535,65535
space      equ $
spa        equ space/256
sp1        equ spa+1
sp0        equ sp1*256
space2     equ sp0-space

           defs space2,0        ; empty block upto 0x..00, must have an easier soltion

encoding                        ; the result, hopefully
           defs sourcelen*2 ,0  ; reserved 0 space

the        END here
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

so, you only have to set
(hl),0
inc hl
(hl),elias

and virtualy you are done if you count that 2^b bit
i did not test this one but i want to know if its use-able for an zx0 ON Z80 machine

its stupid, i warn you
and in the C the elias is "optimised " directly after use.
its feels like b64, streching up things

Code: Select all

        ld de,source
        ld hl,destiny
        ld a,(de)

;        and a       half carry SET
;        jr hc,bit8     there is NO HALF CARRRY INSTRUCTION ......

        ld (hl),0
        inc hl
;bit8    
        ld (hl),a
        ld c,a       ; 16bit %00000000xxxxxxxx
        dec hl

        ld b,8
b8      rla c
        jr c,bdone
        djnz b8
bdone   equ $
       ; RET

   ; on full djnz b=0 and NonCarry, else Carry set and b>=1

; its it realy only this?
; do you need it "left sided" or "right sided" now

        ld a,b
        and a
        ret z       ; bc holds last value's

      ld b,8
again
      ld d,(hl)
      inc hl
      ld e,(hl)
      dec hl      ; de=%00000000xxxxxxxx
      ld c,e      ;  c=%xxxxxxxx
      rla c
      jr c,cdone
      sla e       ; x<%xxxxxxxx0<0
      rrc d       ; 0>%x00000000
      djnz again  ;

cdone equ $
      ld (hl),d
      inc hl
      ld (hll),e
      ret   ; ??? 


and this one prints it as well
but what will be done with it?
i just started with it since it is use by the C, the INV is not needed but shows something is done, what ever ido.
the result is THE SAME as the first 4 lines of aboev
DEFB 0,elias
hmmmmmmm

Code: Select all


; elias_gamma ENCoding
; https://en.wikipedia.org/wiki/Elias_gamma_coding
;
; elias-gamma uses infact BOTH big-endian AND little-endian in 1 answer
; eg  0x49 = %01001001
;             76543210
;      >       ^           < points to 2^6
;      %000000 1 001001    13 bits
;       012345 6 543210

; there is NO elias-gamma NUL ??
;Number 	Binary 	γ encoding
;0 =     + 0    0       0          ; NOT CALCULATED
;1 = 2^0 + 0 	1 	1
;2 = 2^1 + 0 	10 	010

; almost working !

; eliasg_037b
; pasmo -d --tapbas --name elias_037b eliasg_037b.asm eliasg_037b.tap eliasg_037b.symbol


setchan    equ 0x1601                 ; ROM openchannel routine

here       equ 40000

           org here

           exx
           push hl

           ld a,2
           call setchan               ; set SCREEN as print channel
           call printcr               ; set next line

           ld de,source
           ld hl,encoding
           ld bc,endsource-source     ; length

do1        call list

           ld hl,encoding
           ld de,endencoding-encoding ; length
do2        call print

           pop hl
           exx
           ld bc,encoding
           ret

;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

list       push bc

           call inve_0

           call elias

           pop bc

           inc de   ; next 8 bit value source
           inc hl   ; next encode field 1 to 15 bits on 16bit

           dec bc
           ld a,b
           or c
           ret z
           jr list


;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
; rewrite off res/set b,(hl) needs pretty much double calculation in exx again
; SET and RES have -0x30 difference in code value
; set 7,(hl)  CB FE
; res 7,(hl)  CB BE
; set 6,(hl)  CB F6
; res 6,(hl)  CB B6
; set 5,(hl)  CB EE
; res 5,(hl)  CB AE
; set 4,(hl)  CB E6
; res 4,(hl)  CB A6
; set 3,(hl)  CB DE
; res 3,(hl)  CB 9E
; set 2,(hl)  CB D6
; res 2,(hl)  CB 96
; set 1,(hl)  CB CE
; res 1,(hl)  CB 8E
; set 0,(hl)  CB C6
; res 0,(hl)  CB 86




; the elias-gamma format  ; all PRINT routines should protect A by push AF
elias      ld a,(de)
           and a
              push af
              ld c,a
              call print8
              call print__
              pop af
              jr nz,el1
              call printcr

           ret z       ; if PRINT is excluded the ret Z is needed

el1        ld (hl),1           ; gamma 1st %10000000
m00        ld b,8
count0     rrc (hl)
           sla a               ; c<7-6543210-<0
           jr c,highest
           djnz count0

highest    ld c,(hl)                ; fetch "1"
m01        srl c
           call print01             ; print last bits as '0' prelead
           jr nc,m01

; printed and stored up to first gamma bit 2^b

           ld c,a
           ld a,b
           exx                      ;   remain7 to remain6 is 0x0a difference in adress
              ld hl,remain6

              ld b,a                ; b>=1
              ld a,8
              sub b
              ld b,a
              and a
              jr z,setret

              ld de,remain5-remain6 ; size of code block to jump over
down          add hl,de
              djnz down

setret        push hl               ; jump adres

           exx
           ret                      ; (pop hl) is a JUMP from STACK

; at least 1 of 8 bits is set, leaving 6 to 0 to fill eventualy
;remain7    sla c                    ;  c< xxxxxxx0
;           jr nc,remain7b
;           set 7,(hl)               ;
;           jr remain7c
;remain7b   res 7,(hl)
;remain7c      call print01

remain6    sla c                    ;  c< xxxxxx00
           jr nc,remain6b
           set 6,(hl)               ;
           jr remain6c
remain6b   res 6,(hl)
remain6c      call print01


remain5    sla c                    ;  c< xxxxx000
           jr nc,remain5b
           set 5,(hl)               ;
           jr remain5c
remain5b   res 5,(hl)
remain5c      call print01


remain4    sla c                    ;  c< xxxx0000
           jr nc,remain4b
           set 4,(hl)
           jr remain4c
remain4b   res 4,(hl)
remain4c      call print01


remain3    sla c                    ;  c< xxx00000
           jr nc,remain3b
           set 3,(hl) 
           jr remain3c
remain3b   res 3,(hl)
remain3c      call print01


remain2    sla c                    ;  c< xx000000
           jr nc,remain2b
           set 2,(hl)
           jr remain2c
remain2b   res 2,(hl)
remain2c      call print01


remain1    sla c                    ;  c< x0000000
           jr nc,remain1b
           set 1,(hl)
           jr remain1c
remain1b   res 1,(hl)
remain1c      call print01


remain0    sla c                    ;  c< 00000000
           jr nc,remain0b
           set 0,(hl)
           jr remain0c
remain0b   res 0,(hl)
remain0c      call print01


remained      call inve_1        
           inc hl                   ; on bit0=1 encoding needs a next space (8 bit)
           ld (hl),c                ; last bits dumped on new bit7 and lower
                                    ; forcing a constant 16 bit space
              call print8

           call printcr
           ret


;+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_

; print full byte
print8     push bc
           ld b,8
bits       sla c             ;   c=7<6543210<0
           call print01
           djnz bits         ; do 8 bits
           pop bc
           ret

inve_1     push af
           ld a,20                  ; INVERSE
           rst 16
           ld a,1                   ; 1 = ON
           rst 16
           pop af
           ret

inve_0     push af
           ld a,20                  ; INVERSE
           rst 16
           ld a,0                   ; 0 = off
           rst 16
           pop af
           ret

print__    push af
           ld a,">"
           rst 16
           ld a," "
           rst 16
           pop af
           ret

prdot      push af
           ld a,"."
           rst 16
           pop af
           ret

printcr    push af
           ld a,13           ; set CR
pr6        rst 16            ; print on next line after 16bit
           pop af
           ret

; print 0 or 1
print01    push af
           ld a,48
           jr nc,pr01
           inc a             ;"1"
pr01       rst 16            ; print
           pop af
           ret

print      ld c,(hl)
           call print8
           bit 0,l           ; hl = 256 aligned, l=0 at start
           jr z,nextelias    ;
           call printcr

nextelias  inc hl            ; next byte

countdown  dec de
           ld a,e
           or d
           jr nz,print
           ret

;---------------------------------------------------------

source
           defb 0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,19,20,21,22,33,44,55,66,77,88,99,111,123,234,240,250,251,252,253,254
endsource  equ $
sourcelen equ endsource-source

           defw 65535,65535,65535
space      equ $
spa        equ space/256
sp1        equ spa+1
sp0        equ sp1*256
space2     equ sp0-space

           defs space2,0        ; empty block upto 0x..00, must have an easier solution

encoding                        ; the result, hopefully
           defs sourcelen*2 ,0  ; reserved 0 space
endencoding equ $

the        END here

C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

thinking out loud:

elias-gamma (or elias-gamma-string) is a bitstring of ANY length and ANY amount off numbers
since its unkown what length next number will have its max number is SET by a series of zero's that realy have to be counted
that is the gamma point '1'
from and including there you take THAT amount of bits from the string
with 8bits numbers this will change between 1 bit and 15 bits per number
11111 is 5 bytes with value 1 within 5 BITS
010010010 is 3 bytes with value 2 with in 9 BITS
000000011111111 is 1 byte with value 255 within 15 BITS

setting a zero by it self, dont know

so, an elias gamma probably comes in flocks of bits :dance
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

a 16bit basic example

Code: Select all

   3 DEF FN b$(d)=VAL$ ((CHR$ 193+"d" AND d<=1)+(("FN b$(INT (d/2))+"+CHR$ 193+STR$ ((d/2)<>INT (d/2))) AND d>1))
   5 DEF FN e$(a)="000000000000000"( TO LEN FN b$(a)-1)+FN b$(a)
   7 DEF FN b(a$)=VAL (CHR$ 196+a$)
  10 DEF FN a(a)=PEEK a+256*PEEK (a+1): DEF FN r()=FN a(23730)
  20 FOR f=0 TO 65535
  21 POKE 23692,-1: IF INKEY$<>"" THEN  LET f=65535
  25 LET a$=FN b$(f)
  30 PRINT TAB 5-LEN STR$ f;f;"  ";a$,
  35 PRINT "0000000000000000"( TO LEN a$-1)+a$
  50 NEXT f
 100 LET g$="": LET g=LEN g$
 105 INPUT "16bit number : ";n: IF n<1 OR n>65535 THEN  GO TO 200
 110 LET g$=g$+FN e$(n)
 120 CLS : PRINT FN e$(n): GO TO 105
 200 CLS : POKE 23692,-1: PRINT LEN g$'"Bits in the string"'g$

1000 SAVE d1"elias16b" LINE 1
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

including "unwrapping"

give some 16bit values and CLOSE with 0 or bigger then 65535

then it SHOULD give back the correct nummbers
just a test like others :roll:

Code: Select all

   3 DEF FN b$(d)=VAL$ ((CHR$ 193+"d" AND d<=1)+(("FN b$(INT (d/2))+"+CHR$ 193+STR$ ((d/2)<>INT (d/2))) AND d>1))
   5 DEF FN e$(a)="000000000000000"( TO LEN FN b$(a)-1)+FN b$(a)
   7 DEF FN b(a$)=VAL (CHR$ 196+a$)
  10 DEF FN a(a)=PEEK a+256*PEEK (a+1): DEF FN r()=FN a(23730)
  20 FOR f=0 TO 65535
  21 POKE 23692,-1: IF INKEY$<>"" THEN  LET f=65535
  25 LET a$=FN b$(f)
  30 PRINT TAB 5-LEN STR$ f;f;"  ";a$,
  35 PRINT "0000000000000000"( TO LEN a$-1)+a$
  50 NEXT f
 100 LET g$="": LET g=LEN g$
 105 INPUT "16bit number : ";n: IF n<1 OR n>65535 THEN  GO TO 200
 110 LET g$=g$+FN e$(n)
 120 CLS : PRINT FN e$(n): GO TO 105
 200 CLS : POKE 23692,-1: PRINT LEN g$'"Bits in the string"'g$
 300 LET i=0: LET e=0: CLS 
 305 LET g=VAL g$(e+1): LET e=e+NOT g: GO TO 305+g
 306 INVERSE i: LET i=NOT i: PRINT g,e
 310 LET a$=g$(e+1 TO e*2+1): PRINT a$'FN b(a$)
 315 LET g$=g$(e*2+2 TO ): LET e=0
 320 IF g$<>"" THEN  GO TO 305
 400 STOP 
1000 CLEAR : SAVE d1"elias16b" LINE 1

C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

unwrapping while saving the elias-gamma-bitstring
in basic only, but now i know what i should want
it looks as clumbsy as it is ...

Code: Select all

   3 DEF FN b$(d)=VAL$ ((CHR$ 193+"d" AND d<=1)+(("FN b$(INT (d/2))+"+CHR$ 193+STR$ ((d/2)<>INT (d/2))) AND d>1))
   5 DEF FN e$(a)="000000000000000"( TO LEN FN b$(a)-1)+FN b$(a)
   7 DEF FN b(a$)=VAL (CHR$ 196+a$)
  10 DEF FN a(a)=PEEK a+256*PEEK (a+1): DEF FN r()=FN a(23730)
  20 FOR f=0 TO 65535
  21 POKE 23692,-1: IF INKEY$<>"" THEN  LET f=65535
  25 LET a$=FN b$(f)
  30 PRINT TAB 5-LEN STR$ f;f;"  ";a$,FN e$(f)
  50 NEXT f
 100 LET g$="": LET g=LEN g$
 105 INPUT "16bit number : ";n: IF n<1 OR n>65535 THEN  GO TO 200
 110 LET g$=g$+FN e$(n)
 120 CLS : PRINT FN e$(n): GO TO 105
 200 CLS : POKE 23692,-1: PRINT LEN g$'"Bits in the string"'g$
 300 LET b=0: LET i=0: LET e=0: CLS 
 305 LET g=VAL g$(b+e+1): LET e=e+NOT g: GO TO 305+g
 306 INVERSE i: LET i=NOT i: PRINT g;", ";e;", ";b;", ";LEN g$'g$
 310 LET e=e+1: LET a$=g$(b+e TO b+e*2-1): PRINT b+e;", ";b+e*2-1;" ,";a$'FN b(a$)
 312 PAUSE 0
 315 LET b=b+2*e-1: LET e=0
 320 IF b<LEN g$ THEN  GO TO 305
 400 STOP 
1000 CLEAR : SAVE d1"elias16b" LINE 1
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

ok
just for fun, some visuals

Code: Select all

   3 DEF FN b$(d)=VAL$ ((CHR$ 193+"d" AND d<=1)+(("FN b$(INT (d/2))+"+CHR$ 193+STR$ ((d/2)<>INT (d/2))) AND d>1))
   5 DEF FN e$(a)="000000000000000"( TO LEN FN b$(a)-1)+FN b$(a)
   7 DEF FN b(a$)=VAL (CHR$ 196+a$)
  10 DEF FN a(a)=PEEK a+256*PEEK (a+1): DEF FN r()=FN a(23621)
  20 FOR f=0 TO 65535
  21 POKE 23692,-1: IF INKEY$<>"" THEN  LET f=65535
  25 LET a$=FN b$(f)
  30 PRINT TAB 5-LEN STR$ f;f;"  ";a$,FN e$(f)
  50 NEXT f
 100 LET g$="": LET g=LEN g$
 105 INPUT "16bit number : ";n: IF n<1 OR n>65535 THEN  GO TO 200
 110 LET g$=g$+FN e$(n)
 120 CLS : PRINT FN e$(n): GO TO 105
 200 CLS : POKE 23692,-1: PRINT LEN g$'"Bits in the string"'g$: PAUSE 0
 300 LET b=0: LET e=0: CLS 
 305 LET g=VAL g$(b+e+1): LET e=e+NOT g: GO TO 305+g
 307 DIM x$(LEN g$+8): LET x$(b+1 TO b+2)=CHR$ 19+CHR$ 1: LET x$(b+3+e TO b+4+e)=CHR$ 20+CHR$ 1: LET x$(b+5+1+e*2 TO b+5+4+e*2)=CHR$ 20+CHR$ 0+CHR$ 19+CHR$ 0
 308 PRINT AT 0,0;g$; OVER 1;AT 0,0;x$'g;", ";e;", ";b;", ";LEN g$
 310 LET e=e+1: LET a$=g$(b+e TO b+e*2-1): PRINT b+e;", ";b+e*2-1;" ,";"BIN ";a$'FN b(a$)
 312 PAUSE 0
 315 LET b=b+2*e-1: LET e=0
 320 IF b<LEN g$ THEN  CLS : GO TO 305
 400 STOP 
1000 CLEAR : SAVE "el16b004" LINE 1
User avatar
Einar Saukas
Bugaboo
Posts: 3157
Joined: Wed Nov 15, 2017 2:48 pm

Re: starting with elias-gamma

Post by Einar Saukas »

Notice there's "classic" Elias gamma coding (that's explained in Wikipedia) and the more efficient "interlaced" Elias gamma coding.

There are also other variants such as Exponential-Golomb coding (see Wikipedia). It's identical to Elias gamma coding except starting at 0 instead of 1.

You will find a comparison of them in the first table of this link.

ZX0 uses "interlaced" Elias gamma coding starting at 1.
Last edited by Einar Saukas on Thu Apr 18, 2024 7:45 pm, edited 3 times in total.
User avatar
Einar Saukas
Bugaboo
Posts: 3157
Joined: Wed Nov 15, 2017 2:48 pm

Re: starting with elias-gamma

Post by Einar Saukas »

C.Born wrote: Tue Apr 16, 2024 10:44 pm i want to know if its use-able for an zx0 ON Z80 machine
In theory, the ZX0 compressor should work in a Z80 machine. However you probably won't be able to compress data blocks larger than 1K and, even so, it will probably take a VERY long time to run.

I don't think that's a good idea...
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

Einar Saukas wrote: Thu Apr 18, 2024 7:44 pm even so, it will probably take a VERY long time to run.
I don't think that's a good idea...
waiting 5-6 times for a tapeloading error on a full 48k program like WSHAS
thats 30-45 minutes life time, waiting for a 5 ball tictac movie takes 24h rendering, for about 2 seconds zx cpu time.

ONCE zx0 is made on ZX it will save a lot of (reeeaaal) loading time from 13k to say 3-8 k
IF i use a (real) loading scheme that is a turbo type then the 3-9k will act like 1.5 to 4k or even faster...

i dont understand why people avoid the love of their life if they have to wait like in the old days ...
i am GLAD ZX CAN do a zx0 altough the momentary C takes about 20k memory and that accept for the commandline issue. starting as a DEF FN might close a mere 100-300 bytes? not much yet. can i make the elias i made with in about 50 bytes? maybe!!!
thats only the start, i know.
thanks for the links,
now i can compare and maybe change some starting point, since the elias-bitstring is only the start since the C line has an optimise inside the elias command, its only step numero uno... if i have THIS elias-gammabitstring in ASM .. thats next
cheers!!!
:idea:

ps

interlaced, thats your package ??
reading
User avatar
Einar Saukas
Bugaboo
Posts: 3157
Joined: Wed Nov 15, 2017 2:48 pm

Re: starting with elias-gamma

Post by Einar Saukas »

C.Born wrote: Thu Apr 18, 2024 11:05 pm ONCE zx0 is made on ZX it will save a lot of (reeeaaal) loading time from 13k to say 3-8 k
IF i use a (real) loading scheme that is a turbo type then the 3-9k will act like 1.5 to 4k or even faster...
These benefits (from using data that was compressed with ZX0) will be exactly the same, regardless if you originally compressed it using a Spectrum or PC.

But if you are having fun porting ZX0, go ahead :)
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

you miss the point of "origin" by passing by on zx via that pc

the origin of '0'
there was a roman salesman having all products all the time depending on the ship loads arriving
plenty off oil, seeds and wood and stone, but often lacking fruits

one day that arab guy came allong again and started pointing out all missing fruits with a fat circle on the empty space
asking, why dont you say "nothing" on that spot? you only have rotten apples and mollded grapes and thats realy all, so dont say you have any
the salesman replyed with the news about al storms and ships that got lost. there is nothing to write down if nothing comes in

yet that arab pointed out the number zero
an empty spot, with a circle around it
bravo!
now we know what we have if we see an empty circle
like a pot from above
an empty sack
an empty amphora!!

NOTHING IN IT
AT ALL!

:dance

cheers mate
c



ps
there is no "music" tread hre but i like to suggest "echo" from pinkflyod now, while hoping for code dropping in asming code
etc
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

sorry, realy love you people, but some times i dont get it

its like a

but you insist on a


tja
?
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

10 BORDER 4: CLS : POKE 16384,1
views as 00000001

this sets a bit the visual problem, in my mind i have to turn that picture in to a POKE 16384,128, but only visualy.
its logical to start with bit 0 as first bit, now i need a smart way to pin every bit on the correct number, on n=1 then the next free bit is bit 1 on byte 0
no smarties yet,just being lame with the next bit
probaly i have to change that remaintrap a little.
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

hmm
al least this one does not crash, but neither has a result.

i should look at the shiftt of a double register instead, but that has its own troubles

Code: Select all

; shift 8bit as 1-15bit within 24bit max
; elias
; a
; 63 =6bits = 111111 > 00000111111  = 11bits in DE
; last positin x= b
; xxxxxx00 00000000 00000000
; SHIFT elias djnz
; >>>>>>00 00011111 1.......
; OR    A         H        L
; xxxxxxXX XXXXXXXX X0000000

but i am close to "some thing that works"
it reverses some output
if someone sees it feel welcome to adjust :P

Code: Select all

; should be Elias, but now alias ...
; to be INCLUDEd as ASM

; #INCLUDE alias_0020.asm

; pasmo -d --bin alias_0020.asm alias_0020.bin alias_0020.symbol

; pasmo -d --tapbas --name alias_0020 alias_0020.asm alias_0020.tap alias_0020.symbol

; tape    equ  0



; an elias-gamma format Z80 routine 
; DE holds SOURCE, hl holds DESTINY


; IF tape       ; for TEST purpose only
here          equ 40000
destiny       equ 16384

              org here
              exx
              push hl             ; save sysvar
              ld de,source
              ld hl,destiny
;ENDIF


elias:        di                  ; EXX used, avoid ROM troubles
              ld a,(golomb)
              ld c,a              ; c=0 or 1

              ld a,(de)           ; elias source
              add a,c
              ret z               ; on elias = 0 or golomb=255 (255+1)

              ld b,8
              ld c,1              ; gamma 1st %00000001
count0        rrc c               ;  > c gamma 1st=%10000000
              sla a               ; c<7-6543210-<0  %xxxx0000
              jr c,gammafound
              djnz count0         ; will b=0 happen ??
gammafound    equ $               ; b=2^(b-1) value and c is gammabit position
                                  ; a holds remains after the gamma bit
                                  ; a= %xxxxxxx0 to %00000000
              ld (remains),a      ; every bit AFTER the gamma bit
              ld a,b
              ld (gammabit),a     ;   1 to 8 , position as value
              ld a,c              ;                         87654321
              ld (lastgamma),a    ;   position inside byte %0000000x

; set a bit at a time, just keep counting, start at BIT 0, BYTE 0
; for NOW this is the first step for the first bit of the first eliasgamma of the eliasgammabitstring 
; starting at 0bit,0byte

; BUT NEXT bit wont be at '0' but needs a calc

              ld  a,(bits)        ; besure the ONCE_PRESET=1 is correct
              exx
              ld b,a              ; fetch last bit POSITION

difference    equ testb1-testb0
              ld hl,testb0-difference   ; 1 step lower
              ld de,difference     

calcjump      add hl,de
              djnz calcjump    ; b=1 as minimum is first BIT0 to do
              push hl          ; infact jump adres
              exx

              ld a,(gammabit)  ;
              ld b,a           ; b= AMOUNT of bits
precalc       ld a,(bits)
              add a,b          ; never more then 8 added
              and %00000111    ; cut down 0-7
              inc a            ; working with 1-8
              ld (bits),a      ; rewrite precalc NEXT ELIAS(part) start bit position

              ld a,(lastgamma)
              ld c,a           ; c=position inside 
              ret ; = JUMP/POP from STACK


; the very    first bit will be FIXED here (0 or 1)
testb0        srl c                    ;  x0000000 > c
              jr nc,res_b0
              set 0,(hl)
              jr $+2
res_b0        res 0,(hl)
              djnz $+2
              jr gammadone
   
testb1        srl c                    ;  xx000000 > c
              jr nc,res_b1
              set 1,(hl)
              jr $+2
res_b1        res 1,(hl)
              djnz $+2
              jr gammadone

testb2        srl c                    ;  xxx00000 > c
              jr nc,res_b2
              set 2,(hl)
              jr $+2
res_b2        res 2,(hl)
              djnz $+2
              jr gammadone

testb3        srl c                    ;  xxxx0000 > c
              jr nc,res_b3
              set 3,(hl) 
              jr $+2
res_b3        res 3,(hl)
              djnz $+2
              jr gammadone

testb4        srl c                    ;  xxxxx000 > c
              jr nc,res_b4
              set 4,(hl)
              jr $+2
res_b4        res 4,(hl)
              djnz $+2
              jr gammadone

testb5        srl c                    ;  xxxxxx00 > c
              jr nc,res_b5
              set 5,(hl)               ;
              jr $+2
res_b5        res 5,(hl)
              djnz $+2
              jr gammadone

testb6        srl c                    ;  xxxxxxx0 > c
              jr nc,res_b6
              set 6,(hl)               ;
              jr $+2
res_b6        res 6,(hl)
              djnz $+2
              jr gammadone

testb7        srl c                    ;  xxxxxxxx > c
              jr nc,res_b7
              set 7,(hl)               ;
              jr $+2
res_b7        res 7,(hl)
              djnz $+2
              jr gammadone

; if NO carries occured yet  we started in the middle of a byte and have to raise the destiny with 1
; and restart FIXING bits from BIT0 again
           
              inc hl                   ; raise destiny adres 1 byte
              jr testb0               ; next serie of 8 bit POSITIONS

gammadone     ld a,(gammabit)     ; b=2^b value
              ld b,a
              dec b ; last bit is written already,b=7-0
              ret z ; nothing to do              

              ld  a,(bits)        ; besure the ONCE_PRESET=1 is correct
              exx
              ld b,a              ; fetch last bit POSITION

differenceA    equ testa1-testa0
              ld hl,testa0-differenceA   ; 1 step lower
              ld de,differenceA

calcjumpA     add hl,de
              djnz calcjumpA   ; b=1 as minimum is first BIT0 to do
              push hl          ; infact jump adres
              exx

              ld a,(gammabit)  ;
              ld b,a           ; b= AMOUNT of bits

precalcA      ld a,(bits)
              add a,b
              and %00000111
              inc a
              ld (bits),a      ; rewrite precalc NEXT ELIAS(part) start bit position

              ld a,(remains)
              ld c,a           ; purges remaining bits
              ret ; = JUMP/POP from STACK


testa0        sla c                    ;  c < xxxxxxx0
              jr nc,res_a0
              set 0,(hl)
              jr $+2
res_a0        res 0,(hl)
              djnz $+2
              jr remaindone
   
testa1        sla c                    ;  c < xxxxxx00
              jr nc,res_a1
              set 1,(hl)
              jr $+2
res_a1        res 1,(hl)
              djnz $+2
              jr remaindone

testa2        sla c                    ;  c < xxxxx000
              jr nc,res_a2
              set 2,(hl)
              jr $+2
res_a2        res 2,(hl)
              djnz $+2
              jr remaindone

testa3        sla c                    ;  c < xxxx0000
              jr nc,res_a3
              set 3,(hl) 
              jr $+2
res_a3        res 3,(hl)
              djnz $+2
              jr remaindone

testa4        sla c                    ;  c < xxx00000
              jr nc,res_a4
              set 4,(hl)
              jr $+2
res_a4        res 4,(hl)
              djnz $+2
              jr remaindone

testa5        sla c                    ;  c < xx000000
              jr nc,res_a5
              set 5,(hl)               ;
              jr $+2
res_a5        res 5,(hl)
              djnz $+2
              jr remaindone

testa6        sla c                    ;  c < x0000000
              jr nc,res_a6
              set 6,(hl)               ;
              jr $+2
res_a6        res 6,(hl)
              djnz $+2
              jr remaindone

testa7        sla c                    ;  c < 00000000
              jr nc,res_a7
              set 7,(hl)               ;
              jr $+2
res_a7        res 7,(hl)
              djnz $+2
              jr remaindone

; if NO carries occured yet  we started in the middle of a byte and have to raise the destiny with 1
; and restart FIXING bits from BIT0 again
           
              inc hl                   ; raise destiny adres 1 byte
              jr testa0                ; next serie of 8 bit POSITIONS


remaindone    pop hl
              exx
              ei                       ;
              ret                      ; is the whole value purged into a bitstring?
                                       ; hope so


lastgamma     defb 0              ; starting with bit 0

golomb        defb 0              ; use value elias0 on golomb=1 OR not on golomb=0

remains       defb 0

gammabit      defb 0              ; b = number

bits          defb 1              ; b=bits counted in the string from 1 to 8



                                  ;               01234567 89abcdef
source        defb %00100100      ;           >   00000100 100     
                                  ;               76543210 76543210
                                  ; on screen     00100000 00000100
                                  ; does not !!!!


 
;IF tape
the END here
;ENDIF

if i get this working i hopefully can change the SET and RES for
something like

ld a,(remains)
jr nc,wipebit
OR (hl) ; a=0000x000
ld (hl),a
jr further
wipebit:
CPL
AND (hl)
ld (hl),a ; a=xxxx0xxxx
CPL ; and back for next bit

further:
rotate a once more

but CPL only works with A reg ... :|
User avatar
arkannoyed
Manic Miner
Posts: 438
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: starting with elias-gamma

Post by arkannoyed »

You could change the bit according to a register holding the current bit. A method I used in Chess 2k19. The source is in the archive.
If I recall, it sets the bit, then resets it if required by xor-ing the register again. Obviously only possible using A
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

arkannoyed wrote: Sun Apr 21, 2024 4:49 pm You could change the bit according to a register holding the current bit. A method I used in Chess 2k19. The source is in the archive.
If I recall, it sets the bit, then resets it if required by xor-ing the register again. Obviously only possible using A
thank you, i have a look into the wonders of XOR, always set and then perhaps unset the bit, ok

meanwhile i see at least 1 bug

Code: Select all

              ld  a,(bits)        ; besure the ONCE_PRESET=1 is correct
              exx
              ld b,a              ; fetch last bit POSITION

difference    equ testb1-testb0
              ld hl,testb0-difference   ; 1 step lower
              ld de,difference     

calcjump      add hl,de
              djnz calcjump    ; b=1 as minimum is first BIT0 to do
              push hl          ; infact jump adres
              exx
i load B with 'bits' but use it as a value !!
what is 'bits', an POSITION or a VALUE ??

i have to stay alert a litle BIT more ,
why is that a pun?
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

Hi in 3d chess you use IXl and IXh
I was wondering if i should use IX as destiny instead off de,
for long shifts thats 2 extra registers.
i looked a bit in your source for bits and found
test_7:
ld e,l ;adjust IX to match HL
ld ixl,e ;

COULD tha be written as
test_7:
ld ixl,l ;adjust IX to match HL

i followed the use off e from that spot and it seems always overwritten after this use, but i dont know. just looking around, and i have to start using IXl etc a bit more my selfves
cheers
User avatar
ParadigmShifter
Manic Miner
Posts: 673
Joined: Sat Sep 09, 2023 4:55 am

Re: starting with elias-gamma

Post by ParadigmShifter »

You can't LD IXL, L since the option for index registers overrides use of HL in the instruction.
User avatar
arkannoyed
Manic Miner
Posts: 438
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: starting with elias-gamma

Post by arkannoyed »

So, I use the C register holding the current target bit, so 1, 2, 4, 8, 16, 32, 64, 128. With A holding the current byte that’s going to get added to.

Then the sequence goes like this;

Jr nc, jump over if the bit is a 0
Bit= 1:
Or c
Bit=0:
Xor c

This way, regardless of when the target bit in A is, it will be set correctly.

Then the bit mask in C is moved to the next with;

Rlc c
Jr nc, skip
Update A:
Ld (xx), a ; load a somewhere
Inc xx
Skip:
Continue….

Hope that helps, no need for all those set or res instructions.
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

ParadigmShifter wrote: Mon Apr 22, 2024 2:34 am You can't LD IXL, L since the option for index registers overrides use of HL in the instruction.
i found that, its excluding H and L becouse those codes are used already
0x6b is ld l,e which uses l already
0xDD6b does the same apperently, but different
http://z80.info/z80sean.txt

DD60 LD IXH,B*
DD61 LD IXH,C*
DD62 LD IXH,D*
DD63 LD IXH,E*
DD64 LD IXH,IXH*
DD65 LD IXH,IXL*
DD66 d LD H,(IX+d)
DD67 LD IXH,A*
DD68 LD IXL,B*
DD69 LD IXL,C*
DD6A LD IXL,D*
DD6B LD IXL,E*
DD6C LD IXL,IXH*
DD6D LD IXL,IXL*
DD6E d LD L,(IX+d)
DD6F LD IXL,A*
C.Born
Manic Miner
Posts: 239
Joined: Sat Dec 09, 2017 4:09 pm

Re: starting with elias-gamma

Post by C.Born »

arkannoyed wrote: Mon Apr 22, 2024 7:29 am So, I use the C register holding the current target bit, so 1, 2, 4, 8, 16, 32, 64, 128. With A holding the current byte that’s going to get added to.

Then the sequence goes like this;

Jr nc, jump over if the bit is a 0
Bit= 1:
Or c
Bit=0:
Xor c

This way, regardless of when the target bit in A is, it will be set correctly.

Then the bit mask in C is moved to the next with;

Rlc c
Jr nc, skip
Update A:
Ld (xx), a ; load a somewhere
Inc xx
Skip:
Continue….

Hope that helps, no need for all those set or res instructions.
yes i have to get rid of that setres ladder game
i started upside down probably
Post Reply