1 |
8 |
gdevic |
#!/usr/bin/env python3
|
2 |
|
|
#
|
3 |
|
|
# This script reads and parses all top-level modules and generates a core block
|
4 |
|
|
# file containing instantiation of these modules. This generated file is included
|
5 |
|
|
# by core.vh
|
6 |
|
|
#
|
7 |
|
|
#-------------------------------------------------------------------------------
|
8 |
|
|
# Copyright (C) 2016 Goran Devic
|
9 |
|
|
#
|
10 |
|
|
# This program is free software; you can redistribute it and/or modify it
|
11 |
|
|
# under the terms of the GNU General Public License as published by the Free
|
12 |
|
|
# Software Foundation; either version 2 of the License, or (at your option)
|
13 |
|
|
# any later version.
|
14 |
|
|
#
|
15 |
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
16 |
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
17 |
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
18 |
|
|
# more details.
|
19 |
|
|
#-------------------------------------------------------------------------------
|
20 |
|
|
import os
|
21 |
|
|
|
22 |
|
|
# Define a set of module cross-connections. These are the chip's internal buses
|
23 |
|
|
# which we inject as connections as we generate a list of module instances
|
24 |
|
|
xconnections = [
|
25 |
|
|
['interrupts', 'db', 'db0[4:3]'],
|
26 |
|
|
['ir', 'db', 'db0[7:0]'],
|
27 |
|
|
['alu_control', 'db', 'db1[7:0]'],
|
28 |
|
|
['alu_control', 'op543', '{pla[104],pla[103],pla[102]}'],
|
29 |
|
|
['alu_flags', 'db', 'db1[7:0]'],
|
30 |
|
|
['alu', 'db', 'db2[7:0]'],
|
31 |
|
|
['alu', 'bsel', 'db0[5:3]'],
|
32 |
|
|
['reg_file', 'db_hi_ds', 'db2[7:0]'],
|
33 |
|
|
['reg_file', 'db_lo_ds', 'db1[7:0]'],
|
34 |
|
|
['reg_file', 'db_hi_as', 'db_hi_as[7:0]'],
|
35 |
|
|
['reg_file', 'db_lo_as', 'db_lo_as[7:0]'],
|
36 |
|
|
['address_latch', 'abus', '{db_hi_as[7:0], db_lo_as[7:0]}'],
|
37 |
|
|
['bus_control', 'db', 'db0[7:0]']
|
38 |
|
|
]
|
39 |
|
|
|
40 |
|
|
# Define a list of modules that are not used (but listed in 'top-level-files.txt' )
|
41 |
|
|
skip_modules = ['address_pins', 'data_pins', 'control_pins_n']
|
42 |
|
|
|
43 |
|
|
# For error-checking, make sure every xconnection entry has been utilized
|
44 |
|
|
xcount = len(xconnections)
|
45 |
|
|
|
46 |
|
|
def connect(module, wire):
|
47 |
|
|
global xcount
|
48 |
|
|
for xconnection in xconnections:
|
49 |
|
|
m, w, xcon = xconnection
|
50 |
|
|
if module==m and wire==w:
|
51 |
|
|
print("Cross-connecting:", module, wire, "->", xcon)
|
52 |
|
|
xcount -= 1
|
53 |
|
|
return xcon
|
54 |
|
|
return wire
|
55 |
|
|
|
56 |
|
|
def parse(wires, lines):
|
57 |
|
|
while(len(lines)>0 and lines[0].startswith(');')==False):
|
58 |
|
|
line = lines[0].strip()
|
59 |
|
|
lines.pop(0)
|
60 |
|
|
if len(line)==0 or line[0]=='(' or line[0]=='/':
|
61 |
|
|
continue
|
62 |
|
|
tokens = line.split()
|
63 |
|
|
if len(tokens)>=3 and tokens[0] in ['input', 'output']:
|
64 |
|
|
tokens.pop(0)
|
65 |
|
|
if len(tokens)>=2 and tokens[0] in ['wire', 'reg']:
|
66 |
|
|
tokens.pop(0)
|
67 |
|
|
if len(tokens)>=2 and tokens[0].startswith('['):
|
68 |
|
|
tokens.pop(0)
|
69 |
|
|
if len(tokens)>=2 and tokens[0]=='`include':
|
70 |
|
|
include_file = tokens[1].replace('"', '')
|
71 |
|
|
with open('../control/' + include_file) as f:
|
72 |
|
|
included_lines = f.read().splitlines()
|
73 |
|
|
parse(wires, included_lines)
|
74 |
|
|
continue
|
75 |
|
|
name = tokens[0]
|
76 |
|
|
if name.endswith(','):
|
77 |
|
|
name = name[:-1]
|
78 |
|
|
wires.append(name)
|
79 |
|
|
|
80 |
|
|
with open('../top-level-files.txt') as f:
|
81 |
|
|
files = f.read().splitlines()
|
82 |
|
|
|
83 |
|
|
# Create a file that should be included in the top-level core source
|
84 |
|
|
with open('coremodules.vh', 'w') as file1:
|
85 |
|
|
file1.write("// Automatically generated by gencoremodules.py\n")
|
86 |
|
|
|
87 |
|
|
# Read and parse each file from the list of input files
|
88 |
|
|
for infile in files:
|
89 |
|
|
if not os.path.isfile('../' + infile):
|
90 |
|
|
continue
|
91 |
|
|
with open('../' + infile, "r") as f:
|
92 |
|
|
lines = f.read().splitlines()
|
93 |
|
|
|
94 |
|
|
# Find 'module' section; read and generate instantiation statement
|
95 |
|
|
while(len(lines)>0 and lines[0].startswith('module ')==False):
|
96 |
|
|
lines.pop(0)
|
97 |
|
|
if len(lines)==0:
|
98 |
|
|
continue
|
99 |
|
|
|
100 |
|
|
module_name = lines[0].split()[1]
|
101 |
|
|
lines.pop(0)
|
102 |
|
|
if module_name.endswith('('):
|
103 |
|
|
module_name = module_name[:-1]
|
104 |
|
|
if module_name in skip_modules:
|
105 |
|
|
continue
|
106 |
|
|
|
107 |
|
|
# Read a list of input/output wires, one per line
|
108 |
|
|
wires = []
|
109 |
|
|
parse(wires, lines)
|
110 |
|
|
|
111 |
|
|
# Print the names of all parsed signals in a module instantiation format
|
112 |
|
|
with open('coremodules.vh', 'a') as file1:
|
113 |
|
|
file1.write("\n" + module_name + " " + module_name + "_(\n")
|
114 |
|
|
while(len(wires)>0):
|
115 |
|
|
wire = wires.pop(0)
|
116 |
|
|
terminator = ','
|
117 |
|
|
if len(wires)==0:
|
118 |
|
|
terminator = "\n);"
|
119 |
|
|
file1.write(" ." + wire + " (" + connect(module_name, wire) + ")" + terminator + "\n")
|
120 |
|
|
|
121 |
|
|
assert(xcount==0)
|
122 |
|
|
|
123 |
|
|
# Touch files that include 'coremodules.vh' to ensure it will recompile correctly
|
124 |
|
|
os.utime("core.vh", None)
|