1 package net.mograsim.logic.model.verilog.examples;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.Scanner;
9 import java.util.stream.Collectors;
10 import java.util.stream.Stream;
12 import com.google.gson.JsonElement;
14 import net.mograsim.logic.core.types.BitVector;
15 import net.mograsim.logic.model.am2900.Am2900Loader;
16 import net.mograsim.logic.model.am2900.components.ModelAm2900MPROM;
17 import net.mograsim.logic.model.am2900.components.ModelAm2900MainMemory;
18 import net.mograsim.logic.model.am2900.components.ModelAm2900MicroInstructionMemory;
19 import net.mograsim.logic.model.model.LogicModelModifiable;
20 import net.mograsim.logic.model.model.components.ModelComponent;
21 import net.mograsim.logic.model.model.components.Orientation;
22 import net.mograsim.logic.model.model.components.atomic.ModelBitDisplay;
23 import net.mograsim.logic.model.model.components.atomic.ModelClock;
24 import net.mograsim.logic.model.model.components.atomic.ModelClock.ModelClockParams;
25 import net.mograsim.logic.model.model.components.atomic.ModelFixedOutput;
26 import net.mograsim.logic.model.model.components.atomic.ModelNandGate;
27 import net.mograsim.logic.model.model.components.atomic.ModelTextComponent;
28 import net.mograsim.logic.model.model.components.atomic.ModelTriStateBuffer;
29 import net.mograsim.logic.model.model.components.atomic.ModelTriStateBuffer.ModelTriStateBufferParams;
30 import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
31 import net.mograsim.logic.model.serializing.IdentifyParams;
32 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
33 import net.mograsim.logic.model.verilog.converter.ModelComponentToVerilogComponentDeclarationMapping;
34 import net.mograsim.logic.model.verilog.converter.ModelComponentToVerilogConverter;
35 import net.mograsim.logic.model.verilog.helper.UnionFind;
36 import net.mograsim.logic.model.verilog.model.VerilogComponentImplementation;
38 public class ExportAm2900
40 public static void main(String[] args) throws IOException
44 String rootComponentID;
45 try (Scanner sysin = new Scanner(System.in))
47 System.out.print("Directory to export Verilog into >");
48 target = Paths.get(sysin.nextLine());
49 if (!Files.exists(target))
50 Files.createDirectories(target);
51 else if (!Files.isDirectory(target))
52 throw new IllegalArgumentException("Target exists and is not a directory");
54 System.out.print("Component ID to serialize recursively >");
55 rootComponentID = sysin.nextLine();
58 if (!Files.exists(target))
59 Files.createDirectories(target);
60 else if (!Files.isDirectory(target))
61 throw new IllegalArgumentException("Target exists and is not a directory");
63 LogicModelModifiable model = new LogicModelModifiable();
65 Set<ModelComponentToVerilogComponentDeclarationMapping> atomicComponentMappings = Stream.of(//
66 new ModelNandGate(model, 1), //
67 new ModelFixedOutput(model, BitVector.SINGLE_0, null), //
68 new ModelFixedOutput(model, BitVector.SINGLE_1, null), //
69 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(1, Orientation.RIGHT)), //
70 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(1, Orientation.RIGHT_ALT)), //
71 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(1, Orientation.DOWN)), //
72 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(4, Orientation.RIGHT)), //
73 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(12, Orientation.RIGHT_ALT)), //
74 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(12, Orientation.DOWN)), //
75 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(16, Orientation.LEFT)), //
76 new ModelTriStateBuffer(model, new ModelTriStateBufferParams(16, Orientation.RIGHT_ALT)), //
77 new ModelWireCrossPoint(model, 1), //
78 new ModelWireCrossPoint(model, 2), //
79 new ModelWireCrossPoint(model, 4), //
80 new ModelWireCrossPoint(model, 9), //
81 new ModelWireCrossPoint(model, 12), //
82 new ModelWireCrossPoint(model, 16), //
83 new ModelClock(model, new ModelClockParams(7000, Orientation.RIGHT)), //
84 new ModelTextComponent(model, "A bus"), //
85 new ModelTextComponent(model, "MPM addr"), //
86 new ModelTextComponent(model, "D bus"), //
87 new ModelAm2900MainMemory(model, null), //
88 new ModelAm2900MPROM(model, null), //
89 new ModelAm2900MicroInstructionMemory(model, null), //
90 new ModelBitDisplay(model, 12), //
91 new ModelBitDisplay(model, 16)//
94 String id = c.getIDForSerializing(new IdentifyParams());
95 JsonElement params = c.getParamsForSerializingJSON(new IdentifyParams());
96 ModelComponentToVerilogComponentDeclarationMapping generateCanonicalDeclarationMapping = ModelComponentToVerilogConverter
97 .generateCanonicalDeclarationMapping(c, new UnionFind<>(), id, params,
98 ModelComponentToVerilogConverter.sanitizeVerilogID("mgs_" + id + (params.isJsonNull() ? "" : "_" + params)));
99 return generateCanonicalDeclarationMapping;
100 }).collect(Collectors.toSet());
102 ModelComponent root = IndirectModelComponentCreator.createComponent(model, rootComponentID);
103 Set<VerilogComponentImplementation> convertResult = ModelComponentToVerilogConverter.convert(atomicComponentMappings, Set.of(root),
105 for (VerilogComponentImplementation convertedComponent : convertResult)
106 Files.writeString(target.resolve(convertedComponent.getDeclaration().getID() + ".v"), convertedComponent.toVerilogCode());