I recently got back into z80 asm after a few months of other stuff. I'm just curious as to how efficient or inefficient my code is. Also, two questions, what is the significance of .nolist and .list for the assembler? Do I have to put my #defines between them as if in a scope?
Code:
Edit: Misaligned comments.
Code:
.nolist
#include "ti83plus.inc"
;******************
;* Screen *
;******************
; I typically only use these in one place, so defining them as macros
; rather than calling them is fine, at least in this scenario. Further
; developments may prove otherwise, but it is not important right now.
#define OpenGraphScreen bcall(_RunIndicOff) \ bcall(_ClrLCDFull) \ bcall(_GrBufClr)
#define CloseGraphScreen bcall(_GrBufClr) \ bcall(_RunIndicOn)
#define UpdateGraphScreen bcall(_GrBufCpy)
#define ClearHomeScreen bcall(_HomeUp) \ bcall(_ClrLCDFull) \ bcall(_ClrTxtShd)
#define WaitForKey bcall(_GetKey)
.list
.org userMem-2
.db $BB,$6D
Start:
ClearHomeScreen
OpenGraphScreen
; basic test case
ld h,11
ld l,27
ld e,156
call DrawVerticalLine
UpdateGraphScreen
WaitForKey
CloseGraphScreen
ClearHomeScreen
ret
;******************
;* Graphics *
;******************
; Desc: sets hl to the address of the pixel at (b,c)
; sets a to the mask of the pixel at (b,c)
; Inputs: bc
; Outputs: hl
; Destroys: af,bc,de,hl
; T-States: 412/167
; Size: 39
GetPixel: ; T-States ; Size
; calculate byte y offset (y*12)
sla c ; 8 ; 2
sla c ; 8 ; 2
ld d,0 ; 7 ; 2
ld e,c ; 4 ; 1
ld h,0 ; 7 ; 2
ld l,c ; 4 ; 1
add hl,hl ; 11 ; 1
add hl,de ; 11 ; 1
; calculate byte x offset (x/8)
ld a,b ; 4 ; 1
rra ; 4 ; 1
rra ; 4 ; 1
rra ; 4 ; 1
and $1F ; 7 ; 2
; calculate pixel address
ld e,a ; 4 ; 1
add hl,de ; 11 ; 1
ld de,plotSScreen ; 10 ; 3
add hl,de ; 11 ; 1
; calculate pixel mask
ld a,b ; 4 ; 1
and $07 ; 7 ; 2
ld b,$80 ; 7 ; 2
_GetPixel_Loop:
or a ; 4 ; 1
jr z,_GetPixel_Exit ; 12/7 ; 2
dec a ; 4 ; 1
srl b ; 8 ; 2
jr _GetPixel_Loop ; 12 ; 2
_GetPixel_Exit:
ld a,b ; 4 ; 1
ret ; 10 ; 1
; Desc: Turns on a pixel at (b,c)
; Inputs: bc
; Outputs: pixel on at (b,c)
; Destroys: af,bc,de,hl
; T-States: 453/208
; Size: 6
PixelOn: ; T-States ; Size
call GetPixel ; 429/184 ; 3
or (hl) ; 7 ; 1
ld (hl),a ; 7 ; 1
ret ; 10 ; 1
; Desc: Turns off a pixel at (b,c)
; Inputs: bc
; Outputs: pixel off at (b,c)
; Destroys: af,bc,de,hl
; T-States: 457/212
; Size: 7
PixelOff: ; T-States ; Size
call GetPixel ; 429/184 ; 3
cpl ; 4 ; 1
and (hl) ; 7 ; 1
ld (hl),a ; 7 ; 1
ret ; 10 ; 1
; Desc: Inverts a pixel at (b,c)
; Inputs: bc
; Outputs: pixel inverted at (b,c)
; Destroys: af,bc,de,hl
; T-States: 453/208
; Size: 6
PixelInvert: ; T-States ; Size
call GetPixel ; 429/184 ; 3
xor (hl) ; 7 ; 1
ld (hl),a ; 7 ; 1
ret ; 10 ; 1
; Desc: Displays a vertical clipped line from (x0,y0)=(h,l) to (x1,y1)=(h,e)
; Inputs: e,hl
; Outputs: None
; Destroys: af,bc,de,hl
; T-States: 5012/36
; Size: 42
DrawVerticalLine: ; T-States ; Size
; if y1 < y0, swap them
ld a,e ; 4 ; 1
cp l ; 4 ; 1
jr nc,_DrawVerticalLine_Skip ; 12/7 ; 2
ld e,l ; 4 ; 1
ld l,a ; 4 ; 1
_DrawVerticalLine_Skip:
; check if line is completely off screen
ld a,63 ; 7 ; 2
cp l ; 4 ; 1
ret c ; 11/5 ; 1
; clamp y1 to bottom of screen
cp e ; 4 ; 1
jr nc,DrawVerticalLineUnsafe ; 12/7 ; 2
ld e,63 ; 7 ; 2
; Only call this if the line is completely on screen and y0 <= y1
DrawVerticalLineUnsafe:
; get the address and mask of the first pixel
; Note: the mask stays the same for all pixels
ld b,h ; 4 ; 1
ld c,l ; 4 ; 1
push bc ; 11 ; 1
push de ; 11 ; 1
call GetPixel ; 429/184 ; 3
pop de ; 10 ; 1
pop bc ; 10 ; 1
ld d,a ; 4 ; 1
_DrawVerticalLine_Loop:
; draw the pixel
or (hl) ; 7 ; 1
ld (hl),a ; 7 ; 1
; check if the line is done being drawn
ld a,c ; 4 ; 1
cp e ; 4 ; 1
ret z ; 11/5 ; 1
; get the address of the next pixel (hl+=12)
ld a,l ; 4 ; 1
add a,12 ; 7 ; 2
ld l,a ; 4 ; 1
ld a,h ; 4 ; 1
adc a,0 ; 7 ; 2
ld h,a ; 4 ; 1
; calculate next pixel information
inc c ; 4 ; 1
ld a,d ; 4 ; 1
jr _DrawVerticalLine_Loop ; 12 ; 2
; Desc: Displays a horizontal clipped line from (x0,y0)=(h,l) to (x1,y1)=(d,l)
; Inputs: d,hl
; Outputs: None
; Destroys:
; T-States:
; Size:
DrawHorizontalLine: ; T-States ; Size
; TODO
ret
; Desc: Displays a clipped line from (x0,y0)=(h,l) to (x1,y1)=(d,e)
; Inputs: de,hl
; Outputs: None
; Destroys:
; T-States:
; Size:
DrawLine: ; T-States ; Size
; TODO
ret
Edit: Misaligned comments.