I know this is a pretty big request... but if anybody would be willing to help, that'd be pretty awesome.
I spent a while making vertical and horizontal line drawers. Please look over the code and tell me how clean/optimized they look. Thank you!! I want as much input as I can get.
Horizontal line drawer:
Code: vertical_line:
;h = xstart, l = ystart, a = length
;negate length if it is less than 0 and move xstart and ystart to the top of the line.
bit 7, a
jr z, _posLength
neg
;protect af so we can calculate the new ystart.
push af
;ystart = ystart-length = -(length-ystart) = -(a-l)
sub l
neg
inc a
ld l, a
pop af
;hl and length are corrected for negative length, carry on...
_posLength:
;calculate hl = (y0)*12
push af
push hl
ld h, 0
add hl, hl ;Because for each y coord we go down from y = 0, we are 12 bits further down, we will put y0*12 into hl
add hl, hl
ld d, h
ld e, l
add hl, hl
add hl, de
ld de, PlotSScreen
add hl, de ;bit to start writing at minus x coord now in hl.
pop de
push de
;now we have the x coord in d. to get the starting bit, we add d/8 to hl.
srl d
srl d
srl d
;store d/8 in de
ld e, d
ld d, 0
add hl, de
;now we have the starting bit in hl, we need to get xstart mod 8 into
;b so we can bitshift a an appropriate number of times. When we pop af,
;we get xstart in a, then all we have to do is and %07 to calculate
;xstart mod 7 (which will be in a) and then we store it in b.
pop af
and %00000111
ld b, a
ld a, %10000000
jr z, _noBitShft
_bitShft:
srl a
djnz _bitshft
_noBitShft:
;now we have all the data taken from xstart and ystart in hl and a in the
;form of the starting bit in hl and the shifted line of bits in a. Now
;all we have left to do is print out the line in a length times. We pop bc
;in order to get the length into b
pop bc
;we load 12 into de because each time we want to advance a row, we have to
;add 12 to hl becase each row on the screen is 96/8, 12 bytes.
ld de, 12
_drawline:
;we have to push af so that we don't ruin a each time we do or (hl).
push af
or (hl)
ld (hl), a
add hl, de
pop af
djnz _drawline
ret
and
horizontal line drawer:
Code: horizontal_line:
;again, h = xstart, l = ystart, and a = length
;check and see if length is negative. If so, negate a and adjust hl accordingly.
bit 7, a
jr z, _posLength
neg
;protect af so we can calculate the new ystart.
push af
;xstart = xstart-length = -(length-xstart) = -(a-h)
sub h
neg
inc a
ld h, a
pop af
;hl and length are corrected for negative length, carry on...
_posLength:
;protect initial data during arithmetic operations to find the first bit to write to.
push af
push hl ;we push hl second so that it is on top of the stack so we can access xstart and ystart
;multiply y0 by 12
ld h, 0
add hl, hl
add hl, hl
ld d, h
ld e, l
add hl, hl
add hl, de
;now, hl contains y0 times 12. The ystart data becomes useless.
;here, we have to find xstart/8 and add to ystart * 12
;load the xstart data into de, but be sure to re-preserve it. xstart is now in d and ystart is now in e
pop de
push de
;now the row of the bit to be written at (ystart*12) is on top of the stack so we can retrive it once we divide xstart by 8
;divide xstart (d) by 8 and then add to the top member of the stack
srl d
srl d
srl d
ld e, d
ld d, 0
add hl, de
ld de, plotSScreen
add hl, de
;now, hl contains the starting bit.
;load xstart data into b
pop bc
;now do b mod 8
ld a, b
and %00000111
ld c, a ;keep xstart mod 8 in c so that it isn't effected by djnz
ld b, a
cp 0
;load a solid 8-bit line into a.
ld a, $FF
jr z, _noBitShft
_bitshft:
srl a
djnz _bitshft
_noBitShft:
;pop de loads length into d. protect bitshifted $FF momentarily in e so that we can do arithmetic work with a.
pop de
;push de
ld e, a
ld a, d
add a, c
sub 8
jr nc, _noBitMask
neg
ld d, 0
;now a holds the number of bits to take off the end.
ld b, a
ld a, $FF
_bitMaskShft:
sla a
djnz _bitMaskShft
;do the actual bitmasking.
and e
;now a contains the properly shortened line
jr _dispFirstSeg
_nobitMask:
ld d, a
ld a, e
_dispFirstSeg:
or (hl)
ld (hl), a
inc hl
ld a, d
;if the remaining length is 0, return.
cp 0
ret z
;now both d and a contain the length of the remaining line. We have to take a mod 8 and push it because we will need that for later. (It's the length of the very end of the line)
and %00000111
push af
srl d
srl d
srl d
ld b, d
;now in b is how many times to print out a solid line in hl
;verify that b is greater than zero
ld a, b
cp 0
jr z, _noSolidLine
_drawSolidLine:
ld (hl), $FF
inc hl
djnz _drawSolidLine
_noSolidLine:
;put the remaining length of the line into af. should be less than 8 by this point.
;We will load $FF (%1111,1111) into a and then shift a left 8 - remaining_length times. A length of 0 will result in 8 rotations and a length of 8 will result in 0 rotations.
;note from a bug fix: we used to shift a 7 - remaining_length times, but if the remaining length was ever 7, b would be 0 and on the first djnz leftBitShift, b would go to FF and a would get shifted 255 times.
pop af
neg
ret z
;if neg returns a 0, there is no more line to draw. return.
add a, 8
ld b, a
ld a, $FF
_leftBitShift:
sla a
djnz _leftBitShift
or (hl)
ld (hl), a
ret
Thanks so much