'
' Four-Variable PIC Delay Calculator, Version 2
'
' Written by Andy Warren 31 Jan 1995
' Fixed the bug found by Eric Liu 30 April 1995
'
' (C) 1995 Fast Forward Engineering
'
' Permission is hereby granted for all
' non-commercial use.
'

	defdbl a-z

start:

	x = 0

	input "Number of cycles to delay:",delay
	delay = int(delay)

	if delay < 32 then goto inline

	x = delay-16

			      d = int(x/50463234): if d > 255 then d = 255
	x = x - d * 50463234: c = int (x/197122):  if c > 255 then c = 255
	x = x - c * 197122:   b = int (x/770):     if b > 255 then b = 255
	x = x - b * 770:      a = int (x/3):       if a > 255 then a = 255

	a = a + 1: b = b + 1: c = c + 1: d = d + 1

	x = 3*a + 770*b + 197122*c + 50463234*d - 50661121 + 8

	if a = 256 then a = 0
	if b = 256 then b = 0
	if c = 256 then c = 0
	if d = 256 then d = 0

	a$=right$("   "+str$(a),3)
	b$=right$("   "+str$(b),3)
	c$=right$("   "+str$(c),3)
	d$=right$("   "+str$(d),3)

	print
	print"       list r=dec"
	print
	print"r1     equ     [any file register]"
	print"r2     equ     r1+1"
	print"r3     equ     r2+1"
	print"r4     equ     r3+1"
	print"                    ___________________
	print"delay: movlw ";a$;"   |                   |"
	print"       movwf  r1   |                   V"
	print"       movlw ";b$;"   |       loop:  decfsz r1"
	print"       movwf  r2   |              goto   loop"
	print"       movlw ";c$;"   |              decfsz r2"
	print"       movwf  r3   |              goto   loop"
	print"       movlw ";d$;"   |              decfsz r3"
	print"       movwf  r4   |              goto   loop"
	print"           |       |              decfsz r4"
	print"           |_______|              goto   loop"

inline:

	x = delay - x

	g = int(x/2): if g = 0 then goto inline1

	for y = 1 to g
	print"                                  goto   $+1"
	next

inline1:

	if x/2 = int(x/2) then goto finis

	print"                                  nop"

finis:

	end

