| 1 |
/* |
| 2 |
* nextpnr -- Next Generation Place and Route |
| 3 |
* |
| 4 |
* Copyright (C) 2024 The Project Beyond Authors. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 7 |
* purpose with or without fee is hereby granted, provided that the above |
| 8 |
* copyright notice and this permission notice appear in all copies. |
| 9 |
* |
| 10 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <vector> |
| 21 |
#include "command.h" |
| 22 |
#include "gtest/gtest.h" |
| 23 |
#include "nextpnr.h" |
| 24 |
#include "uarch/ng-ultra/ng_ultra.h" |
| 25 |
#include "uarch/ng-ultra/pack.h" |
| 26 |
#define HIMBAECHEL_CONSTIDS "uarch/ng-ultra/constids.inc" |
| 27 |
#include "himbaechel_constids.h" |
| 28 |
|
| 29 |
USING_NEXTPNR_NAMESPACE |
| 30 |
|
| 31 |
class NGUltraLutDffTest : public ::testing::Test |
| 32 |
{ |
| 33 |
protected: |
| 34 |
virtual void SetUp() |
| 35 |
{ |
| 36 |
init_share_dirname(); |
| 37 |
chipArgs.device = "NG-ULTRA"; |
| 38 |
ctx = new Context(chipArgs); |
| 39 |
ctx->uarch->init(ctx); |
| 40 |
ctx->late_init(); |
| 41 |
impl = (NgUltraImpl *)(ctx->uarch.get()); |
| 42 |
} |
| 43 |
|
| 44 |
virtual void TearDown() { delete ctx; } |
| 45 |
|
| 46 |
int const_autoidx = 0; |
| 47 |
NetInfo *add_constant_driver(const char *name, char constval) |
| 48 |
{ |
| 49 |
IdString cell_name = ctx->idf("%s%s%d", name, (constval == '1' ? "$VCC$" : "$GND$"), const_autoidx++); |
| 50 |
CellInfo *cc = ctx->createCell(cell_name, ctx->id(constval == '1' ? "VCC" : "GND")); |
| 51 |
cc->ports[ctx->id("Y")].name = ctx->id("Y"); |
| 52 |
cc->ports[ctx->id("Y")].type = PORT_OUT; |
| 53 |
NetInfo *net = ctx->createNet(cell_name); |
| 54 |
cc->connectPort(ctx->id("Y"), net); |
| 55 |
return net; |
| 56 |
} |
| 57 |
|
| 58 |
void add_port(CellInfo *cell, const std::string &name, PortType dir) |
| 59 |
{ |
| 60 |
IdString id = ctx->id(name); |
| 61 |
cell->ports[id].name = id; |
| 62 |
cell->ports[id].type = dir; |
| 63 |
}; |
| 64 |
|
| 65 |
int evaluate_lut(int I1, int I2, int I3, int I4, int lut_table) |
| 66 |
{ |
| 67 |
int S1 = I4 ? (lut_table >> 8) & 0xff : lut_table & 0xff; |
| 68 |
int S2 = I3 ? (S1 >> 4 & 0xf) : S1 & 0xf; |
| 69 |
int S3 = I2 ? (S2 >> 2 & 0x3) : S2 & 0x3; |
| 70 |
int O = I1 ? (S3 >> 1 & 0x1) : S3 & 0x1; |
| 71 |
return O; |
| 72 |
} |
| 73 |
|
| 74 |
ArchArgs chipArgs; |
| 75 |
Context *ctx; |
| 76 |
NgUltraImpl *impl; |
| 77 |
}; |
| 78 |
|
| 79 |
TEST_F(NGUltraLutDffTest, pack_constants) |
| 80 |
{ |
| 81 |
NgUltraPacker packer(ctx, impl); |
| 82 |
packer.pack_constants(); |
| 83 |
ASSERT_EQ(ctx->cells.size(), 2LU); |
| 84 |
} |
| 85 |
|
| 86 |
TEST_F(NGUltraLutDffTest, remove_constants) |
| 87 |
{ |
| 88 |
NgUltraPacker packer(ctx, impl); |
| 89 |
packer.pack_constants(); |
| 90 |
impl->remove_constants(); |
| 91 |
ASSERT_EQ(ctx->cells.size(), 0LU); |
| 92 |
} |
| 93 |
|
| 94 |
TEST_F(NGUltraLutDffTest, remove_unused_gnd) |
| 95 |
{ |
| 96 |
NgUltraPacker packer(ctx, impl); |
| 97 |
CellInfo *cell = ctx->createCell(ctx->id("TEST"), id_NX_LUT); |
| 98 |
add_port(cell, "I1", PORT_IN); |
| 99 |
add_port(cell, "I2", PORT_IN); |
| 100 |
add_port(cell, "I3", PORT_IN); |
| 101 |
add_port(cell, "I4", PORT_IN); |
| 102 |
cell->connectPort(id_I1, add_constant_driver("TEST", '1')); |
| 103 |
cell->connectPort(id_I2, add_constant_driver("TEST", '1')); |
| 104 |
cell->connectPort(id_I3, add_constant_driver("TEST", '1')); |
| 105 |
|
| 106 |
ASSERT_EQ(ctx->cells.size(), 4LU); |
| 107 |
packer.pack_constants(); |
| 108 |
ASSERT_EQ(ctx->cells.size(), 3LU); |
| 109 |
impl->remove_constants(); |
| 110 |
ASSERT_EQ(ctx->cells.size(), 2LU); |
| 111 |
ASSERT_EQ(ctx->cells.find(ctx->id("$PACKER_GND_DRV")), ctx->cells.end()); |
| 112 |
ASSERT_NE(ctx->cells.find(ctx->id("$PACKER_VCC_DRV")), ctx->cells.end()); |
| 113 |
ASSERT_EQ(ctx->nets.find(ctx->id("$PACKER_GND")), ctx->nets.end()); |
| 114 |
ASSERT_NE(ctx->nets.find(ctx->id("$PACKER_VCC")), ctx->nets.end()); |
| 115 |
} |
| 116 |
|
| 117 |
TEST_F(NGUltraLutDffTest, remove_unused_vcc) |
| 118 |
{ |
| 119 |
NgUltraPacker packer(ctx, impl); |
| 120 |
CellInfo *cell = ctx->createCell(ctx->id("TEST"), id_NX_LUT); |
| 121 |
add_port(cell, "I1", PORT_IN); |
| 122 |
add_port(cell, "I2", PORT_IN); |
| 123 |
add_port(cell, "I3", PORT_IN); |
| 124 |
add_port(cell, "I4", PORT_IN); |
| 125 |
cell->connectPort(id_I1, add_constant_driver("TEST", '0')); |
| 126 |
cell->connectPort(id_I2, add_constant_driver("TEST", '0')); |
| 127 |
cell->connectPort(id_I3, add_constant_driver("TEST", '0')); |
| 128 |
|
| 129 |
ASSERT_EQ(ctx->cells.size(), 4LU); |
| 130 |
packer.pack_constants(); |
| 131 |
ASSERT_EQ(ctx->cells.size(), 3LU); |
| 132 |
impl->remove_constants(); |
| 133 |
ASSERT_EQ(ctx->cells.size(), 2LU); |
| 134 |
ASSERT_NE(ctx->cells.find(ctx->id("$PACKER_GND_DRV")), ctx->cells.end()); |
| 135 |
ASSERT_EQ(ctx->cells.find(ctx->id("$PACKER_VCC_DRV")), ctx->cells.end()); |
| 136 |
ASSERT_NE(ctx->nets.find(ctx->id("$PACKER_GND")), ctx->nets.end()); |
| 137 |
ASSERT_EQ(ctx->nets.find(ctx->id("$PACKER_VCC")), ctx->nets.end()); |
| 138 |
} |
| 139 |
|
| 140 |
TEST_F(NGUltraLutDffTest, make_init_with_const_input) |
| 141 |
{ |
| 142 |
NgUltraPacker packer(ctx, impl); |
| 143 |
for (int lut_table = 0; lut_table < 0x10000; lut_table++) { |
| 144 |
for (int lut = 0; lut < 16; lut++) { |
| 145 |
int I4 = (lut & 8) ? 1 : 0; |
| 146 |
int I3 = (lut & 4) ? 1 : 0; |
| 147 |
int I2 = (lut & 2) ? 1 : 0; |
| 148 |
int I1 = (lut & 1) ? 1 : 0; |
| 149 |
|
| 150 |
int tab1 = packer.make_init_with_const_input(lut_table, 0, I1); |
| 151 |
int tab2 = packer.make_init_with_const_input(tab1, 1, I2); |
| 152 |
int tab3 = packer.make_init_with_const_input(tab2, 2, I3); |
| 153 |
int tab4 = packer.make_init_with_const_input(tab3, 3, I4); |
| 154 |
|
| 155 |
ASSERT_EQ(evaluate_lut(I1, I2, I3, I4, lut_table), evaluate_lut(I1, I2, I3, I4, tab1)); |
| 156 |
ASSERT_EQ(evaluate_lut(I1, I2, I3, I4, lut_table), evaluate_lut(I1, I2, I3, I4, tab2)); |
| 157 |
ASSERT_EQ(evaluate_lut(I1, I2, I3, I4, lut_table), evaluate_lut(I1, I2, I3, I4, tab3)); |
| 158 |
ASSERT_EQ(evaluate_lut(I1, I2, I3, I4, lut_table), evaluate_lut(I1, I2, I3, I4, tab4)); |
| 159 |
} |
| 160 |
} |
| 161 |
} |