Building the simplest possible hardware: An AND gate
Every piece of hardware does one thing: it takes electrical signals IN and produces electrical signals OUT. That's it. An FPGA lets you define what happens between IN and OUT.
Let's build an AND gate. It has 2 inputs and 1 output. Output is HIGH only when BOTH inputs are HIGH.
A B | Y --------+---- 0 0 | 0 0 1 | 0 1 0 | 0 1 1 | 1
Create a file called and_gate.v in your project:
// This is your first hardware module // It describes a 2-input AND gate module and_gate ( input a, // First input wire input b, // Second input wire output y // Output wire ); // This single line describes the AND operation // 'assign' means: "wire y is ALWAYS connected to (a AND b)" assign y = a & b; endmodule
Create and_gate.cst to map your module to physical pins on the Tang Primer 20K:
//=========================================== // Pin Constraints for AND Gate // Tang Primer 20K (GW2A-18C) //=========================================== // Button S1 → Input A IO_LOC "a" T3; IO_PORT "a" PULL_MODE=UP IO_TYPE=LVCMOS33; // LED 0 → Input B (we'll use LED as input for testing) IO_LOC "b" L16; IO_PORT "b" IO_TYPE=LVCMOS33; // LED 1 → Output Y IO_LOC "y" L14; IO_PORT "y" DRIVE=8 IO_TYPE=LVCMOS33;
and_gate.v as source fileand_gate.cst as constraint fileGW2A-LV18PG256C8/I7