|
| 1 | +# Copyright 2025 ETH Zurich and University of Bologna. |
| 2 | +# Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Philip Wiese <[email protected]> |
| 6 | + |
| 7 | +# ------------------------------------------------------------------------------ |
| 8 | +# STUDENT TASK 1a: |
| 9 | +# |
| 10 | +# In this exercise, you will visualize the area breakdown of the i_croc module |
| 11 | +# using a bar plot. You will learn common tasks such as scaling axes, setting |
| 12 | +# titles, and customizing the appearance of the plot. |
| 13 | +# |
| 14 | +# The code below extracts the hierarchical area data. Your task is to visualize |
| 15 | +# the area breakdown of the i_croc module as a bar plot and customize the plot |
| 16 | +# to improve its readability. |
| 17 | +# |
| 18 | +# Follow the instructions in the code comments to complete the exercise. |
| 19 | +# ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +# Import the modules to extract hierarchical area data and plot the results |
| 22 | +from area import extract_areas, get_path |
| 23 | + |
| 24 | +# Argparse is a module that allows you to parse command-line arguments |
| 25 | +# See https://docs.python.org/3/library/argparse.html for more information |
| 26 | +import argparse |
| 27 | + |
| 28 | +# Import the matplotlib library for plotting |
| 29 | +# See https://matplotlib.org/stable/gallery/index.html for examples |
| 30 | +from matplotlib import pyplot as plt |
| 31 | + |
| 32 | +# Import the os module to create directories |
| 33 | +import os |
| 34 | + |
| 35 | +# IHP-130 sg13g2_stdcell library (1 GE = 7.25 um^2) |
| 36 | +# See http://eda.ee.ethz.ch/index.php?title=Ihp13#What_is_one_gate_equivalent_? |
| 37 | +AREA_PER_GE = 7.25 |
| 38 | + |
| 39 | +if __name__ == "__main__": |
| 40 | + # Usage: python 01a_plot_area_bar.py <area_report_file> |
| 41 | + parser = argparse.ArgumentParser( |
| 42 | + description="Extract hierarchical areas from an OpenROAD area report." |
| 43 | + ) |
| 44 | + parser.add_argument("file", type=str, help="Path to the area report file") |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + # Extract the hierarchical area data |
| 48 | + area_tree = extract_areas(args.file) |
| 49 | + |
| 50 | + if area_tree is None: |
| 51 | + print("No hierarchical area data found.") |
| 52 | + exit(-1) |
| 53 | + |
| 54 | + # Extract the hierarchical area data |
| 55 | + node_i_croc = get_path(area_tree, ["<top>", "i_croc_soc", "i_croc"]) |
| 56 | + |
| 57 | + # Extract names and convert areas to kGE |
| 58 | + names = [] |
| 59 | + areas_kge = [] |
| 60 | + |
| 61 | + for node in node_i_croc.children: |
| 62 | + print(node) |
| 63 | + # Convert area from um^2 to kGE |
| 64 | + area_kge = node.area / (AREA_PER_GE * 1e3) |
| 65 | + areas_kge.append(area_kge) |
| 66 | + names.append(node.name) |
| 67 | + |
| 68 | + # ========================================================================== |
| 69 | + # STUDENT TASKS - MODIFY THE CODE BELOW TO COMPLETE THE EXERCISE |
| 70 | + # ========================================================================== |
| 71 | + |
| 72 | + # -------------------------------------------------------------------------- |
| 73 | + # STUDENT TASK 1a.1: Create a bar plot |
| 74 | + # -------------------------------------------------------------------------- |
| 75 | + # Use `plt.bar(...)` to create a bar chart. |
| 76 | + # |
| 77 | + # - The x-axis should represent the component names. |
| 78 | + # - The y-axis should represent the area values. |
| 79 | + # -------------------------------------------------------------------------- |
| 80 | + |
| 81 | + # Set the figure size |
| 82 | + plt.figure(figsize=(10, 5)) |
| 83 | + |
| 84 | + # Label for y-axis |
| 85 | + plt.ylabel("Area [kGE]") |
| 86 | + |
| 87 | + # Label for x-axis |
| 88 | + plt.xlabel("Component") |
| 89 | + |
| 90 | + # Title |
| 91 | + plt.title("Area Breakdown: i_croc", fontsize=12, fontweight="bold") |
| 92 | + |
| 93 | + # -------------------------------------------------------------------------- |
| 94 | + # STUDENT TASK 1a.2: Improve the readability of the plot |
| 95 | + # -------------------------------------------------------------------------- |
| 96 | + # Improve the visualization by: |
| 97 | + # - Rotating x-axis labels if they overlap |
| 98 | + # - Adding grid lines for better readability |
| 99 | + # - Use distinct colors for each bar |
| 100 | + # |
| 101 | + # HINT: Use `plt.xticks(...)` for better readability. |
| 102 | + # You can also use `plt.yscale('log')` if areas vary significantly. |
| 103 | + # -------------------------------------------------------------------------- |
| 104 | + |
| 105 | + # Rotate x-axis labels for better readability |
| 106 | + plt.xticks(rotation=45, ha="right") |
| 107 | + |
| 108 | + # Add grid lines for better readability |
| 109 | + plt.grid(axis="y", linestyle="--", alpha=0.6) |
| 110 | + |
| 111 | + # Use distinct colors for each bar |
| 112 | + cmap = plt.get_cmap("tab10") |
| 113 | + colors = [cmap(i) for i in range(len(names))] |
| 114 | + |
| 115 | + # -------------------------------------------------------------------------- |
| 116 | + # STUDENT TASK 1a.3: Remap the names of the components |
| 117 | + # -------------------------------------------------------------------------- |
| 118 | + # Remap the names of the components for better readability. |
| 119 | + # Remember to move the plot commands to the correct position in the code. |
| 120 | + # |
| 121 | + # HINT: Use the `names` list to remap the names of the components. |
| 122 | + # -------------------------------------------------------------------------- |
| 123 | + |
| 124 | + # Remap the names of the components for better readability |
| 125 | + names = [ |
| 126 | + "SRAM Bank 1", |
| 127 | + "SRAM Bank 2", |
| 128 | + "Core", |
| 129 | + "Debug Module", |
| 130 | + "JTAG TAP", |
| 131 | + "GPIO", |
| 132 | + "SoC Control", |
| 133 | + "Timer", |
| 134 | + "UART", |
| 135 | + ] |
| 136 | + |
| 137 | + # -------------------------------------------------------------------------- |
| 138 | + # STUDENT TASK 1a.4: Save and display the plot |
| 139 | + # -------------------------------------------------------------------------- |
| 140 | + # Save the figure as a high-quality PDF and display the plot. |
| 141 | + # |
| 142 | + # HINT: Use `plt.tight_layout()` to adjust the layout for better |
| 143 | + # readability. |
| 144 | + # Save the figure using `plt.savefig(...) |
| 145 | + # -------------------------------------------------------------------------- |
| 146 | + |
| 147 | + # Create a bar chart |
| 148 | + plt.bar(names, areas_kge, color=colors) |
| 149 | + |
| 150 | + # Adjust layout for better |
| 151 | + plt.tight_layout() |
| 152 | + |
| 153 | + # Create a directory if it does not exist |
| 154 | + os.makedirs("plots", exist_ok=True) |
| 155 | + |
| 156 | + # Save figure |
| 157 | + plt.savefig("plots/01a_area_breakdown_bar.pdf") |
| 158 | + plt.savefig("plots/01a_area_breakdown_bar.png") |
| 159 | + |
| 160 | + # Display the plot |
| 161 | + plt.show() |
0 commit comments