OpenCores
URL https://opencores.org/ocsvn/fade_ether_protocol/fade_ether_protocol/trunk

Subversion Repositories fade_ether_protocol

[/] [fade_ether_protocol/] [trunk/] [old_stable_version/] [FPGA_no_MAC/] [src/] [ack_fifo/] [rec_to_pkg.py] - Blame information for rev 34

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 wzab
#!/usr/bin/python
2
# The script below is written by Wojciech M. Zabolotny
3
# wzab<at>ise.pw.edu.pl 19.03.2012
4
# it is published as PUBLIC DOMAIN
5
import sys
6
class field:
7
  last_bit = 0;
8
  def __init__(self,field_desc):
9
    fd = field_desc.split(",")
10
    self.fname = fd[0]
11
    if not fd[1] in ["signed","unsigned","std_logic_vector"]:
12
       raise Exception("Wrong field type")
13
    self.ftype = fd[1]
14
    if len(fd)==3:
15
       self.b1=int(fd[2])-1
16
       self.b2=0
17
    elif len(fd)==4:
18
       self.b1=int(fd[2])
19
       self.b2=int(fd[3])
20
    else:
21
       raise Exception("Syntax error in line: "+field_desc)
22
    #Assign vector bits
23
    self.v1=field.last_bit
24
    self.v2=field.last_bit+abs(self.b2-self.b1)
25
    field.last_bit = self.v2+1
26
 
27
if len(sys.argv) != 2:
28
   print """
29
The rec_to_pkg scripts creates VHDL package for conversion
30
between the VHDL records containing "signed" and "unsigned"
31
fields and std_logic_vectors.
32
It should be called as: rec_to_pkg.py description_file
33
where the description file should have the following syntax:
34
 
35
#Optional comment line
36
record record_name
37
#optional comment lines
38
#[...]
39
field_name,signed_or_unsigned,width
40
#or
41
field_name,signed_or_unsigned,left_bit_nr,right_bit_nr
42
end
43
 
44
The generated package is written to the record_name_pkg.vhd file
45
"""
46
   exit(0)
47
fin=open(sys.argv[1])
48
#Read the full description of the type
49
type_desc=[l.strip() for l in fin.readlines() if len(l) > 0 and l[0] != "#" ]
50
#The first line should contain the record name
51
l=type_desc[0].split(" ")
52
if l[0] != "record":
53
   raise Exception("Syntax error! The first line should have form \"record name_of_type\"")
54
type_name=l[1]
55
pkg_name=type_name+"_pkg"
56
#Prepare for analysis of fields
57
msb=0
58
fields=[]
59
end_found = False
60
#Find the field definitions
61
for l in type_desc[1:]:
62
   if l=="end":
63
      end_found=True
64
      break
65
   fields.append(field(l))
66
if not end_found:
67
   raise Exception("Syntax error: no \"end\" found")
68
#If we got here, probably the syntax was correct
69
#Lets generate the package
70
p="""\
71
library ieee;
72
use ieee.std_logic_1164.all;
73
use ieee.numeric_std.all;
74
"""
75
p+="package "+pkg_name+" is\n\n"
76
p+="type "+type_name+" is record\n"
77
for f in fields:
78
   s="    "+f.fname+" : "+f.ftype+"("
79
   if f.b1 > f.b2:
80
      s=s+str(f.b1)+" downto "+str(f.b2)+");\n"
81
   else:
82
      s=s+str(f.b1)+" to "+str(f.b2)+");\n"
83
   p+=s
84
p+="end record;\n\n"
85
#Write width of our type
86
p+="constant "+type_name+"_width : integer := "+str(field.last_bit)+";\n\n"
87
#Write headers of conversion functions
88
p+="function "+type_name+"_to_stlv(\n"
89
p+="  constant din : "+type_name+")\n"
90
p+="  return std_logic_vector;\n\n"
91
p+="function stlv_to_"+type_name+"(\n"
92
p+="  constant din : std_logic_vector)\n"
93
p+="  return "+type_name+";\n\n"
94
p+="end "+pkg_name+";\n\n"
95
#Now the body of the package - the conversion functions
96
p+="package body "+pkg_name+" is\n\n"
97
#
98
p+="function "+type_name+"_to_stlv(\n"
99
p+="  constant din : "+type_name+")\n"
100
p+="  return std_logic_vector is\n"
101
p+="  variable res : std_logic_vector("+str(field.last_bit-1)+" downto 0);\n"
102
p+="begin\n"
103
for f in fields:
104
  p+="  res("+str(f.v2)+" downto "+str(f.v1)+ ") := std_logic_vector(din."+f.fname+");\n"
105
p+="  return res;\n"
106
p+="end "+type_name+"_to_stlv;\n\n"
107
#
108
p+="function stlv_to_"+type_name+"(\n"
109
p+="  constant din : std_logic_vector)\n"
110
p+="  return "+type_name+" is\n"
111
p+="  variable res : "+type_name+";\n"
112
p+="begin\n"
113
for f in fields:
114
  p+="  res."+f.fname+":="+f.ftype+"(din("+str(f.v2)+" downto "+str(f.v1)+"));\n"
115
p+="  return res;\n"
116
p+="end stlv_to_"+type_name+";\n\n"
117
p+="end "+pkg_name+";\n"
118
 
119
#The output file name
120
fout_name=type_name+"_pkg.vhd"
121
fout=open(fout_name,"w")
122
fout.write(p)
123
fout.close()
124
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.