summaryrefslogtreecommitdiffstats
path: root/part_3/ex12/sin_gen_scripts/sinegen.py
blob: 9dba85572e0e543e9059f3f76d95493603f3cd82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# sinegen.py - Generate sinewave table
#  ... for use with Altera ROMs
# 
from math import sin, cos, radians

DEPTH = 1024  # Size of ROM
WIDTH = 10   # Size of data in bits
OUTMAX = 2**WIDTH - 1   # Amplitude of sinewave

filename = "rom_data.mif"
f = open(filename,'w')

#  Header for the .mif file
print >> f, "-- ROM Initialization file\n"
print >> f, "DEPTH = %d;" % DEPTH
print >> f, "WIDTH = %d;" % WIDTH
print >> f, "ADDRESS_RADIX = HEX;"
print >> f, "DATA_RADIX = HEX;\n"
print >> f, "CONTENT\nBEGIN\n"

for address in range(DEPTH):
	angle = (address*2*pi)/DEPTH
	sine_value = sin(angle)
	data = int((sine_value)*0.5*OUTMAX)+OUTMAX/2
	
	print "%4x : %4x;" % (address, data)
	print >> f, "%4x : %4x;" % (address, data)

print >> f, "END;\n"

f.close()