KermMartian wrote:
Hey, all you did was use my code slightly permuted. Wink Nice job, calc84.
I was going to post something like that anyway, then you ninja'd me
Ugg. I forgot how absurdly limited the z80 instruction set is. All my C/x86 tricks are LESS efficient in z80.
elfprince13 wrote:
Ugg. I forgot how absurdly limited the z80 instruction set is. All my C/x86 tricks are LESS efficient in z80.


Bam!

Code:
unsigned char b = 0x55;
b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;


Of course, this is going to be fastest on a 64-bit CPU...

Regardless, if speed is what you want, a lookup table will easily win
On a similar note, write a routine that will take an 8-bit number and double each bit to get a 16-bit value. For example, 10110001 -> 1100111100000011. No look-up tables allowed, of course.

I have 34 bytes and 136 cycles (input in A, output in HL)

Edit2: Fixed counting error (again)
Kllrnohj wrote:
Bam!

Code:
unsigned char b = 0x55;
b = (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;


Of course, this is going to be fastest on a 64-bit CPU...

I've read hakmem and the stanford bit twiddling file too Wink

I'm posting this code to save it until I try it out. I have no idea how fast it will be (or even how many bytes it is, but I like its style Very Happy

The last 2 lines are optional, since calc84maniac didn't specify the routine had to be reusable. Without them I come to 37 bytes. Not sure how many CCs, but it'll vary depending on the number of 1s and 0s in the byte passed in a,

Code:

Init:
   ld hl,0
   ld b,a
   ld c,3
lstart:
   bit 0,b
   jr z,skadd
        ld a,l
   add a,c
        ld l,a
skadd:
   sla c
   sla c
   jr nz, hax
   ld c,h
   ld h,l
   ld l,c
   ld c,3
hax:
   ld a,(lstart+1)
   add a,00001000b
   ld (lstart+1),a
   cp %10000000
   jp nz,lstart

;; optional in case you want to run the code again.....
   ld a,%01000000
   ld (lstart+1),a


[edit]
fixed, one of my bit patterns was off. still not sure how many cc's.
136 clocks. 34 bytes. I win.
Edit: addition fail. 140 clocks 135 bytes.

Code:
   rrca; HABCDEFG   H      4   4
   rra;  HHABCDEF   G      4   8
   rra;  GHHABCDE   F      4   12
   rr l; GHHABCDE   ?   F   8   20
   sra a;GGHHABCD   E   F   8   28
   rl l; GGHHABCD   F   E   8   36
   ld h,a;         F   E   4   40
   rra;  FGGHHABC   D   E   4   44
   rr h; DGGHHABC*   D   E   8   52
   rr h; DDGGHHAB*   C   E   8   60
   rr h; CDDGGHHA*   B   E   8   68
   sra a;FFGGHHAB   C   E   8   76
   rr h; CCDDGGHH*   A   E   8   82
   rr l; FFGGHHAB   E      8   90
   rra;  EFFGGHHA   B      4   94
   rr h; BCCDDFFG*   G      8   102
   sra h;BBCCDDFF*   G      8   108
   sra a;EEFFGGHH   A      8   116
   rr h; ABBCCDDF*   F      8   124
   sra h;AABBCCDD         8   132
   ld l,a;EEFFGGHH A      4   136
Is that the whole thing? Why are you rr l'ing before you've assigned a value to l?
I rr l so I can get what was in carry later

My chart that I have beside it is What's in A, what's in Carry, and what did I rr l.
Shouldn't there be an "rrca" before the first line?
calc84, you are correct. I must have missed that on the copy paste. I'll fix that now.
Here's mine:

Code:
rrca
rra
rra
ld l,a
rra
sra l
rla
rr l
sra l
rra
rr l
sra l

rrca
rra
rra
ld h,a
rra
sra h
rla
rr h
sra h
rra
rr h
sra h
Hello!

It's not my turn to post, but I'm going to anyway 'cause I need a routine. Here is the task: create a routine that clears (white) a horizontal line on screen. BUT - not just any horizontal line, but a horizontal line that starts at h, l (x, y) and has a width of a.
So, if I were to, say, use this syntax:
ld h, 15
ld l, 20
ld a, 10
call ClearHorizLine
I would expect a horizontal line to be cleared at (15, 20) on screen that was 10 pixels long.

And....GO!
SirCmpwn wrote:
Hello!

It's not my turn to post, but I'm going to anyway 'cause I need a routine. Here is the task: create a routine that clears (white) a horizontal line on screen. BUT - not just any horizontal line, but a horizontal line that starts at h, l (x, y) and has a width of a.
So, if I were to, say, use this syntax:
ld h, 15
ld l, 20
ld a, 10
call ClearHorizLine
I would expect a horizontal line to be cleared at (15, 20) on screen that was 10 pixels long.

And....GO!


That isn't an algorithm challenge, its a "you're too lazy to just code it yourself" challenge, and those challenges suck.
Kllrnohj wrote:

That isn't an algorithm challenge, its a "you're too lazy to just code it yourself" challenge, and those challenges suck.


Bah! Be that as it may, these kind of routines are huge and deserve optimization.
My solution, 70 bytes (I know smaller is possible, but I'm going for speed as well as size)

Code:
ClearHorizLine:
   ;A = x pos
   ;L = y pos
   ;C = width
   
   ld h,0
   ld d,h
   ld e,l
   add hl,hl
   add hl,de
   add hl,hl
   add hl,hl
   ld b,a
   rra
   scf
   rra
   srl a
   ld d,$93
   ld e,a
   add hl,de
   ld a,b
   or $f8
   ld b,a
   add a,c
   ld c,a
   ld a,$ff
   jr nc,CHZloop4
CHZloop1:
   add a,a
   inc b
   jr nz,CHZloop1
   and (hl)
   ld (hl),a
   inc hl
   ld a,c
   sub 8
   jr c,CHZskip
CHZloop2:
   ld (hl),b
   inc hl
   sub 8
   jr nc,CHZloop2
CHZskip:
   ld b,a
   xor a
CHZloop3:
   scf
   rla
   inc b
   jr nz,CHZloop3
   and (hl)
   ld (hl),a
   ret
CHZloop4:
   add a,a
   inc b
   jr nz,CHZloop4
   ld b,a
CHZloop5:
   scf
   rla
   inc c
   jr nz,CHZloop5
   or b
   and (hl)
   ld (hl),a
   ret
It's not as good as calcs, but it's what I've got

Code:
;l = y
;a = x
;b = length
line:
   ld e,l
   ld h,00h
   ld d,h
   add hl,de
   add hl,de
   add hl,hl
   add hl,hl;y offset * 12
   ld e,a
   and 07h; bit offset within byte
   ld c,a
   srl e
   srl e
   srl e
   add hl,de; byte number in which to start
   ld de,gbuf
   add hl,de; address of first byte of line
   ld a,b
   cp 8
   jp c,SmallLine
lineleftcap:
   ld d,0FFh
   ld a,c
   sub b
   neg
   ld b,a;length-offset=number of pixels left to draw
   ld a,c
   or a;if it's right on the byte, no need for cap
   jp z,lineloopprep
lineleftcaploop:
   srl d
   dec a
   jp nz,lineleftcaploop
lineleftcapskip1:
   ld a,(hl)
   xor d
   ld (hl),a
   inc hl
lineloopprep:
   ld a,b
   and 07h
   ld c,a
   srl b
   srl b
   srl b;b is number of bytes now
   ld a,b
   or a
   jp z,linerightcap
lineloop:
   ld a,(hl)
   xor 0FFh
   ld (hl),a
   inc hl
   djnz lineloop
linerightcap:
   ld a,c
   or a
   ret z
   ld d,80h
   dec a
   jp z,linerightcaploopskip
linerightcaploop:
   sra d
   dec a
   jp nz,linerightcaploop
linerightcaploopskip:
   ld a,(hl)
   xor d
   ld (hl),a
   ret
   
SmallLine:
   ld d,80h
   dec b
   jp z,SmallLineNext
SmallLineLoop:
   sra d
   djnz SmallLineLoop
   ld e,00h
   ld a,c
   or a
   jp z,SmallLineSkip1
SmallLineLoop2:
   srl d
   rr e
   dec a
   jp nz,SmallLineLoop2
SmallLineSkip1:
   ld a,(hl)
   xor d
   ld (hl),a
   inc hl
   ld a,(hl)
   xor e
   ld (hl),a
   ret
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement