File size: 8,873 Bytes
829b19c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | package nand
import chisel3._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// TEST HARNESSES β ChiselTest verification for all NAND-derived
// modules. Truth tables match SPICE circuit simulation results.
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Nand2Test extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "Nand2"
it should "match NAND truth table" in {
test(new Nand2) { dut =>
for (a <- Seq(false, true); b <- Seq(false, true)) {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.clock.step(1)
dut.io.y.expect(!(a && b).B)
}
}
}
}
class Nand3Test extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "Nand3"
it should "match 3-input NAND truth table" in {
test(new Nand3) { dut =>
for {
a <- Seq(false, true)
b <- Seq(false, true)
c <- Seq(false, true)
} {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.io.c.poke(c.B)
dut.clock.step(1)
dut.io.y.expect(!(a && b && c).B)
}
}
}
}
class InvFromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "InvFromNand"
it should "invert" in {
test(new InvFromNand) { dut =>
dut.io.a.poke(false.B); dut.clock.step(1); dut.io.y.expect(true.B)
dut.io.a.poke(true.B); dut.clock.step(1); dut.io.y.expect(false.B)
}
}
}
class And2FromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "And2FromNand"
it should "match AND truth table" in {
test(new And2FromNand) { dut =>
for (a <- Seq(false, true); b <- Seq(false, true)) {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.clock.step(1)
dut.io.y.expect((a && b).B)
}
}
}
}
class Or2FromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "Or2FromNand"
it should "match OR truth table" in {
test(new Or2FromNand) { dut =>
for (a <- Seq(false, true); b <- Seq(false, true)) {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.clock.step(1)
dut.io.y.expect((a || b).B)
}
}
}
}
class Xor2FromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "Xor2FromNand"
it should "match XOR truth table" in {
test(new Xor2FromNand) { dut =>
for (a <- Seq(false, true); b <- Seq(false, true)) {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.clock.step(1)
dut.io.y.expect((a ^ b).B)
}
}
}
}
class Xnor2FromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "Xnor2FromNand"
it should "match XNOR truth table" in {
test(new Xnor2FromNand) { dut =>
for (a <- Seq(false, true); b <- Seq(false, true)) {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.clock.step(1)
dut.io.y.expect(!(a ^ b).B)
}
}
}
}
class Mux2FromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "Mux2FromNand"
it should "select a when sel=0, b when sel=1" in {
test(new Mux2FromNand) { dut =>
for (sel <- Seq(false, true)) {
for (a <- Seq(false, true); b <- Seq(false, true)) {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.io.sel.poke(sel.B)
dut.clock.step(1)
dut.io.y.expect((if (sel) b else a).B)
}
}
}
}
}
// βββ Sequential tests βββββββββββββββββββββββββββββββββββββββββββββ
class SrLatchFromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "SrLatchFromNand"
it should "SET, HOLD, RESET correctly" in {
test(new SrLatchFromNand) { dut =>
// Initial: hold (both inactive-high)
dut.io.s_n.poke(true.B)
dut.io.r_n.poke(true.B)
dut.clock.step(1)
// SET: S'=0
dut.io.s_n.poke(false.B)
dut.clock.step(2)
dut.io.q.expect(true.B)
dut.io.qbar.expect(false.B)
// HOLD
dut.io.s_n.poke(true.B)
dut.io.r_n.poke(true.B)
dut.clock.step(2)
dut.io.q.expect(true.B)
// RESET: R'=0
dut.io.r_n.poke(false.B)
dut.clock.step(2)
dut.io.q.expect(false.B)
dut.io.qbar.expect(true.B)
// HOLD after reset
dut.io.r_n.poke(true.B)
dut.clock.step(2)
dut.io.q.expect(false.B)
}
}
}
class DFlipFlopFromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "DFlipFlopFromNand"
it should "capture D on rising clock edge" in {
test(new DFlipFlopFromNand) { dut =>
// Set D=1, clock low β nothing captured
dut.io.d.poke(true.B)
dut.io.clk.poke(false.B)
dut.clock.step(2)
// Rising edge β capture D=1
dut.io.clk.poke(true.B)
dut.clock.step(2)
dut.io.q.expect(true.B)
// Change D while clock high β Q holds
dut.io.d.poke(false.B)
dut.clock.step(2)
dut.io.q.expect(true.B)
// Falling edge β Q still holds
dut.io.clk.poke(false.B)
dut.clock.step(2)
dut.io.q.expect(true.B)
// Next rising edge with D=0 β capture 0
dut.io.clk.poke(true.B)
dut.clock.step(2)
dut.io.q.expect(false.B)
}
}
}
// βββ Arithmetic tests βββββββββββββββββββββββββββββββββββββββββββββ
class HalfAdderFromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "HalfAdderFromNand"
it should "match half-adder truth table" in {
test(new HalfAdderFromNand) { dut =>
Map(
(false, false) -> (false, false),
(false, true) -> (true, false),
(true, false) -> (true, false),
(true, true) -> (false, true),
).foreach { case ((a, b), (sum, cout)) =>
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.clock.step(1)
dut.io.sum.expect(sum.B)
dut.io.cout.expect(cout.B)
}
}
}
}
class FullAdderFromNandTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "FullAdderFromNand"
it should "match full-adder truth table" in {
test(new FullAdderFromNand) { dut =>
for {
a <- Seq(false, true)
b <- Seq(false, true)
cin <- Seq(false, true)
} {
dut.io.a.poke(a.B)
dut.io.b.poke(b.B)
dut.io.cin.poke(cin.B)
dut.clock.step(1)
val total = (if (a) 1 else 0) + (if (b) 1 else 0) + (if (cin) 1 else 0)
dut.io.sum.expect((total % 2 == 1).B)
dut.io.cout.expect((total >= 2).B)
}
}
}
}
class RippleCarryAdder8Test extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "RippleCarryAdder8"
it should "add correctly for various operand pairs" in {
test(new RippleCarryAdder(8)) { dut =>
val testCases = Seq(
(0, 0, 0),
(1, 1, 0),
(42, 73, 0),
(127, 128 % 256, 0),
(200, 55, 0),
(255, 1, 0),
(255, 255, 0),
(255, 255, 1),
)
for ((a, b, cin) <- testCases) {
dut.io.a.poke(a.U)
dut.io.b.poke(b.U)
dut.io.cin.poke((cin == 1).B)
dut.clock.step(1)
val expected = (a + b + cin) % 256
val carryOut = (a + b + cin) >= 256
dut.io.sum.expect(expected.U)
dut.io.cout.expect(carryOut.B)
}
}
}
}
class AluFromNand8Test extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior of "AluFromNand8"
it should "perform AND, OR, XOR, ADD correctly" in {
test(new AluFromNand(8)) { dut =>
// AND: op=00
dut.io.op.poke(0.U)
dut.io.a.poke(0xFF.U); dut.io.b.poke(0x55.U)
dut.clock.step(1)
dut.io.y.expect(0x55.U)
// OR: op=01
dut.io.op.poke(1.U)
dut.io.a.poke(0x0F.U); dut.io.b.poke(0xF0.U)
dut.clock.step(1)
dut.io.y.expect(0xFF.U)
// XOR: op=10
dut.io.op.poke(2.U)
dut.io.a.poke(0xFF.U); dut.io.b.poke(0x55.U)
dut.clock.step(1)
dut.io.y.expect(0xAA.U)
// ADD: op=11
dut.io.op.poke(3.U)
dut.io.cin.poke(false.B)
dut.io.a.poke(42.U); dut.io.b.poke(73.U)
dut.clock.step(1)
dut.io.y.expect(115.U)
}
}
}
|