1313BUILD_DIR = BASE_DIR / "build"
1414PROCESSADOR_BASE = Path ("/eda/processadores" )
1515
16- def analyze_all_vhdl_files (vhdl_files ):
17- """Analisar todos os arquivos VHDL de uma vez."""
18- print (f"[INFO] Analisando arquivos VHDL:" )
19- for f in vhdl_files :
20- print (f" { f } " )
21- cmd = ["ghdl" , "-a" , * map (str , vhdl_files )]
16+ def run_ghdl_import (cpu_name , vhdl_files ):
17+ """Importar todos os arquivos VHDL com GHDL -i."""
18+ print ("[INFO] Importando arquivos VHDL com GHDL (-i)..." )
19+ cmd = [
20+ "ghdl" , "-i" , "--std=08" ,
21+ f"--work={ cpu_name } " ,
22+ f"--workdir={ BUILD_DIR } " ,
23+ f"-P{ BUILD_DIR } "
24+ ] + list (map (str , vhdl_files ))
2225 print (f"[CMD] { ' ' .join (cmd )} " )
2326 subprocess .run (cmd , check = True )
2427
25- def synthesize_vhdl_entity ( entity_name , output_file ):
26- """Sintetizar a entidade principal para Verilog ."""
27- print (f "[INFO] Sintetizando entidade VHDL: { entity_name } " )
28+ def run_ghdl_elaborate ( cpu_name , top_module ):
29+ """Elaborar com GHDL -m ."""
30+ print ("[INFO] Elaborando projeto com GHDL (-m)... " )
2831 cmd = [
29- "ghdl" , "--synth" , entity_name ,
30- "--out=verilog" , "-o" , str (output_file )
32+ "ghdl" , "-m" , "--std=08" ,
33+ f"--work={ cpu_name } " ,
34+ f"--workdir={ BUILD_DIR } " ,
35+ f"-P{ BUILD_DIR } " ,
36+ f"{ top_module } "
3137 ]
3238 print (f"[CMD] { ' ' .join (cmd )} " )
3339 subprocess .run (cmd , check = True )
3440
41+ def synthesize_to_verilog (cpu_name , output_file , top_module ):
42+ """Sintetizar o VHDL com GHDL para Verilog."""
43+ print (f"[INFO] Sintetizando { cpu_name } para Verilog..." )
44+ cmd = [
45+ "ghdl" , "synth" , "--latches" , "--std=08" ,
46+ f"--work={ cpu_name } " ,
47+ f"--workdir={ BUILD_DIR } " ,
48+ f"-P{ BUILD_DIR } " ,
49+ "--out=verilog" , top_module
50+ ]
51+ print (f"[CMD] { ' ' .join (cmd )} > { output_file } " )
52+ with open (output_file , "w" ) as f :
53+ subprocess .run (cmd , stdout = f , check = True )
54+
3555def main ():
3656 if len (sys .argv ) != 2 :
3757 print ("Uso: simulate.py <nome_do_processador>" )
3858 sys .exit (1 )
3959
4060 cpu_name = sys .argv [1 ]
4161
42- # Mensagens informativas
43- print ("[INFO] Iniciando simulação do processador..." )
44- print ("[INFO] Processador:" , cpu_name )
45- print ("[INFO] Diretório base:" , BASE_DIR )
46- print ("[INFO] Diretório RTL:" , RTL_DIR )
47- print ("[INFO] Diretório de configuração:" , CONFIG_DIR )
48- print ("[INFO] Diretório interno:" , INTERNAL_DIR )
49- print ("[INFO] Diretório de build:" , BUILD_DIR )
50- print ("[INFO] Verificando arquivos..." )
62+ print ("[INFO] Iniciando simulação do processador:" , cpu_name )
5163
5264 config_file = CONFIG_DIR / f"{ cpu_name } .json"
5365 top_module_file = RTL_DIR / f"{ cpu_name } .sv"
@@ -57,16 +69,16 @@ def main():
5769 sys .exit (1 )
5870
5971 if not top_module_file .exists ():
60- print (f"[ERRO] Top module do processador não encontrado: { top_module_file } " )
72+ print (f"[ERRO] Top module não encontrado: { top_module_file } " )
6173 sys .exit (1 )
6274
6375 with open (config_file ) as f :
6476 config = json .load (f )
6577
6678 file_list = config .get ("files" , [])
6779 include_dirs = config .get ("include_dirs" , [])
80+ top_module = config .get ("top_module" , cpu_name )
6881
69- # Separar arquivos VHDL e outros
7082 vhdl_files = []
7183 other_files = []
7284
@@ -75,35 +87,31 @@ def main():
7587 if not src_file .exists ():
7688 print (f"[AVISO] Arquivo não encontrado: { src_file } " )
7789 continue
78-
7990 if src_file .suffix .lower () in [".vhdl" , ".vhd" ]:
8091 vhdl_files .append (src_file )
8192 else :
8293 other_files .append (str (src_file ))
8394
84- # Analisa todos os arquivos VHDL juntos (na ordem que apareceram)
8595 if vhdl_files :
86- analyze_all_vhdl_files (vhdl_files )
87- # Supondo que o nome da entidade VHDL principal é igual ao nome do processador
88- verilog_output = PROCESSADOR_BASE / cpu_name / f"{ cpu_name } .v"
89- synthesize_vhdl_entity (cpu_name , verilog_output )
96+ BUILD_DIR .mkdir (exist_ok = True )
97+ run_ghdl_import (cpu_name , vhdl_files )
98+ run_ghdl_elaborate (cpu_name , top_module )
99+
100+ verilog_output = BUILD_DIR / f"{ cpu_name } .v"
101+ synthesize_to_verilog (cpu_name , verilog_output , top_module )
90102 other_files .append (str (verilog_output ))
91103
92- # Adiciona tops e arquivos fixos
93104 other_files .append (str (top_module_file ))
94- other_files .append (str (INTERNAL_DIR / "verification_top.sv" ))
95- other_files .append (str (INTERNAL_DIR / "memory.sv" ))
96- other_files .append (str (INTERNAL_DIR / "axi4_to_wishbone.sv" ))
97- other_files .append (str (INTERNAL_DIR / "axi4lite_to_wishbone.sv" ))
98- other_files .append (str (INTERNAL_DIR / "ahblite_to_wishbone.sv" ))
99-
100- # Prepara diretório de build
101- BUILD_DIR .mkdir (exist_ok = True )
105+ other_files += [
106+ str (INTERNAL_DIR / "verification_top.sv" ),
107+ str (INTERNAL_DIR / "memory.sv" ),
108+ str (INTERNAL_DIR / "axi4_to_wishbone.sv" ),
109+ str (INTERNAL_DIR / "axi4lite_to_wishbone.sv" ),
110+ str (INTERNAL_DIR / "ahblite_to_wishbone.sv" )
111+ ]
102112
103- # Monta lista de -I para include_dirs
104113 include_flags = []
105114 for inc_dir in include_dirs :
106- # Monta caminho absoluto dos include dirs
107115 inc_path = PROCESSADOR_BASE / cpu_name / inc_dir
108116 if inc_path .exists ():
109117 include_flags .append (f"-I{ inc_path } " )
@@ -112,9 +120,7 @@ def main():
112120
113121 verilator_cmd = [
114122 "verilator" , "--cc" , "--exe" , "--build" ,
115- "--trace" , # habilita trace
116- "-Wno-fatal" ,
117- "-DSIMULATION" ,
123+ "--trace" , "-Wno-fatal" , "-DSIMULATION" ,
118124 "--top-module" , "verification_top" ,
119125 str (INTERNAL_DIR / "soc_main.cpp" ),
120126 * include_flags ,
@@ -123,12 +129,11 @@ def main():
123129 ]
124130
125131 print (f"[CMD] { ' ' .join (verilator_cmd )} " )
126- print ("[INFO] Rodando Verilator..." )
127132 subprocess .run (verilator_cmd , check = True , cwd = BUILD_DIR )
128133
129134 sim_executable = BUILD_DIR / "obj_dir" / "Vverification_top"
130135 if sim_executable .exists ():
131- print ("[INFO] Iniciando simulação..." )
136+ print ("[INFO] Executando simulação..." )
132137 subprocess .run ([str (sim_executable )], check = True )
133138 else :
134139 print ("[ERRO] Executável de simulação não encontrado." )
0 commit comments