Optimizations and documenting code

This commit is contained in:
Pedro de Oliveira 2014-08-21 05:55:54 +01:00
parent bb433bd645
commit 95d804311a
1 changed files with 81 additions and 73 deletions

View File

@ -6,6 +6,9 @@ tv_flag EQU $5c3c ; TV flags
last_k EQU $5c08 ; Last pressed key last_k EQU $5c08 ; Last pressed key
clr_screen EQU $0daf ; ROM routine to clear the screen clr_screen EQU $0daf ; ROM routine to clear the screen
; http://www.z80.info/pseudo-random.txt
; seed - http://www.wearmouth.demon.co.uk/sys/frames.htm
; Screen is 256x192 ; Screen is 256x192
; Star Structure ; Star Structure
@ -21,17 +24,18 @@ start
ld (tv_flag), a ld (tv_flag), a
push bc push bc
call clear_screen call clear_screen ; Clear the screen
call initStars call initStars ; Initialize the number of stars defined in MAX_STARS
main_start main_start
ld hl, STARS ld hl, STARS ; Points to X
ld c, MAX_STARS ld c, MAX_STARS
main main
inc hl ; Y ; CLEAR THE LAST POSITION
inc hl ; Speed ld de, $3 ; Skip to PrevX
inc hl ; PrevX adc hl, de
ld a, (hl) ld a, (hl)
ld d, a ; Save PrevX to D ld d, a ; Save PrevX to D
inc hl ; PrevY inc hl ; PrevY
@ -39,40 +43,38 @@ main
ld a, (hl) ld a, (hl)
ld e, a ; Save PrevY to E ld e, a ; Save PrevY to E
push hl
push bc push bc
push hl
call get_screen_address call get_screen_address
; Video RAM address for those X,Y is now in HL and the bit needed ; Video RAM address for those X,Y is now in HL and the bit needed
; to be set in that address value is in A ; to be set in that address value is in A
call clear_pixel ; Uses those values clears the pixel call clear_pixel ; Uses those values clears the pixel
pop bc
pop hl pop hl
dec hl ; PrevX
dec hl ; Speed
dec hl ; Y
dec hl ; X
ld bc, $4 ; Go back to X
sbc hl, bc
pop bc
; WRITES THE PIXEL
ld a, (hl) ; HL points to X ld a, (hl) ; HL points to X
ld d, a ; Save X to D ld d, a ; Save X to D
inc hl inc hl ; Y
ld a, (hl) ; HL now points to Y ld a, (hl)
ld e, a ; Save Y to E ld e, a ; Save Y to E
push hl
push bc push bc
push hl
call get_screen_address call get_screen_address
; Video RAM address for those X,Y is now in HL and the bit needed ; Video RAM address for those X,Y is now in HL and the bit needed
; to be set in that address value is in A ; to be set in that address value is in A
call write_pixel ; Uses those values and writes the pixel call write_pixel ; Uses those values and writes the pixel
pop bc
pop hl pop hl
inc hl ; Speed ld bc, $4 ; Jump 4 positions - Next star
inc hl ; PrevX adc hl, bc
inc hl ; PrevY
inc hl ; Next star pop bc
dec c ; Decrement counter dec c ; Decrement counter
jr nz, main ; Repeat if not zero jr nz, main ; Repeat if not zero
@ -84,45 +86,44 @@ main
ret ret
PROC PROC
; Initialize stars X and Y with "random" values
initStars initStars
push bc push bc
ld d, MAX_STARS ld d, MAX_STARS ; Number of stars to process
ld hl, STARS ld hl, STARS ; HL points to X of first start
initStars_loop initStars_loop
push hl push hl
call getRandomX call getRandomX ; Get a random value for X | 1 - 250 | TODO: 255?
pop hl pop hl
ld (hl), a ; Set X to random value
ld (hl), a
inc hl ; points to Y
inc hl ; Y
push hl push hl
call getRandomY call getRandomY ; Get a random value for Y | 1 - 191 | TODO : allow 0
pop hl pop hl
ld (hl), a ; Set Y to random value
ld (hl), a inc hl ; points to Speed
inc hl ; Speed
push hl push hl
call getRandomSpeed call getRandomSpeed ; Get a random value for Speed | 1 - 4
pop hl pop hl
ld (hl), a ; Set Speed to random value
ld (hl), a ld bc, $3 ; Jump to next star
adc hl, bc ; Skip PrevX and PrevY
inc hl ; PrevX dec d ; Decrement counter
inc hl ; PrevY jr nz, initStars_loop ; If not zero, do it again
inc hl ; Next one
dec d
jr nz, initStars_loop
pop bc pop bc
ret ret
ENDP ENDP
PROC PROC
; Gets a value a from a list of pre-calculated values
; Returns to begin after 0 is found | TODO: change this
getRandomX getRandomX
push hl push hl
getRandomX_loop getRandomX_loop
@ -141,6 +142,8 @@ getRandomX_reset
ENDP ENDP
PROC PROC
; Gets a value a from a list of pre-calculated values
; Returns to begin after 0 is found | TODO: change this
getRandomY getRandomY
push hl push hl
getRandomY_loop getRandomY_loop
@ -159,6 +162,8 @@ getRandomY_reset
ENDP ENDP
PROC PROC
; Gets a value a from a list of pre-calculated values
; Returns to begin after 0 is found | TODO: change this
getRandomSpeed getRandomSpeed
push hl push hl
getRandomSpeed_loop getRandomSpeed_loop
@ -183,68 +188,71 @@ increment_x
ld hl, STARS ld hl, STARS
ld c, MAX_STARS ld c, MAX_STARS
increment_x_loop increment_x_loop
push de ; First lets copy current position to previous position
ld d, (hl) ; Save current X to D ld d, (hl) ; Save current X to D
inc hl ; Y inc hl ; points to Y
ld e, (hl) ; Save current Y to E ld e, (hl) ; Save current Y to E
inc hl ; Speed
inc hl ; PrevX inc hl ; points to Speed
ld (hl), d inc hl ; points to PrevX
ld (hl), d ; Save X
inc hl ; PrevY inc hl ; PrevY
ld (hl), e ld (hl), e ; Save Y
dec hl ; PrevX
dec hl ; Speed ld de, $4
dec hl ; Y sbc hl, de ; Go back to X
dec hl ; X
pop de
ld a, (hl) ld a, (hl) ; Is X at $FF - end of screen
cp $ff cp $ff
jr z, increment_x_zero jr z, increment_x_zero ; Yes, lets reset
inc hl ; Skip to Y ; Increments X position by speed value
inc hl ; Skip to Speed ; X = X + Y
inc hl ; points to Y
inc hl ; points to Speed
ld b, (hl) ; Read speed to B ld b, (hl) ; Read speed to B
dec hl ; Back to Y dec hl ; Back to Y
dec hl ; Back to X dec hl ; Back to X
add a, b ; Add speed to X add a, b ; X = X + Speed
jr c, increment_x_zero ; If carry is set, it passed $ff, lets reset
jr c, increment_x_zero
increment_x_update increment_x_update
ld (hl), a ; Saves to X the value in A
inc hl ; Y ld (hl), a ; Save X with the value in A
inc hl ; Speed
inc hl ; PrevX ld de, $5 ; Skip 5 bytes to the next star
inc hl ; PrevY adc hl, de
inc hl ; Next
dec c dec c ; Decrement counter
jr nz, increment_x_loop jr nz, increment_x_loop ; If not zero, do it again
pop bc pop bc
ret ret
increment_x_zero increment_x_zero
; Sets X to 0 and Y and Speed to random values
push bc push bc
inc hl ; Set to Y position inc hl ; point to Y
push hl push hl
call getRandomY call getRandomY ; Get a random Y value
pop hl pop hl
ld (hl), a ; Set Y = getRandomY ld (hl), a ; Set Y = getRandomY
inc hl ; Set to speed position inc hl ; point to speed
push hl push hl
call getRandomSpeed call getRandomSpeed ; Get a random Speed value
pop hl pop hl
ld (hl), a ; Set Speed = getRandomSpeed ld (hl), a ; Set Speed = getRandomSpeed
dec hl ld de, $2
dec hl ; Get back to X position sbc hl, de ; Get back to X position
ld a, $0 ; X = 0 ld a, $0 ; X = 0
pop bc pop bc