1

**ASM / Re: Sine Approximation [Z80]**

« **on:**February 03, 2022, 09:08:55 pm »

Necropost: this came up elsewhere and I made a python version if anybody is interested. I think it could be implemented really well with logic gates!

https://gist.github.com/Zeda/98aab98fd32a231b878772a435ffb306

https://gist.github.com/Zeda/98aab98fd32a231b878772a435ffb306

Code: [Select]

`#!/usr/bin/python3`

# This is ported from:

# https://www.omnimaga.org/asm-language/sine-approximation-(z80)/

# (Xeda112358 a.k.a. Zeda a.k.a. ZedaZ80)

#

from math import sin

def sineb(x):

"""Approximates 128*sin(x*2*pi/256)"""

a1 = int(x)

a0 = a1 << 1

a2 = a1 >> 1

# iteration 1

if a1 & 64:

l = ~a0 & 127

else:

l = a0 & 127

# iteration 2

if a1 & 32:

l += ~a1 & 31

else:

l += a1 & 31

# iteration 3

if a1 & 16:

l += ~a2 & 7

else:

l += a2 & 7

# check if it needs to be negated

if a1 & 128:

return -l

else:

return l

# Plot a graph of the approximation vs the actual

for x in range(0, 256, 2):

y = sineb(x)

z = int(sin(x*3.1415926535/128)*128)

# translate and scale for "graphing"

y += 128

z += 128

y >>= 1

z >>= 1

# "graph"

# X - Approximation

# O - Actual

# 8 - used when the graphs overlap

if y == z:

print(" "*y + "8")

elif y > z:

print(" "*z + "X" + " "*(y-z-1) + "O")

else:

print(" "*y + "O" + " "*(z-y-1) + "X")