cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

delay.s (1443B)


      1	.include	"global.s"
      2
      3	.area	_HOME
      4
      5	;; Delay DE milliseconds
      6	;; 
      7	;; Entry conditions
      8	;;   DE = number of milliseconds to delay (1 to 65536, 0 = 65536)
      9	;; 
     10	;; Register used: AF, DE
     11	.CPMS	= 4194/4	; 4.194304 MHz
     12
     13_delay::
     14	LDA	HL,2(SP)	; Skip return address
     15	LD	E,(HL)		; DE = delay
     16	INC	HL
     17	LD	D,(HL)
     18
     19.delay::			; 6 cycles for the CALL
     20	PUSH	BC		; 4 cycles
     21	CALL	.dly		; 12 cycles to return from .dly (6+1+5)
     22	LD	B,#.CPMS/20-2	; 2 cycles
     23				; =========
     24				; 24 cycles
     25.ldlp:
     26	PUSH	BC		; 4 cycles
     27	POP	BC		; 3 cycles
     28	PUSH	BC		; 4 cycles
     29	POP	BC		; 3 cycles
     30	NOP			; 1 cycles
     315$:	DEC	B		; 1 cycles
     32	JP	NZ,.ldlp	; 3 cycles (if TRUE: 4 cycles)
     33	NOP			; 1 cycles
     34				; =========
     35				; 20 cycles
     36	;; Exit in 16 cycles
     37	POP	BC		; 3 cycles
     38	PUSH	BC		; 4 cycles
     39	POP	BC		; 3 cycles
     40	NOP			; 1 cycles
     41	NOP			; 1 cycles
     42	RET			; 4 cycles
     43				; =========
     44				; 16 cycles
     45
     46	;; Delay all but last millisecond
     47.dly:
     48	DEC	DE		; 2 cycles
     49	LD	A,E		; 1 cycles
     50	OR	D		; 1 cycles
     51	RET	Z		; 2 cycles (upon return: 5 cycles)
     52	LD	B,#.CPMS/20-1	; 2 cycles
     53				; =========
     54				; 8 cycles
     55.dlp:
     56	PUSH	BC		; 4 cycles
     57	POP	BC		; 3 cycles
     58	PUSH	BC		; 4 cycles
     59	POP	BC		; 3 cycles
     60	NOP			; 1 cycles
     61	DEC	B		; 1 cycles
     62	JP	NZ,.dlp		; 3 cycles (if TRUE: 4 cycles)
     63	NOP			; 1 cycles
     64				; =========
     65				; 20 cycles
     66	;; Exit in 15 cycles
     67	PUSH	BC		; 4 cycles
     68	POP	BC		; 3 cycles
     69	NOP			; 1 cycles
     70	NOP			; 1 cycles
     71	JR	.dly		; 3 cycles
     72				; =========
     73				; 12 cycles