/* Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include "wait.h" #include "print.h" #include "debug.h" #include "util.h" #include "matrix.h" #include "debounce.h" #include "quantum.h" #if (MATRIX_COLS <= 8) # define print_matrix_header() print("\nr/c 01234567\n") # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) # define matrix_bitpop(i) bitpop(matrix[i]) # define ROW_SHIFTER ((uint8_t)1) #elif (MATRIX_COLS <= 16) # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) # define matrix_bitpop(i) bitpop16(matrix[i]) # define ROW_SHIFTER ((uint16_t)1) #elif (MATRIX_COLS <= 32) # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) # define matrix_bitpop(i) bitpop32(matrix[i]) # define ROW_SHIFTER ((uint32_t)1) #endif #ifdef MATRIX_MASKED extern const matrix_row_t matrix_mask[]; #endif #ifdef DIRECT_PINS static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; #endif /* matrix state(1:on, 0:off) */ static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values static matrix_row_t matrix[MATRIX_ROWS]; // debounced values __attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); } __attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); } __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } __attribute__((weak)) void matrix_init_user(void) {} __attribute__((weak)) void matrix_scan_user(void) {} inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } inline uint8_t matrix_cols(void) { return MATRIX_COLS; } // Deprecated. bool matrix_is_modified(void) { if (debounce_active()) return false; return true; } inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } inline matrix_row_t matrix_get_row(uint8_t row) { // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a // switch blocker installed and the switch is always pressed. #ifdef MATRIX_MASKED return matrix[row] & matrix_mask[row]; #else return matrix[row]; #endif } void matrix_print(void) { print_matrix_header(); for (uint8_t row = 0; row < MATRIX_ROWS; row++) { phex(row); print(": "); print_matrix_row(row); print("\n"); } } uint8_t matrix_key_count(void) { uint8_t count = 0; for (uint8_t i = 0; i < MATRIX_ROWS; i++) { count += matrix_bitpop(i); } return count; } #ifdef DIRECT_PINS static void init_pins(void) { for (int row = 0; row < MATRIX_ROWS; row++) { for (int col = 0; col < MATRIX_COLS; col++) { pin_t pin = direct_pins[row][col]; if (pin != NO_PIN) { setPinInputHigh(pin); } } } } static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { matrix_row_t last_row_value = current_matrix[current_row]; current_matrix[current_row] = 0; for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { pin_t pin = direct_pins[current_row][col_index]; if (pin != NO_PIN) { current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); } } return (last_row_value != current_matrix[current_row]); } #elif (DIODE_DIRECTION == COL2ROW) static void select_row(uint8_t row) { setPinOutput(row_pins[row]); writePinLow(row_pins[row]); } static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); } static void unselect_rows(void) { for (uint8_t x = 0; x < MATRIX_ROWS; x++) { setPinInputHigh(row_pins[x]); } } static void init_pins(void) { unselect_rows(); for (uint8_t x = 0; x < MATRIX_COLS; x++) { setPinInputHigh(col_pins[x]); } } static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { // Store last value of row prior to reading matrix_row_t last_row_value = current_matrix[current_row]; // Clear data in matrix row current_matrix[current_row] = 0; // Select row and wait for row selecton to stabilize select_row(current_row); wait_us(30); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { // Select the col pin to read (active low) uint8_t pin_state = readPin(col_pins[col_index]); // Populate the matrix row with the state of the col pin current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); } // Unselect row unselect_row(current_row); return (last_row_value != current_matrix[current_row]); } #elif (DIODE_DIRECTION == ROW2COL) static void select_col(uint8_t col) { setPinOutput(col_pins[col]); writePinLow(col_pins[col]); } static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); } static void unselect_cols(void) { for (uint8_t x = 0; x < MATRIX_COLS; x++) { setPinInputHigh(col_pins[x]); } } static void init_pins(void) { unselect_cols(); for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
\chapter{Technology Mapping}
\label{chapter:techmap}

Previous chapters outlined how HDL code is transformed into an RTL netlist. The
RTL netlist is still based on abstract coarse-grain cell types like arbitrary
width adders and even multipliers. This chapter covers how an RTL netlist is
transformed into a functionally equivalent netlist utilizing the cell types
available in the target architecture.

Technology mapping is often performed in two phases. In the first phase RTL cells
are mapped to an internal library of single-bit cells (see Sec.~\ref{sec:celllib_gates}).
In the second phase this netlist of internal gate types is transformed to a netlist
of gates from the target technology library.

When the target architecture provides coarse-grain cells (such as block ram
or ALUs), these must be mapped to directly form the RTL netlist, as information
on the coarse-grain structure of the design is lost when it is mapped to
bit-width gate types.

\section{Cell Substitution}

The simplest form of technology mapping is cell substitution, as performed by
the {\tt techmap} pass. This pass, when provided with a Verilog file that
implements the RTL cell types using simpler cells, simply replaces the RTL
cells with the provided implementation.

When no map file is provided, {\tt techmap} uses a built-in map file that
maps the Yosys RTL cell types to the internal gate library used by Yosys.
The curious reader may find this map file as {\tt techlibs/common/techmap.v} in
the Yosys source tree.

Additional features have been added to {\tt techmap} to allow for conditional
mapping of cells (see {\tt help techmap} or Sec.~\ref{cmd:techmap}). This can
for example be useful if the target architecture supports hardware multipliers for
certain bit-widths but not for others.

A usual synthesis flow would first use the {\tt techmap} pass to directly map
some RTL cells to coarse-grain cells provided by the target architecture (if
any) and then use techmap with the built-in default file to map the remaining
RTL cells to gate logic.

\section{Subcircuit Substitution}

Sometimes the target architecture provides cells that are more powerful than
the RTL cells used by Yosys. For example a cell in the target architecture that can
calculate the absolute-difference of two numbers does not match any single
RTL cell type but only combinations of cells.

For these cases Yosys provides the {\tt extract} pass that can match a given set
of modules against a design and identify the portions of the design that are
identical (i.e.~isomorphic subcircuits) to any of the given modules. These
matched subcircuits are then replaced by instances of the given modules.

The {\tt extract} pass also finds basic variations of the given modules,
such as swapped inputs on commutative cell types.

In addition to this the {\tt extract} pass also has limited support for
frequent subcircuit mining, i.e.~the process of finding recurring subcircuits
in the design. This has a few applications, including the design of new
coarse-grain architectures \cite{intersynthFdlBookChapter}.

The hard algorithmic work done by the {\tt extract} pass (solving the
isomorphic subcircuit problem and frequent subcircuit mining) is performed
using the SubCircuit library that can also be used stand-alone without Yosys
(see Sec.~\ref{sec:SubCircuit}).

\section{Gate-Level Technology Mapping}
\label{sec:techmap_extern}

On the gate-level the target architecture is usually described by a ``Liberty
file''. The Liberty file format is an industry standard format that can be
used to describe the behaviour and other properties of standard library cells
\citeweblink{LibertyFormat}.

Mapping a design utilizing the Yosys internal gate library (e.g.~as a result
of mapping it to this representation using the {\tt techmap} pass) is
performed in two phases.

First the register cells must be mapped to the registers that are available
on the target architectures. The target architecture might not provide all
variations of d-type flip-flops with positive and negative clock edge,
high-active and low-active asynchronous set and/or reset, etc. Therefore the
process of mapping the registers might add additional inverters to the design
and thus it is important to map the register cells first.

Mapping of the register cells may be performed by using the {\tt dfflibmap}
pass. This pass expects a Liberty file as argument (using the {\tt -liberty}
option) and only uses the register cells from the Liberty file.

Secondly the combinational logic must be mapped to the target architecture.
This is done using the external program ABC \citeweblink{ABC} via the
{\tt abc} pass by using the {\tt -liberty} option to the pass. Note that
in this case only the combinatorial cells are used from the cell library.

Occasionally Liberty files contain trade secrets (such as sensitive timing
information) that cannot be shared freely. This complicates processes such as
reporting bugs in the tools involved. When the information in the Liberty file
used by Yosys and ABC are not part of the sensitive information, the additional
tool {\tt yosys-filterlib} (see Sec.~\ref{sec:filterlib}) can be used to strip
the sensitive information from the Liberty file.